/
headingbuttonsui.ts
96 lines (85 loc) · 3.14 KB
/
headingbuttonsui.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* @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 heading/headingbuttonsui
*/
import { icons, Plugin } from 'ckeditor5/src/core.js';
import { ButtonView } from 'ckeditor5/src/ui.js';
import { getLocalizedOptions } from './utils.js';
import type { HeadingOption } from './headingconfig.js';
import type HeadingCommand from './headingcommand.js';
const defaultIcons: Record<string, string> = {
heading1: icons.heading1,
heading2: icons.heading2,
heading3: icons.heading3,
heading4: icons.heading4,
heading5: icons.heading5,
heading6: icons.heading6
};
/**
* The `HeadingButtonsUI` plugin defines a set of UI buttons that can be used instead of the
* standard drop down component.
*
* This feature is not enabled by default by the {@link module:heading/heading~Heading} plugin and needs to be
* installed manually to the editor configuration.
*
* Plugin introduces button UI elements, which names are same as `model` property from {@link module:heading/headingconfig~HeadingOption}.
*
* ```ts
* ClassicEditor
* .create( {
* plugins: [ ..., Heading, Paragraph, HeadingButtonsUI, ParagraphButtonUI ]
* heading: {
* options: [
* { model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
* { model: 'heading1', view: 'h2', title: 'Heading 1', class: 'ck-heading_heading1' },
* { model: 'heading2', view: 'h3', title: 'Heading 2', class: 'ck-heading_heading2' },
* { model: 'heading3', view: 'h4', title: 'Heading 3', class: 'ck-heading_heading3' }
* ]
* },
* toolbar: [ 'paragraph', 'heading1', 'heading2', 'heading3' ]
* } )
* .then( ... )
* .catch( ... );
* ```
*
* NOTE: The `'paragraph'` button is defined in by the {@link module:paragraph/paragraphbuttonui~ParagraphButtonUI} plugin
* which needs to be loaded manually as well.
*
* It is possible to use custom icons by providing `icon` config option in {@link module:heading/headingconfig~HeadingOption}.
* For the default configuration standard icons are used.
*/
export default class HeadingButtonsUI extends Plugin {
/**
* @inheritDoc
*/
public init(): void {
const options = getLocalizedOptions( this.editor );
options
.filter( item => item.model !== 'paragraph' )
.map( item => this._createButton( item ) );
}
/**
* Creates single button view from provided configuration option.
*/
private _createButton( option: HeadingOption ) {
const editor = this.editor;
editor.ui.componentFactory.add( option.model, locale => {
const view = new ButtonView( locale );
const command: HeadingCommand = editor.commands.get( 'heading' )!;
view.label = option.title;
view.icon = option.icon || defaultIcons[ option.model ];
view.tooltip = true;
view.isToggleable = true;
view.bind( 'isEnabled' ).to( command );
view.bind( 'isOn' ).to( command, 'value', value => value == option.model );
view.on( 'execute', () => {
editor.execute( 'heading', { value: option.model } );
editor.editing.view.focus();
} );
return view;
} );
}
}