|
1 | 1 | const Mousetrap = require('mousetrap'); |
| 2 | +const merge = require('lodash.merge'); |
| 3 | + |
| 4 | +const defaultConfig = { |
| 5 | + debug: false, |
| 6 | + hotkeys: { |
| 7 | + navigation: { |
| 8 | + up: 'ctrl+alt+up', |
| 9 | + down: 'ctrl+alt+down', |
| 10 | + left: 'ctrl+alt+left', |
| 11 | + right: 'ctrl+alt+right' |
| 12 | + }, |
| 13 | + jump_prefix: 'ctrl+alt', |
| 14 | + permutation_modifier: 'shift', |
| 15 | + }, |
| 16 | + showIndicators: true, |
| 17 | + indicatorPrefix: '^⌥', |
| 18 | + indicatorStyle: { |
| 19 | + position: 'absolute', |
| 20 | + top: 0, |
| 21 | + left: 0, |
| 22 | + fontSize: '10px' |
| 23 | + }, |
| 24 | +}; |
2 | 25 |
|
3 | | -let debug_enabled_ = false; |
| 26 | +let config = defaultConfig; |
4 | 27 |
|
5 | 28 | const debug = function () { |
6 | | - if (debug_enabled_){ |
| 29 | + if (config.debug){ |
7 | 30 | [].unshift.call(arguments, '|HYPER-PANE|'); |
8 | 31 | console.log.apply(this, arguments); |
9 | 32 | } |
10 | | -} |
| 33 | +}; |
11 | 34 |
|
12 | 35 | /** |
13 | 36 | * Duplicate Hyper code |
@@ -73,15 +96,12 @@ const UI_MOVE_LEFT_PANE = 'UI_MOVE_LEFT_PANE'; |
73 | 96 | const UI_MOVE_RIGHT_PANE = 'UI_MOVE_RIGHT_PANE'; |
74 | 97 | const UI_SWITCH_SESSIONS = 'UI_SWITCH_SESSIONS'; |
75 | 98 |
|
76 | | -// Keys |
77 | | -const JUMP_KEYS = 'ctrl+alt'; |
78 | | -const SWITCH_MODIFIER = 'shift'; |
79 | | -const NAV_KEYS = { |
80 | | - UI_MOVE_UP_PANE: 'ctrl+alt+up', |
81 | | - UI_MOVE_DOWN_PANE: 'ctrl+alt+down', |
82 | | - UI_MOVE_LEFT_PANE: 'ctrl+alt+left', |
83 | | - UI_MOVE_RIGHT_PANE: 'ctrl+alt+right' |
84 | | -}; |
| 99 | +const navigationActionMap = { |
| 100 | + up: 'UI_MOVE_UP_PANE', |
| 101 | + down: 'UI_MOVE_DOWN_PANE', |
| 102 | + left: 'UI_MOVE_LEFT_PANE', |
| 103 | + right: 'UI_MOVE_RIGHT_PANE' |
| 104 | +} |
85 | 105 |
|
86 | 106 | // Others |
87 | 107 | const ROOT_FRAME = { |
@@ -274,6 +294,18 @@ const updateChildrenFrames = (state, groupUid) => { |
274 | 294 | * Plugin bindings |
275 | 295 | */ |
276 | 296 |
|
| 297 | + exports.middleware = store => next => action => { |
| 298 | + switch (action.type) { |
| 299 | + case 'CONFIG_LOAD': |
| 300 | + case 'CONFIG_RELOAD': |
| 301 | + if (action.config.paneNavigation) { |
| 302 | + config = merge(JSON.parse(JSON.stringify(defaultConfig)), action.config.paneNavigation); |
| 303 | + } |
| 304 | + break; |
| 305 | + } |
| 306 | + return next(action); |
| 307 | +} |
| 308 | + |
277 | 309 | exports.reduceTermGroups = (state, action) => { |
278 | 310 |
|
279 | 311 | switch (action.type) { |
@@ -385,47 +417,58 @@ exports.decorateTerms = (Terms, { React, notify, Notification }) => { |
385 | 417 | const document = term.getTermDocument(); |
386 | 418 | const keys = new Mousetrap(document); |
387 | 419 |
|
388 | | - ['1','2','3','4','5','6','7','8','9'].forEach(num => { |
389 | | - let shortcut = JUMP_KEYS + `+${num}`; |
390 | | - //debug('Add shortcut', shortcut); |
391 | | - keys.bind( |
392 | | - shortcut, |
393 | | - (e) => { |
394 | | - this.props.onMoveToPane(num); |
395 | | - e.preventDefault(); |
396 | | - this.reattachKeyListner(); |
397 | | - } |
398 | | - ); |
399 | | - shortcut = `${SWITCH_MODIFIER} + ${shortcut}`; |
400 | | - //debug('Add shortcut', shortcut); |
401 | | - keys.bind( |
402 | | - shortcut, |
403 | | - (e) => { |
404 | | - this.props.onMoveToPane(num, true); |
405 | | - e.preventDefault(); |
406 | | - this.reattachKeyListner(); |
| 420 | + const jump_prefix = config.hotkeys.jump_prefix; |
| 421 | + const permutation_modifier = config.hotkeys.permutation_modifier; |
| 422 | + if (jump_prefix && jump_prefix.length) { |
| 423 | + ['1','2','3','4','5','6','7','8','9'].forEach(num => { |
| 424 | + let shortcut = jump_prefix+ `+${num}`; |
| 425 | + //debug('Add shortcut', shortcut); |
| 426 | + keys.bind( |
| 427 | + shortcut, |
| 428 | + (e) => { |
| 429 | + this.props.onMoveToPane(num); |
| 430 | + e.preventDefault(); |
| 431 | + this.reattachKeyListner(); |
| 432 | + } |
| 433 | + ); |
| 434 | + if (permutation_modifier && permutation_modifier.length) { |
| 435 | + shortcut = `${permutation_modifier} + ${shortcut}`; |
| 436 | + //debug('Add shortcut', shortcut); |
| 437 | + keys.bind( |
| 438 | + shortcut, |
| 439 | + (e) => { |
| 440 | + this.props.onMoveToPane(num, true); |
| 441 | + e.preventDefault(); |
| 442 | + this.reattachKeyListner(); |
| 443 | + } |
| 444 | + ); |
407 | 445 | } |
408 | | - ); |
409 | | - }); |
| 446 | + }); |
| 447 | + } |
410 | 448 |
|
411 | | - Object.keys(NAV_KEYS).forEach(direction => { |
412 | | - keys.bind( |
413 | | - NAV_KEYS[direction], |
414 | | - (e) => { |
415 | | - this.props.onMoveToDirectionPane(direction); |
416 | | - e.preventDefault(); |
417 | | - this.reattachKeyListner(); |
418 | | - } |
419 | | - ); |
420 | | - |
421 | | - keys.bind( |
422 | | - `${SWITCH_MODIFIER}+` + NAV_KEYS[direction], |
423 | | - (e) => { |
424 | | - this.props.onMoveToDirectionPane(direction, true); |
425 | | - e.preventDefault(); |
426 | | - this.reattachKeyListner(); |
| 449 | + Object.keys(config.hotkeys.navigation).forEach(direction => { |
| 450 | + const key = config.hotkeys.navigation[direction]; |
| 451 | + const actionType = navigationActionMap[direction]; |
| 452 | + if (key && key.length && actionType && actionType.length) { |
| 453 | + keys.bind( |
| 454 | + key, |
| 455 | + (e) => { |
| 456 | + this.props.onMoveToDirectionPane(actionType); |
| 457 | + e.preventDefault(); |
| 458 | + this.reattachKeyListner(); |
| 459 | + } |
| 460 | + ); |
| 461 | + if (permutation_modifier && permutation_modifier.length) { |
| 462 | + keys.bind( |
| 463 | + `${permutation_modifier}+` + key, |
| 464 | + (e) => { |
| 465 | + this.props.onMoveToDirectionPane(actionType, true); |
| 466 | + e.preventDefault(); |
| 467 | + this.reattachKeyListner(); |
| 468 | + } |
| 469 | + ); |
427 | 470 | } |
428 | | - ); |
| 471 | + } |
429 | 472 | }); |
430 | 473 | this.keys = keys; |
431 | 474 | } |
@@ -460,17 +503,15 @@ exports.decorateTerm = (Term, { React, notify }) => { |
460 | 503 | super(props, context); |
461 | 504 | } |
462 | 505 | render () { |
| 506 | + if (!config.showIndicators) { |
| 507 | + return React.createElement(Term, this.props); |
| 508 | + } |
463 | 509 | const myCustomChildrenBefore = React.createElement( |
464 | 510 | 'div', |
465 | 511 | { |
466 | | - style: { |
467 | | - position: 'absolute', |
468 | | - top: 0, |
469 | | - left: 0, |
470 | | - fontSize: '10px' |
471 | | - } |
| 512 | + style: config.indicatorStyle |
472 | 513 | }, |
473 | | - this.props.termShorcutNum > 0 ? '^⌥' + this.props.termShorcutNum : '' |
| 514 | + this.props.termShorcutNum > 0 ? config.indicatorPrefix + this.props.termShorcutNum : '' |
474 | 515 | ); |
475 | 516 | const customChildrenBefore = this.props.customChildrenBefore |
476 | 517 | ? Array.from(this.props.customChildrenBefore).concat(myCustomChildrenBefore) |
|
0 commit comments