瀏覽代碼

#396: Introduce OperationFactory.

Maciej Gołaszewski 9 年之前
父節點
當前提交
9242a812de

+ 2 - 19
packages/ckeditor5-engine/src/treemodel/delta/delta.js

@@ -9,14 +9,7 @@ import clone from '../../../utils/lib/lodash/clone.js';
 
 import CKEditorError from '../../../utils/ckeditorerror.js';
 
-import AttributeOperation from '../operation/attributeoperation.js';
-import InsertOperation from '../operation/insertoperation.js';
-import MoveOperation from '../operation/moveoperation.js';
-import NoOperation from '../operation/nooperation.js';
-import Operation from '../operation/operation.js';
-import ReinsertOperation from '../operation/reinsertoperation.js';
-import RemoveOperation from '../operation/removeoperation.js';
-import RootAttributeOperation from '../operation/rootattributeoperation.js';
+import OperationFactory from '../operation/operationfactory.js';
 
 /**
  * Base class for all deltas.
@@ -195,23 +188,13 @@ export default class Delta {
 		let delta = new Constructor();
 
 		if ( json.operations.length ) {
-			json.operations.forEach( ( operation ) => delta.addOperation( operations[ operation.__className ].fromJSON( operation, doc ) ) );
+			json.operations.forEach( ( operation ) => delta.addOperation( OperationFactory.fromJSON( operation, doc ) ) );
 		}
 
 		return delta;
 	}
 }
 
-const operations = {};
-operations[ AttributeOperation.className ] = AttributeOperation;
-operations[ InsertOperation.className ] = InsertOperation;
-operations[ MoveOperation.className ] = MoveOperation;
-operations[ NoOperation.className ] = NoOperation;
-operations[ Operation.className ] = Operation;
-operations[ ReinsertOperation.className ] = ReinsertOperation;
-operations[ RemoveOperation.className ] = RemoveOperation;
-operations[ RootAttributeOperation.className ] = RootAttributeOperation;
-
 const deserializers = new Map();
 
 export function registerDeserializer( className, constructor ) {

+ 44 - 0
packages/ckeditor5-engine/src/treemodel/operation/operationfactory.js

@@ -0,0 +1,44 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import AttributeOperation from '../operation/attributeoperation.js';
+import InsertOperation from '../operation/insertoperation.js';
+import MoveOperation from '../operation/moveoperation.js';
+import NoOperation from '../operation/nooperation.js';
+import Operation from '../operation/operation.js';
+import ReinsertOperation from '../operation/reinsertoperation.js';
+import RemoveOperation from '../operation/removeoperation.js';
+import RootAttributeOperation from '../operation/rootattributeoperation.js';
+
+const operations = {};
+operations[ AttributeOperation.className ] = AttributeOperation;
+operations[ InsertOperation.className ] = InsertOperation;
+operations[ MoveOperation.className ] = MoveOperation;
+operations[ NoOperation.className ] = NoOperation;
+operations[ Operation.className ] = Operation;
+operations[ ReinsertOperation.className ] = ReinsertOperation;
+operations[ RemoveOperation.className ] = RemoveOperation;
+operations[ RootAttributeOperation.className ] = RootAttributeOperation;
+
+/**
+ * A factory class for creating operations.
+ *
+ * @abstract
+ * @memberOf engine.treeModel.operation
+ */
+export default class OperationFactory {
+	/**
+	 * Creates concrete Operation object from deserilized object, i.e. from parsed JSON string.
+	 *
+	 * @param {Object} json Deserialized JSON object.
+	 * @param {engine.treeModel.Document} document Document on which this operation will be applied.
+	 * @returns {engine.treeModel.operation.Operation}
+	 */
+	static fromJSON( json, document ) {
+		return operations[ json.__className ].fromJSON( json, document );
+	}
+}