mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 02:16:05 +01:00 
			
		
		
		
	basic executor / command mechanism
This commit is contained in:
		| @@ -4,16 +4,17 @@ import toastService from "../services/toast.js"; | |||||||
| import treeCache from "../services/tree_cache.js"; | import treeCache from "../services/tree_cache.js"; | ||||||
| import treeChangesService from "../services/branches.js"; | import treeChangesService from "../services/branches.js"; | ||||||
| import appContext from "../services/app_context.js"; | import appContext from "../services/app_context.js"; | ||||||
|  | import treeService from "../services/tree.js"; | ||||||
|  |  | ||||||
| const $dialog = $("#move-to-dialog"); | const $dialog = $("#move-to-dialog"); | ||||||
| const $form = $("#move-to-form"); | const $form = $("#move-to-form"); | ||||||
| const $noteAutoComplete = $("#move-to-note-autocomplete"); | const $noteAutoComplete = $("#move-to-note-autocomplete"); | ||||||
| const $noteList = $("#move-to-note-list"); | const $noteList = $("#move-to-note-list"); | ||||||
|  |  | ||||||
| let movedNodes; | let movedBranchIds; | ||||||
|  |  | ||||||
| export async function showDialog(nodes) { | export async function showDialog(branchIds) { | ||||||
|     movedNodes = nodes; |     movedBranchIds = branchIds; | ||||||
|  |  | ||||||
|     utils.openDialog($dialog); |     utils.openDialog($dialog); | ||||||
|  |  | ||||||
| @@ -21,8 +22,9 @@ export async function showDialog(nodes) { | |||||||
|  |  | ||||||
|     $noteList.empty(); |     $noteList.empty(); | ||||||
|  |  | ||||||
|     for (const node of movedNodes) { |     for (const branchId of movedBranchIds) { | ||||||
|         const note = await treeCache.getNote(node.data.noteId); |         const branch = treeCache.getBranch(branchId); | ||||||
|  |         const note = await treeCache.getNote(branch.noteId); | ||||||
|  |  | ||||||
|         $noteList.append($("<li>").text(note.title)); |         $noteList.append($("<li>").text(note.title)); | ||||||
|     } |     } | ||||||
| @@ -31,13 +33,12 @@ export async function showDialog(nodes) { | |||||||
|     noteAutocompleteService.showRecentNotes($noteAutoComplete); |     noteAutocompleteService.showRecentNotes($noteAutoComplete); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function moveNotesTo(notePath) { | async function moveNotesTo(parentNoteId) { | ||||||
|     // FIXME |     await treeChangesService.moveToParentNote(movedBranchIds, parentNoteId); | ||||||
|     const targetNode = await appContext.getMainNoteTree().getNodeFromPath(notePath); |  | ||||||
|  |  | ||||||
|     await treeChangesService.moveToParentNote(movedNodes, targetNode); |     const parentNote = await treeCache.getNote(parentNoteId); | ||||||
|  |  | ||||||
|     toastService.showMessage(`Selected notes have been moved into ${targetNode.title}`); |     toastService.showMessage(`Selected notes have been moved into ${parentNote.title}`); | ||||||
| } | } | ||||||
|  |  | ||||||
| $form.on('submit', () => { | $form.on('submit', () => { | ||||||
| @@ -46,7 +47,9 @@ $form.on('submit', () => { | |||||||
|     if (notePath) { |     if (notePath) { | ||||||
|         $dialog.modal('hide'); |         $dialog.modal('hide'); | ||||||
|  |  | ||||||
|         moveNotesTo(notePath); |         const noteId = treeService.getNoteIdFromNotePath(notePath); | ||||||
|  |  | ||||||
|  |         moveNotesTo(noteId); | ||||||
|     } |     } | ||||||
|     else { |     else { | ||||||
|         console.error("No path to move to."); |         console.error("No path to move to."); | ||||||
|   | |||||||
| @@ -1,7 +1,7 @@ | |||||||
| import server from "./server.js"; | import server from "./server.js"; | ||||||
| import treeCache from "./tree_cache.js"; | import treeCache from "./tree_cache.js"; | ||||||
| import bundleService from "./bundle.js"; | import bundleService from "./bundle.js"; | ||||||
| import DialogEventComponent from "./dialog_events.js"; | import DialogCommandExecutor from "./dialog_command_executor.js"; | ||||||
| import Entrypoints from "./entrypoints.js"; | import Entrypoints from "./entrypoints.js"; | ||||||
| import options from "./options.js"; | import options from "./options.js"; | ||||||
| import utils from "./utils.js"; | import utils from "./utils.js"; | ||||||
| @@ -15,6 +15,7 @@ class AppContext { | |||||||
|         this.layout = layout; |         this.layout = layout; | ||||||
|         this.tabManager = new TabManager(this); |         this.tabManager = new TabManager(this); | ||||||
|         this.components = []; |         this.components = []; | ||||||
|  |         this.executors = []; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     async start() { |     async start() { | ||||||
| @@ -42,8 +43,11 @@ class AppContext { | |||||||
|         this.components = [ |         this.components = [ | ||||||
|             this.tabManager, |             this.tabManager, | ||||||
|             rootContainer, |             rootContainer, | ||||||
|             new Entrypoints(this), |             new Entrypoints(this) | ||||||
|             new DialogEventComponent(this) |         ]; | ||||||
|  |  | ||||||
|  |         this.executors = [ | ||||||
|  |             new DialogCommandExecutor(this, this) | ||||||
|         ]; |         ]; | ||||||
|  |  | ||||||
|         if (utils.isElectron()) { |         if (utils.isElectron()) { | ||||||
| @@ -80,6 +84,30 @@ class AppContext { | |||||||
|  |  | ||||||
|         this.trigger('treeCacheReloaded'); |         this.trigger('treeCacheReloaded'); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     async triggerCommand(name, data) { | ||||||
|  |         for (const executor of this.executors) { | ||||||
|  |             const fun = executor[name + 'Command']; | ||||||
|  |  | ||||||
|  |             const called = await this.callMethod(executor, fun, data); | ||||||
|  |  | ||||||
|  |             if (called) { | ||||||
|  |                 return; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         console.error(`Unhandled command ${name}`); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     async callMethod(thiz, fun, data) { | ||||||
|  |         if (typeof fun !== 'function') { | ||||||
|  |             return false; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         await fun.call(thiz, data); | ||||||
|  |  | ||||||
|  |         return true; | ||||||
|  |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| const layout = new Layout(); | const layout = new Layout(); | ||||||
|   | |||||||
| @@ -1,39 +1,39 @@ | |||||||
| import Component from "../widgets/component.js"; | import Component from "../widgets/component.js"; | ||||||
| 
 | 
 | ||||||
| export default class DialogEventComponent extends Component { | export default class DialogCommandExecutor extends Component { | ||||||
|     jumpToNoteListener() { |     jumpToNoteCommand() { | ||||||
|         import("../dialogs/jump_to_note.js").then(d => d.showDialog()); |         import("../dialogs/jump_to_note.js").then(d => d.showDialog()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     showRecentChangesListener() { |     showRecentChangesCommand() { | ||||||
|         import("../dialogs/recent_changes.js").then(d => d.showDialog()); |         import("../dialogs/recent_changes.js").then(d => d.showDialog()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     showAttributesListener() { |     showAttributesCommand() { | ||||||
|         import("../dialogs/attributes.js").then(d => d.showDialog()); |         import("../dialogs/attributes.js").then(d => d.showDialog()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     showNoteInfoListener() { |     showNoteInfoCommand() { | ||||||
|         import("../dialogs/note_info.js").then(d => d.showDialog()); |         import("../dialogs/note_info.js").then(d => d.showDialog()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     showNoteRevisionsListener() { |     showNoteRevisionsCommand() { | ||||||
|         import("../dialogs/note_revisions.js").then(d => d.showCurrentNoteRevisions()); |         import("../dialogs/note_revisions.js").then(d => d.showCurrentNoteRevisions()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     showNoteSourceListener() { |     showNoteSourceCommand() { | ||||||
|         import("../dialogs/note_source.js").then(d => d.showDialog()); |         import("../dialogs/note_source.js").then(d => d.showDialog()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     showLinkMapListener() { |     showLinkMapCommand() { | ||||||
|         import("../dialogs/link_map.js").then(d => d.showDialog()); |         import("../dialogs/link_map.js").then(d => d.showDialog()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     pasteMarkdownIntoTextListener() { |     pasteMarkdownIntoTextCommand() { | ||||||
|         import("../dialogs/markdown_import.js").then(d => d.importMarkdownInline()); |         import("../dialogs/markdown_import.js").then(d => d.importMarkdownInline()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     async cloneNotesToListener() { |     async cloneNotesToCommand() { | ||||||
|         // FIXME
 |         // FIXME
 | ||||||
|         const selectedOrActiveNodes = this.appContext.getMainNoteTree().getSelectedOrActiveNodes(); |         const selectedOrActiveNodes = this.appContext.getMainNoteTree().getSelectedOrActiveNodes(); | ||||||
| 
 | 
 | ||||||
| @@ -43,15 +43,7 @@ export default class DialogEventComponent extends Component { | |||||||
|         d.showDialog(noteIds); |         d.showDialog(noteIds); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     async moveNotesToListener() { |     async editBranchPrefixCommand() { | ||||||
|         // FIXME
 |  | ||||||
|         const selectedOrActiveNodes = this.appContext.getMainNoteTree().getSelectedOrActiveNodes(); |  | ||||||
| 
 |  | ||||||
|         const d = await import("../dialogs/move_to.js"); |  | ||||||
|         d.showDialog(selectedOrActiveNodes); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     async editBranchPrefixListener() { |  | ||||||
|         const notePath = this.appContext.tabManager.getActiveTabNotePath(); |         const notePath = this.appContext.tabManager.getActiveTabNotePath(); | ||||||
| 
 | 
 | ||||||
|         if (notePath) { |         if (notePath) { | ||||||
| @@ -60,7 +52,12 @@ export default class DialogEventComponent extends Component { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     addLinkToTextListener() { |     addLinkToTextCommand() { | ||||||
|         import("../dialogs/add_link.js").then(d => d.showDialog()); |         import("../dialogs/add_link.js").then(d => d.showDialog()); | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     async moveBranchIdsToCommand({branchIds}) { | ||||||
|  |         const d = await import("../dialogs/move_to.js"); | ||||||
|  |         d.showDialog(branchIds); | ||||||
|  |     } | ||||||
| } | } | ||||||
| @@ -149,9 +149,7 @@ class TreeContextMenu { | |||||||
|             clipboard.cut(this.getSelectedOrActiveBranchIds()); |             clipboard.cut(this.getSelectedOrActiveBranchIds()); | ||||||
|         } |         } | ||||||
|         else if (cmd === "moveTo") { |         else if (cmd === "moveTo") { | ||||||
|             const nodes = this.treeWidget.getSelectedOrActiveNodes(this.node); |             this.treeWidget.triggerCommand('moveNotesTo'); | ||||||
|  |  | ||||||
|             import("../dialogs/move_to.js").then(d => d.showDialog(nodes)); |  | ||||||
|         } |         } | ||||||
|         else if (cmd === "pasteAfter") { |         else if (cmd === "pasteAfter") { | ||||||
|             clipboard.pasteAfter(this.node.data.branchId); |             clipboard.pasteAfter(this.node.data.branchId); | ||||||
|   | |||||||
| @@ -27,20 +27,7 @@ export default class Component { | |||||||
|  |  | ||||||
|         const start = Date.now(); |         const start = Date.now(); | ||||||
|  |  | ||||||
|         if (typeof fun === 'function') { |         await this.callMethod(fun, data); | ||||||
|             let release; |  | ||||||
|  |  | ||||||
|             try { |  | ||||||
|                 release = await this.mutex.acquire(); |  | ||||||
|  |  | ||||||
|                 await fun.call(this, data); |  | ||||||
|             } |  | ||||||
|             finally { |  | ||||||
|                 if (release) { |  | ||||||
|                     release(); |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         const end = Date.now(); |         const end = Date.now(); | ||||||
|  |  | ||||||
| @@ -65,7 +52,33 @@ export default class Component { | |||||||
|         await Promise.all(promises); |         await Promise.all(promises); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     triggerCommand(name, data) { |     async triggerCommand(name, data) { | ||||||
|  |         const fun = this[name + 'Command']; | ||||||
|  |  | ||||||
|  |         const called = await this.callMethod(fun, data); | ||||||
|  |  | ||||||
|  |         if (!called) { | ||||||
|  |             await this.parent.triggerCommand(name, data); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     async callMethod(fun, data) { | ||||||
|  |         if (typeof fun !== 'function') { | ||||||
|  |             return false; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         let release; | ||||||
|  |  | ||||||
|  |         try { | ||||||
|  |             release = await this.mutex.acquire(); | ||||||
|  |  | ||||||
|  |             await fun.call(this, data); | ||||||
|  |  | ||||||
|  |             return true; | ||||||
|  |         } finally { | ||||||
|  |             if (release) { | ||||||
|  |                 release(); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @@ -616,4 +616,10 @@ export default class NoteTreeWidget extends TabAwareWidget { | |||||||
|     treeCacheReloadedListener() { |     treeCacheReloadedListener() { | ||||||
|         this.reloadTreeFromCache(); |         this.reloadTreeFromCache(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     async moveNotesToCommand() { | ||||||
|  |         const selectedOrActiveBranchIds = this.getSelectedOrActiveNodes().map(node => node.data.branchId); | ||||||
|  |  | ||||||
|  |         this.triggerCommand('moveBranchIdsTo', {branchIds: selectedOrActiveBranchIds}); | ||||||
|  |     } | ||||||
| } | } | ||||||
		Reference in New Issue
	
	Block a user