/
utils.ts
48 lines (41 loc) · 1.26 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module basic-styles/utils
*/
import type { Editor, Plugin } from 'ckeditor5/src/core.js';
import type AttributeCommand from './attributecommand.js';
import type { ButtonView, MenuBarMenuListItemButtonView } from 'ckeditor5/src/ui.js';
/**
* Returns a function that creates a (toolbar or menu bar) button for a basic style feature.
*/
export function getButtonCreator( {
editor, commandName, plugin, icon, label, keystroke
}: {
editor: Editor;
commandName: string;
icon: string;
label: string;
plugin: Plugin;
keystroke?: string;
} ) {
return <T extends typeof ButtonView | typeof MenuBarMenuListItemButtonView>( ButtonClass: T ): InstanceType<T> => {
const command = editor.commands.get( commandName )! as AttributeCommand;
const view = new ButtonClass( editor.locale ) as InstanceType<T>;
view.set( {
label,
icon,
keystroke,
isToggleable: true
} );
view.bind( 'isEnabled' ).to( command, 'isEnabled' );
// Execute the command.
plugin.listenTo( view, 'execute', () => {
editor.execute( commandName );
editor.editing.view.focus();
} );
return view;
};
}