-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
styleutils.ts
350 lines (296 loc) · 10.8 KB
/
styleutils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module style/styleutils
*/
import { Plugin, type Editor } from 'ckeditor5/src/core.js';
import type { Element, MatcherObjectPattern, DocumentSelection, Selectable } from 'ckeditor5/src/engine.js';
import type { DecoratedMethodEvent } from 'ckeditor5/src/utils.js';
import type { TemplateDefinition } from 'ckeditor5/src/ui.js';
import type { DataFilter, DataSchema, GeneralHtmlSupport, DataSchemaBlockElementDefinition } from '@ckeditor/ckeditor5-html-support';
import type { StyleDefinition } from './styleconfig.js';
import { isObject } from 'lodash-es';
// These are intermediate element names that can't be rendered as style preview because they don't make sense standalone.
const NON_PREVIEWABLE_ELEMENT_NAMES = [
'caption', 'colgroup', 'dd', 'dt', 'figcaption', 'legend', 'li', 'optgroup', 'option', 'rp',
'rt', 'summary', 'tbody', 'td', 'tfoot', 'th', 'thead', 'tr'
];
export default class StyleUtils extends Plugin {
private _htmlSupport!: GeneralHtmlSupport;
/**
* @inheritDoc
*/
public static get pluginName() {
return 'StyleUtils' as const;
}
/**
* @inheritDoc
*/
constructor( editor: Editor ) {
super( editor );
this.decorate( 'isStyleEnabledForBlock' );
this.decorate( 'isStyleActiveForBlock' );
this.decorate( 'getAffectedBlocks' );
this.decorate( 'isStyleEnabledForInlineSelection' );
this.decorate( 'isStyleActiveForInlineSelection' );
this.decorate( 'getAffectedInlineSelectable' );
this.decorate( 'getStylePreview' );
this.decorate( 'configureGHSDataFilter' );
}
/**
* @inheritDoc
*/
public init(): void {
this._htmlSupport = this.editor.plugins.get( 'GeneralHtmlSupport' );
}
/**
* Normalizes {@link module:style/styleconfig~StyleConfig#definitions} in the configuration of the styles feature.
* The structure of normalized styles looks as follows:
*
* ```ts
* {
* block: [
* <module:style/style~StyleDefinition>,
* <module:style/style~StyleDefinition>,
* ...
* ],
* inline: [
* <module:style/style~StyleDefinition>,
* <module:style/style~StyleDefinition>,
* ...
* ]
* }
* ```
*
* @returns An object with normalized style definitions grouped into `block` and `inline` categories (arrays).
*/
public normalizeConfig(
dataSchema: DataSchema,
styleDefinitions: Array<StyleDefinition> = []
): NormalizedStyleDefinitions {
const normalizedDefinitions: NormalizedStyleDefinitions = {
block: [],
inline: []
};
for ( const definition of styleDefinitions ) {
const modelElements: Array<string> = [];
const ghsAttributes: Array<string> = [];
for ( const ghsDefinition of dataSchema.getDefinitionsForView( definition.element ) ) {
const appliesToBlock = 'appliesToBlock' in ghsDefinition ? ghsDefinition.appliesToBlock : false;
if ( ghsDefinition.isBlock || appliesToBlock ) {
if ( typeof appliesToBlock == 'string' ) {
modelElements.push( appliesToBlock );
} else if ( ghsDefinition.isBlock ) {
const ghsBlockDefinition: DataSchemaBlockElementDefinition = ghsDefinition;
modelElements.push( ghsDefinition.model );
if ( ghsBlockDefinition.paragraphLikeModel ) {
modelElements.push( ghsBlockDefinition.paragraphLikeModel );
}
}
} else {
ghsAttributes.push( ghsDefinition.model );
}
}
const previewTemplate = this.getStylePreview( definition, [
{ text: 'AaBbCcDdEeFfGgHhIiJj' }
] );
if ( modelElements.length ) {
normalizedDefinitions.block.push( {
...definition,
previewTemplate,
modelElements,
isBlock: true
} );
} else {
normalizedDefinitions.inline.push( {
...definition,
previewTemplate,
ghsAttributes
} );
}
}
return normalizedDefinitions;
}
/**
* Verifies if the given style is applicable to the provided block element.
*
* @internal
*/
public isStyleEnabledForBlock( definition: BlockStyleDefinition, block: Element ): boolean {
const model = this.editor.model;
const attributeName = this._htmlSupport.getGhsAttributeNameForElement( definition.element );
if ( !model.schema.checkAttribute( block, attributeName ) ) {
return false;
}
return definition.modelElements.includes( block.name );
}
/**
* Returns true if the given style is applied to the specified block element.
*
* @internal
*/
public isStyleActiveForBlock( definition: BlockStyleDefinition, block: Element ): boolean {
const attributeName = this._htmlSupport.getGhsAttributeNameForElement( definition.element );
const ghsAttributeValue = block.getAttribute( attributeName );
return this.hasAllClasses( ghsAttributeValue, definition.classes );
}
/**
* Returns an array of block elements that style should be applied to.
*
* @internal
*/
public getAffectedBlocks( definition: BlockStyleDefinition, block: Element ): Array<Element> | null {
if ( definition.modelElements.includes( block.name ) ) {
return [ block ];
}
return null;
}
/**
* Verifies if the given style is applicable to the provided document selection.
*
* @internal
*/
public isStyleEnabledForInlineSelection( definition: InlineStyleDefinition, selection: DocumentSelection ): boolean {
const model = this.editor.model;
for ( const ghsAttributeName of definition.ghsAttributes ) {
if ( model.schema.checkAttributeInSelection( selection, ghsAttributeName ) ) {
return true;
}
}
return false;
}
/**
* Returns true if the given style is applied to the specified document selection.
*
* @internal
*/
public isStyleActiveForInlineSelection( definition: InlineStyleDefinition, selection: DocumentSelection ): boolean {
for ( const ghsAttributeName of definition.ghsAttributes ) {
const ghsAttributeValue = this._getValueFromFirstAllowedNode( selection, ghsAttributeName );
if ( this.hasAllClasses( ghsAttributeValue, definition.classes ) ) {
return true;
}
}
return false;
}
/**
* Returns a selectable that given style should be applied to.
*
* @internal
*/
public getAffectedInlineSelectable( definition: InlineStyleDefinition, selection: DocumentSelection ): Selectable {
return selection;
}
/**
* Returns the `TemplateDefinition` used by styles dropdown to render style preview.
*
* @internal
*/
public getStylePreview( definition: StyleDefinition, children: Iterable<TemplateDefinition> ): TemplateDefinition {
const { element, classes } = definition;
return {
tag: isPreviewable( element ) ? element : 'div',
attributes: {
class: classes
},
children
};
}
/**
* Verifies if all classes are present in the given GHS attribute.
*
* @internal
*/
public hasAllClasses( ghsAttributeValue: unknown, classes: Array<string> ): boolean {
return isObject( ghsAttributeValue ) &&
hasClassesProperty( ghsAttributeValue ) &&
classes.every( className => ghsAttributeValue.classes.includes( className ) );
}
/**
* This is where the styles feature configures the GHS feature. This method translates normalized
* {@link module:style/styleconfig~StyleDefinition style definitions} to
* {@link module:engine/view/matcher~MatcherObjectPattern matcher patterns} and feeds them to the GHS
* {@link module:html-support/datafilter~DataFilter} plugin.
*
* @internal
*/
public configureGHSDataFilter( { block, inline }: NormalizedStyleDefinitions ): void {
const ghsDataFilter: DataFilter = this.editor.plugins.get( 'DataFilter' );
ghsDataFilter.loadAllowedConfig( block.map( normalizedStyleDefinitionToMatcherPattern ) );
ghsDataFilter.loadAllowedConfig( inline.map( normalizedStyleDefinitionToMatcherPattern ) );
}
/**
* Checks the attribute value of the first node in the selection that allows the attribute.
* For the collapsed selection, returns the selection attribute.
*
* @param selection The document selection.
* @param attributeName Name of the GHS attribute.
* @returns The attribute value.
*/
private _getValueFromFirstAllowedNode( selection: DocumentSelection, attributeName: string ): unknown | null {
const model = this.editor.model;
const schema = model.schema;
if ( selection.isCollapsed ) {
return selection.getAttribute( attributeName );
}
for ( const range of selection.getRanges() ) {
for ( const item of range.getItems() ) {
if ( schema.checkAttribute( item, attributeName ) ) {
return item.getAttribute( attributeName );
}
}
}
return null;
}
}
/**
* Checks if given object has `classes` property which is an array.
*
* @param obj Object to check.
*/
function hasClassesProperty<T extends { classes?: Array<unknown> }>( obj: T ): obj is T & { classes: Array<unknown> } {
return Boolean( obj.classes ) && Array.isArray( obj.classes );
}
/**
* Decides whether an element should be created in the preview or a substitute `<div>` should
* be used instead. This avoids previewing a standalone `<td>`, `<li>`, etc. without a parent.
*
* @param elementName Name of the element
* @returns Boolean indicating whether the element can be rendered.
*/
function isPreviewable( elementName: string ): boolean {
return !NON_PREVIEWABLE_ELEMENT_NAMES.includes( elementName );
}
/**
* Translates a normalized style definition to a view matcher pattern.
*/
function normalizedStyleDefinitionToMatcherPattern( { element, classes }: StyleDefinition ): MatcherObjectPattern {
return {
name: element,
classes
};
}
export interface NormalizedStyleDefinitions {
block: Array<BlockStyleDefinition>;
inline: Array<InlineStyleDefinition>;
}
export interface BlockStyleDefinition extends StyleDefinition {
isBlock: true;
modelElements: Array<string>;
previewTemplate: TemplateDefinition;
}
export interface InlineStyleDefinition extends StyleDefinition {
ghsAttributes: Array<string>;
previewTemplate: TemplateDefinition;
}
export type NormalizedStyleDefinition = BlockStyleDefinition | InlineStyleDefinition;
export type StyleUtilsIsEnabledForBlockEvent = DecoratedMethodEvent<StyleUtils, 'isStyleEnabledForBlock'>;
export type StyleUtilsIsActiveForBlockEvent = DecoratedMethodEvent<StyleUtils, 'isStyleActiveForBlock'>;
export type StyleUtilsGetAffectedBlocksEvent = DecoratedMethodEvent<StyleUtils, 'getAffectedBlocks'>;
export type StyleUtilsIsStyleEnabledForInlineSelectionEvent = DecoratedMethodEvent<StyleUtils, 'isStyleEnabledForInlineSelection'>;
export type StyleUtilsIsStyleActiveForInlineSelectionEvent = DecoratedMethodEvent<StyleUtils, 'isStyleActiveForInlineSelection'>;
export type StyleUtilsGetAffectedInlineSelectableEvent = DecoratedMethodEvent<StyleUtils, 'getAffectedInlineSelectable'>;
export type StyleUtilsGetStylePreviewEvent = DecoratedMethodEvent<StyleUtils, 'getStylePreview'>;
export type StyleUtilsConfigureGHSDataFilterEvent = DecoratedMethodEvent<StyleUtils, 'configureGHSDataFilter'>;