Bläddra i källkod

Added InsertTextDelta.

Szymon Cofalik 10 år sedan
förälder
incheckning
01862f1663

+ 55 - 0
packages/ckeditor5-engine/src/treemodel/delta/inserttextdelta.js

@@ -0,0 +1,55 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+CKEDITOR.define( [
+	'treemodel/delta/delta',
+	'treemodel/delta/register',
+	'treemodel/operation/insertoperation',
+	'treemodel/nodelist'
+], ( Delta, register, InsertOperation, NodeList ) => {
+	/**
+	 * To provide specific OT behavior and better collisions solving, the {@link treeModel.Batch#insert} method
+	 * uses the `InsertTextDelta` class which inherits from the `Delta` class and may overwrite some methods.
+	 *
+	 * @class treeModel.delta.InsertTextDelta
+	 */
+	class InsertTextDelta extends Delta {}
+
+	/**
+	 * Inserts a node or nodes at the given position. The nodes will have same attributes as the current attributes of
+	 * {@link treeModel.Document#selection document selection}. Commonly used for typing or plain-text paste (without formatting).
+	 *
+	 * @chainable
+	 * @memberOf treeModel.Batch
+	 * @method insertText
+	 * @param {treeModel.Position} position Position of insertion.
+	 * @param {treeModel.Node|treeModel.Text|treeModel.NodeList|String|Iterable} nodes The list of nodes to be inserted.
+	 * List of nodes can be of any type accepted by the {@link treeModel.NodeList} constructor.
+	 */
+	register( 'insertText', function( position, nodes ) {
+		const delta = new InsertTextDelta();
+
+		/* istanbul ignore else */
+		if ( !( nodes instanceof NodeList ) ) {
+			nodes = new NodeList( nodes );
+		}
+
+		for ( let node of new NodeList( nodes ) ) {
+			node.setAttrsTo( this.doc.selection.getAttrs() );
+		}
+
+		const operation = new InsertOperation( position, nodes, this.doc.version );
+		this.doc.applyOperation( operation );
+		delta.addOperation( operation );
+
+		this.addDelta( delta );
+
+		return this;
+	} );
+
+	return InsertTextDelta;
+} );

+ 63 - 0
packages/ckeditor5-engine/tests/treemodel/delta/inserttextdelta.js

@@ -0,0 +1,63 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* bender-tags: treemodel, delta */
+
+'use strict';
+
+const modules = bender.amd.require(
+	'treemodel/document',
+	'treemodel/attribute',
+	'treemodel/position'
+);
+
+describe( 'Batch', () => {
+	let Document, Attribute, Position;
+
+	before( () => {
+		Document = modules[ 'treemodel/document' ];
+		Attribute = modules[ 'treemodel/attribute' ];
+		Position = modules[ 'treemodel/position' ];
+	} );
+
+	let doc, root, batch, chain, attrs;
+
+	beforeEach( () => {
+		doc = new Document();
+		root = doc.createRoot( 'root' );
+
+		root.insertChildren( 0, 'abc' );
+
+		batch = doc.batch();
+
+		attrs = [
+			new Attribute( 'bold', true ),
+			new Attribute( 'foo', 'bar' )
+		];
+
+		doc.selection.setAttrsTo( attrs );
+
+		chain = batch.insertText( new Position( root, [ 2 ] ), 'xyz' );
+	} );
+
+	describe( 'insert', () => {
+		it( 'should insert given nodes at given position', () => {
+			expect( root.getChildCount() ).to.equal( 6 );
+			expect( root.getChild( 2 ).character ).to.equal( 'x' );
+			expect( root.getChild( 3 ).character ).to.equal( 'y' );
+			expect( root.getChild( 4 ).character ).to.equal( 'z' );
+		} );
+
+		it( 'should set inserted nodes attributes to same as current selection attributes', () => {
+			expect( Array.from( root.getChild( 2 ).getAttrs() ) ).to.deep.equal( attrs );
+			expect( Array.from( root.getChild( 3 ).getAttrs() ) ).to.deep.equal( attrs );
+			expect( Array.from( root.getChild( 4 ).getAttrs() ) ).to.deep.equal( attrs );
+		} );
+
+		it( 'should be chainable', () => {
+			expect( chain ).to.equal( batch );
+		} );
+	} );
+} );