Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(search): offer an orderBy key only the steps it can sort on
Elian Dorana16c97a83a63
diff --git a/apps/client/src/widgets/ribbon/search_completions.spec.ts b/apps/client/src/widgets/ribbon/search_completions.spec.tsindex 9e089065dd..84d6508221 100644--- a/apps/client/src/widgets/ribbon/search_completions.spec.ts+++ b/apps/client/src/widgets/ribbon/search_completions.spec.ts@@ -117,6 +117,20 @@ describe("searchCompletionAt", () => {expect(await titlesOf(complete("~author.relations.son."))).toContain("title");});+ it("offers a sort key only the segments ValueExtractor can sort on", async () => {+ const everywhere = await titlesOf(complete("#book AND note."));+ expect(everywhere).toEqual(expect.arrayContaining([ "ancestors", "links", "backlinks", "content", "text" ]));++ const sortKey = await titlesOf(complete("#book orderBy note."));+ expect(sortKey).toEqual(expect.arrayContaining([ "title", "parents", "children", "labels", "relations" ]));+ for (const unsortable of [ "ancestors", "links", "backlinks", "content", "rawContent", "text" ]) {+ expect(sortKey).not.toContain(unsortable);+ }++ expect(await titlesOf(complete("#book orderBy note.title, note.parents."))).not.toContain("links");+ expect(await titlesOf(complete("#book orderBy note.title limit 10 AND note."))).toContain("links");+ });+it("stops where a terminal property ends the path", () => {expect(complete("note.title.")).toBeNull();expect(complete("note.labels.publicationYear.")).toBeNull();diff --git a/apps/client/src/widgets/ribbon/search_completions.ts b/apps/client/src/widgets/ribbon/search_completions.tsindex 23745945cd..7412503231 100644--- a/apps/client/src/widgets/ribbon/search_completions.ts+++ b/apps/client/src/widgets/ribbon/search_completions.ts@@ -51,7 +51,7 @@ export function searchCompletionAt(before: string, explicit: boolean): SearchComconst path = matchBefore(context, PROPERTY_PATH);if (path) {- return pathCompletion(path.text, context.pos);+ return pathCompletion(path.text, context.pos, orderingPosition(context) !== "none");}// `~=` and `~*` reach the operators below instead: an attribute name cannot be spelled with@@ -324,7 +324,7 @@ function escapeInQuotes(value: string, quote: string) {* `note.relations.` that is an attribute name; after `note.title.` it is nothing at all, a* terminal property having nothing to walk onto.*/-function pathCompletion(path: string, pos: number): SearchCompletion | null {+function pathCompletion(path: string, pos: number, sortKey: boolean): SearchCompletion | null {const segments = path.split(".");const typed = segments[segments.length - 1];const previous = segments[segments.length - 2];@@ -344,7 +344,8 @@ function pathCompletion(path: string, pos: number): SearchCompletion | null {return null;}- return { kind: "entries", key: "segments", from, query: typed, entries: segmentEntries, preselect: false };+ const key = sortKey ? "segments:sort" : "segments";+ return { kind: "entries", key, from, query: typed, entries: () => segmentEntries(sortKey), preselect: false };}/**@@ -422,8 +423,19 @@ function orderingPosition(context: Context): OrderingPosition {return written ? "sorted" : "key";}-function segmentEntries(): SearchEntry[] {- return Object.values(SEARCH_NOTE_PATH).flat().map((segment) => {+/** The traversals `ValueExtractor.validate()` accepts in an `orderBy` key; it reads no content either. */+const SORT_KEY_TRAVERSALS: ReadonlySet<string> = new Set([ "parents", "children" ]);++function segmentEntries(sortKey: boolean): SearchEntry[] {+ const segments = sortKey+ ? [+ ...SEARCH_NOTE_PATH.properties,+ ...SEARCH_NOTE_PATH.traversals.filter((segment) => SORT_KEY_TRAVERSALS.has(segment)),+ ...SEARCH_NOTE_PATH.attributeSegments+ ]+ : Object.values(SEARCH_NOTE_PATH).flat();++ return segments.map((segment) => {const { icon, detail } = SEGMENTS[segment];return entry(segment, detail ? t(detail) : undefined, `bx ${icon}`);});