@ckeditor/ckeditor5-special-characters 36.0.1 → 37.0.0-alpha.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.
@@ -2,11 +2,9 @@
2
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 special-characters/specialcharacters
8
7
  */
9
-
10
8
  import { Plugin } from 'ckeditor5/src/core';
11
9
  import { Typing } from 'ckeditor5/src/typing';
12
10
  import { createDropdown } from 'ckeditor5/src/ui';
@@ -15,341 +13,194 @@ import SpecialCharactersNavigationView from './ui/specialcharactersnavigationvie
15
13
  import CharacterGridView from './ui/charactergridview';
16
14
  import CharacterInfoView from './ui/characterinfoview';
17
15
  import SpecialCharactersView from './ui/specialcharactersview';
18
-
19
16
  import specialCharactersIcon from '../theme/icons/specialcharacters.svg';
20
17
  import '../theme/specialcharacters.css';
21
-
22
18
  const ALL_SPECIAL_CHARACTERS_GROUP = 'All';
23
-
24
19
  /**
25
20
  * The special characters feature.
26
21
  *
27
22
  * Introduces the `'specialCharacters'` dropdown.
28
- *
29
- * @extends module:core/plugin~Plugin
30
23
  */
31
24
  export default class SpecialCharacters extends Plugin {
32
- /**
33
- * @inheritDoc
34
- */
35
- static get requires() {
36
- return [ Typing ];
37
- }
38
-
39
- /**
40
- * @inheritDoc
41
- */
42
- static get pluginName() {
43
- return 'SpecialCharacters';
44
- }
45
-
46
- /**
47
- * @inheritDoc
48
- */
49
- constructor( editor ) {
50
- super( editor );
51
-
52
- const t = editor.t;
53
-
54
- /**
55
- * Registered characters. A pair of a character name and its symbol.
56
- *
57
- * @private
58
- * @member {Map.<String, String>} #_characters
59
- */
60
- this._characters = new Map();
61
-
62
- /**
63
- * Registered groups. Each group contains a displayed label and a collection with symbol names.
64
- *
65
- * @private
66
- * @member {Map.<String, {items: Set.<String>, label: String}>} #_groups
67
- */
68
- this._groups = new Map();
69
-
70
- /**
71
- * A label describing the "All" special characters category.
72
- *
73
- * @private
74
- * @member {String} #_allSpecialCharactersGroupLabel
75
- */
76
- this._allSpecialCharactersGroupLabel = t( 'All' );
77
- }
78
-
79
- /**
80
- * @inheritDoc
81
- */
82
- init() {
83
- const editor = this.editor;
84
- const t = editor.t;
85
-
86
- const inputCommand = editor.commands.get( 'input' );
87
-
88
- // Add the `specialCharacters` dropdown button to feature components.
89
- editor.ui.componentFactory.add( 'specialCharacters', locale => {
90
- const dropdownView = createDropdown( locale );
91
- let dropdownPanelContent;
92
-
93
- dropdownView.buttonView.set( {
94
- label: t( 'Special characters' ),
95
- icon: specialCharactersIcon,
96
- tooltip: true
97
- } );
98
-
99
- dropdownView.bind( 'isEnabled' ).to( inputCommand );
100
-
101
- // Insert a special character when a tile was clicked.
102
- dropdownView.on( 'execute', ( evt, data ) => {
103
- editor.execute( 'insertText', { text: data.character } );
104
- editor.editing.view.focus();
105
- } );
106
-
107
- dropdownView.on( 'change:isOpen', () => {
108
- if ( !dropdownPanelContent ) {
109
- dropdownPanelContent = this._createDropdownPanelContent( locale, dropdownView );
110
-
111
- const specialCharactersView = new SpecialCharactersView(
112
- locale,
113
- dropdownPanelContent.navigationView,
114
- dropdownPanelContent.gridView,
115
- dropdownPanelContent.infoView
116
- );
117
-
118
- dropdownView.panelView.children.add( specialCharactersView );
119
- }
120
-
121
- dropdownPanelContent.infoView.set( {
122
- character: null,
123
- name: null
124
- } );
125
- } );
126
-
127
- return dropdownView;
128
- } );
129
- }
130
-
131
- /**
132
- * Adds a collection of special characters to the specified group. The title of a special character must be unique.
133
- *
134
- * **Note:** The "All" category name is reserved by the plugin and cannot be used as a new name for a special
135
- * characters category.
136
- *
137
- * @param {String} groupName
138
- * @param {Array.<module:special-characters/specialcharacters~SpecialCharacterDefinition>} items
139
- * @param {Object} options
140
- * @param {String} [options.label=groupName]
141
- */
142
- addItems( groupName, items, options = { label: groupName } ) {
143
- if ( groupName === ALL_SPECIAL_CHARACTERS_GROUP ) {
144
- /**
145
- * The name "All" for a special category group cannot be used because it is a special category that displays all
146
- * available special characters.
147
- *
148
- * @error special-character-invalid-group-name
149
- */
150
- throw new CKEditorError( 'special-character-invalid-group-name', null );
151
- }
152
-
153
- const group = this._getGroup( groupName, options.label );
154
-
155
- for ( const item of items ) {
156
- group.items.add( item.title );
157
- this._characters.set( item.title, item.character );
158
- }
159
- }
160
-
161
- /**
162
- * Returns special character groups in an order determined based on configuration and registration sequence.
163
- *
164
- * @returns {Iterable.<String>}
165
- */
166
- getGroups() {
167
- const groups = Array.from( this._groups.keys() );
168
- const order = this.editor.config.get( 'specialCharacters.order' ) || [];
169
-
170
- const invalidGroup = order.find( item => !groups.includes( item ) );
171
-
172
- if ( invalidGroup ) {
173
- /**
174
- * One of the special character groups in the "specialCharacters.order" configuration doesn't exist.
175
- *
176
- * @error special-character-invalid-order-group-name
177
- */
178
- throw new CKEditorError( 'special-character-invalid-order-group-name', null, { invalidGroup } );
179
- }
180
-
181
- return new Set( [
182
- ...order,
183
- ...groups
184
- ] );
185
- }
186
-
187
- /**
188
- * Returns a collection of special characters symbol names (titles).
189
- *
190
- * @param {String} groupName
191
- * @returns {Set.<String>|undefined}
192
- */
193
- getCharactersForGroup( groupName ) {
194
- if ( groupName === ALL_SPECIAL_CHARACTERS_GROUP ) {
195
- return new Set( this._characters.keys() );
196
- }
197
-
198
- const group = this._groups.get( groupName );
199
-
200
- if ( group ) {
201
- return group.items;
202
- }
203
- }
204
-
205
- /**
206
- * Returns the symbol of a special character for the specified name. If the special character could not be found, `undefined`
207
- * is returned.
208
- *
209
- * @param {String} title The title of a special character.
210
- * @returns {String|undefined}
211
- */
212
- getCharacter( title ) {
213
- return this._characters.get( title );
214
- }
215
-
216
- /**
217
- * Returns a group of special characters. If the group with the specified name does not exist, it will be created.
218
- *
219
- * @private
220
- * @param {String} groupName The name of the group to create.
221
- * @param {String} label The label describing the new group.
222
- */
223
- _getGroup( groupName, label ) {
224
- if ( !this._groups.has( groupName ) ) {
225
- this._groups.set( groupName, {
226
- items: new Set(),
227
- label
228
- } );
229
- }
230
-
231
- return this._groups.get( groupName );
232
- }
233
-
234
- /**
235
- * Updates the symbol grid depending on the currently selected character group.
236
- *
237
- * @private
238
- * @param {String} currentGroupName
239
- * @param {module:special-characters/ui/charactergridview~CharacterGridView} gridView
240
- */
241
- _updateGrid( currentGroupName, gridView ) {
242
- // Updating the grid starts with removing all tiles belonging to the old group.
243
- gridView.tiles.clear();
244
-
245
- const characterTitles = this.getCharactersForGroup( currentGroupName );
246
-
247
- for ( const title of characterTitles ) {
248
- const character = this.getCharacter( title );
249
-
250
- gridView.tiles.add( gridView.createTile( character, title ) );
251
- }
252
- }
253
-
254
- /**
255
- * Initializes the dropdown, used for lazy loading.
256
- *
257
- * @private
258
- * @param {module:utils/locale~Locale} locale
259
- * @param {module:ui/dropdown/dropdownview~DropdownView} dropdownView
260
- * @returns {Object} Returns an object with `navigationView`, `gridView` and `infoView` properties, containing UI parts.
261
- */
262
- _createDropdownPanelContent( locale, dropdownView ) {
263
- // The map contains a name of category (an identifier) and its label (a translational string).
264
- const specialCharsGroups = new Map( [
265
- // Add a special group that shows all available special characters.
266
- [ ALL_SPECIAL_CHARACTERS_GROUP, this._allSpecialCharactersGroupLabel ],
267
-
268
- ...Array.from( this.getGroups() )
269
- .map( name => ( [ name, this._groups.get( name ).label ] ) )
270
- ] );
271
-
272
- const navigationView = new SpecialCharactersNavigationView( locale, specialCharsGroups );
273
- const gridView = new CharacterGridView( locale );
274
- const infoView = new CharacterInfoView( locale );
275
-
276
- gridView.delegate( 'execute' ).to( dropdownView );
277
-
278
- gridView.on( 'tileHover', ( evt, data ) => {
279
- infoView.set( data );
280
- } );
281
-
282
- gridView.on( 'tileFocus', ( evt, data ) => {
283
- infoView.set( data );
284
- } );
285
-
286
- // Update the grid of special characters when a user changed the character group.
287
- navigationView.on( 'execute', () => {
288
- this._updateGrid( navigationView.currentGroupName, gridView );
289
- } );
290
-
291
- // Set the initial content of the special characters grid.
292
- this._updateGrid( navigationView.currentGroupName, gridView );
293
-
294
- return { navigationView, gridView, infoView };
295
- }
25
+ /**
26
+ * @inheritDoc
27
+ */
28
+ static get requires() {
29
+ return [Typing];
30
+ }
31
+ /**
32
+ * @inheritDoc
33
+ */
34
+ static get pluginName() {
35
+ return 'SpecialCharacters';
36
+ }
37
+ /**
38
+ * @inheritDoc
39
+ */
40
+ constructor(editor) {
41
+ super(editor);
42
+ const t = editor.t;
43
+ this._characters = new Map();
44
+ this._groups = new Map();
45
+ this._allSpecialCharactersGroupLabel = t('All');
46
+ }
47
+ /**
48
+ * @inheritDoc
49
+ */
50
+ init() {
51
+ const editor = this.editor;
52
+ const t = editor.t;
53
+ const inputCommand = editor.commands.get('insertText');
54
+ // Add the `specialCharacters` dropdown button to feature components.
55
+ editor.ui.componentFactory.add('specialCharacters', locale => {
56
+ const dropdownView = createDropdown(locale);
57
+ let dropdownPanelContent;
58
+ dropdownView.buttonView.set({
59
+ label: t('Special characters'),
60
+ icon: specialCharactersIcon,
61
+ tooltip: true
62
+ });
63
+ dropdownView.bind('isEnabled').to(inputCommand);
64
+ // Insert a special character when a tile was clicked.
65
+ dropdownView.on('execute', (evt, data) => {
66
+ editor.execute('insertText', { text: data.character });
67
+ editor.editing.view.focus();
68
+ });
69
+ dropdownView.on('change:isOpen', () => {
70
+ if (!dropdownPanelContent) {
71
+ dropdownPanelContent = this._createDropdownPanelContent(locale, dropdownView);
72
+ const specialCharactersView = new SpecialCharactersView(locale, dropdownPanelContent.navigationView, dropdownPanelContent.gridView, dropdownPanelContent.infoView);
73
+ dropdownView.panelView.children.add(specialCharactersView);
74
+ }
75
+ dropdownPanelContent.infoView.set({
76
+ character: null,
77
+ name: null
78
+ });
79
+ });
80
+ return dropdownView;
81
+ });
82
+ }
83
+ /**
84
+ * Adds a collection of special characters to the specified group. The title of a special character must be unique.
85
+ *
86
+ * **Note:** The "All" category name is reserved by the plugin and cannot be used as a new name for a special
87
+ * characters category.
88
+ */
89
+ addItems(groupName, items, options = { label: groupName }) {
90
+ if (groupName === ALL_SPECIAL_CHARACTERS_GROUP) {
91
+ /**
92
+ * The name "All" for a special category group cannot be used because it is a special category that displays all
93
+ * available special characters.
94
+ *
95
+ * @error special-character-invalid-group-name
96
+ */
97
+ throw new CKEditorError('special-character-invalid-group-name', null);
98
+ }
99
+ const group = this._getGroup(groupName, options.label);
100
+ for (const item of items) {
101
+ group.items.add(item.title);
102
+ this._characters.set(item.title, item.character);
103
+ }
104
+ }
105
+ /**
106
+ * Returns special character groups in an order determined based on configuration and registration sequence.
107
+ */
108
+ getGroups() {
109
+ const groups = Array.from(this._groups.keys());
110
+ const order = this.editor.config.get('specialCharacters.order') || [];
111
+ const invalidGroup = order.find(item => !groups.includes(item));
112
+ if (invalidGroup) {
113
+ /**
114
+ * One of the special character groups in the "specialCharacters.order" configuration doesn't exist.
115
+ *
116
+ * @error special-character-invalid-order-group-name
117
+ */
118
+ throw new CKEditorError('special-character-invalid-order-group-name', null, { invalidGroup });
119
+ }
120
+ return new Set([
121
+ ...order,
122
+ ...groups
123
+ ]);
124
+ }
125
+ /**
126
+ * Returns a collection of special characters symbol names (titles).
127
+ */
128
+ getCharactersForGroup(groupName) {
129
+ if (groupName === ALL_SPECIAL_CHARACTERS_GROUP) {
130
+ return new Set(this._characters.keys());
131
+ }
132
+ const group = this._groups.get(groupName);
133
+ if (group) {
134
+ return group.items;
135
+ }
136
+ }
137
+ /**
138
+ * Returns the symbol of a special character for the specified name. If the special character could not be found, `undefined`
139
+ * is returned.
140
+ *
141
+ * @param title The title of a special character.
142
+ */
143
+ getCharacter(title) {
144
+ return this._characters.get(title);
145
+ }
146
+ /**
147
+ * Returns a group of special characters. If the group with the specified name does not exist, it will be created.
148
+ *
149
+ * @param groupName The name of the group to create.
150
+ * @param label The label describing the new group.
151
+ */
152
+ _getGroup(groupName, label) {
153
+ if (!this._groups.has(groupName)) {
154
+ this._groups.set(groupName, {
155
+ items: new Set(),
156
+ label
157
+ });
158
+ }
159
+ return this._groups.get(groupName);
160
+ }
161
+ /**
162
+ * Updates the symbol grid depending on the currently selected character group.
163
+ */
164
+ _updateGrid(currentGroupName, gridView) {
165
+ // Updating the grid starts with removing all tiles belonging to the old group.
166
+ gridView.tiles.clear();
167
+ const characterTitles = this.getCharactersForGroup(currentGroupName);
168
+ for (const title of characterTitles) {
169
+ const character = this.getCharacter(title);
170
+ gridView.tiles.add(gridView.createTile(character, title));
171
+ }
172
+ }
173
+ /**
174
+ * Initializes the dropdown, used for lazy loading.
175
+ *
176
+ * @returns An object with `navigationView`, `gridView` and `infoView` properties, containing UI parts.
177
+ */
178
+ _createDropdownPanelContent(locale, dropdownView) {
179
+ const groupEntries = Array
180
+ .from(this.getGroups())
181
+ .map(name => ([name, this._groups.get(name).label]));
182
+ // The map contains a name of category (an identifier) and its label (a translational string).
183
+ const specialCharsGroups = new Map([
184
+ // Add a special group that shows all available special characters.
185
+ [ALL_SPECIAL_CHARACTERS_GROUP, this._allSpecialCharactersGroupLabel],
186
+ ...groupEntries
187
+ ]);
188
+ const navigationView = new SpecialCharactersNavigationView(locale, specialCharsGroups);
189
+ const gridView = new CharacterGridView(locale);
190
+ const infoView = new CharacterInfoView(locale);
191
+ gridView.delegate('execute').to(dropdownView);
192
+ gridView.on('tileHover', (evt, data) => {
193
+ infoView.set(data);
194
+ });
195
+ gridView.on('tileFocus', (evt, data) => {
196
+ infoView.set(data);
197
+ });
198
+ // Update the grid of special characters when a user changed the character group.
199
+ navigationView.on('execute', () => {
200
+ this._updateGrid(navigationView.currentGroupName, gridView);
201
+ });
202
+ // Set the initial content of the special characters grid.
203
+ this._updateGrid(navigationView.currentGroupName, gridView);
204
+ return { navigationView, gridView, infoView };
205
+ }
296
206
  }
297
-
298
- /**
299
- * @typedef {Object} module:special-characters/specialcharacters~SpecialCharacterDefinition
300
- *
301
- * @property {String} title A unique name of the character (e.g. "greek small letter epsilon").
302
- * @property {String} character A human-readable character displayed as the label (e.g. "ε").
303
- */
304
-
305
- /**
306
- * The configuration of the {@link module:special-characters/specialcharacters~SpecialCharacters} feature.
307
- *
308
- * Read more in {@link module:special-characters/specialcharacters~SpecialCharactersConfig}.
309
- *
310
- * @member {module:special-characters/specialcharacters~SpecialCharactersConfig}
311
- * module:core/editor/editorconfig~EditorConfig#specialCharacters
312
- */
313
-
314
- /**
315
- * The configuration of the special characters feature.
316
- *
317
- * Read more about {@glink features/special-characters#configuration configuring the special characters feature}.
318
- *
319
- * ClassicEditor
320
- * .create( editorElement, {
321
- * specialCharacters: ... // Special characters feature options.
322
- * } )
323
- * .then( ... )
324
- * .catch( ... );
325
- *
326
- * See {@link module:core/editor/editorconfig~EditorConfig all editor configuration options}.
327
- *
328
- * @interface SpecialCharactersConfig
329
- */
330
-
331
- /**
332
- * The configuration of the special characters category order.
333
- *
334
- * Special characters categories are displayed in the UI in the order in which they were registered. Using the `order` property
335
- * allows to override this behaviour and enforce specific order. Categories not listed in the `order` property will be displayed
336
- * in the default order below categories listed in the configuration.
337
- *
338
- * ClassicEditor
339
- * .create( editorElement, {
340
- * plugins: [ SpecialCharacters, SpecialCharactersEssentials, ... ],
341
- * specialCharacters: {
342
- * order: [
343
- * 'Text',
344
- * 'Latin',
345
- * 'Mathematical',
346
- * 'Currency',
347
- * 'Arrows'
348
- * ]
349
- * }
350
- * } )
351
- * .then( ... )
352
- * .catch( ... );
353
- *
354
- * @member {Array.<String>} module:special-characters/specialcharacters~SpecialCharactersConfig#order
355
- */
@@ -0,0 +1,30 @@
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 special-characters/specialcharactersarrows
7
+ */
8
+ import { Plugin } from 'ckeditor5/src/core';
9
+ /**
10
+ * A plugin that provides special characters for the "Arrows" category.
11
+ *
12
+ * ```ts
13
+ * ClassicEditor
14
+ * .create( {
15
+ * plugins: [ ..., SpecialCharacters, SpecialCharactersArrows ],
16
+ * } )
17
+ * .then( ... )
18
+ * .catch( ... );
19
+ * ```
20
+ */
21
+ export default class SpecialCharactersArrows extends Plugin {
22
+ /**
23
+ * @inheritDoc
24
+ */
25
+ static get pluginName(): 'SpecialCharactersArrows';
26
+ /**
27
+ * @inheritDoc
28
+ */
29
+ init(): void;
30
+ }