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
feat(calendar): count the years of a yearly event from the Repeats dropdown
Elian Doran37cd693f94dc
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 1250107d19..1f1f2c985b 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -1088,7 +1088,9 @@"count": "After","times": "times","custom_rule": "Rule",- "custom_hint": "An iCalendar recurrence rule (RRULE) that says more than the fields above can. Clearing it stops the repetition."+ "custom_hint": "An iCalendar recurrence rule (RRULE) that says more than the fields above can. Clearing it stops the repetition.",+ "count_years": "Show the number of years in the title",+ "count_years_locked": "This event already has a title template, set on the note or inherited by it. Change it in the note's attributes."}},"close_pane_button": {diff --git a/apps/client/src/widgets/collections/calendar/RecurrenceEditor.spec.tsx b/apps/client/src/widgets/collections/calendar/RecurrenceEditor.spec.tsxnew file mode 100644index 0000000000..048c071cb1--- /dev/null+++ b/apps/client/src/widgets/collections/calendar/RecurrenceEditor.spec.tsx@@ -0,0 +1,108 @@+import { render } from "preact";+import { act } from "preact/test-utils";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++import Component from "../../../components/component";+import type FNote from "../../../entities/fnote";+import attributes from "../../../services/attributes";+import froca from "../../../services/froca";+import { buildNote } from "../../../test/easy-froca";+import { ParentComponent } from "../../react/react_utils";+import RecurrenceEditor, { YEAR_COUNT_TEMPLATE } from "./RecurrenceEditor";++vi.mock("../../../services/i18n", () => ({+ t: (key: string) => key+}));++describe("RecurrenceEditor's year count", () => {+ let container: HTMLElement;+ const setLabel = vi.spyOn(attributes, "setLabel").mockResolvedValue(undefined);+ const removeLabel = vi.spyOn(attributes, "removeOwnedLabelByName").mockResolvedValue(true);++ beforeEach(() => {+ container = document.createElement("div");+ document.body.appendChild(container);+ setLabel.mockClear();+ removeLabel.mockClear();+ });++ afterEach(() => {+ act(() => render(null, container));+ container.remove();+ });++ /** Renders the editor for `note` and opens its dropdown. */+ function open(note: FNote) {+ act(() => render(+ <ParentComponent.Provider value={new Component()}>+ <RecurrenceEditor note={note} />+ </ParentComponent.Provider>,+ container));++ const toggle = container.querySelector<HTMLButtonElement>(".calendar-recurrence-dropdown button");+ if (!toggle) throw new Error("expected the dropdown toggle to render");+ act(() => {+ toggle.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true }));+ toggle.click();+ });+ // The frequency choice is in the panel whatever the rule, so its absence means it never opened.+ expect(document.querySelector(".calendar-recurrence-dropdown .tn-segmented-choice")).not.toBeNull();+ }++ const checkbox = () =>+ document.querySelector<HTMLInputElement>(".calendar-recurrence-years input[type='checkbox']");++ it("is offered on a yearly rule only", () => {+ open(buildNote({ title: "Standup", "#startDate": "2026-01-05", "#recurrence": "RRULE:FREQ=WEEKLY" }));+ expect(checkbox()).toBeNull();++ act(() => render(null, container));+ open(buildNote({ title: "John Smith", "#startDate": "1990-05-12", "#recurrence": "RRULE:FREQ=YEARLY" }));+ expect(checkbox()?.checked).toBe(false);+ expect(checkbox()?.disabled).toBe(false);+ });++ it("writes the template when checked and removes it when unchecked", () => {+ const note = buildNote({ title: "John Smith", "#startDate": "1990-05-12", "#recurrence": "RRULE:FREQ=YEARLY" });+ open(note);+ act(() => { checkbox()?.click(); });+ expect(setLabel).toHaveBeenCalledWith(note.noteId, "calendar:titleTemplate", YEAR_COUNT_TEMPLATE);++ act(() => render(null, container));+ const counted = buildNote({+ title: "Jane Smith",+ "#startDate": "1990-05-12",+ "#recurrence": "RRULE:FREQ=YEARLY",+ "#calendar:titleTemplate": YEAR_COUNT_TEMPLATE+ });+ open(counted);+ expect(checkbox()?.checked).toBe(true);+ act(() => { checkbox()?.click(); });+ expect(removeLabel).toHaveBeenCalledWith(counted, "calendar:titleTemplate");+ });++ it("is locked when a template the checkbox did not write applies", () => {+ open(buildNote({+ title: "John Smith",+ "#startDate": "1990-05-12",+ "#recurrence": "RRULE:FREQ=YEARLY",+ "#calendar:titleTemplate": "${title} turns ${years}"+ }));+ expect(checkbox()?.checked).toBe(false);+ expect(checkbox()?.disabled).toBe(true);++ act(() => render(null, container));+ const calendar = buildNote({+ title: "Birthdays",+ "#calendar:titleTemplate(inheritable)": YEAR_COUNT_TEMPLATE,+ children: [+ { title: "John Smith", "#startDate": "1990-05-12", "#recurrence": "RRULE:FREQ=YEARLY" }+ ]+ });+ const inheriting = froca.notes[calendar.getChildNoteIds()[0] ?? ""];+ expect(inheriting).toBeDefined();+ open(inheriting);+ expect(checkbox()?.checked).toBe(true);+ expect(checkbox()?.disabled).toBe(true);+ });+});diff --git a/apps/client/src/widgets/collections/calendar/RecurrenceEditor.tsx b/apps/client/src/widgets/collections/calendar/RecurrenceEditor.tsxindex 228a9d5de2..48e5431d65 100644--- a/apps/client/src/widgets/collections/calendar/RecurrenceEditor.tsx+++ b/apps/client/src/widgets/collections/calendar/RecurrenceEditor.tsx@@ -7,9 +7,10 @@ import FNote from "../../../entities/fnote";import { t } from "../../../services/i18n";import LabelValueInput from "../../attribute_widgets/label_value_input";import { DropdownPanel } from "../../react/Dropdown";+import FormCheckbox from "../../react/FormCheckbox";import FormSelect from "../../react/FormSelect";import FormTextBox, { FormTextBoxWithUnit } from "../../react/FormTextBox";-import { useUniqueName } from "../../react/hooks";+import { useNoteLabel, useUniqueName } from "../../react/hooks";import SegmentedChoice from "../../react/SegmentedChoice";import { EventField, EventFieldControls, EventFieldList } from "./EventField";import { useEventLabel } from "./hooks";@@ -67,6 +68,7 @@ export default function RecurrenceEditor({ note }: { note: FNote }) {<EventFieldList><FrequencyField model={model} commit={commit} />{model.kind === "simple" && <SimpleRuleFields rule={model.rule} commit={commit} />}+ {model.kind === "simple" && model.rule.frequency === "YEARLY" && <YearCountField note={note} />}{model.kind === "custom" && <CustomRuleField value={model.value} commit={commit} />}</EventFieldList></DropdownPanel>@@ -293,6 +295,32 @@ function EndsField({ ends, onChange }: {);}+/** The `#calendar:titleTemplate` the year count writes: the title, then the whole years since `#startDate`. */+export const YEAR_COUNT_TEMPLATE = "${title} (${years})";++/**+ * Shows the years since `#startDate` on each occurrence of a yearly event by writing+ * {@link YEAR_COUNT_TEMPLATE}. Locked when another template applies, the note's own or an inherited+ * one, so the checkbox never overwrites a template or fails to remove one.+ */+function YearCountField({ note }: { note: FNote }) {+ const [ template, setTemplate ] = useNoteLabel(note, "calendar:titleTemplate");+ const ownsCount = note.getOwnedLabelValue("calendar:titleTemplate") === YEAR_COUNT_TEMPLATE;+ const locked = !!template && !ownsCount;++ return (+ <div className="calendar-recurrence-years">+ <FormCheckbox+ label={t("calendar.recurrence.count_years")}+ hint={locked ? t("calendar.recurrence.count_years_locked") : undefined}+ currentValue={template === YEAR_COUNT_TEMPLATE}+ disabled={locked}+ onChange={(checked) => setTemplate(checked ? YEAR_COUNT_TEMPLATE : null)}+ />+ </div>+ );+}+/** The raw RRULE string, for a rule the structured fields cannot say (see recurrence.ts). */function CustomRuleField({ value, commit }: {value: string;diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Calendar.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Calendar.htmlindex 368dfad9e6..100cf50341 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Calendar.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Calendar.html@@ -548,12 +548,22 @@ class="table">to loop infinitely).</p><pre><code class="language-text-x-trilium-auto">#calendar:title="shortName" #shortName="John S."</code></pre><h3>Displaying an age on a recurring event</h3><p>A birthday or an anniversary repeats every year, and what is interesting- about each occurrence is the number of years it marks. <code>#calendar:titleTemplate</code> sets- the title an event is displayed with, and is filled in again for every- occurrence the recurrence rule produces:</p><pre><code class="language-text-x-trilium-auto">#startDate=1990-05-12 #recurrence="FREQ=YEARLY" #calendar:titleTemplate="${title} (${years})"</code></pre>+ about each occurrence is the number of years it marks. To display it:</p>+<ol>+ <li>Set the event's start date to the original date (for example, the date+ of birth).</li>+ <li>Click the event, then select the <em>Repeats</em> dropdown.</li>+ <li>Select <em>Yearly</em> and check <em>Show the number of years in the title</em>.</li>+</ol><p>The event is then displayed as “John Smith (35)” in 2025, “John Smith(36)” in 2026, and so on. Only the calendar is affected; the note keepsits own title.</p>+<p>The checkbox sets the <code>#calendar:titleTemplate</code> label, which+ sets the title an event is displayed with and is filled in again for every+ occurrence the recurrence rule produces:</p><pre><code class="language-text-x-trilium-auto">#startDate=1990-05-12 #recurrence="FREQ=YEARLY" #calendar:titleTemplate="${title} (${years})"</code></pre>+<p>To word the title differently, edit the label directly. The checkbox is+ then disabled, as it is when the template is inherited, so that it never+ overwrites a template of your own.</p><p>The following values can be used in the template:</p><figure class="table"><table>diff --git a/docs/User Guide/User Guide/Collections/Calendar.md b/docs/User Guide/User Guide/Collections/Calendar.mdindex dec2a93b3c..2563327644 100644--- a/docs/User Guide/User Guide/Collections/Calendar.md+++ b/docs/User Guide/User Guide/Collections/Calendar.md@@ -302,13 +302,21 @@ Moreover, if there are more relations of the same name, they will be displayed a### Displaying an age on a recurring event-A birthday or an anniversary repeats every year, and what is interesting about each occurrence is the number of years it marks. `#calendar:titleTemplate` sets the title an event is displayed with, and is filled in again for every occurrence the recurrence rule produces:+A birthday or an anniversary repeats every year, and what is interesting about each occurrence is the number of years it marks. To display it:++1. Set the event's start date to the original date (for example, the date of birth).+2. Click the event, then select the _Repeats_ dropdown.+3. Select _Yearly_ and check _Show the number of years in the title_.++The event is then displayed as “John Smith (35)” in 2025, “John Smith (36)” in 2026, and so on. Only the calendar is affected; the note keeps its own title.++The checkbox sets the `#calendar:titleTemplate` label, which sets the title an event is displayed with and is filled in again for every occurrence the recurrence rule produces:```#startDate=1990-05-12 #recurrence="FREQ=YEARLY" #calendar:titleTemplate="${title} (${years})"```-The event is then displayed as “John Smith (35)” in 2025, “John Smith (36)” in 2026, and so on. Only the calendar is affected; the note keeps its own title.+To word the title differently, edit the label directly. The checkbox is then disabled, as it is when the template is inherited, so that it never overwrites a template of your own.The following values can be used in the template: