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

136 lines
7.6 KiB
JavaScript
Raw Normal View History

import treeService from './tree.js';
2018-03-26 10:37:02 +08:00
import treeCache from "./tree_cache.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";
2020-02-29 20:03:05 +08:00
import contextMenu from "./context_menu.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;
}
2020-02-29 20:03:05 +08:00
async show(e) {
contextMenu.show({
x: e.pageX,
y: e.pageY,
items: await this.getMenuItems(),
selectMenuItemHandler: (item, e) => this.selectMenuItemHandler(item, e)
})
}
2019-05-04 02:27:38 +08:00
2020-02-29 20:03:05 +08:00
getNoteTypeItems(command) {
2019-05-04 02:27:38 +08:00
return [
2020-02-29 20:03:05 +08:00
{ title: "Text", command: command, type: "text", uiIcon: "note" },
{ title: "Code", command: command, type: "code", uiIcon: "code" },
{ title: "Saved search", command: command, type: "search", uiIcon: "file-find" },
{ title: "Relation Map", command: command, type: "relation-map", uiIcon: "map-alt" },
{ title: "Render HTML note", command: command, type: "render", uiIcon: "extension" },
{ title: "Book", command: command, type: "book", uiIcon: "book" }
2019-05-04 02:27:38 +08:00
];
}
2020-02-29 20:03:05 +08:00
async getMenuItems() {
2019-05-04 02:27:38 +08:00
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 [
2020-02-29 20:03:05 +08:00
{ title: 'Open in new tab', command: "openInTab", uiIcon: "empty", enabled: noSelectedNotes },
{ title: 'Insert note after <kbd data-command="createNoteAfter"></kbd>', command: "insertNoteAfter", uiIcon: "plus",
2019-05-04 02:27:38 +08:00
items: insertNoteAfterEnabled ? this.getNoteTypeItems("insertNoteAfter") : null,
enabled: insertNoteAfterEnabled && noSelectedNotes },
2020-02-29 20:03:05 +08:00
{ title: 'Insert child note <kbd data-command="createNoteInto"></kbd>', command: "insertChildNote", uiIcon: "plus",
items: notSearch ? this.getNoteTypeItems("insertChildNote") : null,
enabled: notSearch && noSelectedNotes },
2020-02-29 20:03:05 +08:00
{ title: 'Delete <kbd data-command="deleteNotes"></kbd>', command: "deleteNotes", uiIcon: "trash",
enabled: isNotRoot && !isHoisted && parentNotSearch },
2019-05-04 02:27:38 +08:00
{ title: "----" },
2020-02-29 20:03:05 +08:00
{ title: 'Search in subtree <kbd data-command="searchInSubtree"></kbd>', command: "searchInSubtree", uiIcon: "search",
enabled: notSearch && noSelectedNotes },
2020-02-29 20:03:05 +08:00
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>', command: "toggleNoteHoisting", uiIcon: "arrow-up" },
{ title: 'Edit branch prefix <kbd data-command="editBranchPrefix"></kbd>', command: "editBranchPrefix", uiIcon: "empty",
enabled: isNotRoot && parentNotSearch && noSelectedNotes},
{ title: "Advanced", uiIcon: "empty", enabled: true, items: [
2020-02-29 20:03:05 +08:00
{ title: 'Collapse subtree <kbd data-command="collapseSubtree"></kbd>', command: "collapseSubtree", uiIcon: "align-justify", enabled: noSelectedNotes },
{ title: "Force note sync", command: "forceNoteSync", uiIcon: "refresh", enabled: noSelectedNotes },
{ title: 'Sort alphabetically <kbd data-command="sortChildNotes"></kbd>', command: "sortChildNotes", uiIcon: "empty", enabled: noSelectedNotes && notSearch }
] },
2019-05-04 02:27:38 +08:00
{ title: "----" },
2020-02-29 20:03:05 +08:00
{ title: "Protect subtree", command: "protectSubtree", uiIcon: "check-shield", enabled: noSelectedNotes },
{ title: "Unprotect subtree", command: "unprotectSubtree", uiIcon: "shield", enabled: noSelectedNotes },
2019-05-04 02:27:38 +08:00
{ title: "----" },
2020-02-29 20:03:05 +08:00
{ title: 'Copy / clone <kbd data-command="copyNotesToClipboard"></kbd>', command: "copyNotesToClipboard", uiIcon: "copy",
2019-11-12 05:57:51 +08:00
enabled: isNotRoot && !isHoisted },
2020-02-29 20:03:05 +08:00
{ title: 'Clone to ... <kbd data-command="cloneNotesTo"></kbd>', command: "cloneNotesTo", uiIcon: "empty",
2019-11-12 05:57:51 +08:00
enabled: isNotRoot && !isHoisted },
2020-02-29 20:03:05 +08:00
{ title: 'Cut <kbd data-command="cutNotesToClipboard"></kbd>', command: "cutNotesToClipboard", uiIcon: "cut",
enabled: isNotRoot && !isHoisted && parentNotSearch },
2020-02-29 20:03:05 +08:00
{ title: 'Move to ... <kbd data-command="moveNotesTo"></kbd>', command: "moveNotesTo", uiIcon: "empty",
2019-11-12 05:57:51 +08:00
enabled: isNotRoot && !isHoisted && parentNotSearch },
2020-02-29 20:03:05 +08:00
{ title: 'Paste into <kbd data-command="pasteNotesFromClipboard"></kbd>', command: "pasteNotesFromClipboard", uiIcon: "paste",
2019-11-21 02:24:23 +08:00
enabled: !clipboard.isClipboardEmpty() && notSearch && noSelectedNotes },
2020-02-29 20:03:05 +08:00
{ title: 'Paste after', command: "pasteNotesAfterFromClipboard", uiIcon: "paste",
2019-11-21 02:24:23 +08:00
enabled: !clipboard.isClipboardEmpty() && isNotRoot && !isHoisted && parentNotSearch && noSelectedNotes },
2020-02-29 20:03:05 +08:00
{ title: "Duplicate note here", command: "duplicateNote", uiIcon: "empty",
enabled: noSelectedNotes && parentNotSearch && isNotRoot && !isHoisted && (!note.isProtected || protectedSessionHolder.isProtectedSessionAvailable()) },
2019-05-04 02:27:38 +08:00
{ title: "----" },
2020-02-29 20:03:05 +08:00
{ title: "Export", command: "exportNote", uiIcon: "empty",
enabled: notSearch && noSelectedNotes },
2020-02-29 20:03:05 +08:00
{ title: "Import into note", command: "importIntoNote", uiIcon: "empty",
enabled: notSearch && noSelectedNotes }
2019-05-04 02:27:38 +08:00
].filter(row => row !== null);
}
2020-02-29 20:03:05 +08:00
async selectMenuItemHandler({command, type}) {
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-02-29 20:03:05 +08:00
if (command === 'openInTab') {
2020-02-29 23:26:46 +08:00
appContext.tabManager.openTabWithNote(notePath);
}
2020-02-29 20:03:05 +08:00
else if (command === "insertNoteAfter") {
2019-05-04 02:27:38 +08:00
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
noteCreateService.createNote(parentNoteId, {
target: 'after',
targetBranchId: this.node.data.branchId,
2019-05-04 02:27:38 +08:00
type: type,
isProtected: isProtected
});
}
2020-02-29 20:03:05 +08:00
else if (command === "insertChildNote") {
noteCreateService.createNote(noteId, {
2019-05-04 02:27:38 +08:00
type: type,
isProtected: this.node.data.isProtected
});
}
else {
2020-02-29 20:03:05 +08:00
this.treeWidget.triggerCommand(command, {node: this.node});
2019-05-04 02:27:38 +08:00
}
}
}
2019-05-04 02:27:38 +08:00
export default TreeContextMenu;