refactor(note-create): replace enum with optional fields

This commit is contained in:
Jakob Schlanstedt 2025-10-28 18:26:54 +01:00
parent a5ef5eee2f
commit 66e499a2e1
13 changed files with 39 additions and 47 deletions

View File

@ -11,7 +11,7 @@ import froca from "../services/froca.js";
import linkService from "../services/link.js"; import linkService from "../services/link.js";
import { t } from "../services/i18n.js"; import { t } from "../services/i18n.js";
import { CreateChildrenResponse, SqlExecuteResponse } from "@triliumnext/commons"; 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 { export default class Entrypoints extends Component {
constructor() { constructor() {
@ -26,7 +26,7 @@ export default class Entrypoints extends Component {
async createNoteIntoInboxCommand() { async createNoteIntoInboxCommand() {
await noteCreateService.createNote( await noteCreateService.createNote(
{ target: CreateNoteTarget.IntoInbox } as CreateNoteIntoInboxOpts { target: "into" } as CreateNoteIntoInboxOpts
); );
} }

View File

@ -1,5 +1,5 @@
import appContext, { type EventData } from "./app_context.js"; 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 treeService from "../services/tree.js";
import hoistedNoteService from "../services/hoisted_note.js"; import hoistedNoteService from "../services/hoisted_note.js";
import Component from "./component.js"; import Component from "./component.js";
@ -50,7 +50,7 @@ export default class MainTreeExecutors extends Component {
await noteCreateService.createNote( await noteCreateService.createNote(
{ {
target: CreateNoteTarget.IntoNoteURL, target: "into",
parentNoteUrl: activeNoteContext.notePath, parentNoteUrl: activeNoteContext.notePath,
isProtected: activeNoteContext.note.isProtected, isProtected: activeNoteContext.note.isProtected,
saveSelection: false, saveSelection: false,
@ -79,7 +79,7 @@ export default class MainTreeExecutors extends Component {
await noteCreateService.createNote( await noteCreateService.createNote(
{ {
target: CreateNoteTarget.AfterNoteURL, target: "after",
parentNoteUrl: parentNotePath, parentNoteUrl: parentNotePath,
targetBranchId: node.data.branchId, targetBranchId: node.data.branchId,
isProtected: isProtected, isProtected: isProtected,

View File

@ -8,7 +8,7 @@ import options from "../services/options.js";
import froca from "../services/froca.js"; import froca from "../services/froca.js";
import utils from "../services/utils.js"; import utils from "../services/utils.js";
import toastService from "../services/toast.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 { export default class RootCommandExecutor extends Component {
editReadOnlyNoteCommand() { editReadOnlyNoteCommand() {
@ -242,7 +242,7 @@ export default class RootCommandExecutor extends Component {
const result = await noteCreateService.createNote( const result = await noteCreateService.createNote(
{ {
target: CreateNoteTarget.IntoNoteURL, target: "into",
title: "New AI Chat", title: "New AI Chat",
type: "aiChat", type: "aiChat",
content: JSON.stringify({ content: JSON.stringify({

View File

@ -2,7 +2,7 @@ import NoteColorPicker from "./custom-items/NoteColorPicker.jsx";
import treeService from "../services/tree.js"; import treeService from "../services/tree.js";
import froca from "../services/froca.js"; import froca from "../services/froca.js";
import clipboard from "../services/clipboard.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 contextMenu, { type MenuCommandItem, type MenuItem } from "./context_menu.js";
import appContext, { type ContextMenuCommandData, type FilteredCommandNames } from "../components/app_context.js"; import appContext, { type ContextMenuCommandData, type FilteredCommandNames } from "../components/app_context.js";
import noteTypesService from "../services/note_types.js"; import noteTypesService from "../services/note_types.js";
@ -286,7 +286,7 @@ export default class TreeContextMenu implements SelectMenuItemEventListener<Tree
noteCreateService.createNote( noteCreateService.createNote(
{ {
target: CreateNoteTarget.AfterNoteURL, target: "after",
parentNoteUrl: parentNotePath, parentNoteUrl: parentNotePath,
targetBranchId: this.node.data.branchId, targetBranchId: this.node.data.branchId,
type: type, type: type,
@ -300,7 +300,7 @@ export default class TreeContextMenu implements SelectMenuItemEventListener<Tree
noteCreateService.createNote( noteCreateService.createNote(
{ {
target: CreateNoteTarget.IntoNoteURL, target: "into",
parentNoteUrl: parentNotePath, parentNoteUrl: parentNotePath,
type: type, type: type,
isProtected: this.node.data.isProtected, isProtected: this.node.data.isProtected,

View File

@ -1,6 +1,6 @@
import server from "./server.js"; import server from "./server.js";
import appContext from "../components/app_context.js"; import appContext from "../components/app_context.js";
import noteCreateService, { CreateNoteIntoUrlOpts, CreateNoteTarget, CreateNoteIntoInboxOpts } from "./note_create.js"; import noteCreateService, { CreateNoteIntoUrlOpts, CreateNoteIntoInboxOpts } from "./note_create.js";
import froca from "./froca.js"; import froca from "./froca.js";
import { t } from "./i18n.js"; import { t } from "./i18n.js";
import commandRegistry from "./command_registry.js"; import commandRegistry from "./command_registry.js";
@ -483,7 +483,7 @@ function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
case SuggestionAction.CreateNoteIntoInbox: { case SuggestionAction.CreateNoteIntoInbox: {
const { note } = await noteCreateService.createNote( const { note } = await noteCreateService.createNote(
{ {
target: CreateNoteTarget.IntoInbox, target: "inbox",
title: suggestion.noteTitle, title: suggestion.noteTitle,
activate: true, activate: true,
promptForType: true, promptForType: true,
@ -503,7 +503,7 @@ function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
case SuggestionAction.CreateAndLinkNoteIntoInbox: { case SuggestionAction.CreateAndLinkNoteIntoInbox: {
const { note } = await noteCreateService.createNote( const { note } = await noteCreateService.createNote(
{ {
target: CreateNoteTarget.IntoInbox, target: "inbox",
title: suggestion.noteTitle, title: suggestion.noteTitle,
activate: false, activate: false,
promptForType: true, promptForType: true,
@ -523,7 +523,7 @@ function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
case SuggestionAction.CreateNoteIntoPath: { case SuggestionAction.CreateNoteIntoPath: {
const { note } = await noteCreateService.createNote( const { note } = await noteCreateService.createNote(
{ {
target: CreateNoteTarget.IntoNoteURL, target: "into",
parentNoteUrl: suggestion.parentNoteId, parentNoteUrl: suggestion.parentNoteId,
title: suggestion.noteTitle, title: suggestion.noteTitle,
activate: true, activate: true,
@ -544,7 +544,7 @@ function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
case SuggestionAction.CreateAndLinkNoteIntoPath: { case SuggestionAction.CreateAndLinkNoteIntoPath: {
const { note } = await noteCreateService.createNote( const { note } = await noteCreateService.createNote(
{ {
target: CreateNoteTarget.IntoNoteURL, target: "into",
parentNoteUrl: suggestion.parentNoteId, parentNoteUrl: suggestion.parentNoteId,
title: suggestion.noteTitle, title: suggestion.noteTitle,
activate: false, activate: false,

View File

@ -63,7 +63,7 @@ type PromptingRule = {
* Combine with `&` to ensure valid logical combinations. * Combine with `&` to ensure valid logical combinations.
*/ */
export type CreateNoteOpts = { export type CreateNoteOpts = {
target: CreateNoteTarget; target: "into" | "after" | "before" | "inbox";
isProtected?: boolean; isProtected?: boolean;
saveSelection?: boolean; saveSelection?: boolean;
title?: string | null; title?: string | null;
@ -99,13 +99,6 @@ type NeverDefineParentNoteUrlRule = {
}; };
export type CreateNoteIntoInboxOpts = CreateNoteOpts & NeverDefineParentNoteUrlRule; export type CreateNoteIntoInboxOpts = CreateNoteOpts & NeverDefineParentNoteUrlRule;
export enum CreateNoteTarget {
IntoNoteURL,
AfterNoteURL,
BeforeNoteURL,
IntoInbox,
}
interface Response { interface Response {
// TODO: Deduplicate with server once we have client/server architecture. // TODO: Deduplicate with server once we have client/server architecture.
note: FNote; note: FNote;
@ -136,16 +129,16 @@ async function createNote(
} }
switch (resolvedOptions.target) { switch (resolvedOptions.target) {
case CreateNoteTarget.IntoNoteURL: case "into":
return await createNoteAtNote("into", {...options} as CreateNoteAtUrlOpts); return await createNoteAtNote("into", {...options} as CreateNoteAtUrlOpts);
case CreateNoteTarget.BeforeNoteURL: case "before":
return await createNoteAtNote("before", resolvedOptions as CreateNoteBeforeUrlOpts); return await createNoteAtNote("before", resolvedOptions as CreateNoteBeforeUrlOpts);
case CreateNoteTarget.AfterNoteURL: case "after":
return await createNoteAtNote("after", resolvedOptions as CreateNoteAfterUrlOpts); return await createNoteAtNote("after", resolvedOptions as CreateNoteAfterUrlOpts);
case CreateNoteTarget.IntoInbox: case "inbox":
return await createNoteIntoInbox(resolvedOptions as CreateNoteIntoInboxOpts); return await createNoteIntoInbox(resolvedOptions as CreateNoteIntoInboxOpts);
default: { default: {
@ -176,7 +169,7 @@ async function promptForType(
resolvedOptions = resolvedOptions as CreateNoteIntoUrlOpts; resolvedOptions = resolvedOptions as CreateNoteIntoUrlOpts;
resolvedOptions = { resolvedOptions = {
...resolvedOptions, ...resolvedOptions,
target: CreateNoteTarget.IntoNoteURL, target: "into",
parentNoteUrl: notePath, parentNoteUrl: notePath,
} as CreateNoteIntoUrlOpts; } as CreateNoteIntoUrlOpts;
} }

View File

@ -7,7 +7,7 @@ import branches from "../../../services/branches";
import { executeBulkActions } from "../../../services/bulk_action"; import { executeBulkActions } from "../../../services/bulk_action";
import froca from "../../../services/froca"; import froca from "../../../services/froca";
import { t } from "../../../services/i18n"; 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 server from "../../../services/server";
import { ColumnMap } from "./data"; import { ColumnMap } from "./data";
@ -40,7 +40,7 @@ export default class BoardApi {
// Create a new note as a child of the parent note // Create a new note as a child of the parent note
const { note: newNote, branch: newBranch } = await note_create.createNote({ const { note: newNote, branch: newBranch } = await note_create.createNote({
target: CreateNoteTarget.IntoNoteURL, target: "into",
parentNoteUrl: parentNotePath, parentNoteUrl: parentNotePath,
activate: false, activate: false,
title, title,
@ -141,7 +141,7 @@ export default class BoardApi {
async insertRowAtPosition( async insertRowAtPosition(
column: string, column: string,
relativeToBranchId: string, relativeToBranchId: string,
direction: CreateNoteTarget.BeforeNoteURL | CreateNoteTarget.AfterNoteURL direction: "before" | "after"
) { ) {
const { note, branch } = await note_create.createNote( const { note, branch } = await note_create.createNote(
{ {

View File

@ -6,7 +6,6 @@ import attributes from "../../../services/attributes";
import branches from "../../../services/branches"; import branches from "../../../services/branches";
import dialog from "../../../services/dialog"; import dialog from "../../../services/dialog";
import { t } from "../../../services/i18n"; import { t } from "../../../services/i18n";
import { CreateNoteTarget } from "../../../services/note_create";
import Api from "./api"; import Api from "./api";
export function openColumnContextMenu(api: Api, event: ContextMenuEvent, column: string) { 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( handler: () => api.insertRowAtPosition(
column, column,
branchId, branchId,
CreateNoteTarget.BeforeNoteURL) "before")
}, },
{ {
title: t("board_view.insert-below"), title: t("board_view.insert-below"),
@ -69,7 +68,7 @@ export function openNoteContextMenu(api: Api, event: ContextMenuEvent, note: FNo
handler: () => api.insertRowAtPosition( handler: () => api.insertRowAtPosition(
column, column,
branchId, branchId,
CreateNoteTarget.AfterNoteURL) "after")
}, },
{ kind: "separator" }, { kind: "separator" },
{ {

View File

@ -9,7 +9,7 @@ import branches from "../../../services/branches.js";
import Component from "../../../components/component.js"; import Component from "../../../components/component.js";
import NoteColorPicker from "../../../menus/custom-items/NoteColorPicker.jsx"; import NoteColorPicker from "../../../menus/custom-items/NoteColorPicker.jsx";
import { RefObject } from "preact"; 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<Tabulator>): Partial<EventCallBackMethods> { export function useContextMenu(parentNote: FNote, parentComponent: Component | null | undefined, tabulator: RefObject<Tabulator>): Partial<EventCallBackMethods> {
const events: Partial<EventCallBackMethods> = {}; const events: Partial<EventCallBackMethods> = {};
@ -184,7 +184,7 @@ export function showRowContextMenu(parentComponent: Component, e: MouseEvent, ro
handler: () => parentComponent?.triggerCommand("addNewRow", { handler: () => parentComponent?.triggerCommand("addNewRow", {
parentNotePath: parentNoteId, parentNotePath: parentNoteId,
customOpts: { customOpts: {
target: CreateNoteTarget.BeforeNoteURL, target: "before",
targetBranchId: rowData.branchId, targetBranchId: rowData.branchId,
} as CreateNoteBeforeUrlOpts } as CreateNoteBeforeUrlOpts
}) })
@ -198,7 +198,7 @@ export function showRowContextMenu(parentComponent: Component, e: MouseEvent, ro
parentComponent?.triggerCommand("addNewRow", { parentComponent?.triggerCommand("addNewRow", {
parentNotePath: note?.noteId, parentNotePath: note?.noteId,
customOpts: { customOpts: {
target: CreateNoteTarget.AfterNoteURL, target: "after",
targetBranchId: branchId, targetBranchId: branchId,
} as CreateNoteAfterUrlOpts } as CreateNoteAfterUrlOpts
}); });
@ -211,7 +211,7 @@ export function showRowContextMenu(parentComponent: Component, e: MouseEvent, ro
handler: () => parentComponent?.triggerCommand("addNewRow", { handler: () => parentComponent?.triggerCommand("addNewRow", {
parentNotePath: parentNoteId, parentNotePath: parentNoteId,
customOpts: { customOpts: {
target: CreateNoteTarget.AfterNoteURL, target: "after",
targetBranchId: rowData.branchId, targetBranchId: rowData.branchId,
} as CreateNoteAfterUrlOpts } as CreateNoteAfterUrlOpts
}) })

View File

@ -1,6 +1,6 @@
import { EventCallBackMethods, RowComponent, Tabulator } from "tabulator-tables"; import { EventCallBackMethods, RowComponent, Tabulator } from "tabulator-tables";
import { CommandListenerData } from "../../../components/app_context"; 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 { useLegacyImperativeHandlers } from "../../react/hooks";
import { RefObject } from "preact"; import { RefObject } from "preact";
import { setAttribute, setLabel } from "../../../services/attributes"; import { setAttribute, setLabel } from "../../../services/attributes";

View File

@ -3,7 +3,7 @@ import appContext from "../../components/app_context";
import contextMenu from "../../menus/context_menu"; import contextMenu from "../../menus/context_menu";
import branches from "../../services/branches"; import branches from "../../services/branches";
import { t } from "../../services/i18n"; 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 tree from "../../services/tree";
import ActionButton from "../react/ActionButton"; import ActionButton from "../react/ActionButton";
import { ParentComponent } from "../react/react_utils"; import { ParentComponent } from "../react/react_utils";
@ -31,7 +31,7 @@ export default function MobileDetailMenu() {
if (command === "insertChildNote") { if (command === "insertChildNote") {
note_create.createNote( note_create.createNote(
{ {
target: CreateNoteTarget.IntoNoteURL, target: "into",
parentNoteUrl: appContext.tabManager.getActiveContextNotePath() ?? undefined parentNoteUrl: appContext.tabManager.getActiveContextNotePath() ?? undefined
} as CreateNoteIntoUrlOpts } as CreateNoteIntoUrlOpts
); );

View File

@ -7,7 +7,7 @@ import branchService from "../services/branches.js";
import ws from "../services/ws.js"; import ws from "../services/ws.js";
import NoteContextAwareWidget from "./note_context_aware_widget.js"; import NoteContextAwareWidget from "./note_context_aware_widget.js";
import server from "../services/server.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 toastService from "../services/toast.js";
import appContext, { type CommandListenerData, type EventData } from "../components/app_context.js"; import appContext, { type CommandListenerData, type EventData } from "../components/app_context.js";
import keyboardActionsService from "../services/keyboard_actions.js"; import keyboardActionsService from "../services/keyboard_actions.js";
@ -226,7 +226,7 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
const parentNotePath = treeService.getNotePath(node); const parentNotePath = treeService.getNotePath(node);
noteCreateService.createNote( noteCreateService.createNote(
{ {
target: CreateNoteTarget.IntoNoteURL, target: "into",
parentNoteUrl: parentNotePath, parentNoteUrl: parentNotePath,
isProtected: node.data.isProtected isProtected: node.data.isProtected
} as CreateNoteIntoUrlOpts, } as CreateNoteIntoUrlOpts,
@ -1844,7 +1844,7 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
const notePath = treeService.getNotePath(node); const notePath = treeService.getNotePath(node);
noteCreateService.createNote( noteCreateService.createNote(
{ {
target: CreateNoteTarget.IntoNoteURL, target: "into",
parentNoteUrl: notePath, parentNoteUrl: notePath,
isProtected: node.data.isProtected isProtected: node.data.isProtected
} as CreateNoteIntoUrlOpts } as CreateNoteIntoUrlOpts

View File

@ -19,7 +19,7 @@ import contextMenu from "../../../menus/context_menu";
import type { CommandData, FilteredCommandNames } from "../../../components/app_context"; import type { CommandData, FilteredCommandNames } from "../../../components/app_context";
import { AttributeType } from "@triliumnext/commons"; import { AttributeType } from "@triliumnext/commons";
import attributes from "../../../services/attributes"; 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"; import { CreateNoteAction } from "@triliumnext/commons";
type AttributeCommandNames = FilteredCommandNames<CommandData>; type AttributeCommandNames = FilteredCommandNames<CommandData>;
@ -263,7 +263,7 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxI
case CreateNoteAction.CreateAndLinkNoteIntoInbox: { case CreateNoteAction.CreateAndLinkNoteIntoInbox: {
const { note } = await note_create.createNote( const { note } = await note_create.createNote(
{ {
target: CreateNoteTarget.IntoInbox, target: "inbox",
title, title,
activate: false activate: false
} as CreateNoteIntoInboxOpts } as CreateNoteIntoInboxOpts
@ -275,7 +275,7 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxI
case CreateNoteAction.CreateAndLinkNoteIntoPath: { case CreateNoteAction.CreateAndLinkNoteIntoPath: {
const resp = await note_create.createNote( const resp = await note_create.createNote(
{ {
target: CreateNoteTarget.IntoNoteURL, target: "into",
parentNoteUrl: parentNotePath, parentNoteUrl: parentNotePath,
title, title,
activate: false, activate: false,