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

Commit

Permalink
Renamed _batchStack to more correct _items and made minor simplificat…
Browse files Browse the repository at this point in the history
…ions. Tests: Fixed the way how feature is initialized and made sure to use beforeEach().
  • Loading branch information
Reinmar committed May 13, 2016
1 parent 89885e9 commit bdf3ba9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 38 deletions.
26 changes: 12 additions & 14 deletions src/undocommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ export default class UndoCommand extends Command {
super( editor );

/**
* Items that are pairs of batches which are saved by the command and model selection state at the moment of saving the batch.
* Items that are pairs of:
*
* * batches which are saved by the command and,
* * model selection state at the moment of saving the batch.
*
* @private
* @member {Array.<engine.treeModel.Batch>} undo.UndoCommand#_batchStack
* @member {Array} undo.UndoCommand#_items
*/
this._batchStack = [];
this._items = [];
}

/**
Expand All @@ -36,23 +39,23 @@ export default class UndoCommand extends Command {
isBackward: this.editor.document.selection.isBackward
};

this._batchStack.push( { batch, selection } );
this._items.push( { batch, selection } );
this.refreshState();
}

/**
* Removes all batches from the stack.
*/
clearStack() {
this._batchStack = [];
this._items = [];
this.refreshState();
}

/**
* @inheritDoc
*/
_checkEnabled() {
return this._batchStack.length > 0;
return this._items.length > 0;
}

/**
Expand All @@ -70,17 +73,12 @@ export default class UndoCommand extends Command {
// If batch is not given, set `batchIndex` to the last index in command stack.
// If it is given, find it on the stack.
if ( !batch ) {
batchIndex = this._batchStack.length - 1;
batchIndex = this._items.length - 1;
} else {
for ( let i = 0; i < this._batchStack.length; i++ ) {
if ( this._batchStack[ i ].batch == batch ) {
batchIndex = i;
break;
}
}
batchIndex = this._items.findIndex( item => item.batch == batch );
}

const undoItem = this._batchStack.splice( batchIndex, 1 )[ 0 ];
const undoItem = this._items.splice( batchIndex, 1 )[ 0 ];

// Get the batch to undo.
const undoBatch = undoItem.batch;
Expand Down
34 changes: 13 additions & 21 deletions tests/undo.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@ import Editor from '/ckeditor5/editor.js';
import ModelDocument from '/ckeditor5/engine/treemodel/document.js';
import Range from '/ckeditor5/engine/treemodel/range.js';
import Position from '/ckeditor5/engine/treemodel/position.js';
import UndoFeature from '/ckeditor5/undo/undo.js';

let element, editor, undo, doc, root;
import Undo from '/ckeditor5/undo/undo.js';
import Creator from '/ckeditor5/creator/creator.js';

import { getData, setData } from '/tests/engine/_utils/model.js';

//import deleteContents from '/ckeditor5/engine/treemodel/composer/deletecontents.js';
// import deleteContents from '/ckeditor5/engine/treemodel/composer/deletecontents.js';

let element, editor, doc, root;

before( () => {
beforeEach( () => {
element = document.createElement( 'div' );
document.body.appendChild( element );

editor = new Editor( element );

doc = new ModelDocument();
editor.document = doc;
root = doc.createRoot( 'root' );

undo = new UndoFeature( editor );
undo.init();
} );
editor = new Editor( element, {
creator: Creator,
features: [ Undo ]
} );

after( () => {
undo.destroy();
editor.document = doc;

return editor.init();
} );

function setSelection( pathA, pathB ) {
Expand All @@ -52,14 +52,6 @@ function undoDisabled() {
}

describe( 'undo integration', () => {
beforeEach( () => {
// Clearing root because `setData` has a bug.
root.removeChildren( 0, root.getChildCount() );

editor.commands.get( 'undo' ).clearStack();
editor.commands.get( 'redo' ).clearStack();
} );

describe( 'adding and removing content', () => {
it( 'add and undo', () => {
input( '<p>fo<selection />o</p><p>bar</p>' );
Expand Down
7 changes: 4 additions & 3 deletions tests/undocommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ afterEach( () => {
describe( 'UndoCommand', () => {
describe( 'constructor', () => {
it( 'should create undo command with empty batch stack', () => {
expect( undo._batchStack.length ).to.equal( 0 );
expect( undo._checkEnabled() ).to.be.false;
} );
} );

describe( 'clearStack', () => {
it( 'should remove all batches from the stack', () => {
undo.addBatch( doc.batch() );
undo.clearStack();
expect( undo._checkEnabled() ).to.be.true;

expect( undo._batchStack.length ).to.equal( 0 );
undo.clearStack();
expect( undo._checkEnabled() ).to.be.false;
} );
} );

Expand Down

0 comments on commit bdf3ba9

Please sign in to comment.