UNPKG

3.2 kBJavaScriptView Raw
1/**
2 * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4 */
5/**
6 * @module heading/headingbuttonsui
7 */
8import { icons, Plugin } from 'ckeditor5/src/core.js';
9import { ButtonView } from 'ckeditor5/src/ui.js';
10import { getLocalizedOptions } from './utils.js';
11const defaultIcons = {
12 heading1: icons.heading1,
13 heading2: icons.heading2,
14 heading3: icons.heading3,
15 heading4: icons.heading4,
16 heading5: icons.heading5,
17 heading6: icons.heading6
18};
19/**
20 * The `HeadingButtonsUI` plugin defines a set of UI buttons that can be used instead of the
21 * standard drop down component.
22 *
23 * This feature is not enabled by default by the {@link module:heading/heading~Heading} plugin and needs to be
24 * installed manually to the editor configuration.
25 *
26 * Plugin introduces button UI elements, which names are same as `model` property from {@link module:heading/headingconfig~HeadingOption}.
27 *
28 * ```ts
29 * ClassicEditor
30 * .create( {
31 * plugins: [ ..., Heading, Paragraph, HeadingButtonsUI, ParagraphButtonUI ]
32 * heading: {
33 * options: [
34 * { model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
35 * { model: 'heading1', view: 'h2', title: 'Heading 1', class: 'ck-heading_heading1' },
36 * { model: 'heading2', view: 'h3', title: 'Heading 2', class: 'ck-heading_heading2' },
37 * { model: 'heading3', view: 'h4', title: 'Heading 3', class: 'ck-heading_heading3' }
38 * ]
39 * },
40 * toolbar: [ 'paragraph', 'heading1', 'heading2', 'heading3' ]
41 * } )
42 * .then( ... )
43 * .catch( ... );
44 * ```
45 *
46 * NOTE: The `'paragraph'` button is defined in by the {@link module:paragraph/paragraphbuttonui~ParagraphButtonUI} plugin
47 * which needs to be loaded manually as well.
48 *
49 * It is possible to use custom icons by providing `icon` config option in {@link module:heading/headingconfig~HeadingOption}.
50 * For the default configuration standard icons are used.
51 */
52export default class HeadingButtonsUI extends Plugin {
53 /**
54 * @inheritDoc
55 */
56 init() {
57 const options = getLocalizedOptions(this.editor);
58 options
59 .filter(item => item.model !== 'paragraph')
60 .map(item => this._createButton(item));
61 }
62 /**
63 * Creates single button view from provided configuration option.
64 */
65 _createButton(option) {
66 const editor = this.editor;
67 editor.ui.componentFactory.add(option.model, locale => {
68 const view = new ButtonView(locale);
69 const command = editor.commands.get('heading');
70 view.label = option.title;
71 view.icon = option.icon || defaultIcons[option.model];
72 view.tooltip = true;
73 view.isToggleable = true;
74 view.bind('isEnabled').to(command);
75 view.bind('isOn').to(command, 'value', value => value == option.model);
76 view.on('execute', () => {
77 editor.execute('heading', { value: option.model });
78 editor.editing.view.focus();
79 });
80 return view;
81 });
82 }
83}