Explorar el Código

Used position and offset in DetachOperation.

Oskar Wróbel hace 8 años
padre
commit
1adac5868e

+ 26 - 8
packages/ckeditor5-engine/src/model/operation/detachoperation.js

@@ -8,6 +8,8 @@
  */
 
 import Operation from './operation';
+import Position from '../position';
+import Range from '../range';
 import { remove } from '../writer';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 
@@ -21,19 +23,28 @@ export default class DetachOperation extends Operation {
 	/**
 	 * Creates an insert operation.
 	 *
-	 * @param {module:engine/model/range~Range} range Range to remove.
+	 * @param {module:engine/model/position~Position} sourcePosition
+	 * Position before the first {@link module:engine/model/item~Item model item} to move.
+	 * @param {Number} howMany Offset size of moved range. Moved range will start from `sourcePosition` and end at
+	 * `sourcePosition` with offset shifted by `howMany`.
 	 * @param {Number} baseVersion {@link module:engine/model/document~Document#version} on which operation can be applied.
 	 */
-	constructor( range, baseVersion ) {
+	constructor( sourcePosition, howMany, baseVersion ) {
 		super( baseVersion );
 
 		/**
-		 * Node to remove.
+		 * Position before the first {@link module:engine/model/item~Item model item} to detach.
 		 *
-		 * @readonly
-		 * @member {module:engine/model/range~Range} #range
+		 * @member {module:engine/model/position~Position} #sourcePosition
 		 */
-		this.range = range;
+		this.sourcePosition = Position.createFromPosition( sourcePosition );
+
+		/**
+		 * Offset size of moved range.
+		 *
+		 * @member {Number} #howMany
+		 */
+		this.howMany = howMany;
 	}
 
 	/**
@@ -54,7 +65,7 @@ export default class DetachOperation extends Operation {
 	 * @inheritDoc
 	 */
 	_execute() {
-		if ( this.range.root.document ) {
+		if ( this.sourcePosition.root.document ) {
 			/**
 			 * Cannot detach document node.
 			 * Use {@link module:engine/model/operation/removeoperation~RemoveOperation remove operation} instead.
@@ -64,8 +75,15 @@ export default class DetachOperation extends Operation {
 			throw new CKEditorError( 'detach-operation-on-document-node: Cannot detach document node.' );
 		}
 
-		const nodes = remove( this.range );
+		const nodes = remove( Range.createFromPositionAndShift( this.sourcePosition, this.howMany ) );
 
 		return { nodes };
 	}
+
+	/**
+	 * @inheritDoc
+	 */
+	static get className() {
+		return 'engine.model.operation.DetachOperation';
+	}
 }

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

@@ -7,7 +7,7 @@ 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';
+import Position from '../../../src/model/position';
 
 describe( 'DetachOperation', () => {
 	let doc, batch, docFrag, element;
@@ -22,13 +22,13 @@ describe( 'DetachOperation', () => {
 	} );
 
 	it( 'should have type equal to detach', () => {
-		const op = new DetachOperation( element, doc.version );
+		const op = new DetachOperation( Position.createBefore( element ), 1, doc.version );
 
 		expect( op.type ).to.equal( 'detach' );
 	} );
 
 	it( 'should remove given element from parent', () => {
-		const op = new DetachOperation( Range.createOn( element ), doc.version );
+		const op = new DetachOperation( Position.createBefore( element ), 1, doc.version );
 
 		doc.applyOperation( wrapInDelta( op ) );
 
@@ -40,7 +40,7 @@ describe( 'DetachOperation', () => {
 		const element = batch.createElement( 'element' );
 		batch.append( element, root );
 
-		const op = new DetachOperation( Range.createOn( element ), doc.version );
+		const op = new DetachOperation( Position.createBefore( element ), 1, doc.version );
 
 		expect( () => {
 			op._execute();
@@ -48,7 +48,7 @@ describe( 'DetachOperation', () => {
 	} );
 
 	it( 'should be not a document operation', () => {
-		const op = new DetachOperation( element, doc.version );
+		const op = new DetachOperation( Position.createBefore( element ), 1, doc.version );
 
 		expect( op.isDocumentOperation ).to.false;
 	} );