Sfoglia il codice sorgente

Introduced insert delta.

Piotr Jasiun 10 anni fa
parent
commit
a6451a5174

+ 29 - 0
packages/ckeditor5-utils/src/document/delta/insertdelta.js

@@ -0,0 +1,29 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+CKEDITOR.define( [
+	'document/delta/delta',
+	'document/delta/register',
+	'document/operation/insertoperation'
+], ( Delta, register, InsertOperation ) => {
+	/**
+	 * @class document.delta.InsertDelta
+	 */
+	class InsertDelta extends Delta {}
+
+	register( 'insert', ( doc, transaction, position, nodes ) => {
+		const delta = new InsertDelta();
+
+		const operation = new InsertOperation( position, nodes, doc.version );
+		doc.applyOperation( operation );
+		delta.addOperation( operation );
+
+		transaction.addDelta( delta );
+	} );
+
+	return InsertDelta;
+} );

+ 1 - 0
packages/ckeditor5-utils/src/document/transaction.js

@@ -7,6 +7,7 @@
 
 CKEDITOR.define( [
 	'document/delta/transaction-base',
+	'document/delta/insertdelta',
 	'document/delta/changedelta'
 ], ( Transaction ) => {
 	return Transaction;

+ 40 - 0
packages/ckeditor5-utils/tests/document/deltas/insertdelta.js

@@ -0,0 +1,40 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* bender-tags: document, delta */
+
+/* bender-include: ../../_tools/tools.js */
+
+'use strict';
+
+const modules = bender.amd.require(
+	'document/document',
+	'document/position' );
+
+describe( 'Transaction', () => {
+	let Document, Position;
+
+	let doc, root;
+
+	before( () => {
+		Document = modules[ 'document/document' ];
+		Position = modules[ 'document/position' ];
+	} );
+
+	beforeEach( () => {
+		doc = new Document();
+		root = doc.createRoot( 'root' );
+	} );
+
+	it( 'should insert text', () => {
+		const position = new Position( [ 0 ], root );
+		doc.makeTransaction().insert( position, 'foo' );
+
+		expect( root.getChildCount() ).to.equal( 3 );
+		expect( root.getChild( 0 ).character ).to.equal( 'f' );
+		expect( root.getChild( 1 ).character ).to.equal( 'o' );
+		expect( root.getChild( 2 ).character ).to.equal( 'o' );
+	} );
+} );