Przeglądaj źródła

Merge pull request #73 from ckeditor/t/72

Fix: Selection should be correctly restored when undoing after all editor content was replaced or removed. Closes #72.
Piotrek Koszuliński 8 lat temu
rodzic
commit
c79b69d35a

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

@@ -56,9 +56,11 @@ export default class BaseCommand extends Command {
 	 * @param {module:engine/model/batch~Batch} batch The batch to add.
 	 * @param {module:engine/model/batch~Batch} batch The batch to add.
 	 */
 	 */
 	addBatch( batch ) {
 	addBatch( batch ) {
+		const docSelection = this.editor.document.selection;
+
 		const 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 } );
 		this._stack.push( { batch, selection } );

+ 52 - 0
packages/ckeditor5-undo/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 Range from '@ckeditor/ckeditor5-engine/src/model/range';
 import Position from '@ckeditor/ckeditor5-engine/src/model/position';
 import Position from '@ckeditor/ckeditor5-engine/src/model/position';
+import Element from '@ckeditor/ckeditor5-engine/src/model/element';
 import UndoEngine from '../src/undoengine';
 import UndoEngine from '../src/undoengine';
 
 
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
@@ -209,6 +210,57 @@ describe( 'UndoEngine integration', () => {
 
 
 			undoDisabled();
 			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', () => {
 	describe( 'moving', () => {