Skip to content

Build your personal knowledge base with Trilium Notes

refactor(text): insert the date/time from the editor plugin

Elian Dorand773d5513787
diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.tsx b/apps/client/src/widgets/type_widgets/text/EditableText.tsx
index 812e54a60e..4baf753761 100644
--- a/apps/client/src/widgets/type_widgets/text/EditableText.tsx
+++ b/apps/client/src/widgets/type_widgets/text/EditableText.tsx
@@ -154,13 +154,13 @@ export default function EditableText({ note, parentComponent, ntxId, noteContext
setIconPickerRequest(request);
return () => setIconPickerRequest(null);
},
- insertDateTimeToTextCommand() {
- if (!editorApiRef.current) return;
- const date = new Date();
- const customDateTimeFormat = options.get("customDateTimeFormat");
- const dateString = utils.formatDateTime(date, customDateTimeFormat);
-
- addTextToEditor(dateString);
+ formatDateTime(date: Date) {
+ return utils.formatDateTime(date, options.get("customDateTimeFormat"));
+ },
+ // Keyboard shortcut
+ async insertDateTimeToTextCommand() {
+ const editor = await waitForEditor();
+ editor?.execute("insertDateTimeToText");
},
// Include note functionality note
addIncludeNoteToTextCommand() {
diff --git a/packages/ckeditor5/src/augmentation.ts b/packages/ckeditor5/src/augmentation.ts
index c7f598e81b..1df277d259 100644
--- a/packages/ckeditor5/src/augmentation.ts
+++ b/packages/ckeditor5/src/augmentation.ts
@@ -36,6 +36,8 @@ declare global {
* a balloon — leaves `container` alone and answers `null`.
*/
showIconPicker(request: IconPickerRequest): (() => void) | null;
+ /** Formats `date` for insertion, in the user's `customDateTimeFormat`. */
+ formatDateTime(date: Date): string;
loadReferenceLinkTitle($el: JQuery<HTMLElement>, href: string): Promise<void>;
createNoteForReferenceLink(title: string, intoInbox: boolean): Promise<string | undefined>;
loadIncludedNote(noteId: string, $el: JQuery<HTMLElement>, boxSize?: string): void;
diff --git a/packages/ckeditor5/src/plugins/insert_date_time.spec.ts b/packages/ckeditor5/src/plugins/insert_date_time.spec.ts
index 4b68aaa548..c3288c9bfc 100644
--- a/packages/ckeditor5/src/plugins/insert_date_time.spec.ts
+++ b/packages/ckeditor5/src/plugins/insert_date_time.spec.ts
@@ -1,4 +1,4 @@
-import { ClassicEditor, Essentials, Paragraph } from "ckeditor5";
+import { _getModelData, _setModelData, Bold, ButtonView, ClassicEditor, Essentials, HorizontalLine, Paragraph } from "ckeditor5";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createTestEditor } from "../../test/editor-kit.js";
@@ -7,15 +7,15 @@ import InsertDateTimePlugin, { COMMAND_NAME } from "./insert_date_time.js";
describe("InsertDateTimePlugin", () => {
let editor: ClassicEditor;
- let triggerCommand: ReturnType<typeof vi.fn>;
+ let formatDateTime: ReturnType<typeof vi.fn>;
beforeEach(async () => {
- triggerCommand = vi.fn();
+ formatDateTime = vi.fn(() => "2026-09-25 10:30");
installGlobMock({
- getComponentByEl: () => ({ triggerCommand })
+ getComponentByEl: () => ({ formatDateTime })
});
- editor = await createTestEditor([Essentials, Paragraph, InsertDateTimePlugin]);
+ editor = await createTestEditor([Essentials, Paragraph, Bold, HorizontalLine, InsertDateTimePlugin]);
});
it("loads the plugin and registers the command and toolbar button", () => {
@@ -29,13 +29,40 @@ describe("InsertDateTimePlugin", () => {
expect(view.label).toBe("Insert date/time");
});
- it("triggers insertDateTimeToText on the glob component when executed", () => {
+ it("inserts the date formatted by the host at the caret", () => {
+ _setModelData(editor.model, "<paragraph>Due: []</paragraph>");
editor.execute(COMMAND_NAME);
- expect(triggerCommand).toHaveBeenCalledWith("insertDateTimeToText");
+
+ expect(formatDateTime).toHaveBeenCalledWith(expect.any(Date));
+ expect(_getModelData(editor.model)).toBe("<paragraph>Due: 2026-09-25 10:30[]</paragraph>");
+ });
+
+ it("replaces a selection and keeps its formatting", () => {
+ _setModelData(editor.model, "<paragraph>Due: <$text bold=\"true\">[yesterday]</$text></paragraph>");
+ editor.execute(COMMAND_NAME);
+
+ expect(_getModelData(editor.model))
+ .toBe("<paragraph>Due: <$text bold=\"true\">2026-09-25 10:30[]</$text></paragraph>");
+ });
+
+ it("replaces a selected widget with a paragraph holding the date", () => {
+ _setModelData(editor.model, "<paragraph>a</paragraph>[<horizontalLine></horizontalLine>]");
+ editor.execute(COMMAND_NAME);
+
+ expect(_getModelData(editor.model))
+ .toBe("<paragraph>a</paragraph><paragraph>2026-09-25 10:30[]</paragraph>");
+ });
+
+ it("undoes the insertion in a single step", () => {
+ _setModelData(editor.model, "<paragraph>[yesterday]</paragraph>");
+ editor.execute(COMMAND_NAME);
+ editor.execute("undo");
+
+ expect(_getModelData(editor.model)).toBe("<paragraph>[yesterday]</paragraph>");
});
it("wires the button to the command (enablement and execution)", () => {
- const view = editor.ui.componentFactory.create("dateTime") as { isEnabled: boolean; fire(name: string): void };
+ const view = editor.ui.componentFactory.create("dateTime") as ButtonView;
const command = editor.commands.get(COMMAND_NAME);
expect(view.isEnabled).toBe(command?.isEnabled);
@@ -45,13 +72,12 @@ describe("InsertDateTimePlugin", () => {
expect(spy).toHaveBeenCalledWith(COMMAND_NAME);
});
- it("is enabled when the editor is not read-only", () => {
- expect(editor.commands.get(COMMAND_NAME)?.isEnabled).toBe(true);
- });
+ it("is enabled only when the editor is editable", () => {
+ const command = editor.commands.get(COMMAND_NAME);
+ expect(command?.isEnabled).toBe(true);
- it("is disabled when the editor is read-only", () => {
editor.enableReadOnlyMode("test");
- expect(editor.commands.get(COMMAND_NAME)?.isEnabled).toBe(false);
+ expect(command?.isEnabled).toBe(false);
editor.disableReadOnlyMode("test");
});
});
diff --git a/packages/ckeditor5/src/plugins/insert_date_time.ts b/packages/ckeditor5/src/plugins/insert_date_time.ts
index c9a5bcf6a2..54491dce63 100644
--- a/packages/ckeditor5/src/plugins/insert_date_time.ts
+++ b/packages/ckeditor5/src/plugins/insert_date_time.ts
@@ -8,7 +8,8 @@ export default class InsertDateTimePlugin extends Plugin {
const editor = this.editor;
const t = editor.t;
- editor.commands.add(COMMAND_NAME, new InsertDateTimeCommand(editor));
+ const command = new InsertDateTimeCommand(editor);
+ editor.commands.add(COMMAND_NAME, command);
editor.ui.componentFactory.add('dateTime', locale => {
const view = new ButtonView( locale );
@@ -19,8 +20,6 @@ export default class InsertDateTimePlugin extends Plugin {
tooltip: true
} );
- // enable only if the editor is not read only
- const command = editor.commands.get(COMMAND_NAME)!;
view.bind('isEnabled').to(command, 'isEnabled');
view.on('execute', () => {
editor.execute(COMMAND_NAME);
@@ -31,19 +30,22 @@ export default class InsertDateTimePlugin extends Plugin {
}
}
+/**
+ * Inserts the current date and time, formatted by the host through `formatDateTime()`, in place of
+ * the selection. The text takes the selection's attributes, so it stays bold inside bold text.
+ */
class InsertDateTimeCommand extends Command {
- refresh() {
- this.isEnabled = !this.editor.isReadOnly;
- }
-
execute() {
- const editor = this.editor;
- const editorEl = editor.editing.view.getDomRoot();
- const component = glob.getComponentByEl(editorEl);
-
- component.triggerCommand('insertDateTimeToText');
- editor.editing.view.focus();
+ const model = this.editor.model;
+ const selection = model.document.selection;
+ const editorEl = this.editor.editing.view.getDomRoot();
+ const text = glob.getComponentByEl<EditorComponent>(editorEl).formatDateTime(new Date());
+
+ model.change(writer => {
+ const attributes = selection.getAttributes();
+ model.insertContent(writer.createText(text, attributes));
+ });
}
}