From 4fbe076375dd01fcd391872109114913aae78f4b Mon Sep 17 00:00:00 2001 From: Szymon Cofalik Date: Mon, 11 Sep 2017 12:32:25 +0200 Subject: [PATCH 1/2] Fixed: Do not restore selection if selection had only a default range. --- src/basecommand.js | 6 +++-- tests/undoengine-integration.js | 42 +++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/basecommand.js b/src/basecommand.js index 6391ef9..cd1b0fb 100644 --- a/src/basecommand.js +++ b/src/basecommand.js @@ -56,9 +56,11 @@ export default class BaseCommand extends Command { * @param {module:engine/model/batch~Batch} batch The batch to add. */ addBatch( batch ) { + const docSelection = this.editor.document.selection; + const selection = { - ranges: Array.from( this.editor.document.selection.getRanges() ), - isBackward: this.editor.document.selection.isBackward + ranges: docSelection.hasOwnRange ? Array.from( docSelection.getRanges() ) : [], + isBackward: docSelection.isBackward }; this._stack.push( { batch, selection } ); diff --git a/tests/undoengine-integration.js b/tests/undoengine-integration.js index 05177a0..e55cc54 100644 --- a/tests/undoengine-integration.js +++ b/tests/undoengine-integration.js @@ -179,6 +179,48 @@ describe( 'UndoEngine integration', () => { undoDisabled(); } ); + + it( 'undo remove all content', () => { + input( '

foo[]

' ); + + doc.batch().remove( Range.createIn( root ) ); + output( '[]' ); + + editor.execute( 'undo' ); + output( '

foo[]

' ); + + undoDisabled(); + } ); + + it( 'undo insert first content', () => { + input( '' ); + + doc.batch().insert( doc.selection.getFirstPosition(), new Element( 'p' ) ); + output( '

[]

' ); + + editor.execute( 'undo' ); + output( '[]' ); + + undoDisabled(); + } ); + + // #72. + it( 'undo paste multiple elements simulation', () => { + input( '

' ); + + const p = root.getChild( 0 ); + const pos = new Position( root, [ 0 ] ); + + doc.batch().remove( p ).insert( pos, new Element( 'p' ) ).insert( pos.getShiftedBy( 1 ), new Element( 'p' ) ); + + output( '

[]

' ); + + editor.execute( 'undo' ); + + output( '

[]

' ); + + undoDisabled(); + } ); } ); describe( 'moving', () => { From 2c9f9a7484bb6b4db7b195c6da8517accd1ab762 Mon Sep 17 00:00:00 2001 From: Szymon Cofalik Date: Wed, 13 Sep 2017 10:00:47 +0200 Subject: [PATCH 2/2] Tests: Fixed tests after merging with master. --- tests/undoengine-integration.js | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/tests/undoengine-integration.js b/tests/undoengine-integration.js index 36f668c..5e9f69f 100644 --- a/tests/undoengine-integration.js +++ b/tests/undoengine-integration.js @@ -9,6 +9,7 @@ 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 Element from '@ckeditor/ckeditor5-engine/src/model/element'; import UndoEngine from '../src/undoengine'; import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; @@ -211,13 +212,15 @@ describe( 'UndoEngine integration', () => { } ); it( 'undo remove all content', () => { - input( '

foo[]

' ); + input( 'foo[]' ); - doc.batch().remove( Range.createIn( root ) ); - output( '[]' ); + doc.enqueueChanges( () => { + doc.batch().remove( Range.createIn( root ) ); + } ); + output( '[]' ); // All hail our king and savior, autoparagraphing! editor.execute( 'undo' ); - output( '

foo[]

' ); + output( 'foo[]' ); undoDisabled(); } ); @@ -225,29 +228,36 @@ describe( 'UndoEngine integration', () => { it( 'undo insert first content', () => { input( '' ); - doc.batch().insert( doc.selection.getFirstPosition(), new Element( 'p' ) ); - output( '

[]

' ); + doc.enqueueChanges( () => { + doc.batch().insert( doc.selection.getFirstPosition(), new Element( 'heading1' ) ); + } ); + output( '[]' ); editor.execute( 'undo' ); - output( '[]' ); + output( '[]' ); // All hail our king and savior, autoparagraphing! undoDisabled(); } ); // #72. it( 'undo paste multiple elements simulation', () => { - input( '

' ); + input( '' ); const p = root.getChild( 0 ); const pos = new Position( root, [ 0 ] ); - doc.batch().remove( p ).insert( pos, new Element( 'p' ) ).insert( pos.getShiftedBy( 1 ), new Element( 'p' ) ); + doc.enqueueChanges( () => { + doc.batch() + .remove( p ) + .insert( pos, new Element( 'heading1' ) ) + .insert( pos.getShiftedBy( 1 ), new Element( 'heading2' ) ); + } ); - output( '

[]

' ); + output( '[]' ); editor.execute( 'undo' ); - output( '

[]

' ); + output( '[]' ); undoDisabled(); } );