Parcourir la source

Changed: Slight refactor in UndoCommand and RedoCommand.

Szymon Cofalik il y a 9 ans
Parent
commit
bf4fcdad78

+ 8 - 5
packages/ckeditor5-undo/src/redocommand.js

@@ -30,13 +30,10 @@ export default class RedoCommand extends BaseCommand {
 	_doExecute() {
 		const item = this._items.pop();
 
-		// All changes done by the command execution will be saved as one batch.
-		const newBatch = this.editor.document.batch( 'redo' );
-
 		// All changes have to be done in one `enqueueChanges` callback so other listeners will not
 		// step between consecutive deltas, or won't do changes to the document before selection is properly restored.
 		this.editor.document.enqueueChanges( () => {
-			this._redo( item.batch, newBatch, this.editor.document );
+			this._redo( item.batch );
 			this._restoreSelection( item.selection.ranges, item.selection.isBackward );
 		} );
 
@@ -52,7 +49,13 @@ export default class RedoCommand extends BaseCommand {
 	 * @param {engine.model.Batch} redoingBatch Batch that will contain transformed and applied deltas from `storedBatch`.
 	 * @param {engine.model.Document} document Document that is operated on by the command.
 	 */
-	_redo( storedBatch, redoingBatch, document ) {
+	_redo( storedBatch ) {
+		const document = this.editor.document;
+
+		// All changes done by the command execution will be saved as one batch.
+		const redoingBatch = document.batch();
+		this._createdBatches.add( redoingBatch );
+
 		const deltasToRedo = storedBatch.deltas.slice();
 		deltasToRedo.reverse();
 

+ 12 - 7
packages/ckeditor5-undo/src/undocommand.js

@@ -35,14 +35,11 @@ export default class UndoCommand extends BaseCommand {
 
 		const item = this._items.splice( batchIndex, 1 )[ 0 ];
 
-		// All changes done by the command execution will be saved as one batch.
-		const newBatch = this.editor.document.batch( 'undo' );
-
 		// All changes has to be done in one `enqueueChanges` callback so other listeners will not
 		// step between consecutive deltas, or won't do changes to the document before selection is properly restored.
 		this.editor.document.enqueueChanges( () => {
-			this._undo( item.batch, newBatch, this.editor.document );
-			this._restoreSelection( item.selection.ranges, item.selection.isBackward, item.batch.baseVersion, this.editor.document );
+			this._undo( item.batch );
+			this._restoreSelection( item.selection.ranges, item.selection.isBackward, item.batch.baseVersion );
 		} );
 
 		this.fire( 'revert', item.batch );
@@ -76,7 +73,13 @@ export default class UndoCommand extends BaseCommand {
 	 * @param {engine.model.Batch} undoingBatch Batch that will contain transformed and applied deltas from `batchToUndo`.
 	 * @param {engine.model.Document} document Document that is operated on by the command.
 	 */
-	_undo( batchToUndo, undoingBatch, document ) {
+	_undo( batchToUndo ) {
+		const document = this.editor.document;
+
+		// All changes done by the command execution will be saved as one batch.
+		const undoingBatch = document.batch();
+		this._createdBatches.add( undoingBatch );
+
 		const history = document.history;
 		const deltasToUndo = batchToUndo.deltas.slice();
 		deltasToUndo.reverse();
@@ -183,7 +186,9 @@ export default class UndoCommand extends BaseCommand {
 	 * @param {Number} baseVersion
 	 * @param {engine.model.Document} document Document that is operated on by the command.
 	 */
-	_restoreSelection( ranges, isBackward, baseVersion, document ) {
+	_restoreSelection( ranges, isBackward, baseVersion ) {
+		const document = this.editor.document;
+
 		// This will keep the transformed selection ranges.
 		const selectionRanges = [];