Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit e7d09cd

Browse files
author
Piotr Jasiun
authored
Merge pull request #1665 from ckeditor/1640
Other: Change `Conversion` class API. Closes #1640. BREAKING CHANGE: The `Conversion#register()` method was removed from the public API. Use constructor parameters to pass dispatchers and `Conversion#addAlias()` to register an alternative conversion group for registered upcast or downcast dispatchers.
2 parents aac4948 + ad0cee7 commit e7d09cd

File tree

10 files changed

+206
-167
lines changed

10 files changed

+206
-167
lines changed

src/conversion/conversion.js

Lines changed: 69 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
*/
99

1010
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
11+
import UpcastHelpers from './upcasthelpers';
12+
import DowncastHelpers from './downcasthelpers';
1113

1214
/**
1315
* A utility class that helps add converters to upcast and downcast dispatchers.
@@ -56,45 +58,69 @@ import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
5658
export default class Conversion {
5759
/**
5860
* 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
5966
*/
60-
constructor() {
67+
constructor( downcastDispatchers, upcastDispatchers ) {
6168
/**
69+
* Maps dispatchers group name to ConversionHelpers instances.
70+
*
6271
* @private
63-
* @member {Map}
72+
* @member {Map.<String,module:engine/conversion/conversionhelpers~ConversionHelpers>}
6473
*/
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+
6683
}
6784

6885
/**
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.
7187
*
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+
* );
7492
*
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.
7898
*/
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 ) {
81104
/**
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.
83106
*
84-
* @error conversion-register-group-exists
107+
* @error conversion-add-alias-dispatcher-not-registered
85108
*/
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.' );
88111
}
89112

90-
this._conversionHelpers.set( name, conversionHelpers );
113+
this._createConversionHelpers( { name: alias, dispatchers: [ dispatcher ], isDowncast: isDowncast } );
91114
}
92115

93116
/**
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.
95121
*
96122
* 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.
98124
*
99125
* # Using bulit-in conversion helpers
100126
*
@@ -149,7 +175,16 @@ export default class Conversion {
149175
* @returns {module:engine/conversion/downcasthelpers~DowncastHelpers|module:engine/conversion/upcasthelpers~UpcastHelpers}
150176
*/
151177
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 );
153188
}
154189

155190
/**
@@ -535,26 +570,28 @@ export default class Conversion {
535570
}
536571

537572
/**
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.
542574
*
543575
* @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
546581
*/
547-
_getConversionHelpers( groupName ) {
548-
if ( !this._conversionHelpers.has( groupName ) ) {
582+
_createConversionHelpers( { name, dispatchers, isDowncast } ) {
583+
if ( this._helpers.has( name ) ) {
549584
/**
550-
* Trying to add a converter to an unknown dispatchers group.
585+
* Trying to register a group name that has already been registered.
551586
*
552-
* @error conversion-for-unknown-group
587+
* @error conversion-group-exists
553588
*/
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.' );
555590
}
556591

557-
return this._conversionHelpers.get( groupName );
592+
const helpers = isDowncast ? new DowncastHelpers( dispatchers ) : new UpcastHelpers( dispatchers );
593+
594+
this._helpers.set( name, helpers );
558595
}
559596
}
560597

@@ -605,48 +642,3 @@ function* _getUpcastDefinition( model, view, upcastAlso ) {
605642
}
606643
}
607644
}
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-
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
3+
* For licensing, see LICENSE.md.
4+
*/
5+
6+
/**
7+
* @module engine/conversion/conversionhelpers
8+
*/
9+
10+
/**
11+
* Base class for conversion helpers.
12+
*/
13+
export default class ConversionHelpers {
14+
/**
15+
* Creates a conversion helpers instance.
16+
*
17+
* @param {Array.<module:engine/conversion/downcastdispatcher~DowncastDispatcher|
18+
* module:engine/conversion/upcastdispatcher~UpcastDispatcher>} dispatchers
19+
*/
20+
constructor( dispatchers ) {
21+
this._dispatchers = dispatchers;
22+
}
23+
24+
/**
25+
* Registers a conversion helper.
26+
*
27+
* **Note**: See full usage example in the `{@link module:engine/conversion/conversion~Conversion#for conversion.for()}`
28+
* method description.
29+
*
30+
* @param {Function} conversionHelper The function to be called on event.
31+
* @returns {module:engine/conversion/downcasthelpers~DowncastHelpers|module:engine/conversion/upcasthelpers~UpcastHelpers}
32+
*/
33+
add( conversionHelper ) {
34+
for ( const dispatcher of this._dispatchers ) {
35+
conversionHelper( dispatcher );
36+
}
37+
38+
return this;
39+
}
40+
}

src/conversion/downcasthelpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import ModelElement from '../model/element';
99

1010
import ViewAttributeElement from '../view/attributeelement';
1111
import DocumentSelection from '../model/documentselection';
12-
import { ConversionHelpers } from './conversion';
12+
import ConversionHelpers from './conversionhelpers';
1313

1414
import log from '@ckeditor/ckeditor5-utils/src/log';
1515
import { cloneDeep } from 'lodash-es';
@@ -23,7 +23,7 @@ import { cloneDeep } from 'lodash-es';
2323
/**
2424
* Downcast conversion helper functions.
2525
*
26-
* @extends module:engine/conversion/conversion~ConversionHelpers
26+
* @extends module:engine/conversion/conversionhelpers~ConversionHelpers
2727
*/
2828
export default class DowncastHelpers extends ConversionHelpers {
2929
/**

src/conversion/upcasthelpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import Matcher from '../view/matcher';
77
import ModelRange from '../model/range';
8-
import { ConversionHelpers } from './conversion';
8+
import ConversionHelpers from './conversionhelpers';
99

1010
import { cloneDeep } from 'lodash-es';
1111
import ModelSelection from '../model/selection';
@@ -20,7 +20,7 @@ import ModelSelection from '../model/selection';
2020
/**
2121
* Upcast conversion helper functions.
2222
*
23-
* @extends module:engine/conversion/conversion~ConversionHelpers
23+
* @extends module:engine/conversion/conversionhelpers~ConversionHelpers
2424
*/
2525
export default class UpcastHelpers extends ConversionHelpers {
2626
/**

tests/controller/datacontroller.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ describe( 'DataController', () => {
3939

4040
data = new DataController( model, htmlDataProcessor );
4141

42-
upcastHelpers = new UpcastHelpers( data.upcastDispatcher );
43-
downcastHelpers = new DowncastHelpers( data.downcastDispatcher );
42+
upcastHelpers = new UpcastHelpers( [ data.upcastDispatcher ] );
43+
downcastHelpers = new DowncastHelpers( [ data.downcastDispatcher ] );
4444
} );
4545

4646
describe( 'constructor()', () => {

tests/controller/editingcontroller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ describe( 'EditingController', () => {
9090
model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
9191
model.schema.register( 'div', { inheritAllFrom: '$block' } );
9292

93-
const downcastHelpers = new DowncastHelpers( editing.downcastDispatcher );
93+
const downcastHelpers = new DowncastHelpers( [ editing.downcastDispatcher ] );
9494

9595
downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
9696
downcastHelpers.elementToElement( { model: 'div', view: 'div' } );

0 commit comments

Comments
 (0)