Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting batchType option in the setData method. #8807

Merged
merged 3 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/ckeditor5-engine/src/dev-utils/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ getData._stringify = stringify;
* name will be used.
* @param {Array<Object>} [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 = {} ) {
Expand All @@ -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 );
Expand All @@ -154,7 +160,7 @@ export function setData( model, data, options = {} ) {
writer.setSelectionAttribute( selection.getAttributes() );
}
}
} );
}
}

// Set parse as setData private method - needed for testing/spying.
Expand Down
16 changes: 16 additions & 0 deletions packages/ckeditor5-engine/tests/dev-utils/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
} );
Expand Down