refactored existing context menus

This commit is contained in:
zadam 2020-02-29 13:03:05 +01:00
parent 1239293435
commit 724ba352e6
8 changed files with 88 additions and 134 deletions

View File

@ -60,8 +60,6 @@ class AppContext extends Component {
if (utils.isElectron()) { if (utils.isElectron()) {
this.child(new ZoomService()); this.child(new ZoomService());
import("./spell_check.js").then(spellCheckService => spellCheckService.initSpellCheck());
} }
this.triggerEvent('initialRenderComplete'); this.triggerEvent('initialRenderComplete');
@ -106,7 +104,7 @@ $(window).on('beforeunload', () => {
}); });
function isNotePathInAddress() { function isNotePathInAddress() {
const [notePath, tabId] = getHashValueFromAddress(); const [notePath, tabId] = treeService.getHashValueFromAddress();
return notePath.startsWith("root") return notePath.startsWith("root")
// empty string is for empty/uninitialized tab // empty string is for empty/uninitialized tab

View File

@ -70,8 +70,6 @@ class ContextMenu {
this.hide(); this.hide();
e.originalTarget = event.target;
if (item.handler) { if (item.handler) {
item.handler(item, e); item.handler(item, e);
} }

View File

@ -1,5 +1,5 @@
import treeService from './tree.js'; import treeService from './tree.js';
import contextMenuService from "./context_menu.js"; import contextMenu from "./context_menu.js";
import appContext from "./app_context.js"; import appContext from "./app_context.js";
function getNotePathFromUrl(url) { function getNotePathFromUrl(url) {
@ -111,17 +111,16 @@ function newTabContextMenu(e) {
e.preventDefault(); e.preventDefault();
contextMenuService.initContextMenu(e, { contextMenu.show({
getContextMenuItems: () => { x: e.pageX,
return [ y: e.pageY,
{title: "Open note in new tab", cmd: "openNoteInNewTab", uiIcon: "arrow-up-right"} items: [
]; {title: "Open note in new tab", command: "openNoteInNewTab", uiIcon: "arrow-up-right"}
}, ],
selectContextMenuItem: (e, cmd) => { selectMenuItemHandler: ({command}) => {
if (cmd === 'openNoteInNewTab') { if (command === 'openNoteInNewTab') {
const tabContext = appContext.tabManager.openEmptyTab(); const tabContext = appContext.tabManager.openEmptyTab();
tabContext.setNote(notePath); tabContext.setNote(notePath);
appContext.tabManager.activateTab(tabContext.tabId);
} }
} }
}); });

View File

@ -1,47 +0,0 @@
import options from "./options.js";
export async function initSpellCheck() {return;
const {SpellCheckHandler, ContextMenuListener, ContextMenuBuilder} = require('electron-spellchecker');
const {remote, shell} = require('electron');
const spellCheckHandler = new SpellCheckHandler();
// not fully disabling the spellcheck since we want to preserve the context menu
// this will just get rid of the "red squiggles"
if (options.is('spellCheckEnabled')) {
spellCheckHandler.attachToInput();
}
spellCheckHandler.switchLanguage(options.get('spellCheckLanguageCode'));
spellCheckHandler.currentSpellcheckerChanged.subscribe(() => {
console.debug(`Detected language is ${spellCheckHandler.currentSpellcheckerLanguage}`);
spellCheckHandler.currentSpellchecker.add("trilium");
spellCheckHandler.currentSpellchecker.add("https");
spellCheckHandler.currentSpellchecker.add("github");
spellCheckHandler.currentSpellchecker.add("unordered");
});
const contextMenuBuilder = new ContextMenuBuilder(spellCheckHandler, null, true, (menu, menuInfo) => {
// There's no menu.remove(id) so this is a convoluted way of removing the 'Search with Google' menu item
const oldItems = menu.items;
menu.clear();
oldItems.forEach(oldItem => {
if (!oldItem.label.includes('Google')) {
menu.append(oldItem);
} else {
menu.append(new remote.MenuItem({
label: 'Search with DuckDuckGo',
click: () => {
shell.openExternal(`https://duckduckgo.com/?q=${encodeURIComponent(menuInfo.selectionText)}`);
}
}));
}
});
});
new ContextMenuListener(async (info) => {
await contextMenuBuilder.showPopupMenu(info);
});
}

View File

@ -5,6 +5,7 @@ import clipboard from './clipboard.js';
import protectedSessionHolder from "./protected_session_holder.js"; import protectedSessionHolder from "./protected_session_holder.js";
import appContext from "./app_context.js"; import appContext from "./app_context.js";
import noteCreateService from "./note_create.js"; import noteCreateService from "./note_create.js";
import contextMenu from "./context_menu.js";
class TreeContextMenu { class TreeContextMenu {
/** /**
@ -16,18 +17,27 @@ class TreeContextMenu {
this.node = node; this.node = node;
} }
getNoteTypeItems(baseCmd) { async show(e) {
contextMenu.show({
x: e.pageX,
y: e.pageY,
items: await this.getMenuItems(),
selectMenuItemHandler: (item, e) => this.selectMenuItemHandler(item, e)
})
}
getNoteTypeItems(command) {
return [ return [
{ title: "Text", cmd: baseCmd + "_text", uiIcon: "note" }, { title: "Text", command: command, type: "text", uiIcon: "note" },
{ title: "Code", cmd: baseCmd + "_code", uiIcon: "code" }, { title: "Code", command: command, type: "code", uiIcon: "code" },
{ title: "Saved search", cmd: baseCmd + "_search", uiIcon: "file-find" }, { title: "Saved search", command: command, type: "search", uiIcon: "file-find" },
{ title: "Relation Map", cmd: baseCmd + "_relation-map", uiIcon: "map-alt" }, { title: "Relation Map", command: command, type: "relation-map", uiIcon: "map-alt" },
{ title: "Render HTML note", cmd: baseCmd + "_render", uiIcon: "extension" }, { title: "Render HTML note", command: command, type: "render", uiIcon: "extension" },
{ title: "Book", cmd: baseCmd + "_book", uiIcon: "book" } { title: "Book", command: command, type: "book", uiIcon: "book" }
]; ];
} }
async getContextMenuItems() { async getMenuItems() {
const note = await treeCache.getNote(this.node.data.noteId); const note = await treeCache.getNote(this.node.data.noteId);
const branch = treeCache.getBranch(this.node.data.branchId); const branch = treeCache.getBranch(this.node.data.branchId);
const parentNote = await treeCache.getNote(branch.parentNoteId); const parentNote = await treeCache.getNote(branch.parentNoteId);
@ -46,66 +56,65 @@ class TreeContextMenu {
const insertNoteAfterEnabled = isNotRoot && !isHoisted && parentNotSearch; const insertNoteAfterEnabled = isNotRoot && !isHoisted && parentNotSearch;
return [ return [
{ title: 'Open in new tab', cmd: "openInTab", uiIcon: "empty", enabled: noSelectedNotes }, { title: 'Open in new tab', command: "openInTab", uiIcon: "empty", enabled: noSelectedNotes },
{ title: 'Insert note after <kbd data-command="createNoteAfter"></kbd>', cmd: "insertNoteAfter", uiIcon: "plus", { title: 'Insert note after <kbd data-command="createNoteAfter"></kbd>', command: "insertNoteAfter", uiIcon: "plus",
items: insertNoteAfterEnabled ? this.getNoteTypeItems("insertNoteAfter") : null, items: insertNoteAfterEnabled ? this.getNoteTypeItems("insertNoteAfter") : null,
enabled: insertNoteAfterEnabled && noSelectedNotes }, enabled: insertNoteAfterEnabled && noSelectedNotes },
{ title: 'Insert child note <kbd data-command="createNoteInto"></kbd>', cmd: "insertChildNote", uiIcon: "plus", { title: 'Insert child note <kbd data-command="createNoteInto"></kbd>', command: "insertChildNote", uiIcon: "plus",
items: notSearch ? this.getNoteTypeItems("insertChildNote") : null, items: notSearch ? this.getNoteTypeItems("insertChildNote") : null,
enabled: notSearch && noSelectedNotes }, enabled: notSearch && noSelectedNotes },
{ title: 'Delete <kbd data-command="deleteNotes"></kbd>', cmd: "deleteNotes", uiIcon: "trash", { title: 'Delete <kbd data-command="deleteNotes"></kbd>', command: "deleteNotes", uiIcon: "trash",
enabled: isNotRoot && !isHoisted && parentNotSearch }, enabled: isNotRoot && !isHoisted && parentNotSearch },
{ title: "----" }, { title: "----" },
{ title: 'Search in subtree <kbd data-command="searchInSubtree"></kbd>', cmd: "searchInSubtree", uiIcon: "search", { title: 'Search in subtree <kbd data-command="searchInSubtree"></kbd>', command: "searchInSubtree", uiIcon: "search",
enabled: notSearch && noSelectedNotes }, enabled: notSearch && noSelectedNotes },
isHoisted ? null : { title: 'Hoist note <kbd data-command="toggleNoteHoisting"></kbd>', cmd: "toggleNoteHoisting", uiIcon: "empty", enabled: noSelectedNotes && notSearch }, isHoisted ? null : { title: 'Hoist note <kbd data-command="toggleNoteHoisting"></kbd>', command: "toggleNoteHoisting", uiIcon: "empty", enabled: noSelectedNotes && notSearch },
!isHoisted || !isNotRoot ? null : { title: 'Unhoist note <kbd data-command="ToggleNoteHoisting"></kbd>', cmd: "toggleNoteHoisting", uiIcon: "arrow-up" }, !isHoisted || !isNotRoot ? null : { title: 'Unhoist note <kbd data-command="ToggleNoteHoisting"></kbd>', command: "toggleNoteHoisting", uiIcon: "arrow-up" },
{ title: 'Edit branch prefix <kbd data-command="editBranchPrefix"></kbd>', cmd: "editBranchPrefix", uiIcon: "empty", { title: 'Edit branch prefix <kbd data-command="editBranchPrefix"></kbd>', command: "editBranchPrefix", uiIcon: "empty",
enabled: isNotRoot && parentNotSearch && noSelectedNotes}, enabled: isNotRoot && parentNotSearch && noSelectedNotes},
{ title: "Advanced", uiIcon: "empty", enabled: true, items: [ { title: "Advanced", uiIcon: "empty", enabled: true, items: [
{ title: 'Collapse subtree <kbd data-command="collapseSubtree"></kbd>', cmd: "collapseSubtree", uiIcon: "align-justify", enabled: noSelectedNotes }, { title: 'Collapse subtree <kbd data-command="collapseSubtree"></kbd>', command: "collapseSubtree", uiIcon: "align-justify", enabled: noSelectedNotes },
{ title: "Force note sync", cmd: "forceNoteSync", uiIcon: "refresh", enabled: noSelectedNotes }, { title: "Force note sync", command: "forceNoteSync", uiIcon: "refresh", enabled: noSelectedNotes },
{ title: 'Sort alphabetically <kbd data-command="sortChildNotes"></kbd>', cmd: "sortChildNotes", uiIcon: "empty", enabled: noSelectedNotes && notSearch } { title: 'Sort alphabetically <kbd data-command="sortChildNotes"></kbd>', command: "sortChildNotes", uiIcon: "empty", enabled: noSelectedNotes && notSearch }
] }, ] },
{ title: "----" }, { title: "----" },
{ title: "Protect subtree", cmd: "protectSubtree", uiIcon: "check-shield", enabled: noSelectedNotes }, { title: "Protect subtree", command: "protectSubtree", uiIcon: "check-shield", enabled: noSelectedNotes },
{ title: "Unprotect subtree", cmd: "unprotectSubtree", uiIcon: "shield", enabled: noSelectedNotes }, { title: "Unprotect subtree", command: "unprotectSubtree", uiIcon: "shield", enabled: noSelectedNotes },
{ title: "----" }, { title: "----" },
{ title: 'Copy / clone <kbd data-command="copyNotesToClipboard"></kbd>', cmd: "copyNotesToClipboard", uiIcon: "copy", { title: 'Copy / clone <kbd data-command="copyNotesToClipboard"></kbd>', command: "copyNotesToClipboard", uiIcon: "copy",
enabled: isNotRoot && !isHoisted }, enabled: isNotRoot && !isHoisted },
{ title: 'Clone to ... <kbd data-command="cloneNotesTo"></kbd>', cmd: "cloneNotesTo", uiIcon: "empty", { title: 'Clone to ... <kbd data-command="cloneNotesTo"></kbd>', command: "cloneNotesTo", uiIcon: "empty",
enabled: isNotRoot && !isHoisted }, enabled: isNotRoot && !isHoisted },
{ title: 'Cut <kbd data-command="cutNotesToClipboard"></kbd>', cmd: "cutNotesToClipboard", uiIcon: "cut", { title: 'Cut <kbd data-command="cutNotesToClipboard"></kbd>', command: "cutNotesToClipboard", uiIcon: "cut",
enabled: isNotRoot && !isHoisted && parentNotSearch }, enabled: isNotRoot && !isHoisted && parentNotSearch },
{ title: 'Move to ... <kbd data-command="moveNotesTo"></kbd>', cmd: "moveNotesTo", uiIcon: "empty", { title: 'Move to ... <kbd data-command="moveNotesTo"></kbd>', command: "moveNotesTo", uiIcon: "empty",
enabled: isNotRoot && !isHoisted && parentNotSearch }, enabled: isNotRoot && !isHoisted && parentNotSearch },
{ title: 'Paste into <kbd data-command="pasteNotesFromClipboard"></kbd>', cmd: "pasteNotesFromClipboard", uiIcon: "paste", { title: 'Paste into <kbd data-command="pasteNotesFromClipboard"></kbd>', command: "pasteNotesFromClipboard", uiIcon: "paste",
enabled: !clipboard.isClipboardEmpty() && notSearch && noSelectedNotes }, enabled: !clipboard.isClipboardEmpty() && notSearch && noSelectedNotes },
{ title: 'Paste after', cmd: "pasteNotesAfterFromClipboard", uiIcon: "paste", { title: 'Paste after', command: "pasteNotesAfterFromClipboard", uiIcon: "paste",
enabled: !clipboard.isClipboardEmpty() && isNotRoot && !isHoisted && parentNotSearch && noSelectedNotes }, enabled: !clipboard.isClipboardEmpty() && isNotRoot && !isHoisted && parentNotSearch && noSelectedNotes },
{ title: "Duplicate note here", cmd: "duplicateNote", uiIcon: "empty", { title: "Duplicate note here", command: "duplicateNote", uiIcon: "empty",
enabled: noSelectedNotes && parentNotSearch && isNotRoot && !isHoisted && (!note.isProtected || protectedSessionHolder.isProtectedSessionAvailable()) }, enabled: noSelectedNotes && parentNotSearch && isNotRoot && !isHoisted && (!note.isProtected || protectedSessionHolder.isProtectedSessionAvailable()) },
{ title: "----" }, { title: "----" },
{ title: "Export", cmd: "exportNote", uiIcon: "empty", { title: "Export", command: "exportNote", uiIcon: "empty",
enabled: notSearch && noSelectedNotes }, enabled: notSearch && noSelectedNotes },
{ title: "Import into note", cmd: "importIntoNote", uiIcon: "empty", { title: "Import into note", command: "importIntoNote", uiIcon: "empty",
enabled: notSearch && noSelectedNotes } enabled: notSearch && noSelectedNotes }
].filter(row => row !== null); ].filter(row => row !== null);
} }
async selectContextMenuItem(event, cmd) { async selectMenuItemHandler({command, type}) {
const noteId = this.node.data.noteId; const noteId = this.node.data.noteId;
const notePath = treeService.getNotePath(this.node); const notePath = treeService.getNotePath(this.node);
if (cmd === 'openInTab') { if (command === 'openInTab') {
const tabContext = appContext.tabManager.openEmptyTab(); const tabContext = appContext.tabManager.openEmptyTab();
appContext.tabManager.activateTab(tabContext.tabId); appContext.tabManager.activateTab(tabContext.tabId);
tabContext.setNote(notePath); tabContext.setNote(notePath);
} }
else if (cmd.startsWith("insertNoteAfter")) { else if (command === "insertNoteAfter") {
const parentNoteId = this.node.data.parentNoteId; const parentNoteId = this.node.data.parentNoteId;
const isProtected = await treeService.getParentProtectedStatus(this.node); const isProtected = await treeService.getParentProtectedStatus(this.node);
const type = cmd.split("_")[1];
noteCreateService.createNote(parentNoteId, { noteCreateService.createNote(parentNoteId, {
target: 'after', target: 'after',
@ -114,16 +123,14 @@ class TreeContextMenu {
isProtected: isProtected isProtected: isProtected
}); });
} }
else if (cmd.startsWith("insertChildNote")) { else if (command === "insertChildNote") {
const type = cmd.split("_")[1];
noteCreateService.createNote(noteId, { noteCreateService.createNote(noteId, {
type: type, type: type,
isProtected: this.node.data.isProtected isProtected: this.node.data.isProtected
}); });
} }
else { else {
this.treeWidget.triggerCommand(cmd, {node: this.node}); this.treeWidget.triggerCommand(command, {node: this.node});
} }
} }
} }

View File

@ -222,7 +222,8 @@ export default class NoteTreeWidget extends TabAwareWidget {
this.$widget.on('contextmenu', '.fancytree-node', e => { this.$widget.on('contextmenu', '.fancytree-node', e => {
const node = $.ui.fancytree.getNode(e); const node = $.ui.fancytree.getNode(e);
contextMenuWidget.initContextMenu(e, new TreeContextMenu(this, node)); const treeContextMenu = new TreeContextMenu(this, node);
treeContextMenu.show(e);
return false; // blocks default browser right click menu return false; // blocks default browser right click menu
}); });

View File

@ -6,7 +6,7 @@
*/ */
import BasicWidget from "./basic_widget.js"; import BasicWidget from "./basic_widget.js";
import contextMenuService from "../services/context_menu.js"; import contextMenu from "../services/context_menu.js";
import utils from "../services/utils.js"; import utils from "../services/utils.js";
import keyboardActionService from "../services/keyboard_actions.js"; import keyboardActionService from "../services/keyboard_actions.js";
import appContext from "../services/app_context.js"; import appContext from "../services/app_context.js";
@ -254,15 +254,15 @@ export default class TabRowWidget extends BasicWidget {
const tabId = $(e.target).closest(".note-tab").attr('data-tab-id'); const tabId = $(e.target).closest(".note-tab").attr('data-tab-id');
contextMenuService.initContextMenu(e, { contextMenu.show({
getContextMenuItems: () => { x: e.pageX,
return [ y: e.pageY,
{title: "Close all tabs", cmd: "removeAllTabs", uiIcon: "empty"}, items: [
{title: "Close all tabs except for this", cmd: "removeAllTabsExceptForThis", uiIcon: "empty"} {title: "Close all tabs", command: "removeAllTabs", uiIcon: "empty"},
]; {title: "Close all tabs except for this", command: "removeAllTabsExceptForThis", uiIcon: "empty"}
}, ],
selectContextMenuItem: (e, cmd) => { selectMenuItemHandler: ({command}) => {
this.triggerCommand(cmd, {tabId}); this.triggerCommand(command, {tabId});
} }
}); });
}); });

View File

@ -2,7 +2,7 @@ import server from "../../services/server.js";
import linkService from "../../services/link.js"; import linkService from "../../services/link.js";
import libraryLoader from "../../services/library_loader.js"; import libraryLoader from "../../services/library_loader.js";
import treeService from "../../services/tree.js"; import treeService from "../../services/tree.js";
import contextMenuWidget from "../../services/context_menu.js"; import contextMenu from "../../services/context_menu.js";
import toastService from "../../services/toast.js"; import toastService from "../../services/toast.js";
import attributeAutocompleteService from "../../services/attribute_autocomplete.js"; import attributeAutocompleteService from "../../services/attribute_autocomplete.js";
import TypeWidget from "./type_widget.js"; import TypeWidget from "./type_widget.js";
@ -133,15 +133,15 @@ export default class RelationMapTypeWidget extends TypeWidget {
this.$relationMapContainer.attr("id", "relation-map-container-" + (containerCounter++)); this.$relationMapContainer.attr("id", "relation-map-container-" + (containerCounter++));
this.$relationMapContainer.on("contextmenu", ".note-box", e => { this.$relationMapContainer.on("contextmenu", ".note-box", e => {
contextMenuWidget.initContextMenu(e, { contextMenu.show({
getContextMenuItems: () => { x: e.pageX,
return [ y: e.pageY,
{title: "Open in new tab", cmd: "open-in-new-tab", uiIcon: "empty"}, items: [
{title: "Remove note", cmd: "remove", uiIcon: "trash"}, {title: "Open in new tab", command: "openInNewTab", uiIcon: "empty"},
{title: "Edit title", cmd: "edit-title", uiIcon: "pencil"}, {title: "Remove note", command: "remove", uiIcon: "trash"},
]; {title: "Edit title", command: "editTitle", uiIcon: "pencil"},
}, ],
selectContextMenuItem: (event, cmd) => this.tabContextMenuHandler(event, cmd) selectMenuItemHandler: ({command}) => this.contextMenuHandler(command, e.target)
}); });
return false; return false;
@ -190,16 +190,16 @@ export default class RelationMapTypeWidget extends TypeWidget {
return this.$widget; return this.$widget;
} }
async tabContextMenuHandler(event, cmd) { async contextMenuHandler(command, originalTarget) {
const $noteBox = $(event.originalTarget).closest(".note-box"); const $noteBox = $(originalTarget).closest(".note-box");
const $title = $noteBox.find(".title a"); const $title = $noteBox.find(".title a");
const noteId = this.idToNoteId($noteBox.prop("id")); const noteId = this.idToNoteId($noteBox.prop("id"));
if (cmd === "open-in-new-tab") { if (command === "openInNewTab") {
const tabContext = appContext.tabManager.openEmptyTab(); const tabContext = appContext.tabManager.openEmptyTab();
tabContext.setNote(noteId); tabContext.setNote(noteId);
} }
else if (cmd === "remove") { else if (command === "remove") {
const confirmDialog = await import('../../dialogs/confirm.js'); const confirmDialog = await import('../../dialogs/confirm.js');
if (!await confirmDialog.confirmDeleteNoteBoxWithNote($title.text())) { if (!await confirmDialog.confirmDeleteNoteBoxWithNote($title.text())) {
@ -221,7 +221,7 @@ export default class RelationMapTypeWidget extends TypeWidget {
this.saveData(); this.saveData();
} }
else if (cmd === "edit-title") { else if (command === "editTitle") {
const promptDialog = await import("../../dialogs/prompt.js"); const promptDialog = await import("../../dialogs/prompt.js");
const title = await promptDialog.ask({ const title = await promptDialog.ask({
message: "Enter new note title:", message: "Enter new note title:",
@ -234,8 +234,6 @@ export default class RelationMapTypeWidget extends TypeWidget {
await server.put(`notes/${noteId}/change-title`, { title }); await server.put(`notes/${noteId}/change-title`, { title });
treeService.setNoteTitle(noteId, title);
$title.text(title); $title.text(title);
} }
} }
@ -449,12 +447,12 @@ export default class RelationMapTypeWidget extends TypeWidget {
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
contextMenuWidget.initContextMenu(event, { contextMenu.show({
getContextMenuItems: () => { x: event.pageX,
return [ {title: "Remove relation", cmd: "remove", uiIcon: "trash"} ]; y: event.pageY,
}, items: [ {title: "Remove relation", command: "remove", uiIcon: "trash"} ],
selectContextMenuItem: async (event, cmd) => { selectMenuItemHandler: async ({command}) => {
if (cmd === 'remove') { if (command === 'remove') {
const confirmDialog = await import('../../dialogs/confirm.js'); const confirmDialog = await import('../../dialogs/confirm.js');
if (!await confirmDialog.confirm("Are you sure you want to remove the relation?")) { if (!await confirmDialog.confirm("Are you sure you want to remove the relation?")) {