trilium/src/public/javascripts/services/tree_context_menu.js

206 lines
10 KiB
JavaScript
Raw Normal View History

import treeService from './tree.js';
2019-08-27 02:21:43 +08:00
import ws from './ws.js';
import protectedSessionService from './protected_session.js';
2018-04-02 08:33:10 +08:00
import treeChangesService from './branches.js';
2018-03-26 10:37:02 +08:00
import treeCache from "./tree_cache.js";
2018-04-07 06:46:29 +08:00
import syncService from "./sync.js";
2018-12-12 04:53:56 +08:00
import hoistedNoteService from './hoisted_note.js';
2019-05-04 02:27:38 +08:00
import clipboard from './clipboard.js';
2019-10-19 18:36:16 +08:00
import protectedSessionHolder from "./protected_session_holder.js";
2020-01-22 05:54:16 +08:00
import appContext from "./app_context.js";
import noteCreateService from "./note_create.js";
2019-05-04 02:27:38 +08:00
class TreeContextMenu {
/**
* @param {NoteTreeWidget} treeWidget
* @param {FancytreeNode} node
*/
constructor(treeWidget, node) {
this.treeWidget = treeWidget;
2019-05-04 02:27:38 +08:00
this.node = node;
}
getNoteTypeItems(baseCmd) {
return [
2019-11-12 05:57:51 +08:00
{ title: "Text", cmd: baseCmd + "_text", uiIcon: "note" },
{ title: "Code", cmd: baseCmd + "_code", uiIcon: "code" },
{ title: "Saved search", cmd: baseCmd + "_search", uiIcon: "file-find" },
{ title: "Relation Map", cmd: baseCmd + "_relation-map", uiIcon: "map-alt" },
{ title: "Render HTML note", cmd: baseCmd + "_render", uiIcon: "extension" },
{ title: "Book", cmd: baseCmd + "_book", uiIcon: "book" }
2019-05-04 02:27:38 +08:00
];
}
async getContextMenuItems() {
const note = await treeCache.getNote(this.node.data.noteId);
const branch = treeCache.getBranch(this.node.data.branchId);
2019-05-04 02:27:38 +08:00
const parentNote = await treeCache.getNote(branch.parentNoteId);
const isNotRoot = note.noteId !== 'root';
2020-02-11 03:57:56 +08:00
const isHoisted = note.noteId === hoistedNoteService.getHoistedNoteId();
// some actions don't support multi-note so they are disabled when notes are selected
// the only exception is when the only selected note is the one that was right-clicked, then
// it's clear what the user meant to do.
const selNodes = this.treeWidget.getSelectedNodes();
const noSelectedNotes = selNodes.length === 0
|| (selNodes.length === 1 && selNodes[0] === this.node);
2019-05-04 02:27:38 +08:00
const notSearch = note.type !== 'search';
const parentNotSearch = !parentNote || parentNote.type !== 'search';
const insertNoteAfterEnabled = isNotRoot && !isHoisted && parentNotSearch;
2019-05-04 02:27:38 +08:00
return [
{ title: 'Open in new tab', cmd: "openInTab", uiIcon: "empty", enabled: noSelectedNotes },
{ title: 'Insert note after <kbd data-kb-action="createNoteAfter"></kbd>', cmd: "insertNoteAfter", uiIcon: "plus",
2019-05-04 02:27:38 +08:00
items: insertNoteAfterEnabled ? this.getNoteTypeItems("insertNoteAfter") : null,
enabled: insertNoteAfterEnabled && noSelectedNotes },
{ title: 'Insert child note <kbd data-kb-action="createNoteInto"></kbd>', cmd: "insertChildNote", uiIcon: "plus",
items: notSearch ? this.getNoteTypeItems("insertChildNote") : null,
enabled: notSearch && noSelectedNotes },
{ title: 'Delete <kbd data-kb-action="deleteNotes"></kbd>', cmd: "delete", uiIcon: "trash",
enabled: isNotRoot && !isHoisted && parentNotSearch },
2019-05-04 02:27:38 +08:00
{ title: "----" },
{ title: 'Search in subtree <kbd data-kb-action="searchInSubtree"></kbd>', cmd: "searchInSubtree", uiIcon: "search",
enabled: notSearch && noSelectedNotes },
isHoisted ? null : { title: 'Hoist note <kbd data-kb-action="toggleNoteHoisting"></kbd>', cmd: "hoist", uiIcon: "empty", enabled: noSelectedNotes && notSearch },
!isHoisted || !isNotRoot ? null : { title: 'Unhoist note <kbd data-kb-action="ToggleNoteHoisting"></kbd>', cmd: "unhoist", uiIcon: "arrow-up" },
{ title: 'Edit branch prefix <kbd data-kb-action="editBranchPrefix"></kbd>', cmd: "editBranchPrefix", uiIcon: "empty",
enabled: isNotRoot && parentNotSearch && noSelectedNotes},
{ title: "Advanced", uiIcon: "empty", enabled: true, items: [
{ title: 'Collapse subtree <kbd data-kb-action="collapseSubtree"></kbd>', cmd: "collapseSubtree", uiIcon: "align-justify", enabled: noSelectedNotes },
2020-02-15 16:16:23 +08:00
{ title: "Force note sync", cmd: "forceNoteSync", uiIcon: "refresh", enabled: noSelectedNotes },
{ title: 'Sort alphabetically <kbd data-kb-action="sortChildNotes"></kbd>', cmd: "sortAlphabetically", uiIcon: "empty", enabled: noSelectedNotes && notSearch }
] },
2019-05-04 02:27:38 +08:00
{ title: "----" },
2019-11-09 20:32:06 +08:00
{ title: "Protect subtree", cmd: "protectSubtree", uiIcon: "check-shield", enabled: noSelectedNotes },
{ title: "Unprotect subtree", cmd: "unprotectSubtree", uiIcon: "shield", enabled: noSelectedNotes },
2019-05-04 02:27:38 +08:00
{ title: "----" },
{ title: 'Copy / clone <kbd data-kb-action="copyNotesToClipboard"></kbd>', cmd: "copy", uiIcon: "copy",
2019-11-12 05:57:51 +08:00
enabled: isNotRoot && !isHoisted },
{ title: 'Clone to ... <kbd data-kb-action="cloneNotesTo"></kbd>', cmd: "cloneTo", uiIcon: "empty",
2019-11-12 05:57:51 +08:00
enabled: isNotRoot && !isHoisted },
{ title: 'Cut <kbd data-kb-action="cutNotesToClipboard"></kbd>', cmd: "cut", uiIcon: "cut",
enabled: isNotRoot && !isHoisted && parentNotSearch },
{ title: 'Move to ... <kbd data-kb-action="moveNotesTo"></kbd>', cmd: "moveTo", uiIcon: "empty",
2019-11-12 05:57:51 +08:00
enabled: isNotRoot && !isHoisted && parentNotSearch },
{ title: 'Paste into <kbd data-kb-action="pasteNotesFromClipboard"></kbd>', cmd: "pasteInto", uiIcon: "paste",
2019-11-21 02:24:23 +08:00
enabled: !clipboard.isClipboardEmpty() && notSearch && noSelectedNotes },
{ title: 'Paste after', cmd: "pasteAfter", uiIcon: "paste",
2019-11-21 02:24:23 +08:00
enabled: !clipboard.isClipboardEmpty() && isNotRoot && !isHoisted && parentNotSearch && noSelectedNotes },
2019-10-19 18:36:16 +08:00
{ title: "Duplicate note here", cmd: "duplicateNote", uiIcon: "empty",
enabled: noSelectedNotes && parentNotSearch && isNotRoot && !isHoisted && (!note.isProtected || protectedSessionHolder.isProtectedSessionAvailable()) },
2019-05-04 02:27:38 +08:00
{ title: "----" },
{ title: "Export", cmd: "export", uiIcon: "empty",
enabled: notSearch && noSelectedNotes },
2019-05-04 02:27:38 +08:00
{ title: "Import into note", cmd: "importIntoNote", uiIcon: "empty",
enabled: notSearch && noSelectedNotes }
2019-05-04 02:27:38 +08:00
].filter(row => row !== null);
}
async selectContextMenuItem(event, cmd) {
2020-01-22 05:54:16 +08:00
const noteId = this.node.data.noteId;
2020-02-11 03:57:56 +08:00
const notePath = treeService.getNotePath(this.node);
2020-01-22 04:43:23 +08:00
if (cmd === 'openInTab') {
2020-02-10 04:13:05 +08:00
const tabContext = appContext.tabManager.openEmptyTab();
appContext.tabManager.activateTab(tabContext.tabId);
2020-01-25 00:54:47 +08:00
tabContext.setNote(notePath);
}
2019-05-04 02:27:38 +08:00
else if (cmd.startsWith("insertNoteAfter")) {
const parentNoteId = this.node.data.parentNoteId;
2020-01-25 16:56:08 +08:00
const isProtected = await treeService.getParentProtectedStatus(this.node);
2019-05-04 02:27:38 +08:00
const type = cmd.split("_")[1];
noteCreateService.createNote(parentNoteId, {
target: 'after',
targetBranchId: this.node.data.branchId,
2019-05-04 02:27:38 +08:00
type: type,
isProtected: isProtected
});
}
2019-05-04 02:27:38 +08:00
else if (cmd.startsWith("insertChildNote")) {
const type = cmd.split("_")[1];
noteCreateService.createNote(noteId, {
2019-05-04 02:27:38 +08:00
type: type,
isProtected: this.node.data.isProtected
});
}
else if (cmd === "editBranchPrefix") {
const branchPrefixDialog = await import('../dialogs/branch_prefix.js');
2019-05-04 02:27:38 +08:00
branchPrefixDialog.showDialog(this.node);
}
else if (cmd === "protectSubtree") {
2020-01-22 05:54:16 +08:00
protectedSessionService.protectSubtree(noteId, true);
2019-05-04 02:27:38 +08:00
}
else if (cmd === "unprotectSubtree") {
2020-01-22 05:54:16 +08:00
protectedSessionService.protectSubtree(noteId, false);
2019-05-04 02:27:38 +08:00
}
else if (cmd === "copy") {
2020-01-12 17:35:33 +08:00
clipboard.copy(this.getSelectedOrActiveBranchIds());
2019-05-04 02:27:38 +08:00
}
else if (cmd === "cloneTo") {
const nodes = this.treeWidget.getSelectedOrActiveNodes(this.node);
const noteIds = nodes.map(node => node.data.noteId);
2019-11-12 05:57:51 +08:00
import("../dialogs/clone_to.js").then(d => d.showDialog(noteIds));
}
2019-05-04 02:27:38 +08:00
else if (cmd === "cut") {
2020-01-12 17:35:33 +08:00
clipboard.cut(this.getSelectedOrActiveBranchIds());
2019-05-04 02:27:38 +08:00
}
2019-11-12 05:57:51 +08:00
else if (cmd === "moveTo") {
2020-02-15 17:41:21 +08:00
this.treeWidget.triggerCommand('moveNotesTo');
2019-11-12 05:57:51 +08:00
}
2019-05-04 02:27:38 +08:00
else if (cmd === "pasteAfter") {
2020-01-12 17:35:33 +08:00
clipboard.pasteAfter(this.node.data.branchId);
2019-05-04 02:27:38 +08:00
}
else if (cmd === "pasteInto") {
2020-01-22 05:54:16 +08:00
clipboard.pasteInto(noteId);
2019-05-04 02:27:38 +08:00
}
else if (cmd === "delete") {
2020-02-13 05:25:52 +08:00
treeChangesService.deleteNotes(this.treeWidget, this.getSelectedOrActiveBranchIds());
2019-05-04 02:27:38 +08:00
}
else if (cmd === "export") {
const exportDialog = await import('../dialogs/export.js');
2020-01-22 04:43:23 +08:00
exportDialog.showDialog(notePath,"subtree");
2019-05-04 02:27:38 +08:00
}
else if (cmd === "importIntoNote") {
const importDialog = await import('../dialogs/import.js');
2020-01-22 05:54:16 +08:00
importDialog.showDialog(noteId);
2019-05-04 02:27:38 +08:00
}
else if (cmd === "collapseSubtree") {
this.treeWidget.collapseTree(this.node);
2019-05-04 02:27:38 +08:00
}
else if (cmd === "forceNoteSync") {
2020-01-22 05:54:16 +08:00
syncService.forceNoteSync(noteId);
2019-05-04 02:27:38 +08:00
}
else if (cmd === "sortAlphabetically") {
2020-01-22 05:54:16 +08:00
treeService.sortAlphabetically(noteId);
2019-05-04 02:27:38 +08:00
}
else if (cmd === "hoist") {
2020-01-22 05:54:16 +08:00
hoistedNoteService.setHoistedNoteId(noteId);
2019-05-04 02:27:38 +08:00
}
else if (cmd === "unhoist") {
hoistedNoteService.unhoist();
}
2019-10-19 18:36:16 +08:00
else if (cmd === "duplicateNote") {
const branch = treeCache.getBranch(this.node.data.branchId);
2019-10-19 18:36:16 +08:00
2020-02-11 03:57:56 +08:00
noteCreateService.duplicateNote(noteId, branch.parentNoteId);
2019-10-19 18:36:16 +08:00
}
else if (cmd === "searchInSubtree") {
2020-01-22 05:54:16 +08:00
appContext.trigger("searchInSubtree", {noteId});
}
2019-05-04 02:27:38 +08:00
else {
2019-08-27 02:21:43 +08:00
ws.logError("Unknown command: " + cmd);
2019-05-04 02:27:38 +08:00
}
}
2020-01-12 17:35:33 +08:00
getSelectedOrActiveBranchIds() {
const nodes = this.treeWidget.getSelectedOrActiveNodes(this.node);
return nodes.map(node => node.data.branchId);
}
}
2019-05-04 02:27:38 +08:00
export default TreeContextMenu;