Skip to content

Commit

Permalink
CHG: refactor inputManager and keystroke module
Browse files Browse the repository at this point in the history
  • Loading branch information
farthinker committed Oct 8, 2015
1 parent fa8f885 commit a887d9d
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 279 deletions.
4 changes: 2 additions & 2 deletions Gruntfile.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module.exports = (grunt) ->
dest: 'site/assets/scripts/'
ext: '.js'

muduleSpec:
moduleSpec:
expand: true
flatten: true
src: 'spec/src/*.coffee'
Expand Down Expand Up @@ -260,7 +260,7 @@ module.exports = (grunt) ->

grunt.registerTask 'default', ['site', 'express', 'jasmine:test:build', 'watch']
grunt.registerTask 'site', ['sass', 'coffee', 'umd', 'copy:vendor', 'copy:scripts', 'copy:styles', 'usebanner', 'jekyll']
grunt.registerTask 'test', ['sass', 'coffee', 'umd', 'jasmine']
grunt.registerTask 'test', ['coffee:moduleSpec', 'coffee:buttonSpec', 'jasmine']
grunt.registerTask 'package', ['clean:package', 'copy:package', 'uglify:simditor', 'compress']

grunt.registerTask 'fonticons', ['curl']
168 changes: 90 additions & 78 deletions lib/simditor.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/*!
* Simditor v2.2.4
* http://simditor.tower.im/
* 2015-10-08
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module unless amdModuleId is set
Expand Down Expand Up @@ -763,7 +758,7 @@ InputManager = (function(superClass) {
InputManager.prototype._arrowKeys = [37, 38, 39, 40];

InputManager.prototype._init = function() {
var submitKey;
var selectAllKey, submitKey;
this.editor = this._module;
this.throttledValueChanged = this.editor.util.throttle((function(_this) {
return function(params) {
Expand All @@ -777,10 +772,6 @@ InputManager = (function(superClass) {
return _this.editor.trigger('selectionchanged');
};
})(this), 50);
this._keystrokeHandlers = {};
this.hotkeys = simpleHotkeys({
el: this.editor.body
});
$(document).on('selectionchange.simditor' + this.editor.id, (function(_this) {
return function(e) {
var triggerEvent;
Expand Down Expand Up @@ -844,21 +835,22 @@ InputManager = (function(superClass) {
})(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('drop', $.proxy(this._onDrop, this)).on('input', $.proxy(this._onInput, this));
if (this.editor.util.browser.firefox) {
this.addShortcut('cmd+left', (function(_this) {
this.editor.hotkeys.add('cmd+left', (function(_this) {
return function(e) {
e.preventDefault();
_this.editor.selection._selection.modify('move', 'backward', 'lineboundary');
return false;
};
})(this));
this.addShortcut('cmd+right', (function(_this) {
this.editor.hotkeys.add('cmd+right', (function(_this) {
return function(e) {
e.preventDefault();
_this.editor.selection._selection.modify('move', 'forward', 'lineboundary');
return false;
};
})(this));
this.addShortcut((this.editor.util.os.mac ? 'cmd+a' : 'ctrl+a'), (function(_this) {
selectAllKey = this.editor.util.os.mac ? 'cmd+a' : 'ctrl+a';
this.editor.hotkeys.add(selectAllKey, (function(_this) {
return function(e) {
var $children, firstBlock, lastBlock, range;
$children = _this.editor.body.children();
Expand All @@ -876,7 +868,7 @@ InputManager = (function(superClass) {
})(this));
}
submitKey = this.editor.util.os.mac ? 'cmd+enter' : 'ctrl+enter';
this.addShortcut(submitKey, (function(_this) {
this.editor.hotkeys.add(submitKey, (function(_this) {
return function(e) {
_this.editor.el.closest('form').find('button:submit').click();
return false;
Expand Down Expand Up @@ -927,36 +919,16 @@ InputManager = (function(superClass) {
};

InputManager.prototype._onKeyDown = function(e) {
var base, ref, ref1, result;
var ref, ref1;
if (this.editor.triggerHandler(e) === false) {
return false;
}
if (this.hotkeys.respondTo(e)) {
if (this.editor.hotkeys.respondTo(e)) {
return;
}
if (e.which in this._keystrokeHandlers) {
result = typeof (base = this._keystrokeHandlers[e.which])['*'] === "function" ? base['*'](e) : void 0;
if (result) {
this.throttledValueChanged();
return false;
}
this.editor.selection.startNodes().each((function(_this) {
return function(i, node) {
var handler, ref;
if (node.nodeType !== Node.ELEMENT_NODE) {
return;
}
handler = (ref = _this._keystrokeHandlers[e.which]) != null ? ref[node.tagName.toLowerCase()] : void 0;
result = typeof handler === "function" ? handler(e, $(node)) : void 0;
if (result === true || result === false) {
return false;
}
};
})(this));
if (result) {
this.throttledValueChanged();
return false;
}
if (this.editor.keystroke.respondTo(e)) {
this.throttledValueChanged();
return false;
}
if ((ref = e.which, indexOf.call(this._modifierKeys, ref) >= 0) || (ref1 = e.which, indexOf.call(this._arrowKeys, ref1) >= 0)) {
return;
Expand Down Expand Up @@ -1003,17 +975,6 @@ InputManager = (function(superClass) {
return this.throttledValueChanged(['oninput']);
};

InputManager.prototype.addKeystrokeHandler = function(key, node, handler) {
if (!this._keystrokeHandlers[key]) {
this._keystrokeHandlers[key] = {};
}
return this._keystrokeHandlers[key][node] = handler;
};

InputManager.prototype.addShortcut = function(keys, handler) {
return this.hotkeys.add(keys, $.proxy(handler, this));
};

return InputManager;

})(SimpleModule);
Expand All @@ -1028,10 +989,53 @@ Keystroke = (function(superClass) {
Keystroke.pluginName = 'Keystroke';

Keystroke.prototype._init = function() {
var titleEnterHandler;
this.editor = this._module;
this._keystrokeHandlers = {};
return this._initKeystrokeHandlers();
};

Keystroke.prototype.add = function(key, node, handler) {
key = key.toLowerCase();
key = this.editor.hotkeys.constructor.aliases[key] || key;
if (!this._keystrokeHandlers[key]) {
this._keystrokeHandlers[key] = {};
}
return this._keystrokeHandlers[key][node] = handler;
};

Keystroke.prototype.respondTo = function(e) {
var base, key, ref, result;
key = (ref = this.editor.hotkeys.constructor.keyNameMap[e.which]) != null ? ref.toLowerCase() : void 0;
if (!key) {
return;
}
if (key in this._keystrokeHandlers) {
result = typeof (base = this._keystrokeHandlers[key])['*'] === "function" ? base['*'](e) : void 0;
if (!result) {
this.editor.selection.startNodes().each((function(_this) {
return function(i, node) {
var handler, ref1;
if (node.nodeType !== Node.ELEMENT_NODE) {
return;
}
handler = (ref1 = _this._keystrokeHandlers[key]) != null ? ref1[node.tagName.toLowerCase()] : void 0;
result = typeof handler === "function" ? handler(e, $(node)) : void 0;
if (result === true || result === false) {
return false;
}
};
})(this));
}
if (result) {
return true;
}
}
};

Keystroke.prototype._initKeystrokeHandlers = function() {
var titleEnterHandler;
if (this.editor.util.browser.safari) {
this.editor.inputManager.addKeystrokeHandler('13', '*', (function(_this) {
this.add('enter', '*', (function(_this) {
return function(e) {
var $blockEl, $br;
if (!e.shiftKey) {
Expand Down Expand Up @@ -1065,14 +1069,14 @@ Keystroke = (function(superClass) {
return true;
};
})(this);
this.editor.inputManager.addKeystrokeHandler('13', 'h1', titleEnterHandler);
this.editor.inputManager.addKeystrokeHandler('13', 'h2', titleEnterHandler);
this.editor.inputManager.addKeystrokeHandler('13', 'h3', titleEnterHandler);
this.editor.inputManager.addKeystrokeHandler('13', 'h4', titleEnterHandler);
this.editor.inputManager.addKeystrokeHandler('13', 'h5', titleEnterHandler);
this.editor.inputManager.addKeystrokeHandler('13', 'h6', titleEnterHandler);
}
this.editor.inputManager.addKeystrokeHandler('8', '*', (function(_this) {
this.add('enter', 'h1', titleEnterHandler);
this.add('enter', 'h2', titleEnterHandler);
this.add('enter', 'h3', titleEnterHandler);
this.add('enter', 'h4', titleEnterHandler);
this.add('enter', 'h5', titleEnterHandler);
this.add('enter', 'h6', titleEnterHandler);
}
this.add('backspace', '*', (function(_this) {
return function(e) {
var $blockEl, $prevBlockEl, $rootBlock, isWebkit;
$rootBlock = _this.editor.selection.rootNodes().first();
Expand All @@ -1093,7 +1097,7 @@ Keystroke = (function(superClass) {
}
};
})(this));
this.editor.inputManager.addKeystrokeHandler('13', 'li', (function(_this) {
this.add('enter', 'li', (function(_this) {
return function(e, $node) {
var $cloneNode, listEl, newBlockEl, newListEl;
$cloneNode = $node.clone();
Expand Down Expand Up @@ -1139,7 +1143,7 @@ Keystroke = (function(superClass) {
return true;
};
})(this));
this.editor.inputManager.addKeystrokeHandler('13', 'pre', (function(_this) {
this.add('enter', 'pre', (function(_this) {
return function(e, $node) {
var $p, breakNode, range;
e.preventDefault();
Expand All @@ -1165,7 +1169,7 @@ Keystroke = (function(superClass) {
return true;
};
})(this));
this.editor.inputManager.addKeystrokeHandler('13', 'blockquote', (function(_this) {
this.add('enter', 'blockquote', (function(_this) {
return function(e, $node) {
var $closestBlock, range;
$closestBlock = _this.editor.selection.blockNodes().last();
Expand All @@ -1178,7 +1182,7 @@ Keystroke = (function(superClass) {
return true;
};
})(this));
this.editor.inputManager.addKeystrokeHandler('8', 'li', (function(_this) {
this.add('backspace', 'li', (function(_this) {
return function(e, $node) {
var $br, $childList, $newLi, $prevChildList, $prevNode, $textNode, isFF, range, text;
$childList = $node.children('ul, ol');
Expand Down Expand Up @@ -1227,7 +1231,7 @@ Keystroke = (function(superClass) {
return true;
};
})(this));
this.editor.inputManager.addKeystrokeHandler('8', 'pre', (function(_this) {
this.add('backspace', 'pre', (function(_this) {
return function(e, $node) {
var $newNode, codeStr, range;
if (!_this.editor.selection.rangeAtStartOf($node)) {
Expand All @@ -1241,7 +1245,7 @@ Keystroke = (function(superClass) {
return true;
};
})(this));
return this.editor.inputManager.addKeystrokeHandler('8', 'blockquote', (function(_this) {
return this.add('backspace', 'blockquote', (function(_this) {
return function(e, $node) {
var $firstChild, range;
if (!_this.editor.selection.rangeAtStartOf($node)) {
Expand Down Expand Up @@ -1290,14 +1294,14 @@ UndoManager = (function(superClass) {
undoShortcut = 'ctrl+z';
redoShortcut = 'shift+ctrl+z';
}
this.editor.inputManager.addShortcut(undoShortcut, (function(_this) {
this.editor.hotkeys.add(undoShortcut, (function(_this) {
return function(e) {
e.preventDefault();
_this.undo();
return false;
};
})(this));
this.editor.inputManager.addShortcut(redoShortcut, (function(_this) {
this.editor.hotkeys.add(redoShortcut, (function(_this) {
return function(e) {
e.preventDefault();
_this.redo();
Expand Down Expand Up @@ -1982,7 +1986,7 @@ Indentation = (function(superClass) {

Indentation.prototype._init = function() {
this.editor = this._module;
return this.editor.inputManager.addKeystrokeHandler('9', '*', (function(_this) {
return this.editor.keystroke.add('tab', '*', (function(_this) {
return function(e) {
var codeButton;
codeButton = _this.editor.toolbar.findButton('code');
Expand Down Expand Up @@ -2422,6 +2426,14 @@ Simditor = (function(superClass) {
}
this.id = ++Simditor.count;
this._render();
if (simpleHotkeys) {
this.hotkeys = simpleHotkeys({
el: this.body
});
} else {
throw new Error('simditor: simple-hotkeys is required.');
return;
}
if (this.opts.upload && simpleUploader) {
uploadOpts = typeof this.opts.upload === 'object' ? this.opts.upload : {};
this.uploader = simpleUploader(uploadOpts);
Expand Down Expand Up @@ -2789,7 +2801,7 @@ Button = (function(superClass) {
};
})(this));
if (this.shortcut != null) {
this.editor.inputManager.addShortcut(this.shortcut, (function(_this) {
this.editor.hotkeys.add(this.shortcut, (function(_this) {
return function(e) {
_this.el.mousedown();
return false;
Expand Down Expand Up @@ -4765,25 +4777,25 @@ TableButton = (function(superClass) {
return _this.editor.body.find('.simditor-table td, .simditor-table th').removeClass('active');
};
})(this));
this.editor.inputManager.addKeystrokeHandler('38', 'td', (function(_this) {
this.editor.keystroke.add('up', 'td', (function(_this) {
return function(e, $node) {
_this._tdNav($node, 'up');
return true;
};
})(this));
this.editor.inputManager.addKeystrokeHandler('38', 'th', (function(_this) {
this.editor.keystroke.add('up', 'th', (function(_this) {
return function(e, $node) {
_this._tdNav($node, 'up');
return true;
};
})(this));
this.editor.inputManager.addKeystrokeHandler('40', 'td', (function(_this) {
this.editor.keystroke.add('down', 'td', (function(_this) {
return function(e, $node) {
_this._tdNav($node, 'down');
return true;
};
})(this));
return this.editor.inputManager.addKeystrokeHandler('40', 'th', (function(_this) {
return this.editor.keystroke.add('down', 'th', (function(_this) {
return function(e, $node) {
_this._tdNav($node, 'down');
return true;
Expand Down Expand Up @@ -4911,25 +4923,25 @@ TableButton = (function(superClass) {
};

TableButton.prototype._initShortcuts = function() {
this.editor.inputManager.addShortcut('ctrl+alt+up', (function(_this) {
this.editor.hotkeys.add('ctrl+alt+up', (function(_this) {
return function(e) {
_this.editMenu.find('.menu-item[data-param=insertRowAbove]').click();
return false;
};
})(this));
this.editor.inputManager.addShortcut('ctrl+alt+down', (function(_this) {
this.editor.hotkeys.add('ctrl+alt+down', (function(_this) {
return function(e) {
_this.editMenu.find('.menu-item[data-param=insertRowBelow]').click();
return false;
};
})(this));
this.editor.inputManager.addShortcut('ctrl+alt+left', (function(_this) {
this.editor.hotkeys.add('ctrl+alt+left', (function(_this) {
return function(e) {
_this.editMenu.find('.menu-item[data-param=insertColLeft]').click();
return false;
};
})(this));
return this.editor.inputManager.addShortcut('ctrl+alt+right', (function(_this) {
return this.editor.hotkeys.add('ctrl+alt+right', (function(_this) {
return function(e) {
_this.editMenu.find('.menu-item[data-param=insertColRight]').click();
return false;
Expand Down
Loading

0 comments on commit a887d9d

Please sign in to comment.