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

Commit

Permalink
Merge pull request #73 from ckeditor/t/72
Browse files Browse the repository at this point in the history
Fix: Selection should be correctly restored when undoing after all editor content was replaced or removed. Closes #72.
  • Loading branch information
Reinmar authored Sep 13, 2017
2 parents 5d20834 + 2c9f9a7 commit 58f953f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/basecommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } );
Expand Down
52 changes: 52 additions & 0 deletions tests/undoengine-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -209,6 +210,57 @@ describe( 'UndoEngine integration', () => {

undoDisabled();
} );

it( 'undo remove all content', () => {
input( '<paragraph>foo[]</paragraph>' );

doc.enqueueChanges( () => {
doc.batch().remove( Range.createIn( root ) );
} );
output( '<paragraph>[]</paragraph>' ); // All hail our king and savior, autoparagraphing!

editor.execute( 'undo' );
output( '<paragraph>foo[]</paragraph>' );

undoDisabled();
} );

it( 'undo insert first content', () => {
input( '' );

doc.enqueueChanges( () => {
doc.batch().insert( doc.selection.getFirstPosition(), new Element( 'heading1' ) );
} );
output( '<heading1>[]</heading1>' );

editor.execute( 'undo' );
output( '<paragraph>[]</paragraph>' ); // All hail our king and savior, autoparagraphing!

undoDisabled();
} );

// #72.
it( 'undo paste multiple elements simulation', () => {
input( '<paragraph></paragraph>' );

const p = root.getChild( 0 );
const pos = new Position( root, [ 0 ] );

doc.enqueueChanges( () => {
doc.batch()
.remove( p )
.insert( pos, new Element( 'heading1' ) )
.insert( pos.getShiftedBy( 1 ), new Element( 'heading2' ) );
} );

output( '<heading1>[]</heading1><heading2></heading2>' );

editor.execute( 'undo' );

output( '<paragraph>[]</paragraph>' );

undoDisabled();
} );
} );

describe( 'moving', () => {
Expand Down

0 comments on commit 58f953f

Please sign in to comment.