Skip to content

Commit 74ee58c

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

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-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: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,30 @@ 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 support multiple simultaneous locks
26+
// and avoid race conditions. Race conditions may occur, for example,
27+
// when locking shortcuts on editor focus and moving from one editor
28+
// to another, as blur events can be triggered after focus.
29+
let shortcutsLockCounter = 0
30+
export function lockShortcuts() {
31+
shortcutsLockCounter++
32+
mutableShortcutsEnabled.value = false
33+
}
34+
export function unlockShortcuts() {
35+
shortcutsLockCounter--
36+
if (shortcutsLockCounter <= 0) {
37+
shortcutsLockCounter = 0
38+
mutableShortcutsEnabled.value = true
39+
}
40+
}
41+
1942
export const breakpoints = useBreakpoints({
2043
xs: 460,
2144
...breakpointsTailwind,

0 commit comments

Comments
 (0)