@ckeditor/ckeditor5-style 41.4.2 → 42.0.0-alpha.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/dist/index.js CHANGED
@@ -8,10 +8,39 @@ import { FocusTracker, KeystrokeHandler, first, logWarning } from '@ckeditor/cke
8
8
  import { isObject } from 'lodash-es';
9
9
  import { findAttributeRange, findAttributeRangeBound } from '@ckeditor/ckeditor5-typing/dist/index.js';
10
10
 
11
- class StyleGridButtonView extends ButtonView {
11
+ /**
12
+ * A class representing an individual button (style) in the grid. Renders a rich preview of the style.
13
+ */ class StyleGridButtonView extends ButtonView {
14
+ /**
15
+ * Definition of the style the button will apply when executed.
16
+ */ styleDefinition;
17
+ /**
18
+ * The view rendering the preview of the style.
19
+ */ previewView;
12
20
  /**
13
- * Creates the view representing the preview of the style.
14
- */ _createPreview() {
21
+ * Creates an instance of the {@link module:style/ui/stylegridbuttonview~StyleGridButtonView} class.
22
+ *
23
+ * @param locale The localization services instance.
24
+ * @param styleDefinition Definition of the style.
25
+ */ constructor(locale, styleDefinition){
26
+ super(locale);
27
+ this.styleDefinition = styleDefinition;
28
+ this.previewView = this._createPreview();
29
+ this.set({
30
+ label: styleDefinition.name,
31
+ class: 'ck-style-grid__button',
32
+ withText: true
33
+ });
34
+ this.extendTemplate({
35
+ attributes: {
36
+ role: 'option'
37
+ }
38
+ });
39
+ this.children.add(this.previewView, 0);
40
+ }
41
+ /**
42
+ * Creates the view representing the preview of the style.
43
+ */ _createPreview() {
15
44
  const previewView = new View(this.locale);
16
45
  previewView.setTemplate({
17
46
  tag: 'div',
@@ -31,65 +60,27 @@ class StyleGridButtonView extends ButtonView {
31
60
  });
32
61
  return previewView;
33
62
  }
34
- /**
35
- * Creates an instance of the {@link module:style/ui/stylegridbuttonview~StyleGridButtonView} class.
36
- *
37
- * @param locale The localization services instance.
38
- * @param styleDefinition Definition of the style.
39
- */ constructor(locale, styleDefinition){
40
- super(locale);
41
- this.styleDefinition = styleDefinition;
42
- this.previewView = this._createPreview();
43
- this.set({
44
- label: styleDefinition.name,
45
- class: 'ck-style-grid__button',
46
- withText: true
47
- });
48
- this.extendTemplate({
49
- attributes: {
50
- role: 'option'
51
- }
52
- });
53
- this.children.add(this.previewView, 0);
54
- }
55
63
  }
56
64
 
57
- class StyleGridView extends View {
65
+ /**
66
+ * A class representing a grid of styles ({@link module:style/ui/stylegridbuttonview~StyleGridButtonView buttons}).
67
+ * Allows users to select a style.
68
+ */ class StyleGridView extends View {
58
69
  /**
59
- * @inheritDoc
60
- */ render() {
61
- super.render();
62
- for (const child of this.children){
63
- this.focusTracker.add(child.element);
64
- }
65
- addKeyboardHandlingForGrid({
66
- keystrokeHandler: this.keystrokes,
67
- focusTracker: this.focusTracker,
68
- gridItems: this.children,
69
- numberOfColumns: 3,
70
- uiLanguageDirection: this.locale && this.locale.uiLanguageDirection
71
- });
72
- // Start listening for the keystrokes coming from the grid view.
73
- this.keystrokes.listenTo(this.element);
74
- }
70
+ * Tracks information about the DOM focus in the view.
71
+ */ focusTracker;
75
72
  /**
76
- * Focuses the first style button in the grid.
77
- */ focus() {
78
- this.children.first.focus();
79
- }
73
+ * An instance of the {@link module:utils/keystrokehandler~KeystrokeHandler}.
74
+ */ keystrokes;
80
75
  /**
81
- * @inheritDoc
82
- */ destroy() {
83
- super.destroy();
84
- this.focusTracker.destroy();
85
- this.keystrokes.destroy();
86
- }
76
+ * A collection of style {@link module:style/ui/stylegridbuttonview~StyleGridButtonView buttons}.
77
+ */ children;
87
78
  /**
88
- * Creates an instance of the {@link module:style/ui/stylegridview~StyleGridView} class.
89
- *
90
- * @param locale The localization services instance.
91
- * @param styleDefinitions Definitions of the styles.
92
- */ constructor(locale, styleDefinitions){
79
+ * Creates an instance of the {@link module:style/ui/stylegridview~StyleGridView} class.
80
+ *
81
+ * @param locale The localization services instance.
82
+ * @param styleDefinitions Definitions of the styles.
83
+ */ constructor(locale, styleDefinitions){
93
84
  super(locale);
94
85
  this.focusTracker = new FocusTracker();
95
86
  this.keystrokes = new KeystrokeHandler();
@@ -123,16 +114,55 @@ class StyleGridView extends View {
123
114
  children: this.children
124
115
  });
125
116
  }
117
+ /**
118
+ * @inheritDoc
119
+ */ render() {
120
+ super.render();
121
+ for (const child of this.children){
122
+ this.focusTracker.add(child.element);
123
+ }
124
+ addKeyboardHandlingForGrid({
125
+ keystrokeHandler: this.keystrokes,
126
+ focusTracker: this.focusTracker,
127
+ gridItems: this.children,
128
+ numberOfColumns: 3,
129
+ uiLanguageDirection: this.locale && this.locale.uiLanguageDirection
130
+ });
131
+ // Start listening for the keystrokes coming from the grid view.
132
+ this.keystrokes.listenTo(this.element);
133
+ }
134
+ /**
135
+ * Focuses the first style button in the grid.
136
+ */ focus() {
137
+ this.children.first.focus();
138
+ }
139
+ /**
140
+ * @inheritDoc
141
+ */ destroy() {
142
+ super.destroy();
143
+ this.focusTracker.destroy();
144
+ this.keystrokes.destroy();
145
+ }
126
146
  }
127
147
 
128
- class StyleGroupView extends View {
129
- /**
130
- * Creates an instance of the {@link module:style/ui/stylegroupview~StyleGroupView} class.
131
- *
132
- * @param locale The localization services instance.
133
- * @param label The localized label of the group.
134
- * @param styleDefinitions Definitions of the styles in the group.
135
- */ constructor(locale, label, styleDefinitions){
148
+ /**
149
+ * A class representing a group of styles (e.g. "block" or "inline").
150
+ *
151
+ * Renders a {@link module:style/ui/stylegridview~StyleGridView style grid} and a label.
152
+ */ class StyleGroupView extends View {
153
+ /**
154
+ * The styles grid of the group.
155
+ */ gridView;
156
+ /**
157
+ * The label of the group.
158
+ */ labelView;
159
+ /**
160
+ * Creates an instance of the {@link module:style/ui/stylegroupview~StyleGroupView} class.
161
+ *
162
+ * @param locale The localization services instance.
163
+ * @param label The localized label of the group.
164
+ * @param styleDefinitions Definitions of the styles in the group.
165
+ */ constructor(locale, label, styleDefinitions){
136
166
  super(locale);
137
167
  this.labelView = new LabelView(locale);
138
168
  this.labelView.text = label;
@@ -155,35 +185,37 @@ class StyleGroupView extends View {
155
185
  }
156
186
  }
157
187
 
158
- class StylePanelView extends View {
188
+ /**
189
+ * A class representing a panel with available content styles. It renders styles in button grids, grouped
190
+ * in categories.
191
+ */ class StylePanelView extends View {
159
192
  /**
160
- * @inheritDoc
161
- */ render() {
162
- super.render();
163
- // Register the views as focusable.
164
- this._focusables.add(this.blockStylesGroupView.gridView);
165
- this._focusables.add(this.inlineStylesGroupView.gridView);
166
- // Register the views in the focus tracker.
167
- this.focusTracker.add(this.blockStylesGroupView.gridView.element);
168
- this.focusTracker.add(this.inlineStylesGroupView.gridView.element);
169
- this.keystrokes.listenTo(this.element);
170
- }
193
+ * Tracks information about DOM focus in the panel.
194
+ */ focusTracker;
171
195
  /**
172
- * Focuses the first focusable element in the panel.
173
- */ focus() {
174
- this._focusCycler.focusFirst();
175
- }
196
+ * An instance of the {@link module:utils/keystrokehandler~KeystrokeHandler}.
197
+ */ keystrokes;
176
198
  /**
177
- * Focuses the last focusable element in the panel.
178
- */ focusLast() {
179
- this._focusCycler.focusLast();
180
- }
199
+ * A collection of panel children.
200
+ */ children;
181
201
  /**
182
- * Creates an instance of the {@link module:style/ui/stylegroupview~StyleGroupView} class.
183
- *
184
- * @param locale The localization services instance.
185
- * @param styleDefinitions Normalized definitions of the styles.
186
- */ constructor(locale, styleDefinitions){
202
+ * A view representing block styles group.
203
+ */ blockStylesGroupView;
204
+ /**
205
+ * A view representing inline styles group
206
+ */ inlineStylesGroupView;
207
+ /**
208
+ * A collection of views that can be focused in the panel.
209
+ */ _focusables;
210
+ /**
211
+ * Helps cycling over {@link #_focusables} in the panel.
212
+ */ _focusCycler;
213
+ /**
214
+ * Creates an instance of the {@link module:style/ui/stylegroupview~StyleGroupView} class.
215
+ *
216
+ * @param locale The localization services instance.
217
+ * @param styleDefinitions Normalized definitions of the styles.
218
+ */ constructor(locale, styleDefinitions){
187
219
  super(locale);
188
220
  const t = locale.t;
189
221
  this.focusTracker = new FocusTracker();
@@ -230,6 +262,28 @@ class StylePanelView extends View {
230
262
  children: this.children
231
263
  });
232
264
  }
265
+ /**
266
+ * @inheritDoc
267
+ */ render() {
268
+ super.render();
269
+ // Register the views as focusable.
270
+ this._focusables.add(this.blockStylesGroupView.gridView);
271
+ this._focusables.add(this.inlineStylesGroupView.gridView);
272
+ // Register the views in the focus tracker.
273
+ this.focusTracker.add(this.blockStylesGroupView.gridView.element);
274
+ this.focusTracker.add(this.inlineStylesGroupView.gridView.element);
275
+ this.keystrokes.listenTo(this.element);
276
+ }
277
+ /**
278
+ * Focuses the first focusable element in the panel.
279
+ */ focus() {
280
+ this._focusCycler.focusFirst();
281
+ }
282
+ /**
283
+ * Focuses the last focusable element in the panel.
284
+ */ focusLast() {
285
+ this._focusCycler.focusLast();
286
+ }
233
287
  }
234
288
 
235
289
  // These are intermediate element names that can't be rendered as style preview because they don't make sense standalone.
@@ -254,37 +308,51 @@ const NON_PREVIEWABLE_ELEMENT_NAMES = [
254
308
  'tr'
255
309
  ];
256
310
  class StyleUtils extends Plugin {
311
+ _htmlSupport;
257
312
  /**
258
- * @inheritDoc
259
- */ static get pluginName() {
313
+ * @inheritDoc
314
+ */ static get pluginName() {
260
315
  return 'StyleUtils';
261
316
  }
262
317
  /**
263
- * @inheritDoc
264
- */ init() {
318
+ * @inheritDoc
319
+ */ constructor(editor){
320
+ super(editor);
321
+ this.decorate('isStyleEnabledForBlock');
322
+ this.decorate('isStyleActiveForBlock');
323
+ this.decorate('getAffectedBlocks');
324
+ this.decorate('isStyleEnabledForInlineSelection');
325
+ this.decorate('isStyleActiveForInlineSelection');
326
+ this.decorate('getAffectedInlineSelectable');
327
+ this.decorate('getStylePreview');
328
+ this.decorate('configureGHSDataFilter');
329
+ }
330
+ /**
331
+ * @inheritDoc
332
+ */ init() {
265
333
  this._htmlSupport = this.editor.plugins.get('GeneralHtmlSupport');
266
334
  }
267
335
  /**
268
- * Normalizes {@link module:style/styleconfig~StyleConfig#definitions} in the configuration of the styles feature.
269
- * The structure of normalized styles looks as follows:
270
- *
271
- * ```ts
272
- * {
273
- * block: [
274
- * <module:style/style~StyleDefinition>,
275
- * <module:style/style~StyleDefinition>,
276
- * ...
277
- * ],
278
- * inline: [
279
- * <module:style/style~StyleDefinition>,
280
- * <module:style/style~StyleDefinition>,
281
- * ...
282
- * ]
283
- * }
284
- * ```
285
- *
286
- * @returns An object with normalized style definitions grouped into `block` and `inline` categories (arrays).
287
- */ normalizeConfig(dataSchema, styleDefinitions = []) {
336
+ * Normalizes {@link module:style/styleconfig~StyleConfig#definitions} in the configuration of the styles feature.
337
+ * The structure of normalized styles looks as follows:
338
+ *
339
+ * ```ts
340
+ * {
341
+ * block: [
342
+ * <module:style/style~StyleDefinition>,
343
+ * <module:style/style~StyleDefinition>,
344
+ * ...
345
+ * ],
346
+ * inline: [
347
+ * <module:style/style~StyleDefinition>,
348
+ * <module:style/style~StyleDefinition>,
349
+ * ...
350
+ * ]
351
+ * }
352
+ * ```
353
+ *
354
+ * @returns An object with normalized style definitions grouped into `block` and `inline` categories (arrays).
355
+ */ normalizeConfig(dataSchema, styleDefinitions = []) {
288
356
  const normalizedDefinitions = {
289
357
  block: [],
290
358
  inline: []
@@ -331,10 +399,10 @@ class StyleUtils extends Plugin {
331
399
  return normalizedDefinitions;
332
400
  }
333
401
  /**
334
- * Verifies if the given style is applicable to the provided block element.
335
- *
336
- * @internal
337
- */ isStyleEnabledForBlock(definition, block) {
402
+ * Verifies if the given style is applicable to the provided block element.
403
+ *
404
+ * @internal
405
+ */ isStyleEnabledForBlock(definition, block) {
338
406
  const model = this.editor.model;
339
407
  const attributeName = this._htmlSupport.getGhsAttributeNameForElement(definition.element);
340
408
  if (!model.schema.checkAttribute(block, attributeName)) {
@@ -343,19 +411,19 @@ class StyleUtils extends Plugin {
343
411
  return definition.modelElements.includes(block.name);
344
412
  }
345
413
  /**
346
- * Returns true if the given style is applied to the specified block element.
347
- *
348
- * @internal
349
- */ isStyleActiveForBlock(definition, block) {
414
+ * Returns true if the given style is applied to the specified block element.
415
+ *
416
+ * @internal
417
+ */ isStyleActiveForBlock(definition, block) {
350
418
  const attributeName = this._htmlSupport.getGhsAttributeNameForElement(definition.element);
351
419
  const ghsAttributeValue = block.getAttribute(attributeName);
352
420
  return this.hasAllClasses(ghsAttributeValue, definition.classes);
353
421
  }
354
422
  /**
355
- * Returns an array of block elements that style should be applied to.
356
- *
357
- * @internal
358
- */ getAffectedBlocks(definition, block) {
423
+ * Returns an array of block elements that style should be applied to.
424
+ *
425
+ * @internal
426
+ */ getAffectedBlocks(definition, block) {
359
427
  if (definition.modelElements.includes(block.name)) {
360
428
  return [
361
429
  block
@@ -364,10 +432,10 @@ class StyleUtils extends Plugin {
364
432
  return null;
365
433
  }
366
434
  /**
367
- * Verifies if the given style is applicable to the provided document selection.
368
- *
369
- * @internal
370
- */ isStyleEnabledForInlineSelection(definition, selection) {
435
+ * Verifies if the given style is applicable to the provided document selection.
436
+ *
437
+ * @internal
438
+ */ isStyleEnabledForInlineSelection(definition, selection) {
371
439
  const model = this.editor.model;
372
440
  for (const ghsAttributeName of definition.ghsAttributes){
373
441
  if (model.schema.checkAttributeInSelection(selection, ghsAttributeName)) {
@@ -377,10 +445,10 @@ class StyleUtils extends Plugin {
377
445
  return false;
378
446
  }
379
447
  /**
380
- * Returns true if the given style is applied to the specified document selection.
381
- *
382
- * @internal
383
- */ isStyleActiveForInlineSelection(definition, selection) {
448
+ * Returns true if the given style is applied to the specified document selection.
449
+ *
450
+ * @internal
451
+ */ isStyleActiveForInlineSelection(definition, selection) {
384
452
  for (const ghsAttributeName of definition.ghsAttributes){
385
453
  const ghsAttributeValue = this._getValueFromFirstAllowedNode(selection, ghsAttributeName);
386
454
  if (this.hasAllClasses(ghsAttributeValue, definition.classes)) {
@@ -390,17 +458,17 @@ class StyleUtils extends Plugin {
390
458
  return false;
391
459
  }
392
460
  /**
393
- * Returns a selectable that given style should be applied to.
394
- *
395
- * @internal
396
- */ getAffectedInlineSelectable(definition, selection) {
461
+ * Returns a selectable that given style should be applied to.
462
+ *
463
+ * @internal
464
+ */ getAffectedInlineSelectable(definition, selection) {
397
465
  return selection;
398
466
  }
399
467
  /**
400
- * Returns the `TemplateDefinition` used by styles dropdown to render style preview.
401
- *
402
- * @internal
403
- */ getStylePreview(definition, children) {
468
+ * Returns the `TemplateDefinition` used by styles dropdown to render style preview.
469
+ *
470
+ * @internal
471
+ */ getStylePreview(definition, children) {
404
472
  const { element, classes } = definition;
405
473
  return {
406
474
  tag: isPreviewable(element) ? element : 'div',
@@ -411,32 +479,32 @@ class StyleUtils extends Plugin {
411
479
  };
412
480
  }
413
481
  /**
414
- * Verifies if all classes are present in the given GHS attribute.
415
- *
416
- * @internal
417
- */ hasAllClasses(ghsAttributeValue, classes) {
482
+ * Verifies if all classes are present in the given GHS attribute.
483
+ *
484
+ * @internal
485
+ */ hasAllClasses(ghsAttributeValue, classes) {
418
486
  return isObject(ghsAttributeValue) && hasClassesProperty(ghsAttributeValue) && classes.every((className)=>ghsAttributeValue.classes.includes(className));
419
487
  }
420
488
  /**
421
- * This is where the styles feature configures the GHS feature. This method translates normalized
422
- * {@link module:style/styleconfig~StyleDefinition style definitions} to
423
- * {@link module:engine/view/matcher~MatcherObjectPattern matcher patterns} and feeds them to the GHS
424
- * {@link module:html-support/datafilter~DataFilter} plugin.
425
- *
426
- * @internal
427
- */ configureGHSDataFilter({ block, inline }) {
489
+ * This is where the styles feature configures the GHS feature. This method translates normalized
490
+ * {@link module:style/styleconfig~StyleDefinition style definitions} to
491
+ * {@link module:engine/view/matcher~MatcherObjectPattern matcher patterns} and feeds them to the GHS
492
+ * {@link module:html-support/datafilter~DataFilter} plugin.
493
+ *
494
+ * @internal
495
+ */ configureGHSDataFilter({ block, inline }) {
428
496
  const ghsDataFilter = this.editor.plugins.get('DataFilter');
429
497
  ghsDataFilter.loadAllowedConfig(block.map(normalizedStyleDefinitionToMatcherPattern));
430
498
  ghsDataFilter.loadAllowedConfig(inline.map(normalizedStyleDefinitionToMatcherPattern));
431
499
  }
432
500
  /**
433
- * Checks the attribute value of the first node in the selection that allows the attribute.
434
- * For the collapsed selection, returns the selection attribute.
435
- *
436
- * @param selection The document selection.
437
- * @param attributeName Name of the GHS attribute.
438
- * @returns The attribute value.
439
- */ _getValueFromFirstAllowedNode(selection, attributeName) {
501
+ * Checks the attribute value of the first node in the selection that allows the attribute.
502
+ * For the collapsed selection, returns the selection attribute.
503
+ *
504
+ * @param selection The document selection.
505
+ * @param attributeName Name of the GHS attribute.
506
+ * @returns The attribute value.
507
+ */ _getValueFromFirstAllowedNode(selection, attributeName) {
440
508
  const model = this.editor.model;
441
509
  const schema = model.schema;
442
510
  if (selection.isCollapsed) {
@@ -451,19 +519,6 @@ class StyleUtils extends Plugin {
451
519
  }
452
520
  return null;
453
521
  }
454
- /**
455
- * @inheritDoc
456
- */ constructor(editor){
457
- super(editor);
458
- this.decorate('isStyleEnabledForBlock');
459
- this.decorate('isStyleActiveForBlock');
460
- this.decorate('getAffectedBlocks');
461
- this.decorate('isStyleEnabledForInlineSelection');
462
- this.decorate('isStyleActiveForInlineSelection');
463
- this.decorate('getAffectedInlineSelectable');
464
- this.decorate('getStylePreview');
465
- this.decorate('configureGHSDataFilter');
466
- }
467
522
  }
468
523
  /**
469
524
  * Checks if given object has `classes` property which is an array.
@@ -490,22 +545,27 @@ class StyleUtils extends Plugin {
490
545
  };
491
546
  }
492
547
 
493
- class StyleUI extends Plugin {
548
+ /**
549
+ * The UI plugin of the style feature .
550
+ *
551
+ * It registers the `'style'` UI dropdown in the editor's {@link module:ui/componentfactory~ComponentFactory component factory}
552
+ * that displays a grid of styles and allows changing styles of the content.
553
+ */ class StyleUI extends Plugin {
494
554
  /**
495
- * @inheritDoc
496
- */ static get pluginName() {
555
+ * @inheritDoc
556
+ */ static get pluginName() {
497
557
  return 'StyleUI';
498
558
  }
499
559
  /**
500
- * @inheritDoc
501
- */ static get requires() {
560
+ * @inheritDoc
561
+ */ static get requires() {
502
562
  return [
503
563
  StyleUtils
504
564
  ];
505
565
  }
506
566
  /**
507
- * @inheritDoc
508
- */ init() {
567
+ * @inheritDoc
568
+ */ init() {
509
569
  const editor = this.editor;
510
570
  const dataSchema = editor.plugins.get('DataSchema');
511
571
  const styleUtils = editor.plugins.get('StyleUtils');
@@ -566,10 +626,32 @@ class StyleUI extends Plugin {
566
626
  }
567
627
  }
568
628
 
569
- class StyleCommand extends Command {
629
+ /**
630
+ * Style command.
631
+ *
632
+ * Applies and removes styles from selection and elements.
633
+ */ class StyleCommand extends Command {
570
634
  /**
571
- * @inheritDoc
572
- */ refresh() {
635
+ * Normalized definitions of the styles.
636
+ */ _styleDefinitions;
637
+ /**
638
+ * The StyleUtils plugin.
639
+ */ _styleUtils;
640
+ /**
641
+ * Creates an instance of the command.
642
+ *
643
+ * @param editor Editor on which this command will be used.
644
+ * @param styleDefinitions Normalized definitions of the styles.
645
+ */ constructor(editor, styleDefinitions){
646
+ super(editor);
647
+ this.set('value', []);
648
+ this.set('enabledStyles', []);
649
+ this._styleDefinitions = styleDefinitions;
650
+ this._styleUtils = this.editor.plugins.get(StyleUtils);
651
+ }
652
+ /**
653
+ * @inheritDoc
654
+ */ refresh() {
573
655
  const model = this.editor.model;
574
656
  const selection = model.document.selection;
575
657
  const value = new Set();
@@ -619,41 +701,41 @@ class StyleCommand extends Command {
619
701
  this.value = this.isEnabled ? Array.from(value).sort() : [];
620
702
  }
621
703
  /**
622
- * Executes the command &ndash; applies the style classes to the selection or removes it from the selection.
623
- *
624
- * If the command value already contains the requested style, it will remove the style classes. Otherwise, it will set it.
625
- *
626
- * The execution result differs, depending on the {@link module:engine/model/document~Document#selection} and the
627
- * style type (inline or block):
628
- *
629
- * * When applying inline styles:
630
- * * If the selection is on a range, the command applies the style classes to all nodes in that range.
631
- * * If the selection is collapsed in a non-empty node, the command applies the style classes to the
632
- * {@link module:engine/model/document~Document#selection}.
633
- *
634
- * * When applying block styles:
635
- * * If the selection is on a range, the command applies the style classes to the nearest block parent element.
636
- *
637
- * @fires execute
638
- * @param options Command options.
639
- * @param options.styleName Style name matching the one defined in the
640
- * {@link module:style/styleconfig~StyleConfig#definitions configuration}.
641
- * @param options.forceValue Whether the command should add given style (`true`) or remove it (`false`) from the selection.
642
- * If not set (default), the command will toggle the style basing on the first selected node. Note, that this will not force
643
- * setting a style on an element that cannot receive given style.
644
- */ execute({ styleName, forceValue }) {
704
+ * Executes the command &ndash; applies the style classes to the selection or removes it from the selection.
705
+ *
706
+ * If the command value already contains the requested style, it will remove the style classes. Otherwise, it will set it.
707
+ *
708
+ * The execution result differs, depending on the {@link module:engine/model/document~Document#selection} and the
709
+ * style type (inline or block):
710
+ *
711
+ * * When applying inline styles:
712
+ * * If the selection is on a range, the command applies the style classes to all nodes in that range.
713
+ * * If the selection is collapsed in a non-empty node, the command applies the style classes to the
714
+ * {@link module:engine/model/document~Document#selection}.
715
+ *
716
+ * * When applying block styles:
717
+ * * If the selection is on a range, the command applies the style classes to the nearest block parent element.
718
+ *
719
+ * @fires execute
720
+ * @param options Command options.
721
+ * @param options.styleName Style name matching the one defined in the
722
+ * {@link module:style/styleconfig~StyleConfig#definitions configuration}.
723
+ * @param options.forceValue Whether the command should add given style (`true`) or remove it (`false`) from the selection.
724
+ * If not set (default), the command will toggle the style basing on the first selected node. Note, that this will not force
725
+ * setting a style on an element that cannot receive given style.
726
+ */ execute({ styleName, forceValue }) {
645
727
  if (!this.enabledStyles.includes(styleName)) {
646
728
  /**
647
- * Style command can be executed only with a correct style name.
648
- *
649
- * This warning may be caused by:
650
- *
651
- * * passing a name that is not specified in the {@link module:style/styleconfig~StyleConfig#definitions configuration}
652
- * (e.g. a CSS class name),
653
- * * when trying to apply a style that is not allowed on a given element.
654
- *
655
- * @error style-command-executed-with-incorrect-style-name
656
- */ logWarning('style-command-executed-with-incorrect-style-name');
729
+ * Style command can be executed only with a correct style name.
730
+ *
731
+ * This warning may be caused by:
732
+ *
733
+ * * passing a name that is not specified in the {@link module:style/styleconfig~StyleConfig#definitions configuration}
734
+ * (e.g. a CSS class name),
735
+ * * when trying to apply a style that is not allowed on a given element.
736
+ *
737
+ * @error style-command-executed-with-incorrect-style-name
738
+ */ logWarning('style-command-executed-with-incorrect-style-name');
657
739
  return;
658
740
  }
659
741
  const model = this.editor.model;
@@ -685,8 +767,8 @@ class StyleCommand extends Command {
685
767
  });
686
768
  }
687
769
  /**
688
- * Returns a set of elements that should be affected by the block-style change.
689
- */ _findAffectedBlocks(selectedBlocks, definition) {
770
+ * Returns a set of elements that should be affected by the block-style change.
771
+ */ _findAffectedBlocks(selectedBlocks, definition) {
690
772
  const blocks = new Set();
691
773
  for (const selectedBlock of selectedBlocks){
692
774
  const ancestorBlocks = selectedBlock.getAncestors({
@@ -708,18 +790,6 @@ class StyleCommand extends Command {
708
790
  }
709
791
  return blocks;
710
792
  }
711
- /**
712
- * Creates an instance of the command.
713
- *
714
- * @param editor Editor on which this command will be used.
715
- * @param styleDefinitions Normalized definitions of the styles.
716
- */ constructor(editor, styleDefinitions){
717
- super(editor);
718
- this.set('value', []);
719
- this.set('enabledStyles', []);
720
- this._styleDefinitions = styleDefinitions;
721
- this._styleUtils = this.editor.plugins.get(StyleUtils);
722
- }
723
793
  }
724
794
  /**
725
795
  * Returns classes that are defined only in the supplied definition and not in any other active definition. It's used
@@ -756,22 +826,25 @@ class StyleCommand extends Command {
756
826
  }
757
827
 
758
828
  class ListStyleSupport extends Plugin {
829
+ _listUtils;
830
+ _styleUtils;
831
+ _htmlSupport;
759
832
  /**
760
- * @inheritDoc
761
- */ static get pluginName() {
833
+ * @inheritDoc
834
+ */ static get pluginName() {
762
835
  return 'ListStyleSupport';
763
836
  }
764
837
  /**
765
- * @inheritDoc
766
- */ static get requires() {
838
+ * @inheritDoc
839
+ */ static get requires() {
767
840
  return [
768
841
  StyleUtils,
769
842
  'GeneralHtmlSupport'
770
843
  ];
771
844
  }
772
845
  /**
773
- * @inheritDoc
774
- */ init() {
846
+ * @inheritDoc
847
+ */ init() {
775
848
  const editor = this.editor;
776
849
  if (!editor.plugins.has('ListEditing')) {
777
850
  return;
@@ -815,8 +888,8 @@ class ListStyleSupport extends Plugin {
815
888
  });
816
889
  }
817
890
  /**
818
- * Verifies if the given style is applicable to the provided block element.
819
- */ _isStyleEnabledForBlock(definition, block) {
891
+ * Verifies if the given style is applicable to the provided block element.
892
+ */ _isStyleEnabledForBlock(definition, block) {
820
893
  const model = this.editor.model;
821
894
  if (![
822
895
  'ol',
@@ -841,15 +914,15 @@ class ListStyleSupport extends Plugin {
841
914
  }
842
915
  }
843
916
  /**
844
- * Returns true if the given style is applied to the specified block element.
845
- */ _isStyleActiveForBlock(definition, block) {
917
+ * Returns true if the given style is applied to the specified block element.
918
+ */ _isStyleActiveForBlock(definition, block) {
846
919
  const attributeName = this._htmlSupport.getGhsAttributeNameForElement(definition.element);
847
920
  const ghsAttributeValue = block.getAttribute(attributeName);
848
921
  return this._styleUtils.hasAllClasses(ghsAttributeValue, definition.classes);
849
922
  }
850
923
  /**
851
- * Returns an array of block elements that style should be applied to.
852
- */ _getAffectedBlocks(definition, block) {
924
+ * Returns an array of block elements that style should be applied to.
925
+ */ _getAffectedBlocks(definition, block) {
853
926
  if (!this._isStyleEnabledForBlock(definition, block)) {
854
927
  return null;
855
928
  }
@@ -862,8 +935,8 @@ class ListStyleSupport extends Plugin {
862
935
  }
863
936
  }
864
937
  /**
865
- * Returns a view template definition for the style preview.
866
- */ _getStylePreview(definition, children) {
938
+ * Returns a view template definition for the style preview.
939
+ */ _getStylePreview(definition, children) {
867
940
  const { element, classes } = definition;
868
941
  if (element == 'ol' || element == 'ul') {
869
942
  return {
@@ -897,21 +970,23 @@ class ListStyleSupport extends Plugin {
897
970
  }
898
971
 
899
972
  class TableStyleSupport extends Plugin {
973
+ _tableUtils;
974
+ _styleUtils;
900
975
  /**
901
- * @inheritDoc
902
- */ static get pluginName() {
976
+ * @inheritDoc
977
+ */ static get pluginName() {
903
978
  return 'TableStyleSupport';
904
979
  }
905
980
  /**
906
- * @inheritDoc
907
- */ static get requires() {
981
+ * @inheritDoc
982
+ */ static get requires() {
908
983
  return [
909
984
  StyleUtils
910
985
  ];
911
986
  }
912
987
  /**
913
- * @inheritDoc
914
- */ init() {
988
+ * @inheritDoc
989
+ */ init() {
915
990
  const editor = this.editor;
916
991
  if (!editor.plugins.has('TableEditing')) {
917
992
  return;
@@ -943,12 +1018,12 @@ class TableStyleSupport extends Plugin {
943
1018
  });
944
1019
  }
945
1020
  /**
946
- * Checks if this plugin's custom logic should be applied for defintion-block pair.
947
- *
948
- * @param definition Style definition that is being considered.
949
- * @param block Block element to check if should be styled.
950
- * @returns True if the defintion-block pair meet the plugin criteria, false otherwise.
951
- */ _isApplicable(definition, block) {
1021
+ * Checks if this plugin's custom logic should be applied for defintion-block pair.
1022
+ *
1023
+ * @param definition Style definition that is being considered.
1024
+ * @param block Block element to check if should be styled.
1025
+ * @returns True if the defintion-block pair meet the plugin criteria, false otherwise.
1026
+ */ _isApplicable(definition, block) {
952
1027
  if ([
953
1028
  'td',
954
1029
  'th'
@@ -964,12 +1039,12 @@ class TableStyleSupport extends Plugin {
964
1039
  return false;
965
1040
  }
966
1041
  /**
967
- * Checks if the style definition should be applied to selected block.
968
- *
969
- * @param definition Style definition that is being considered.
970
- * @param block Block element to check if should be styled.
971
- * @returns True if the block should be style with the style description, false otherwise.
972
- */ _isStyleEnabledForBlock(definition, block) {
1042
+ * Checks if the style definition should be applied to selected block.
1043
+ *
1044
+ * @param definition Style definition that is being considered.
1045
+ * @param block Block element to check if should be styled.
1046
+ * @returns True if the block should be style with the style description, false otherwise.
1047
+ */ _isStyleEnabledForBlock(definition, block) {
973
1048
  if ([
974
1049
  'td',
975
1050
  'th'
@@ -1000,12 +1075,12 @@ class TableStyleSupport extends Plugin {
1000
1075
  /* istanbul ignore next -- @preserve */ return false;
1001
1076
  }
1002
1077
  /**
1003
- * Gets all blocks that the style should be applied to.
1004
- *
1005
- * @param definition Style definition that is being considered.
1006
- * @param block A block element from selection.
1007
- * @returns An array with the block that was passed as an argument if meets the criteria, null otherwise.
1008
- */ _getAffectedBlocks(definition, block) {
1078
+ * Gets all blocks that the style should be applied to.
1079
+ *
1080
+ * @param definition Style definition that is being considered.
1081
+ * @param block A block element from selection.
1082
+ * @returns An array with the block that was passed as an argument if meets the criteria, null otherwise.
1083
+ */ _getAffectedBlocks(definition, block) {
1009
1084
  if (!this._isStyleEnabledForBlock(definition, block)) {
1010
1085
  return null;
1011
1086
  }
@@ -1016,22 +1091,24 @@ class TableStyleSupport extends Plugin {
1016
1091
  }
1017
1092
 
1018
1093
  class LinkStyleSupport extends Plugin {
1094
+ _styleUtils;
1095
+ _htmlSupport;
1019
1096
  /**
1020
- * @inheritDoc
1021
- */ static get pluginName() {
1097
+ * @inheritDoc
1098
+ */ static get pluginName() {
1022
1099
  return 'LinkStyleSupport';
1023
1100
  }
1024
1101
  /**
1025
- * @inheritDoc
1026
- */ static get requires() {
1102
+ * @inheritDoc
1103
+ */ static get requires() {
1027
1104
  return [
1028
1105
  StyleUtils,
1029
1106
  'GeneralHtmlSupport'
1030
1107
  ];
1031
1108
  }
1032
1109
  /**
1033
- * @inheritDoc
1034
- */ init() {
1110
+ * @inheritDoc
1111
+ */ init() {
1035
1112
  const editor = this.editor;
1036
1113
  if (!editor.plugins.has('LinkEditing')) {
1037
1114
  return;
@@ -1068,8 +1145,8 @@ class LinkStyleSupport extends Plugin {
1068
1145
  });
1069
1146
  }
1070
1147
  /**
1071
- * Verifies if the given style is applicable to the provided document selection.
1072
- */ _isStyleEnabled(definition, selection) {
1148
+ * Verifies if the given style is applicable to the provided document selection.
1149
+ */ _isStyleEnabled(definition, selection) {
1073
1150
  const model = this.editor.model;
1074
1151
  // Handle collapsed selection.
1075
1152
  if (selection.isCollapsed) {
@@ -1086,8 +1163,8 @@ class LinkStyleSupport extends Plugin {
1086
1163
  return false;
1087
1164
  }
1088
1165
  /**
1089
- * Returns true if the given style is applied to the specified document selection.
1090
- */ _isStyleActive(definition, selection) {
1166
+ * Returns true if the given style is applied to the specified document selection.
1167
+ */ _isStyleActive(definition, selection) {
1091
1168
  const model = this.editor.model;
1092
1169
  const attributeName = this._htmlSupport.getGhsAttributeNameForElement(definition.element);
1093
1170
  // Handle collapsed selection.
@@ -1112,8 +1189,8 @@ class LinkStyleSupport extends Plugin {
1112
1189
  return false;
1113
1190
  }
1114
1191
  /**
1115
- * Returns a selectable that given style should be applied to.
1116
- */ _getAffectedSelectable(definition, selection) {
1192
+ * Returns a selectable that given style should be applied to.
1193
+ */ _getAffectedSelectable(definition, selection) {
1117
1194
  const model = this.editor.model;
1118
1195
  // Handle collapsed selection.
1119
1196
  if (selection.isCollapsed) {
@@ -1163,15 +1240,21 @@ class LinkStyleSupport extends Plugin {
1163
1240
  return ranges;
1164
1241
  }
1165
1242
 
1166
- class StyleEditing extends Plugin {
1243
+ /**
1244
+ * The style engine feature.
1245
+ *
1246
+ * It configures the {@glink features/html/general-html-support General HTML Support feature} based on
1247
+ * {@link module:style/styleconfig~StyleConfig#definitions configured style definitions} and introduces the
1248
+ * {@link module:style/stylecommand~StyleCommand style command} that applies styles to the content of the document.
1249
+ */ class StyleEditing extends Plugin {
1167
1250
  /**
1168
- * @inheritDoc
1169
- */ static get pluginName() {
1251
+ * @inheritDoc
1252
+ */ static get pluginName() {
1170
1253
  return 'StyleEditing';
1171
1254
  }
1172
1255
  /**
1173
- * @inheritDoc
1174
- */ static get requires() {
1256
+ * @inheritDoc
1257
+ */ static get requires() {
1175
1258
  return [
1176
1259
  'GeneralHtmlSupport',
1177
1260
  StyleUtils,
@@ -1181,8 +1264,8 @@ class StyleEditing extends Plugin {
1181
1264
  ];
1182
1265
  }
1183
1266
  /**
1184
- * @inheritDoc
1185
- */ init() {
1267
+ * @inheritDoc
1268
+ */ init() {
1186
1269
  const editor = this.editor;
1187
1270
  const dataSchema = editor.plugins.get('DataSchema');
1188
1271
  const styleUtils = editor.plugins.get('StyleUtils');
@@ -1193,15 +1276,20 @@ class StyleEditing extends Plugin {
1193
1276
  }
1194
1277
  }
1195
1278
 
1196
- class Style extends Plugin {
1279
+ /**
1280
+ * The style plugin.
1281
+ *
1282
+ * This is a "glue" plugin that loads the {@link module:style/styleediting~StyleEditing style editing feature}
1283
+ * and {@link module:style/styleui~StyleUI style UI feature}.
1284
+ */ class Style extends Plugin {
1197
1285
  /**
1198
- * @inheritDoc
1199
- */ static get pluginName() {
1286
+ * @inheritDoc
1287
+ */ static get pluginName() {
1200
1288
  return 'Style';
1201
1289
  }
1202
1290
  /**
1203
- * @inheritDoc
1204
- */ static get requires() {
1291
+ * @inheritDoc
1292
+ */ static get requires() {
1205
1293
  return [
1206
1294
  StyleEditing,
1207
1295
  StyleUI