Skip to content

Build your personal knowledge base with Trilium Notes

refactor(calendar): curate and localize slot duration options

Elian Doran847569bafd7f
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index dec2b60555..c299cf5c85 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2578,7 +2578,11 @@
"calendar_view": {
"delete_note": "Delete note...",
"set_slot_duration": "Time slot duration",
- "set_slot_label_interval": "Time slot label interval"
+ "set_slot_label_interval": "Time slot label interval",
+ "minutes_one": "{{count}} minute",
+ "minutes_other": "{{count}} minutes",
+ "hours_one": "{{count}} hour",
+ "hours_other": "{{count}} hours"
},
"ocr": {
"extracted_text": "Extracted Text (OCR)",
diff --git a/apps/client/src/widgets/collections/calendar/index.tsx b/apps/client/src/widgets/collections/calendar/index.tsx
index 01e6d2a1f2..be4a3734b5 100644
--- a/apps/client/src/widgets/collections/calendar/index.tsx
+++ b/apps/client/src/widgets/collections/calendar/index.tsx
@@ -39,7 +39,6 @@ interface CalendarViewData {
nextText: string;
}
-
const CALENDAR_VIEWS = [
{
type: "timeGridDay",
@@ -83,7 +82,6 @@ const SUPPORTED_CALENDAR_VIEW_TYPE = CALENDAR_VIEWS.map(v => v.type);
const DEFAULT_SLOT_DURATION = "00:15:00";
const DEFAULT_SLOT_LABEL_INTERVAL = "01:00:00";
-
// Here we hard-code the imports in order to ensure that they are embedded by webpack without having to load all the languages.
export const LOCALE_MAPPINGS: Record<DISPLAYABLE_LOCALE_IDS, (() => Promise<{ default: LocaleInput }>) | null> = {
de: () => import("@fullcalendar/core/locales/de"),
diff --git a/apps/client/src/widgets/collections/calendar/utils.ts b/apps/client/src/widgets/collections/calendar/utils.ts
index 5d0d0f4e95..9ab47b9f9d 100644
--- a/apps/client/src/widgets/collections/calendar/utils.ts
+++ b/apps/client/src/widgets/collections/calendar/utils.ts
@@ -1,6 +1,5 @@
import { DateSelectArg } from "@fullcalendar/core/index.js";
import { EventImpl } from "@fullcalendar/core/internal";
-import { dayjs } from "@triliumnext/commons";
import FNote from "../../../entities/fnote";
export function parseStartEndDateFromEvent(e: DateSelectArg | EventImpl) {
@@ -82,15 +81,19 @@ export function getCustomisableLabel(note: FNote, defaultLabelName: string, cust
return note.getLabelValue(defaultLabelName);
}
+// Bounds for a FullCalendar slot duration / label interval, in seconds.
+const MIN_DURATION_SECONDS = 60; // 1 minute
+const MAX_DURATION_SECONDS = 24 * 60 * 60; // 24 hours
+
export function isValidDuration(str: string | null | undefined): boolean {
- if (!str || !/^(\d{2}):([0-5]\d):([0-5]\d)$/.test(str)) return false;
+ // The regex already constrains minutes/seconds to 00–59, so only the total needs bounding.
+ const match = str?.match(/^(\d{2}):([0-5]\d):([0-5]\d)$/);
+ if (!match) return false;
- const [hours, minutes, seconds] = str.split(":").map(Number);
- const totalMs = dayjs.duration({ hours, minutes, seconds }).asMilliseconds();
- const oneMinute = dayjs.duration(1, "minute").asMilliseconds();
- const twentyFourHours = dayjs.duration(24, "hours").asMilliseconds();
+ const [, hours, minutes, seconds] = match;
+ const totalSeconds = Number(hours) * 3600 + Number(minutes) * 60 + Number(seconds);
- return totalMs >= oneMinute && totalMs <= twentyFourHours;
+ return totalSeconds >= MIN_DURATION_SECONDS && totalSeconds <= MAX_DURATION_SECONDS;
}
// Source: https://stackoverflow.com/a/30465299/4898894
diff --git a/apps/client/src/widgets/ribbon/collection-properties-config.tsx b/apps/client/src/widgets/ribbon/collection-properties-config.tsx
index 47734ad66c..7e686c919a 100644
--- a/apps/client/src/widgets/ribbon/collection-properties-config.tsx
+++ b/apps/client/src/widgets/ribbon/collection-properties-config.tsx
@@ -65,30 +65,16 @@ export const bookPropertiesConfig: Record<ViewTypeOptions, BookConfig> = {
icon: "bx bx-time",
type: "combobox",
bindToLabel: "calendar:slotDuration",
- options: [
- { value: "00:01:00", label: "1 Minute" },
- { value: "00:05:00", label: "5 Minutes" },
- { value: "00:10:00", label: "10 Minutes" },
- { value: "00:15:00", label: "15 Minutes" },
- { value: "00:20:00", label: "20 Minutes" },
- { value: "00:30:00", label: "30 Minutes" },
- { value: "01:00:00", label: "1 Hour" }
- ]
+ options: slotDurationOptions([5, 10, 15, 20, 30, 60])
},
{
label: t("calendar_view.set_slot_label_interval"),
icon: "bx bx-time",
type: "combobox",
bindToLabel: "calendar:slotLabelInterval",
- options: [
- { value: "00:01:00", label: "1 Minute" },
- { value: "00:05:00", label: "5 Minutes" },
- { value: "00:10:00", label: "10 Minutes" },
- { value: "00:20:00", label: "20 Minutes" },
- { value: "00:30:00", label: "30 Minutes" },
- { value: "01:00:00", label: "1 Hour" }
- ]
- },
+ // Labels want to be coarser than the slots themselves.
+ options: slotDurationOptions([15, 30, 60])
+ }
]
},
geoMap: {
@@ -177,6 +163,20 @@ function buildMapLayer([ id, layer ]: [ string, MapLayer ]): ComboBoxItem {
};
}
+/** Builds FullCalendar duration options (`HH:MM:00`) from a list of whole minutes, with localized labels. */
+function slotDurationOptions(minutes: number[]): ComboBoxItem[] {
+ return minutes.map((total) => ({
+ value: `${pad(Math.floor(total / 60))}:${pad(total % 60)}:00`,
+ label: total % 60 === 0
+ ? t("calendar_view.hours", { count: total / 60 })
+ : t("calendar_view.minutes", { count: total })
+ }));
+}
+
+function pad(n: number) {
+ return String(n).padStart(2, "0");
+}
+
function ListExpandDepth(context: { note: FNote, parentComponent: Component }) {
const [ currentDepth ] = useNoteLabel(context.note, "expanded");
diff --git a/packages/commons/src/lib/attribute_names.ts b/packages/commons/src/lib/attribute_names.ts
index 79966099dd..7e26e71ffe 100644
--- a/packages/commons/src/lib/attribute_names.ts
+++ b/packages/commons/src/lib/attribute_names.ts
@@ -50,11 +50,11 @@ type Labels = {
geolocation: string;
expanded: string;
"calendar:hideWeekends": boolean;
+ "calendar:weekNumbers": boolean;
+ "calendar:view": string;
"calendar:initialDate": string;
"calendar:slotDuration": string;
"calendar:slotLabelInterval": string;
- "calendar:weekNumbers": boolean;
- "calendar:view": string;
"map:style": string;
"map:scale": boolean;
"map:hideLabels": boolean;