From 244a6a43f20ef65a83e14fb97d2d914894192769 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Chrze=C5=9Bcian?= Date: Mon, 11 Jan 2021 18:44:46 +0100 Subject: [PATCH 1/3] Supporting batchType option in the setData method. --- packages/ckeditor5-engine/src/dev-utils/model.js | 10 ++++++++-- .../ckeditor5-engine/tests/dev-utils/model.js | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/ckeditor5-engine/src/dev-utils/model.js b/packages/ckeditor5-engine/src/dev-utils/model.js index 57cde34a686..97cec9aac22 100644 --- a/packages/ckeditor5-engine/src/dev-utils/model.js +++ b/packages/ckeditor5-engine/src/dev-utils/model.js @@ -128,7 +128,13 @@ export function setData( model, data, options = {} ) { modelDocumentFragment = parsedResult; } - model.change( writer => { + if ( typeof options.batchType === 'string' ) { + model.enqueueChange( options.batchType, writeToModel ); + } else { + model.change( writeToModel ); + } + + function writeToModel( writer ) { // Replace existing model in document by new one. writer.remove( writer.createRangeIn( modelRoot ) ); writer.insert( modelDocumentFragment, modelRoot ); @@ -154,7 +160,7 @@ export function setData( model, data, options = {} ) { writer.setSelectionAttribute( selection.getAttributes() ); } } - } ); + }; } // Set parse as setData private method - needed for testing/spying. diff --git a/packages/ckeditor5-engine/tests/dev-utils/model.js b/packages/ckeditor5-engine/tests/dev-utils/model.js index 6ef1a5b0cf8..6c78b2efd70 100644 --- a/packages/ckeditor5-engine/tests/dev-utils/model.js +++ b/packages/ckeditor5-engine/tests/dev-utils/model.js @@ -130,6 +130,22 @@ describe( 'model test utils', () => { expect( args[ 0 ] ).to.equal( data ); } ); + it( 'should use model#enqueueChange method if the batchType option was provided', () => { + const changeSpy = sinon.spy( model, 'enqueueChange' ); + const batchType = 'default'; + setData( model, 'text', { batchType } ); + + sinon.assert.calledTwice( changeSpy ); + sinon.assert.calledWith( changeSpy, batchType ); + } ); + + it( 'should use model#change method if no batchType option was provided', () => { + const changeSpy = sinon.spy( model, 'change' ); + setData( model, 'text', {} ); + + sinon.assert.calledOnce( changeSpy ); + } ); + it( 'should insert text', () => { testUtils( 'this is test text', '[]this is test text' ); } ); From 29b6101c9511f6bcb050b1bdde12ff2ed11698e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Chrze=C5=9Bcian?= Date: Tue, 12 Jan 2021 09:56:06 +0100 Subject: [PATCH 2/3] Unnecessary semicolon removed. --- packages/ckeditor5-engine/src/dev-utils/model.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ckeditor5-engine/src/dev-utils/model.js b/packages/ckeditor5-engine/src/dev-utils/model.js index 97cec9aac22..ad2ee1538ca 100644 --- a/packages/ckeditor5-engine/src/dev-utils/model.js +++ b/packages/ckeditor5-engine/src/dev-utils/model.js @@ -160,7 +160,7 @@ export function setData( model, data, options = {} ) { writer.setSelectionAttribute( selection.getAttributes() ); } } - }; + } } // Set parse as setData private method - needed for testing/spying. From d46906a666ca6d458e066d41025978f5a89179f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Chrze=C5=9Bcian?= Date: Tue, 12 Jan 2021 12:10:10 +0100 Subject: [PATCH 3/3] Updated default batchType in docs. --- packages/ckeditor5-engine/src/dev-utils/model.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ckeditor5-engine/src/dev-utils/model.js b/packages/ckeditor5-engine/src/dev-utils/model.js index ad2ee1538ca..36fc443d05c 100644 --- a/packages/ckeditor5-engine/src/dev-utils/model.js +++ b/packages/ckeditor5-engine/src/dev-utils/model.js @@ -102,7 +102,7 @@ getData._stringify = stringify; * name will be used. * @param {Array} [options.selectionAttributes] A list of attributes which will be passed to the selection. * @param {Boolean} [options.lastRangeBackward=false] If set to `true`, the last range will be added as backward. - * @param {String} [options.batchType='transparent'] Batch type used for inserting elements. + * @param {String} [options.batchType='default'] Batch type used for inserting elements. * See {@link module:engine/model/batch~Batch#type}. */ export function setData( model, data, options = {} ) {