Skip to content

Commit

Permalink
FIX: undo bug
Browse files Browse the repository at this point in the history
  • Loading branch information
farthinker committed Oct 28, 2015
1 parent b0c85f1 commit 80b921e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 31 deletions.
27 changes: 16 additions & 11 deletions lib/simditor.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
* Simditor v2.3.3
* http://simditor.tower.im/
* 2015-10-22
* 2015-10-28
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
Expand Down Expand Up @@ -903,9 +903,9 @@ InputManager = (function(superClass) {
if (_this.lastCaretPosition) {
_this.editor.undoManager.caretPosition(_this.lastCaretPosition);
} else {
$blockEl = _this.body.children.first();
$blockEl = _this.editor.body.children().first();
range = document.createRange();
_this.selection.setRangeAtStartOf($blockEl, range);
_this.editor.selection.setRangeAtStartOf($blockEl, range);
}
}
_this.lastCaretPosition = null;
Expand Down Expand Up @@ -1329,7 +1329,7 @@ UndoManager = (function(superClass) {
return function() {
return _this._pushUndoState();
};
})(this), 500);
})(this), 2000);
this.editor.on('valuechanged', (function(_this) {
return function(e, src) {
if (src === 'undo' || src === 'redo') {
Expand All @@ -1340,8 +1340,14 @@ UndoManager = (function(superClass) {
})(this));
this.editor.on('selectionchanged', (function(_this) {
return function(e) {
_this.resetCaretPosition();
return _this.update();
return _this.resetCaretPosition();
};
})(this));
this.editor.on('focus', (function(_this) {
return function(e) {
if (_this._stack.length === 0) {
return _this._pushUndoState();
}
};
})(this));
return this.editor.on('blur', (function(_this) {
Expand Down Expand Up @@ -1380,19 +1386,18 @@ UndoManager = (function(superClass) {
};

UndoManager.prototype._pushUndoState = function() {
var currentState, html;
var caret;
if (this.editor.triggerHandler('pushundostate') === false) {
return;
}
currentState = this.currentState();
html = this.editor.body.html();
if (currentState && currentState.html === html) {
caret = this.caretPosition();
if (!caret.start) {
return;
}
this._index += 1;
this._stack.length = this._index;
this._stack.push({
html: html,
html: this.editor.body.html(),
caret: this.caretPosition()
});
if (this._stack.length > this._capacity) {
Expand Down
25 changes: 15 additions & 10 deletions site/assets/scripts/simditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -898,9 +898,9 @@ InputManager = (function(superClass) {
if (_this.lastCaretPosition) {
_this.editor.undoManager.caretPosition(_this.lastCaretPosition);
} else {
$blockEl = _this.body.children.first();
$blockEl = _this.editor.body.children().first();
range = document.createRange();
_this.selection.setRangeAtStartOf($blockEl, range);
_this.editor.selection.setRangeAtStartOf($blockEl, range);
}
}
_this.lastCaretPosition = null;
Expand Down Expand Up @@ -1324,7 +1324,7 @@ UndoManager = (function(superClass) {
return function() {
return _this._pushUndoState();
};
})(this), 500);
})(this), 2000);
this.editor.on('valuechanged', (function(_this) {
return function(e, src) {
if (src === 'undo' || src === 'redo') {
Expand All @@ -1335,8 +1335,14 @@ UndoManager = (function(superClass) {
})(this));
this.editor.on('selectionchanged', (function(_this) {
return function(e) {
_this.resetCaretPosition();
return _this.update();
return _this.resetCaretPosition();
};
})(this));
this.editor.on('focus', (function(_this) {
return function(e) {
if (_this._stack.length === 0) {
return _this._pushUndoState();
}
};
})(this));
return this.editor.on('blur', (function(_this) {
Expand Down Expand Up @@ -1375,19 +1381,18 @@ UndoManager = (function(superClass) {
};

UndoManager.prototype._pushUndoState = function() {
var currentState, html;
var caret;
if (this.editor.triggerHandler('pushundostate') === false) {
return;
}
currentState = this.currentState();
html = this.editor.body.html();
if (currentState && currentState.html === html) {
caret = this.caretPosition();
if (!caret.start) {
return;
}
this._index += 1;
this._stack.length = this._index;
this._stack.push({
html: html,
html: this.editor.body.html(),
caret: this.caretPosition()
});
if (this._stack.length > this._capacity) {
Expand Down
4 changes: 2 additions & 2 deletions src/inputManager.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ class InputManager extends SimpleModule
if @lastCaretPosition
@editor.undoManager.caretPosition @lastCaretPosition
else
$blockEl = @body.children.first()
$blockEl = @editor.body.children().first()
range = document.createRange()
@selection.setRangeAtStartOf $blockEl, range
@editor.selection.setRangeAtStartOf $blockEl, range

@lastCaretPosition = null
@editor.triggerHandler 'focus'
Expand Down
19 changes: 12 additions & 7 deletions src/undoManager.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,18 @@ class UndoManager extends SimpleModule

@throttledPushState = @editor.util.throttle =>
@_pushUndoState()
, 500
, 2000

@editor.on 'valuechanged', (e, src) =>
return if src == 'undo' or src == 'redo'
@throttledPushState()

@editor.on 'selectionchanged', (e) =>
@resetCaretPosition()
@update()
# @update()

@editor.on 'focus', (e) =>
@_pushUndoState() if @_stack.length == 0

@editor.on 'blur', (e) =>
@resetCaretPosition()
Expand Down Expand Up @@ -74,15 +77,18 @@ class UndoManager extends SimpleModule
_pushUndoState: ->
return if @editor.triggerHandler('pushundostate') == false

currentState = @currentState()
html = @editor.body.html()
return if currentState and currentState.html == html
# currentState = @currentState()
# html = @editor.body.html()
# return if currentState and currentState.html == html

caret = @caretPosition()
return unless caret.start

@_index += 1
@_stack.length = @_index

@_stack.push
html: html
html: @editor.body.html()
caret: @caretPosition()

if @_stack.length > @_capacity
Expand Down Expand Up @@ -135,7 +141,6 @@ class UndoManager extends SimpleModule
currentState.html = html
currentState.caret = @caretPosition()


_getNodeOffset: (node, index) ->
if $.isNumeric index
$parent = $(node)
Expand Down
2 changes: 1 addition & 1 deletion styles/simditor.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
* Simditor v2.3.3
* http://simditor.tower.im/
* 2015-10-20
* 2015-10-28
*/
@font-face {
font-family: 'Simditor';
Expand Down

0 comments on commit 80b921e

Please sign in to comment.