Skip to content

Build your personal knowledge base with Trilium Notes

chore(text): pass the date formatter through editor config

Elian Doran99e7616531f0
diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.tsx b/apps/client/src/widgets/type_widgets/text/EditableText.tsx
index 5d56160d64..42c306d208 100644
--- a/apps/client/src/widgets/type_widgets/text/EditableText.tsx
+++ b/apps/client/src/widgets/type_widgets/text/EditableText.tsx
@@ -27,7 +27,6 @@ import { t } from "../../../services/i18n";
import link, { parseNavigationStateFromUrl, type ViewScope } from "../../../services/link";
import type LoadResults from "../../../services/load_results";
import note_create from "../../../services/note_create";
-import options from "../../../services/options";
import { consumeSearchTerms } from "../../../services/search_jump";
import { getNoteShareLink } from "../../../services/share_link";
import toast from "../../../services/toast";
@@ -267,9 +266,6 @@ export default function EditableText({
setIconPickerRequest(request);
return () => setIconPickerRequest(null);
},
- formatDateTime(date: Date, format?: string) {
- return utils.formatDateTime(date, format ?? options.get("customDateTimeFormat"));
- },
// Keyboard shortcut
async insertDateTimeToTextCommand() {
const editor = await waitForEditor();
diff --git a/apps/client/src/widgets/type_widgets/text/config.spec.ts b/apps/client/src/widgets/type_widgets/text/config.spec.ts
index 25c92d8f7c..d9d6e15072 100644
--- a/apps/client/src/widgets/type_widgets/text/config.spec.ts
+++ b/apps/client/src/widgets/type_widgets/text/config.spec.ts
@@ -335,6 +335,16 @@ describe("CK config - tables", () => {
expect((await buildConfig(baseOpts())).autoSort)
.toEqual({ dateFormats: ["DD.MM.YYYY HH:mm"] });
});
+
+ it("formats inserted dates in a chosen format or the user's current one", async () => {
+ const { formatDateTime } = await buildConfig(baseOpts());
+ const date = new Date(2026, 8, 25, 10, 30);
+
+ expect(formatDateTime?.(date, "YYYY")).toBe("2026");
+
+ optionsState.map.customDateTimeFormat = "DD.MM.YYYY HH:mm";
+ expect(formatDateTime?.(date)).toBe("25.09.2026 10:30");
+ });
});
describe("CK config - licensing", () => {
diff --git a/apps/client/src/widgets/type_widgets/text/config.ts b/apps/client/src/widgets/type_widgets/text/config.ts
index 3aca1e9605..517f81e555 100644
--- a/apps/client/src/widgets/type_widgets/text/config.ts
+++ b/apps/client/src/widgets/type_widgets/text/config.ts
@@ -14,7 +14,7 @@ import { sanitizeNoteContentHtml } from "../../../services/sanitize_content.js";
import { getSourceEmbedTypeByLanguage, SOURCE_EMBED_TYPES } from "../../../services/source_embeds.js";
import { ensureMimeTypesForHighlighting, isSyntaxHighlightEnabled } from "../../../services/syntax_highlight.js";
import { getTaskStateDefinitions, openCustomTaskStateConfig } from "../../../services/task_states.js";
-import { isMac, isMobile } from "../../../services/utils.js";
+import { formatDateTime, isMac, isMobile } from "../../../services/utils.js";
import { resolveContentLanguage } from "../../../utils/formatters.js";
import SAMPLE_DIAGRAMS from "../mermaid/sample_diagrams.js";
import buildAiAssistantStream, { type AiNoteLocationProvider, buildAiAssistantQuickActions } from "./ai_assistant_stream.js";
@@ -196,6 +196,8 @@ export async function buildConfig(opts: BuildEditorOptions): Promise<EditorConfi
copy: copyTextWithToast,
copyHtml: copyHtmlWithToast
},
+ // Reads `customDateTimeFormat` on each call, so a changed format applies to open editors.
+ formatDateTime: (date, format) => formatDateTime(date, format ?? options.get("customDateTimeFormat")),
slashCommand: {
// Drop CKEditor's built-in list slash commands, whose Title Case titles the palette
// re-defines in sentence case.
diff --git a/packages/ckeditor5/src/augmentation.ts b/packages/ckeditor5/src/augmentation.ts
index 2dc14e739a..60b2d420a7 100644
--- a/packages/ckeditor5/src/augmentation.ts
+++ b/packages/ckeditor5/src/augmentation.ts
@@ -103,11 +103,6 @@ declare global {
* a balloon — leaves `container` alone and answers `null`.
*/
showIconPicker(request: IconPickerRequest): (() => void) | null;
- /**
- * Formats `date` for insertion in the Day.js `format`, or in the user's
- * `customDateTimeFormat` when none is given.
- */
- formatDateTime(date: Date, format?: string): string;
loadReferenceLinkTitle(
$el: JQuery<HTMLElement>, href: string, storedTitle?: string
): Promise<void>;
diff --git a/packages/ckeditor5/src/index.ts b/packages/ckeditor5/src/index.ts
index a671edd1af..edd6f51320 100644
--- a/packages/ckeditor5/src/index.ts
+++ b/packages/ckeditor5/src/index.ts
@@ -52,6 +52,7 @@ import "./plugins/content_embed/content_embed.js";
import "./plugins/content_embed/content_embed_caret.js";
import "./plugins/find_in_link_widgets.js";
import "./plugins/find_reveal.js";
+import "./plugins/insert_date_time.js";
import "./plugins/math/math.js";
import "./plugins/tabs/tabs.js";
import "./plugins/multicolumn/multicolumn.js";
diff --git a/packages/ckeditor5/src/plugins/insert_date_time.spec.ts b/packages/ckeditor5/src/plugins/insert_date_time.spec.ts
index a276b5822d..83c09cbf46 100644
--- a/packages/ckeditor5/src/plugins/insert_date_time.spec.ts
+++ b/packages/ckeditor5/src/plugins/insert_date_time.spec.ts
@@ -5,7 +5,6 @@ import {
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createTestEditor } from "../../test/editor-kit.js";
-import { installGlobMock } from "../../test/globals-test-kit.js";
import InsertDateTimePlugin, { COMMAND_NAME, DATE_TIME_PRESETS } from "./insert_date_time.js";
describe("InsertDateTimePlugin", () => {
@@ -14,11 +13,9 @@ describe("InsertDateTimePlugin", () => {
beforeEach(async () => {
formatDateTime = vi.fn((_date: Date, format?: string) => (format ? `<${format}>` : "2026-09-25 10:30"));
- installGlobMock({
- getComponentByEl: () => ({ formatDateTime })
+ editor = await createTestEditor([Essentials, Paragraph, Bold, HorizontalLine, InsertDateTimePlugin], {
+ formatDateTime
});
-
- editor = await createTestEditor([Essentials, Paragraph, Bold, HorizontalLine, InsertDateTimePlugin]);
});
it("loads the plugin and registers the command and toolbar button", () => {
@@ -27,7 +24,7 @@ describe("InsertDateTimePlugin", () => {
expect(editor.ui.componentFactory.has("dateTime")).toBe(true);
});
- it("inserts the date formatted by the host at the caret", () => {
+ it("inserts the date formatted by formatDateTime at the caret", () => {
_setModelData(editor.model, "<paragraph>Due: []</paragraph>");
editor.execute(COMMAND_NAME);
@@ -59,7 +56,7 @@ describe("InsertDateTimePlugin", () => {
expect(_getModelData(editor.model)).toBe("<paragraph>[yesterday]</paragraph>");
});
- it("passes a chosen format to the host", () => {
+ it("passes a chosen format to formatDateTime", () => {
_setModelData(editor.model, "<paragraph>[]</paragraph>");
editor.execute(COMMAND_NAME, { format: "HH:mm" });
@@ -67,6 +64,21 @@ describe("InsertDateTimePlugin", () => {
expect(_getModelData(editor.model)).toBe("<paragraph><HH:mm>[]</paragraph>");
});
+ it("inserts the date in the browser's locale format without formatDateTime", async () => {
+ const plainEditor = await createTestEditor([ Essentials, Paragraph, InsertDateTimePlugin ]);
+ vi.useFakeTimers({ now: new Date(2026, 8, 25, 10, 30), toFake: [ "Date" ] });
+
+ try {
+ _setModelData(plainEditor.model, "<paragraph>[]</paragraph>");
+ plainEditor.execute(COMMAND_NAME, { format: "HH:mm" });
+
+ const expected = new Date(2026, 8, 25, 10, 30).toLocaleString();
+ expect(_getModelData(plainEditor.model)).toBe(`<paragraph>${expected}[]</paragraph>`);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
describe("split button", () => {
let dropdown: DropdownView;
diff --git a/packages/ckeditor5/src/plugins/insert_date_time.ts b/packages/ckeditor5/src/plugins/insert_date_time.ts
index a09f8a1052..537a8a341e 100644
--- a/packages/ckeditor5/src/plugins/insert_date_time.ts
+++ b/packages/ckeditor5/src/plugins/insert_date_time.ts
@@ -67,13 +67,13 @@ export default class InsertDateTimePlugin extends Plugin {
}
/**
- * Inserts the current date and time, formatted by the host through `formatDateTime()`, in place of
+ * Inserts the current date and time, formatted by the `formatDateTime` config entry, in place of
* the selection. The text takes the selection's attributes, so it stays bold inside bold text.
*/
class InsertDateTimeCommand extends Command {
/**
- * @param options.format a Day.js format string; without one, the host applies the user's
+ * @param options.format a Day.js format string; without one, `formatDateTime` applies the user's
* `customDateTimeFormat`.
*/
execute({ format }: { format?: string } = {}) {
@@ -131,6 +131,18 @@ function createFormatItems(editor: Editor): ListDropdownItemDefinition[] {
}
function formatNow(editor: Editor, format?: string) {
- const editorEl = editor.editing.view.getDomRoot();
- return glob.getComponentByEl<EditorComponent>(editorEl).formatDateTime(new Date(), format);
+ const formatDateTime = editor.config.get('formatDateTime');
+ const now = new Date();
+ return formatDateTime ? formatDateTime(now, format) : now.toLocaleString();
+}
+
+declare module 'ckeditor5' {
+ interface EditorConfig {
+ /**
+ * Formats `date` for insertion in the Day.js `format`, or in the user's
+ * `customDateTimeFormat` when none is given. Without it, the date is inserted in the
+ * browser's locale format and the presets render the same.
+ */
+ formatDateTime?(date: Date, format?: string): string;
+ }
}
diff --git a/packages/ckeditor5/src/plugins/mention/slash_commands.spec.ts b/packages/ckeditor5/src/plugins/mention/slash_commands.spec.ts
index 7d5f0a3042..c56f88e600 100644
--- a/packages/ckeditor5/src/plugins/mention/slash_commands.spec.ts
+++ b/packages/ckeditor5/src/plugins/mention/slash_commands.spec.ts
@@ -17,7 +17,6 @@ import {
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createTestEditor } from "../../../test/editor-kit.js";
-import { installGlobMock } from "../../../test/globals-test-kit.js";
import { COMMAND_NAME as CONTENT_EMBED_COMMAND } from "../content_embed/content_embed.js";
import { INSERT_ICON_COMMAND } from "../inline_icon/inline_icon_editing.js";
import InlineIconUI from "../inline_icon/inline_icon_ui.js";
@@ -698,8 +697,7 @@ describe("buildTriliumSlashCommands", () => {
beforeEach(async () => {
formatDateTime = vi.fn((_date: Date, format?: string) => format ?? "2026-09-25 10:30");
- installGlobMock({ getComponentByEl: () => ({ formatDateTime }) });
- editor = await createTestEditor([ Essentials, Paragraph, InsertDateTimePlugin ]);
+ editor = await createTestEditor([ Essentials, Paragraph, InsertDateTimePlugin ], { formatDateTime });
});
function dateTimeEntries() {