basic executor / command mechanism

This commit is contained in:
zadam
2020-02-15 10:41:21 +01:00
parent 6d847d22d3
commit e1bf4dcbc9
6 changed files with 97 additions and 52 deletions

View File

@@ -1,7 +1,7 @@
import server from "./server.js";
import treeCache from "./tree_cache.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 options from "./options.js";
import utils from "./utils.js";
@@ -15,6 +15,7 @@ class AppContext {
this.layout = layout;
this.tabManager = new TabManager(this);
this.components = [];
this.executors = [];
}
async start() {
@@ -42,8 +43,11 @@ class AppContext {
this.components = [
this.tabManager,
rootContainer,
new Entrypoints(this),
new DialogEventComponent(this)
new Entrypoints(this)
];
this.executors = [
new DialogCommandExecutor(this, this)
];
if (utils.isElectron()) {
@@ -80,6 +84,30 @@ class AppContext {
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();

View File

@@ -1,39 +1,39 @@
import Component from "../widgets/component.js";
export default class DialogEventComponent extends Component {
jumpToNoteListener() {
export default class DialogCommandExecutor extends Component {
jumpToNoteCommand() {
import("../dialogs/jump_to_note.js").then(d => d.showDialog());
}
showRecentChangesListener() {
showRecentChangesCommand() {
import("../dialogs/recent_changes.js").then(d => d.showDialog());
}
showAttributesListener() {
showAttributesCommand() {
import("../dialogs/attributes.js").then(d => d.showDialog());
}
showNoteInfoListener() {
showNoteInfoCommand() {
import("../dialogs/note_info.js").then(d => d.showDialog());
}
showNoteRevisionsListener() {
showNoteRevisionsCommand() {
import("../dialogs/note_revisions.js").then(d => d.showCurrentNoteRevisions());
}
showNoteSourceListener() {
showNoteSourceCommand() {
import("../dialogs/note_source.js").then(d => d.showDialog());
}
showLinkMapListener() {
showLinkMapCommand() {
import("../dialogs/link_map.js").then(d => d.showDialog());
}
pasteMarkdownIntoTextListener() {
pasteMarkdownIntoTextCommand() {
import("../dialogs/markdown_import.js").then(d => d.importMarkdownInline());
}
async cloneNotesToListener() {
async cloneNotesToCommand() {
// FIXME
const selectedOrActiveNodes = this.appContext.getMainNoteTree().getSelectedOrActiveNodes();
@@ -43,15 +43,7 @@ export default class DialogEventComponent extends Component {
d.showDialog(noteIds);
}
async moveNotesToListener() {
// FIXME
const selectedOrActiveNodes = this.appContext.getMainNoteTree().getSelectedOrActiveNodes();
const d = await import("../dialogs/move_to.js");
d.showDialog(selectedOrActiveNodes);
}
async editBranchPrefixListener() {
async editBranchPrefixCommand() {
const notePath = this.appContext.tabManager.getActiveTabNotePath();
if (notePath) {
@@ -60,7 +52,12 @@ export default class DialogEventComponent extends Component {
}
}
addLinkToTextListener() {
addLinkToTextCommand() {
import("../dialogs/add_link.js").then(d => d.showDialog());
}
async moveBranchIdsToCommand({branchIds}) {
const d = await import("../dialogs/move_to.js");
d.showDialog(branchIds);
}
}

View File

@@ -149,9 +149,7 @@ class TreeContextMenu {
clipboard.cut(this.getSelectedOrActiveBranchIds());
}
else if (cmd === "moveTo") {
const nodes = this.treeWidget.getSelectedOrActiveNodes(this.node);
import("../dialogs/move_to.js").then(d => d.showDialog(nodes));
this.treeWidget.triggerCommand('moveNotesTo');
}
else if (cmd === "pasteAfter") {
clipboard.pasteAfter(this.node.data.branchId);