1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | 21× 21× 21× 21× 21× 21× 16× 16× 16× 16× 16× 144× 16× 16× 21× 21× 21× 21× 21× 21× 21× 1× 1× 21× 1× 21× 1× 1× 4× 4× 4× 4× 1× 1× 1× 28× 28× 4× 4× 28× 1× 50× 1× 21× 21× 21× 16× 16× 16× 16× 16× 4× 4× 16× 3× 3× 16× 2× 2× 16× 16× 7× 7× 1× 1× 3× 3× 1× 1× 2× 2× 7× 7× 21× 21× 21× 21× 21× 21× 21× 21× 21× 1× 1× 1× 1× 73× 73× 16× | import { event as domEvent, matches as domMatches } from 'min-dom'; /** * A keyboard abstraction that may be activated and * deactivated by users at will, consuming key events * and triggering diagram actions. * * The implementation fires the following key events that allow * other components to hook into key handling: * * - keyboard.bind * - keyboard.unbind * - keyboard.init * - keyboard.destroy * * All events contain the fields (node, listeners). * * A default binding for the keyboard may be specified via the * `keyboard.bindTo` configuration option. * * @param {Config} config * @param {EventBus} eventBus * @param {EditorActions} editorActions */ export default function Keyboard(config, eventBus, editorActions) { var self = this; this._config = config || {}; this._eventBus = eventBus; this._editorActions = editorActions; this._listeners = []; // our key handler is a singleton that passes // (keycode, modifiers) to each listener. // // listeners must indicate that they handled a key event // by returning true. This stops the event propagation. // this._keyHandler = function(event) { var i, l, target = event.target, listeners = self._listeners, code = event.keyCode || event.charCode || -1; Iif (target && (domMatches(target, 'input, textarea') || target.contentEditable === 'true')) { return; } for (i = 0; (l = listeners[i]); i++) { if (l(code, event)) { event.preventDefault(); event.stopPropagation(); } } }; // properly clean dom registrations eventBus.on('diagram.destroy', function() { self._fire('destroy'); self.unbind(); self._listeners = null; }); eventBus.on('diagram.init', function() { self._fire('init'); }); eventBus.on('attach', function() { Eif (config && config.bindTo) { self.bind(config.bindTo); } }); eventBus.on('detach', function() { self.unbind(); }); this._init(); } Keyboard.$inject = [ 'config.keyboard', 'eventBus', 'editorActions' ]; Keyboard.prototype.bind = function(node) { // make sure that the keyboard is only bound once to the DOM this.unbind(); this._node = node; // bind key events domEvent.bind(node, 'keydown', this._keyHandler, true); this._fire('bind'); }; Keyboard.prototype.getBinding = function() { return this._node; }; Keyboard.prototype.unbind = function() { var node = this._node; if (node) { this._fire('unbind'); // unbind key events domEvent.unbind(node, 'keydown', this._keyHandler, true); } this._node = null; }; Keyboard.prototype._fire = function(event) { this._eventBus.fire('keyboard.' + event, { node: this._node, listeners: this._listeners }); }; Keyboard.prototype._init = function() { var listeners = this._listeners; var editorActions = this._editorActions, config = this._config; // init default listeners // undo // (CTRL|CMD) + Z function undo(key, modifiers) { Iif (isCmd(modifiers) && !isShift(modifiers) && key === 90) { editorActions.trigger('undo'); return true; } } // redo // CTRL + Y // CMD + SHIFT + Z function redo(key, modifiers) { Iif (isCmd(modifiers) && (key === 89 || (key === 90 && isShift(modifiers)))) { editorActions.trigger('redo'); return true; } } // copy // CTRL/CMD + C function copy(key, modifiers) { Iif (isCmd(modifiers) && (key === 67)) { editorActions.trigger('copy'); return true; } } // paste // CTRL/CMD + V function paste(key, modifiers) { Iif (isCmd(modifiers) && (key === 86)) { editorActions.trigger('paste'); return true; } } /** * zoom in one step * CTRL + + * * 107 = numpad plus * 187 = regular plus * 171 = regular plus in Firefox (german keyboard layout) * 61 = regular plus in Firefox (US keyboard layout) */ function zoomIn(key, modifiers) { if ((key === 107 || key === 187 || key === 171 || key === 61) && isCmd(modifiers)) { editorActions.trigger('stepZoom', { value: 1 }); return true; } } /** * zoom out one step * CTRL + - * * 109 = numpad minus * 189 = regular minus * 173 = regular minus in Firefox (US and german keyboard layout) */ function zoomOut(key, modifiers) { if ((key === 109 || key === 189 || key === 173) && isCmd(modifiers)) { editorActions.trigger('stepZoom', { value: -1 }); return true; } } /** * zoom to the default level * CTRL + 0 * * 96 = numpad zero * 48 = regular zero */ function zoomDefault(key, modifiers) { if ((key === 96 || key === 48) && isCmd(modifiers)) { editorActions.trigger('zoom', { value: 1 }); return true; } } // delete selected element // DEL function removeSelection(key, modifiers) { Iif (key === 46) { editorActions.trigger('removeSelection'); return true; } } // move canvas left // left arrow // // 37 = Left // 38 = Up // 39 = Right // 40 = Down function moveCanvas(key, modifiers) { if ([37, 38, 39, 40].indexOf(key) >= 0) { var opts = { invertY: config.invertY, speed: (config.speed || 50) }; switch (key) { case 37: // Left opts.direction = 'left'; break; case 38: // Up opts.direction = 'up'; break; case 39: // Right opts.direction = 'right'; break; case 40: // Down opts.direction = 'down'; break; } editorActions.trigger('moveCanvas', opts); return true; } } listeners.push(undo); listeners.push(redo); listeners.push(copy); listeners.push(paste); listeners.push(removeSelection); listeners.push(zoomIn); listeners.push(zoomOut); listeners.push(zoomDefault); listeners.push(moveCanvas); }; /** * Add a listener function that is notified with (key, modifiers) whenever * the keyboard is bound and the user presses a key. * * @param {Function} listenerFn */ Keyboard.prototype.addListener = function(listenerFn) { this._listeners.push(listenerFn); }; Keyboard.prototype.hasModifier = hasModifier; Keyboard.prototype.isCmd = isCmd; Keyboard.prototype.isShift = isShift; function hasModifier(modifiers) { return (modifiers.ctrlKey || modifiers.metaKey || modifiers.shiftKey || modifiers.altKey); } function isCmd(modifiers) { // ensure we don't react to AltGr // (mapped to CTRL + ALT) Iif (modifiers.altKey) { return false; } return modifiers.ctrlKey || modifiers.metaKey; } function isShift(modifiers) { return modifiers.shiftKey; } |