Skip to content

Commit d8b2872

Browse files
feat(keymap): add cmd+ / cmd- to control editor font size (#222)
1 parent cdaab98 commit d8b2872

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

src/renderer/App.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {
4848
useSupportNotification,
4949
checkForRemoteNotification
5050
} from '@/composable/notification'
51+
import { useKeyMap } from '@/composable/keymap'
5152
5253
// По какой то причине необходимо явно установить роут в '/'
5354
// для корректного поведения в продакшен сборке
@@ -97,6 +98,7 @@ const init = async () => {
9798
9899
trackAppUpdate()
99100
checkForRemoteNotification()
101+
useKeyMap()
100102
}
101103
102104
const setTheme = (theme: string) => {

src/renderer/components/editor/EditorCodemirror.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,15 @@ watch(
323323
)
324324
325325
emitter.on('snippet:format', () => format())
326+
emitter.on('editor:refresh', () => editor.refresh())
326327
327328
onMounted(() => {
328329
init()
329330
})
330331
331332
onUnmounted(() => {
332333
emitter.off('snippet:format')
334+
emitter.off('editor:refresh')
333335
})
334336
</script>
335337

src/renderer/composable/keymap.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { store } from '@/electron'
2+
import { EDITOR_DEFAULTS, useAppStore } from '@/store/app'
3+
import { useMagicKeys } from '@vueuse/core'
4+
import { watch } from 'vue'
5+
import router from '@/router'
6+
import { emitter } from '.'
7+
8+
export const useKeyMap = () => {
9+
const appStore = useAppStore()
10+
11+
// Вариант с meta.value && Equal.value вызывается только один раз,
12+
// последующее нажатие на клавишу "="" не вызывает метод.
13+
// Поэтому сделал так
14+
useMagicKeys({
15+
passive: false,
16+
onEventFired: e => {
17+
const { path } = router.currentRoute.value
18+
19+
if (path !== '/') return
20+
21+
if (e.metaKey && e.key === '=') {
22+
appStore.editor.fontSize += 1
23+
}
24+
25+
if (e.metaKey && e.key === '-') {
26+
if (appStore.editor.fontSize === 1) return
27+
appStore.editor.fontSize -= 1
28+
}
29+
30+
if (e.metaKey && e.key === '0') {
31+
appStore.editor.fontSize = EDITOR_DEFAULTS.fontSize
32+
}
33+
34+
emitter.emit('editor:refresh', true)
35+
}
36+
})
37+
38+
watch(
39+
() => appStore.editor,
40+
v => {
41+
store.preferences.set('editor', { ...v })
42+
},
43+
{ deep: true }
44+
)
45+
}

src/renderer/store/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {
1010
import { defineStore } from 'pinia'
1111
import { version } from '../../../package.json'
1212

13-
const EDITOR_DEFAULTS: EditorSettings = {
13+
export const EDITOR_DEFAULTS: EditorSettings = {
1414
fontFamily: 'SF Mono, Consolas, Menlo, Ubuntu Mono, monospace',
1515
fontSize: 12,
1616
tabSize: 2,

src/shared/types/renderer/composable/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ export type EmitterEvents = {
77
'scroll-to:folder': string
88
'scroll-to:snippet': string
99
'search:focus': boolean
10+
'editor:refresh': boolean
1011
}

0 commit comments

Comments
 (0)