Commit ec5ae11b authored by techird's avatar techird

整理按键支持

parent 4ba6d7a3
......@@ -116,6 +116,8 @@ define(function(require, exports, module) {
* @param {argument} args 要传递给命令的其它参数
*/
execCommand: function(name) {
if (!name) return null;
name = name.toLowerCase();
var cmdArgs = [].slice.call(arguments, 1),
......
......@@ -3,14 +3,6 @@ define(function(require, exports, module) {
var utils = require('./utils');
var Minder = require('./minder');
function listen(element, type, handler) {
var types = utils.isArray(type) ? type : utils.trim(type).split(' '),
k = types.length;
types.forEach(function(name) {
element.addEventListener(name, handler, false);
});
}
/**
* @class MinderEvent
* @description 表示一个脑图中发生的事件
......@@ -134,7 +126,7 @@ define(function(require, exports, module) {
}
});
Minder.registerInitHook(function() {
Minder.registerInitHook(function(option) {
this._initEvents();
});
......@@ -144,17 +136,12 @@ define(function(require, exports, module) {
this._eventCallbacks = {};
},
_bindEvents: function() {
this._bindPaperEvents();
this._bindKeyboardEvents();
},
_resetEvents: function() {
this._initEvents();
this._bindEvents();
},
_bindPaperEvents: function() {
_bindEvents: function() {
/* jscs:disable maximumLineLength */
this._paper.on('click dblclick mousedown contextmenu mouseup mousemove mouseover mousewheel DOMMouseScroll touchstart touchmove touchend dragenter dragleave drop', this._firePharse.bind(this));
if (window) {
......@@ -162,61 +149,15 @@ define(function(require, exports, module) {
}
},
_bindKeyboardEvents: function() {
if (this._keyboardReceiver) return;
var receiver = this._keyboardReceiver = document.createElement('input');
receiver.classList.add('km-receiver');
var renderTarget = this._renderTarget;
renderTarget.appendChild(receiver);
var minder = this;
listen(receiver, 'keydown keyup keypress copy paste blur focus input', function(e) {
switch (e.type) {
case 'blur':
minder.blur();
break;
case 'focus':
minder.focus();
break;
case 'input':
receiver.value = null;
break;
}
minder._firePharse(e);
e.preventDefault();
});
this.on('beforemousedown', function(e) {
this.focus();
e.preventDefault();
});
this.focus = function() {
if (!this.isFocused()) {
renderTarget.classList.add('focus');
receiver.select();
receiver.focus();
this.renderNodeBatch(this.getSelectedNodes());
}
return this;
};
this.blur = function() {
if (this.isFocused()) {
renderTarget.classList.remove('focus');
receiver.blur();
this.renderNodeBatch(this.getSelectedNodes());
}
return this;
};
this.focus();
},
isFocused: function() {
var renderTarget = this._renderTarget;
return renderTarget && renderTarget.classList.contains('focus');
/**
* @method dispatchKeyEvent
* @description 派发键盘(相关)事件到脑图实例上,让实例的模块处理
* @grammar dispatchKeyEvent(e) => {this}
* @param {Event} e 原生的 Dom 事件对象
*/
dispatchKeyEvent: function(e) {
this._firePharse(e);
e.preventDefault();
},
_firePharse: function(e) {
......
define(function(require, exports, module) {
var kity = require('./kity');
var Minder = require('./minder');
Minder.registerInitHook(function() {
this.on('beforemousedown', function(e) {
this.focus();
e.preventDefault();
});
this.on('paperrender', function() {
this.focus();
});
});
kity.extendClass(Minder, {
focus: function() {
if (!this.isFocused()) {
var renderTarget = this._renderTarget;
renderTarget.classList.add('focus');
this.renderNodeBatch(this.getSelectedNodes());
}
this.fire('focus');
return this;
},
blur: function() {
if (this.isFocused()) {
var renderTarget = this._renderTarget;
renderTarget.classList.remove('focus');
this.renderNodeBatch(this.getSelectedNodes());
}
this.fire('blur');
return this;
},
isFocused: function() {
var renderTarget = this._renderTarget;
return renderTarget && renderTarget.classList.contains('focus');
}
});
});
\ No newline at end of file
define(function(require, exports, module) {
var kity = require('./kity');
var utils = require('./utils');
var Minder = require('./minder');
function listen(element, type, handler) {
type.split(' ').forEach(function(name) {
element.addEventListener(name, handler, false);
});
}
Minder.registerInitHook(function(option) {
this.addDefaultOption({
enableKeyReceiver: true
});
if (this.getOption('enableKeyReceiver')) {
this.on('paperrender', function() {
this._initKeyReceiver();
});
}
});
kity.extendClass(Minder, {
_initKeyReceiver: function() {
if (this._keyReceiver) return;
var receiver = this._keyReceiver = document.createElement('input');
receiver.classList.add('km-receiver');
var renderTarget = this._renderTarget;
renderTarget.appendChild(receiver);
var minder = this;
listen(receiver, 'keydown keyup keypress copy paste blur focus input', function(e) {
switch (e.type) {
case 'blur':
minder.blur();
break;
case 'focus':
minder.focus();
break;
case 'input':
receiver.value = null;
break;
}
minder._firePharse(e);
e.preventDefault();
});
this.on('focus', function() {
receiver.select();
receiver.focus();
});
this.on('blur', function() {
receiver.blur();
});
if (this.isFocused()) {
receiver.select();
receiver.focus();
}
}
});
});
\ No newline at end of file
......@@ -22,7 +22,7 @@ define(function(require, exports, module) {
},
getOption: function(key) {
if (key) {
return this._options[key] || this._defaultOptions[key];
return key in this._options ? this._options[key] : this._defaultOptions[key];
} else {
return utils.extend({}, this._defaultOptions, this._options);
}
......
......@@ -75,7 +75,7 @@ define(function(require, exports, module) {
nodes = utils.isArray(nodes) ? nodes : [nodes];
nodes.forEach(function(node) {
if (me._selectedNodes.indexOf(node) !== -1) return;
me._selectedNodes.push(node);
me._selectedNodes.unshift(node);
});
this.renderChangedSelection(lastSelect);
return this;
......
......@@ -24,10 +24,12 @@ define(function(require, exports, module) {
kityminder.data = require('./core/data');
require('./core/compatibility');
kityminder.KeyMap = require('./core/keymap');
require('./core/key');
require('./core/shortcut');
require('./core/status');
require('./core/paper');
require('./core/select');
require('./core/focus');
require('./core/keyreceiver');
kityminder.Module = require('./core/module');
require('./core/readonly');
kityminder.Render = require('./core/render');
......
......@@ -52,6 +52,7 @@ define(function(require, exports, module) {
return km.execCommand('AppendChildNode', text);
}
var node = km.createNode(text, parent, sibling.getIndex() + 1);
node.setGlobalLayoutTransform(sibling.getGlobalLayoutTransform());
km.select(node, true);
node.render();
km.layout(600);
......@@ -74,12 +75,17 @@ define(function(require, exports, module) {
execute: function(km) {
var nodes = km.getSelectedNodes();
var ancestor = MinderNode.getCommonAncestor.apply(null, nodes);
var index = nodes[0].getIndex();
nodes.forEach(function(node) {
if (!node.isRoot()) km.removeNode(node);
});
km.select(ancestor || km.getRoot(), true);
if (nodes.length == 1) {
var selectBack = ancestor.children[index - 1] || ancestor.children[index];
km.select(selectBack || ancestor || km.getRoot(), true);
} else {
km.select(ancestor || km.getRoot(), true);
}
km.layout(600);
},
queryState: function(km) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment