🎨 Refactor and optimize code
This commit is contained in:
@@ -66,7 +66,7 @@ const defaultEditorOptions = {
|
||||
* 前端命令注册表
|
||||
* 将后端定义的command字段映射到具体的前端方法和翻译键
|
||||
*/
|
||||
export const commandRegistry = {
|
||||
export const commands = {
|
||||
[KeyBindingCommand.ShowSearchCommand]: {
|
||||
handler: showSearchVisibilityCommand,
|
||||
descriptionKey: 'keybindings.commands.showSearch'
|
||||
@@ -299,7 +299,7 @@ export const commandRegistry = {
|
||||
* @returns 对应的处理函数,如果不存在则返回 undefined
|
||||
*/
|
||||
export const getCommandHandler = (command: KeyBindingCommand) => {
|
||||
return commandRegistry[command]?.handler;
|
||||
return commands[command]?.handler;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -308,7 +308,7 @@ export const getCommandHandler = (command: KeyBindingCommand) => {
|
||||
* @returns 对应的描述,如果不存在则返回 undefined
|
||||
*/
|
||||
export const getCommandDescription = (command: KeyBindingCommand) => {
|
||||
const descriptionKey = commandRegistry[command]?.descriptionKey;
|
||||
const descriptionKey = commands[command]?.descriptionKey;
|
||||
return descriptionKey ? i18n.global.t(descriptionKey) : undefined;
|
||||
};
|
||||
|
||||
@@ -318,7 +318,7 @@ export const getCommandDescription = (command: KeyBindingCommand) => {
|
||||
* @returns 是否已注册
|
||||
*/
|
||||
export const isCommandRegistered = (command: KeyBindingCommand): boolean => {
|
||||
return command in commandRegistry;
|
||||
return command in commands;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -326,5 +326,5 @@ export const isCommandRegistered = (command: KeyBindingCommand): boolean => {
|
||||
* @returns 已注册的命令列表
|
||||
*/
|
||||
export const getRegisteredCommands = (): KeyBindingCommand[] => {
|
||||
return Object.keys(commandRegistry) as KeyBindingCommand[];
|
||||
return Object.keys(commands) as KeyBindingCommand[];
|
||||
};
|
||||
@@ -2,11 +2,9 @@ import { Extension } from '@codemirror/state';
|
||||
import { useKeybindingStore } from '@/stores/keybindingStore';
|
||||
import { useExtensionStore } from '@/stores/extensionStore';
|
||||
import { KeymapManager } from './keymapManager';
|
||||
import { ExtensionID } from '@/../bindings/voidraft/internal/models/models';
|
||||
|
||||
/**
|
||||
* 异步创建快捷键扩展
|
||||
* 确保快捷键配置和扩展配置已加载
|
||||
*/
|
||||
export const createDynamicKeymapExtension = async (): Promise<Extension> => {
|
||||
const keybindingStore = useKeybindingStore();
|
||||
@@ -42,17 +40,7 @@ export const updateKeymapExtension = (view: any): void => {
|
||||
KeymapManager.updateKeymap(view, keybindingStore.keyBindings, enabledExtensionIds);
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取指定扩展的快捷键
|
||||
* @param extensionId 扩展ID
|
||||
* @returns 该扩展的快捷键列表
|
||||
*/
|
||||
export const getExtensionKeyBindings = (extensionId: ExtensionID) => {
|
||||
const keybindingStore = useKeybindingStore();
|
||||
return keybindingStore.getKeyBindingsByExtension(extensionId);
|
||||
};
|
||||
|
||||
// 导出相关模块
|
||||
export { KeymapManager } from './keymapManager';
|
||||
export { commandRegistry, getCommandHandler, getCommandDescription, isCommandRegistered, getRegisteredCommands } from './commandRegistry';
|
||||
export { commands, getCommandHandler, getCommandDescription, isCommandRegistered, getRegisteredCommands } from './commands';
|
||||
export type { KeyBinding, CommandHandler, CommandDefinition, KeymapResult } from './types';
|
||||
@@ -2,7 +2,7 @@ import {keymap} from '@codemirror/view';
|
||||
import {Extension, Compartment} from '@codemirror/state';
|
||||
import {KeyBinding as KeyBindingConfig, ExtensionID} from '@/../bindings/voidraft/internal/models/models';
|
||||
import {KeyBinding, KeymapResult} from './types';
|
||||
import {getCommandHandler, isCommandRegistered} from './commandRegistry';
|
||||
import {getCommandHandler, isCommandRegistered} from './commands';
|
||||
|
||||
/**
|
||||
* 快捷键管理器
|
||||
@@ -82,45 +82,4 @@ export class KeymapManager {
|
||||
effects: this.compartment.reconfigure(keymap.of(cmKeyBindings))
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 按扩展分组快捷键
|
||||
* @param keyBindings 快捷键配置列表
|
||||
* @returns 按扩展分组的快捷键映射
|
||||
*/
|
||||
static groupByExtension(keyBindings: KeyBindingConfig[]): Map<ExtensionID, KeyBindingConfig[]> {
|
||||
const groups = new Map<ExtensionID, KeyBindingConfig[]>();
|
||||
|
||||
for (const binding of keyBindings) {
|
||||
if (!groups.has(binding.extension)) {
|
||||
groups.set(binding.extension, []);
|
||||
}
|
||||
groups.get(binding.extension)!.push(binding);
|
||||
}
|
||||
|
||||
return groups;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证快捷键配置
|
||||
* @param keyBindings 快捷键配置列表
|
||||
* @returns 验证结果
|
||||
*/
|
||||
static validateKeyBindings(keyBindings: KeyBindingConfig[]): {
|
||||
valid: KeyBindingConfig[]
|
||||
invalid: KeyBindingConfig[]
|
||||
} {
|
||||
const valid: KeyBindingConfig[] = [];
|
||||
const invalid: KeyBindingConfig[] = [];
|
||||
|
||||
for (const binding of keyBindings) {
|
||||
if (binding.enabled && binding.key && isCommandRegistered(binding.command)) {
|
||||
valid.push(binding);
|
||||
} else {
|
||||
invalid.push(binding);
|
||||
}
|
||||
}
|
||||
|
||||
return {valid, invalid};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user