Skip to content

Commit 0934c08

Browse files
committed
added shortcut manager and allow user to customize toggle mode shortcut
1 parent fb24efd commit 0934c08

File tree

9 files changed

+100
-12
lines changed

9 files changed

+100
-12
lines changed

browser/lib/utils.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,25 @@ export function escapeHtmlCharacters (text) {
5454
: html
5555
}
5656

57+
export function isObjectEqual (a, b) {
58+
const aProps = Object.getOwnPropertyNames(a)
59+
const bProps = Object.getOwnPropertyNames(b)
60+
61+
if (aProps.length !== bProps.length) {
62+
return false
63+
}
64+
65+
for (var i = 0; i < aProps.length; i++) {
66+
const propName = aProps[i]
67+
if (a[propName] !== b[propName]) {
68+
return false
69+
}
70+
}
71+
return true
72+
}
73+
5774
export default {
5875
lastFindInArray,
59-
escapeHtmlCharacters
76+
escapeHtmlCharacters,
77+
isObjectEqual
6078
}

browser/main/Main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { hashHistory } from 'react-router'
1616
import store from 'browser/main/store'
1717
import i18n from 'browser/lib/i18n'
1818
import { getLocales } from 'browser/lib/Languages'
19+
import applyShortcuts from 'browser/main/lib/shortcutManager'
1920
const path = require('path')
2021
const electron = require('electron')
2122
const { remote } = electron
@@ -159,7 +160,7 @@ class Main extends React.Component {
159160
} else {
160161
i18n.setLocale('en')
161162
}
162-
163+
applyShortcuts()
163164
// Reload all data
164165
dataApi.init()
165166
.then((data) => {

browser/main/index.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { syncHistoryWithStore } from 'react-router-redux'
99
require('./lib/ipcClient')
1010
require('../lib/customMeta')
1111
import i18n from 'browser/lib/i18n'
12-
import ee from 'browser/main/lib/eventEmitter'
1312

1413
const electron = require('electron')
1514

@@ -64,12 +63,6 @@ document.addEventListener('keyup', function (e) {
6463
}
6564
})
6665

67-
document.addEventListener('keydown', function(e) {
68-
if (e.key === 'm' && e.ctrlKey) {
69-
ee.emit('topbar:togglemodebutton')
70-
}
71-
});
72-
7366
document.addEventListener('click', function (e) {
7467
const className = e.target.className
7568
if (!className && typeof (className) !== 'string') return

browser/main/lib/ConfigManager.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import _ from 'lodash'
22
import RcParser from 'browser/lib/RcParser'
33
import i18n from 'browser/lib/i18n'
4+
import ee from 'browser/main/lib/eventEmitter'
45

56
const OSX = global.process.platform === 'darwin'
67
const win = global.process.platform === 'win32'
@@ -20,7 +21,8 @@ export const DEFAULT_CONFIG = {
2021
listStyle: 'DEFAULT', // 'DEFAULT', 'SMALL'
2122
amaEnabled: true,
2223
hotkey: {
23-
toggleMain: OSX ? 'Cmd + Alt + L' : 'Super + Alt + E'
24+
toggleMain: OSX ? 'Cmd + Alt + L' : 'Super + Alt + E',
25+
toggleMode: OSX ? 'Cmd + M' : 'Ctrl + M'
2426
},
2527
ui: {
2628
language: 'en',
@@ -166,6 +168,7 @@ function set (updates) {
166168
ipcRenderer.send('config-renew', {
167169
config: get()
168170
})
171+
ee.emit('config-renew')
169172
}
170173

171174
function assignConfigValues (originalConfig, rcConfig) {

browser/main/lib/shortcut.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import ee from 'browser/main/lib/eventEmitter'
2+
3+
module.exports = {
4+
'toggleMode': () => {
5+
ee.emit('topbar:togglemodebutton')
6+
}
7+
}

browser/main/lib/shortcutManager.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import Mousetrap from 'mousetrap'
2+
import CM from 'browser/main/lib/ConfigManager'
3+
import ee from 'browser/main/lib/eventEmitter'
4+
import { isObjectEqual } from 'browser/lib/utils'
5+
require('mousetrap-global-bind')
6+
const functions = require('./shortcut')
7+
8+
let shortcuts = CM.get().hotkey
9+
10+
ee.on('config-renew', function () {
11+
// only update if hotkey changed !
12+
const newHotkey = CM.get().hotkey
13+
if (!isObjectEqual(newHotkey, shortcuts)) {
14+
updateShortcut(newHotkey)
15+
}
16+
})
17+
18+
function updateShortcut (newHotkey) {
19+
Mousetrap.reset()
20+
shortcuts = newHotkey
21+
applyShortcuts(newHotkey)
22+
}
23+
24+
function formatShortcut (shortcut) {
25+
return shortcut.toLowerCase().replace(/ /g, '')
26+
}
27+
28+
function applyShortcuts (shortcuts) {
29+
for (const shortcut in shortcuts) {
30+
const toggler = formatShortcut(shortcuts[shortcut])
31+
// only bind if the function for that shortcut exists
32+
if (functions[shortcut]) {
33+
Mousetrap.bindGlobal(toggler, functions[shortcut])
34+
}
35+
}
36+
}
37+
38+
applyShortcuts(CM.get().hotkey)
39+
40+
module.exports = applyShortcuts

browser/main/modals/PreferencesModal/HotkeyTab.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ class HotkeyTab extends React.Component {
6767
handleHotkeyChange (e) {
6868
const { config } = this.state
6969
config.hotkey = {
70-
toggleMain: this.refs.toggleMain.value
70+
toggleMain: this.refs.toggleMain.value,
71+
toggleMode: this.refs.toggleMode.value
7172
}
7273
this.setState({
7374
config
@@ -115,6 +116,17 @@ class HotkeyTab extends React.Component {
115116
/>
116117
</div>
117118
</div>
119+
<div styleName='group-section'>
120+
<div styleName='group-section-label'>{i18n.__('Toggle editor mode')}</div>
121+
<div styleName='group-section-control'>
122+
<input styleName='group-section-control-input'
123+
onChange={(e) => this.handleHotkeyChange(e)}
124+
ref='toggleMode'
125+
value={config.hotkey.toggleMode}
126+
type='text'
127+
/>
128+
</div>
129+
</div>
118130
<div styleName='group-control'>
119131
<button styleName='group-control-leftButton'
120132
onClick={(e) => this.handleHintToggleButtonClick(e)}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979
"md5": "^2.0.0",
8080
"mdurl": "^1.0.1",
8181
"moment": "^2.10.3",
82+
"mousetrap": "^1.6.1",
83+
"mousetrap-global-bind": "^1.1.0",
8284
"node-ipc": "^8.1.0",
8385
"raphael": "^2.2.7",
8486
"react": "^15.5.4",

yarn.lock

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1864,14 +1864,18 @@ codemirror-mode-elixir@^1.1.1:
18641864
dependencies:
18651865
codemirror "^5.20.2"
18661866

1867-
codemirror@^5.18.2, codemirror@^5.19.0:
1867+
codemirror@^5.18.2:
18681868
version "5.26.0"
18691869
resolved "https://registry.yarnpkg.com/codemirror/-/codemirror-5.26.0.tgz#bcbee86816ed123870c260461c2b5c40b68746e5"
18701870

18711871
codemirror@^5.20.2:
18721872
version "5.33.0"
18731873
resolved "https://registry.yarnpkg.com/codemirror/-/codemirror-5.33.0.tgz#462ad9a6fe8d38b541a9536a3997e1ef93b40c6a"
18741874

1875+
codemirror@^5.37.0:
1876+
version "5.37.0"
1877+
resolved "https://registry.yarnpkg.com/codemirror/-/codemirror-5.37.0.tgz#c349b584e158f590277f26d37c2469a6bc538036"
1878+
18751879
coffee-script@^1.10.0:
18761880
version "1.12.6"
18771881
resolved "https://registry.yarnpkg.com/coffee-script/-/coffee-script-1.12.6.tgz#285a3f7115689065064d6bf9ef4572db66695cbf"
@@ -5960,6 +5964,14 @@ moment@^2.10.3:
59605964
version "2.18.1"
59615965
resolved "https://registry.yarnpkg.com/moment/-/moment-2.18.1.tgz#c36193dd3ce1c2eed2adb7c802dbbc77a81b1c0f"
59625966

5967+
mousetrap-global-bind@^1.1.0:
5968+
version "1.1.0"
5969+
resolved "https://registry.yarnpkg.com/mousetrap-global-bind/-/mousetrap-global-bind-1.1.0.tgz#cd7de9222bd0646fa2e010d54c84a74c26a88edd"
5970+
5971+
mousetrap@^1.6.1:
5972+
version "1.6.1"
5973+
resolved "https://registry.yarnpkg.com/mousetrap/-/mousetrap-1.6.1.tgz#2a085f5c751294c75e7e81f6ec2545b29cbf42d9"
5974+
59635975
59645976
version "0.7.1"
59655977
resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.1.tgz#9cd13c03adbff25b65effde7ce864ee952017098"

0 commit comments

Comments
 (0)