feat(slash): insert date/time

This commit is contained in:
Elian Doran 2025-06-16 20:42:55 +03:00
parent 6f386f50ff
commit 9bfff03cff
No known key found for this signature in database
3 changed files with 43 additions and 23 deletions

View File

@ -19,7 +19,7 @@ import { PopupEditor, ClassicEditor, EditorWatchdog, type CKTextEditor, type Men
import "@triliumnext/ckeditor5/index.css"; import "@triliumnext/ckeditor5/index.css";
import { normalizeMimeTypeForCKEditor } from "@triliumnext/commons"; import { normalizeMimeTypeForCKEditor } from "@triliumnext/commons";
const ENABLE_INSPECTOR = false; const ENABLE_INSPECTOR = true;
const mentionSetup: MentionFeed[] = [ const mentionSetup: MentionFeed[] = [
{ {

View File

@ -3,13 +3,27 @@ import type { SlashCommandEditorConfig } from 'ckeditor5-premium-features';
import { icons as admonitionIcons } from '@triliumnext/ckeditor5-admonition'; import { icons as admonitionIcons } from '@triliumnext/ckeditor5-admonition';
import { icons as footnoteIcons } from '@triliumnext/ckeditor5-footnotes'; import { icons as footnoteIcons } from '@triliumnext/ckeditor5-footnotes';
import { ADMONITION_TYPES, type AdmonitionType } from '@triliumnext/ckeditor5-admonition'; import { ADMONITION_TYPES, type AdmonitionType } from '@triliumnext/ckeditor5-admonition';
import dateTimeIcon from './icons/date-time.svg?raw';
type SlashCommandDefinition = SlashCommandEditorConfig["extraCommands"][number]; type SlashCommandDefinition = SlashCommandEditorConfig["extraCommands"][number];
export default function buildExtraCommands(): SlashCommandDefinition[] { export default function buildExtraCommands(): SlashCommandDefinition[] {
return [ return [
...buildAdmonitionExtraCommands(), ...buildAdmonitionExtraCommands(),
...buildFootnoteExtraCommands() {
id: 'footnote',
title: 'Footnote',
description: 'Create a new footnote and reference it here',
icon: footnoteIcons.insertFootnoteIcon,
commandName: "InsertFootnote"
},
{
id: "datetime",
title: "Insert Date/Time",
description: "Insert the current date and time",
icon: dateTimeIcon,
commandName: "insertDateTimeToText"
}
]; ];
} }
@ -27,14 +41,3 @@ function buildAdmonitionExtraCommands(): SlashCommandDefinition[] {
return commands; return commands;
} }
function buildFootnoteExtraCommands(): SlashCommandDefinition[] {
return [
{
id: 'footnote',
title: 'Footnote',
description: 'Create a new footnote and reference it here',
icon: footnoteIcons.insertFootnoteIcon,
execute: (editor: Editor) => editor.execute("InsertFootnote")
}
];
}

View File

@ -1,10 +1,14 @@
import { ButtonView, Plugin } from 'ckeditor5'; import { ButtonView, Command, Plugin } from 'ckeditor5';
import dateTimeIcon from '../icons/date-time.svg?raw'; import dateTimeIcon from '../icons/date-time.svg?raw';
const COMMAND_NAME = 'insertDateTimeToText';
export default class InsertDateTimePlugin extends Plugin { export default class InsertDateTimePlugin extends Plugin {
init() { init() {
const editor = this.editor; const editor = this.editor;
editor.commands.add(COMMAND_NAME, new InsertDateTimeCommand(editor));
editor.ui.componentFactory.add('dateTime', locale => { editor.ui.componentFactory.add('dateTime', locale => {
const view = new ButtonView( locale ); const view = new ButtonView( locale );
@ -15,17 +19,30 @@ export default class InsertDateTimePlugin extends Plugin {
} ); } );
// enable only if the editor is not read only // enable only if the editor is not read only
view.bind('isEnabled').to(editor, 'isReadOnly', isReadOnly => !isReadOnly); const command = editor.commands.get(COMMAND_NAME)!;
view.bind('isEnabled').to(command, 'isEnabled');
view.on('execute', () => { view.on('execute', () => {
editor.execute(COMMAND_NAME);
editor.editing.view.focus();
});
return view;
});
}
}
class InsertDateTimeCommand extends Command {
refresh() {
this.isEnabled = !this.editor.isReadOnly;
}
execute() {
const editor = this.editor;
const editorEl = editor.editing.view.getDomRoot(); const editorEl = editor.editing.view.getDomRoot();
const component = glob.getComponentByEl(editorEl); const component = glob.getComponentByEl(editorEl);
component.triggerCommand('insertDateTimeToText'); component.triggerCommand('insertDateTimeToText');
editor.editing.view.focus(); editor.editing.view.focus();
} );
return view;
});
} }
} }