Skip to content

Commit a885556

Browse files
author
Jakob Schlanstedt
committed
feat(search): add create into inbox to search
1 parent 8bf4dbb commit a885556

File tree

3 files changed

+112
-106
lines changed

3 files changed

+112
-106
lines changed

apps/client/src/services/note_autocomplete.ts

Lines changed: 32 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,27 @@ function renderSuggestion(suggestion: Suggestion): string {
349349
: renderNoteSuggestion(suggestion);
350350
}
351351

352+
function mapSuggestionToCreateNoteAction(
353+
action: SuggestionAction
354+
): CreateNoteAction | null {
355+
switch (action) {
356+
case SuggestionAction.CreateNote:
357+
return CreateNoteAction.CreateNote;
358+
359+
case SuggestionAction.CreateAndLinkNote:
360+
return CreateNoteAction.CreateAndLinkNote;
361+
362+
case SuggestionAction.CreateChildNote:
363+
return CreateNoteAction.CreateChildNote;
364+
365+
case SuggestionAction.CreateAndLinkChildNote:
366+
return CreateNoteAction.CreateAndLinkChildNote;
367+
368+
default:
369+
return null;
370+
}
371+
}
372+
352373
function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
353374
if ($el.hasClass("note-autocomplete-input")) {
354375
// clear any event listener added in previous invocation of this function
@@ -507,77 +528,18 @@ function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
507528
await doExternalLink();
508529
break;
509530

510-
case SuggestionAction.CreateNote: {
511-
const { note } = await noteCreateService.createNote(
512-
{
513-
target: "inbox",
514-
title: suggestion.noteTitle,
515-
activate: true,
516-
promptForType: true,
517-
}
518-
);
519-
520-
if (!note) break;
521-
522-
await resolveSuggestionNotePathUnderCurrentHoist(note);
523-
await selectNoteFromAutocomplete(suggestion);
524-
break;
525-
}
526-
527-
case SuggestionAction.CreateAndLinkNote: {
528-
const { note } = await noteCreateService.createNote(
529-
{
530-
target: "inbox",
531-
title: suggestion.noteTitle,
532-
activate: false,
533-
promptForType: true,
534-
}
535-
);
536-
537-
if (!note) break;
538-
539-
await resolveSuggestionNotePathUnderCurrentHoist(note);
540-
await selectNoteFromAutocomplete(suggestion);
541-
break;
542-
}
543-
544-
case SuggestionAction.CreateChildNote: {
545-
if (!suggestion.parentNoteId) {
546-
console.warn("Missing parentNoteId for CreateNoteIntoPath");
547-
return;
548-
}
549-
550-
const { note } = await noteCreateService.createNote(
551-
{
552-
target: "into",
553-
parentNoteUrl: suggestion.parentNoteId,
554-
title: suggestion.noteTitle,
555-
activate: true,
556-
promptForType: true,
557-
},
558-
);
559-
560-
if (!note) break;
561-
562-
await resolveSuggestionNotePathUnderCurrentHoist(note);
563-
await selectNoteFromAutocomplete(suggestion);
564-
break;
565-
}
566-
531+
case SuggestionAction.CreateNote:
532+
case SuggestionAction.CreateAndLinkNote:
533+
case SuggestionAction.CreateChildNote:
567534
case SuggestionAction.CreateAndLinkChildNote: {
568-
if (!suggestion.parentNoteId) {
569-
console.warn("Missing parentNoteId for CreateNoteIntoPath");
570-
return;
571-
}
572-
573-
const { note } = await noteCreateService.createNote(
574-
{
575-
target: "into",
576-
parentNoteUrl: suggestion.parentNoteId,
577-
title: suggestion.noteTitle,
578-
activate: false,
579-
promptForType: true,
580-
}
535+
const createNoteAction = mapSuggestionToCreateNoteAction(
536+
suggestion.action
537+
)!;
538+
const { note } = await noteCreateService.createNoteFromAction(
539+
createNoteAction,
540+
true,
541+
suggestion.noteTitle,
542+
suggestion.parentNoteId,
581543
);
582544

583545
if (!note) break;

apps/client/src/services/note_create.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type FBranch from "../entities/fbranch.js";
1111
import type { ChooseNoteTypeResponse } from "../widgets/dialogs/note_type_chooser.js";
1212
import type { CKTextEditor } from "@triliumnext/ckeditor5";
1313
import dateNoteService from "../services/date_notes.js";
14+
import { CreateNoteAction } from "@triliumnext/commons";
1415

1516
/**
1617
* Defines the type hierarchy and rules for valid argument combinations
@@ -114,6 +115,7 @@ interface DuplicateResponse {
114115
note: FNote;
115116
}
116117

118+
// The low level note creation
117119
async function createNote(
118120
options: CreateNoteOpts
119121
): Promise<{ note: FNote | null; branch: FBranch | undefined }> {
@@ -141,6 +143,76 @@ async function createNote(
141143
}
142144
}
143145

146+
// A wrapper to standardize note creation
147+
async function createNoteFromAction(
148+
action: CreateNoteAction,
149+
promptForType: boolean,
150+
title: string | undefined,
151+
parentNoteUrl: string | undefined,
152+
): Promise<{ note: FNote | null; branch: FBranch | undefined }> {
153+
switch (action) {
154+
case CreateNoteAction.CreateNote: {
155+
const resp = await createNote(
156+
{
157+
target: "inbox",
158+
title: title,
159+
activate: true,
160+
promptForType: promptForType,
161+
}
162+
);
163+
return resp;
164+
}
165+
case CreateNoteAction.CreateAndLinkNote: {
166+
const resp = await createNote(
167+
{
168+
target: "inbox",
169+
title,
170+
activate: false,
171+
promptForType: promptForType,
172+
}
173+
);
174+
return resp;
175+
}
176+
case CreateNoteAction.CreateChildNote: {
177+
if (!parentNoteUrl) {
178+
console.warn("Missing parentNotePath in createNoteFromCkEditor()");
179+
return { note: null, branch: undefined };
180+
}
181+
182+
const resp = await createNote(
183+
{
184+
target: "into",
185+
parentNoteUrl,
186+
title,
187+
activate: true,
188+
promptForType: true,
189+
},
190+
);
191+
return resp
192+
}
193+
case CreateNoteAction.CreateAndLinkChildNote: {
194+
if (!parentNoteUrl) {
195+
console.warn("Missing parentNotePath in createNoteFromCkEditor()");
196+
return { note: null, branch: undefined };
197+
}
198+
const resp = await createNote(
199+
{
200+
target: "into",
201+
parentNoteUrl: parentNoteUrl,
202+
title,
203+
activate: false,
204+
promptForType: promptForType,
205+
},
206+
)
207+
return resp;
208+
}
209+
210+
default:
211+
console.warn("Unknown CreateNoteAction:", action);
212+
return { note: null, branch: undefined };
213+
}
214+
}
215+
144216
async function promptForType(
145217
options: CreateNoteOpts
146218
) : Promise<CreateNoteOpts | null> {
@@ -326,5 +398,6 @@ async function duplicateSubtree(noteId: string, parentNotePath: string) {
326398

327399
export default {
328400
createNote,
401+
createNoteFromAction,
329402
duplicateSubtree,
330403
};

apps/client/src/widgets/ribbon/components/AttributeEditor.tsx

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -253,43 +253,14 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxI
253253
parentNotePath: string | undefined,
254254
action: CreateNoteAction
255255
): Promise<string> => {
256-
switch (action) {
257-
case CreateNoteAction.CreateNote:
258-
case CreateNoteAction.CreateAndLinkNote: {
259-
const { note } = await note_create.createNote(
260-
{
261-
target: "inbox",
262-
title,
263-
activate: false,
264-
promptForType: true,
265-
}
266-
);
267-
return note?.getBestNotePathString() ?? "";
268-
}
269-
270-
case CreateNoteAction.CreateChildNote:
271-
case CreateNoteAction.CreateAndLinkChildNote: {
272-
if (!parentNotePath) {
273-
console.warn("Missing parentNotePath in createNoteFromCkEditor()");
274-
return "";
275-
}
276-
const resp = await note_create.createNote(
277-
{
278-
target: "into",
279-
parentNoteUrl: parentNotePath,
280-
title,
281-
activate: false,
282-
promptForType: true,
283-
},
284-
)
285-
return resp?.note?.getBestNotePathString() ?? "";
286-
}
287-
288-
default:
289-
console.warn("Unknown CreateNoteAction:", action);
290-
return "";
256+
const { note } = await note_create.createNoteFromAction(
257+
action,
258+
true,
259+
parentNotePath,
260+
title,
261+
);
262+
return note?.getBestNotePathString() ?? "";
291263
}
292-
}
293264
}), [ notePath ]));
294265

295266
// Keyboard shortcuts

0 commit comments

Comments
 (0)