Selaa lähdekoodia

Working on...

Piotr Jasiun 9 vuotta sitten
vanhempi
sitoutus
de32b15e2c

+ 5 - 1
packages/ckeditor5-engine/src/treemodel/element.js

@@ -122,10 +122,14 @@ export default class Element extends Node {
 	 *
 	 * @method core.treeModel.Element#removeChildren
 	 * @param {Number} index Position of the first node to remove.
-	 * @param {Number} number Number of nodes to remove.
+	 * @param {Number} [number] Number of nodes to remove.
 	 * @returns {core.treeModel.NodeList} The list of removed nodes.
 	 */
 	removeChildren( index, number ) {
+		if ( !number ) {
+			number = 1;
+		}
+
 		let nodeList = this._children.remove( index, number );
 
 		for ( let node of nodeList._nodes ) {

+ 45 - 1
packages/ckeditor5-engine/src/treeview/element.js

@@ -65,6 +65,18 @@ export default class Element extends Node {
 		}
 	}
 
+	cloneNode( deep ) {
+		const childrenClone = [];
+
+		if ( deep ) {
+			for ( child of this.getChildren() ) {
+				childrenClone.push( child.cloneNode( deep ) );
+			}
+		}
+
+		return new this( this.name, this._attrs, childrenClone );
+	}
+
 	/**
 	 * {@link core.treeView.Element#insert Insert} a child node or a list of child nodes at the end of this node and sets
 	 * the parent of these nodes to this element.
@@ -196,17 +208,49 @@ export default class Element extends Node {
 	 * Removes number of child nodes starting at the given index and set the parent of these nodes to `null`.
 	 *
 	 * @param {Number} index Number of the first node to remove.
-	 * @param {Number} number Number of nodes to remove.
+	 * @param {Number} [number] Number of nodes to remove.
 	 * @returns {Array.<core.treeView.Node>} The array of removed nodes.
 	 * @fires core.treeView.Node#change
 	 */
 	removeChildren( index, number ) {
 		this._fireChange( 'CHILDREN', this );
 
+		if ( !number ) {
+			number = 1;
+		}
+
 		for ( let i = index; i < index + number; i++ ) {
 			this._children[ i ].parent = null;
 		}
 
 		return this._children.splice( index, number );
 	}
+
+	same( otherNode ) {
+		if ( !otherNode instanceof Element ) {
+			return false;
+		}
+
+		if ( this.name != otherNode.name ) {
+			return false;
+		}
+
+		const thisNodeAttrKeys = this.getAttributeKeys();
+		const otherNodeAttrKeys = this.getAttributeKeys();
+		let count = 0;
+
+		for ( key of thisNodeAttrKeys ) {
+			if ( this.getAttribute( key ) !== otherNode.getAttribute( key ) ) {
+				return false;
+			}
+
+			count++;
+		}
+
+		if ( count != utils.count( otherNodeAttrKeys ) ) {
+			return false;
+		}
+
+		return true;
+	}
 }

+ 4 - 0
packages/ckeditor5-engine/src/treeview/node.js

@@ -115,6 +115,10 @@ export default class Node {
 		this._treeView = treeView;
 	}
 
+	remove() {
+		this.parent.removeChildren( this.getIndex() )
+	}
+
 	/**
 	 * @param {core.treeView.ChangeType} type Type of the change.
 	 * @param {core.treeView.Node} node Changed node.

+ 25 - 0
packages/ckeditor5-engine/src/treeview/position.js

@@ -33,4 +33,29 @@ export default class Position {
 		 */
 		this.offset = offset;
 	}
+
+	/**
+	 * Returns a new instance of Position with offset incremented by `shift` value.
+	 *
+	 * @param {Number} shift How position offset should get changed. Accepts negative values.
+	 * @returns {treeView.Position} Shifted position.
+	 */
+	getShiftedBy( shift ) {
+		let shifted = Position.createFromPosition( this );
+
+		let offset = shifted.offset + shift;
+		shifted.offset = offset < 0 ? 0 : offset;
+
+		return shifted;
+	}
+
+	/**
+	 * Creates and returns a new instance of Position, which is equal to passed position.
+	 *
+	 * @param {treeModel.Position} position Position to be cloned.
+	 * @returns {treeModel.Position}
+	 */
+	static createFromPosition( position ) {
+		return new this( position.parent, position.offset );
+	}
 }

+ 17 - 0
packages/ckeditor5-engine/src/treeview/text.js

@@ -33,6 +33,15 @@ export default class Text extends Node {
 		this._data = data;
 	}
 
+	cloneNode() {
+		return new this( this.data );
+	}
+
+	/**
+	 * The text content.
+	 *
+	 * Setting the data fires the {@link treeView.Node#change change event}.
+	 */
 	get data() {
 		return this._data;
 	}
@@ -42,4 +51,12 @@ export default class Text extends Node {
 
 		this._data = data;
 	}
+
+	same( otherNode ) {
+		if ( !otherNode instanceof Text ) {
+			return false;
+		}
+
+		return this.data === otherNode.data;
+	}
 }

+ 3 - 0
packages/ckeditor5-engine/src/treeview/treeview.js

@@ -8,6 +8,7 @@
 import EmitterMixin from '../emittermixin.js';
 import Renderer from './renderer.js';
 import Converter from './converter.js';
+import Writer from './writer.js';
 
 import utils from '../utils.js';
 
@@ -47,6 +48,8 @@ export default class TreeView {
 		 */
 		this.observers = new Set();
 
+		this.writer = new Writer();
+
 		/**
 		 * Instance of the {@link core.treeView.Converter converter} use by {@link core.treeView.TreeView#renderer renderer} and
 		 * {@link core.treeView.TreeView#observers observers}.

+ 18 - 0
packages/ckeditor5-engine/src/utils.js

@@ -175,6 +175,24 @@ const utils = {
 		return null;
 	},
 
+	/**
+	 * Returns the number of items return by the iterator.
+	 *
+	 *		utils.count( [ 1, 2, 3, 4, 5 ] ); // 5;
+	 *
+	 * @param {Iterable.<*>} iterator Any iterator.
+	 * @returns {Number} Number of items returned by that iterator.
+	 */
+	count( iterator ) {
+		let count = 0;
+
+		for ( let _ of iterator ) { // jshint ignore:line
+			count++;
+		}
+
+		return count;
+	},
+
 	/**
 	 * Copies enumerable properties and symbols from the objects given as 2nd+ parameters to the
 	 * prototype of first object (a constructor).

+ 1 - 19
packages/ckeditor5-engine/tests/_utils/utils.js

@@ -36,24 +36,6 @@ const utils = {
 		} );
 	},
 
-	/**
-	 * Returns the number of elements return by the iterator.
-	 *
-	 *		testUtils.getIteratorCount( [ 1, 2, 3, 4, 5 ] ); // 5;
-	 *
-	 * @param {Iterable.<*>} iterator Any iterator.
-	 * @returns {Number} Number of elements returned by that iterator.
-	 */
-	getIteratorCount( iterator ) {
-		let count = 0;
-
-		for ( let _ of iterator ) { // jshint ignore:line
-			count++;
-		}
-
-		return count;
-	},
-
 	/**
 	 * Creates an instance inheriting from {@link core.EmitterMixin} with one additional method `observe()`.
 	 * It allows observing changes to attributes in objects being {@link core.Observable observable}.
@@ -79,7 +61,7 @@ const utils = {
 			observe: {
 				value: function observe( observableName, observable ) {
 					observer.listenTo( observable, 'change', ( evt, propertyName, value, oldValue ) => {
-						console.log( `[Change in ${ observableName }] ${ propertyName } = '${ value }' (was '${ oldValue }')` );
+						console.log( `[ Change in $ { observableName } ] $ { propertyName } = '${ value }' ( was '${ oldValue }' )` );
 					} );
 
 					return observer;