diff --git a/apps/client/src/services/note_autocomplete.ts b/apps/client/src/services/note_autocomplete.ts index 8bc8d3fff..557b4b9cc 100644 --- a/apps/client/src/services/note_autocomplete.ts +++ b/apps/client/src/services/note_autocomplete.ts @@ -349,6 +349,27 @@ function renderSuggestion(suggestion: Suggestion): string { : renderNoteSuggestion(suggestion); } +function mapSuggestionToCreateNoteAction( + action: SuggestionAction +): CreateNoteAction | null { + switch (action) { + case SuggestionAction.CreateNote: + return CreateNoteAction.CreateNote; + + case SuggestionAction.CreateAndLinkNote: + return CreateNoteAction.CreateAndLinkNote; + + case SuggestionAction.CreateChildNote: + return CreateNoteAction.CreateChildNote; + + case SuggestionAction.CreateAndLinkChildNote: + return CreateNoteAction.CreateAndLinkChildNote; + + default: + return null; + } +} + function initNoteAutocomplete($el: JQuery, options?: Options) { if ($el.hasClass("note-autocomplete-input")) { // clear any event listener added in previous invocation of this function @@ -507,77 +528,18 @@ function initNoteAutocomplete($el: JQuery, options?: Options) { await doExternalLink(); break; - case SuggestionAction.CreateNote: { - const { note } = await noteCreateService.createNote( - { - target: "inbox", - title: suggestion.noteTitle, - activate: true, - promptForType: true, - } - ); - - if (!note) break; - - await resolveSuggestionNotePathUnderCurrentHoist(note); - await selectNoteFromAutocomplete(suggestion); - break; - } - - case SuggestionAction.CreateAndLinkNote: { - const { note } = await noteCreateService.createNote( - { - target: "inbox", - title: suggestion.noteTitle, - activate: false, - promptForType: true, - } - ); - - if (!note) break; - - await resolveSuggestionNotePathUnderCurrentHoist(note); - await selectNoteFromAutocomplete(suggestion); - break; - } - - case SuggestionAction.CreateChildNote: { - if (!suggestion.parentNoteId) { - console.warn("Missing parentNoteId for CreateNoteIntoPath"); - return; - } - - const { note } = await noteCreateService.createNote( - { - target: "into", - parentNoteUrl: suggestion.parentNoteId, - title: suggestion.noteTitle, - activate: true, - promptForType: true, - }, - ); - - if (!note) break; - - await resolveSuggestionNotePathUnderCurrentHoist(note); - await selectNoteFromAutocomplete(suggestion); - break; - } - + case SuggestionAction.CreateNote: + case SuggestionAction.CreateAndLinkNote: + case SuggestionAction.CreateChildNote: case SuggestionAction.CreateAndLinkChildNote: { - if (!suggestion.parentNoteId) { - console.warn("Missing parentNoteId for CreateNoteIntoPath"); - return; - } - - const { note } = await noteCreateService.createNote( - { - target: "into", - parentNoteUrl: suggestion.parentNoteId, - title: suggestion.noteTitle, - activate: false, - promptForType: true, - } + const createNoteAction = mapSuggestionToCreateNoteAction( + suggestion.action + )!; + const { note } = await noteCreateService.createNoteFromAction( + createNoteAction, + true, + suggestion.noteTitle, + suggestion.parentNoteId, ); if (!note) break; diff --git a/apps/client/src/services/note_create.ts b/apps/client/src/services/note_create.ts index bd4d80dab..a15518996 100644 --- a/apps/client/src/services/note_create.ts +++ b/apps/client/src/services/note_create.ts @@ -11,6 +11,7 @@ import type FBranch from "../entities/fbranch.js"; import type { ChooseNoteTypeResponse } from "../widgets/dialogs/note_type_chooser.js"; import type { CKTextEditor } from "@triliumnext/ckeditor5"; import dateNoteService from "../services/date_notes.js"; +import { CreateNoteAction } from "@triliumnext/commons"; /** * Defines the type hierarchy and rules for valid argument combinations @@ -114,6 +115,7 @@ interface DuplicateResponse { note: FNote; } +// The low level note creation async function createNote( options: CreateNoteOpts ): Promise<{ note: FNote | null; branch: FBranch | undefined }> { @@ -141,6 +143,76 @@ async function createNote( } } +// A wrapper to standardize note creation +async function createNoteFromAction( + action: CreateNoteAction, + promptForType: boolean, + title: string | undefined, + parentNoteUrl: string | undefined, +): Promise<{ note: FNote | null; branch: FBranch | undefined }> { + switch (action) { + case CreateNoteAction.CreateNote: { + const resp = await createNote( + { + target: "inbox", + title: title, + activate: true, + promptForType: promptForType, + } + ); + return resp; + } + case CreateNoteAction.CreateAndLinkNote: { + const resp = await createNote( + { + target: "inbox", + title, + activate: false, + promptForType: promptForType, + } + ); + return resp; + } + case CreateNoteAction.CreateChildNote: { + if (!parentNoteUrl) { + console.warn("Missing parentNotePath in createNoteFromCkEditor()"); + return { note: null, branch: undefined }; + } + + const resp = await createNote( + { + target: "into", + parentNoteUrl, + title, + activate: true, + promptForType: true, + }, + ); + return resp + } + case CreateNoteAction.CreateAndLinkChildNote: { + if (!parentNoteUrl) { + console.warn("Missing parentNotePath in createNoteFromCkEditor()"); + return { note: null, branch: undefined }; + } + const resp = await createNote( + { + target: "into", + parentNoteUrl: parentNoteUrl, + title, + activate: false, + promptForType: promptForType, + }, + ) + return resp; + } + + default: + console.warn("Unknown CreateNoteAction:", action); + return { note: null, branch: undefined }; + } +} + async function promptForType( options: CreateNoteOpts ) : Promise { @@ -326,5 +398,6 @@ async function duplicateSubtree(noteId: string, parentNotePath: string) { export default { createNote, + createNoteFromAction, duplicateSubtree, }; diff --git a/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx b/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx index 243a6c08d..4315a1b27 100644 --- a/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx +++ b/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx @@ -253,43 +253,14 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxI parentNotePath: string | undefined, action: CreateNoteAction ): Promise => { - switch (action) { - case CreateNoteAction.CreateNote: - case CreateNoteAction.CreateAndLinkNote: { - const { note } = await note_create.createNote( - { - target: "inbox", - title, - activate: false, - promptForType: true, - } - ); - return note?.getBestNotePathString() ?? ""; - } - - case CreateNoteAction.CreateChildNote: - case CreateNoteAction.CreateAndLinkChildNote: { - if (!parentNotePath) { - console.warn("Missing parentNotePath in createNoteFromCkEditor()"); - return ""; - } - const resp = await note_create.createNote( - { - target: "into", - parentNoteUrl: parentNotePath, - title, - activate: false, - promptForType: true, - }, - ) - return resp?.note?.getBestNotePathString() ?? ""; - } - - default: - console.warn("Unknown CreateNoteAction:", action); - return ""; + const { note } = await note_create.createNoteFromAction( + action, + true, + parentNotePath, + title, + ); + return note?.getBestNotePathString() ?? ""; } - } }), [ notePath ])); // Keyboard shortcuts