Skip to content

Build your personal knowledge base with Trilium Notes

feat(search): complete the smart dates after a date property

Elian Dorana3bdc2154fea
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 897c5aedbe..4ac84063ff 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -4333,6 +4333,14 @@
"keyword_or": "Either condition must match",
"keyword_not": "Excludes the notes the condition in parentheses matches",
"keyword_order_by": "Sorts the results by a property or label",
- "keyword_limit": "Keeps only the first results"
+ "keyword_limit": "Keeps only the first results",
+ "date_now": "The current date and time",
+ "date_today": "Today's date",
+ "date_month": "The current month",
+ "date_year": "The current year",
+ "date_now_offset": "Any number of seconds before now",
+ "date_today_offset": "Any number of days before today",
+ "date_month_offset": "Any number of months before this one",
+ "date_year_offset": "Any number of years before this one"
}
}
diff --git a/apps/client/src/widgets/ribbon/search_completions.spec.ts b/apps/client/src/widgets/ribbon/search_completions.spec.ts
index a9d6de24aa..bb46846af2 100644
--- a/apps/client/src/widgets/ribbon/search_completions.spec.ts
+++ b/apps/client/src/widgets/ribbon/search_completions.spec.ts
@@ -251,6 +251,24 @@ describe("searchCompletionSource", () => {
.toEqual([ "note", "and", "or", "not", "orderBy", "limit" ]);
expect(labelsOf(await complete("note.labels.type = fic"))).toEqual([ "fiction", "science fiction" ]);
});
+
+ it("offers the smart dates after a date property, unquoted so the parser resolves them", async () => {
+ const dates = await complete("note.dateCreated >= t");
+
+ expect(labelsOf(dates)).toEqual([
+ "now", "now-60", "today", "today-30", "month", "month-1", "year", "year-1"
+ ]);
+ // Quoting one would make it the text it spells instead of a date.
+ expect(dates?.options.every((option) => option.apply === undefined)).toBe(true);
+ expect(dates?.options.every((option) => option.detail)).toBe(true);
+
+ for (const property of [ "dateModified", "utcDateCreated", "utcDateModified" ]) {
+ expect(labelsOf(await complete(`note.${property} < `))).toContain("today");
+ }
+
+ // A quote the user opened is a date they are spelling out themselves.
+ expect(await complete("note.dateCreated >= \"2")).toBeNull();
+ });
});
});
diff --git a/apps/client/src/widgets/ribbon/search_completions.ts b/apps/client/src/widgets/ribbon/search_completions.ts
index c49fa26fad..cb14cc99aa 100644
--- a/apps/client/src/widgets/ribbon/search_completions.ts
+++ b/apps/client/src/widgets/ribbon/search_completions.ts
@@ -198,14 +198,17 @@ function propertyValueBeingTyped(context: CompletionContext) {
return null;
}
- return {
- ...value,
- options: values().map(({ label, detail }) => {
- const applied = applyValue(label, value.quote);
+ // A quote the user opened is a value they are spelling out, so the ones that mean anything
+ // only bare are dropped. For a date property that leaves nothing to offer.
+ const options = values()
+ .filter(({ verbatim }) => !(verbatim && value.quote))
+ .map(({ label, detail, verbatim }) => {
+ const applied = verbatim ? label : applyValue(label, value.quote);
return { label, detail, apply: applied === label ? undefined : applied };
- })
- };
+ });
+
+ return options.length ? { ...value, options } : null;
}
/**
@@ -233,17 +236,46 @@ function valueBeingTyped(tail: string) {
* lexer lowercases the query. The lists are the ones the note row and the code-note MIME dropdown
* already use, so a type or a MIME added there is offered here without further work.
*/
-const PROPERTY_VALUES = new Map<string, () => { label: string, detail?: string }[]>([
+const PROPERTY_VALUES = new Map<string, () => PropertyValue[]>([
[ "type", () => ALLOWED_NOTE_TYPES.map((noteType) => ({ label: noteType })) ],
[ "mime", () => MIME_TYPES_DICT.map(({ mime, title }) => ({ label: mime, detail: title })) ],
[ "isprotected", booleanValues ],
- [ "isarchived", booleanValues ]
+ [ "isarchived", booleanValues ],
+ [ "datecreated", dateValues ],
+ [ "datemodified", dateValues ],
+ [ "utcdatecreated", dateValues ],
+ [ "utcdatemodified", dateValues ]
]);
-function booleanValues() {
+interface PropertyValue {
+ label: string;
+ detail?: string;
+ /** Inserted as it stands, the parser reading it as something other than the text it spells. */
+ verbatim?: boolean;
+}
+
+function booleanValues(): PropertyValue[] {
return [ { label: "true" }, { label: "false" } ];
}
+/**
+ * The dates `resolveConstantOperand` resolves. Nothing else in the app names them, so this is the
+ * only place to find them. Each is offered with an offset as well, counted in the unit that date
+ * steps in, so the form arrives as text to edit rather than as something to read and retype.
+ */
+function dateValues(): PropertyValue[] {
+ return [
+ { label: "now", detail: t("search_completion.date_now"), verbatim: true },
+ { label: "now-60", detail: t("search_completion.date_now_offset"), verbatim: true },
+ { label: "today", detail: t("search_completion.date_today"), verbatim: true },
+ { label: "today-30", detail: t("search_completion.date_today_offset"), verbatim: true },
+ { label: "month", detail: t("search_completion.date_month"), verbatim: true },
+ { label: "month-1", detail: t("search_completion.date_month_offset"), verbatim: true },
+ { label: "year", detail: t("search_completion.date_year"), verbatim: true },
+ { label: "year-1", detail: t("search_completion.date_year_offset"), verbatim: true }
+ ];
+}
+
/**
* Offers the values `name` already holds across the database. Only labels reach here: a relation's
* value is a note ID, and the endpoint behind this collects label values alone.