feat(search): add create into inbox to search

This commit is contained in:
Jakob Schlanstedt 2025-11-21 09:35:18 +01:00
parent 09aa22c74b
commit f8090d9217
3 changed files with 112 additions and 106 deletions

View File

@ -349,6 +349,27 @@ function renderSuggestion(suggestion: Suggestion): string {
: renderNoteSuggestion(suggestion); : 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<HTMLElement>, options?: Options) { function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
if ($el.hasClass("note-autocomplete-input")) { if ($el.hasClass("note-autocomplete-input")) {
// clear any event listener added in previous invocation of this function // clear any event listener added in previous invocation of this function
@ -507,77 +528,18 @@ function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
await doExternalLink(); await doExternalLink();
break; break;
case SuggestionAction.CreateNote: { case SuggestionAction.CreateNote:
const { note } = await noteCreateService.createNote( case SuggestionAction.CreateAndLinkNote:
{ case SuggestionAction.CreateChildNote:
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.CreateAndLinkChildNote: { case SuggestionAction.CreateAndLinkChildNote: {
if (!suggestion.parentNoteId) { const createNoteAction = mapSuggestionToCreateNoteAction(
console.warn("Missing parentNoteId for CreateNoteIntoPath"); suggestion.action
return; )!;
} const { note } = await noteCreateService.createNoteFromAction(
createNoteAction,
const { note } = await noteCreateService.createNote( true,
{ suggestion.noteTitle,
target: "into", suggestion.parentNoteId,
parentNoteUrl: suggestion.parentNoteId,
title: suggestion.noteTitle,
activate: false,
promptForType: true,
}
); );
if (!note) break; if (!note) break;

View File

@ -11,6 +11,7 @@ import type FBranch from "../entities/fbranch.js";
import type { ChooseNoteTypeResponse } from "../widgets/dialogs/note_type_chooser.js"; import type { ChooseNoteTypeResponse } from "../widgets/dialogs/note_type_chooser.js";
import type { CKTextEditor } from "@triliumnext/ckeditor5"; import type { CKTextEditor } from "@triliumnext/ckeditor5";
import dateNoteService from "../services/date_notes.js"; import dateNoteService from "../services/date_notes.js";
import { CreateNoteAction } from "@triliumnext/commons";
/** /**
* Defines the type hierarchy and rules for valid argument combinations * Defines the type hierarchy and rules for valid argument combinations
@ -114,6 +115,7 @@ interface DuplicateResponse {
note: FNote; note: FNote;
} }
// The low level note creation
async function createNote( async function createNote(
options: CreateNoteOpts options: CreateNoteOpts
): Promise<{ note: FNote | null; branch: FBranch | undefined }> { ): 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( async function promptForType(
options: CreateNoteOpts options: CreateNoteOpts
) : Promise<CreateNoteOpts | null> { ) : Promise<CreateNoteOpts | null> {
@ -326,5 +398,6 @@ async function duplicateSubtree(noteId: string, parentNotePath: string) {
export default { export default {
createNote, createNote,
createNoteFromAction,
duplicateSubtree, duplicateSubtree,
}; };

View File

@ -253,43 +253,14 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxI
parentNotePath: string | undefined, parentNotePath: string | undefined,
action: CreateNoteAction action: CreateNoteAction
): Promise<string> => { ): Promise<string> => {
switch (action) { const { note } = await note_create.createNoteFromAction(
case CreateNoteAction.CreateNote: action,
case CreateNoteAction.CreateAndLinkNote: { true,
const { note } = await note_create.createNote( parentNotePath,
{ title,
target: "inbox", );
title, return note?.getBestNotePathString() ?? "";
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 "";
} }
}
}), [ notePath ])); }), [ notePath ]));
// Keyboard shortcuts // Keyboard shortcuts