From 7a20c9fcb758f2189e1fc5a2a53133facd92fc88 Mon Sep 17 00:00:00 2001 From: Maciej Bukowski Date: Thu, 25 Jan 2018 16:46:59 +0100 Subject: [PATCH] Aligned code to the changes in DocumentSelection API. --- src/basecommand.js | 7 +++++-- tests/redocommand.js | 12 +++++++++--- tests/undocommand.js | 32 ++++++++++++++++++++++++-------- tests/undoengine-integration.js | 8 +++++--- 4 files changed, 43 insertions(+), 16 deletions(-) diff --git a/src/basecommand.js b/src/basecommand.js index 1a2ff3c..acdaa0b 100644 --- a/src/basecommand.js +++ b/src/basecommand.js @@ -85,7 +85,8 @@ export default class BaseCommand extends Command { * @param {Array.} 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 ); + } ); } } diff --git a/tests/redocommand.js b/tests/redocommand.js index e13c2dd..f636690 100644 --- a/tests/redocommand.js +++ b/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 => { diff --git a/tests/undocommand.js b/tests/undocommand.js index 24ff7dd..1f8fdb0 100644 --- a/tests/undocommand.js +++ b/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 => { diff --git a/tests/undoengine-integration.js b/tests/undoengine-integration.js index 014d783..5c81b1e 100644 --- a/tests/undoengine-integration.js +++ b/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' ); } );