@ckeditor/ckeditor5-highlight 35.3.2 → 36.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,20 +1,15 @@
1
1
  /**
2
- * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module highlight/highlightui
8
7
  */
9
-
10
8
  import { Plugin, icons } from 'ckeditor5/src/core';
11
9
  import { ButtonView, SplitButtonView, ToolbarSeparatorView, createDropdown, addToolbarToDropdown } from 'ckeditor5/src/ui';
12
-
13
10
  import markerIcon from './../theme/icons/marker.svg';
14
11
  import penIcon from './../theme/icons/pen.svg';
15
-
16
12
  import './../theme/highlight.css';
17
-
18
13
  /**
19
14
  * The default highlight UI plugin. It introduces:
20
15
  *
@@ -32,234 +27,190 @@ import './../theme/highlight.css';
32
27
  *
33
28
  * See the {@link module:highlight/highlight~HighlightConfig#options configuration} to learn more
34
29
  * about the defaults.
35
- *
36
- * @extends module:core/plugin~Plugin
37
30
  */
38
31
  export default class HighlightUI extends Plugin {
39
- /**
40
- * Returns the localized option titles provided by the plugin.
41
- *
42
- * The following localized titles corresponding with default
43
- * {@link module:highlight/highlight~HighlightConfig#options} are available:
44
- *
45
- * * `'Yellow marker'`,
46
- * * `'Green marker'`,
47
- * * `'Pink marker'`,
48
- * * `'Blue marker'`,
49
- * * `'Red pen'`,
50
- * * `'Green pen'`.
51
- *
52
- * @readonly
53
- * @type {Object.<String,String>}
54
- */
55
- get localizedOptionTitles() {
56
- const t = this.editor.t;
57
-
58
- return {
59
- 'Yellow marker': t( 'Yellow marker' ),
60
- 'Green marker': t( 'Green marker' ),
61
- 'Pink marker': t( 'Pink marker' ),
62
- 'Blue marker': t( 'Blue marker' ),
63
- 'Red pen': t( 'Red pen' ),
64
- 'Green pen': t( 'Green pen' )
65
- };
66
- }
67
-
68
- /**
69
- * @inheritDoc
70
- */
71
- static get pluginName() {
72
- return 'HighlightUI';
73
- }
74
-
75
- /**
76
- * @inheritDoc
77
- */
78
- init() {
79
- const options = this.editor.config.get( 'highlight.options' );
80
-
81
- for ( const option of options ) {
82
- this._addHighlighterButton( option );
83
- }
84
-
85
- this._addRemoveHighlightButton();
86
-
87
- this._addDropdown( options );
88
- }
89
-
90
- /**
91
- * Creates the "Remove highlight" button.
92
- *
93
- * @private
94
- */
95
- _addRemoveHighlightButton() {
96
- const t = this.editor.t;
97
- const command = this.editor.commands.get( 'highlight' );
98
-
99
- this._addButton( 'removeHighlight', t( 'Remove highlight' ), icons.eraser, null, button => {
100
- button.bind( 'isEnabled' ).to( command, 'isEnabled' );
101
- } );
102
- }
103
-
104
- /**
105
- * Creates a toolbar button from the provided highlight option.
106
- *
107
- * @param {module:highlight/highlight~HighlightOption} option
108
- * @private
109
- */
110
- _addHighlighterButton( option ) {
111
- const command = this.editor.commands.get( 'highlight' );
112
-
113
- // TODO: change naming
114
- this._addButton( 'highlight:' + option.model, option.title, getIconForType( option.type ), option.model, decorateHighlightButton );
115
-
116
- function decorateHighlightButton( button ) {
117
- button.bind( 'isEnabled' ).to( command, 'isEnabled' );
118
- button.bind( 'isOn' ).to( command, 'value', value => value === option.model );
119
- button.iconView.fillColor = option.color;
120
- button.isToggleable = true;
121
- }
122
- }
123
-
124
- /**
125
- * Internal method for creating highlight buttons.
126
- *
127
- * @param {String} name The name of the button.
128
- * @param {String} label The label for the button.
129
- * @param {String} icon The button icon.
130
- * @param {*} value The `value` property passed to the executed command.
131
- * @param {Function} decorateButton A callback getting ButtonView instance so that it can be further customized.
132
- * @private
133
- */
134
- _addButton( name, label, icon, value, decorateButton ) {
135
- const editor = this.editor;
136
-
137
- editor.ui.componentFactory.add( name, locale => {
138
- const buttonView = new ButtonView( locale );
139
-
140
- const localized = this.localizedOptionTitles[ label ] ? this.localizedOptionTitles[ label ] : label;
141
-
142
- buttonView.set( {
143
- label: localized,
144
- icon,
145
- tooltip: true
146
- } );
147
-
148
- buttonView.on( 'execute', () => {
149
- editor.execute( 'highlight', { value } );
150
- editor.editing.view.focus();
151
- } );
152
-
153
- // Add additional behavior for buttonView.
154
- decorateButton( buttonView );
155
-
156
- return buttonView;
157
- } );
158
- }
159
-
160
- /**
161
- * Creates the split button dropdown UI from the provided highlight options.
162
- *
163
- * @param {Array.<module:highlight/highlight~HighlightOption>} options
164
- * @private
165
- */
166
- _addDropdown( options ) {
167
- const editor = this.editor;
168
- const t = editor.t;
169
- const componentFactory = editor.ui.componentFactory;
170
-
171
- const startingHighlighter = options[ 0 ];
172
-
173
- const optionsMap = options.reduce( ( retVal, option ) => {
174
- retVal[ option.model ] = option;
175
-
176
- return retVal;
177
- }, {} );
178
-
179
- componentFactory.add( 'highlight', locale => {
180
- const command = editor.commands.get( 'highlight' );
181
- const dropdownView = createDropdown( locale, SplitButtonView );
182
- const splitButtonView = dropdownView.buttonView;
183
-
184
- splitButtonView.set( {
185
- label: t( 'Highlight' ),
186
- tooltip: true,
187
- // Holds last executed highlighter.
188
- lastExecuted: startingHighlighter.model,
189
- // Holds current highlighter to execute (might be different then last used).
190
- commandValue: startingHighlighter.model,
191
- isToggleable: true
192
- } );
193
-
194
- // Dropdown button changes to selection (command.value):
195
- // - If selection is in highlight it get active highlight appearance (icon, color) and is activated.
196
- // - Otherwise it gets appearance (icon, color) of last executed highlight.
197
- splitButtonView.bind( 'icon' ).to( command, 'value', value => getIconForType( getActiveOption( value, 'type' ) ) );
198
- splitButtonView.bind( 'color' ).to( command, 'value', value => getActiveOption( value, 'color' ) );
199
- splitButtonView.bind( 'commandValue' ).to( command, 'value', value => getActiveOption( value, 'model' ) );
200
- splitButtonView.bind( 'isOn' ).to( command, 'value', value => !!value );
201
-
202
- splitButtonView.delegate( 'execute' ).to( dropdownView );
203
-
204
- // Create buttons array.
205
- const buttons = options.map( option => {
206
- // Get existing highlighter button.
207
- const buttonView = componentFactory.create( 'highlight:' + option.model );
208
-
209
- // Update lastExecutedHighlight on execute.
210
- this.listenTo( buttonView, 'execute', () => {
211
- dropdownView.buttonView.set( { lastExecuted: option.model } );
212
- } );
213
-
214
- return buttonView;
215
- } );
216
-
217
- // Make toolbar button enabled when any button in dropdown is enabled before adding separator and eraser.
218
- dropdownView.bind( 'isEnabled' ).toMany( buttons, 'isEnabled', ( ...areEnabled ) => areEnabled.some( isEnabled => isEnabled ) );
219
-
220
- // Add separator and eraser buttons to dropdown.
221
- buttons.push( new ToolbarSeparatorView() );
222
- buttons.push( componentFactory.create( 'removeHighlight' ) );
223
-
224
- addToolbarToDropdown( dropdownView, buttons, { enableActiveItemFocusOnDropdownOpen: true } );
225
- bindToolbarIconStyleToActiveColor( dropdownView );
226
-
227
- dropdownView.toolbarView.ariaLabel = t( 'Text highlight toolbar' );
228
-
229
- // Execute current action from dropdown's split button action button.
230
- splitButtonView.on( 'execute', () => {
231
- editor.execute( 'highlight', { value: splitButtonView.commandValue } );
232
- } );
233
-
234
- // Focus the editable after executing the command.
235
- // It overrides a default behaviour where the focus is moved to the dropdown button (#12125).
236
- this.listenTo( dropdownView, 'execute', () => {
237
- editor.editing.view.focus();
238
- } );
239
-
240
- // Returns active highlighter option depending on current command value.
241
- // If current is not set or it is the same as last execute this method will return the option key (like icon or color)
242
- // of last executed highlighter. Otherwise it will return option key for current one.
243
- function getActiveOption( current, key ) {
244
- const whichHighlighter = !current ||
245
- current === splitButtonView.lastExecuted ? splitButtonView.lastExecuted : current;
246
-
247
- return optionsMap[ whichHighlighter ][ key ];
248
- }
249
-
250
- return dropdownView;
251
- } );
252
- }
32
+ /**
33
+ * Returns the localized option titles provided by the plugin.
34
+ *
35
+ * The following localized titles corresponding with default
36
+ * {@link module:highlight/highlight~HighlightConfig#options} are available:
37
+ *
38
+ * * `'Yellow marker'`,
39
+ * * `'Green marker'`,
40
+ * * `'Pink marker'`,
41
+ * * `'Blue marker'`,
42
+ * * `'Red pen'`,
43
+ * * `'Green pen'`.
44
+ */
45
+ get localizedOptionTitles() {
46
+ const t = this.editor.t;
47
+ return {
48
+ 'Yellow marker': t('Yellow marker'),
49
+ 'Green marker': t('Green marker'),
50
+ 'Pink marker': t('Pink marker'),
51
+ 'Blue marker': t('Blue marker'),
52
+ 'Red pen': t('Red pen'),
53
+ 'Green pen': t('Green pen')
54
+ };
55
+ }
56
+ /**
57
+ * @inheritDoc
58
+ */
59
+ static get pluginName() {
60
+ return 'HighlightUI';
61
+ }
62
+ /**
63
+ * @inheritDoc
64
+ */
65
+ init() {
66
+ const options = this.editor.config.get('highlight.options');
67
+ for (const option of options) {
68
+ this._addHighlighterButton(option);
69
+ }
70
+ this._addRemoveHighlightButton();
71
+ this._addDropdown(options);
72
+ }
73
+ /**
74
+ * Creates the "Remove highlight" button.
75
+ */
76
+ _addRemoveHighlightButton() {
77
+ const t = this.editor.t;
78
+ const command = this.editor.commands.get('highlight');
79
+ this._addButton('removeHighlight', t('Remove highlight'), icons.eraser, null, button => {
80
+ button.bind('isEnabled').to(command, 'isEnabled');
81
+ });
82
+ }
83
+ /**
84
+ * Creates a toolbar button from the provided highlight option.
85
+ */
86
+ _addHighlighterButton(option) {
87
+ const command = this.editor.commands.get('highlight');
88
+ // TODO: change naming
89
+ this._addButton('highlight:' + option.model, option.title, getIconForType(option.type), option.model, decorateHighlightButton);
90
+ function decorateHighlightButton(button) {
91
+ button.bind('isEnabled').to(command, 'isEnabled');
92
+ button.bind('isOn').to(command, 'value', value => value === option.model);
93
+ button.iconView.fillColor = option.color;
94
+ button.isToggleable = true;
95
+ }
96
+ }
97
+ /**
98
+ * Internal method for creating highlight buttons.
99
+ *
100
+ * @param name The name of the button.
101
+ * @param label The label for the button.
102
+ * @param icon The button icon.
103
+ * @param value The `value` property passed to the executed command.
104
+ * @param decorateButton A callback getting ButtonView instance so that it can be further customized.
105
+ */
106
+ _addButton(name, label, icon, value, decorateButton) {
107
+ const editor = this.editor;
108
+ editor.ui.componentFactory.add(name, locale => {
109
+ const buttonView = new ButtonView(locale);
110
+ const localized = this.localizedOptionTitles[label] ? this.localizedOptionTitles[label] : label;
111
+ buttonView.set({
112
+ label: localized,
113
+ icon,
114
+ tooltip: true
115
+ });
116
+ buttonView.on('execute', () => {
117
+ editor.execute('highlight', { value });
118
+ editor.editing.view.focus();
119
+ });
120
+ // Add additional behavior for buttonView.
121
+ decorateButton(buttonView);
122
+ return buttonView;
123
+ });
124
+ }
125
+ /**
126
+ * Creates the split button dropdown UI from the provided highlight options.
127
+ */
128
+ _addDropdown(options) {
129
+ const editor = this.editor;
130
+ const t = editor.t;
131
+ const componentFactory = editor.ui.componentFactory;
132
+ const startingHighlighter = options[0];
133
+ const optionsMap = options.reduce((retVal, option) => {
134
+ retVal[option.model] = option;
135
+ return retVal;
136
+ }, {});
137
+ componentFactory.add('highlight', locale => {
138
+ const command = editor.commands.get('highlight');
139
+ const dropdownView = createDropdown(locale, SplitButtonView);
140
+ const splitButtonView = dropdownView.buttonView;
141
+ splitButtonView.set({
142
+ label: t('Highlight'),
143
+ tooltip: true,
144
+ // Holds last executed highlighter.
145
+ lastExecuted: startingHighlighter.model,
146
+ // Holds current highlighter to execute (might be different then last used).
147
+ commandValue: startingHighlighter.model,
148
+ isToggleable: true
149
+ });
150
+ // Dropdown button changes to selection (command.value):
151
+ // - If selection is in highlight it get active highlight appearance (icon, color) and is activated.
152
+ // - Otherwise it gets appearance (icon, color) of last executed highlight.
153
+ splitButtonView.bind('icon').to(command, 'value', value => getIconForType(getActiveOption(value, 'type')));
154
+ splitButtonView.bind('color').to(command, 'value', value => getActiveOption(value, 'color'));
155
+ splitButtonView.bind('commandValue').to(command, 'value', value => getActiveOption(value, 'model'));
156
+ splitButtonView.bind('isOn').to(command, 'value', value => !!value);
157
+ splitButtonView.delegate('execute').to(dropdownView);
158
+ // Create buttons array.
159
+ const buttonsCreator = () => {
160
+ const buttons = options.map(option => {
161
+ // Get existing highlighter button.
162
+ const buttonView = componentFactory.create('highlight:' + option.model);
163
+ // Update lastExecutedHighlight on execute.
164
+ this.listenTo(buttonView, 'execute', () => {
165
+ dropdownView.buttonView.set({ lastExecuted: option.model });
166
+ });
167
+ return buttonView;
168
+ });
169
+ // Add separator and eraser buttons to dropdown.
170
+ buttons.push(new ToolbarSeparatorView());
171
+ buttons.push(componentFactory.create('removeHighlight'));
172
+ return buttons;
173
+ };
174
+ // Make toolbar button enabled when any button in dropdown is enabled before adding separator and eraser.
175
+ dropdownView.bind('isEnabled').to(command, 'isEnabled');
176
+ addToolbarToDropdown(dropdownView, buttonsCreator, {
177
+ enableActiveItemFocusOnDropdownOpen: true,
178
+ ariaLabel: t('Text highlight toolbar')
179
+ });
180
+ bindToolbarIconStyleToActiveColor(dropdownView);
181
+ // Execute current action from dropdown's split button action button.
182
+ splitButtonView.on('execute', () => {
183
+ editor.execute('highlight', { value: splitButtonView.commandValue });
184
+ });
185
+ // Focus the editable after executing the command.
186
+ // It overrides a default behaviour where the focus is moved to the dropdown button (#12125).
187
+ this.listenTo(dropdownView, 'execute', () => {
188
+ editor.editing.view.focus();
189
+ });
190
+ /**
191
+ * Returns active highlighter option depending on current command value.
192
+ * If current is not set or it is the same as last execute this method will return the option key (like icon or color)
193
+ * of last executed highlighter. Otherwise it will return option key for current one.
194
+ */
195
+ function getActiveOption(current, key) {
196
+ const whichHighlighter = !current ||
197
+ current === splitButtonView.lastExecuted ? splitButtonView.lastExecuted : current;
198
+ return optionsMap[whichHighlighter][key];
199
+ }
200
+ return dropdownView;
201
+ });
202
+ }
253
203
  }
254
-
255
- // Extends split button icon style to reflect last used button style.
256
- function bindToolbarIconStyleToActiveColor( dropdownView ) {
257
- const actionView = dropdownView.buttonView.actionView;
258
-
259
- actionView.iconView.bind( 'fillColor' ).to( dropdownView.buttonView, 'color' );
204
+ /**
205
+ * Extends split button icon style to reflect last used button style.
206
+ */
207
+ function bindToolbarIconStyleToActiveColor(dropdownView) {
208
+ const actionView = dropdownView.buttonView.actionView;
209
+ actionView.iconView.bind('fillColor').to(dropdownView.buttonView, 'color');
260
210
  }
261
-
262
- // Returns icon for given highlighter type.
263
- function getIconForType( type ) {
264
- return type === 'marker' ? markerIcon : penIcon;
211
+ /**
212
+ * Returns icon for given highlighter type.
213
+ */
214
+ function getIconForType(type) {
215
+ return type === 'marker' ? markerIcon : penIcon;
265
216
  }