trilium/src/public/javascripts/context_menu.js

181 lines
7.2 KiB
JavaScript
Raw Normal View History

"use strict";
2017-11-05 07:33:39 +08:00
const contextMenu = (function() {
2018-02-15 12:31:20 +08:00
const $tree = $("#tree");
2017-11-05 07:28:49 +08:00
let clipboardIds = [];
let clipboardMode = null;
async function pasteAfter(node) {
if (clipboardMode === 'cut') {
const nodes = clipboardIds.map(nodeKey => treeUtils.getNodeByKey(nodeKey));
await treeChanges.moveAfterNode(nodes, node);
clipboardIds = [];
clipboardMode = null;
}
else if (clipboardMode === 'copy') {
for (const noteId of clipboardIds) {
2018-03-25 09:39:15 +08:00
await cloning.cloneNoteAfter(noteId, node.data.branchId);
}
// copy will keep clipboardIds and clipboardMode so it's possible to paste into multiple places
}
else if (clipboardIds.length === 0) {
2017-11-29 00:36:32 +08:00
// just do nothing
}
else {
2018-03-25 11:37:55 +08:00
utils.throwError("Unrecognized clipboard mode=" + clipboardMode);
}
2017-11-05 07:33:39 +08:00
}
async function pasteInto(node) {
if (clipboardMode === 'cut') {
const nodes = clipboardIds.map(nodeKey => treeUtils.getNodeByKey(nodeKey));
await treeChanges.moveToNode(nodes, node);
clipboardIds = [];
clipboardMode = null;
}
else if (clipboardMode === 'copy') {
for (const noteId of clipboardIds) {
2018-01-29 08:30:14 +08:00
await cloning.cloneNoteTo(noteId, node.data.noteId);
}
// copy will keep clipboardIds and clipboardMode so it's possible to paste into multiple places
}
else if (clipboardIds.length === 0) {
// just do nothing
}
else {
2018-03-25 11:37:55 +08:00
utils.throwError("Unrecognized clipboard mode=" + mode);
}
}
function copy(nodes) {
2018-01-29 08:30:14 +08:00
clipboardIds = nodes.map(node => node.data.noteId);
clipboardMode = 'copy';
2018-03-25 11:37:55 +08:00
utils.showMessage("Note(s) have been copied into clipboard.");
2017-11-05 07:33:39 +08:00
}
function cut(nodes) {
clipboardIds = nodes.map(node => node.key);
clipboardMode = 'cut';
2018-03-25 11:37:55 +08:00
utils.showMessage("Note(s) have been cut into clipboard.");
2017-11-05 07:33:39 +08:00
}
2017-11-05 07:33:39 +08:00
const contextMenuSettings = {
delegate: "span.fancytree-title",
autoFocus: true,
menu: [
{title: "Insert note here <kbd>Ctrl+O</kbd>", cmd: "insertNoteHere", uiIcon: "ui-icon-plus"},
{title: "Insert child note <kbd>Ctrl+P</kbd>", cmd: "insertChildNote", uiIcon: "ui-icon-plus"},
{title: "Delete <kbd>Ctrl+Del</kbd>", cmd: "delete", uiIcon: "ui-icon-trash"},
2017-11-05 07:33:39 +08:00
{title: "----"},
{title: "Edit tree prefix <kbd>F2</kbd>", cmd: "editTreePrefix", uiIcon: "ui-icon-pencil"},
2017-11-27 11:34:25 +08:00
{title: "----"},
2017-11-15 13:04:26 +08:00
{title: "Protect sub-tree", cmd: "protectSubTree", uiIcon: "ui-icon-locked"},
{title: "Unprotect sub-tree", cmd: "unprotectSubTree", uiIcon: "ui-icon-unlocked"},
2017-11-05 07:33:39 +08:00
{title: "----"},
{title: "Copy / clone <kbd>Ctrl+C</kbd>", cmd: "copy", uiIcon: "ui-icon-copy"},
{title: "Cut <kbd>Ctrl+X</kbd>", cmd: "cut", uiIcon: "ui-icon-scissors"},
{title: "Paste into <kbd>Ctrl+V</kbd>", cmd: "pasteInto", uiIcon: "ui-icon-clipboard"},
{title: "Paste after", cmd: "pasteAfter", uiIcon: "ui-icon-clipboard"},
{title: "----"},
2018-02-25 23:55:21 +08:00
{title: "Export sub-tree", cmd: "exportSubTree", uiIcon: " ui-icon-arrowthick-1-ne"},
{title: "Import sub-tree into", cmd: "importSubTree", uiIcon: "ui-icon-arrowthick-1-sw"},
{title: "----"},
{title: "Collapse sub-tree <kbd>Alt+-</kbd>", cmd: "collapseSubTree", uiIcon: "ui-icon-minus"},
{title: "Force note sync", cmd: "forceNoteSync", uiIcon: "ui-icon-refresh"},
{title: "Sort alphabetically <kbd>Alt+S</kbd>", cmd: "sortAlphabetically", uiIcon: " ui-icon-arrowthick-2-n-s"}
2017-11-05 07:33:39 +08:00
],
beforeOpen: (event, ui) => {
const node = $.ui.fancytree.getNode(ui.target);
2018-03-25 09:39:15 +08:00
const branch = treeService.getBranch(node.data.branchId);
const note = treeService.getNote(node.data.noteId);
const parentNote = treeService.getNote(branch.parentNoteId);
2017-11-05 07:33:39 +08:00
// Modify menu entries depending on node status
$tree.contextmenu("enableEntry", "pasteAfter", clipboardIds.length > 0 && (!parentNote || parentNote.type !== 'search'));
$tree.contextmenu("enableEntry", "pasteInto", clipboardIds.length > 0 && note.type !== 'search');
$tree.contextmenu("enableEntry", "insertNoteHere", !parentNote || parentNote.type !== 'search');
$tree.contextmenu("enableEntry", "insertChildNote", note.type !== 'search');
$tree.contextmenu("enableEntry", "importSubTree", note.type !== 'search');
$tree.contextmenu("enableEntry", "exportSubTree", note.type !== 'search');
2017-11-05 07:33:39 +08:00
// Activate node on right-click
node.setActive();
// Disable tree keyboard handling
ui.menu.prevKeyboard = node.tree.options.keyboard;
node.tree.options.keyboard = false;
},
close: (event, ui) => {},
select: (event, ui) => {
const node = $.ui.fancytree.getNode(ui.target);
2017-11-05 07:33:39 +08:00
if (ui.cmd === "insertNoteHere") {
2018-01-29 08:30:14 +08:00
const parentNoteId = node.data.parentNoteId;
2017-11-15 12:01:23 +08:00
const isProtected = treeUtils.getParentProtectedStatus(node);
2018-03-25 09:39:15 +08:00
treeService.createNote(node, parentNoteId, 'after', isProtected);
2017-11-05 07:33:39 +08:00
}
else if (ui.cmd === "insertChildNote") {
2018-03-25 09:39:15 +08:00
treeService.createNote(node, node.data.noteId, 'into');
2017-11-05 07:33:39 +08:00
}
2017-11-27 11:34:25 +08:00
else if (ui.cmd === "editTreePrefix") {
editTreePrefix.showDialog(node);
}
2017-11-15 13:04:26 +08:00
else if (ui.cmd === "protectSubTree") {
2018-01-29 08:30:14 +08:00
protected_session.protectSubTree(node.data.noteId, true);
2017-11-05 07:33:39 +08:00
}
2017-11-15 13:04:26 +08:00
else if (ui.cmd === "unprotectSubTree") {
2018-01-29 08:30:14 +08:00
protected_session.protectSubTree(node.data.noteId, false);
2017-11-05 07:33:39 +08:00
}
else if (ui.cmd === "copy") {
2018-03-25 09:39:15 +08:00
copy(treeService.getSelectedNodes());
}
2017-11-05 07:33:39 +08:00
else if (ui.cmd === "cut") {
2018-03-25 09:39:15 +08:00
cut(treeService.getSelectedNodes());
2017-11-05 07:33:39 +08:00
}
else if (ui.cmd === "pasteAfter") {
pasteAfter(node);
}
else if (ui.cmd === "pasteInto") {
pasteInto(node);
}
else if (ui.cmd === "delete") {
2018-03-25 09:39:15 +08:00
treeChanges.deleteNodes(treeService.getSelectedNodes(true));
2017-11-05 07:33:39 +08:00
}
2018-02-25 23:55:21 +08:00
else if (ui.cmd === "exportSubTree") {
exportSubTree(node.data.noteId);
}
else if (ui.cmd === "importSubTree") {
importSubTree(node.data.noteId);
}
else if (ui.cmd === "collapseSubTree") {
2018-03-25 09:39:15 +08:00
treeService.collapseTree(node);
}
2018-02-25 23:55:21 +08:00
else if (ui.cmd === "forceNoteSync") {
2018-01-29 08:30:14 +08:00
forceNoteSync(node.data.noteId);
}
2018-02-25 23:55:21 +08:00
else if (ui.cmd === "sortAlphabetically") {
2018-03-25 09:39:15 +08:00
treeService.sortAlphabetically(node.data.noteId);
}
2017-11-05 07:33:39 +08:00
else {
2017-12-02 11:28:22 +08:00
messaging.logError("Unknown command: " + ui.cmd);
2017-11-05 07:33:39 +08:00
}
}
2017-11-05 07:33:39 +08:00
};
return {
pasteAfter,
pasteInto,
cut,
2017-11-29 00:36:32 +08:00
copy,
2017-11-05 07:33:39 +08:00
contextMenuSettings
}
2017-11-05 07:33:39 +08:00
})();