|
8 | 8 | */ |
9 | 9 |
|
10 | 10 | import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror'; |
| 11 | +import UpcastHelpers from './upcasthelpers'; |
| 12 | +import DowncastHelpers from './downcasthelpers'; |
11 | 13 |
|
12 | 14 | /** |
13 | 15 | * A utility class that helps add converters to upcast and downcast dispatchers. |
@@ -56,45 +58,69 @@ import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror'; |
56 | 58 | export default class Conversion { |
57 | 59 | /** |
58 | 60 | * Creates a new conversion instance. |
| 61 | + * |
| 62 | + * @param {module:engine/conversion/downcastdispatcher~DowncastDispatcher| |
| 63 | + * Array.<module:engine/conversion/downcastdispatcher~DowncastDispatcher>} downcastDispatchers |
| 64 | + * @param {module:engine/conversion/upcastdispatcher~UpcastDispatcher| |
| 65 | + * Array.<module:engine/conversion/upcastdispatcher~UpcastDispatcher>} upcastDispatchers |
59 | 66 | */ |
60 | | - constructor() { |
| 67 | + constructor( downcastDispatchers, upcastDispatchers ) { |
61 | 68 | /** |
| 69 | + * Maps dispatchers group name to ConversionHelpers instances. |
| 70 | + * |
62 | 71 | * @private |
63 | | - * @member {Map} |
| 72 | + * @member {Map.<String,module:engine/conversion/conversionhelpers~ConversionHelpers>} |
64 | 73 | */ |
65 | | - this._conversionHelpers = new Map(); |
| 74 | + this._helpers = new Map(); |
| 75 | + |
| 76 | + // Define default 'downcast' & 'upcast' dispatchers groups. Those groups are always available as two-way converters needs them. |
| 77 | + this._downcast = Array.isArray( downcastDispatchers ) ? downcastDispatchers : [ downcastDispatchers ]; |
| 78 | + this._createConversionHelpers( { name: 'downcast', dispatchers: this._downcast, isDowncast: true } ); |
| 79 | + |
| 80 | + this._upcast = Array.isArray( upcastDispatchers ) ? upcastDispatchers : [ upcastDispatchers ]; |
| 81 | + this._createConversionHelpers( { name: 'upcast', dispatchers: this._upcast, isDowncast: false } ); |
| 82 | + |
66 | 83 | } |
67 | 84 |
|
68 | 85 | /** |
69 | | - * Registers one or more converters under a given group name. The group name can then be used to assign a converter |
70 | | - * to multiple dispatchers at once. |
| 86 | + * Define an alias for registered dispatcher. |
71 | 87 | * |
72 | | - * If a given group name is used for the second time, the |
73 | | - * {@link module:utils/ckeditorerror~CKEditorError `conversion-register-group-exists` error} is thrown. |
| 88 | + * const conversion = new Conversion( |
| 89 | + * [ dataDowncastDispatcher, editingDowncastDispatcher ], |
| 90 | + * upcastDispatcher |
| 91 | + * ); |
74 | 92 | * |
75 | | - * @param {String} name The name for dispatchers group. |
76 | | - * @param {module:engine/conversion/downcasthelpers~DowncastHelpers| |
77 | | - * module:engine/conversion/upcasthelpers~UpcastHelpers} conversionHelpers |
| 93 | + * conversion.addAlias( 'dataDowncast', dataDowncastDispatcher ); |
| 94 | + * |
| 95 | + * @param {String} alias An alias of a dispatcher. |
| 96 | + * @param {module:engine/conversion/downcastdispatcher~DowncastDispatcher| |
| 97 | + * module:engine/conversion/upcastdispatcher~UpcastDispatcher} dispatcher Dispatcher which should have an alias. |
78 | 98 | */ |
79 | | - register( name, conversionHelpers ) { |
80 | | - if ( this._conversionHelpers.has( name ) ) { |
| 99 | + addAlias( alias, dispatcher ) { |
| 100 | + const isDowncast = this._downcast.includes( dispatcher ); |
| 101 | + const isUpcast = this._upcast.includes( dispatcher ); |
| 102 | + |
| 103 | + if ( !isUpcast && !isDowncast ) { |
81 | 104 | /** |
82 | | - * Trying to register a group name that has already been registered. |
| 105 | + * Trying to register and alias for a dispatcher that nas not been registered. |
83 | 106 | * |
84 | | - * @error conversion-register-group-exists |
| 107 | + * @error conversion-add-alias-dispatcher-not-registered |
85 | 108 | */ |
86 | | - throw new CKEditorError( 'conversion-register-group-exists: Trying to register' + |
87 | | - 'a group name that has already been registered.' ); |
| 109 | + throw new CKEditorError( 'conversion-add-alias-dispatcher-not-registered: ' + |
| 110 | + 'Trying to register and alias for a dispatcher that nas not been registered.' ); |
88 | 111 | } |
89 | 112 |
|
90 | | - this._conversionHelpers.set( name, conversionHelpers ); |
| 113 | + this._createConversionHelpers( { name: alias, dispatchers: [ dispatcher ], isDowncast: isDowncast } ); |
91 | 114 | } |
92 | 115 |
|
93 | 116 | /** |
94 | | - * Provides a chainable API to assign converters to conversion dispatchers. |
| 117 | + * Provides a chainable API to assign converters to conversion dispatchers group. |
| 118 | + * |
| 119 | + * If the given group name has not been registered, the |
| 120 | + * {@link module:utils/ckeditorerror~CKEditorError `conversion-for-unknown-group` error} is thrown. |
95 | 121 | * |
96 | 122 | * You can use conversion helpers available directly in the `for()` chain or your custom ones via |
97 | | - * the {@link module:engine/conversion/conversion~ConversionHelpers#add `add()`} method. |
| 123 | + * the {@link module:engine/conversion/conversionhelpers~ConversionHelpers#add `add()`} method. |
98 | 124 | * |
99 | 125 | * # Using bulit-in conversion helpers |
100 | 126 | * |
@@ -149,7 +175,16 @@ export default class Conversion { |
149 | 175 | * @returns {module:engine/conversion/downcasthelpers~DowncastHelpers|module:engine/conversion/upcasthelpers~UpcastHelpers} |
150 | 176 | */ |
151 | 177 | for( groupName ) { |
152 | | - return this._getConversionHelpers( groupName ); |
| 178 | + if ( !this._helpers.has( groupName ) ) { |
| 179 | + /** |
| 180 | + * Trying to add a converter to an unknown dispatchers group. |
| 181 | + * |
| 182 | + * @error conversion-for-unknown-group |
| 183 | + */ |
| 184 | + throw new CKEditorError( 'conversion-for-unknown-group: Trying to add a converter to an unknown dispatchers group.' ); |
| 185 | + } |
| 186 | + |
| 187 | + return this._helpers.get( groupName ); |
153 | 188 | } |
154 | 189 |
|
155 | 190 | /** |
@@ -535,26 +570,28 @@ export default class Conversion { |
535 | 570 | } |
536 | 571 |
|
537 | 572 | /** |
538 | | - * Returns conversion helpers registered under a given name. |
539 | | - * |
540 | | - * If the given group name has not been registered, the |
541 | | - * {@link module:utils/ckeditorerror~CKEditorError `conversion-for-unknown-group` error} is thrown. |
| 573 | + * Creates and caches conversion helpers for given dispatchers group. |
542 | 574 | * |
543 | 575 | * @private |
544 | | - * @param {String} groupName |
545 | | - * @returns {module:engine/conversion/downcasthelpers~DowncastHelpers|module:engine/conversion/upcasthelpers~UpcastHelpers} |
| 576 | + * @param {Object} options |
| 577 | + * @param {String} options.name Group name. |
| 578 | + * @param {Array.<module:engine/conversion/downcastdispatcher~DowncastDispatcher| |
| 579 | + * module:engine/conversion/upcastdispatcher~UpcastDispatcher>} options.dispatchers |
| 580 | + * @param {Boolean} options.isDowncast |
546 | 581 | */ |
547 | | - _getConversionHelpers( groupName ) { |
548 | | - if ( !this._conversionHelpers.has( groupName ) ) { |
| 582 | + _createConversionHelpers( { name, dispatchers, isDowncast } ) { |
| 583 | + if ( this._helpers.has( name ) ) { |
549 | 584 | /** |
550 | | - * Trying to add a converter to an unknown dispatchers group. |
| 585 | + * Trying to register a group name that has already been registered. |
551 | 586 | * |
552 | | - * @error conversion-for-unknown-group |
| 587 | + * @error conversion-group-exists |
553 | 588 | */ |
554 | | - throw new CKEditorError( 'conversion-for-unknown-group: Trying to add a converter to an unknown dispatchers group.' ); |
| 589 | + throw new CKEditorError( 'conversion-group-exists: Trying to register a group name that has already been registered.' ); |
555 | 590 | } |
556 | 591 |
|
557 | | - return this._conversionHelpers.get( groupName ); |
| 592 | + const helpers = isDowncast ? new DowncastHelpers( dispatchers ) : new UpcastHelpers( dispatchers ); |
| 593 | + |
| 594 | + this._helpers.set( name, helpers ); |
558 | 595 | } |
559 | 596 | } |
560 | 597 |
|
@@ -605,48 +642,3 @@ function* _getUpcastDefinition( model, view, upcastAlso ) { |
605 | 642 | } |
606 | 643 | } |
607 | 644 | } |
608 | | - |
609 | | -/** |
610 | | - * Base class for conversion helpers. |
611 | | - */ |
612 | | -export class ConversionHelpers { |
613 | | - /** |
614 | | - * Creates a conversion helpers instance. |
615 | | - * |
616 | | - * @param {Array.<module:engine/conversion/downcastdispatcher~DowncastDispatcher| |
617 | | - * module:engine/conversion/upcastdispatcher~UpcastDispatcher>} dispatcher |
618 | | - */ |
619 | | - constructor( dispatcher ) { |
620 | | - this._dispatchers = Array.isArray( dispatcher ) ? dispatcher : [ dispatcher ]; |
621 | | - } |
622 | | - |
623 | | - /** |
624 | | - * Registers a conversion helper. |
625 | | - * |
626 | | - * **Note**: See full usage example in the `{@link module:engine/conversion/conversion~Conversion#for conversion.for()}` |
627 | | - * method description. |
628 | | - * |
629 | | - * @param {Function} conversionHelper The function to be called on event. |
630 | | - * @returns {module:engine/conversion/downcasthelpers~DowncastHelpers|module:engine/conversion/upcasthelpers~UpcastHelpers} |
631 | | - */ |
632 | | - add( conversionHelper ) { |
633 | | - this._addToDispatchers( conversionHelper ); |
634 | | - |
635 | | - return this; |
636 | | - } |
637 | | - |
638 | | - /** |
639 | | - * Helper function for the `Conversion` `.add()` method. |
640 | | - * |
641 | | - * Calls `conversionHelper` on each dispatcher from the group specified earlier in the `.for()` call, effectively |
642 | | - * adding converters to all specified dispatchers. |
643 | | - * |
644 | | - * @private |
645 | | - * @param {Function} conversionHelper |
646 | | - */ |
647 | | - _addToDispatchers( conversionHelper ) { |
648 | | - for ( const dispatcher of this._dispatchers ) { |
649 | | - conversionHelper( dispatcher ); |
650 | | - } |
651 | | - } |
652 | | -} |
0 commit comments