/
conversion.ts
720 lines (691 loc) · 24.1 KB
/
conversion.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/**
* @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 engine/conversion/conversion
*/
import {
CKEditorError,
toArray,
type ArrayOrItem,
type PriorityString
} from '@ckeditor/ckeditor5-utils';
import UpcastHelpers from './upcasthelpers.js';
import DowncastHelpers, {
type AttributeCreatorFunction,
type AttributeDescriptor
} from './downcasthelpers.js';
import type DowncastDispatcher from './downcastdispatcher.js';
import type UpcastDispatcher from './upcastdispatcher.js';
import type ElementDefinition from '../view/elementdefinition.js';
import type { MatcherPattern } from '../view/matcher.js';
/**
* A utility class that helps add converters to upcast and downcast dispatchers.
*
* We recommend reading the {@glink framework/deep-dive/conversion/intro editor conversion} guide first to
* understand the core concepts of the conversion mechanisms.
*
* An instance of the conversion manager is available in the
* {@link module:core/editor/editor~Editor#conversion `editor.conversion`} property
* and by default has the following groups of dispatchers (i.e. directions of conversion):
*
* * `downcast` (editing and data downcasts)
* * `editingDowncast`
* * `dataDowncast`
* * `upcast`
*
* # One-way converters
*
* To add a converter to a specific group, use the {@link module:engine/conversion/conversion~Conversion#for `for()`}
* method:
*
* ```ts
* // Add a converter to editing downcast and data downcast.
* editor.conversion.for( 'downcast' ).elementToElement( config ) );
*
* // Add a converter to the data pipepline only:
* editor.conversion.for( 'dataDowncast' ).elementToElement( dataConversionConfig ) );
*
* // And a slightly different one for the editing pipeline:
* editor.conversion.for( 'editingDowncast' ).elementToElement( editingConversionConfig ) );
* ```
*
* See {@link module:engine/conversion/conversion~Conversion#for `for()`} method documentation to learn more about
* available conversion helpers and how to use your custom ones.
*
* # Two-way converters
*
* Besides using one-way converters via the `for()` method, you can also use other methods available in this
* class to add two-way converters (upcast and downcast):
*
* * {@link module:engine/conversion/conversion~Conversion#elementToElement `elementToElement()`} –
* Model element to view element and vice versa.
* * {@link module:engine/conversion/conversion~Conversion#attributeToElement `attributeToElement()`} –
* Model attribute to view element and vice versa.
* * {@link module:engine/conversion/conversion~Conversion#attributeToAttribute `attributeToAttribute()`} –
* Model attribute to view attribute and vice versa.
*/
export default class Conversion {
/**
* Maps dispatchers group name to ConversionHelpers instances.
*/
private readonly _helpers = new Map<string, DowncastHelpers | UpcastHelpers>();
private readonly _downcast: Array<DowncastDispatcher>;
private readonly _upcast: Array<UpcastDispatcher>;
/**
* Creates a new conversion instance.
*/
constructor(
downcastDispatchers: ArrayOrItem<DowncastDispatcher>,
upcastDispatchers: ArrayOrItem<UpcastDispatcher>
) {
// Define default 'downcast' & 'upcast' dispatchers groups. Those groups are always available as two-way converters needs them.
this._downcast = toArray( downcastDispatchers );
this._createConversionHelpers( { name: 'downcast', dispatchers: this._downcast, isDowncast: true } );
this._upcast = toArray( upcastDispatchers );
this._createConversionHelpers( { name: 'upcast', dispatchers: this._upcast, isDowncast: false } );
}
public addAlias(
alias: `${ string }Downcast`,
dispatcher: DowncastDispatcher
): void;
public addAlias(
alias: `${ string }Upcast`,
dispatcher: UpcastDispatcher
): void;
public addAlias(
alias: string,
dispatcher: DowncastDispatcher | UpcastDispatcher
): void;
/**
* Define an alias for registered dispatcher.
*
* ```ts
* const conversion = new Conversion(
* [ dataDowncastDispatcher, editingDowncastDispatcher ],
* upcastDispatcher
* );
*
* conversion.addAlias( 'dataDowncast', dataDowncastDispatcher );
* ```
*
* @param alias An alias of a dispatcher.
* @param dispatcher Dispatcher which should have an alias.
*/
public addAlias(
alias: string,
dispatcher: DowncastDispatcher | UpcastDispatcher
): void {
const isDowncast = this._downcast.includes( dispatcher as any );
const isUpcast = this._upcast.includes( dispatcher as any );
if ( !isUpcast && !isDowncast ) {
/**
* Trying to register an alias for a dispatcher that nas not been registered.
*
* @error conversion-add-alias-dispatcher-not-registered
*/
throw new CKEditorError(
'conversion-add-alias-dispatcher-not-registered',
this
);
}
this._createConversionHelpers( { name: alias, dispatchers: [ dispatcher ], isDowncast } );
}
public for( groupName: 'downcast' | 'dataDowncast' | 'editingDowncast' ): DowncastHelpers;
public for( groupName: 'upcast' ): UpcastHelpers;
public for<T extends string>( groupName: T ): ConversionType<T>;
/**
* Provides a chainable API to assign converters to a conversion dispatchers group.
*
* If the given group name has not been registered, the
* {@link module:utils/ckeditorerror~CKEditorError `conversion-for-unknown-group` error} is thrown.
*
* You can use conversion helpers available directly in the `for()` chain or your custom ones via
* the {@link module:engine/conversion/conversionhelpers~ConversionHelpers#add `add()`} method.
*
* # Using built-in conversion helpers
*
* The `for()` chain comes with a set of conversion helpers which you can use like this:
*
* ```ts
* editor.conversion.for( 'downcast' )
* .elementToElement( config1 ) // Adds an element-to-element downcast converter.
* .attributeToElement( config2 ); // Adds an attribute-to-element downcast converter.
*
* editor.conversion.for( 'upcast' )
* .elementToAttribute( config3 ); // Adds an element-to-attribute upcast converter.
* ```
*
* Refer to the documentation of built-in conversion helpers to learn about their configuration options.
*
* * downcast (model-to-view) conversion helpers:
*
* * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#elementToElement `elementToElement()`},
* * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#attributeToElement `attributeToElement()`},
* * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#attributeToAttribute `attributeToAttribute()`}.
* * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#markerToElement `markerToElement()`}.
* * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#markerToHighlight `markerToHighlight()`}.
*
* * upcast (view-to-model) conversion helpers:
*
* * {@link module:engine/conversion/upcasthelpers~UpcastHelpers#elementToElement `elementToElement()`},
* * {@link module:engine/conversion/upcasthelpers~UpcastHelpers#elementToAttribute `elementToAttribute()`},
* * {@link module:engine/conversion/upcasthelpers~UpcastHelpers#attributeToAttribute `attributeToAttribute()`}.
* * {@link module:engine/conversion/upcasthelpers~UpcastHelpers#elementToMarker `elementToMarker()`}.
*
* # Using custom conversion helpers
*
* If you need to implement an atypical converter, you can do so by calling:
*
* ```ts
* editor.conversion.for( direction ).add( customHelper );
* ```
*
* The `.add()` method takes exactly one parameter, which is a function. This function should accept one parameter that
* is a dispatcher instance. The function should add an actual converter to the passed dispatcher instance.
*
* Example:
*
* ```ts
* editor.conversion.for( 'upcast' ).add( dispatcher => {
* dispatcher.on( 'element:a', ( evt, data, conversionApi ) => {
* // Do something with a view <a> element.
* } );
* } );
* ```
*
* Refer to the documentation of {@link module:engine/conversion/upcastdispatcher~UpcastDispatcher}
* and {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher} to learn how to write
* custom converters.
*
* @param groupName The name of dispatchers group to add the converters to.
*/
public for( groupName: string ): DowncastHelpers | UpcastHelpers {
if ( !this._helpers.has( groupName ) ) {
/**
* Trying to add a converter to an unknown dispatchers group.
*
* @error conversion-for-unknown-group
*/
throw new CKEditorError( 'conversion-for-unknown-group', this );
}
return this._helpers.get( groupName )!;
}
/**
* Sets up converters between the model and the view that convert a model element to a view element (and vice versa).
* For example, the model `<paragraph>Foo</paragraph>` is turned into `<p>Foo</p>` in the view.
*
* ```ts
* // A simple conversion from the `paragraph` model element to the `<p>` view element (and vice versa).
* editor.conversion.elementToElement( { model: 'paragraph', view: 'p' } );
*
* // Override other converters by specifying a converter definition with a higher priority.
* editor.conversion.elementToElement( { model: 'paragraph', view: 'div', converterPriority: 'high' } );
*
* // View specified as an object instead of a string.
* editor.conversion.elementToElement( {
* model: 'fancyParagraph',
* view: {
* name: 'p',
* classes: 'fancy'
* }
* } );
*
* // Use `upcastAlso` to define other view elements that should also be converted to a `paragraph` element.
* editor.conversion.elementToElement( {
* model: 'paragraph',
* view: 'p',
* upcastAlso: [
* 'div',
* {
* // Any element with the `display: block` style.
* styles: {
* display: 'block'
* }
* }
* ]
* } );
*
* // `upcastAlso` set as callback enables a conversion of a wide range of different view elements.
* editor.conversion.elementToElement( {
* model: 'heading',
* view: 'h2',
* // Convert "heading-like" paragraphs to headings.
* upcastAlso: viewElement => {
* const fontSize = viewElement.getStyle( 'font-size' );
*
* if ( !fontSize ) {
* return null;
* }
*
* const match = fontSize.match( /(\d+)\s*px/ );
*
* if ( !match ) {
* return null;
* }
*
* const size = Number( match[ 1 ] );
*
* if ( size > 26 ) {
* // Returned value can be an object with the matched properties.
* // These properties will be "consumed" during the conversion.
* // See `engine.view.Matcher~MatcherPattern` and `engine.view.Matcher#match` for more details.
*
* return { name: true, styles: [ 'font-size' ] };
* }
*
* return null;
* }
* } );
* ```
*
* `definition.model` is a `String` with a model element name to convert from or to.
*
* @param definition The converter definition.
*/
public elementToElement( definition: {
model: string;
view: ElementDefinition;
upcastAlso?: ArrayOrItem<ElementDefinition | MatcherPattern>;
converterPriority?: PriorityString;
} ): void {
// Set up downcast converter.
this.for( 'downcast' ).elementToElement( definition );
// Set up upcast converter.
for ( const { model, view } of _getAllUpcastDefinitions( definition ) ) {
this.for( 'upcast' )
.elementToElement( {
model,
view,
converterPriority: definition.converterPriority
} );
}
}
/**
* Sets up converters between the model and the view that convert a model attribute to a view element (and vice versa).
* For example, a model text node with `"Foo"` as data and the `bold` attribute will be turned to `<strong>Foo</strong>` in the view.
*
* ```ts
* // A simple conversion from the `bold=true` attribute to the `<strong>` view element (and vice versa).
* editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
*
* // Override other converters by specifying a converter definition with a higher priority.
* editor.conversion.attributeToElement( { model: 'bold', view: 'b', converterPriority: 'high' } );
*
* // View specified as an object instead of a string.
* editor.conversion.attributeToElement( {
* model: 'bold',
* view: {
* name: 'span',
* classes: 'bold'
* }
* } );
*
* // Use `config.model.name` to define the conversion only from a given node type, `$text` in this case.
* // The same attribute on different elements may then be handled by a different converter.
* editor.conversion.attributeToElement( {
* model: {
* key: 'textDecoration',
* values: [ 'underline', 'lineThrough' ],
* name: '$text'
* },
* view: {
* underline: {
* name: 'span',
* styles: {
* 'text-decoration': 'underline'
* }
* },
* lineThrough: {
* name: 'span',
* styles: {
* 'text-decoration': 'line-through'
* }
* }
* }
* } );
*
* // Use `upcastAlso` to define other view elements that should also be converted to the `bold` attribute.
* editor.conversion.attributeToElement( {
* model: 'bold',
* view: 'strong',
* upcastAlso: [
* 'b',
* {
* name: 'span',
* classes: 'bold'
* },
* {
* name: 'span',
* styles: {
* 'font-weight': 'bold'
* }
* },
* viewElement => {
* const fontWeight = viewElement.getStyle( 'font-weight' );
*
* if ( viewElement.is( 'element', 'span' ) && fontWeight && /\d+/.test() && Number( fontWeight ) > 500 ) {
* // Returned value can be an object with the matched properties.
* // These properties will be "consumed" during the conversion.
* // See `engine.view.Matcher~MatcherPattern` and `engine.view.Matcher#match` for more details.
*
* return {
* name: true,
* styles: [ 'font-weight' ]
* };
* }
* }
* ]
* } );
*
* // Conversion from and to a model attribute key whose value is an enum (`fontSize=big|small`).
* // `upcastAlso` set as callback enables a conversion of a wide range of different view elements.
* editor.conversion.attributeToElement( {
* model: {
* key: 'fontSize',
* values: [ 'big', 'small' ]
* },
* view: {
* big: {
* name: 'span',
* styles: {
* 'font-size': '1.2em'
* }
* },
* small: {
* name: 'span',
* styles: {
* 'font-size': '0.8em'
* }
* }
* },
* upcastAlso: {
* big: viewElement => {
* const fontSize = viewElement.getStyle( 'font-size' );
*
* if ( !fontSize ) {
* return null;
* }
*
* const match = fontSize.match( /(\d+)\s*px/ );
*
* if ( !match ) {
* return null;
* }
*
* const size = Number( match[ 1 ] );
*
* if ( viewElement.is( 'element', 'span' ) && size > 10 ) {
* // Returned value can be an object with the matched properties.
* // These properties will be "consumed" during the conversion.
* // See `engine.view.Matcher~MatcherPattern` and `engine.view.Matcher#match` for more details.
*
* return { name: true, styles: [ 'font-size' ] };
* }
*
* return null;
* },
* small: viewElement => {
* const fontSize = viewElement.getStyle( 'font-size' );
*
* if ( !fontSize ) {
* return null;
* }
*
* const match = fontSize.match( /(\d+)\s*px/ );
*
* if ( !match ) {
* return null;
* }
*
* const size = Number( match[ 1 ] );
*
* if ( viewElement.is( 'element', 'span' ) && size < 10 ) {
* // Returned value can be an object with the matched properties.
* // These properties will be "consumed" during the conversion.
* // See `engine.view.Matcher~MatcherPattern` and `engine.view.Matcher#match` for more details.
*
* return { name: true, styles: [ 'font-size' ] };
* }
*
* return null;
* }
* }
* } );
* ```
*
* The `definition.model` parameter specifies which model attribute should be converted from or to. It can be a `{ key, value }` object
* describing the attribute key and value to convert or a `String` specifying just the attribute key (in such a case
* `value` is set to `true`).
*
* @param definition The converter definition.
*/
public attributeToElement<TValues extends string>(
definition: {
model: string | {
key: string;
name?: string;
};
view: ElementDefinition;
upcastAlso?: ArrayOrItem<MatcherPattern>;
converterPriority?: PriorityString;
} | {
model: {
key: string;
name?: string;
values: Array<TValues>;
};
view: Record<TValues, ElementDefinition>;
upcastAlso?: Record<TValues, ArrayOrItem<MatcherPattern>>;
converterPriority?: PriorityString;
}
): void {
// Set up downcast converter.
this.for( 'downcast' ).attributeToElement( definition );
// Set up upcast converter.
for ( const { model, view } of _getAllUpcastDefinitions( definition ) ) {
this.for( 'upcast' )
.elementToAttribute( {
view,
model,
converterPriority: definition.converterPriority
} );
}
}
/**
* Sets up converters between the model and the view that convert a model attribute to a view attribute (and vice versa). For example,
* `<imageBlock src='foo.jpg'></imageBlock>` is converted to `<img src='foo.jpg'></img>` (the same attribute key and value).
* This type of converters is intended to be used with {@link module:engine/model/element~Element model element} nodes.
* To convert the text attributes,
* the {@link module:engine/conversion/conversion~Conversion#attributeToElement `attributeToElement converter`}should be set up.
*
* ```ts
* // A simple conversion from the `source` model attribute to the `src` view attribute (and vice versa).
* editor.conversion.attributeToAttribute( { model: 'source', view: 'src' } );
*
* // Attribute values are strictly specified.
* editor.conversion.attributeToAttribute( {
* model: {
* name: 'imageInline',
* key: 'aside',
* values: [ 'aside' ]
* },
* view: {
* aside: {
* name: 'img',
* key: 'class',
* value: [ 'aside', 'half-size' ]
* }
* }
* } );
*
* // Set the style attribute.
* editor.conversion.attributeToAttribute( {
* model: {
* name: 'imageInline',
* key: 'aside',
* values: [ 'aside' ]
* },
* view: {
* aside: {
* name: 'img',
* key: 'style',
* value: {
* float: 'right',
* width: '50%',
* margin: '5px'
* }
* }
* }
* } );
*
* // Conversion from and to a model attribute key whose value is an enum (`align=right|center`).
* // Use `upcastAlso` to define other view elements that should also be converted to the `align=right` attribute.
* editor.conversion.attributeToAttribute( {
* model: {
* key: 'align',
* values: [ 'right', 'center' ]
* },
* view: {
* right: {
* key: 'class',
* value: 'align-right'
* },
* center: {
* key: 'class',
* value: 'align-center'
* }
* },
* upcastAlso: {
* right: {
* styles: {
* 'text-align': 'right'
* }
* },
* center: {
* styles: {
* 'text-align': 'center'
* }
* }
* }
* } );
* ```
*
* The `definition.model` parameter specifies which model attribute should be converted from and to.
* It can be a `{ key, [ values ], [ name ] }` object or a `String`, which will be treated like `{ key: definition.model }`.
* The `key` property is the model attribute key to convert from and to.
* The `values` are the possible model attribute values. If the `values` parameter is not set, the model attribute value
* will be the same as the view attribute value.
* If `name` is set, the conversion will be set up only for model elements with the given name.
*
* The `definition.view` parameter specifies which view attribute should be converted from and to.
* It can be a `{ key, value, [ name ] }` object or a `String`, which will be treated like `{ key: definition.view }`.
* The `key` property is the view attribute key to convert from and to.
* The `value` is the view attribute value to convert from and to. If `definition.value` is not set, the view attribute value will be
* the same as the model attribute value.
* If `key` is `'class'`, `value` can be a `String` or an array of `String`s.
* If `key` is `'style'`, `value` is an object with key-value pairs.
* In other cases, `value` is a `String`.
* If `name` is set, the conversion will be set up only for model elements with the given name.
* If `definition.model.values` is set, `definition.view` is an object that assigns values from `definition.model.values`
* to `{ key, value, [ name ] }` objects.
*
* `definition.upcastAlso` specifies which other matching view elements should also be upcast to the given model configuration.
* If `definition.model.values` is set, `definition.upcastAlso` should be an object assigning values from `definition.model.values`
* to {@link module:engine/view/matcher~MatcherPattern}s or arrays of {@link module:engine/view/matcher~MatcherPattern}s.
*
* **Note:** `definition.model` and `definition.view` form should be mirrored, so the same types of parameters should
* be given in both parameters.
*
* @param definition The converter definition.
* @param definition.model The model attribute to convert from and to.
* @param definition.view The view attribute to convert from and to.
* @param definition.upcastAlso Any view element matching `definition.upcastAlso` will also be converted to the given model attribute.
* `definition.upcastAlso` is used only if `config.model.values` is specified.
*/
public attributeToAttribute<TValues extends string>(
definition: {
model: string | {
key: string;
name?: string;
};
view: string | ( AttributeDescriptor & { name?: string } );
upcastAlso?: ArrayOrItem<string | ( AttributeDescriptor & { name?: string } ) | AttributeCreatorFunction>;
converterPriority?: PriorityString;
} | {
model: {
key: string;
name?: string;
values: Array<TValues>;
};
view: Record<TValues, ( AttributeDescriptor & { name?: string } )>;
upcastAlso?: Record<TValues, ( AttributeDescriptor & { name?: string } ) | AttributeCreatorFunction>;
converterPriority?: PriorityString;
} ): void {
// Set up downcast converter.
this.for( 'downcast' ).attributeToAttribute( definition );
// Set up upcast converter.
for ( const { model, view } of _getAllUpcastDefinitions( definition ) ) {
this.for( 'upcast' )
.attributeToAttribute( {
view,
model
} );
}
}
/**
* Creates and caches conversion helpers for given dispatchers group.
*
* @param options.name Group name.
*/
private _createConversionHelpers(
{ name, dispatchers, isDowncast }: {
name: string;
dispatchers: Array<DowncastDispatcher | UpcastDispatcher>;
isDowncast: boolean;
}
): void {
if ( this._helpers.has( name ) ) {
/**
* Trying to register a group name that has already been registered.
*
* @error conversion-group-exists
*/
throw new CKEditorError( 'conversion-group-exists', this );
}
const helpers = isDowncast ?
new DowncastHelpers( dispatchers as Array<DowncastDispatcher> ) :
new UpcastHelpers( dispatchers as Array<UpcastDispatcher> );
this._helpers.set( name, helpers );
}
}
/**
* Helper function that creates a joint array out of an item passed in `definition.view` and items passed in
* `definition.upcastAlso`.
*/
function* _getAllUpcastDefinitions( definition: any ): IterableIterator<{ model: any; view: any }> {
if ( definition.model.values ) {
for ( const value of definition.model.values ) {
const model = { key: definition.model.key, value };
const view = definition.view[ value ];
const upcastAlso = definition.upcastAlso ? definition.upcastAlso[ value ] : undefined;
yield* _getUpcastDefinition( model, view, upcastAlso );
}
} else {
yield* _getUpcastDefinition( definition.model, definition.view, definition.upcastAlso );
}
}
function* _getUpcastDefinition( model: unknown, view: unknown, upcastAlso?: unknown ): any {
yield { model, view };
if ( upcastAlso ) {
for ( const upcastAlsoItem of toArray( upcastAlso ) ) {
yield { model, view: upcastAlsoItem };
}
}
}
type ConversionType<T extends string> = T extends `${ string }Downcast`
? DowncastHelpers
: T extends `${ string }Upcast`
? UpcastHelpers
: DowncastHelpers | UpcastHelpers;