Browse Source

Aligned code to the changes in DocumentSelection API.

Maciej Bukowski 7 years ago
parent
commit
99a06a6e94

+ 5 - 2
packages/ckeditor5-undo/src/basecommand.js

@@ -85,7 +85,8 @@ export default class BaseCommand extends Command {
 	 * @param {Array.<module:engine/model/delta/delta~Delta>} deltas Deltas which has been applied since selection has been stored.
 	 */
 	_restoreSelection( ranges, isBackward, deltas ) {
-		const document = this.editor.model.document;
+		const model = this.editor.model;
+		const document = model.document;
 
 		// This will keep the transformed selection ranges.
 		const selectionRanges = [];
@@ -110,7 +111,9 @@ export default class BaseCommand extends Command {
 
 		// `selectionRanges` may be empty if all ranges ended up in graveyard. If that is the case, do not restore selection.
 		if ( selectionRanges.length ) {
-			document.selection.setRanges( selectionRanges, isBackward );
+			model.change( writer => {
+				writer.setSelection( selectionRanges, isBackward );
+			} );
 		}
 	}
 

+ 9 - 3
packages/ckeditor5-undo/tests/redocommand.js

@@ -50,7 +50,9 @@ describe( 'RedoCommand', () => {
 				 [root]
 				 - {}
 				 */
-				editor.model.document.selection.setRanges( [ r( 0, 0 ) ] );
+				model.change( writer => {
+					writer.setSelection( r( 0, 0 ) );
+				} );
 				batch0 = new Batch();
 				undo.addBatch( batch0 );
 				model.enqueueChange( batch0, writer => {
@@ -67,7 +69,9 @@ describe( 'RedoCommand', () => {
 				 - r{}
 				 */
 				// Let's make things spicy and this time, make a backward selection.
-				editor.model.document.selection.setRanges( [ r( 2, 4 ) ], true );
+				model.change( writer => {
+					writer.setSelection( r( 2, 4 ), true );
+				} );
 				batch1 = new Batch();
 				undo.addBatch( batch1 );
 				model.enqueueChange( batch1, writer => {
@@ -83,7 +87,9 @@ describe( 'RedoCommand', () => {
 				 - a
 				 - r
 				 */
-				editor.model.document.selection.setRanges( [ r( 1, 3 ) ] );
+				model.change( writer => {
+					writer.setSelection( r( 1, 3 ) );
+				} );
 				batch2 = new Batch();
 				undo.addBatch( batch2 );
 				model.enqueueChange( batch2, writer => {

+ 24 - 8
packages/ckeditor5-undo/tests/undocommand.js

@@ -40,7 +40,9 @@ describe( 'UndoCommand', () => {
 				 [root]
 				 - {}
 				 */
-				editor.model.document.selection.setRanges( [ r( 0, 0 ) ] );
+				model.change( writer => {
+					writer.setSelection( r( 0, 0 ) );
+				} );
 				batch0 = new Batch();
 				undo.addBatch( batch0 );
 				model.enqueueChange( batch0, writer => {
@@ -57,7 +59,9 @@ describe( 'UndoCommand', () => {
 				 - r{}
 				 */
 				// Let's make things spicy and this time, make a backward selection.
-				editor.model.document.selection.setRanges( [ r( 2, 4 ) ], true );
+				model.change( writer => {
+					writer.setSelection( r( 2, 4 ), true );
+				} );
 				batch1 = new Batch();
 				undo.addBatch( batch1 );
 				model.enqueueChange( batch1, writer => {
@@ -73,7 +77,9 @@ describe( 'UndoCommand', () => {
 				 - a
 				 - r
 				 */
-				editor.model.document.selection.setRanges( [ r( 1, 3 ) ] );
+				model.change( writer => {
+					writer.setSelection( r( 1, 3 ) );
+				} );
 				batch2 = new Batch();
 				undo.addBatch( batch2 );
 				model.enqueueChange( batch2, writer => {
@@ -89,7 +95,9 @@ describe( 'UndoCommand', () => {
 				 - {o
 				 - o} (key: value)
 				 */
-				editor.model.document.selection.setRanges( [ r( 1, 4 ) ] );
+				model.change( writer => {
+					writer.setSelection( r( 1, 4 ) );
+				} );
 				batch3 = new Batch();
 				undo.addBatch( batch3 );
 				model.enqueueChange( batch3, writer => {
@@ -106,7 +114,9 @@ describe( 'UndoCommand', () => {
 				 - o
 				 - o (key: value)
 				 */
-				editor.model.document.selection.setRanges( [ r( 0, 1 ) ] );
+				model.change( writer => {
+					writer.setSelection( r( 0, 1 ) );
+				} );
 				model.enqueueChange( batch2, writer => {
 					writer.move( r( 0, 1 ), p( 3 ) );
 				} );
@@ -121,7 +131,9 @@ describe( 'UndoCommand', () => {
 				 - f
 				 - o{} (key: value)
 				 */
-				editor.model.document.selection.setRanges( [ r( 4, 4 ) ] );
+				model.change( writer => {
+					writer.setSelection( r( 4, 4 ) );
+				} );
 			} );
 
 			it( 'should revert changes done by deltas from the batch that was most recently added to the command stack', () => {
@@ -303,7 +315,9 @@ describe( 'UndoCommand', () => {
 			root.appendChildren( new Text( 'abcdef' ) );
 			expect( getCaseText( root ) ).to.equal( 'abcdef' );
 
-			editor.model.document.selection.setRanges( [ r( 1, 4 ) ] );
+			model.change( writer => {
+				writer.setSelection( r( 1, 4 ) );
+			} );
 			const batch0 = new Batch();
 			undo.addBatch( batch0 );
 			model.enqueueChange( batch0, writer => {
@@ -311,7 +325,9 @@ describe( 'UndoCommand', () => {
 			} );
 			expect( getCaseText( root ) ).to.equal( 'aBCDef' );
 
-			editor.model.document.selection.setRanges( [ r( 3, 4 ) ] );
+			model.change( writer => {
+				writer.setSelection( r( 3, 4 ) );
+			} );
 			const batch1 = new Batch();
 			undo.addBatch( batch1 );
 			model.enqueueChange( batch1, writer => {

+ 5 - 3
packages/ckeditor5-undo/tests/undoengine-integration.js

@@ -48,7 +48,9 @@ describe( 'UndoEngine integration', () => {
 	} );
 
 	function setSelection( pathA, pathB ) {
-		doc.selection.setRanges( [ new Range( new Position( root, pathA ), new Position( root, pathB ) ) ] );
+		model.change( writer => {
+			writer.setSelection( new Range( new Position( root, pathA ), new Position( root, pathB ) ) );
+		} );
 	}
 
 	function input( input ) {
@@ -897,10 +899,10 @@ describe( 'UndoEngine integration', () => {
 
 			editor.execute( 'enter' );
 
-			model.change( () => {
+			model.change( writer => {
 				const range = new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 1, 3 ] ) );
 
-				doc.selection.setRanges( [ range ] );
+				writer.setSelection( range );
 
 				editor.execute( 'delete' );
 			} );