Skip to content

Commit 67143ba

Browse files
authored
Merge pull request #1929 from ZeroX-DG/shortcut-mode
Shortcut to toggle mode
2 parents 0ca4e6c + 0934c08 commit 67143ba

File tree

9 files changed

+99
-4
lines changed

9 files changed

+99
-4
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/Detail/MarkdownNoteDetail.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ class MarkdownNoteDetail extends React.Component {
5555

5656
componentDidMount () {
5757
ee.on('topbar:togglelockbutton', this.toggleLockButton)
58+
ee.on('topbar:togglemodebutton', () => {
59+
const reversedType = this.state.editorType === 'SPLIT' ? 'EDITOR_PREVIEW' : 'SPLIT'
60+
this.handleSwitchMode(reversedType)
61+
})
5862
}
5963

6064
componentWillReceiveProps (nextProps) {

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/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',
@@ -167,6 +169,7 @@ function set (updates) {
167169
ipcRenderer.send('config-renew', {
168170
config: get()
169171
})
172+
ee.emit('config-renew')
170173
}
171174

172175
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
@@ -81,6 +81,8 @@
8181
"md5": "^2.0.0",
8282
"mdurl": "^1.0.1",
8383
"moment": "^2.10.3",
84+
"mousetrap": "^1.6.1",
85+
"mousetrap-global-bind": "^1.1.0",
8486
"node-ipc": "^8.1.0",
8587
"raphael": "^2.2.7",
8688
"react": "^15.5.4",

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5978,6 +5978,14 @@ moment@^2.10.3:
59785978
version "2.18.1"
59795979
resolved "https://registry.yarnpkg.com/moment/-/moment-2.18.1.tgz#c36193dd3ce1c2eed2adb7c802dbbc77a81b1c0f"
59805980

5981+
mousetrap-global-bind@^1.1.0:
5982+
version "1.1.0"
5983+
resolved "https://registry.yarnpkg.com/mousetrap-global-bind/-/mousetrap-global-bind-1.1.0.tgz#cd7de9222bd0646fa2e010d54c84a74c26a88edd"
5984+
5985+
mousetrap@^1.6.1:
5986+
version "1.6.1"
5987+
resolved "https://registry.yarnpkg.com/mousetrap/-/mousetrap-1.6.1.tgz#2a085f5c751294c75e7e81f6ec2545b29cbf42d9"
5988+
59815989
59825990
version "0.7.1"
59835991
resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.1.tgz#9cd13c03adbff25b65effde7ce864ee952017098"

0 commit comments

Comments
 (0)