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

Introduced Model#createOperationFromJSON #1815

Merged
merged 2 commits into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions src/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import ModelElement from './element';
import ModelRange from './range';
import ModelPosition from './position';
import ModelSelection from './selection';
import OperationFactory from './operation/operationfactory';

import insertContent from './utils/insertcontent';
import deleteContent from './utils/deletecontent';
Expand Down Expand Up @@ -755,6 +756,18 @@ export default class Model {
return new Batch( type );
}

/**
* Creates an `Operation` instance from a JSON object with the operation data (e.g. taken from a parsed JSON string).
*
* This is an alias for {@link module:engine/model/operation/operationfactory~OperationFactory#fromJSON}.
*
* @param {Object} json Deserialized JSON object.
* @returns {module:engine/model/operation/operation~Operation}
*/
createOperationFromJSON( json ) {
return OperationFactory.fromJSON( json, this.document );
}

/**
* Removes all events listeners set by model instance and destroys {@link module:engine/model/document~Document}.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/model/operation/operationfactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ operations[ MergeOperation.className ] = MergeOperation;
*/
export default class OperationFactory {
/**
* Creates concrete `Operation` object from deserialized object, i.e. from parsed JSON string.
* Creates an `Operation` instance from a JSON object with the operation data (e.g. taken from a parsed JSON string).
*
* @param {Object} json Deserialized JSON object.
* @param {module:engine/model/document~Document} document Document on which this operation will be applied.
Expand Down
13 changes: 13 additions & 0 deletions tests/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import ModelPosition from '../../src/model/position';
import ModelSelection from '../../src/model/selection';
import ModelDocumentFragment from '../../src/model/documentfragment';
import Batch from '../../src/model/batch';
import NoOperation from '../../src/model/operation/nooperation';
import { getData, setData, stringify } from '../../src/dev-utils/model';
import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
Expand Down Expand Up @@ -826,6 +827,18 @@ describe( 'Model', () => {
} );
} );

describe( 'createOperationFromJson()', () => {
it( 'should create operation from JSON', () => {
const operation = model.createOperationFromJSON( {
__className: 'NoOperation',
baseVersion: 0
} );

expect( operation ).to.instanceof( NoOperation );
expect( operation.baseVersion ).to.equal( 0 );
} );
} );

describe( 'destroy()', () => {
it( 'should destroy document', () => {
sinon.spy( model.document, 'destroy' );
Expand Down