Skip to content

Build your personal knowledge base with Trilium Notes

feat(mobile/nav): integrate clipboard and other options

Elian Doran2afea943b59d
diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.ts
index ba213a4edf..5b232fd48e 100644
--- a/apps/client/src/components/app_context.ts
+++ b/apps/client/src/components/app_context.ts
@@ -58,9 +58,16 @@ export interface CommandData {
* Represents a set of commands that are triggered from the context menu, providing information such as the selected note.
*/
export interface ContextMenuCommandData extends CommandData {
- node: Fancytree.FancytreeNode;
+ /**
+ * Fancytree node for the target when the command originated from the
+ * Fancytree-based note tree. Omitted when dispatched from node-free UIs
+ * (e.g. the mobile drill-down navigator) — handlers should prefer the
+ * explicit `noteId` / `branchId` / `notePath` fields below.
+ */
+ node?: Fancytree.FancytreeNode;
notePath?: string;
noteId?: string;
+ branchId?: string;
selectedOrActiveBranchIds: string[];
selectedOrActiveNoteIds?: string[];
}
diff --git a/apps/client/src/components/main_tree_executors.ts b/apps/client/src/components/main_tree_executors.ts
index 9bc037ccf3..9470dcb5eb 100644
--- a/apps/client/src/components/main_tree_executors.ts
+++ b/apps/client/src/components/main_tree_executors.ts
@@ -1,13 +1,22 @@
-import appContext, { type EventData } from "./app_context.js";
+import appContext, { type CommandListenerData, type EventData } from "./app_context.js";
+import branchService from "../services/branches.js";
+import clipboard from "../services/clipboard.js";
+import froca from "../services/froca.js";
+import hoistedNoteService from "../services/hoisted_note.js";
import noteCreateService from "../services/note_create.js";
+import protectedSessionHolder from "../services/protected_session_holder.js";
+import protectedSessionService from "../services/protected_session.js";
import treeService from "../services/tree.js";
-import hoistedNoteService from "../services/hoisted_note.js";
import Component from "./component.js";
/**
* This class contains command executors which logically belong to the NoteTree widget, but for better user experience,
* the keyboard shortcuts must be active on the whole screen and not just on the widget itself, so the executors
* must be at the root of the component tree.
+ *
+ * Many of these commands are invoked from the tree's context menu (desktop) and the mobile drill-down navigator's
+ * context menu (standalone mobile). Because the mobile navigator doesn't contain the `NoteTreeWidget`, command
+ * handlers that ought to run regardless of which UI triggered them live here instead of on the widget.
*/
export default class MainTreeExecutors extends Component {
/**
@@ -79,4 +88,91 @@ export default class MainTreeExecutors extends Component {
saveSelection: false
});
}
+
+ async deleteNotesCommand({ selectedOrActiveBranchIds }: CommandListenerData<"deleteNotes">) {
+ const branchIds = selectedOrActiveBranchIds.filter((branchId) => !branchId.startsWith("virt-")); // search results can't be deleted
+
+ if (!branchIds.length) {
+ return;
+ }
+
+ await branchService.deleteNotes(branchIds);
+
+ this.tree?.clearSelectedNodes();
+ }
+
+ async editBranchPrefixCommand({ node, selectedOrActiveBranchIds }: CommandListenerData<"editBranchPrefix">) {
+ const branchIds = selectedOrActiveBranchIds.filter((branchId) => !branchId.startsWith("virt-"));
+
+ if (!branchIds.length) {
+ return;
+ }
+
+ appContext.triggerEvent("editBranchPrefix", {
+ selectedOrActiveBranchIds: branchIds,
+ node
+ });
+ }
+
+ copyNotesToClipboardCommand({ selectedOrActiveBranchIds }: CommandListenerData<"copyNotesToClipboard">) {
+ clipboard.copy(selectedOrActiveBranchIds);
+ }
+
+ cutNotesToClipboardCommand({ selectedOrActiveBranchIds }: CommandListenerData<"cutNotesToClipboard">) {
+ clipboard.cut(selectedOrActiveBranchIds);
+ }
+
+ pasteNotesFromClipboardCommand({ node, branchId }: CommandListenerData<"pasteNotesFromClipboard">) {
+ const targetBranchId = branchId ?? node?.data.branchId;
+ if (!targetBranchId) return;
+ clipboard.pasteInto(targetBranchId);
+ }
+
+ pasteNotesAfterFromClipboardCommand({ node, branchId }: CommandListenerData<"pasteNotesAfterFromClipboard">) {
+ const targetBranchId = branchId ?? node?.data.branchId;
+ if (!targetBranchId) return;
+ clipboard.pasteAfter(targetBranchId);
+ }
+
+ async exportNoteCommand({ node, notePath }: CommandListenerData<"exportNote">) {
+ const path = notePath ?? (node ? treeService.getNotePath(node) : undefined);
+ if (!path) return;
+
+ this.triggerCommand("showExportDialog", { notePath: path, defaultType: "subtree" });
+ }
+
+ async importIntoNoteCommand({ node, noteId }: CommandListenerData<"importIntoNote">) {
+ const targetNoteId = noteId ?? node?.data.noteId;
+ if (!targetNoteId) return;
+ this.triggerCommand("showImportDialog", { noteId: targetNoteId });
+ }
+
+ protectSubtreeCommand({ node, noteId }: CommandListenerData<"protectSubtree">) {
+ const targetNoteId = noteId ?? node?.data.noteId;
+ if (!targetNoteId) return;
+ protectedSessionService.protectNote(targetNoteId, true, true);
+ }
+
+ unprotectSubtreeCommand({ node, noteId }: CommandListenerData<"unprotectSubtree">) {
+ const targetNoteId = noteId ?? node?.data.noteId;
+ if (!targetNoteId) return;
+ protectedSessionService.protectNote(targetNoteId, false, true);
+ }
+
+ async duplicateSubtreeCommand({ selectedOrActiveBranchIds }: CommandListenerData<"duplicateSubtree">) {
+ for (const branchId of selectedOrActiveBranchIds) {
+ const branch = froca.getBranch(branchId);
+ if (!branch) continue;
+ const note = await froca.getNote(branch.noteId);
+ if (!note) continue;
+ if (note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) continue;
+ noteCreateService.duplicateSubtree(branch.noteId, branch.parentNoteId);
+ }
+ }
+
+ async recentChangesInSubtreeCommand({ node, noteId }: CommandListenerData<"recentChangesInSubtree">) {
+ const ancestorNoteId = noteId ?? node?.data.noteId;
+ if (!ancestorNoteId) return;
+ this.triggerCommand("showRecentChanges", { ancestorNoteId });
+ }
}
diff --git a/apps/client/src/menus/tree_context_menu.ts b/apps/client/src/menus/tree_context_menu.ts
index 7547f2e16c..b22b875ef3 100644
--- a/apps/client/src/menus/tree_context_menu.ts
+++ b/apps/client/src/menus/tree_context_menu.ts
@@ -391,6 +391,7 @@ export async function handleTreeContextMenuSelect(
node: resolved.node,
notePath,
noteId: note.noteId,
+ branchId: branch.branchId,
selectedOrActiveBranchIds,
selectedOrActiveNoteIds
});
diff --git a/apps/client/src/widgets/dialogs/sort_child_notes.tsx b/apps/client/src/widgets/dialogs/sort_child_notes.tsx
index 10b69806c0..bd5a9e87b8 100644
--- a/apps/client/src/widgets/dialogs/sort_child_notes.tsx
+++ b/apps/client/src/widgets/dialogs/sort_child_notes.tsx
@@ -18,8 +18,10 @@ export default function SortChildNotesDialog() {
const [ sortLocale, setSortLocale ] = useState("");
const [ shown, setShown ] = useState(false);
- useTriliumEvent("sortChildNotes", ({ node }) => {
- setParentNoteId(node.data.noteId);
+ useTriliumEvent("sortChildNotes", ({ node, noteId }) => {
+ const targetNoteId = noteId ?? node?.data.noteId;
+ if (!targetNoteId) return;
+ setParentNoteId(targetNoteId);
setShown(true);
});
diff --git a/apps/client/src/widgets/note_tree.ts b/apps/client/src/widgets/note_tree.ts
index 27af4490ed..c934267349 100644
--- a/apps/client/src/widgets/note_tree.ts
+++ b/apps/client/src/widgets/note_tree.ts
@@ -13,7 +13,6 @@ import type FNote from "../entities/fnote.js";
import contextMenu from "../menus/context_menu.js";
import type { TreeCommandNames } from "../menus/tree_context_menu.js";
import branchService from "../services/branches.js";
-import clipboard from "../services/clipboard.js";
import dialogService from "../services/dialog.js";
import froca from "../services/froca.js";
import hoistedNoteService from "../services/hoisted_note.js";
@@ -24,8 +23,6 @@ import type LoadResults from "../services/load_results.js";
import type { AttributeRow, BranchRow } from "../services/load_results.js";
import noteCreateService from "../services/note_create.js";
import options from "../services/options.js";
-import protectedSessionService from "../services/protected_session.js";
-import protectedSessionHolder from "../services/protected_session_holder.js";
import server from "../services/server.js";
import shortcutService from "../services/shortcuts.js";
import toastService from "../services/toast.js";
@@ -1611,32 +1608,6 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
return nodes.map((node) => node.data.noteId);
}
- async deleteNotesCommand({ node }: CommandListenerData<"deleteNotes">) {
- const branchIds = this.getSelectedOrActiveBranchIds(node).filter((branchId) => !branchId.startsWith("virt-")); // search results can't be deleted
-
- if (!branchIds.length) {
- return;
- }
-
- await branchService.deleteNotes(branchIds);
-
- this.clearSelectedNodes();
- }
-
- async editBranchPrefixCommand({ node }: CommandListenerData<"editBranchPrefix">) {
- const branchIds = this.getSelectedOrActiveBranchIds(node).filter((branchId) => !branchId.startsWith("virt-"));
-
- if (!branchIds.length) {
- return;
- }
-
- // Trigger the event with the selected branch IDs
- appContext.triggerEvent("editBranchPrefix", {
- selectedOrActiveBranchIds: branchIds,
- node
- });
- }
-
canBeMovedUpOrDown(node: Fancytree.FancytreeNode) {
if (node.data.noteId === "root") {
return false;
@@ -1661,7 +1632,7 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
}
async moveNoteDownCommand({ node }: CommandListenerData<"moveNoteDown">) {
- if (!this.canBeMovedUpOrDown(node)) {
+ if (!node || !this.canBeMovedUpOrDown(node)) {
return;
}
@@ -1674,10 +1645,12 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
}
moveNoteUpInHierarchyCommand({ node }: CommandListenerData<"moveNoteUpInHierarchy">) {
+ if (!node) return;
branchService.moveNodeUpInHierarchy(node);
}
moveNoteDownInHierarchyCommand({ node }: CommandListenerData<"moveNoteDownInHierarchy">) {
+ if (!node) return;
const toNode = node.getPrevSibling();
if (toNode !== null) {
@@ -1741,72 +1714,17 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
this.collapseTree(node);
}
- async recentChangesInSubtreeCommand({ node }: CommandListenerData<"recentChangesInSubtree">) {
- this.triggerCommand("showRecentChanges", { ancestorNoteId: node.data.noteId });
- }
-
selectAllNotesInParentCommand({ node }: CommandListenerData<"selectAllNotesInParent">) {
+ if (!node) return;
for (const child of node.getParent().getChildren()) {
child.setSelected(true);
}
}
- copyNotesToClipboardCommand({ node }: CommandListenerData<"copyNotesToClipboard">) {
- clipboard.copy(this.getSelectedOrActiveBranchIds(node));
- }
-
- cutNotesToClipboardCommand({ node }: CommandListenerData<"cutNotesToClipboard">) {
- clipboard.cut(this.getSelectedOrActiveBranchIds(node));
- }
-
- pasteNotesFromClipboardCommand({ node }: CommandListenerData<"pasteNotesFromClipboard">) {
- clipboard.pasteInto(node.data.branchId);
- }
-
- pasteNotesAfterFromClipboardCommand({ node }: CommandListenerData<"pasteNotesAfterFromClipboard">) {
- clipboard.pasteAfter(node.data.branchId);
- }
-
- async exportNoteCommand({ node }: CommandListenerData<"exportNote">) {
- const notePath = treeService.getNotePath(node);
-
- this.triggerCommand("showExportDialog", { notePath, defaultType: "subtree" });
- }
-
- async importIntoNoteCommand({ node }: CommandListenerData<"importIntoNote">) {
- this.triggerCommand("showImportDialog", { noteId: node.data.noteId });
- }
-
editNoteTitleCommand() {
appContext.triggerCommand("focusOnTitle");
}
- protectSubtreeCommand({ node }: CommandListenerData<"protectSubtree">) {
- protectedSessionService.protectNote(node.data.noteId, true, true);
- }
-
- unprotectSubtreeCommand({ node }: CommandListenerData<"unprotectSubtree">) {
- protectedSessionService.protectNote(node.data.noteId, false, true);
- }
-
- duplicateSubtreeCommand({ node }: CommandListenerData<"duplicateSubtree">) {
- const nodesToDuplicate = this.getSelectedOrActiveNodes(node);
-
- for (const nodeToDuplicate of nodesToDuplicate) {
- const note = froca.getNoteFromCache(nodeToDuplicate.data.noteId);
-
- if (note?.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) {
- continue;
- }
-
- const branch = froca.getBranch(nodeToDuplicate.data.branchId);
-
- if (branch?.parentNoteId) {
- noteCreateService.duplicateSubtree(nodeToDuplicate.data.noteId, branch.parentNoteId);
- }
- }
- }
-
moveLauncherToVisibleCommand({ selectedOrActiveBranchIds }: CommandListenerData<"moveLauncherToVisible">) {
this.#moveLaunchers(selectedOrActiveBranchIds, "_lbVisibleLaunchers", "_lbMobileVisibleLaunchers");
}
@@ -1828,18 +1746,22 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
}
addNoteLauncherCommand({ node }: CommandListenerData<"addNoteLauncher">) {
+ if (!node) return;
this.createLauncherNote(node, "note");
}
addScriptLauncherCommand({ node }: CommandListenerData<"addScriptLauncher">) {
+ if (!node) return;
this.createLauncherNote(node, "script");
}
addWidgetLauncherCommand({ node }: CommandListenerData<"addWidgetLauncher">) {
+ if (!node) return;
this.createLauncherNote(node, "customWidget");
}
addSpacerLauncherCommand({ node }: CommandListenerData<"addSpacerLauncher">) {
+ if (!node) return;
this.createLauncherNote(node, "spacer");
}