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