Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge branch t/ckeditor5-engine/1209
Browse files Browse the repository at this point in the history
Internal: Update API usage after merge t/1209 on engine.
  • Loading branch information
Piotr Jasiun committed Jan 29, 2018
2 parents e1cb122 + 7a20c9f commit 72e0836
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
7 changes: 5 additions & 2 deletions src/basecommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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 );
} );
}
}

Expand Down
12 changes: 9 additions & 3 deletions tests/redocommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand Down
32 changes: 24 additions & 8 deletions tests/undocommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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 ) );
} );
Expand All @@ -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', () => {
Expand Down Expand Up @@ -303,15 +315,19 @@ 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 => {
writer.setAttribute( 'uppercase', true, r( 1, 4 ) );
} );
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 => {
Expand Down
8 changes: 5 additions & 3 deletions tests/undoengine-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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' );
} );
Expand Down

0 comments on commit 72e0836

Please sign in to comment.