Sfoglia il codice sorgente

Added DetachOperation.

Oskar Wróbel 8 anni fa
parent
commit
b475704eee

+ 71 - 0
packages/ckeditor5-engine/src/model/operation/detachoperation.js

@@ -0,0 +1,71 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/**
+ * @module engine/model/operation/detachoperation
+ */
+
+import Operation from './operation';
+import { remove } from '../writer';
+import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
+
+/**
+ * Operation to permanently remove node from detached root.
+ * Note this operation is only a local operation and won't be send to the other clients.
+ *
+ * @extends module:engine/model/operation/operation~Operation
+ */
+export default class DetachOperation extends Operation {
+	/**
+	 * Creates an insert operation.
+	 *
+	 * @param {module:engine/model/range~Range} range Range to remove.
+	 * @param {Number} baseVersion {@link module:engine/model/document~Document#version} on which operation can be applied.
+	 */
+	constructor( range, baseVersion ) {
+		super( baseVersion );
+
+		/**
+		 * Node to remove.
+		 *
+		 * @readonly
+		 * @member {module:engine/model/range~Range} #range
+		 */
+		this.range = range;
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	get type() {
+		return 'detach';
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	get isDocumentOperation() {
+		return false;
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	_execute() {
+		if ( this.range.root.document ) {
+			/**
+			 * Cannot detach document node.
+			 * Use {@link module:engine/model/operation/removeoperation~RemoveOperation remove operation} instead.
+			 *
+			 * @error detach-operation-on-document-node
+			 */
+			throw new CKEditorError( 'detach-operation-on-document-node: Cannot detach document node.' );
+		}
+
+		const nodes = remove( this.range );
+
+		return { nodes };
+	}
+}

+ 55 - 0
packages/ckeditor5-engine/tests/model/operation/detachoperation.js

@@ -0,0 +1,55 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import Document from '../../../src/model/document';
+import DetachOperation from '../../../src/model/operation/detachoperation';
+import { wrapInDelta } from '../../../tests/model/_utils/utils';
+import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
+import Range from '../../../src/model/range';
+
+describe( 'DetachOperation', () => {
+	let doc, batch, docFrag, element;
+
+	beforeEach( () => {
+		doc = new Document();
+		batch = doc.batch();
+
+		docFrag = batch.createDocumentFragment();
+		element = batch.createElement( 'element' );
+		batch.append( element, docFrag );
+	} );
+
+	it( 'should have type equal to detach', () => {
+		const op = new DetachOperation( element, doc.version );
+
+		expect( op.type ).to.equal( 'detach' );
+	} );
+
+	it( 'should remove given element from parent', () => {
+		const op = new DetachOperation( Range.createOn( element ), doc.version );
+
+		doc.applyOperation( wrapInDelta( op ) );
+
+		expect( docFrag.childCount ).to.equal( 0 );
+	} );
+
+	it( 'should throw when is executed on element from document', () => {
+		const root = doc.createRoot();
+		const element = batch.createElement( 'element' );
+		batch.append( element, root );
+
+		const op = new DetachOperation( Range.createOn( element ), doc.version );
+
+		expect( () => {
+			op._execute();
+		} ).to.throw( CKEditorError, /^detach-operation-on-document-node/ );
+	} );
+
+	it( 'should be not a document operation', () => {
+		const op = new DetachOperation( element, doc.version );
+
+		expect( op.isDocumentOperation ).to.false;
+	} );
+} );