8
0
Pārlūkot izejas kodu

Merge branch t/ckeditor5-engine/1555

Internal: Use built-in factories of range, position and selection classes. Avoid importing things from the engine. See ckeditor/ckeditor5-engine#1555.
Piotrek Koszuliński 7 gadi atpakaļ
vecāks
revīzija
2a24ce5ef2

+ 1 - 2
packages/ckeditor5-undo/src/redocommand.js

@@ -8,7 +8,6 @@
  */
 
 import BaseCommand from './basecommand';
-import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
 
 /**
  * The redo command stores {@link module:engine/model/batch~Batch batches} that were used to undo a batch by
@@ -31,7 +30,7 @@ export default class RedoCommand extends BaseCommand {
 	 */
 	execute() {
 		const item = this._stack.pop();
-		const redoingBatch = new Batch();
+		const redoingBatch = this.editor.model.createBatch();
 
 		// All changes have to be done in one `enqueueChange` callback so other listeners will not step between consecutive
 		// operations, or won't do changes to the document before selection is properly restored.

+ 1 - 2
packages/ckeditor5-undo/src/undocommand.js

@@ -8,7 +8,6 @@
  */
 
 import BaseCommand from './basecommand';
-import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
 
 /**
  * The undo command stores {@link module:engine/model/batch~Batch batches} applied to the
@@ -34,7 +33,7 @@ export default class UndoCommand extends BaseCommand {
 		const batchIndex = batch ? this._stack.findIndex( a => a.batch == batch ) : this._stack.length - 1;
 
 		const item = this._stack.splice( batchIndex, 1 )[ 0 ];
-		const undoingBatch = new Batch();
+		const undoingBatch = this.editor.model.createBatch();
 
 		// All changes has to be done in one `enqueueChange` callback so other listeners will not
 		// step between consecutive operations, or won't do changes to the document before selection is properly restored.

+ 2 - 3
packages/ckeditor5-undo/tests/basecommand.js

@@ -4,7 +4,6 @@
  */
 
 import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
-import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
 import BaseCommand from '../src/basecommand';
 
 describe( 'BaseCommand', () => {
@@ -31,7 +30,7 @@ describe( 'BaseCommand', () => {
 		} );
 
 		it( 'should be true if there are batches in command stack', () => {
-			base.addBatch( new Batch() );
+			base.addBatch( editor.model.createBatch() );
 
 			expect( base.isEnabled ).to.be.true;
 		} );
@@ -39,7 +38,7 @@ describe( 'BaseCommand', () => {
 
 	describe( 'clearStack', () => {
 		it( 'should remove all batches from the stack', () => {
-			base.addBatch( new Batch() );
+			base.addBatch( editor.model.createBatch() );
 			base.clearStack();
 
 			expect( base.isEnabled ).to.be.false;

+ 5 - 7
packages/ckeditor5-undo/tests/redocommand.js

@@ -4,8 +4,6 @@
  */
 
 import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
-import Range from '@ckeditor/ckeditor5-engine/src/model/range';
-import Position from '@ckeditor/ckeditor5-engine/src/model/position';
 import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
 import UndoCommand from '../src/undocommand';
 import RedoCommand from '../src/redocommand';
@@ -30,8 +28,8 @@ describe( 'RedoCommand', () => {
 
 	describe( 'RedoCommand', () => {
 		describe( 'execute()', () => {
-			const p = pos => new Position( root, [].concat( pos ) );
-			const r = ( a, b ) => new Range( p( a ), p( b ) );
+			const p = pos => model.createPositionFromPath( root, [].concat( pos ) );
+			const r = ( a, b ) => model.createRange( p( a ), p( b ) );
 
 			let batch0, batch1, batch2;
 			const batches = new Set();
@@ -55,7 +53,7 @@ describe( 'RedoCommand', () => {
 					writer.setSelection( r( 0, 0 ) );
 				} );
 
-				batch0 = new Batch();
+				batch0 = model.createBatch();
 				undo.addBatch( batch0 );
 				model.enqueueChange( batch0, writer => {
 					writer.insertText( 'foobar', p( 0 ) );
@@ -74,7 +72,7 @@ describe( 'RedoCommand', () => {
 				model.change( writer => {
 					writer.setSelection( r( 2, 4 ), { backward: true } );
 				} );
-				batch1 = new Batch();
+				batch1 = model.createBatch();
 				undo.addBatch( batch1 );
 				model.enqueueChange( batch1, writer => {
 					writer.setAttribute( 'key', 'value', r( 2, 4 ) );
@@ -92,7 +90,7 @@ describe( 'RedoCommand', () => {
 				model.change( writer => {
 					writer.setSelection( r( 1, 3 ) );
 				} );
-				batch2 = new Batch();
+				batch2 = model.createBatch();
 				undo.addBatch( batch2 );
 				model.enqueueChange( batch2, writer => {
 					writer.move( r( 1, 3 ), p( 6 ) );

+ 9 - 11
packages/ckeditor5-undo/tests/undocommand.js

@@ -4,8 +4,6 @@
  */
 
 import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
-import Range from '@ckeditor/ckeditor5-engine/src/model/range';
-import Position from '@ckeditor/ckeditor5-engine/src/model/position';
 import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
 import UndoCommand from '../src/undocommand';
 import { itemAt, getText } from '@ckeditor/ckeditor5-engine/tests/model/_utils/utils';
@@ -29,8 +27,8 @@ describe( 'UndoCommand', () => {
 	} );
 
 	describe( 'UndoCommand', () => {
-		const p = pos => new Position( root, [].concat( pos ) );
-		const r = ( a, b ) => new Range( p( a ), p( b ) );
+		const p = pos => model.createPositionFromPath( root, [].concat( pos ) );
+		const r = ( a, b ) => model.createRange( p( a ), p( b ) );
 
 		describe( 'execute()', () => {
 			let batch0, batch1, batch2, batch3;
@@ -43,7 +41,7 @@ describe( 'UndoCommand', () => {
 				model.change( writer => {
 					writer.setSelection( r( 0, 0 ) );
 				} );
-				batch0 = new Batch();
+				batch0 = model.createBatch();
 				undo.addBatch( batch0 );
 				model.enqueueChange( batch0, writer => {
 					writer.insertText( 'foobar', p( 0 ) );
@@ -62,7 +60,7 @@ describe( 'UndoCommand', () => {
 				model.change( writer => {
 					writer.setSelection( r( 2, 4 ), { backward: true } );
 				} );
-				batch1 = new Batch();
+				batch1 = model.createBatch();
 				undo.addBatch( batch1 );
 				model.enqueueChange( batch1, writer => {
 					writer.setAttribute( 'key', 'value', r( 2, 4 ) );
@@ -80,7 +78,7 @@ describe( 'UndoCommand', () => {
 				model.change( writer => {
 					writer.setSelection( r( 1, 3 ) );
 				} );
-				batch2 = new Batch();
+				batch2 = model.createBatch();
 				undo.addBatch( batch2 );
 				model.enqueueChange( batch2, writer => {
 					writer.move( r( 1, 3 ), p( 6 ) );
@@ -98,7 +96,7 @@ describe( 'UndoCommand', () => {
 				model.change( writer => {
 					writer.setSelection( r( 1, 4 ) );
 				} );
-				batch3 = new Batch();
+				batch3 = model.createBatch();
 				undo.addBatch( batch3 );
 				model.enqueueChange( batch3, writer => {
 					writer.wrap( r( 1, 4 ), 'p' );
@@ -265,7 +263,7 @@ describe( 'UndoCommand', () => {
 				// Graveyard contains "foobar".
 				expect( doc.graveyard.maxOffset ).to.equal( 6 );
 
-				for ( const item of Range.createIn( doc.graveyard ).getItems() ) {
+				for ( const item of model.createRangeIn( doc.graveyard ).getItems() ) {
 					expect( item.hasAttribute( 'key' ) ).to.be.false;
 				}
 
@@ -332,7 +330,7 @@ describe( 'UndoCommand', () => {
 			model.change( writer => {
 				writer.setSelection( r( 1, 4 ) );
 			} );
-			const batch0 = new Batch();
+			const batch0 = model.createBatch();
 			undo.addBatch( batch0 );
 			model.enqueueChange( batch0, writer => {
 				writer.setAttribute( 'uppercase', true, r( 1, 4 ) );
@@ -342,7 +340,7 @@ describe( 'UndoCommand', () => {
 			model.change( writer => {
 				writer.setSelection( r( 3, 4 ) );
 			} );
-			const batch1 = new Batch();
+			const batch1 = model.createBatch();
 			undo.addBatch( batch1 );
 			model.enqueueChange( batch1, writer => {
 				writer.move( r( 3, 4 ), p( 1 ) );

+ 33 - 26
packages/ckeditor5-undo/tests/undoediting-integration.js

@@ -7,8 +7,6 @@
 
 import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
 
-import Range from '@ckeditor/ckeditor5-engine/src/model/range';
-import Position from '@ckeditor/ckeditor5-engine/src/model/position';
 import UndoEditing from '../src/undoediting';
 
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
@@ -49,7 +47,9 @@ describe( 'UndoEditing integration', () => {
 
 	function setSelection( pathA, pathB ) {
 		model.change( writer => {
-			writer.setSelection( new Range( new Position( root, pathA ), new Position( root, pathB ) ) );
+			writer.setSelection( writer.createRange(
+				writer.createPositionFromPath( root, pathA ), writer.createPositionFromPath( root, pathB )
+			) );
 		} );
 	}
 
@@ -91,7 +91,7 @@ describe( 'UndoEditing integration', () => {
 
 			model.change( writer => {
 				writer.insertText( 'zzz', doc.selection.getFirstPosition() );
-				writer.insertText( 'xxx', new Position( root, [ 1, 0 ] ) );
+				writer.insertText( 'xxx', writer.createPositionFromPath( root, [ 1, 0 ] ) );
 			} );
 
 			output( '<paragraph>fozzz[]o</paragraph><paragraph>xxxbar</paragraph>' );
@@ -149,13 +149,15 @@ describe( 'UndoEditing integration', () => {
 			input( '<paragraph>[]foo</paragraph><paragraph>bar</paragraph>' );
 
 			model.change( writer => {
-				writer.remove( Range.createFromPositionAndShift( doc.selection.getFirstPosition(), 2 ) );
+				const start = doc.selection.getFirstPosition();
+				writer.remove( writer.createRange( start, start.getShiftedBy( 2 ) ) );
 			} );
 			output( '<paragraph>[]o</paragraph><paragraph>bar</paragraph>' );
 
 			model.change( writer => {
 				setSelection( [ 1, 1 ], [ 1, 1 ] );
-				writer.remove( Range.createFromPositionAndShift( doc.selection.getFirstPosition(), 2 ) );
+				const start = doc.selection.getFirstPosition();
+				writer.remove( writer.createRange( start, start.getShiftedBy( 2 ) ) );
 			} );
 			output( '<paragraph>o</paragraph><paragraph>b[]</paragraph>' );
 
@@ -180,7 +182,8 @@ describe( 'UndoEditing integration', () => {
 
 			model.change( writer => {
 				setSelection( [ 1, 2 ], [ 1, 2 ] );
-				writer.remove( Range.createFromPositionAndShift( new Position( root, [ 1, 1 ] ), 1 ) );
+				const start = writer.createPositionFromPath( root, [ 1, 1 ] );
+				writer.remove( writer.createRange( start, start.getShiftedBy( 1 ) ) );
 			} );
 			output( '<paragraph>fozzzo</paragraph><paragraph>b[]r</paragraph>' );
 
@@ -202,7 +205,8 @@ describe( 'UndoEditing integration', () => {
 			output( '<paragraph>fozzz[]o</paragraph><paragraph>bar</paragraph>' );
 
 			model.change( writer => {
-				writer.remove( Range.createFromPositionAndShift( new Position( root, [ 0, 2 ] ), 3 ) );
+				const start = writer.createPositionFromPath( root, [ 0, 2 ] );
+				writer.remove( writer.createRange( start, start.getShiftedBy( 3 ) ) );
 			} );
 			output( '<paragraph>fo[]o</paragraph><paragraph>bar</paragraph>' );
 
@@ -219,7 +223,7 @@ describe( 'UndoEditing integration', () => {
 			input( '<paragraph>foo[]</paragraph>' );
 
 			model.change( writer => {
-				writer.remove( Range.createIn( root ) );
+				writer.remove( writer.createRangeIn( root ) );
 			} );
 			output( '<paragraph>[]</paragraph>' ); // All hail our king and savior, autoparagraphing!
 
@@ -234,7 +238,7 @@ describe( 'UndoEditing integration', () => {
 			output( '<paragraph>[]</paragraph>' ); // All hail our king and savior, autoparagraphing!
 
 			model.change( writer => {
-				writer.remove( Range.createIn( root ) );
+				writer.remove( writer.createRangeIn( root ) );
 				writer.insertElement( 'heading1', doc.selection.getFirstPosition() );
 			} );
 
@@ -251,7 +255,7 @@ describe( 'UndoEditing integration', () => {
 			input( '<paragraph></paragraph>' );
 
 			const p = root.getChild( 0 );
-			const pos = new Position( root, [ 0 ] );
+			const pos = model.createPositionFromPath( root, [ 0 ] );
 
 			model.change( writer => {
 				writer.remove( p );
@@ -274,12 +278,12 @@ describe( 'UndoEditing integration', () => {
 			input( '<paragraph>f[o]z</paragraph><paragraph>bar</paragraph>' );
 
 			model.change( writer => {
-				writer.move( doc.selection.getFirstRange(), new Position( root, [ 1, 0 ] ) );
+				writer.move( doc.selection.getFirstRange(), writer.createPositionFromPath( root, [ 1, 0 ] ) );
 			} );
 			output( '<paragraph>fz</paragraph><paragraph>[o]bar</paragraph>' );
 
 			model.change( writer => {
-				writer.move( doc.selection.getFirstRange(), new Position( root, [ 0, 2 ] ) );
+				writer.move( doc.selection.getFirstRange(), writer.createPositionFromPath( root, [ 0, 2 ] ) );
 			} );
 			output( '<paragraph>fz[o]</paragraph><paragraph>bar</paragraph>' );
 
@@ -296,13 +300,13 @@ describe( 'UndoEditing integration', () => {
 			input( '<paragraph>f[o]z</paragraph><paragraph>bar</paragraph>' );
 
 			model.change( writer => {
-				writer.move( doc.selection.getFirstRange(), new Position( root, [ 1, 0 ] ) );
+				writer.move( doc.selection.getFirstRange(), writer.createPositionFromPath( root, [ 1, 0 ] ) );
 			} );
 			output( '<paragraph>fz</paragraph><paragraph>[o]bar</paragraph>' );
 
 			model.change( writer => {
 				setSelection( [ 1 ], [ 2 ] );
-				writer.move( doc.selection.getFirstRange(), new Position( root, [ 0 ] ) );
+				writer.move( doc.selection.getFirstRange(), writer.createPositionFromPath( root, [ 0 ] ) );
 			} );
 			output( '<paragraph>[obar]</paragraph><paragraph>fz</paragraph>' );
 
@@ -373,7 +377,7 @@ describe( 'UndoEditing integration', () => {
 
 			model.change( writer => {
 				setSelection( [ 2, 0 ], [ 2, 1 ] );
-				writer.move( doc.selection.getFirstRange(), new Position( root, [ 0 ] ) );
+				writer.move( doc.selection.getFirstRange(), writer.createPositionFromPath( root, [ 0 ] ) );
 			} );
 			output( '[z]fo<paragraph>b</paragraph>ar' );
 
@@ -404,7 +408,7 @@ describe( 'UndoEditing integration', () => {
 			input( '<paragraph>foo</paragraph><paragraph>[]bar</paragraph>' );
 
 			model.change( writer => {
-				writer.merge( new Position( root, [ 1 ] ) );
+				writer.merge( writer.createPositionFromPath( root, [ 1 ] ) );
 			} );
 			output( '<paragraph>foo[]bar</paragraph>' );
 
@@ -780,12 +784,12 @@ describe( 'UndoEditing integration', () => {
 			output( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
 
 			model.change( writer => {
-				writer.split( Position.createAt( root.getChild( 0 ), 1 ) );
+				writer.split( writer.createPositionAt( root.getChild( 0 ), 1 ) );
 			} );
 			output( '<paragraph>[]F</paragraph><paragraph>oo</paragraph><paragraph>Bar</paragraph>' );
 
 			model.change( writer => {
-				writer.merge( Position.createAt( root, 2 ) );
+				writer.merge( writer.createPositionAt( root, 2 ) );
 			} );
 			output( '<paragraph>[]F</paragraph><paragraph>ooBar</paragraph>' );
 
@@ -809,7 +813,7 @@ describe( 'UndoEditing integration', () => {
 			output( '<heading2>[]Foo</heading2><paragraph>Bar</paragraph>' );
 
 			model.change( writer => {
-				writer.merge( Position.createAt( root, 1 ) );
+				writer.merge( writer.createPositionAt( root, 1 ) );
 			} );
 			output( '<heading2>[]FooBar</heading2>' );
 
@@ -825,7 +829,7 @@ describe( 'UndoEditing integration', () => {
 			input( '<heading1>[]Foo</heading1><paragraph>Bar</paragraph>' );
 
 			model.change( writer => {
-				writer.merge( Position.createAt( root, 1 ) );
+				writer.merge( writer.createPositionAt( root, 1 ) );
 			} );
 			output( '<heading1>[]FooBar</heading1>' );
 
@@ -855,12 +859,12 @@ describe( 'UndoEditing integration', () => {
 			input( '<paragraph>[]Foo</paragraph><paragraph>Bar</paragraph>' );
 
 			model.change( writer => {
-				writer.wrap( Range.createIn( root ), 'div' );
+				writer.wrap( writer.createRangeIn( root ), 'div' );
 			} );
 			output( '<div><paragraph>[]Foo</paragraph><paragraph>Bar</paragraph></div>' );
 
 			model.change( writer => {
-				writer.split( new Position( root, [ 0, 0, 1 ] ) );
+				writer.split( writer.createPositionFromPath( root, [ 0, 0, 1 ] ) );
 			} );
 			output( '<div><paragraph>[]F</paragraph><paragraph>oo</paragraph><paragraph>Bar</paragraph></div>' );
 
@@ -909,7 +913,10 @@ describe( 'UndoEditing integration', () => {
 			editor.execute( 'enter' );
 
 			model.change( writer => {
-				const range = new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 1, 3 ] ) );
+				const range = writer.createRange(
+					writer.createPositionFromPath( root, [ 0, 3 ] ),
+					writer.createPositionFromPath( root, [ 1, 3 ] )
+				);
 
 				writer.setSelection( range );
 
@@ -1025,12 +1032,12 @@ describe( 'UndoEditing integration', () => {
 		// 1. Step - add text and marker to the paragraph.
 		model.change( writer => {
 			writer.appendText( 'foo', paragraph );
-			writer.addMarker( 'marker', { range: Range.createIn( paragraph ), usingOperation: true } );
+			writer.addMarker( 'marker', { range: writer.createRangeIn( paragraph ), usingOperation: true } );
 		} );
 
 		// 2. Step - remove text from paragraph.
 		model.change( writer => {
-			writer.remove( Range.createIn( paragraph ) );
+			writer.remove( writer.createRangeIn( paragraph ) );
 		} );
 
 		// Register post fixer to remove marker from non-empty paragraph.

+ 3 - 3
packages/ckeditor5-undo/tests/undoediting.js

@@ -4,7 +4,7 @@
  */
 
 import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
-import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
+
 import UndoEditing from '../src/undoediting';
 import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
 
@@ -60,7 +60,7 @@ describe( 'UndoEditing', () => {
 		sinon.spy( undo._undoCommand, 'addBatch' );
 		sinon.spy( undo._redoCommand, 'clearStack' );
 
-		const batch = new Batch();
+		const batch = model.createBatch();
 
 		undo._redoCommand._createdBatches.add( batch );
 
@@ -76,7 +76,7 @@ describe( 'UndoEditing', () => {
 		sinon.spy( undo._redoCommand, 'addBatch' );
 		sinon.spy( undo._redoCommand, 'clearStack' );
 
-		undo._undoCommand.fire( 'revert', null, new Batch() );
+		undo._undoCommand.fire( 'revert', null, model.createBatch() );
 
 		expect( undo._redoCommand.addBatch.calledOnce ).to.be.true;
 		expect( undo._redoCommand.clearStack.called ).to.be.false;