You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-07-16 00:14:34 +02:00
Plugins: Improved note change event handling. Also added tests and improved debugging plugins.
This commit is contained in:
@ -37,7 +37,7 @@ export default class Joplin {
|
||||
constructor(implementation: any, plugin: Plugin, store: any) {
|
||||
this.data_ = new JoplinData();
|
||||
this.plugins_ = new JoplinPlugins(plugin);
|
||||
this.workspace_ = new JoplinWorkspace(implementation.workspace, store);
|
||||
this.workspace_ = new JoplinWorkspace(store);
|
||||
this.filters_ = new JoplinFilters();
|
||||
this.commands_ = new JoplinCommands();
|
||||
this.views_ = new JoplinViews(implementation.views, plugin, store);
|
||||
|
@ -1,13 +1,31 @@
|
||||
import { ModelType } from '../../../BaseModel';
|
||||
import eventManager from '../../../eventManager';
|
||||
import makeListener from '../utils/makeListener';
|
||||
import { Disposable } from './types';
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
const Note = require('../../../models/Note');
|
||||
|
||||
enum ItemChangeEventType {
|
||||
Create = 1,
|
||||
Update = 2,
|
||||
Delete = 3,
|
||||
}
|
||||
|
||||
interface ItemChangeEvent {
|
||||
id: string;
|
||||
event: ItemChangeEventType;
|
||||
}
|
||||
|
||||
type ItemChangeHandler = (event: ItemChangeEvent)=> void;
|
||||
|
||||
/**
|
||||
* The workspace service provides access to all the parts of Joplin that are being worked on - i.e. the currently selected notes or notebooks as well
|
||||
* as various related events, such as when a new note is selected, or when the note content changes.
|
||||
* The workspace service provides access to all the parts of Joplin that
|
||||
* are being worked on - i.e. the currently selected notes or notebooks as
|
||||
* well as various related events, such as when a new note is selected, or
|
||||
* when the note content changes.
|
||||
*
|
||||
* [View the demo plugin](https://github.com/laurent22/joplin/tree/dev/packages/app-cli/tests/support/plugins)
|
||||
*/
|
||||
@ -15,45 +33,68 @@ export default class JoplinWorkspace {
|
||||
// TODO: unregister events when plugin is closed or disabled
|
||||
|
||||
private store: any;
|
||||
// private implementation_:any;
|
||||
|
||||
constructor(_implementation: any, store: any) {
|
||||
constructor(store: any) {
|
||||
this.store = store;
|
||||
// this.implementation_ = implementation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a new note or notes are selected.
|
||||
*/
|
||||
async onNoteSelectionChange(callback: Function) {
|
||||
public async onNoteSelectionChange(callback: Function): Promise<Disposable> {
|
||||
eventManager.appStateOn('selectedNoteIds', callback);
|
||||
|
||||
return {};
|
||||
|
||||
// return {
|
||||
// dispose: () => {
|
||||
// eventManager.appStateOff('selectedNoteIds', callback);
|
||||
// }
|
||||
// };
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the content of a note changes.
|
||||
* @deprecated Use `onNoteChange()` instead, which is reliably triggered whenever the note content, or any note property changes.
|
||||
*/
|
||||
public async onNoteContentChange(callback: Function) {
|
||||
eventManager.on('noteContentChange', callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the content of a note changes.
|
||||
*/
|
||||
async onNoteContentChange(callback: Function) {
|
||||
eventManager.on('noteContentChange', callback);
|
||||
public async onNoteChange(handler: ItemChangeHandler): Promise<Disposable> {
|
||||
const wrapperHandler = (event: any) => {
|
||||
if (event.itemType !== ModelType.Note) return;
|
||||
|
||||
handler({
|
||||
id: event.itemId,
|
||||
event: event.eventType,
|
||||
});
|
||||
};
|
||||
|
||||
return makeListener(eventManager, 'itemChange', wrapperHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when an alarm associated with a to-do is triggered.
|
||||
*/
|
||||
async onNoteAlarmTrigger(callback: Function) {
|
||||
eventManager.on('noteAlarmTrigger', callback);
|
||||
public async onNoteAlarmTrigger(callback: Function): Promise<Disposable> {
|
||||
return makeListener(eventManager, 'noteAlarmTrigger', callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the synchronisation process has finished.
|
||||
*/
|
||||
async onSyncComplete(callback: Function) {
|
||||
eventManager.on('syncComplete', callback);
|
||||
public async onSyncComplete(callback: Function): Promise<Disposable> {
|
||||
return makeListener(eventManager, 'syncComplete', callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the currently selected note
|
||||
*/
|
||||
async selectedNote(): Promise<any> {
|
||||
public async selectedNote(): Promise<any> {
|
||||
const noteIds = this.store.getState().selectedNoteIds;
|
||||
if (noteIds.length !== 1) { return null; }
|
||||
return Note.load(noteIds[0]);
|
||||
@ -62,7 +103,7 @@ export default class JoplinWorkspace {
|
||||
/**
|
||||
* Gets the IDs of the selected notes (can be zero, one, or many). Use the data API to retrieve information about these notes.
|
||||
*/
|
||||
async selectedNoteIds(): Promise<string[]> {
|
||||
public async selectedNoteIds(): Promise<string[]> {
|
||||
return this.store.getState().selectedNoteIds.slice();
|
||||
}
|
||||
}
|
||||
|
@ -189,6 +189,10 @@ export interface Script {
|
||||
onStart?(event: any): Promise<void>;
|
||||
}
|
||||
|
||||
export interface Disposable {
|
||||
// dispose():void;
|
||||
}
|
||||
|
||||
// =================================================================
|
||||
// Menu types
|
||||
// =================================================================
|
||||
|
Reference in New Issue
Block a user