From 66e499a2e14ea3cd798f25b8248c2a9ec4fed494 Mon Sep 17 00:00:00 2001 From: Jakob Schlanstedt Date: Tue, 28 Oct 2025 18:26:54 +0100 Subject: [PATCH] refactor(note-create): replace enum with optional fields --- apps/client/src/components/entrypoints.ts | 4 ++-- .../src/components/main_tree_executors.ts | 6 +++--- .../src/components/root_command_executor.ts | 4 ++-- apps/client/src/menus/tree_context_menu.ts | 6 +++--- apps/client/src/services/note_autocomplete.ts | 10 +++++----- apps/client/src/services/note_create.ts | 19 ++++++------------- .../src/widgets/collections/board/api.ts | 6 +++--- .../widgets/collections/board/context_menu.ts | 5 ++--- .../widgets/collections/table/context_menu.ts | 8 ++++---- .../widgets/collections/table/row_editing.ts | 2 +- .../mobile_widgets/mobile_detail_menu.tsx | 4 ++-- apps/client/src/widgets/note_tree.ts | 6 +++--- .../ribbon/components/AttributeEditor.tsx | 6 +++--- 13 files changed, 39 insertions(+), 47 deletions(-) diff --git a/apps/client/src/components/entrypoints.ts b/apps/client/src/components/entrypoints.ts index 1815ac388..5fc9493c3 100644 --- a/apps/client/src/components/entrypoints.ts +++ b/apps/client/src/components/entrypoints.ts @@ -11,7 +11,7 @@ import froca from "../services/froca.js"; import linkService from "../services/link.js"; import { t } from "../services/i18n.js"; import { CreateChildrenResponse, SqlExecuteResponse } from "@triliumnext/commons"; -import noteCreateService, { CreateNoteIntoInboxOpts, CreateNoteTarget } from "../services/note_create.js"; +import noteCreateService, { CreateNoteIntoInboxOpts } from "../services/note_create.js"; export default class Entrypoints extends Component { constructor() { @@ -26,7 +26,7 @@ export default class Entrypoints extends Component { async createNoteIntoInboxCommand() { await noteCreateService.createNote( - { target: CreateNoteTarget.IntoInbox } as CreateNoteIntoInboxOpts + { target: "into" } as CreateNoteIntoInboxOpts ); } diff --git a/apps/client/src/components/main_tree_executors.ts b/apps/client/src/components/main_tree_executors.ts index f11e5aecb..20f0abea5 100644 --- a/apps/client/src/components/main_tree_executors.ts +++ b/apps/client/src/components/main_tree_executors.ts @@ -1,5 +1,5 @@ import appContext, { type EventData } from "./app_context.js"; -import noteCreateService, { CreateNoteTarget, CreateNoteIntoUrlOpts, CreateNoteAfterUrlOpts } from "../services/note_create.js"; +import noteCreateService, { CreateNoteIntoUrlOpts, CreateNoteAfterUrlOpts } from "../services/note_create.js"; import treeService from "../services/tree.js"; import hoistedNoteService from "../services/hoisted_note.js"; import Component from "./component.js"; @@ -50,7 +50,7 @@ export default class MainTreeExecutors extends Component { await noteCreateService.createNote( { - target: CreateNoteTarget.IntoNoteURL, + target: "into", parentNoteUrl: activeNoteContext.notePath, isProtected: activeNoteContext.note.isProtected, saveSelection: false, @@ -79,7 +79,7 @@ export default class MainTreeExecutors extends Component { await noteCreateService.createNote( { - target: CreateNoteTarget.AfterNoteURL, + target: "after", parentNoteUrl: parentNotePath, targetBranchId: node.data.branchId, isProtected: isProtected, diff --git a/apps/client/src/components/root_command_executor.ts b/apps/client/src/components/root_command_executor.ts index 7d1b62876..b077b7e57 100644 --- a/apps/client/src/components/root_command_executor.ts +++ b/apps/client/src/components/root_command_executor.ts @@ -8,7 +8,7 @@ import options from "../services/options.js"; import froca from "../services/froca.js"; import utils from "../services/utils.js"; import toastService from "../services/toast.js"; -import noteCreateService, { CreateNoteIntoUrlOpts, CreateNoteTarget } from "../services/note_create.js"; +import noteCreateService, { CreateNoteIntoUrlOpts } from "../services/note_create.js"; export default class RootCommandExecutor extends Component { editReadOnlyNoteCommand() { @@ -242,7 +242,7 @@ export default class RootCommandExecutor extends Component { const result = await noteCreateService.createNote( { - target: CreateNoteTarget.IntoNoteURL, + target: "into", title: "New AI Chat", type: "aiChat", content: JSON.stringify({ diff --git a/apps/client/src/menus/tree_context_menu.ts b/apps/client/src/menus/tree_context_menu.ts index 909699d4b..3c9d18a75 100644 --- a/apps/client/src/menus/tree_context_menu.ts +++ b/apps/client/src/menus/tree_context_menu.ts @@ -2,7 +2,7 @@ import NoteColorPicker from "./custom-items/NoteColorPicker.jsx"; import treeService from "../services/tree.js"; import froca from "../services/froca.js"; import clipboard from "../services/clipboard.js"; -import noteCreateService, { CreateNoteAfterUrlOpts, CreateNoteIntoUrlOpts, CreateNoteTarget } from "../services/note_create.js"; +import noteCreateService, { CreateNoteAfterUrlOpts, CreateNoteIntoUrlOpts } from "../services/note_create.js"; import contextMenu, { type MenuCommandItem, type MenuItem } from "./context_menu.js"; import appContext, { type ContextMenuCommandData, type FilteredCommandNames } from "../components/app_context.js"; import noteTypesService from "../services/note_types.js"; @@ -286,7 +286,7 @@ export default class TreeContextMenu implements SelectMenuItemEventListener, options?: Options) { case SuggestionAction.CreateNoteIntoInbox: { const { note } = await noteCreateService.createNote( { - target: CreateNoteTarget.IntoInbox, + target: "inbox", title: suggestion.noteTitle, activate: true, promptForType: true, @@ -503,7 +503,7 @@ function initNoteAutocomplete($el: JQuery, options?: Options) { case SuggestionAction.CreateAndLinkNoteIntoInbox: { const { note } = await noteCreateService.createNote( { - target: CreateNoteTarget.IntoInbox, + target: "inbox", title: suggestion.noteTitle, activate: false, promptForType: true, @@ -523,7 +523,7 @@ function initNoteAutocomplete($el: JQuery, options?: Options) { case SuggestionAction.CreateNoteIntoPath: { const { note } = await noteCreateService.createNote( { - target: CreateNoteTarget.IntoNoteURL, + target: "into", parentNoteUrl: suggestion.parentNoteId, title: suggestion.noteTitle, activate: true, @@ -544,7 +544,7 @@ function initNoteAutocomplete($el: JQuery, options?: Options) { case SuggestionAction.CreateAndLinkNoteIntoPath: { const { note } = await noteCreateService.createNote( { - target: CreateNoteTarget.IntoNoteURL, + target: "into", parentNoteUrl: suggestion.parentNoteId, title: suggestion.noteTitle, activate: false, diff --git a/apps/client/src/services/note_create.ts b/apps/client/src/services/note_create.ts index 857c7a42b..906afec57 100644 --- a/apps/client/src/services/note_create.ts +++ b/apps/client/src/services/note_create.ts @@ -63,7 +63,7 @@ type PromptingRule = { * Combine with `&` to ensure valid logical combinations. */ export type CreateNoteOpts = { - target: CreateNoteTarget; + target: "into" | "after" | "before" | "inbox"; isProtected?: boolean; saveSelection?: boolean; title?: string | null; @@ -99,13 +99,6 @@ type NeverDefineParentNoteUrlRule = { }; export type CreateNoteIntoInboxOpts = CreateNoteOpts & NeverDefineParentNoteUrlRule; -export enum CreateNoteTarget { - IntoNoteURL, - AfterNoteURL, - BeforeNoteURL, - IntoInbox, -} - interface Response { // TODO: Deduplicate with server once we have client/server architecture. note: FNote; @@ -136,16 +129,16 @@ async function createNote( } switch (resolvedOptions.target) { - case CreateNoteTarget.IntoNoteURL: + case "into": return await createNoteAtNote("into", {...options} as CreateNoteAtUrlOpts); - case CreateNoteTarget.BeforeNoteURL: + case "before": return await createNoteAtNote("before", resolvedOptions as CreateNoteBeforeUrlOpts); - case CreateNoteTarget.AfterNoteURL: + case "after": return await createNoteAtNote("after", resolvedOptions as CreateNoteAfterUrlOpts); - case CreateNoteTarget.IntoInbox: + case "inbox": return await createNoteIntoInbox(resolvedOptions as CreateNoteIntoInboxOpts); default: { @@ -176,7 +169,7 @@ async function promptForType( resolvedOptions = resolvedOptions as CreateNoteIntoUrlOpts; resolvedOptions = { ...resolvedOptions, - target: CreateNoteTarget.IntoNoteURL, + target: "into", parentNoteUrl: notePath, } as CreateNoteIntoUrlOpts; } diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.ts index 897edb8b3..224b57335 100644 --- a/apps/client/src/widgets/collections/board/api.ts +++ b/apps/client/src/widgets/collections/board/api.ts @@ -7,7 +7,7 @@ import branches from "../../../services/branches"; import { executeBulkActions } from "../../../services/bulk_action"; import froca from "../../../services/froca"; import { t } from "../../../services/i18n"; -import note_create, { CreateNoteIntoUrlOpts, CreateNoteTarget } from "../../../services/note_create.js"; +import note_create, { CreateNoteIntoUrlOpts } from "../../../services/note_create.js"; import server from "../../../services/server"; import { ColumnMap } from "./data"; @@ -40,7 +40,7 @@ export default class BoardApi { // Create a new note as a child of the parent note const { note: newNote, branch: newBranch } = await note_create.createNote({ - target: CreateNoteTarget.IntoNoteURL, + target: "into", parentNoteUrl: parentNotePath, activate: false, title, @@ -141,7 +141,7 @@ export default class BoardApi { async insertRowAtPosition( column: string, relativeToBranchId: string, - direction: CreateNoteTarget.BeforeNoteURL | CreateNoteTarget.AfterNoteURL + direction: "before" | "after" ) { const { note, branch } = await note_create.createNote( { diff --git a/apps/client/src/widgets/collections/board/context_menu.ts b/apps/client/src/widgets/collections/board/context_menu.ts index 23f4cf6b1..4a8f49e63 100644 --- a/apps/client/src/widgets/collections/board/context_menu.ts +++ b/apps/client/src/widgets/collections/board/context_menu.ts @@ -6,7 +6,6 @@ import attributes from "../../../services/attributes"; import branches from "../../../services/branches"; import dialog from "../../../services/dialog"; import { t } from "../../../services/i18n"; -import { CreateNoteTarget } from "../../../services/note_create"; import Api from "./api"; export function openColumnContextMenu(api: Api, event: ContextMenuEvent, column: string) { @@ -61,7 +60,7 @@ export function openNoteContextMenu(api: Api, event: ContextMenuEvent, note: FNo handler: () => api.insertRowAtPosition( column, branchId, - CreateNoteTarget.BeforeNoteURL) + "before") }, { title: t("board_view.insert-below"), @@ -69,7 +68,7 @@ export function openNoteContextMenu(api: Api, event: ContextMenuEvent, note: FNo handler: () => api.insertRowAtPosition( column, branchId, - CreateNoteTarget.AfterNoteURL) + "after") }, { kind: "separator" }, { diff --git a/apps/client/src/widgets/collections/table/context_menu.ts b/apps/client/src/widgets/collections/table/context_menu.ts index 016bc33e4..aff7ff28b 100644 --- a/apps/client/src/widgets/collections/table/context_menu.ts +++ b/apps/client/src/widgets/collections/table/context_menu.ts @@ -9,7 +9,7 @@ import branches from "../../../services/branches.js"; import Component from "../../../components/component.js"; import NoteColorPicker from "../../../menus/custom-items/NoteColorPicker.jsx"; import { RefObject } from "preact"; -import { CreateNoteAfterUrlOpts, CreateNoteBeforeUrlOpts, CreateNoteTarget } from "../../../services/note_create.js"; +import { CreateNoteAfterUrlOpts, CreateNoteBeforeUrlOpts } from "../../../services/note_create.js"; export function useContextMenu(parentNote: FNote, parentComponent: Component | null | undefined, tabulator: RefObject): Partial { const events: Partial = {}; @@ -184,7 +184,7 @@ export function showRowContextMenu(parentComponent: Component, e: MouseEvent, ro handler: () => parentComponent?.triggerCommand("addNewRow", { parentNotePath: parentNoteId, customOpts: { - target: CreateNoteTarget.BeforeNoteURL, + target: "before", targetBranchId: rowData.branchId, } as CreateNoteBeforeUrlOpts }) @@ -198,7 +198,7 @@ export function showRowContextMenu(parentComponent: Component, e: MouseEvent, ro parentComponent?.triggerCommand("addNewRow", { parentNotePath: note?.noteId, customOpts: { - target: CreateNoteTarget.AfterNoteURL, + target: "after", targetBranchId: branchId, } as CreateNoteAfterUrlOpts }); @@ -211,7 +211,7 @@ export function showRowContextMenu(parentComponent: Component, e: MouseEvent, ro handler: () => parentComponent?.triggerCommand("addNewRow", { parentNotePath: parentNoteId, customOpts: { - target: CreateNoteTarget.AfterNoteURL, + target: "after", targetBranchId: rowData.branchId, } as CreateNoteAfterUrlOpts }) diff --git a/apps/client/src/widgets/collections/table/row_editing.ts b/apps/client/src/widgets/collections/table/row_editing.ts index 220f13513..893a4acb4 100644 --- a/apps/client/src/widgets/collections/table/row_editing.ts +++ b/apps/client/src/widgets/collections/table/row_editing.ts @@ -1,6 +1,6 @@ import { EventCallBackMethods, RowComponent, Tabulator } from "tabulator-tables"; import { CommandListenerData } from "../../../components/app_context"; -import note_create, { CreateNoteOpts, CreateNoteIntoUrlOpts as CreateNoteIntoUrlOpts, CreateNoteTarget } from "../../../services/note_create"; +import note_create, { CreateNoteOpts, CreateNoteIntoUrlOpts as CreateNoteIntoUrlOpts } from "../../../services/note_create"; import { useLegacyImperativeHandlers } from "../../react/hooks"; import { RefObject } from "preact"; import { setAttribute, setLabel } from "../../../services/attributes"; diff --git a/apps/client/src/widgets/mobile_widgets/mobile_detail_menu.tsx b/apps/client/src/widgets/mobile_widgets/mobile_detail_menu.tsx index 87ed7dbb6..42cb0b85d 100644 --- a/apps/client/src/widgets/mobile_widgets/mobile_detail_menu.tsx +++ b/apps/client/src/widgets/mobile_widgets/mobile_detail_menu.tsx @@ -3,7 +3,7 @@ import appContext from "../../components/app_context"; import contextMenu from "../../menus/context_menu"; import branches from "../../services/branches"; import { t } from "../../services/i18n"; -import note_create, { CreateNoteIntoUrlOpts, CreateNoteTarget } from "../../services/note_create"; +import note_create, { CreateNoteIntoUrlOpts } from "../../services/note_create"; import tree from "../../services/tree"; import ActionButton from "../react/ActionButton"; import { ParentComponent } from "../react/react_utils"; @@ -31,7 +31,7 @@ export default function MobileDetailMenu() { if (command === "insertChildNote") { note_create.createNote( { - target: CreateNoteTarget.IntoNoteURL, + target: "into", parentNoteUrl: appContext.tabManager.getActiveContextNotePath() ?? undefined } as CreateNoteIntoUrlOpts ); diff --git a/apps/client/src/widgets/note_tree.ts b/apps/client/src/widgets/note_tree.ts index 6c58767bf..37a96f671 100644 --- a/apps/client/src/widgets/note_tree.ts +++ b/apps/client/src/widgets/note_tree.ts @@ -7,7 +7,7 @@ import branchService from "../services/branches.js"; import ws from "../services/ws.js"; import NoteContextAwareWidget from "./note_context_aware_widget.js"; import server from "../services/server.js"; -import noteCreateService, { CreateNoteIntoUrlOpts, CreateNoteTarget } from "../services/note_create.js"; +import noteCreateService, { CreateNoteIntoUrlOpts } from "../services/note_create.js"; import toastService from "../services/toast.js"; import appContext, { type CommandListenerData, type EventData } from "../components/app_context.js"; import keyboardActionsService from "../services/keyboard_actions.js"; @@ -226,7 +226,7 @@ export default class NoteTreeWidget extends NoteContextAwareWidget { const parentNotePath = treeService.getNotePath(node); noteCreateService.createNote( { - target: CreateNoteTarget.IntoNoteURL, + target: "into", parentNoteUrl: parentNotePath, isProtected: node.data.isProtected } as CreateNoteIntoUrlOpts, @@ -1844,7 +1844,7 @@ export default class NoteTreeWidget extends NoteContextAwareWidget { const notePath = treeService.getNotePath(node); noteCreateService.createNote( { - target: CreateNoteTarget.IntoNoteURL, + target: "into", parentNoteUrl: notePath, isProtected: node.data.isProtected } as CreateNoteIntoUrlOpts diff --git a/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx b/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx index a9513bc2d..45b56a04d 100644 --- a/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx +++ b/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx @@ -19,7 +19,7 @@ import contextMenu from "../../../menus/context_menu"; import type { CommandData, FilteredCommandNames } from "../../../components/app_context"; import { AttributeType } from "@triliumnext/commons"; import attributes from "../../../services/attributes"; -import note_create, { CreateNoteAfterUrlOpts, CreateNoteIntoUrlOpts, CreateNoteTarget, CreateNoteIntoInboxOpts } from "../../../services/note_create"; +import note_create, { CreateNoteAfterUrlOpts, CreateNoteIntoUrlOpts, CreateNoteIntoInboxOpts } from "../../../services/note_create"; import { CreateNoteAction } from "@triliumnext/commons"; type AttributeCommandNames = FilteredCommandNames; @@ -263,7 +263,7 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxI case CreateNoteAction.CreateAndLinkNoteIntoInbox: { const { note } = await note_create.createNote( { - target: CreateNoteTarget.IntoInbox, + target: "inbox", title, activate: false } as CreateNoteIntoInboxOpts @@ -275,7 +275,7 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxI case CreateNoteAction.CreateAndLinkNoteIntoPath: { const resp = await note_create.createNote( { - target: CreateNoteTarget.IntoNoteURL, + target: "into", parentNoteUrl: parentNotePath, title, activate: false,