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

Commit

Permalink
Merge c02a29f into c31bea6
Browse files Browse the repository at this point in the history
  • Loading branch information
jodator committed Feb 7, 2019
2 parents c31bea6 + c02a29f commit 1b01248
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 167 deletions.
157 changes: 80 additions & 77 deletions src/conversion/conversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/

import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
import UpcastHelpers from './upcasthelpers';
import DowncastHelpers from './downcasthelpers';

/**
* A utility class that helps add converters to upcast and downcast dispatchers.
Expand Down Expand Up @@ -56,45 +58,77 @@ import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
export default class Conversion {
/**
* Creates a new conversion instance.
*
* @param {module:engine/conversion/downcastdispatcher~DowncastDispatcher|
* Array.<module:engine/conversion/downcastdispatcher~DowncastDispatcher>} downcastDispatchers
* @param {module:engine/conversion/upcastdispatcher~UpcastDispatcher|
* Array.<module:engine/conversion/upcastdispatcher~UpcastDispatcher>} upcastDispatchers
*/
constructor() {
constructor( downcastDispatchers, upcastDispatchers ) {
/**
* Maps dispatchers group name to ConversionHelpers class (Upcast or Downcast).
*
* @private
* @member {Map.<String,Class>}
*/
this._helpers = new Map();

/**
* Maps dispatchers group name to array with dispatchers.
*
* @private
* @member {Map}
* @member {Map.<String,Array.<module:engine/conversion/downcastdispatcher~DowncastDispatcher|
* module:engine/conversion/upcastdispatcher~UpcastDispatcher>>}
*/
this._conversionHelpers = new Map();
this._groups = new Map();

// Define default 'downcast' & 'upcast' dispatchers groups. Those groups are always available as two-way converters needs them.
this._defineDispatchersGroup( 'downcast', downcastDispatchers, DowncastHelpers );
this._defineDispatchersGroup( 'upcast', upcastDispatchers, UpcastHelpers );
}

/**
* Registers one or more converters under a given group name. The group name can then be used to assign a converter
* to multiple dispatchers at once.
* Define an alias for registered dispatcher.
*
* If a given group name is used for the second time, the
* {@link module:utils/ckeditorerror~CKEditorError `conversion-register-group-exists` error} is thrown.
* const conversion = new Conversion(
* [ dataDowncastDispatcher, editingDowncastDispatcher ],
* upcastDispatcher
* );
*
* @param {String} name The name for dispatchers group.
* @param {module:engine/conversion/downcasthelpers~DowncastHelpers|
* module:engine/conversion/upcasthelpers~UpcastHelpers} conversionHelpers
* conversion.addAlias( 'dataDowncast', dataDowncastDispatcher );
*
* @param {String} alias An alias of a dispatcher.
* @param {module:engine/conversion/downcastdispatcher~DowncastDispatcher|
* module:engine/conversion/upcastdispatcher~UpcastDispatcher} dispatcher Dispatcher which should have an alias.
*/
register( name, conversionHelpers ) {
if ( this._conversionHelpers.has( name ) ) {
addAlias( alias, dispatcher ) {
const groupEntry = [ ...this._groups.entries() ].find( ( [ , dispatchers ] ) => dispatchers.includes( dispatcher ) );

if ( !groupEntry ) {
/**
* Trying to register a group name that has already been registered.
* Trying to register and alias for a dispatcher that nas not been registered.
*
* @error conversion-register-group-exists
* @error conversion-add-alias-dispatcher-not-registered
*/
throw new CKEditorError( 'conversion-register-group-exists: Trying to register' +
'a group name that has already been registered.' );
throw new CKEditorError( 'conversion-add-alias-dispatcher-not-registered: ' +
'Trying to register and alias for a dispatcher that nas not been registered.' );
}

this._conversionHelpers.set( name, conversionHelpers );
const groupName = groupEntry[ 0 ];

const helper = this._helpers.get( groupName );

this._defineDispatchersGroup( alias, dispatcher, helper );
}

/**
* Provides a chainable API to assign converters to conversion dispatchers.
* Provides a chainable API to assign converters to 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/conversion~ConversionHelpers#add `add()`} method.
* the {@link module:engine/conversion/conversionhelpers~ConversionHelpers#add `add()`} method.
*
* # Using bulit-in conversion helpers
*
Expand Down Expand Up @@ -149,7 +183,18 @@ export default class Conversion {
* @returns {module:engine/conversion/downcasthelpers~DowncastHelpers|module:engine/conversion/upcasthelpers~UpcastHelpers}
*/
for( groupName ) {
return this._getConversionHelpers( groupName );
if ( !this._groups.has( groupName ) ) {
/**
* Trying to add a converter to an unknown dispatchers group.
*
* @error conversion-for-unknown-group
*/
throw new CKEditorError( 'conversion-for-unknown-group: Trying to add a converter to an unknown dispatchers group.' );
}

const ConversionHelper = this._helpers.get( groupName );

return new ConversionHelper( this._groups.get( groupName ) );
}

/**
Expand Down Expand Up @@ -535,26 +580,29 @@ export default class Conversion {
}

/**
* Returns conversion helpers registered under a given name.
*
* If the given group name has not been registered, the
* {@link module:utils/ckeditorerror~CKEditorError `conversion-for-unknown-group` error} is thrown.
* Registers dispatchers group.
*
* @private
* @param {String} groupName
* @returns {module:engine/conversion/downcasthelpers~DowncastHelpers|module:engine/conversion/upcasthelpers~UpcastHelpers}
* @param {String} name Group name.
* @param {module:engine/conversion/downcastdispatcher~DowncastDispatcher|
* module:engine/conversion/upcastdispatcher~UpcastDispatcher|Array.<module:engine/conversion/downcastdispatcher~DowncastDispatcher|
* module:engine/conversion/upcastdispatcher~UpcastDispatcher>} dispatcher
* @param {module:engine/conversion/conversionhelpers~ConversionHelpers} helpers
*/
_getConversionHelpers( groupName ) {
if ( !this._conversionHelpers.has( groupName ) ) {
_defineDispatchersGroup( name, dispatcher, helpers ) {
if ( this._groups.has( name ) ) {
/**
* Trying to add a converter to an unknown dispatchers group.
* Trying to register a group name that has already been registered.
*
* @error conversion-for-unknown-group
* @error conversion-group-exists
*/
throw new CKEditorError( 'conversion-for-unknown-group: Trying to add a converter to an unknown dispatchers group.' );
throw new CKEditorError( 'conversion-group-exists: Trying to register a group name that has already been registered.' );
}

return this._conversionHelpers.get( groupName );
const dispatchers = Array.isArray( dispatcher ) ? dispatcher : [ dispatcher ];

this._groups.set( name, dispatchers );
this._helpers.set( name, helpers );
}
}

Expand Down Expand Up @@ -605,48 +653,3 @@ function* _getUpcastDefinition( model, view, upcastAlso ) {
}
}
}

/**
* Base class for conversion helpers.
*/
export class ConversionHelpers {
/**
* Creates a conversion helpers instance.
*
* @param {Array.<module:engine/conversion/downcastdispatcher~DowncastDispatcher|
* module:engine/conversion/upcastdispatcher~UpcastDispatcher>} dispatcher
*/
constructor( dispatcher ) {
this._dispatchers = Array.isArray( dispatcher ) ? dispatcher : [ dispatcher ];
}

/**
* Registers a conversion helper.
*
* **Note**: See full usage example in the `{@link module:engine/conversion/conversion~Conversion#for conversion.for()}`
* method description.
*
* @param {Function} conversionHelper The function to be called on event.
* @returns {module:engine/conversion/downcasthelpers~DowncastHelpers|module:engine/conversion/upcasthelpers~UpcastHelpers}
*/
add( conversionHelper ) {
this._addToDispatchers( conversionHelper );

return this;
}

/**
* Helper function for the `Conversion` `.add()` method.
*
* Calls `conversionHelper` on each dispatcher from the group specified earlier in the `.for()` call, effectively
* adding converters to all specified dispatchers.
*
* @private
* @param {Function} conversionHelper
*/
_addToDispatchers( conversionHelper ) {
for ( const dispatcher of this._dispatchers ) {
conversionHelper( dispatcher );
}
}
}
40 changes: 40 additions & 0 deletions src/conversion/conversionhelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/

/**
* @module engine/conversion/conversionhelpers
*/

/**
* Base class for conversion helpers.
*/
export default class ConversionHelpers {
/**
* Creates a conversion helpers instance.
*
* @param {Array.<module:engine/conversion/downcastdispatcher~DowncastDispatcher|
* module:engine/conversion/upcastdispatcher~UpcastDispatcher>} dispatchers
*/
constructor( dispatchers ) {
this._dispatchers = dispatchers;
}

/**
* Registers a conversion helper.
*
* **Note**: See full usage example in the `{@link module:engine/conversion/conversion~Conversion#for conversion.for()}`
* method description.
*
* @param {Function} conversionHelper The function to be called on event.
* @returns {module:engine/conversion/downcasthelpers~DowncastHelpers|module:engine/conversion/upcasthelpers~UpcastHelpers}
*/
add( conversionHelper ) {
for ( const dispatcher of this._dispatchers ) {
conversionHelper( dispatcher );
}

return this;
}
}
4 changes: 2 additions & 2 deletions src/conversion/downcasthelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ModelElement from '../model/element';

import ViewAttributeElement from '../view/attributeelement';
import DocumentSelection from '../model/documentselection';
import { ConversionHelpers } from './conversion';
import ConversionHelpers from './conversionhelpers';

import log from '@ckeditor/ckeditor5-utils/src/log';
import { cloneDeep } from 'lodash-es';
Expand All @@ -23,7 +23,7 @@ import { cloneDeep } from 'lodash-es';
/**
* Downcast conversion helper functions.
*
* @extends module:engine/conversion/conversion~ConversionHelpers
* @extends module:engine/conversion/conversionhelpers~ConversionHelpers
*/
export default class DowncastHelpers extends ConversionHelpers {
/**
Expand Down
4 changes: 2 additions & 2 deletions src/conversion/upcasthelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import Matcher from '../view/matcher';
import ModelRange from '../model/range';
import { ConversionHelpers } from './conversion';
import ConversionHelpers from './conversionhelpers';

import { cloneDeep } from 'lodash-es';
import ModelSelection from '../model/selection';
Expand All @@ -20,7 +20,7 @@ import ModelSelection from '../model/selection';
/**
* Upcast conversion helper functions.
*
* @extends module:engine/conversion/conversion~ConversionHelpers
* @extends module:engine/conversion/conversionhelpers~ConversionHelpers
*/
export default class UpcastHelpers extends ConversionHelpers {
/**
Expand Down
4 changes: 2 additions & 2 deletions tests/controller/datacontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ describe( 'DataController', () => {

data = new DataController( model, htmlDataProcessor );

upcastHelpers = new UpcastHelpers( data.upcastDispatcher );
downcastHelpers = new DowncastHelpers( data.downcastDispatcher );
upcastHelpers = new UpcastHelpers( [ data.upcastDispatcher ] );
downcastHelpers = new DowncastHelpers( [ data.downcastDispatcher ] );
} );

describe( 'constructor()', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/controller/editingcontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe( 'EditingController', () => {
model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
model.schema.register( 'div', { inheritAllFrom: '$block' } );

const downcastHelpers = new DowncastHelpers( editing.downcastDispatcher );
const downcastHelpers = new DowncastHelpers( [ editing.downcastDispatcher ] );

downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
downcastHelpers.elementToElement( { model: 'div', view: 'div' } );
Expand Down
Loading

0 comments on commit 1b01248

Please sign in to comment.