99
1010import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror' ;
1111
12- import {
13- downcastElementToElement ,
14- downcastAttributeToElement ,
15- downcastAttributeToAttribute
16- } from './downcast-converters' ;
17-
18- import {
19- upcastElementToElement ,
20- upcastElementToAttribute ,
21- upcastAttributeToAttribute
22- } from './upcast-converters' ;
23-
2412/**
2513 * A utility class that helps add converters to upcast and downcast dispatchers.
2614 *
@@ -40,17 +28,18 @@ import {
4028 * method:
4129 *
4230 * // Add a converter to editing downcast and data downcast.
43- * editor.conversion.for( 'downcast' ).add( downcastElementToElement ( config ) );
31+ * editor.conversion.for( 'downcast' ).elementToElement ( config ) );
4432 *
4533 * // Add a converter to the data pipepline only:
46- * editor.conversion.for( 'dataDowncast' ).add( downcastElementToElement( dataConversionConfig ) );
34+ * editor.conversion.for( 'dataDowncast' ).elementToElement( dataConversionConfig ) );
35+ *
4736 * // And a slightly different one for the editing pipeline:
48- * editor.conversion.for( 'editingDowncast' ).add( downcastElementToElement ( editingConversionConfig ) );
37+ * editor.conversion.for( 'editingDowncast' ).elementToElement ( editingConversionConfig ) );
4938 *
5039 * The functions used in `add()` calls are one-way converters (i.e. you need to remember yourself to add
5140 * a converter in the other direction, if your feature requires that). They are also called "conversion helpers".
52- * You can find a set of them in the {@link module:engine/conversion/downcast-converters } and
53- * {@link module:engine/conversion/upcast-converters } modules.
41+ * You can find a set of them in the {@link module:engine/conversion/downcasthelpers } and
42+ * {@link module:engine/conversion/upcasthelpers } modules.
5443 *
5544 * Besides allowing to register converters to specific dispatchers, you can also use methods available in this
5645 * class to add two-way converters (upcast and downcast):
@@ -81,13 +70,17 @@ export default class Conversion {
8170 * If a given group name is used for the second time, the
8271 * {@link module:utils/ckeditorerror~CKEditorError `conversion-register-group-exists` error} is thrown.
8372 *
84- * @param {String } groupName The name for dispatchers group.
85- * @param {Array.<module:engine/conversion/downcastdispatcher~DowncastDispatcher|
86- * module:engine/conversion/upcastdispatcher~UpcastDispatcher>} dispatchers Dispatchers to register
73+ * @param {Object } options
74+ * @param {String } options.name The name for dispatchers group.
75+ * @param {module:engine/conversion/downcastdispatcher~DowncastDispatcher|
76+ * module:engine/conversion/upcastdispatcher~UpcastDispatcher|Array.<module:engine/conversion/downcastdispatcher~DowncastDispatcher|
77+ * module:engine/conversion/upcastdispatcher~UpcastDispatcher>} options.dispatcher Dispatcher or array of dispatchers to register
8778 * under the given name.
79+ * @param {module:engine/conversion/downcasthelpers~DowncastHelpers|
80+ * module:engine/conversion/upcasthelpers~UpcastHelpers} helpers
8881 */
89- register ( groupName , dispatchers ) {
90- if ( this . _dispatchersGroups . has ( groupName ) ) {
82+ register ( name , group ) {
83+ if ( this . _dispatchersGroups . has ( name ) ) {
9184 /**
9285 * Trying to register a group name that was already registered.
9386 *
@@ -96,16 +89,18 @@ export default class Conversion {
9689 throw new CKEditorError ( 'conversion-register-group-exists: Trying to register a group name that was already registered.' ) ;
9790 }
9891
99- this . _dispatchersGroups . set ( groupName , dispatchers ) ;
92+ this . _dispatchersGroups . set ( name , group ) ;
10093 }
10194
10295 /**
10396 * Provides chainable API to assign converters to dispatchers registered under a given group name. Converters are added
104- * by calling the `.add()` method of an object returned by this function.
97+ * by calling the {@link module:engine/conversion/conversion~ConversionHelpers#add `.add()`} method of an
98+ * {@link module:engine/conversion/conversion~ConversionHelpers conversion helpers} returned by this function.
10599 *
106- * conversion.for( 'downcast' )
107- * .add( conversionHelperA )
108- * .add( conversionHelperB );
100+ * editor.conversion.for( 'downcast' )
101+ * .add( conversionHelperA ) // Adds a custom converter A.
102+ * .add( conversionHelperB ) // Adds a custom converter B.
103+ * .elementToElement( config ); // Adds a custom element-to-element downcast converter.
109104 *
110105 * In this example `conversionHelperA` and `conversionHelperB` will be called for all dispatchers from the `'model'` group.
111106 *
@@ -117,43 +112,35 @@ export default class Conversion {
117112 *
118113 * For downcast (model-to-view conversion), these are:
119114 *
120- * * {@link module:engine/conversion/downcast-converters~downcastElementToElement Downcast element-to-element converter},
121- * * {@link module:engine/conversion/downcast-converters~downcastAttributeToElement Downcast attribute-to-element converter},
122- * * {@link module:engine/conversion/downcast-converters~downcastAttributeToAttribute Downcast attribute-to-attribute converter}.
115+ * * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#elementToElement Downcast element-to-element converter},
116+ * * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#attributeToElement Downcast attribute-to-element converter},
117+ * * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#attributeToAttribute Downcast attribute-to-attribute converter}.
118+ * * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#markerToElement Downcast marker-to-element converter}.
119+ * * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#markerToHighlight Downcast marker-to-highlight converter}.
123120 *
124121 * For upcast (view-to-model conversion), these are:
125122 *
126- * * {@link module:engine/conversion/upcast-converters~upcastElementToElement Upcast element-to-element converter},
127- * * {@link module:engine/conversion/upcast-converters~upcastElementToAttribute Upcast attribute-to-element converter},
128- * * {@link module:engine/conversion/upcast-converters~upcastAttributeToAttribute Upcast attribute-to-attribute converter}.
123+ * * {@link module:engine/conversion/upcasthelpers~UpcastHelpers#elementToElement Upcast element-to-element converter},
124+ * * {@link module:engine/conversion/upcasthelpers~UpcastHelpers#elementToAttribute Upcast attribute-to-element converter},
125+ * * {@link module:engine/conversion/upcasthelpers~UpcastHelpers#attributeToAttribute Upcast attribute-to-attribute converter}.
126+ * * {@link module:engine/conversion/upcasthelpers~UpcastHelpers#elementToMarker Upcast element-to-marker converter}.
129127 *
130128 * An example of using conversion helpers to convert the `paragraph` model element to the `p` view element (and back):
131129 *
132130 * // Define conversion configuration - model element 'paragraph' should be converted to view element 'p'.
133131 * const config = { model: 'paragraph', view: 'p' };
134132 *
135133 * // Add converters to proper dispatchers using conversion helpers.
136- * conversion.for( 'downcast' ).add( downcastElementToElement( config ) );
137- * conversion.for( 'upcast' ).add( upcastElementToElement( config ) );
138- *
139- * An example of providing a custom conversion helper that uses a custom converter function:
140- *
141- * // Adding a custom `myConverter` converter for 'paragraph' element insertion, with the default priority ('normal').
142- * conversion.for( 'downcast' ).add( conversion.customConverter( 'insert:paragraph', myConverter ) );
134+ * editor.conversion.for( 'downcast' ).elementToElement( config ) );
135+ * editor.conversion.for( 'upcast' ).elementToElement( config ) );
143136 *
144137 * @param {String } groupName The name of dispatchers group to add the converters to.
145- * @returns {Object } An object with the `.add()` method, providing a way to add converters.
138+ * @returns {module:engine/conversion/downcasthelpers~DowncastHelpers|module:engine/conversion/upcasthelpers~UpcastHelpers }
146139 */
147140 for ( groupName ) {
148- const dispatchers = this . _getDispatchers ( groupName ) ;
149-
150- return {
151- add ( conversionHelper ) {
152- _addToDispatchers ( dispatchers , conversionHelper ) ;
141+ const group = this . _getDispatchersGroup ( groupName ) ;
153142
154- return this ;
155- }
156- } ;
143+ return group ;
157144 }
158145
159146 /**
@@ -229,17 +216,16 @@ export default class Conversion {
229216 */
230217 elementToElement ( definition ) {
231218 // Set up downcast converter.
232- this . for ( 'downcast' ) . add ( downcastElementToElement ( definition ) ) ;
219+ this . for ( 'downcast' ) . elementToElement ( definition ) ;
233220
234221 // Set up upcast converter.
235222 for ( const { model, view } of _getAllUpcastDefinitions ( definition ) ) {
236- this . for ( 'upcast' ) . add (
237- upcastElementToElement ( {
223+ this . for ( 'upcast' )
224+ . elementToElement ( {
238225 model,
239226 view,
240227 converterPriority : definition . converterPriority
241- } )
242- ) ;
228+ } ) ;
243229 }
244230 }
245231
@@ -402,17 +388,16 @@ export default class Conversion {
402388 */
403389 attributeToElement ( definition ) {
404390 // Set up downcast converter.
405- this . for ( 'downcast' ) . add ( downcastAttributeToElement ( definition ) ) ;
391+ this . for ( 'downcast' ) . attributeToElement ( definition ) ;
406392
407393 // Set up upcast converter.
408394 for ( const { model, view } of _getAllUpcastDefinitions ( definition ) ) {
409- this . for ( 'upcast' ) . add (
410- upcastElementToAttribute ( {
395+ this . for ( 'upcast' )
396+ . elementToAttribute ( {
411397 view,
412398 model,
413399 converterPriority : definition . priority
414- } )
415- ) ;
400+ } ) ;
416401 }
417402 }
418403
@@ -528,34 +513,30 @@ export default class Conversion {
528513 */
529514 attributeToAttribute ( definition ) {
530515 // Set up downcast converter.
531- this . for ( 'downcast' ) . add ( downcastAttributeToAttribute ( definition ) ) ;
516+ this . for ( 'downcast' ) . attributeToAttribute ( definition ) ;
532517
533518 // Set up upcast converter.
534519 for ( const { model, view } of _getAllUpcastDefinitions ( definition ) ) {
535- this . for ( 'upcast' ) . add (
536- upcastAttributeToAttribute ( {
520+ this . for ( 'upcast' )
521+ . attributeToAttribute ( {
537522 view,
538523 model
539- } )
540- ) ;
524+ } ) ;
541525 }
542526 }
543527
544528 /**
545- * Returns dispatchers registered under a given group name.
529+ * Returns dispatchers group registered under a given group name.
546530 *
547531 * If the given group name has not been registered, the
548532 * {@link module:utils/ckeditorerror~CKEditorError `conversion-for-unknown-group` error} is thrown.
549533 *
550534 * @private
551535 * @param {String } groupName
552- * @returns {Array.<module:engine/conversion/downcastdispatcher~DowncastDispatcher|
553- * module:engine/conversion/upcastdispatcher~UpcastDispatcher> }
536+ * @returns {module:engine/conversion/conversion~DispatchersGroup }
554537 */
555- _getDispatchers ( groupName ) {
556- const dispatchers = this . _dispatchersGroups . get ( groupName ) ;
557-
558- if ( ! dispatchers ) {
538+ _getDispatchersGroup ( groupName ) {
539+ if ( ! this . _dispatchersGroups . has ( groupName ) ) {
559540 /**
560541 * Trying to add a converter to an unknown dispatchers group.
561542 *
@@ -564,7 +545,7 @@ export default class Conversion {
564545 throw new CKEditorError ( 'conversion-for-unknown-group: Trying to add a converter to an unknown dispatchers group.' ) ;
565546 }
566547
567- return dispatchers ;
548+ return this . _dispatchersGroups . get ( groupName ) ;
568549 }
569550}
570551
@@ -585,20 +566,13 @@ export default class Conversion {
585566 * @property {module:utils/priorities~PriorityString } [converterPriority] The converter priority.
586567 */
587568
588- // Helper function for the `Conversion` `.add()` method.
589- //
590- // Calls `conversionHelper` on each dispatcher from the group specified earlier in the `.for()` call, effectively
591- // adding converters to all specified dispatchers.
592- //
593- // @private
594- // @param {Array.<module:engine/conversion/downcastdispatcher~DowncastDispatcher|
595- // module:engine/conversion/upcastdispatcher~UpcastDispatcher> } dispatchers
596- // @param {Function } conversionHelper
597- function _addToDispatchers ( dispatchers , conversionHelper ) {
598- for ( const dispatcher of dispatchers ) {
599- conversionHelper ( dispatcher ) ;
600- }
601- }
569+ /**
570+ * @typedef {Object } module:engine/conversion/conversion~DispatchersGroup
571+ * @property {String } name Group name
572+ * @property {Array.<module:engine/conversion/downcastdispatcher~DowncastDispatcher|
573+ * module:engine/conversion/upcastdispatcher~UpcastDispatcher>} dispatchers
574+ * @property {module:engine/conversion/downcasthelpers~DowncastHelpers|module:engine/conversion/upcasthelpers~UpcastHelpers } helpers
575+ */
602576
603577// Helper function that creates a joint array out of an item passed in `definition.view` and items passed in
604578// `definition.upcastAlso`.
@@ -630,3 +604,48 @@ function* _getUpcastDefinition( model, view, upcastAlso ) {
630604 }
631605 }
632606}
607+
608+ /**
609+ * Base class for conversion helpers.
610+ */
611+ export class ConversionHelpers {
612+ /**
613+ * Creates ConversionHelpers instance.
614+ *
615+ * @param {Array.<module:engine/conversion/downcastdispatcher~DowncastDispatcher|
616+ * module:engine/conversion/upcastdispatcher~UpcastDispatcher>} dispatcher
617+ */
618+ constructor ( dispatcher ) {
619+ this . _dispatchers = Array . isArray ( dispatcher ) ? dispatcher : [ dispatcher ] ;
620+ }
621+
622+ /**
623+ * Registers a conversion helper.
624+ *
625+ * **Note**: See full usage example in the `{@link module:engine/conversion/conversion~Conversion#for conversion.for()}`
626+ * method description
627+ *
628+ * @param {Function } conversionHelper The function to be called on event.
629+ * @returns {module:engine/conversion/downcasthelpers~DowncastHelpers|module:engine/conversion/upcasthelpers~UpcastHelpers }
630+ */
631+ add ( conversionHelper ) {
632+ this . _addToDispatchers ( conversionHelper ) ;
633+
634+ return this ;
635+ }
636+
637+ /**
638+ * Helper function for the `Conversion` `.add()` method.
639+ *
640+ * Calls `conversionHelper` on each dispatcher from the group specified earlier in the `.for()` call, effectively
641+ * adding converters to all specified dispatchers.
642+ *
643+ * @private
644+ * @param {Function } conversionHelper
645+ */
646+ _addToDispatchers ( conversionHelper ) {
647+ for ( const dispatcher of this . _dispatchers ) {
648+ conversionHelper ( dispatcher ) ;
649+ }
650+ }
651+ }
0 commit comments