From 19652fbbce3e981102ce85fa56cff9b5d969e6dd Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sun, 22 Dec 2024 18:33:57 +0200 Subject: [PATCH] refactor(client/ts): use command names enum in context menu --- src/public/app/components/app_context.ts | 23 ++++++++++++++++++- src/public/app/menus/context_menu.ts | 3 ++- src/public/app/menus/electron_context_menu.ts | 4 ++-- src/public/app/menus/launcher_context_menu.ts | 5 ++-- src/public/app/services/note_types.ts | 23 ++++++++++--------- 5 files changed, 41 insertions(+), 17 deletions(-) diff --git a/src/public/app/components/app_context.ts b/src/public/app/components/app_context.ts index d629c2e28..145be6504 100644 --- a/src/public/app/components/app_context.ts +++ b/src/public/app/components/app_context.ts @@ -69,19 +69,40 @@ type CommandMappings = { notePath: string; hoistedNoteId: string; viewScope: ViewScope; - } + }, + openNoteInNewTab: CommandData; + openNoteInNewSplit: CommandData; + openNoteInNewWindow: CommandData; + openNoteInSplit: CommandData; + openInTab: CommandData; + insertNoteAfter: CommandData; + insertChildNote: CommandData; + convertNoteToAttachment: CommandData; + copyNotePathToClipboard: CommandData; executeInActiveNoteDetailWidget: CommandData & { callback: (value: NoteDetailWidget | PromiseLike) => void }; addTextToActiveEditor: CommandData & { text: string; }; + /** Works only in the electron context menu. */ + replaceMisspelling: CommandData; importMarkdownInline: CommandData; showPasswordNotSet: CommandData; showProtectedSessionPasswordDialog: CommandData; closeProtectedSessionPasswordDialog: CommandData; resetLauncher: CommandData; + addNoteLauncher: CommandData; + addScriptLauncher: CommandData; + addWidgetLauncher: CommandData; + addSpacerLauncher: CommandData; + moveLauncherToVisible: CommandData; + moveLauncherToAvailable: CommandData; + duplicateSubtree: CommandData; + deleteNotes: CommandData; + copyImageReferenceToClipboard: CommandData; + copyImageToClipboard: CommandData; } type EventMappings = { diff --git a/src/public/app/menus/context_menu.ts b/src/public/app/menus/context_menu.ts index fd3021fd2..5bc43e76f 100644 --- a/src/public/app/menus/context_menu.ts +++ b/src/public/app/menus/context_menu.ts @@ -1,3 +1,4 @@ +import { CommandNames } from '../components/app_context.js'; import keyboardActionService from '../services/keyboard_actions.js'; interface ContextMenuOptions { @@ -14,7 +15,7 @@ interface MenuSeparatorItem { export interface MenuCommandItem { title: string; - command?: string; + command?: CommandNames; type?: string; uiIcon?: string; templateNoteId?: string; diff --git a/src/public/app/menus/electron_context_menu.ts b/src/public/app/menus/electron_context_menu.ts index faa10a62a..68bab3636 100644 --- a/src/public/app/menus/electron_context_menu.ts +++ b/src/public/app/menus/electron_context_menu.ts @@ -1,7 +1,7 @@ import utils from "../services/utils.js"; import options from "../services/options.js"; import zoomService from "../components/zoom.js"; -import contextMenu from "./context_menu.js"; +import contextMenu, { MenuItem } from "./context_menu.js"; import { t } from "../services/i18n.js"; import type { BrowserWindow } from "electron"; @@ -18,7 +18,7 @@ function setupContextMenu() { const isMac = process.platform === "darwin"; const platformModifier = isMac ? 'Meta' : 'Ctrl'; - const items = []; + const items: MenuItem[] = []; if (params.misspelledWord) { for (const suggestion of params.dictionarySuggestions) { diff --git a/src/public/app/menus/launcher_context_menu.ts b/src/public/app/menus/launcher_context_menu.ts index b67a9b9c9..4b3d48587 100644 --- a/src/public/app/menus/launcher_context_menu.ts +++ b/src/public/app/menus/launcher_context_menu.ts @@ -38,7 +38,7 @@ export default class LauncherContextMenu implements SelectMenuItemEventListener const canBeDeleted = !note?.noteId.startsWith("_"); // fixed notes can't be deleted const canBeReset = !canBeDeleted && note?.isLaunchBarConfig(); - return [ + const items: (MenuItem | null)[] = [ (isVisibleRoot || isAvailableRoot) ? { title: t("launcher_context_menu.add-note-launcher"), command: 'addNoteLauncher', uiIcon: "bx bx-note" } : null, (isVisibleRoot || isAvailableRoot) ? { title: t("launcher_context_menu.add-script-launcher"), command: 'addScriptLauncher', uiIcon: "bx bx-code-curly" } : null, (isVisibleRoot || isAvailableRoot) ? { title: t("launcher_context_menu.add-custom-widget"), command: 'addWidgetLauncher', uiIcon: "bx bx-customize" } : null, @@ -55,7 +55,8 @@ export default class LauncherContextMenu implements SelectMenuItemEventListener { title: "----" }, { title: t("launcher_context_menu.reset"), command: "resetLauncher", uiIcon: "bx bx-reset destructive-action-icon", enabled: canBeReset} - ].filter(row => row !== null); + ]; + return items.filter(row => row !== null); } async selectMenuItemHandler({command}: MenuCommandItem) { diff --git a/src/public/app/services/note_types.ts b/src/public/app/services/note_types.ts index cb491b4c4..722061f18 100644 --- a/src/public/app/services/note_types.ts +++ b/src/public/app/services/note_types.ts @@ -2,19 +2,20 @@ import server from "./server.js"; import froca from "./froca.js"; import { t } from "./i18n.js"; import { MenuItem } from "../menus/context_menu.js"; +import { CommandNames } from "../components/app_context.js"; -async function getNoteTypeItems(command?: string) { +async function getNoteTypeItems(command?: CommandNames) { const items: MenuItem[] = [ - { title: t("note_types.text"), command: command, type: "text", uiIcon: "bx bx-note" }, - { title: t("note_types.code"), command: command, type: "code", uiIcon: "bx bx-code" }, - { title: t("note_types.saved-search"), command: command, type: "search", uiIcon: "bx bx-file-find" }, - { title: t("note_types.relation-map"), command: command, type: "relationMap", uiIcon: "bx bxs-network-chart" }, - { title: t("note_types.note-map"), command: command, type: "noteMap", uiIcon: "bx bxs-network-chart" }, - { title: t("note_types.render-note"), command: command, type: "render", uiIcon: "bx bx-extension" }, - { title: t("note_types.book"), command: command, type: "book", uiIcon: "bx bx-book" }, - { title: t("note_types.mermaid-diagram"), command: command, type: "mermaid", uiIcon: "bx bx-selection" }, - { title: t("note_types.canvas"), command: command, type: "canvas", uiIcon: "bx bx-pen" }, - { title: t("note_types.web-view"), command: command, type: "webView", uiIcon: "bx bx-globe-alt" }, + { title: t("note_types.text"), command, type: "text", uiIcon: "bx bx-note" }, + { title: t("note_types.code"), command, type: "code", uiIcon: "bx bx-code" }, + { title: t("note_types.saved-search"), command, type: "search", uiIcon: "bx bx-file-find" }, + { title: t("note_types.relation-map"), command, type: "relationMap", uiIcon: "bx bxs-network-chart" }, + { title: t("note_types.note-map"), command, type: "noteMap", uiIcon: "bx bxs-network-chart" }, + { title: t("note_types.render-note"), command, type: "render", uiIcon: "bx bx-extension" }, + { title: t("note_types.book"), command, type: "book", uiIcon: "bx bx-book" }, + { title: t("note_types.mermaid-diagram"), command, type: "mermaid", uiIcon: "bx bx-selection" }, + { title: t("note_types.canvas"), command, type: "canvas", uiIcon: "bx bx-pen" }, + { title: t("note_types.web-view"), command, type: "webView", uiIcon: "bx bx-globe-alt" }, { title: t("note_types.mind-map"), command, type: "mindMap", uiIcon: "bx bx-sitemap" } ];