@ckeditor/ckeditor5-font 41.2.0 → 41.3.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.
Files changed (32) hide show
  1. package/dist/content-index.css +16 -0
  2. package/dist/editor-index.css +4 -0
  3. package/dist/index.css +27 -0
  4. package/dist/index.css.map +1 -0
  5. package/dist/index.js +1640 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/types/augmentation.d.ts +58 -0
  8. package/dist/types/font.d.ts +33 -0
  9. package/dist/types/fontbackgroundcolor/fontbackgroundcolorcommand.d.ts +26 -0
  10. package/dist/types/fontbackgroundcolor/fontbackgroundcolorediting.d.ts +26 -0
  11. package/dist/types/fontbackgroundcolor/fontbackgroundcolorui.d.ts +22 -0
  12. package/dist/types/fontbackgroundcolor.d.ts +30 -0
  13. package/dist/types/fontcolor/fontcolorcommand.d.ts +25 -0
  14. package/dist/types/fontcolor/fontcolorediting.d.ts +26 -0
  15. package/dist/types/fontcolor/fontcolorui.d.ts +22 -0
  16. package/dist/types/fontcolor.d.ts +29 -0
  17. package/dist/types/fontcommand.d.ts +48 -0
  18. package/dist/types/fontconfig.d.ts +373 -0
  19. package/dist/types/fontfamily/fontfamilycommand.d.ts +25 -0
  20. package/dist/types/fontfamily/fontfamilyediting.d.ts +39 -0
  21. package/dist/types/fontfamily/fontfamilyui.d.ts +30 -0
  22. package/dist/types/fontfamily/utils.d.ts +15 -0
  23. package/dist/types/fontfamily.d.ts +29 -0
  24. package/dist/types/fontsize/fontsizecommand.d.ts +25 -0
  25. package/dist/types/fontsize/fontsizeediting.d.ts +44 -0
  26. package/dist/types/fontsize/fontsizeui.d.ts +31 -0
  27. package/dist/types/fontsize/utils.d.ts +12 -0
  28. package/dist/types/fontsize.d.ts +37 -0
  29. package/dist/types/index.d.ts +27 -0
  30. package/dist/types/ui/colorui.d.ts +63 -0
  31. package/dist/types/utils.d.ts +80 -0
  32. package/package.json +3 -2
package/dist/index.js ADDED
@@ -0,0 +1,1640 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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
+ import { Command, Plugin } from '@ckeditor/ckeditor5-core/dist/index.js';
6
+ import { ColorSelectorView, createDropdown, addListToDropdown, ViewModel, normalizeColorOptions, getLocalizedColorOptions, focusChildOnDropdownOpen } from '@ckeditor/ckeditor5-ui/dist/index.js';
7
+ import { Collection, CKEditorError } from '@ckeditor/ckeditor5-utils/dist/index.js';
8
+ import { isLength, isPercentage, addBackgroundRules } from '@ckeditor/ckeditor5-engine/dist/index.js';
9
+
10
+ /**
11
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
12
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
13
+ */
14
+ /**
15
+ * @module font/fontcommand
16
+ */
17
+ /**
18
+ * The base font command.
19
+ */
20
+ class FontCommand extends Command {
21
+ /**
22
+ * Creates an instance of the command.
23
+ *
24
+ * @param editor Editor instance.
25
+ * @param attributeKey The name of a model attribute on which this command operates.
26
+ */
27
+ constructor(editor, attributeKey) {
28
+ super(editor);
29
+ this.attributeKey = attributeKey;
30
+ }
31
+ /**
32
+ * @inheritDoc
33
+ */
34
+ refresh() {
35
+ const model = this.editor.model;
36
+ const doc = model.document;
37
+ this.value = doc.selection.getAttribute(this.attributeKey);
38
+ this.isEnabled = model.schema.checkAttributeInSelection(doc.selection, this.attributeKey);
39
+ }
40
+ /**
41
+ * Executes the command. Applies the `value` of the {@link #attributeKey} to the selection.
42
+ * If no `value` is passed, it removes the attribute from the selection.
43
+ *
44
+ * @param options Options for the executed command.
45
+ * @param options.value The value to apply.
46
+ * @fires execute
47
+ */
48
+ execute(options = {}) {
49
+ const model = this.editor.model;
50
+ const document = model.document;
51
+ const selection = document.selection;
52
+ const value = options.value;
53
+ const batch = options.batch;
54
+ const updateAttribute = (writer) => {
55
+ if (selection.isCollapsed) {
56
+ if (value) {
57
+ writer.setSelectionAttribute(this.attributeKey, value);
58
+ }
59
+ else {
60
+ writer.removeSelectionAttribute(this.attributeKey);
61
+ }
62
+ }
63
+ else {
64
+ const ranges = model.schema.getValidRanges(selection.getRanges(), this.attributeKey);
65
+ for (const range of ranges) {
66
+ if (value) {
67
+ writer.setAttribute(this.attributeKey, value, range);
68
+ }
69
+ else {
70
+ writer.removeAttribute(this.attributeKey, range);
71
+ }
72
+ }
73
+ }
74
+ };
75
+ // In some scenarios, you may want to use a single undo step for multiple changes (e.g. in color picker).
76
+ if (batch) {
77
+ model.enqueueChange(batch, writer => {
78
+ updateAttribute(writer);
79
+ });
80
+ }
81
+ else {
82
+ model.change(writer => {
83
+ updateAttribute(writer);
84
+ });
85
+ }
86
+ }
87
+ }
88
+
89
+ /**
90
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
91
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
92
+ */
93
+ /**
94
+ * The name of the font size plugin.
95
+ */
96
+ const FONT_SIZE = 'fontSize';
97
+ /**
98
+ * The name of the font family plugin.
99
+ */
100
+ const FONT_FAMILY = 'fontFamily';
101
+ /**
102
+ * The name of the font color plugin.
103
+ */
104
+ const FONT_COLOR = 'fontColor';
105
+ /**
106
+ * The name of the font background color plugin.
107
+ */
108
+ const FONT_BACKGROUND_COLOR = 'fontBackgroundColor';
109
+ /**
110
+ * Builds a proper converter definition out of input data.
111
+ */
112
+ function buildDefinition(modelAttributeKey, options) {
113
+ const definition = {
114
+ model: {
115
+ key: modelAttributeKey,
116
+ values: []
117
+ },
118
+ view: {},
119
+ upcastAlso: {}
120
+ };
121
+ for (const option of options) {
122
+ definition.model.values.push(option.model);
123
+ definition.view[option.model] = option.view;
124
+ if (option.upcastAlso) {
125
+ definition.upcastAlso[option.model] = option.upcastAlso;
126
+ }
127
+ }
128
+ return definition;
129
+ }
130
+ /**
131
+ * A {@link module:font/fontcolor~FontColor font color} and
132
+ * {@link module:font/fontbackgroundcolor~FontBackgroundColor font background color} helper
133
+ * responsible for upcasting data to the model.
134
+ *
135
+ * **Note**: The `styleAttr` parameter should be either `'color'` or `'background-color'`.
136
+ */
137
+ function renderUpcastAttribute(styleAttr) {
138
+ return (viewElement) => normalizeColorCode(viewElement.getStyle(styleAttr));
139
+ }
140
+ /**
141
+ * A {@link module:font/fontcolor~FontColor font color} and
142
+ * {@link module:font/fontbackgroundcolor~FontBackgroundColor font background color} helper
143
+ * responsible for downcasting a color attribute to a `<span>` element.
144
+ *
145
+ * **Note**: The `styleAttr` parameter should be either `'color'` or `'background-color'`.
146
+ */
147
+ function renderDowncastElement(styleAttr) {
148
+ return (modelAttributeValue, { writer }) => writer.createAttributeElement('span', {
149
+ style: `${styleAttr}:${modelAttributeValue}`
150
+ }, { priority: 7 });
151
+ }
152
+ /**
153
+ * A helper that adds {@link module:ui/colorselector/colorselectorview~ColorSelectorView} to the color dropdown with proper initial values.
154
+ *
155
+ * @param config.dropdownView The dropdown view to which a {@link module:ui/colorselector/colorselectorview~ColorSelectorView}
156
+ * will be added.
157
+ * @param config.colors An array with definitions representing colors to be displayed in the color selector.
158
+ * @param config.removeButtonLabel The label for the button responsible for removing the color.
159
+ * @param config.documentColorsLabel The label for the section with document colors.
160
+ * @param config.documentColorsCount The number of document colors inside the dropdown.
161
+ * @param config.colorPickerViewConfig Configuration of the color picker view.
162
+ * @returns The new color selector view.
163
+ */
164
+ function addColorSelectorToDropdown({ dropdownView, colors, columns, removeButtonLabel, colorPickerLabel, documentColorsLabel, documentColorsCount, colorPickerViewConfig }) {
165
+ const locale = dropdownView.locale;
166
+ const colorSelectorView = new ColorSelectorView(locale, {
167
+ colors,
168
+ columns,
169
+ removeButtonLabel,
170
+ colorPickerLabel,
171
+ documentColorsLabel,
172
+ documentColorsCount,
173
+ colorPickerViewConfig
174
+ });
175
+ dropdownView.colorSelectorView = colorSelectorView;
176
+ dropdownView.panelView.children.add(colorSelectorView);
177
+ return colorSelectorView;
178
+ }
179
+ /**
180
+ * Fixes the color value string.
181
+ */
182
+ function normalizeColorCode(value) {
183
+ return value.replace(/\s/g, '');
184
+ }
185
+
186
+ /**
187
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
188
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
189
+ */
190
+ /**
191
+ * The font family command. It is used by {@link module:font/fontfamily/fontfamilyediting~FontFamilyEditing}
192
+ * to apply the font family.
193
+ *
194
+ * ```ts
195
+ * editor.execute( 'fontFamily', { value: 'Arial' } );
196
+ * ```
197
+ *
198
+ * **Note**: Executing the command without the value removes the attribute from the model.
199
+ */
200
+ class FontFamilyCommand extends FontCommand {
201
+ /**
202
+ * @inheritDoc
203
+ */
204
+ constructor(editor) {
205
+ super(editor, FONT_FAMILY);
206
+ }
207
+ }
208
+
209
+ /**
210
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
211
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
212
+ */
213
+ /**
214
+ * @module font/fontfamily/utils
215
+ */
216
+ /**
217
+ * Normalizes the {@link module:font/fontconfig~FontFamilyConfig#options configuration options}
218
+ * to the {@link module:font/fontconfig~FontFamilyOption} format.
219
+ *
220
+ * @param configuredOptions An array of options taken from the configuration.
221
+ */
222
+ function normalizeOptions$1(configuredOptions) {
223
+ // Convert options to objects.
224
+ return configuredOptions
225
+ .map(getOptionDefinition$1)
226
+ // Filter out undefined values that `getOptionDefinition` might return.
227
+ .filter(option => option !== undefined);
228
+ }
229
+ /**
230
+ * Returns an option definition either created from string shortcut.
231
+ * If object is passed then this method will return it without alternating it. Returns undefined for item than cannot be parsed.
232
+ *
233
+ */
234
+ function getOptionDefinition$1(option) {
235
+ // Treat any object as full item definition provided by user in configuration.
236
+ if (typeof option === 'object') {
237
+ return option;
238
+ }
239
+ // Handle 'default' string as a special case. It will be used to remove the fontFamily attribute.
240
+ if (option === 'default') {
241
+ return {
242
+ title: 'Default',
243
+ model: undefined
244
+ };
245
+ }
246
+ // Ignore values that we cannot parse to a definition.
247
+ if (typeof option !== 'string') {
248
+ return undefined;
249
+ }
250
+ // Return font family definition from font string.
251
+ return generateFontPreset(option);
252
+ }
253
+ /**
254
+ * Creates a predefined preset for pixel size. It deconstructs font-family like string into full configuration option.
255
+ * A font definition is passed as coma delimited set of font family names. Font names might be quoted.
256
+ *
257
+ * @param fontDefinition A font definition form configuration.
258
+ */
259
+ function generateFontPreset(fontDefinition) {
260
+ // Remove quotes from font names. They will be normalized later.
261
+ const fontNames = fontDefinition.replace(/"|'/g, '').split(',');
262
+ // The first matched font name will be used as dropdown list item title and as model value.
263
+ const firstFontName = fontNames[0];
264
+ // CSS-compatible font names.
265
+ const cssFontNames = fontNames.map(normalizeFontNameForCSS).join(', ');
266
+ return {
267
+ title: firstFontName,
268
+ model: cssFontNames,
269
+ view: {
270
+ name: 'span',
271
+ styles: {
272
+ 'font-family': cssFontNames
273
+ },
274
+ priority: 7
275
+ }
276
+ };
277
+ }
278
+ /**
279
+ * Normalizes font name for the style attribute. It adds braces (') if font name contains spaces.
280
+ */
281
+ function normalizeFontNameForCSS(fontName) {
282
+ fontName = fontName.trim();
283
+ // Compound font names should be quoted.
284
+ if (fontName.indexOf(' ') > 0) {
285
+ fontName = `'${fontName}'`;
286
+ }
287
+ return fontName;
288
+ }
289
+
290
+ /**
291
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
292
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
293
+ */
294
+ /**
295
+ * @module font/fontfamily/fontfamilyediting
296
+ */
297
+ /**
298
+ * The font family editing feature.
299
+ *
300
+ * It introduces the {@link module:font/fontfamily/fontfamilycommand~FontFamilyCommand command} and
301
+ * the `fontFamily` attribute in the {@link module:engine/model/model~Model model} which renders
302
+ * in the {@link module:engine/view/view view} as an inline `<span>` element (`<span style="font-family: Arial">`),
303
+ * depending on the {@link module:font/fontconfig~FontFamilyConfig configuration}.
304
+ */
305
+ class FontFamilyEditing extends Plugin {
306
+ /**
307
+ * @inheritDoc
308
+ */
309
+ static get pluginName() {
310
+ return 'FontFamilyEditing';
311
+ }
312
+ /**
313
+ * @inheritDoc
314
+ */
315
+ constructor(editor) {
316
+ super(editor);
317
+ // Define default configuration using font families shortcuts.
318
+ editor.config.define(FONT_FAMILY, {
319
+ options: [
320
+ 'default',
321
+ 'Arial, Helvetica, sans-serif',
322
+ 'Courier New, Courier, monospace',
323
+ 'Georgia, serif',
324
+ 'Lucida Sans Unicode, Lucida Grande, sans-serif',
325
+ 'Tahoma, Geneva, sans-serif',
326
+ 'Times New Roman, Times, serif',
327
+ 'Trebuchet MS, Helvetica, sans-serif',
328
+ 'Verdana, Geneva, sans-serif'
329
+ ],
330
+ supportAllValues: false
331
+ });
332
+ }
333
+ /**
334
+ * @inheritDoc
335
+ */
336
+ init() {
337
+ const editor = this.editor;
338
+ // Allow fontFamily attribute on text nodes.
339
+ editor.model.schema.extend('$text', { allowAttributes: FONT_FAMILY });
340
+ editor.model.schema.setAttributeProperties(FONT_FAMILY, {
341
+ isFormatting: true,
342
+ copyOnEnter: true
343
+ });
344
+ // Get configured font family options without "default" option.
345
+ const options = normalizeOptions$1(editor.config.get('fontFamily.options')).filter(item => item.model);
346
+ const definition = buildDefinition(FONT_FAMILY, options);
347
+ // Set-up the two-way conversion.
348
+ if (editor.config.get('fontFamily.supportAllValues')) {
349
+ this._prepareAnyValueConverters();
350
+ this._prepareCompatibilityConverter();
351
+ }
352
+ else {
353
+ editor.conversion.attributeToElement(definition);
354
+ }
355
+ editor.commands.add(FONT_FAMILY, new FontFamilyCommand(editor));
356
+ }
357
+ /**
358
+ * These converters enable keeping any value found as `style="font-family: *"` as a value of an attribute on a text even
359
+ * if it is not defined in the plugin configuration.
360
+ */
361
+ _prepareAnyValueConverters() {
362
+ const editor = this.editor;
363
+ editor.conversion.for('downcast').attributeToElement({
364
+ model: FONT_FAMILY,
365
+ view: (attributeValue, { writer }) => {
366
+ return writer.createAttributeElement('span', { style: 'font-family:' + attributeValue }, { priority: 7 });
367
+ }
368
+ });
369
+ editor.conversion.for('upcast').elementToAttribute({
370
+ model: {
371
+ key: FONT_FAMILY,
372
+ value: (viewElement) => viewElement.getStyle('font-family')
373
+ },
374
+ view: {
375
+ name: 'span',
376
+ styles: {
377
+ 'font-family': /.*/
378
+ }
379
+ }
380
+ });
381
+ }
382
+ /**
383
+ * Adds support for legacy `<font face="..">` formatting.
384
+ */
385
+ _prepareCompatibilityConverter() {
386
+ const editor = this.editor;
387
+ editor.conversion.for('upcast').elementToAttribute({
388
+ view: {
389
+ name: 'font',
390
+ attributes: {
391
+ 'face': /.*/
392
+ }
393
+ },
394
+ model: {
395
+ key: FONT_FAMILY,
396
+ value: (viewElement) => viewElement.getAttribute('face')
397
+ }
398
+ });
399
+ }
400
+ }
401
+
402
+ var fontFamilyIcon = "<svg viewBox=\"0 0 20 20\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M11.03 3h6.149a.75.75 0 1 1 0 1.5h-5.514L11.03 3zm1.27 3h4.879a.75.75 0 1 1 0 1.5h-4.244L12.3 6zm1.27 3h3.609a.75.75 0 1 1 0 1.5h-2.973L13.57 9zm-2.754 2.5L8.038 4.785 5.261 11.5h5.555zm.62 1.5H4.641l-1.666 4.028H1.312l5.789-14h1.875l5.789 14h-1.663L11.436 13z\"/></svg>";
403
+
404
+ /**
405
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
406
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
407
+ */
408
+ /**
409
+ * @module font/fontfamily/fontfamilyui
410
+ */
411
+ /**
412
+ * The font family UI plugin. It introduces the `'fontFamily'` dropdown.
413
+ */
414
+ class FontFamilyUI extends Plugin {
415
+ /**
416
+ * @inheritDoc
417
+ */
418
+ static get pluginName() {
419
+ return 'FontFamilyUI';
420
+ }
421
+ /**
422
+ * @inheritDoc
423
+ */
424
+ init() {
425
+ const editor = this.editor;
426
+ const t = editor.t;
427
+ const options = this._getLocalizedOptions();
428
+ const command = editor.commands.get(FONT_FAMILY);
429
+ const accessibleLabel = t('Font Family');
430
+ // Register UI component.
431
+ editor.ui.componentFactory.add(FONT_FAMILY, locale => {
432
+ const dropdownView = createDropdown(locale);
433
+ addListToDropdown(dropdownView, () => _prepareListOptions$1(options, command), {
434
+ role: 'menu',
435
+ ariaLabel: accessibleLabel
436
+ });
437
+ dropdownView.buttonView.set({
438
+ label: accessibleLabel,
439
+ icon: fontFamilyIcon,
440
+ tooltip: true
441
+ });
442
+ dropdownView.extendTemplate({
443
+ attributes: {
444
+ class: 'ck-font-family-dropdown'
445
+ }
446
+ });
447
+ dropdownView.bind('isEnabled').to(command);
448
+ // Execute command when an item from the dropdown is selected.
449
+ this.listenTo(dropdownView, 'execute', evt => {
450
+ editor.execute(evt.source.commandName, { value: evt.source.commandParam });
451
+ editor.editing.view.focus();
452
+ });
453
+ return dropdownView;
454
+ });
455
+ }
456
+ /**
457
+ * Returns options as defined in `config.fontFamily.options` but processed to account for
458
+ * editor localization, i.e. to display {@link module:font/fontconfig~FontFamilyOption}
459
+ * in the correct language.
460
+ *
461
+ * Note: The reason behind this method is that there is no way to use {@link module:utils/locale~Locale#t}
462
+ * when the user configuration is defined because the editor does not exist yet.
463
+ */
464
+ _getLocalizedOptions() {
465
+ const editor = this.editor;
466
+ const t = editor.t;
467
+ const options = normalizeOptions$1((editor.config.get(FONT_FAMILY)).options);
468
+ return options.map(option => {
469
+ // The only title to localize is "Default" others are font names.
470
+ if (option.title === 'Default') {
471
+ option.title = t('Default');
472
+ }
473
+ return option;
474
+ });
475
+ }
476
+ }
477
+ /**
478
+ * Prepares FontFamily dropdown items.
479
+ */
480
+ function _prepareListOptions$1(options, command) {
481
+ const itemDefinitions = new Collection();
482
+ // Create dropdown items.
483
+ for (const option of options) {
484
+ const def = {
485
+ type: 'button',
486
+ model: new ViewModel({
487
+ commandName: FONT_FAMILY,
488
+ commandParam: option.model,
489
+ label: option.title,
490
+ role: 'menuitemradio',
491
+ withText: true
492
+ })
493
+ };
494
+ def.model.bind('isOn').to(command, 'value', value => {
495
+ // "Default" or check in strict font-family converters mode.
496
+ if (value === option.model) {
497
+ return true;
498
+ }
499
+ if (!value || !option.model) {
500
+ return false;
501
+ }
502
+ return value.split(',')[0].replace(/'/g, '').toLowerCase() === option.model.toLowerCase();
503
+ });
504
+ // Try to set a dropdown list item style.
505
+ if (option.view && typeof option.view !== 'string' && option.view.styles) {
506
+ def.model.set('labelStyle', `font-family: ${option.view.styles['font-family']}`);
507
+ }
508
+ itemDefinitions.add(def);
509
+ }
510
+ return itemDefinitions;
511
+ }
512
+
513
+ /**
514
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
515
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
516
+ */
517
+ /**
518
+ * @module font/fontfamily
519
+ */
520
+ /**
521
+ * The font family plugin.
522
+ *
523
+ * For a detailed overview, check the {@glink features/font font feature} documentatiom
524
+ * and the {@glink api/font package page}.
525
+ *
526
+ * This is a "glue" plugin which loads the {@link module:font/fontfamily/fontfamilyediting~FontFamilyEditing} and
527
+ * {@link module:font/fontfamily/fontfamilyui~FontFamilyUI} features in the editor.
528
+ */
529
+ class FontFamily extends Plugin {
530
+ /**
531
+ * @inheritDoc
532
+ */
533
+ static get requires() {
534
+ return [FontFamilyEditing, FontFamilyUI];
535
+ }
536
+ /**
537
+ * @inheritDoc
538
+ */
539
+ static get pluginName() {
540
+ return 'FontFamily';
541
+ }
542
+ }
543
+
544
+ /**
545
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
546
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
547
+ */
548
+ /**
549
+ * The font size command. It is used by {@link module:font/fontsize/fontsizeediting~FontSizeEditing}
550
+ * to apply the font size.
551
+ *
552
+ * ```ts
553
+ * editor.execute( 'fontSize', { value: 'small' } );
554
+ * ```
555
+ *
556
+ * **Note**: Executing the command without the value removes the attribute from the model.
557
+ */
558
+ class FontSizeCommand extends FontCommand {
559
+ /**
560
+ * @inheritDoc
561
+ */
562
+ constructor(editor) {
563
+ super(editor, FONT_SIZE);
564
+ }
565
+ }
566
+
567
+ /**
568
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
569
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
570
+ */
571
+ /**
572
+ * @module font/fontsize/utils
573
+ */
574
+ /**
575
+ * Normalizes and translates the {@link module:font/fontconfig~FontSizeConfig#options configuration options}
576
+ * to the {@link module:font/fontconfig~FontSizeOption} format.
577
+ *
578
+ * @param configuredOptions An array of options taken from the configuration.
579
+ */
580
+ function normalizeOptions(configuredOptions) {
581
+ // Convert options to objects.
582
+ return configuredOptions
583
+ .map(item => getOptionDefinition(item))
584
+ // Filter out undefined values that `getOptionDefinition` might return.
585
+ .filter((option) => option !== undefined);
586
+ }
587
+ // Default named presets map. Always create a new instance of the preset object in order to avoid modifying references.
588
+ const namedPresets = {
589
+ get tiny() {
590
+ return {
591
+ title: 'Tiny',
592
+ model: 'tiny',
593
+ view: {
594
+ name: 'span',
595
+ classes: 'text-tiny',
596
+ priority: 7
597
+ }
598
+ };
599
+ },
600
+ get small() {
601
+ return {
602
+ title: 'Small',
603
+ model: 'small',
604
+ view: {
605
+ name: 'span',
606
+ classes: 'text-small',
607
+ priority: 7
608
+ }
609
+ };
610
+ },
611
+ get big() {
612
+ return {
613
+ title: 'Big',
614
+ model: 'big',
615
+ view: {
616
+ name: 'span',
617
+ classes: 'text-big',
618
+ priority: 7
619
+ }
620
+ };
621
+ },
622
+ get huge() {
623
+ return {
624
+ title: 'Huge',
625
+ model: 'huge',
626
+ view: {
627
+ name: 'span',
628
+ classes: 'text-huge',
629
+ priority: 7
630
+ }
631
+ };
632
+ }
633
+ };
634
+ /**
635
+ * Returns an option definition either from preset or creates one from number shortcut.
636
+ * If object is passed then this method will return it without alternating it. Returns undefined for item than cannot be parsed.
637
+ */
638
+ function getOptionDefinition(option) {
639
+ if (typeof option === 'number') {
640
+ option = String(option);
641
+ }
642
+ // Check whether passed option is a full item definition provided by user in configuration.
643
+ if (typeof option === 'object' && isFullItemDefinition(option)) {
644
+ return attachPriority(option);
645
+ }
646
+ const preset = findPreset(option);
647
+ // Item is a named preset.
648
+ if (preset) {
649
+ return attachPriority(preset);
650
+ }
651
+ // 'Default' font size. It will be used to remove the fontSize attribute.
652
+ if (option === 'default') {
653
+ return {
654
+ model: undefined,
655
+ title: 'Default'
656
+ };
657
+ }
658
+ // At this stage we probably have numerical value to generate a preset so parse it's value.
659
+ // Discard any faulty values.
660
+ if (isNumericalDefinition(option)) {
661
+ return undefined;
662
+ }
663
+ // Return font size definition from size value.
664
+ return generatePixelPreset(option);
665
+ }
666
+ /**
667
+ * Creates a predefined preset for pixel size.
668
+ * @param definition Font size in pixels.
669
+ * @returns
670
+ */
671
+ function generatePixelPreset(definition) {
672
+ // Extend a short (numeric value) definition.
673
+ if (typeof definition === 'string') {
674
+ definition = {
675
+ title: definition,
676
+ model: `${parseFloat(definition)}px`
677
+ };
678
+ }
679
+ definition.view = {
680
+ name: 'span',
681
+ styles: {
682
+ 'font-size': definition.model
683
+ }
684
+ };
685
+ return attachPriority(definition);
686
+ }
687
+ /**
688
+ * Adds the priority to the view element definition if missing. It's required due to ckeditor/ckeditor5#2291
689
+ */
690
+ function attachPriority(definition) {
691
+ if (definition.view && typeof definition.view !== 'string' && !definition.view.priority) {
692
+ definition.view.priority = 7;
693
+ }
694
+ return definition;
695
+ }
696
+ /**
697
+ * Returns a prepared preset definition. If passed an object, a name of preset should be defined as `model` value.
698
+ *
699
+ * @param definition.model A preset name.
700
+ */
701
+ function findPreset(definition) {
702
+ return typeof definition === 'string' ? namedPresets[definition] : namedPresets[definition.model];
703
+ }
704
+ /**
705
+ * We treat `definition` as completed if it is an object that contains `title`, `model` and `view` values.
706
+ */
707
+ function isFullItemDefinition(definition) {
708
+ return definition.title && definition.model && definition.view;
709
+ }
710
+ function isNumericalDefinition(definition) {
711
+ let numberValue;
712
+ if (typeof definition === 'object') {
713
+ if (!definition.model) {
714
+ /**
715
+ * Provided value as an option for {@link module:font/fontsize~FontSize} seems to invalid.
716
+ *
717
+ * See valid examples described in the {@link module:font/fontconfig~FontSizeConfig#options plugin configuration}.
718
+ *
719
+ * @error font-size-invalid-definition
720
+ */
721
+ throw new CKEditorError('font-size-invalid-definition', null, definition);
722
+ }
723
+ else {
724
+ numberValue = parseFloat(definition.model);
725
+ }
726
+ }
727
+ else {
728
+ numberValue = parseFloat(definition);
729
+ }
730
+ return isNaN(numberValue);
731
+ }
732
+
733
+ /**
734
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
735
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
736
+ */
737
+ /**
738
+ * @module font/fontsize/fontsizeediting
739
+ */
740
+ // Mapping of `<font size="..">` styling to CSS's `font-size` values.
741
+ const styleFontSize = [
742
+ 'x-small',
743
+ 'x-small',
744
+ 'small',
745
+ 'medium',
746
+ 'large',
747
+ 'x-large',
748
+ 'xx-large',
749
+ 'xxx-large'
750
+ ];
751
+ /**
752
+ * The font size editing feature.
753
+ *
754
+ * It introduces the {@link module:font/fontsize/fontsizecommand~FontSizeCommand command} and the `fontSize`
755
+ * attribute in the {@link module:engine/model/model~Model model} which renders in the {@link module:engine/view/view view}
756
+ * as a `<span>` element with either:
757
+ * * a style attribute (`<span style="font-size:12px">...</span>`),
758
+ * * or a class attribute (`<span class="text-small">...</span>`)
759
+ *
760
+ * depending on the {@link module:font/fontconfig~FontSizeConfig configuration}.
761
+ */
762
+ class FontSizeEditing extends Plugin {
763
+ /**
764
+ * @inheritDoc
765
+ */
766
+ static get pluginName() {
767
+ return 'FontSizeEditing';
768
+ }
769
+ /**
770
+ * @inheritDoc
771
+ */
772
+ constructor(editor) {
773
+ super(editor);
774
+ // Define default configuration using named presets.
775
+ editor.config.define(FONT_SIZE, {
776
+ options: [
777
+ 'tiny',
778
+ 'small',
779
+ 'default',
780
+ 'big',
781
+ 'huge'
782
+ ],
783
+ supportAllValues: false
784
+ });
785
+ }
786
+ /**
787
+ * @inheritDoc
788
+ */
789
+ init() {
790
+ const editor = this.editor;
791
+ // Allow fontSize attribute on text nodes.
792
+ editor.model.schema.extend('$text', { allowAttributes: FONT_SIZE });
793
+ editor.model.schema.setAttributeProperties(FONT_SIZE, {
794
+ isFormatting: true,
795
+ copyOnEnter: true
796
+ });
797
+ const supportAllValues = editor.config.get('fontSize.supportAllValues');
798
+ // Define view to model conversion.
799
+ const options = normalizeOptions(this.editor.config.get('fontSize.options'))
800
+ .filter(item => item.model);
801
+ const definition = buildDefinition(FONT_SIZE, options);
802
+ // Set-up the two-way conversion.
803
+ if (supportAllValues) {
804
+ this._prepareAnyValueConverters(definition);
805
+ this._prepareCompatibilityConverter();
806
+ }
807
+ else {
808
+ editor.conversion.attributeToElement(definition);
809
+ }
810
+ // Add FontSize command.
811
+ editor.commands.add(FONT_SIZE, new FontSizeCommand(editor));
812
+ }
813
+ /**
814
+ * These converters enable keeping any value found as `style="font-size: *"` as a value of an attribute on a text even
815
+ * if it is not defined in the plugin configuration.
816
+ *
817
+ * @param definition Converter definition out of input data.
818
+ */
819
+ _prepareAnyValueConverters(definition) {
820
+ const editor = this.editor;
821
+ // If `fontSize.supportAllValues=true`, we do not allow to use named presets in the plugin's configuration.
822
+ const presets = definition.model.values.filter((value) => {
823
+ return !isLength(String(value)) && !isPercentage(String(value));
824
+ });
825
+ if (presets.length) {
826
+ /**
827
+ * If {@link module:font/fontconfig~FontSizeConfig#supportAllValues `config.fontSize.supportAllValues`} is `true`,
828
+ * you need to use numerical values as font size options.
829
+ *
830
+ * See valid examples described in the {@link module:font/fontconfig~FontSizeConfig#options plugin configuration}.
831
+ *
832
+ * @error font-size-invalid-use-of-named-presets
833
+ * @param {Array.<String>} presets Invalid values.
834
+ */
835
+ throw new CKEditorError('font-size-invalid-use-of-named-presets', null, { presets });
836
+ }
837
+ editor.conversion.for('downcast').attributeToElement({
838
+ model: FONT_SIZE,
839
+ view: (attributeValue, { writer }) => {
840
+ if (!attributeValue) {
841
+ return;
842
+ }
843
+ return writer.createAttributeElement('span', { style: 'font-size:' + attributeValue }, { priority: 7 });
844
+ }
845
+ });
846
+ editor.conversion.for('upcast').elementToAttribute({
847
+ model: {
848
+ key: FONT_SIZE,
849
+ value: (viewElement) => viewElement.getStyle('font-size')
850
+ },
851
+ view: {
852
+ name: 'span',
853
+ styles: {
854
+ 'font-size': /.*/
855
+ }
856
+ }
857
+ });
858
+ }
859
+ /**
860
+ * Adds support for legacy `<font size="..">` formatting.
861
+ */
862
+ _prepareCompatibilityConverter() {
863
+ const editor = this.editor;
864
+ editor.conversion.for('upcast').elementToAttribute({
865
+ view: {
866
+ name: 'font',
867
+ attributes: {
868
+ // Documentation mentions sizes from 1 to 7. To handle old content we support all values
869
+ // up to 999 but clamp it to the valid range. Why 999? It should cover accidental values
870
+ // similar to percentage, e.g. 100%, 200% which could be the usual mistake for font size.
871
+ 'size': /^[+-]?\d{1,3}$/
872
+ }
873
+ },
874
+ model: {
875
+ key: FONT_SIZE,
876
+ value: (viewElement) => {
877
+ const value = viewElement.getAttribute('size');
878
+ const isRelative = value[0] === '-' || value[0] === '+';
879
+ let size = parseInt(value, 10);
880
+ if (isRelative) {
881
+ // Add relative size (which can be negative) to the default size = 3.
882
+ size = 3 + size;
883
+ }
884
+ const maxSize = styleFontSize.length - 1;
885
+ const clampedSize = Math.min(Math.max(size, 0), maxSize);
886
+ return styleFontSize[clampedSize];
887
+ }
888
+ }
889
+ });
890
+ }
891
+ }
892
+
893
+ var fontSizeIcon = "<svg viewBox=\"0 0 20 20\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M9.816 11.5 7.038 4.785 4.261 11.5h5.555zm.62 1.5H3.641l-1.666 4.028H.312l5.789-14h1.875l5.789 14h-1.663L10.436 13zm7.55 2.279.779-.779.707.707-2.265 2.265-2.193-2.265.707-.707.765.765V4.825c0-.042 0-.083.002-.123l-.77.77-.707-.707L17.207 2.5l2.265 2.265-.707.707-.782-.782c.002.043.003.089.003.135v10.454z\"/></svg>";
894
+
895
+ /**
896
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
897
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
898
+ */
899
+ /**
900
+ * @module font/fontsize/fontsizeui
901
+ */
902
+ /**
903
+ * The font size UI plugin. It introduces the `'fontSize'` dropdown.
904
+ */
905
+ class FontSizeUI extends Plugin {
906
+ /**
907
+ * @inheritDoc
908
+ */
909
+ static get pluginName() {
910
+ return 'FontSizeUI';
911
+ }
912
+ /**
913
+ * @inheritDoc
914
+ */
915
+ init() {
916
+ const editor = this.editor;
917
+ const t = editor.t;
918
+ const options = this._getLocalizedOptions();
919
+ const command = editor.commands.get(FONT_SIZE);
920
+ const accessibleLabel = t('Font Size');
921
+ // Register UI component.
922
+ editor.ui.componentFactory.add(FONT_SIZE, locale => {
923
+ const dropdownView = createDropdown(locale);
924
+ addListToDropdown(dropdownView, () => _prepareListOptions(options, command), {
925
+ role: 'menu',
926
+ ariaLabel: accessibleLabel
927
+ });
928
+ // Create dropdown model.
929
+ dropdownView.buttonView.set({
930
+ label: accessibleLabel,
931
+ icon: fontSizeIcon,
932
+ tooltip: true
933
+ });
934
+ dropdownView.extendTemplate({
935
+ attributes: {
936
+ class: [
937
+ 'ck-font-size-dropdown'
938
+ ]
939
+ }
940
+ });
941
+ dropdownView.bind('isEnabled').to(command);
942
+ // Execute command when an item from the dropdown is selected.
943
+ this.listenTo(dropdownView, 'execute', evt => {
944
+ editor.execute(evt.source.commandName, { value: evt.source.commandParam });
945
+ editor.editing.view.focus();
946
+ });
947
+ return dropdownView;
948
+ });
949
+ }
950
+ /**
951
+ * Returns options as defined in `config.fontSize.options` but processed to account for
952
+ * editor localization, i.e. to display {@link module:font/fontconfig~FontSizeOption}
953
+ * in the correct language.
954
+ *
955
+ * Note: The reason behind this method is that there is no way to use {@link module:utils/locale~Locale#t}
956
+ * when the user configuration is defined because the editor does not exist yet.
957
+ */
958
+ _getLocalizedOptions() {
959
+ const editor = this.editor;
960
+ const t = editor.t;
961
+ const localizedTitles = {
962
+ Default: t('Default'),
963
+ Tiny: t('Tiny'),
964
+ Small: t('Small'),
965
+ Big: t('Big'),
966
+ Huge: t('Huge')
967
+ };
968
+ const options = normalizeOptions((editor.config.get(FONT_SIZE)).options);
969
+ return options.map(option => {
970
+ const title = localizedTitles[option.title];
971
+ if (title && title != option.title) {
972
+ // Clone the option to avoid altering the original `namedPresets` from `./utils.js`.
973
+ option = Object.assign({}, option, { title });
974
+ }
975
+ return option;
976
+ });
977
+ }
978
+ }
979
+ /**
980
+ * Prepares FontSize dropdown items.
981
+ */
982
+ function _prepareListOptions(options, command) {
983
+ const itemDefinitions = new Collection();
984
+ for (const option of options) {
985
+ const def = {
986
+ type: 'button',
987
+ model: new ViewModel({
988
+ commandName: FONT_SIZE,
989
+ commandParam: option.model,
990
+ label: option.title,
991
+ class: 'ck-fontsize-option',
992
+ role: 'menuitemradio',
993
+ withText: true
994
+ })
995
+ };
996
+ if (option.view && typeof option.view !== 'string') {
997
+ if (option.view.styles) {
998
+ def.model.set('labelStyle', `font-size:${option.view.styles['font-size']}`);
999
+ }
1000
+ if (option.view.classes) {
1001
+ def.model.set('class', `${def.model.class} ${option.view.classes}`);
1002
+ }
1003
+ }
1004
+ def.model.bind('isOn').to(command, 'value', value => value === option.model);
1005
+ // Add the option to the collection.
1006
+ itemDefinitions.add(def);
1007
+ }
1008
+ return itemDefinitions;
1009
+ }
1010
+
1011
+ /**
1012
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
1013
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
1014
+ */
1015
+ /**
1016
+ * @module font/fontsize
1017
+ */
1018
+ /**
1019
+ * The font size plugin.
1020
+ *
1021
+ * For a detailed overview, check the {@glink features/font font feature} documentation
1022
+ * and the {@glink api/font package page}.
1023
+ *
1024
+ * This is a "glue" plugin which loads the {@link module:font/fontsize/fontsizeediting~FontSizeEditing} and
1025
+ * {@link module:font/fontsize/fontsizeui~FontSizeUI} features in the editor.
1026
+ */
1027
+ class FontSize extends Plugin {
1028
+ /**
1029
+ * @inheritDoc
1030
+ */
1031
+ static get requires() {
1032
+ return [FontSizeEditing, FontSizeUI];
1033
+ }
1034
+ /**
1035
+ * @inheritDoc
1036
+ */
1037
+ static get pluginName() {
1038
+ return 'FontSize';
1039
+ }
1040
+ /**
1041
+ * Normalizes and translates the {@link module:font/fontconfig~FontSizeConfig#options configuration options}
1042
+ * to the {@link module:font/fontconfig~FontSizeOption} format.
1043
+ *
1044
+ * @param configuredOptions An array of options taken from the configuration.
1045
+ */
1046
+ normalizeSizeOptions(options) {
1047
+ return normalizeOptions(options);
1048
+ }
1049
+ }
1050
+
1051
+ /**
1052
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
1053
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
1054
+ */
1055
+ /**
1056
+ * The font color command. It is used by {@link module:font/fontcolor/fontcolorediting~FontColorEditing}
1057
+ * to apply the font color.
1058
+ *
1059
+ * ```ts
1060
+ * editor.execute( 'fontColor', { value: 'rgb(250, 20, 20)' } );
1061
+ * ```
1062
+ *
1063
+ * **Note**: Executing the command with the `null` value removes the attribute from the model.
1064
+ */
1065
+ class FontColorCommand extends FontCommand {
1066
+ /**
1067
+ * @inheritDoc
1068
+ */
1069
+ constructor(editor) {
1070
+ super(editor, FONT_COLOR);
1071
+ }
1072
+ }
1073
+
1074
+ /**
1075
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
1076
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
1077
+ */
1078
+ /**
1079
+ * @module font/fontcolor/fontcolorediting
1080
+ */
1081
+ /**
1082
+ * The font color editing feature.
1083
+ *
1084
+ * It introduces the {@link module:font/fontcolor/fontcolorcommand~FontColorCommand command} and
1085
+ * the `fontColor` attribute in the {@link module:engine/model/model~Model model} which renders
1086
+ * in the {@link module:engine/view/view view} as a `<span>` element (`<span style="color: ...">`),
1087
+ * depending on the {@link module:font/fontconfig~FontColorConfig configuration}.
1088
+ */
1089
+ class FontColorEditing extends Plugin {
1090
+ /**
1091
+ * @inheritDoc
1092
+ */
1093
+ static get pluginName() {
1094
+ return 'FontColorEditing';
1095
+ }
1096
+ /**
1097
+ * @inheritDoc
1098
+ */
1099
+ constructor(editor) {
1100
+ super(editor);
1101
+ editor.config.define(FONT_COLOR, {
1102
+ colors: [
1103
+ {
1104
+ color: 'hsl(0, 0%, 0%)',
1105
+ label: 'Black'
1106
+ },
1107
+ {
1108
+ color: 'hsl(0, 0%, 30%)',
1109
+ label: 'Dim grey'
1110
+ },
1111
+ {
1112
+ color: 'hsl(0, 0%, 60%)',
1113
+ label: 'Grey'
1114
+ },
1115
+ {
1116
+ color: 'hsl(0, 0%, 90%)',
1117
+ label: 'Light grey'
1118
+ },
1119
+ {
1120
+ color: 'hsl(0, 0%, 100%)',
1121
+ label: 'White',
1122
+ hasBorder: true
1123
+ },
1124
+ {
1125
+ color: 'hsl(0, 75%, 60%)',
1126
+ label: 'Red'
1127
+ },
1128
+ {
1129
+ color: 'hsl(30, 75%, 60%)',
1130
+ label: 'Orange'
1131
+ },
1132
+ {
1133
+ color: 'hsl(60, 75%, 60%)',
1134
+ label: 'Yellow'
1135
+ },
1136
+ {
1137
+ color: 'hsl(90, 75%, 60%)',
1138
+ label: 'Light green'
1139
+ },
1140
+ {
1141
+ color: 'hsl(120, 75%, 60%)',
1142
+ label: 'Green'
1143
+ },
1144
+ {
1145
+ color: 'hsl(150, 75%, 60%)',
1146
+ label: 'Aquamarine'
1147
+ },
1148
+ {
1149
+ color: 'hsl(180, 75%, 60%)',
1150
+ label: 'Turquoise'
1151
+ },
1152
+ {
1153
+ color: 'hsl(210, 75%, 60%)',
1154
+ label: 'Light blue'
1155
+ },
1156
+ {
1157
+ color: 'hsl(240, 75%, 60%)',
1158
+ label: 'Blue'
1159
+ },
1160
+ {
1161
+ color: 'hsl(270, 75%, 60%)',
1162
+ label: 'Purple'
1163
+ }
1164
+ ],
1165
+ columns: 5
1166
+ });
1167
+ editor.conversion.for('upcast').elementToAttribute({
1168
+ view: {
1169
+ name: 'span',
1170
+ styles: {
1171
+ 'color': /[\s\S]+/
1172
+ }
1173
+ },
1174
+ model: {
1175
+ key: FONT_COLOR,
1176
+ value: renderUpcastAttribute('color')
1177
+ }
1178
+ });
1179
+ // Support legacy `<font color="..">` formatting.
1180
+ editor.conversion.for('upcast').elementToAttribute({
1181
+ view: {
1182
+ name: 'font',
1183
+ attributes: {
1184
+ 'color': /^#?\w+$/
1185
+ }
1186
+ },
1187
+ model: {
1188
+ key: FONT_COLOR,
1189
+ value: (viewElement) => viewElement.getAttribute('color')
1190
+ }
1191
+ });
1192
+ editor.conversion.for('downcast').attributeToElement({
1193
+ model: FONT_COLOR,
1194
+ view: renderDowncastElement('color')
1195
+ });
1196
+ editor.commands.add(FONT_COLOR, new FontColorCommand(editor));
1197
+ // Allow the font color attribute on text nodes.
1198
+ editor.model.schema.extend('$text', { allowAttributes: FONT_COLOR });
1199
+ editor.model.schema.setAttributeProperties(FONT_COLOR, {
1200
+ isFormatting: true,
1201
+ copyOnEnter: true
1202
+ });
1203
+ }
1204
+ }
1205
+
1206
+ /**
1207
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
1208
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
1209
+ */
1210
+ /**
1211
+ * @module font/ui/colorui
1212
+ */
1213
+ /**
1214
+ * The color UI plugin which isolates the common logic responsible for displaying dropdowns with color grids.
1215
+ *
1216
+ * It is used to create the `'fontBackgroundColor'` and `'fontColor'` dropdowns, each hosting
1217
+ * a {@link module:ui/colorselector/colorselectorview~ColorSelectorView}.
1218
+ */
1219
+ class ColorUI extends Plugin {
1220
+ /**
1221
+ * Creates a plugin which introduces a dropdown with a pre–configured
1222
+ * {@link module:ui/colorselector/colorselectorview~ColorSelectorView}.
1223
+ *
1224
+ * @param config The configuration object.
1225
+ * @param config.commandName The name of the command which will be executed when a color tile is clicked.
1226
+ * @param config.componentName The name of the dropdown in the {@link module:ui/componentfactory~ComponentFactory}
1227
+ * and the configuration scope name in `editor.config`.
1228
+ * @param config.icon The SVG icon used by the dropdown.
1229
+ * @param config.dropdownLabel The label used by the dropdown.
1230
+ */
1231
+ constructor(editor, { commandName, componentName, icon, dropdownLabel }) {
1232
+ super(editor);
1233
+ this.commandName = commandName;
1234
+ this.componentName = componentName;
1235
+ this.icon = icon;
1236
+ this.dropdownLabel = dropdownLabel;
1237
+ this.columns = editor.config.get(`${this.componentName}.columns`);
1238
+ }
1239
+ /**
1240
+ * @inheritDoc
1241
+ */
1242
+ init() {
1243
+ const editor = this.editor;
1244
+ const locale = editor.locale;
1245
+ const t = locale.t;
1246
+ const command = editor.commands.get(this.commandName);
1247
+ const componentConfig = editor.config.get(this.componentName);
1248
+ const colorsConfig = normalizeColorOptions(componentConfig.colors);
1249
+ const localizedColors = getLocalizedColorOptions(locale, colorsConfig);
1250
+ const documentColorsCount = componentConfig.documentColors;
1251
+ const hasColorPicker = componentConfig.colorPicker !== false;
1252
+ // Register the UI component.
1253
+ editor.ui.componentFactory.add(this.componentName, locale => {
1254
+ const dropdownView = createDropdown(locale);
1255
+ // Font color dropdown rendering is deferred once it gets open to improve performance (#6192).
1256
+ let dropdownContentRendered = false;
1257
+ const colorSelectorView = addColorSelectorToDropdown({
1258
+ dropdownView,
1259
+ colors: localizedColors.map(option => ({
1260
+ label: option.label,
1261
+ color: option.model,
1262
+ options: {
1263
+ hasBorder: option.hasBorder
1264
+ }
1265
+ })),
1266
+ columns: this.columns,
1267
+ removeButtonLabel: t('Remove color'),
1268
+ colorPickerLabel: t('Color picker'),
1269
+ documentColorsLabel: documentColorsCount !== 0 ? t('Document colors') : '',
1270
+ documentColorsCount: documentColorsCount === undefined ? this.columns : documentColorsCount,
1271
+ colorPickerViewConfig: hasColorPicker ? (componentConfig.colorPicker || {}) : false
1272
+ });
1273
+ colorSelectorView.bind('selectedColor').to(command, 'value');
1274
+ dropdownView.buttonView.set({
1275
+ label: this.dropdownLabel,
1276
+ icon: this.icon,
1277
+ tooltip: true
1278
+ });
1279
+ dropdownView.extendTemplate({
1280
+ attributes: {
1281
+ class: 'ck-color-ui-dropdown'
1282
+ }
1283
+ });
1284
+ dropdownView.bind('isEnabled').to(command);
1285
+ colorSelectorView.on('execute', (evt, data) => {
1286
+ if (dropdownView.isOpen) {
1287
+ editor.execute(this.commandName, {
1288
+ value: data.value,
1289
+ batch: this._undoStepBatch
1290
+ });
1291
+ }
1292
+ if (data.source !== 'colorPicker') {
1293
+ editor.editing.view.focus();
1294
+ }
1295
+ if (data.source === 'colorPickerSaveButton') {
1296
+ dropdownView.isOpen = false;
1297
+ }
1298
+ });
1299
+ colorSelectorView.on('colorPicker:show', () => {
1300
+ this._undoStepBatch = editor.model.createBatch();
1301
+ });
1302
+ colorSelectorView.on('colorPicker:cancel', () => {
1303
+ if (this._undoStepBatch.operations.length) {
1304
+ // We need to close the dropdown before the undo batch.
1305
+ // Otherwise, ColorUI treats undo as a selected color change,
1306
+ // propagating the update to the whole selection.
1307
+ // That's an issue if spans with various colors were selected.
1308
+ dropdownView.isOpen = false;
1309
+ editor.execute('undo', this._undoStepBatch);
1310
+ }
1311
+ editor.editing.view.focus();
1312
+ });
1313
+ dropdownView.on('change:isOpen', (evt, name, isVisible) => {
1314
+ if (!dropdownContentRendered) {
1315
+ dropdownContentRendered = true;
1316
+ dropdownView.colorSelectorView.appendUI();
1317
+ }
1318
+ if (isVisible) {
1319
+ if (documentColorsCount !== 0) {
1320
+ colorSelectorView.updateDocumentColors(editor.model, this.componentName);
1321
+ }
1322
+ colorSelectorView.updateSelectedColors();
1323
+ colorSelectorView.showColorGridsFragment();
1324
+ }
1325
+ });
1326
+ // Accessibility: focus the first active color when opening the dropdown.
1327
+ focusChildOnDropdownOpen(dropdownView, () => dropdownView.colorSelectorView.colorGridsFragmentView.staticColorsGrid.items.find((item) => item.isOn));
1328
+ return dropdownView;
1329
+ });
1330
+ }
1331
+ }
1332
+
1333
+ var fontColorIcon = "<svg viewBox=\"0 0 20 20\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M12.4 10.3 10 4.5l-2.4 5.8h4.8zm.5 1.2H7.1L5.7 15H4.2l5-12h1.6l5 12h-1.5L13 11.5zm3.1 7H4a1 1 0 0 1 0-2h12a1 1 0 0 1 0 2z\"/></svg>";
1334
+
1335
+ /**
1336
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
1337
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
1338
+ */
1339
+ /**
1340
+ * @module font/fontcolor/fontcolorui
1341
+ */
1342
+ /**
1343
+ * The font color UI plugin. It introduces the `'fontColor'` dropdown.
1344
+ */
1345
+ class FontColorUI extends ColorUI {
1346
+ /**
1347
+ * @inheritDoc
1348
+ */
1349
+ constructor(editor) {
1350
+ const t = editor.locale.t;
1351
+ super(editor, {
1352
+ commandName: FONT_COLOR,
1353
+ componentName: FONT_COLOR,
1354
+ icon: fontColorIcon,
1355
+ dropdownLabel: t('Font Color')
1356
+ });
1357
+ }
1358
+ /**
1359
+ * @inheritDoc
1360
+ */
1361
+ static get pluginName() {
1362
+ return 'FontColorUI';
1363
+ }
1364
+ }
1365
+
1366
+ /**
1367
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
1368
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
1369
+ */
1370
+ /**
1371
+ * @module font/fontcolor
1372
+ */
1373
+ /**
1374
+ * The font color plugin.
1375
+ *
1376
+ * For a detailed overview, check the {@glink features/font font feature} documentation
1377
+ * and the {@glink api/font package page}.
1378
+ *
1379
+ * This is a "glue" plugin which loads the {@link module:font/fontcolor/fontcolorediting~FontColorEditing} and
1380
+ * {@link module:font/fontcolor/fontcolorui~FontColorUI} features in the editor.
1381
+ */
1382
+ class FontColor extends Plugin {
1383
+ /**
1384
+ * @inheritDoc
1385
+ */
1386
+ static get requires() {
1387
+ return [FontColorEditing, FontColorUI];
1388
+ }
1389
+ /**
1390
+ * @inheritDoc
1391
+ */
1392
+ static get pluginName() {
1393
+ return 'FontColor';
1394
+ }
1395
+ }
1396
+
1397
+ /**
1398
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
1399
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
1400
+ */
1401
+ /**
1402
+ * The font background color command. It is used by
1403
+ * {@link module:font/fontbackgroundcolor/fontbackgroundcolorediting~FontBackgroundColorEditing}
1404
+ * to apply the font background color.
1405
+ *
1406
+ * ```ts
1407
+ * editor.execute( 'fontBackgroundColor', { value: 'rgb(250, 20, 20)' } );
1408
+ * ```
1409
+ *
1410
+ * **Note**: Executing the command with the `null` value removes the attribute from the model.
1411
+ */
1412
+ class FontBackgroundColorCommand extends FontCommand {
1413
+ /**
1414
+ * @inheritDoc
1415
+ */
1416
+ constructor(editor) {
1417
+ super(editor, FONT_BACKGROUND_COLOR);
1418
+ }
1419
+ }
1420
+
1421
+ /**
1422
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
1423
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
1424
+ */
1425
+ /**
1426
+ * @module font/fontbackgroundcolor/fontbackgroundcolorediting
1427
+ */
1428
+ /**
1429
+ * The font background color editing feature.
1430
+ *
1431
+ * It introduces the {@link module:font/fontbackgroundcolor/fontbackgroundcolorcommand~FontBackgroundColorCommand command} and
1432
+ * the `fontBackgroundColor` attribute in the {@link module:engine/model/model~Model model} which renders
1433
+ * in the {@link module:engine/view/view view} as a `<span>` element (`<span style="background-color: ...">`),
1434
+ * depending on the {@link module:font/fontconfig~FontColorConfig configuration}.
1435
+ */
1436
+ class FontBackgroundColorEditing extends Plugin {
1437
+ /**
1438
+ * @inheritDoc
1439
+ */
1440
+ static get pluginName() {
1441
+ return 'FontBackgroundColorEditing';
1442
+ }
1443
+ /**
1444
+ * @inheritDoc
1445
+ */
1446
+ constructor(editor) {
1447
+ super(editor);
1448
+ editor.config.define(FONT_BACKGROUND_COLOR, {
1449
+ colors: [
1450
+ {
1451
+ color: 'hsl(0, 0%, 0%)',
1452
+ label: 'Black'
1453
+ },
1454
+ {
1455
+ color: 'hsl(0, 0%, 30%)',
1456
+ label: 'Dim grey'
1457
+ },
1458
+ {
1459
+ color: 'hsl(0, 0%, 60%)',
1460
+ label: 'Grey'
1461
+ },
1462
+ {
1463
+ color: 'hsl(0, 0%, 90%)',
1464
+ label: 'Light grey'
1465
+ },
1466
+ {
1467
+ color: 'hsl(0, 0%, 100%)',
1468
+ label: 'White',
1469
+ hasBorder: true
1470
+ },
1471
+ {
1472
+ color: 'hsl(0, 75%, 60%)',
1473
+ label: 'Red'
1474
+ },
1475
+ {
1476
+ color: 'hsl(30, 75%, 60%)',
1477
+ label: 'Orange'
1478
+ },
1479
+ {
1480
+ color: 'hsl(60, 75%, 60%)',
1481
+ label: 'Yellow'
1482
+ },
1483
+ {
1484
+ color: 'hsl(90, 75%, 60%)',
1485
+ label: 'Light green'
1486
+ },
1487
+ {
1488
+ color: 'hsl(120, 75%, 60%)',
1489
+ label: 'Green'
1490
+ },
1491
+ {
1492
+ color: 'hsl(150, 75%, 60%)',
1493
+ label: 'Aquamarine'
1494
+ },
1495
+ {
1496
+ color: 'hsl(180, 75%, 60%)',
1497
+ label: 'Turquoise'
1498
+ },
1499
+ {
1500
+ color: 'hsl(210, 75%, 60%)',
1501
+ label: 'Light blue'
1502
+ },
1503
+ {
1504
+ color: 'hsl(240, 75%, 60%)',
1505
+ label: 'Blue'
1506
+ },
1507
+ {
1508
+ color: 'hsl(270, 75%, 60%)',
1509
+ label: 'Purple'
1510
+ }
1511
+ ],
1512
+ columns: 5
1513
+ });
1514
+ editor.data.addStyleProcessorRules(addBackgroundRules);
1515
+ editor.conversion.for('upcast').elementToAttribute({
1516
+ view: {
1517
+ name: 'span',
1518
+ styles: {
1519
+ 'background-color': /[\s\S]+/
1520
+ }
1521
+ },
1522
+ model: {
1523
+ key: FONT_BACKGROUND_COLOR,
1524
+ value: renderUpcastAttribute('background-color')
1525
+ }
1526
+ });
1527
+ editor.conversion.for('downcast').attributeToElement({
1528
+ model: FONT_BACKGROUND_COLOR,
1529
+ view: renderDowncastElement('background-color')
1530
+ });
1531
+ editor.commands.add(FONT_BACKGROUND_COLOR, new FontBackgroundColorCommand(editor));
1532
+ // Allow the font backgroundColor attribute on text nodes.
1533
+ editor.model.schema.extend('$text', { allowAttributes: FONT_BACKGROUND_COLOR });
1534
+ editor.model.schema.setAttributeProperties(FONT_BACKGROUND_COLOR, {
1535
+ isFormatting: true,
1536
+ copyOnEnter: true
1537
+ });
1538
+ }
1539
+ }
1540
+
1541
+ var fontBackgroundColorIcon = "<svg viewBox=\"0 0 20 20\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M4 2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2zm8.38 9.262H7.62L10 5.506l2.38 5.756zm.532 1.285L14.34 16h1.426L10.804 4H9.196L4.234 16H5.66l1.428-3.453h5.824z\"/></svg>";
1542
+
1543
+ /**
1544
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
1545
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
1546
+ */
1547
+ /**
1548
+ * @module font/fontbackgroundcolor/fontbackgroundcolorui
1549
+ */
1550
+ /**
1551
+ * The font background color UI plugin. It introduces the `'fontBackgroundColor'` dropdown.
1552
+ */
1553
+ class FontBackgroundColorUI extends ColorUI {
1554
+ /**
1555
+ * @inheritDoc
1556
+ */
1557
+ constructor(editor) {
1558
+ const t = editor.locale.t;
1559
+ super(editor, {
1560
+ commandName: FONT_BACKGROUND_COLOR,
1561
+ componentName: FONT_BACKGROUND_COLOR,
1562
+ icon: fontBackgroundColorIcon,
1563
+ dropdownLabel: t('Font Background Color')
1564
+ });
1565
+ }
1566
+ /**
1567
+ * @inheritDoc
1568
+ */
1569
+ static get pluginName() {
1570
+ return 'FontBackgroundColorUI';
1571
+ }
1572
+ }
1573
+
1574
+ /**
1575
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
1576
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
1577
+ */
1578
+ /**
1579
+ * @module font/fontbackgroundcolor
1580
+ */
1581
+ /**
1582
+ * The font background color plugin.
1583
+ *
1584
+ * For a detailed overview, check the {@glink features/font font feature} documentation
1585
+ * and the {@glink api/font package page}.
1586
+ *
1587
+ * This is a "glue" plugin which loads
1588
+ * the {@link module:font/fontbackgroundcolor/fontbackgroundcolorediting~FontBackgroundColorEditing} and
1589
+ * {@link module:font/fontbackgroundcolor/fontbackgroundcolorui~FontBackgroundColorUI} features in the editor.
1590
+ */
1591
+ class FontBackgroundColor extends Plugin {
1592
+ /**
1593
+ * @inheritDoc
1594
+ */
1595
+ static get requires() {
1596
+ return [FontBackgroundColorEditing, FontBackgroundColorUI];
1597
+ }
1598
+ /**
1599
+ * @inheritDoc
1600
+ */
1601
+ static get pluginName() {
1602
+ return 'FontBackgroundColor';
1603
+ }
1604
+ }
1605
+
1606
+ /**
1607
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
1608
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
1609
+ */
1610
+ /**
1611
+ * @module font/font
1612
+ */
1613
+ /**
1614
+ * A plugin that enables a set of text styling features:
1615
+ *
1616
+ * * {@link module:font/fontsize~FontSize},
1617
+ * * {@link module:font/fontfamily~FontFamily}.
1618
+ * * {@link module:font/fontcolor~FontColor},
1619
+ * * {@link module:font/fontbackgroundcolor~FontBackgroundColor}.
1620
+ *
1621
+ * For a detailed overview, check the {@glink features/font Font feature} documentation
1622
+ * and the {@glink api/font package page}.
1623
+ */
1624
+ class Font extends Plugin {
1625
+ /**
1626
+ * @inheritDoc
1627
+ */
1628
+ static get requires() {
1629
+ return [FontFamily, FontSize, FontColor, FontBackgroundColor];
1630
+ }
1631
+ /**
1632
+ * @inheritDoc
1633
+ */
1634
+ static get pluginName() {
1635
+ return 'Font';
1636
+ }
1637
+ }
1638
+
1639
+ export { Font, FontBackgroundColor, FontBackgroundColorEditing, FontBackgroundColorUI, FontColor, FontColorEditing, FontColorUI, FontFamily, FontFamilyEditing, FontFamilyUI, FontSize, FontSizeEditing, FontSizeUI };
1640
+ //# sourceMappingURL=index.js.map