Kaynağa Gözat

Changed the way of passing options to the downcast conversion process.

Oskar Wróbel 5 yıl önce
ebeveyn
işleme
719a13c12b

+ 10 - 3
packages/ckeditor5-engine/src/controller/datacontroller.js

@@ -207,7 +207,8 @@ export default class DataController {
 	 *
 	 * @param {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment} modelElementOrFragment
 	 * Element or document fragment whose content will be converted.
-	 * @param {Object} [options] Additional configuration passed to the conversion process.
+	 * @param {Object} [options] Additional configuration that will be available through
+	 * {@link module:engine/conversion/downcastdispatcher~DowncastConversionApi#options} during the conversion process.
 	 * @returns {module:engine/view/documentfragment~DocumentFragment} Output view DocumentFragment.
 	 */
 	toView( modelElementOrFragment, options ) {
@@ -223,8 +224,11 @@ export default class DataController {
 
 		this.mapper.bindElements( modelElementOrFragment, viewDocumentFragment );
 
+		// Make additional options available during conversion process through conversionSpi.
+		this.downcastDispatcher.conversionApi.options = options;
+
 		// We have no view controller and rendering to DOM in DataController so view.change() block is not used here.
-		this.downcastDispatcher.convertInsert( modelRange, viewWriter, options );
+		this.downcastDispatcher.convertInsert( modelRange, viewWriter );
 
 		if ( !modelElementOrFragment.is( 'documentFragment' ) ) {
 			// Then, if a document element is converted, convert markers.
@@ -232,10 +236,13 @@ export default class DataController {
 			const markers = _getMarkersRelativeToElement( modelElementOrFragment );
 
 			for ( const [ name, range ] of markers ) {
-				this.downcastDispatcher.convertMarkerAdd( name, range, viewWriter, options );
+				this.downcastDispatcher.convertMarkerAdd( name, range, viewWriter );
 			}
 		}
 
+		// Remove options from conversionApi.
+		delete this.downcastDispatcher.conversionApi.options;
+
 		return viewDocumentFragment;
 	}
 

+ 2 - 7
packages/ckeditor5-engine/src/conversion/downcastdispatcher.js

@@ -165,12 +165,10 @@ export default class DowncastDispatcher {
 	 * @fires attribute
 	 * @param {module:engine/model/range~Range} range The inserted range.
 	 * @param {module:engine/view/downcastwriter~DowncastWriter} writer The view writer that should be used to modify the view document.
-	 * @param {Object} [options={}] Additional, custom configuration passed to the converters through
 	 * {module:engine/conversion/downcastdispatcher~DowncastConversionApi conversion api}. Used only with `dataDowncast` group.
 	 */
-	convertInsert( range, writer, options = {} ) {
+	convertInsert( range, writer ) {
 		this.conversionApi.writer = writer;
-		this.conversionApi.options = options;
 
 		// Create a list of things that can be consumed, consisting of nodes and their attributes.
 		this.conversionApi.consumable = this._createInsertConsumable( range );
@@ -321,17 +319,15 @@ export default class DowncastDispatcher {
 	 * @param {String} markerName Marker name.
 	 * @param {module:engine/model/range~Range} markerRange Marker range.
 	 * @param {module:engine/view/downcastwriter~DowncastWriter} writer View writer that should be used to modify view document.
-	 * @param {Object} [options={}] Additional, custom configuration passed to the converters through
 	 * {module:engine/conversion/downcastdispatcher~DowncastConversionApi conversion api}. Used only with `dataDowncast` group.
 	 */
-	convertMarkerAdd( markerName, markerRange, writer, options = {} ) {
+	convertMarkerAdd( markerName, markerRange, writer ) {
 		// Do not convert if range is in graveyard or not in the document (e.g. in DocumentFragment).
 		if ( !markerRange.root.document || markerRange.root.rootName == '$graveyard' ) {
 			return;
 		}
 
 		this.conversionApi.writer = writer;
-		this.conversionApi.options = options;
 
 		// In markers' case, event name == consumable name.
 		const eventName = 'addMarker:' + markerName;
@@ -487,7 +483,6 @@ export default class DowncastDispatcher {
 	_clearConversionApi() {
 		delete this.conversionApi.writer;
 		delete this.conversionApi.consumable;
-		delete this.conversionApi.options;
 	}
 
 	/**

+ 24 - 8
packages/ckeditor5-engine/tests/controller/datacontroller.js

@@ -564,11 +564,13 @@ describe( 'DataController', () => {
 
 			const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
 
+			const options = { foo: 'bar' };
+
 			data.stringify( modelDocumentFragment );
-			expect( spy.lastCall.args[ 0 ] ).to.deep.equal( {} );
+			expect( spy.lastCall.args[ 0 ] ).to.not.equal( options );
 
-			data.stringify( modelDocumentFragment, { foo: 'bar' } );
-			expect( spy.lastCall.args[ 0 ] ).to.deep.equal( { foo: 'bar' } );
+			data.stringify( modelDocumentFragment, options );
+			expect( spy.lastCall.args[ 0 ] ).to.equal( options );
 		} );
 	} );
 
@@ -684,19 +686,33 @@ describe( 'DataController', () => {
 		} );
 
 		it( 'should allow to provide additional options to the conversion process', () => {
+			const root = model.document.getRoot();
 			const spy = sinon.spy();
 
 			data.downcastDispatcher.on( 'insert:paragraph', ( evt, data, conversionApi ) => {
 				spy( conversionApi.options );
 			}, { priority: 'high' } );
 
-			const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
+			data.downcastDispatcher.on( 'addMarker:marker', ( evt, data, conversionApi ) => {
+				spy( conversionApi.options );
+			}, { priority: 'high' } );
+
+			setData( model, '<paragraph>foo</paragraph>' );
+
+			model.change( writer => {
+				writer.addMarker( 'marker', {
+					range: model.createRange( model.createPositionFromPath( root, [ 0, 1 ] ) ),
+					usingOperation: false
+				} );
+			} );
+
+			const options = { foo: 'bar' };
 
-			data.toView( modelDocumentFragment );
-			expect( spy.lastCall.args[ 0 ] ).to.deep.equal( {} );
+			data.toView( root, options );
 
-			data.toView( modelDocumentFragment, { foo: 'bar' } );
-			expect( spy.lastCall.args[ 0 ] ).to.deep.equal( { foo: 'bar' } );
+			sinon.assert.calledTwice( spy );
+			expect( spy.firstCall.args[ 0 ] ).to.equal( options );
+			expect( spy.lastCall.args[ 0 ] ).to.equal( options );
 		} );
 	} );
 

+ 0 - 41
packages/ckeditor5-engine/tests/conversion/downcastdispatcher.js

@@ -257,31 +257,6 @@ describe( 'DowncastDispatcher', () => {
 			expect( dispatcher.fire.calledWith( 'attribute:bold:image' ) ).to.be.false;
 			expect( dispatcher.fire.calledWith( 'insert:caption' ) ).to.be.false;
 		} );
-
-		it( 'should allow to provide an additional options', () => {
-			root._appendChild( [
-				new ModelText( 'foo', { bold: true } )
-			] );
-
-			const range = model.createRangeIn( root );
-			const conversionOptions = { foo: 'bar' };
-			const spy = sinon.spy();
-
-			dispatcher.on( 'insert', ( evt, data, conversionApi ) => {
-				expect( conversionApi.options ).to.equal( conversionOptions );
-				spy();
-			} );
-
-			dispatcher.on( 'attribute', ( evt, data, conversionApi ) => {
-				expect( conversionApi.options ).to.equal( conversionOptions );
-				spy();
-			} );
-
-			dispatcher.convertInsert( range, {}, conversionOptions );
-
-			// To be sure all assertions was checked.
-			sinon.assert.calledTwice( spy );
-		} );
 	} );
 
 	describe( 'convertRemove', () => {
@@ -655,22 +630,6 @@ describe( 'DowncastDispatcher', () => {
 			// Called once for each item, twice total.
 			expect( highAddMarkerSpy.calledTwice ).to.be.true;
 		} );
-
-		it( 'should allow to provide an additional options', () => {
-			const range = model.createRange( model.createPositionAt( element, 2 ), model.createPositionAt( element, 2 ) );
-			const conversionOptions = { foo: 'bar' };
-			const spy = sinon.spy();
-
-			dispatcher.on( 'addMarker:name', ( evt, data, conversionApi ) => {
-				expect( conversionApi.options ).to.equal( conversionOptions );
-				spy();
-			} );
-
-			dispatcher.convertMarkerAdd( 'name', range, {}, conversionOptions );
-
-			// To be sure all assertions was checked.
-			sinon.assert.called( spy );
-		} );
 	} );
 
 	describe( 'convertMarkerRemove', () => {