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

Commit ebaa2cc

Browse files
authored
Merge pull request #1815 from ckeditor/i/6094
Feature: Introduced `Model#createOperationFromJSON()` which is an alias for `OperationFactory.fromJSON()`. Closes ckeditor/ckeditor5#6094.
2 parents 335d68d + 5238082 commit ebaa2cc

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/model/model.js

+13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import ModelElement from './element';
1818
import ModelRange from './range';
1919
import ModelPosition from './position';
2020
import ModelSelection from './selection';
21+
import OperationFactory from './operation/operationfactory';
2122

2223
import insertContent from './utils/insertcontent';
2324
import deleteContent from './utils/deletecontent';
@@ -755,6 +756,18 @@ export default class Model {
755756
return new Batch( type );
756757
}
757758

759+
/**
760+
* Creates an operation instance from a JSON object (parsed JSON string).
761+
*
762+
* This is an alias for {@link module:engine/model/operation/operationfactory~OperationFactory#fromJSON}.
763+
*
764+
* @param {Object} json Deserialized JSON object.
765+
* @returns {module:engine/model/operation/operation~Operation}
766+
*/
767+
createOperationFromJSON( json ) {
768+
return OperationFactory.fromJSON( json, this.document );
769+
}
770+
758771
/**
759772
* Removes all events listeners set by model instance and destroys {@link module:engine/model/document~Document}.
760773
*/

src/model/operation/operationfactory.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ operations[ MergeOperation.className ] = MergeOperation;
3737
*/
3838
export default class OperationFactory {
3939
/**
40-
* Creates concrete `Operation` object from deserialized object, i.e. from parsed JSON string.
40+
* Creates an operation instance from a JSON object (parsed JSON string).
4141
*
4242
* @param {Object} json Deserialized JSON object.
4343
* @param {module:engine/model/document~Document} document Document on which this operation will be applied.

tests/model/model.js

+13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import ModelPosition from '../../src/model/position';
1111
import ModelSelection from '../../src/model/selection';
1212
import ModelDocumentFragment from '../../src/model/documentfragment';
1313
import Batch from '../../src/model/batch';
14+
import NoOperation from '../../src/model/operation/nooperation';
1415
import { getData, setData, stringify } from '../../src/dev-utils/model';
1516
import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
1617
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
@@ -826,6 +827,18 @@ describe( 'Model', () => {
826827
} );
827828
} );
828829

830+
describe( 'createOperationFromJson()', () => {
831+
it( 'should create operation from JSON', () => {
832+
const operation = model.createOperationFromJSON( {
833+
__className: 'NoOperation',
834+
baseVersion: 0
835+
} );
836+
837+
expect( operation ).to.instanceof( NoOperation );
838+
expect( operation.baseVersion ).to.equal( 0 );
839+
} );
840+
} );
841+
829842
describe( 'destroy()', () => {
830843
it( 'should destroy document', () => {
831844
sinon.spy( model.document, 'destroy' );

0 commit comments

Comments
 (0)