Bladeren bron

Feature: Introduced `Model#createOperationFromJSON` which is an alias for `OperationFactory#fromJSON`.

Szymon Cofalik 5 jaren geleden
bovenliggende
commit
9f2afa3174

+ 13 - 0
packages/ckeditor5-engine/src/model/model.js

@@ -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';
@@ -756,6 +757,18 @@ export default class Model {
 	}
 
 	/**
+	 * 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}.
 	 */
 	destroy() {

+ 1 - 1
packages/ckeditor5-engine/src/model/operation/operationfactory.js

@@ -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.

+ 13 - 0
packages/ckeditor5-engine/tests/model/model.js

@@ -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';
@@ -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' );