feat(slash): insert date/time

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

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';
const COMMAND_NAME = 'insertDateTimeToText';
export default class InsertDateTimePlugin extends Plugin {
init() {
const editor = this.editor;
editor.commands.add(COMMAND_NAME, new InsertDateTimeCommand(editor));
editor.ui.componentFactory.add('dateTime', 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
view.bind('isEnabled').to(editor, 'isReadOnly', isReadOnly => !isReadOnly);
const command = editor.commands.get(COMMAND_NAME)!;
view.bind('isEnabled').to(command, 'isEnabled');
view.on('execute', () => {
const editorEl = editor.editing.view.getDomRoot();
const component = glob.getComponentByEl(editorEl);
component.triggerCommand('insertDateTimeToText');
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 component = glob.getComponentByEl(editorEl);
component.triggerCommand('insertDateTimeToText');
editor.editing.view.focus();
}
}