Skip to content

Commit

Permalink
Merge pull request mycolorway#188 from mycolorway/use-input-event
Browse files Browse the repository at this point in the history
Use input event for firefox valuechanged event
  • Loading branch information
farthinker committed Feb 11, 2015
2 parents 9e164b8 + 5641384 commit 2aa70de
Show file tree
Hide file tree
Showing 12 changed files with 299 additions and 180 deletions.
171 changes: 107 additions & 64 deletions lib/simditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ Formatter = (function(_super) {
var InputManager,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
__slice = [].slice,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };

InputManager = (function(_super) {
Expand All @@ -591,6 +592,16 @@ InputManager = (function(_super) {
InputManager.prototype._init = function() {
var submitKey;
this.editor = this._module;
this.throttledTrigger = this.editor.util.throttle((function(_this) {
return function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return setTimeout(function() {
var _ref;
return (_ref = _this.editor).trigger.apply(_ref, args);
}, 10);
};
})(this), 300);
if (this.opts.pasteImage && typeof this.opts.pasteImage !== 'string') {
this.opts.pasteImage = 'inline';
}
Expand Down Expand Up @@ -661,7 +672,7 @@ InputManager = (function(_super) {
}
});
_this.editor.body.find('pre:empty').append(_this.editor.util.phBr);
if (!_this.editor.util.supportSelectionChange && _this.focused) {
if (!_this.editor.util.support.onselectionchange && _this.focused) {
return _this.editor.trigger('selectionchanged');
}
};
Expand All @@ -671,7 +682,7 @@ InputManager = (function(_super) {
return _this.editor.undoManager.update();
};
})(this));
this.editor.body.on('keydown', $.proxy(this._onKeyDown, this)).on('keypress', $.proxy(this._onKeyPress, this)).on('keyup', $.proxy(this._onKeyUp, this)).on('mouseup', $.proxy(this._onMouseUp, this)).on('focus', $.proxy(this._onFocus, this)).on('blur', $.proxy(this._onBlur, this)).on('paste', $.proxy(this._onPaste, this)).on('drop', $.proxy(this._onDrop, this));
this.editor.body.on('keydown', $.proxy(this._onKeyDown, this)).on('keypress', $.proxy(this._onKeyPress, this)).on('keyup', $.proxy(this._onKeyUp, this)).on('mouseup', $.proxy(this._onMouseUp, this)).on('focus', $.proxy(this._onFocus, this)).on('blur', $.proxy(this._onBlur, this)).on('paste', $.proxy(this._onPaste, this)).on('drop', $.proxy(this._onDrop, this)).on('input', $.proxy(this._onInput, this));
if (this.editor.util.browser.firefox) {
this.addShortcut('cmd+left', (function(_this) {
return function(e) {
Expand Down Expand Up @@ -742,7 +753,7 @@ InputManager = (function(_super) {
};

InputManager.prototype._onMouseUp = function(e) {
if (!this.editor.util.supportSelectionChange) {
if (!this.editor.util.support.onselectionchange) {
return setTimeout((function(_this) {
return function() {
return _this.editor.trigger('selectionchanged');
Expand All @@ -752,7 +763,7 @@ InputManager = (function(_super) {
};

InputManager.prototype._onKeyDown = function(e) {
var $blockEl, metaKey, result, _base, _ref, _ref1;
var result, _base, _ref, _ref1;
if (this.editor.triggerHandler(e) === false) {
return false;
}
Expand All @@ -768,7 +779,7 @@ InputManager = (function(_super) {
this.editor.util.traverseUp((function(_this) {
return function(node) {
var handler, _ref;
if (node.nodeType !== 1) {
if (node.nodeType !== document.ELEMENT_NODE) {
return;
}
handler = (_ref = _this._keystrokeHandlers[e.which]) != null ? _ref[node.tagName.toLowerCase()] : void 0;
Expand All @@ -786,43 +797,11 @@ InputManager = (function(_super) {
if ((_ref = e.which, __indexOf.call(this._modifierKeys, _ref) >= 0) || (_ref1 = e.which, __indexOf.call(this._arrowKeys, _ref1) >= 0)) {
return;
}
metaKey = this.editor.util.metaKey(e);
$blockEl = this.editor.util.closestBlockEl();
if (metaKey && e.which === 86) {
if (this.editor.util.metaKey(e) && e.which === 86) {
return;
}
if (this.editor.util.browser.webkit && e.which === 8 && this.editor.selection.rangeAtStartOf($blockEl)) {
setTimeout((function(_this) {
return function() {
var $newBlockEl;
if (!_this.focused) {
return;
}
$newBlockEl = _this.editor.util.closestBlockEl();
_this.editor.selection.save();
_this.editor.formatter.cleanNode($newBlockEl, true);
_this.editor.selection.restore();
return _this.editor.trigger('valuechanged');
};
})(this), 10);
this.typing = true;
} else if (this._typing) {
if (this._typing !== true) {
clearTimeout(this._typing);
}
this._typing = setTimeout((function(_this) {
return function() {
_this.editor.trigger('valuechanged');
return _this._typing = false;
};
})(this), 200);
} else {
setTimeout((function(_this) {
return function() {
return _this.editor.trigger('valuechanged');
};
})(this), 10);
this._typing = true;
if (!this.editor.util.support.oninput) {
this.throttledTrigger('valuechanged', ['typing']);
}
return null;
};
Expand All @@ -838,7 +817,7 @@ InputManager = (function(_super) {
if (this.editor.triggerHandler(e) === false) {
return false;
}
if (!this.editor.util.supportSelectionChange && (_ref = e.which, __indexOf.call(this._arrowKeys, _ref) >= 0)) {
if (!this.editor.util.support.onselectionchange && (_ref = e.which, __indexOf.call(this._arrowKeys, _ref) >= 0)) {
this.editor.trigger('selectionchanged');
return;
}
Expand Down Expand Up @@ -1021,6 +1000,10 @@ InputManager = (function(_super) {
})(this), 0);
};

InputManager.prototype._onInput = function(e) {
return this.throttledTrigger('valuechanged', ['oninput']);
};

InputManager.prototype.addKeystrokeHandler = function(key, node, handler) {
if (!this._keystrokeHandlers[key]) {
this._keystrokeHandlers[key] = {};
Expand Down Expand Up @@ -1096,7 +1079,7 @@ Keystroke = (function(_super) {
}
this.editor.inputManager.addKeystrokeHandler('8', '*', (function(_this) {
return function(e) {
var $prevBlockEl, $rootBlock;
var $blockEl, $prevBlockEl, $rootBlock;
$rootBlock = _this.editor.util.furthestBlockEl();
$prevBlockEl = $rootBlock.prev();
if ($prevBlockEl.is('hr') && _this.editor.selection.rangeAtStartOf($rootBlock)) {
Expand All @@ -1105,6 +1088,13 @@ Keystroke = (function(_super) {
_this.editor.selection.restore();
return true;
}
$blockEl = _this.editor.util.closestBlockEl();
if (_this.editor.util.browser.webkit && _this.editor.selection.rangeAtStartOf($blockEl)) {
_this.editor.selection.save();
_this.editor.formatter.cleanNode($blockEl, true);
_this.editor.selection.restore();
return null;
}
};
})(this));
this.editor.inputManager.addKeystrokeHandler('9', '*', (function(_this) {
Expand Down Expand Up @@ -1336,7 +1326,7 @@ UndoManager = (function(_super) {
})(this));
return this.editor.on('valuechanged', (function(_this) {
return function(e, src) {
if (src === 'undo') {
if (src === 'undo' || src === 'redo') {
return;
}
if (_this._timer) {
Expand Down Expand Up @@ -1408,7 +1398,7 @@ UndoManager = (function(_super) {
this.caretPosition(state.caret);
this.editor.body.find('.selected').removeClass('selected');
this.editor.sync();
return this.editor.trigger('valuechanged', ['undo']);
return this.editor.trigger('valuechanged', ['redo']);
};

UndoManager.prototype.update = function() {
Expand Down Expand Up @@ -1623,20 +1613,27 @@ Util = (function(_super) {
}
})();

Util.prototype.supportSelectionChange = (function() {
var e, onselectionchange;
onselectionchange = document.onselectionchange;
if (onselectionchange !== void 0) {
try {
document.onselectionchange = 0;
return document.onselectionchange === null;
} catch (_error) {
e = _error;
} finally {
document.onselectionchange = onselectionchange;
}
}
return false;
Util.prototype.support = (function() {
return {
onselectionchange: (function() {
var e, onselectionchange;
onselectionchange = document.onselectionchange;
if (onselectionchange !== void 0) {
try {
document.onselectionchange = 0;
return document.onselectionchange === null;
} catch (_error) {
e = _error;
} finally {
document.onselectionchange = onselectionchange;
}
}
return false;
})(),
oninput: (function() {
return !/(msie|trident)/i.test(navigator.userAgent);
})()
};
})();

Util.prototype.reflow = function(el) {
Expand Down Expand Up @@ -1909,6 +1906,42 @@ Util = (function(_super) {
return bb.getBlob(mimeString);
};

Util.prototype.throttle = function(func, wait) {
var delayedCallTimeout, previousCallTime, stopDelayedCall;
delayedCallTimeout = null;
previousCallTime = 0;
stopDelayedCall = function() {
if (delayedCallTimeout) {
clearTimeout(delayedCallTimeout);
return delayedCallTimeout = null;
}
};
return function() {
var args, now, remaining, result;
now = Date.now();
previousCallTime || (previousCallTime = now);
remaining = wait - (now - previousCallTime);
result = null;
if ((0 < remaining && remaining < wait)) {
previousCallTime = now;
stopDelayedCall();
args = arguments;
delayedCallTimeout = setTimeout(function() {
previousCallTime = 0;
delayedCallTimeout = null;
return result = func.apply(null, args);
}, wait);
} else {
stopDelayedCall();
if (previousCallTime !== now) {
previousCallTime = 0;
}
result = func.apply(null, arguments);
}
return result;
};
};

return Util;

})(SimpleModule);
Expand Down Expand Up @@ -2842,7 +2875,9 @@ BoldButton = (function(_super) {

BoldButton.prototype.command = function() {
document.execCommand('bold');
this.editor.trigger('valuechanged');
if (!this.editor.util.support.oninput) {
this.editor.trigger('valuechanged');
}
return $(document).trigger('selectionchange');
};

Expand Down Expand Up @@ -2898,7 +2933,9 @@ ItalicButton = (function(_super) {

ItalicButton.prototype.command = function() {
document.execCommand('italic');
this.editor.trigger('valuechanged');
if (!this.editor.util.support.oninput) {
this.editor.trigger('valuechanged');
}
return $(document).trigger('selectionchange');
};

Expand Down Expand Up @@ -2954,7 +2991,9 @@ UnderlineButton = (function(_super) {

UnderlineButton.prototype.command = function() {
document.execCommand('underline');
this.editor.trigger('valuechanged');
if (!this.editor.util.support.oninput) {
this.editor.trigger('valuechanged');
}
return $(document).trigger('selectionchange');
};

Expand Down Expand Up @@ -3015,7 +3054,9 @@ ColorButton = (function(_super) {
return;
}
document.execCommand('foreColor', false, hex);
return _this.editor.trigger('valuechanged');
if (!_this.editor.util.support.oninput) {
return _this.editor.trigger('valuechanged');
}
};
})(this));
};
Expand Down Expand Up @@ -3762,7 +3803,7 @@ ImageButton = (function(_super) {
range = document.createRange();
range.selectNode($img[0]);
_this.editor.selection.selectRange(range);
if (!_this.editor.util.supportSelectionChange) {
if (!_this.editor.util.support.onselectionchange) {
_this.editor.trigger('selectionchanged');
}
return false;
Expand Down Expand Up @@ -4898,7 +4939,9 @@ StrikethroughButton = (function(_super) {

StrikethroughButton.prototype.command = function() {
document.execCommand('strikethrough');
this.editor.trigger('valuechanged');
if (!this.editor.util.support.oninput) {
this.editor.trigger('valuechanged');
}
return $(document).trigger('selectionchange');
};

Expand Down
Loading

0 comments on commit 2aa70de

Please sign in to comment.