refactored tree context menu to reduce duplication

This commit is contained in:
zadam
2020-02-16 22:56:40 +01:00
parent 4330dc47a0
commit 32d60a7eb9
6 changed files with 70 additions and 114 deletions

View File

@@ -65,7 +65,7 @@ async function moveToParentNote(branchIdsToMove, newParentNoteId) {
}
}
async function deleteNotes(treeWidget, branchIdsToDelete) {
async function deleteNotes(branchIdsToDelete) {
branchIdsToDelete = filterRootNote(branchIdsToDelete);
if (branchIdsToDelete.length === 0) {
@@ -119,8 +119,6 @@ async function deleteNotes(treeWidget, branchIdsToDelete) {
}
}
treeWidget.clearSelectedNodes();
return true;
}

View File

@@ -1,9 +1,5 @@
import treeService from './tree.js';
import ws from './ws.js';
import protectedSessionService from './protected_session.js';
import treeChangesService from './branches.js';
import treeCache from "./tree_cache.js";
import syncService from "./sync.js";
import hoistedNoteService from './hoisted_note.js';
import clipboard from './clipboard.js';
import protectedSessionHolder from "./protected_session_holder.js";
@@ -57,40 +53,40 @@ class TreeContextMenu {
{ title: 'Insert child note <kbd data-kb-command="createNoteInto"></kbd>', cmd: "insertChildNote", uiIcon: "plus",
items: notSearch ? this.getNoteTypeItems("insertChildNote") : null,
enabled: notSearch && noSelectedNotes },
{ title: 'Delete <kbd data-kb-command="deleteNotes"></kbd>', cmd: "delete", uiIcon: "trash",
{ title: 'Delete <kbd data-kb-command="deleteNotes"></kbd>', cmd: "deleteNotes", uiIcon: "trash",
enabled: isNotRoot && !isHoisted && parentNotSearch },
{ title: "----" },
{ title: 'Search in subtree <kbd data-kb-command="searchInSubtree"></kbd>', cmd: "searchInSubtree", uiIcon: "search",
enabled: notSearch && noSelectedNotes },
isHoisted ? null : { title: 'Hoist note <kbd data-kb-command="toggleNoteHoisting"></kbd>', cmd: "hoist", uiIcon: "empty", enabled: noSelectedNotes && notSearch },
!isHoisted || !isNotRoot ? null : { title: 'Unhoist note <kbd data-kb-command="ToggleNoteHoisting"></kbd>', cmd: "unhoist", uiIcon: "arrow-up" },
isHoisted ? null : { title: 'Hoist note <kbd data-kb-command="toggleNoteHoisting"></kbd>', cmd: "toggleNoteHoisting", uiIcon: "empty", enabled: noSelectedNotes && notSearch },
!isHoisted || !isNotRoot ? null : { title: 'Unhoist note <kbd data-kb-command="ToggleNoteHoisting"></kbd>', cmd: "toggleNoteHoisting", uiIcon: "arrow-up" },
{ title: 'Edit branch prefix <kbd data-kb-command="editBranchPrefix"></kbd>', cmd: "editBranchPrefix", uiIcon: "empty",
enabled: isNotRoot && parentNotSearch && noSelectedNotes},
{ title: "Advanced", uiIcon: "empty", enabled: true, items: [
{ title: 'Collapse subtree <kbd data-kb-command="collapseSubtree"></kbd>', cmd: "collapseSubtree", uiIcon: "align-justify", enabled: noSelectedNotes },
{ title: "Force note sync", cmd: "forceNoteSync", uiIcon: "refresh", enabled: noSelectedNotes },
{ title: 'Sort alphabetically <kbd data-kb-command="sortChildNotes"></kbd>', cmd: "sortAlphabetically", uiIcon: "empty", enabled: noSelectedNotes && notSearch }
{ title: 'Sort alphabetically <kbd data-kb-command="sortChildNotes"></kbd>', cmd: "sortChildNotes", uiIcon: "empty", enabled: noSelectedNotes && notSearch }
] },
{ title: "----" },
{ title: "Protect subtree", cmd: "protectSubtree", uiIcon: "check-shield", enabled: noSelectedNotes },
{ title: "Unprotect subtree", cmd: "unprotectSubtree", uiIcon: "shield", enabled: noSelectedNotes },
{ title: "----" },
{ title: 'Copy / clone <kbd data-kb-command="copyNotesToClipboard"></kbd>', cmd: "copy", uiIcon: "copy",
{ title: 'Copy / clone <kbd data-kb-command="copyNotesToClipboard"></kbd>', cmd: "copyNotesToClipboard", uiIcon: "copy",
enabled: isNotRoot && !isHoisted },
{ title: 'Clone to ... <kbd data-kb-command="cloneNotesTo"></kbd>', cmd: "cloneTo", uiIcon: "empty",
{ title: 'Clone to ... <kbd data-kb-command="cloneNotesTo"></kbd>', cmd: "cloneNotesTo", uiIcon: "empty",
enabled: isNotRoot && !isHoisted },
{ title: 'Cut <kbd data-kb-command="cutNotesToClipboard"></kbd>', cmd: "cut", uiIcon: "cut",
{ title: 'Cut <kbd data-kb-command="cutNotesToClipboard"></kbd>', cmd: "cutNotesToClipboard", uiIcon: "cut",
enabled: isNotRoot && !isHoisted && parentNotSearch },
{ title: 'Move to ... <kbd data-kb-command="moveNotesTo"></kbd>', cmd: "moveTo", uiIcon: "empty",
{ title: 'Move to ... <kbd data-kb-command="moveNotesTo"></kbd>', cmd: "moveNotesTo", uiIcon: "empty",
enabled: isNotRoot && !isHoisted && parentNotSearch },
{ title: 'Paste into <kbd data-kb-command="pasteNotesFromClipboard"></kbd>', cmd: "pasteInto", uiIcon: "paste",
{ title: 'Paste into <kbd data-kb-command="pasteNotesFromClipboard"></kbd>', cmd: "pasteNotesFromClipboard", uiIcon: "paste",
enabled: !clipboard.isClipboardEmpty() && notSearch && noSelectedNotes },
{ title: 'Paste after', cmd: "pasteAfter", uiIcon: "paste",
{ title: 'Paste after', cmd: "pasteNotesAfterFromClipboard", uiIcon: "paste",
enabled: !clipboard.isClipboardEmpty() && isNotRoot && !isHoisted && parentNotSearch && noSelectedNotes },
{ title: "Duplicate note here", cmd: "duplicateNote", uiIcon: "empty",
enabled: noSelectedNotes && parentNotSearch && isNotRoot && !isHoisted && (!note.isProtected || protectedSessionHolder.isProtectedSessionAvailable()) },
{ title: "----" },
{ title: "Export", cmd: "export", uiIcon: "empty",
{ title: "Export", cmd: "exportNote", uiIcon: "empty",
enabled: notSearch && noSelectedNotes },
{ title: "Import into note", cmd: "importIntoNote", uiIcon: "empty",
enabled: notSearch && noSelectedNotes }
@@ -126,81 +122,10 @@ class TreeContextMenu {
isProtected: this.node.data.isProtected
});
}
else if (cmd === "editBranchPrefix") {
const branchPrefixDialog = await import('../dialogs/branch_prefix.js');
branchPrefixDialog.showDialog(this.node);
}
else if (cmd === "protectSubtree") {
protectedSessionService.protectSubtree(noteId, true);
}
else if (cmd === "unprotectSubtree") {
protectedSessionService.protectSubtree(noteId, false);
}
else if (cmd === "copy") {
clipboard.copy(this.getSelectedOrActiveBranchIds());
}
else if (cmd === "cloneTo") {
const nodes = this.treeWidget.getSelectedOrActiveNodes(this.node);
const noteIds = nodes.map(node => node.data.noteId);
import("../dialogs/clone_to.js").then(d => d.showDialog(noteIds));
}
else if (cmd === "cut") {
clipboard.cut(this.getSelectedOrActiveBranchIds());
}
else if (cmd === "moveTo") {
this.treeWidget.triggerCommand('moveNotesTo');
}
else if (cmd === "pasteAfter") {
clipboard.pasteAfter(this.node.data.branchId);
}
else if (cmd === "pasteInto") {
clipboard.pasteInto(noteId);
}
else if (cmd === "delete") {
treeChangesService.deleteNotes(this.treeWidget, this.getSelectedOrActiveBranchIds());
}
else if (cmd === "export") {
const exportDialog = await import('../dialogs/export.js');
exportDialog.showDialog(notePath,"subtree");
}
else if (cmd === "importIntoNote") {
const importDialog = await import('../dialogs/import.js');
importDialog.showDialog(noteId);
}
else if (cmd === "collapseSubtree") {
this.treeWidget.collapseTree(this.node);
}
else if (cmd === "forceNoteSync") {
syncService.forceNoteSync(noteId);
}
else if (cmd === "sortAlphabetically") {
treeService.sortAlphabetically(noteId);
}
else if (cmd === "hoist") {
hoistedNoteService.setHoistedNoteId(noteId);
}
else if (cmd === "unhoist") {
hoistedNoteService.unhoist();
}
else if (cmd === "duplicateNote") {
const branch = treeCache.getBranch(this.node.data.branchId);
noteCreateService.duplicateNote(noteId, branch.parentNoteId);
}
else if (cmd === "searchInSubtree") {
appContext.triggerEvent("searchInSubtree", {noteId});
}
else {
ws.logError("Unknown command: " + cmd);
this.treeWidget.triggerCommand(cmd, {node: this.node});
}
}
getSelectedOrActiveBranchIds() {
const nodes = this.treeWidget.getSelectedOrActiveNodes(this.node);
return nodes.map(node => node.data.branchId);
}
}
export default TreeContextMenu;

View File

@@ -16,6 +16,7 @@ import ProtectedSessionTypeWidget from "./type_widgets/protected_session.js";
import BookTypeWidget from "./type_widgets/book.js";
import appContext from "../services/app_context.js";
import keyboardActionsService from "../services/keyboard_actions.js";
import noteCreateService from "../services/note_create.js";
const TPL = `
<div class="note-detail">
@@ -238,4 +239,17 @@ export default class NoteDetailWidget extends TabAwareWidget {
await super.handleEventInChildren(name, data);
}
}
async cutIntoNoteCommand() {
const note = appContext.tabManager.getActiveTabNote();
if (!note) {
return;
}
await noteCreateService.createNote(note.noteId, {
isProtected: note.isProtected,
saveSelection: true
});
}
}

View File

@@ -14,6 +14,8 @@ import toastService from "../services/toast.js";
import appContext from "../services/app_context.js";
import keyboardActionsService from "../services/keyboard_actions.js";
import clipboard from "../services/clipboard.js";
import protectedSessionService from "../services/protected_session.js";
import syncService from "../services/sync.js";
const TPL = `
<div class="tree">
@@ -30,15 +32,7 @@ const TPL = `
</div>
`;
export default class Notethis extends TabAwareWidget {
constructor(appContext, parent) {
super(appContext, parent);
window.glob.cutIntoNote = () => this.cutIntoNoteEvent();
this.tree = null;
}
export default class NoteTreeWidget extends TabAwareWidget {
doRender() {
this.$widget = $(TPL);
@@ -576,17 +570,6 @@ export default class Notethis extends TabAwareWidget {
}
}
async cutIntoNoteEvent() {
const node = this.getActiveNode();
if (node) {
await noteCreateService.createNote(node.data.noteId, {
isProtected: node.data.isProtected,
saveSelection: true
});
}
}
async setExpandedToServer(branchId, isExpanded) {
utils.assertArguments(branchId);
@@ -675,10 +658,12 @@ export default class Notethis extends TabAwareWidget {
return nodes.map(node => node.data.branchId);
}
deleteNotesCommand({node}) {
async deleteNotesCommand({node}) {
const branchIds = this.getSelectedOrActiveBranchIds(node);
treeChangesService.deleteNotes(this, branchIds);
await treeChangesService.deleteNotes(branchIds);
this.clearSelectedNodes();
}
moveNoteUpCommand({node}) {
@@ -782,6 +767,26 @@ export default class Notethis extends TabAwareWidget {
clipboard.pasteInto(node.data.noteId);
}
pasteNotesAfterFromClipboard({node}) {
clipboard.pasteAfter(node.data.branchId);
}
async exportNoteCommand({node}) {
const exportDialog = await import('../dialogs/export.js');
const notePath = treeService.getNotePath(node);
exportDialog.showDialog(notePath,"subtree");
}
async importIntoNoteCommand({node}) {
const importDialog = await import('../dialogs/import.js');
importDialog.showDialog(node.data.noteId);
}
forceNoteSyncCommand({node}) {
syncService.forceNoteSync(noteId);
}
editNoteTitleCommand({node}) {
appContext.triggerEvent('focusOnTitle');
}
@@ -791,4 +796,18 @@ export default class Notethis extends TabAwareWidget {
node.getParent().setActive().then(this.clearSelectedNodes);
}
}
protectSubtreeCommand({node}) {
protectedSessionService.protectSubtree(node.data.noteId, true);
}
unprotectSubtreeCommand({node}) {
protectedSessionService.protectSubtree(node.data.noteId, false);
}
duplicateNoteCommand({node}) {
const branch = treeCache.getBranch(node.data.branchId);
noteCreateService.duplicateNote(noteId, branch.parentNoteId);
}
}