Skip to content

Commit 6f41012

Browse files
committed
fix: prevent hotkeys when typing in monaco editor
1 parent c87d474 commit 6f41012

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

packages/client/setup/monaco.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { SyncDescriptor } from 'monaco-editor/esm/vs/platform/instantiation/comm
2020
import ts from 'typescript'
2121
import { watchEffect } from 'vue'
2222
import { isDark } from '../logic/dark'
23+
import { lockShortcuts, unlockShortcuts } from '../state'
2324

2425
window.MonacoEnvironment = {
2526
getWorker(_, label) {
@@ -97,6 +98,16 @@ const setup = createSingletonPromise(async () => {
9798
Object.assign(editorOptions, result?.editorOptions)
9899
}
99100

101+
// Disable shortcuts when focusing Monaco editor.
102+
monaco.editor.onDidCreateEditor((editor) => {
103+
editor.onDidFocusEditorWidget(() => {
104+
lockShortcuts()
105+
})
106+
editor.onDidBlurEditorWidget(() => {
107+
unlockShortcuts()
108+
})
109+
})
110+
100111
// Use Shiki to highlight Monaco
101112
shikiToMonaco(highlighter, monaco)
102113
if (typeof themes === 'string') {

packages/client/state/storage.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,28 @@ export const showOverview = ref(false)
1515
export const hmrSkipTransition = ref(false)
1616
export const disableTransition = ref(false)
1717

18-
export const shortcutsEnabled = ref(true)
18+
const mutableShortcutsEnabled = ref(true)
19+
/**
20+
* Whether the keyboard shortcuts are enabled. Readonly,
21+
* use `lockShortcuts` and `unlockShortcuts` to modify.
22+
*/
23+
export const shortcutsEnabled = computed(() => mutableShortcutsEnabled.value)
24+
25+
// Use a lock counter to allow multiple locks, and avoid
26+
// race conditions.
27+
let shortcutsLockCounter = 0
28+
export function lockShortcuts() {
29+
shortcutsLockCounter++
30+
mutableShortcutsEnabled.value = false
31+
}
32+
export function unlockShortcuts() {
33+
shortcutsLockCounter--
34+
if (shortcutsLockCounter <= 0) {
35+
shortcutsLockCounter = 0
36+
mutableShortcutsEnabled.value = true
37+
}
38+
}
39+
1940
export const breakpoints = useBreakpoints({
2041
xs: 460,
2142
...breakpointsTailwind,

0 commit comments

Comments
 (0)