Skip to content

Commit b8e650d

Browse files
author
Jakob Schlanstedt
committed
feat(search): add create into inbox to search
1 parent 4b66477 commit b8e650d

File tree

4 files changed

+119
-146
lines changed

4 files changed

+119
-146
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
@@ -258,43 +258,14 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxI
258258
parentNotePath: string | undefined,
259259
action: CreateNoteAction
260260
): Promise<string> => {
261-
switch (action) {
262-
case CreateNoteAction.CreateNote:
263-
case CreateNoteAction.CreateAndLinkNote: {
264-
const { note } = await note_create.createNote(
265-
{
266-
target: "inbox",
267-
title,
268-
activate: false,
269-
promptForType: true,
270-
}
271-
);
272-
return note?.getBestNotePathString() ?? "";
273-
}
274-
275-
case CreateNoteAction.CreateChildNote:
276-
case CreateNoteAction.CreateAndLinkChildNote: {
277-
if (!parentNotePath) {
278-
console.warn("Missing parentNotePath in createNoteFromCkEditor()");
279-
return "";
280-
}
281-
const resp = await note_create.createNote(
282-
{
283-
target: "into",
284-
parentNoteUrl: parentNotePath,
285-
title,
286-
activate: false,
287-
promptForType: true,
288-
},
289-
)
290-
return resp?.note?.getBestNotePathString() ?? "";
291-
}
292-
293-
default:
294-
console.warn("Unknown CreateNoteAction:", action);
295-
return "";
261+
const { note } = await note_create.createNoteFromAction(
262+
action,
263+
true,
264+
parentNotePath,
265+
title,
266+
);
267+
return note?.getBestNotePathString() ?? "";
296268
}
297-
}
298269
}), [ notePath ]));
299270

300271
// Keyboard shortcuts

apps/client/src/widgets/type_widgets/editable_text.ts

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -497,46 +497,13 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
497497
parentNotePath: string | undefined,
498498
action: CreateNoteAction
499499
): Promise<string> {
500-
switch (action) {
501-
case CreateNoteAction.CreateAndLinkNote: {
502-
const { note } = await noteCreateService.createNote(
503-
{
504-
target: "inbox",
505-
title,
506-
activate: false,
507-
promptForType: true,
508-
}
509-
);
510-
511-
return note?.getBestNotePathString() ?? "";
512-
}
513-
514-
case CreateNoteAction.CreateAndLinkChildNote: {
515-
if (!parentNotePath) {
516-
console.error("Cannot create note: parentNotePath is undefined.");
517-
return "";
518-
}
519-
const { note } = await noteCreateService.createNote(
520-
{
521-
target: "into",
522-
parentNoteUrl: parentNotePath,
523-
title,
524-
activate: false,
525-
promptForType: true,
526-
}
527-
);
528-
529-
return note?.getBestNotePathString() ?? "";
530-
}
531-
532-
// We always create and Link notes in the CkEditor. Never just
533-
// create.
534-
case CreateNoteAction.CreateNote:
535-
case CreateNoteAction.CreateChildNote:
536-
default:
537-
console.warn("impossible CreateNoteAction state:", action);
538-
return "";
539-
}
500+
const { note }= await noteCreateService.createNoteFromAction(
501+
action,
502+
true,
503+
title,
504+
parentNotePath,
505+
)
506+
return note?.getBestNotePathString() ?? "";
540507
}
541508

542509
async refreshIncludedNoteEvent({ noteId }: EventData<"refreshIncludedNote">) {

0 commit comments

Comments
 (0)