♻️ Refactor keybinding service

This commit is contained in:
2025-12-20 16:43:04 +08:00
parent 401eb3ab39
commit 7b746155f7
60 changed files with 4526 additions and 1816 deletions

View File

@@ -1,7 +1,6 @@
import {keymap} from '@codemirror/view';
import {Extension, Compartment} from '@codemirror/state';
import {KeyBinding, keymap} from '@codemirror/view';
import {Compartment, Extension} from '@codemirror/state';
import {KeyBinding as KeyBindingConfig} from '@/../bindings/voidraft/internal/models/ent/models';
import {KeyBinding, KeymapResult} from './types';
import {getCommandHandler, isCommandRegistered} from './commands';
/**
@@ -10,13 +9,13 @@ import {getCommandHandler, isCommandRegistered} from './commands';
*/
export class Manager {
private static compartment = new Compartment();
/**
* 将后端快捷键配置转换为CodeMirror快捷键绑定
* @param keyBindings 后端快捷键配置列表
* @returns 转换结果
*/
static convertToKeyBindings(keyBindings: KeyBindingConfig[]): KeymapResult {
static convertToKeyBindings(keyBindings: KeyBindingConfig[]): KeyBinding[] {
const result: KeyBinding[] = [];
for (const binding of keyBindings) {
@@ -25,29 +24,31 @@ export class Manager {
continue;
}
// 检查命令是否已注册(使用 key 字段作为命令标识符)
if (!binding.key || !isCommandRegistered(binding.key)) {
// 检查命令是否已注册
if (!binding.name || !isCommandRegistered(binding.name)) {
continue;
}
// 获取命令处理函数
const handler = getCommandHandler(binding.key);
const handler = getCommandHandler(binding.name);
if (!handler) {
continue;
}
// 转换为CodeMirror快捷键格式
// binding.command 是快捷键组合 (如 "Mod-f")binding.key 是命令标识符
const keyBinding: KeyBinding = {
key: binding.command || '',
key: binding.key || '',
mac: binding.macos || undefined,
win: binding.windows || undefined,
linux: binding.linux || undefined,
run: handler,
preventDefault: true
preventDefault: binding.preventDefault,
scope: binding.scope || undefined
};
result.push(keyBinding);
}
return {keyBindings: result};
return result;
}
/**
@@ -56,7 +57,7 @@ export class Manager {
* @returns CodeMirror扩展
*/
static createKeymapExtension(keyBindings: KeyBindingConfig[]): Extension {
const {keyBindings: cmKeyBindings} = this.convertToKeyBindings(keyBindings);
const cmKeyBindings = this.convertToKeyBindings(keyBindings);
return this.compartment.of(keymap.of(cmKeyBindings));
}
@@ -66,7 +67,7 @@ export class Manager {
* @param keyBindings 后端快捷键配置列表
*/
static updateKeymap(view: any, keyBindings: KeyBindingConfig[]): void {
const {keyBindings: cmKeyBindings} = this.convertToKeyBindings(keyBindings);
const cmKeyBindings = this.convertToKeyBindings(keyBindings);
view.dispatch({
effects: this.compartment.reconfigure(keymap.of(cmKeyBindings))
});