@ckeditor/ckeditor5-highlight 35.4.0 → 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.
- package/LICENSE.md +1 -1
- package/build/highlight.js +2 -2
- package/package.json +21 -16
- package/src/highlight.js +13 -174
- package/src/highlight.ts +225 -0
- package/src/highlightcommand.js +73 -93
- package/src/highlightcommand.ts +113 -0
- package/src/highlightediting.js +92 -107
- package/src/highlightediting.ts +139 -0
- package/src/highlightui.js +183 -232
- package/src/highlightui.ts +282 -0
- package/src/index.js +1 -3
- package/src/index.ts +12 -0
- package/theme/highlight.css +1 -1
package/src/highlightui.js
CHANGED
@@ -1,20 +1,15 @@
|
|
1
1
|
/**
|
2
|
-
* @license Copyright (c) 2003-
|
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
|
-
|
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
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
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
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
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
|
-
|
263
|
-
|
264
|
-
|
211
|
+
/**
|
212
|
+
* Returns icon for given highlighter type.
|
213
|
+
*/
|
214
|
+
function getIconForType(type) {
|
215
|
+
return type === 'marker' ? markerIcon : penIcon;
|
265
216
|
}
|