Skip to content

Commit

Permalink
fix(taBinde.undomanager): Fix a bug with redo/undo beyond the borders
Browse files Browse the repository at this point in the history
Fixes #558
  • Loading branch information
SimeonC authored and SimeonC committed May 23, 2015
1 parent 8482449 commit dd023c3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
2 changes: 1 addition & 1 deletion dist/textAngular.min.js

Large diffs are not rendered by default.

18 changes: 12 additions & 6 deletions lib/taBind.js
Expand Up @@ -167,16 +167,19 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
}
};

var _redoUndoTimeout;
var _undo = scope['$undoTaBind' + (attrs.id || '')] = function(){
/* istanbul ignore else: can't really test it due to all changes being ignored as well in readonly */
if(!_isReadonly && _isContentEditable){
var content = ngModel.$undoManager.undo();
if(typeof content !== "undefined" && content !== null){
_setInnerHTML(content);
_setViewValue(content, false);
/* istanbul ignore else: browser catch */
if(element[0].childNodes.length) taSelection.setSelectionToElementEnd(element[0].childNodes[element[0].childNodes.length-1]);
else taSelection.setSelectionToElementEnd(element[0]);
if(_redoUndoTimeout) $timeout.cancel(_redoUndoTimeout);
_redoUndoTimeout = $timeout(function(){
element[0].focus();
taSelection.setSelectionToElementEnd(element[0]);
}, 1);
}
}
};
Expand All @@ -188,9 +191,12 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
if(typeof content !== "undefined" && content !== null){
_setInnerHTML(content);
_setViewValue(content, false);
/* istanbul ignore else: browser catch */
if(element[0].childNodes.length) taSelection.setSelectionToElementEnd(element[0].childNodes[element[0].childNodes.length-1]);
else taSelection.setSelectionToElementEnd(element[0]);
/* istanbul ignore next */
if(_redoUndoTimeout) $timeout.cancel(_redoUndoTimeout);
_redoUndoTimeout = $timeout(function(){
element[0].focus();
taSelection.setSelectionToElementEnd(element[0]);
}, 1);
}
}
};
Expand Down
18 changes: 12 additions & 6 deletions src/textAngular.js
Expand Up @@ -1142,16 +1142,19 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
}
};

var _redoUndoTimeout;
var _undo = scope['$undoTaBind' + (attrs.id || '')] = function(){
/* istanbul ignore else: can't really test it due to all changes being ignored as well in readonly */
if(!_isReadonly && _isContentEditable){
var content = ngModel.$undoManager.undo();
if(typeof content !== "undefined" && content !== null){
_setInnerHTML(content);
_setViewValue(content, false);
/* istanbul ignore else: browser catch */
if(element[0].childNodes.length) taSelection.setSelectionToElementEnd(element[0].childNodes[element[0].childNodes.length-1]);
else taSelection.setSelectionToElementEnd(element[0]);
if(_redoUndoTimeout) $timeout.cancel(_redoUndoTimeout);
_redoUndoTimeout = $timeout(function(){
element[0].focus();
taSelection.setSelectionToElementEnd(element[0]);
}, 1);
}
}
};
Expand All @@ -1163,9 +1166,12 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
if(typeof content !== "undefined" && content !== null){
_setInnerHTML(content);
_setViewValue(content, false);
/* istanbul ignore else: browser catch */
if(element[0].childNodes.length) taSelection.setSelectionToElementEnd(element[0].childNodes[element[0].childNodes.length-1]);
else taSelection.setSelectionToElementEnd(element[0]);
/* istanbul ignore next */
if(_redoUndoTimeout) $timeout.cancel(_redoUndoTimeout);
_redoUndoTimeout = $timeout(function(){
element[0].focus();
taSelection.setSelectionToElementEnd(element[0]);
}, 1);
}
}
};
Expand Down
7 changes: 5 additions & 2 deletions test/taBind/taBind.undoManager.spec.js
Expand Up @@ -107,11 +107,13 @@ describe('taBind.undoManager', function () {
$rootScope.$digest();
});
describe('should update', function(){
beforeEach(function(){
beforeEach(inject(function($timeout){
$rootScope.$undoTaBindTest();
$rootScope.$undoTaBindTest();
$timeout.flush();
$rootScope.$redoTaBindTest();
});
$timeout.flush();
}));
it('model', function(){
expect($rootScope.$undoManagerTest.current()).toBe(second);
});
Expand All @@ -124,6 +126,7 @@ describe('taBind.undoManager', function () {
it('not error', function(){
expect(function(){
$rootScope.$redoTaBindTest();
$rootScope.$redoTaBindTest();
}).not.toThrow();
});
it('not change html', function(){
Expand Down

0 comments on commit dd023c3

Please sign in to comment.