🚧 Refactor backup service
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import { Extension } from '@codemirror/state';
|
||||
import { useKeybindingStore } from '@/stores/keybindingStore';
|
||||
import { useExtensionStore } from '@/stores/extensionStore';
|
||||
import { Manager } from './manager';
|
||||
|
||||
/**
|
||||
@@ -8,24 +7,13 @@ import { Manager } from './manager';
|
||||
*/
|
||||
export const createDynamicKeymapExtension = async (): Promise<Extension> => {
|
||||
const keybindingStore = useKeybindingStore();
|
||||
const extensionStore = useExtensionStore();
|
||||
|
||||
// 确保快捷键配置已加载
|
||||
if (keybindingStore.keyBindings.length === 0) {
|
||||
await keybindingStore.loadKeyBindings();
|
||||
}
|
||||
|
||||
// 确保扩展配置已加载
|
||||
if (extensionStore.extensions.length === 0) {
|
||||
await extensionStore.loadExtensions();
|
||||
}
|
||||
|
||||
// 获取启用的扩展key列表
|
||||
const enabledExtensionKeys = extensionStore.enabledExtensions
|
||||
.map(ext => ext.key)
|
||||
.filter((key): key is string => key !== undefined);
|
||||
|
||||
return Manager.createKeymapExtension(keybindingStore.keyBindings, enabledExtensionKeys);
|
||||
return Manager.createKeymapExtension(keybindingStore.keyBindings);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -34,14 +22,7 @@ export const createDynamicKeymapExtension = async (): Promise<Extension> => {
|
||||
*/
|
||||
export const updateKeymapExtension = (view: any): void => {
|
||||
const keybindingStore = useKeybindingStore();
|
||||
const extensionStore = useExtensionStore();
|
||||
|
||||
// 获取启用的扩展key列表
|
||||
const enabledExtensionKeys = extensionStore.enabledExtensions
|
||||
.map(ext => ext.key)
|
||||
.filter((key): key is string => key !== undefined);
|
||||
|
||||
Manager.updateKeymap(view, keybindingStore.keyBindings, enabledExtensionKeys);
|
||||
Manager.updateKeymap(view, keybindingStore.keyBindings);
|
||||
};
|
||||
|
||||
// 导出相关模块
|
||||
|
||||
@@ -14,10 +14,9 @@ export class Manager {
|
||||
/**
|
||||
* 将后端快捷键配置转换为CodeMirror快捷键绑定
|
||||
* @param keyBindings 后端快捷键配置列表
|
||||
* @param enabledExtensions 启用的扩展key列表,如果不提供则使用所有启用的快捷键
|
||||
* @returns 转换结果
|
||||
*/
|
||||
static convertToKeyBindings(keyBindings: KeyBindingConfig[], enabledExtensions?: string[]): KeymapResult {
|
||||
static convertToKeyBindings(keyBindings: KeyBindingConfig[]): KeymapResult {
|
||||
const result: KeyBinding[] = [];
|
||||
|
||||
for (const binding of keyBindings) {
|
||||
@@ -26,11 +25,6 @@ export class Manager {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 如果提供了扩展列表,则只处理启用扩展的快捷键
|
||||
if (enabledExtensions && binding.extension && !enabledExtensions.includes(binding.extension)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 检查命令是否已注册(使用 key 字段作为命令标识符)
|
||||
if (!binding.key || !isCommandRegistered(binding.key)) {
|
||||
continue;
|
||||
@@ -59,13 +53,10 @@ export class Manager {
|
||||
/**
|
||||
* 创建CodeMirror快捷键扩展
|
||||
* @param keyBindings 后端快捷键配置列表
|
||||
* @param enabledExtensions 启用的扩展key列表
|
||||
* @returns CodeMirror扩展
|
||||
*/
|
||||
static createKeymapExtension(keyBindings: KeyBindingConfig[], enabledExtensions?: string[]): Extension {
|
||||
const {keyBindings: cmKeyBindings} =
|
||||
this.convertToKeyBindings(keyBindings, enabledExtensions);
|
||||
|
||||
static createKeymapExtension(keyBindings: KeyBindingConfig[]): Extension {
|
||||
const {keyBindings: cmKeyBindings} = this.convertToKeyBindings(keyBindings);
|
||||
return this.compartment.of(keymap.of(cmKeyBindings));
|
||||
}
|
||||
|
||||
@@ -73,12 +64,9 @@ export class Manager {
|
||||
* 动态更新快捷键扩展
|
||||
* @param view 编辑器视图
|
||||
* @param keyBindings 后端快捷键配置列表
|
||||
* @param enabledExtensions 启用的扩展key列表
|
||||
*/
|
||||
static updateKeymap(view: any, keyBindings: KeyBindingConfig[], enabledExtensions: string[]): void {
|
||||
const {keyBindings: cmKeyBindings} =
|
||||
this.convertToKeyBindings(keyBindings, enabledExtensions);
|
||||
|
||||
static updateKeymap(view: any, keyBindings: KeyBindingConfig[]): void {
|
||||
const {keyBindings: cmKeyBindings} = this.convertToKeyBindings(keyBindings);
|
||||
view.dispatch({
|
||||
effects: this.compartment.reconfigure(keymap.of(cmKeyBindings))
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user