Wrapper for keybindings (codemirror and app) (#2421)

* start

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* add hotkey wrapper

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* updates

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* updates

Signed-off-by: Jess Frazelle <github@jessfraz.com>

---------

Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
Jess Frazelle
2024-05-20 20:52:33 -07:00
committed by GitHub
parent 20c4d44b8b
commit 93ebf13621
8 changed files with 170 additions and 29 deletions

View File

@ -6,6 +6,7 @@ import { isTauri } from 'lib/isTauri'
import { writeTextFile } from '@tauri-apps/plugin-fs'
import toast from 'react-hot-toast'
import { editorManager } from 'lib/singletons'
import { KeyBinding } from '@uiw/react-codemirror'
const PERSIST_CODE_TOKEN = 'persistCode'
@ -13,6 +14,7 @@ export default class CodeManager {
private _code: string = bracket
#updateState: (arg: string) => void = () => {}
private _currentFilePath: string | null = null
private _hotkeys: { [key: string]: () => void } = {}
constructor() {
if (isTauri()) {
@ -48,6 +50,20 @@ export default class CodeManager {
this.#updateState = setCode
}
registerHotkey(hotkey: string, callback: () => void) {
this._hotkeys[hotkey] = callback
}
getCodemirrorHotkeys(): KeyBinding[] {
return Object.keys(this._hotkeys).map((key) => ({
key,
run: () => {
this._hotkeys[key]()
return false
},
}))
}
updateCurrentFilePath(path: string) {
this._currentFilePath = path
}