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

Commit

Permalink
Merge pull request #1815 from ckeditor/i/6094
Browse files Browse the repository at this point in the history
Feature: Introduced `Model#createOperationFromJSON()` which is an alias for `OperationFactory.fromJSON()`. Closes ckeditor/ckeditor5#6094.
  • Loading branch information
Reinmar authored Jan 29, 2020
2 parents 335d68d + 5238082 commit ebaa2cc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
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 (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 (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

0 comments on commit ebaa2cc

Please sign in to comment.