@ckeditor/ckeditor5-highlight 38.1.0 → 38.1.1

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,216 +1,216 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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 highlight/highlightui
7
- */
8
- import { Plugin, icons } from 'ckeditor5/src/core';
9
- import { ButtonView, SplitButtonView, ToolbarSeparatorView, createDropdown, addToolbarToDropdown } from 'ckeditor5/src/ui';
10
- import markerIcon from './../theme/icons/marker.svg';
11
- import penIcon from './../theme/icons/pen.svg';
12
- import './../theme/highlight.css';
13
- /**
14
- * The default highlight UI plugin. It introduces:
15
- *
16
- * * The `'highlight'` dropdown,
17
- * * The `'removeHighlight'` and `'highlight:*'` buttons.
18
- *
19
- * The default configuration includes the following buttons:
20
- *
21
- * * `'highlight:yellowMarker'`
22
- * * `'highlight:greenMarker'`
23
- * * `'highlight:pinkMarker'`
24
- * * `'highlight:blueMarker'`
25
- * * `'highlight:redPen'`
26
- * * `'highlight:greenPen'`
27
- *
28
- * See the {@link module:highlight/highlightconfig~HighlightConfig#options configuration} to learn more
29
- * about the defaults.
30
- */
31
- export default class HighlightUI extends Plugin {
32
- /**
33
- * Returns the localized option titles provided by the plugin.
34
- *
35
- * The following localized titles corresponding with default
36
- * {@link module:highlight/highlightconfig~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
- }
203
- }
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');
210
- }
211
- /**
212
- * Returns icon for given highlighter type.
213
- */
214
- function getIconForType(type) {
215
- return type === 'marker' ? markerIcon : penIcon;
216
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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 highlight/highlightui
7
+ */
8
+ import { Plugin, icons } from 'ckeditor5/src/core';
9
+ import { ButtonView, SplitButtonView, ToolbarSeparatorView, createDropdown, addToolbarToDropdown } from 'ckeditor5/src/ui';
10
+ import markerIcon from './../theme/icons/marker.svg';
11
+ import penIcon from './../theme/icons/pen.svg';
12
+ import './../theme/highlight.css';
13
+ /**
14
+ * The default highlight UI plugin. It introduces:
15
+ *
16
+ * * The `'highlight'` dropdown,
17
+ * * The `'removeHighlight'` and `'highlight:*'` buttons.
18
+ *
19
+ * The default configuration includes the following buttons:
20
+ *
21
+ * * `'highlight:yellowMarker'`
22
+ * * `'highlight:greenMarker'`
23
+ * * `'highlight:pinkMarker'`
24
+ * * `'highlight:blueMarker'`
25
+ * * `'highlight:redPen'`
26
+ * * `'highlight:greenPen'`
27
+ *
28
+ * See the {@link module:highlight/highlightconfig~HighlightConfig#options configuration} to learn more
29
+ * about the defaults.
30
+ */
31
+ export default class HighlightUI extends Plugin {
32
+ /**
33
+ * Returns the localized option titles provided by the plugin.
34
+ *
35
+ * The following localized titles corresponding with default
36
+ * {@link module:highlight/highlightconfig~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
+ }
203
+ }
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');
210
+ }
211
+ /**
212
+ * Returns icon for given highlighter type.
213
+ */
214
+ function getIconForType(type) {
215
+ return type === 'marker' ? markerIcon : penIcon;
216
+ }
package/src/index.d.ts CHANGED
@@ -1,13 +1,13 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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 highlight
7
- */
8
- export { default as Highlight } from './highlight';
9
- export { default as HighlightEditing } from './highlightediting';
10
- export { default as HighlightUI } from './highlightui';
11
- export type { default as HighlightCommand } from './highlightcommand';
12
- export type { HighlightOption, HighlightConfig } from './highlightconfig';
13
- import './augmentation';
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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 highlight
7
+ */
8
+ export { default as Highlight } from './highlight';
9
+ export { default as HighlightEditing } from './highlightediting';
10
+ export { default as HighlightUI } from './highlightui';
11
+ export type { default as HighlightCommand } from './highlightcommand';
12
+ export type { HighlightOption, HighlightConfig } from './highlightconfig';
13
+ import './augmentation';
package/src/index.js CHANGED
@@ -1,11 +1,11 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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 highlight
7
- */
8
- export { default as Highlight } from './highlight';
9
- export { default as HighlightEditing } from './highlightediting';
10
- export { default as HighlightUI } from './highlightui';
11
- import './augmentation';
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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 highlight
7
+ */
8
+ export { default as Highlight } from './highlight';
9
+ export { default as HighlightEditing } from './highlightediting';
10
+ export { default as HighlightUI } from './highlightui';
11
+ import './augmentation';