🚨 Format code

This commit is contained in:
2025-09-24 21:44:42 +08:00
parent 1462d8a753
commit f5bfff80b7
76 changed files with 839 additions and 863 deletions

View File

@@ -1,4 +1,4 @@
import {KeyBindingCommand} from '@/../bindings/voidraft/internal/models/models'
import {KeyBindingCommand} from '@/../bindings/voidraft/internal/models/models';
import {
hideSearchVisibilityCommand,
searchReplaceAll,
@@ -7,7 +7,7 @@ import {
searchToggleRegex,
searchToggleWholeWord,
showSearchVisibilityCommand
} from '../extensions/vscodeSearch/commands'
} from '../extensions/vscodeSearch/commands';
import {
addNewBlockAfterCurrent,
addNewBlockAfterLast,
@@ -20,13 +20,13 @@ import {
moveCurrentBlockUp,
selectNextBlock,
selectPreviousBlock
} from '../extensions/codeblock/commands'
import {selectAll} from '../extensions/codeblock/selectAll'
import {deleteLineCommand} from '../extensions/codeblock/deleteLine'
import {moveLineDown, moveLineUp} from '../extensions/codeblock/moveLines'
import {transposeChars} from '../extensions/codeblock'
import {copyCommand, cutCommand, pasteCommand} from '../extensions/codeblock/copyPaste'
import {textHighlightToggleCommand} from '../extensions/textHighlight/textHighlightExtension'
} from '../extensions/codeblock/commands';
import {selectAll} from '../extensions/codeblock/selectAll';
import {deleteLineCommand} from '../extensions/codeblock/deleteLine';
import {moveLineDown, moveLineUp} from '../extensions/codeblock/moveLines';
import {transposeChars} from '../extensions/codeblock';
import {copyCommand, cutCommand, pasteCommand} from '../extensions/codeblock/copyPaste';
import {textHighlightToggleCommand} from '../extensions/textHighlight/textHighlightExtension';
import {
copyLineDown,
copyLineUp,
@@ -52,15 +52,15 @@ import {
toggleComment,
undo,
undoSelection
} from '@codemirror/commands'
import {foldAll, foldCode, unfoldAll, unfoldCode} from '@codemirror/language'
import i18n from '@/i18n'
} from '@codemirror/commands';
import {foldAll, foldCode, unfoldAll, unfoldCode} from '@codemirror/language';
import i18n from '@/i18n';
// 默认编辑器选项
const defaultEditorOptions = {
defaultBlockToken: 'text',
defaultBlockAutoDetect: true,
}
};
/**
* 前端命令注册表
@@ -291,7 +291,7 @@ export const commandRegistry = {
handler: textHighlightToggleCommand,
descriptionKey: 'keybindings.commands.textHighlightToggle'
},
} as const
} as const;
/**
* 获取命令处理函数
@@ -299,8 +299,8 @@ export const commandRegistry = {
* @returns 对应的处理函数,如果不存在则返回 undefined
*/
export const getCommandHandler = (command: KeyBindingCommand) => {
return commandRegistry[command]?.handler
}
return commandRegistry[command]?.handler;
};
/**
* 获取命令描述
@@ -308,9 +308,9 @@ export const getCommandHandler = (command: KeyBindingCommand) => {
* @returns 对应的描述,如果不存在则返回 undefined
*/
export const getCommandDescription = (command: KeyBindingCommand) => {
const descriptionKey = commandRegistry[command]?.descriptionKey
return descriptionKey ? i18n.global.t(descriptionKey) : undefined
}
const descriptionKey = commandRegistry[command]?.descriptionKey;
return descriptionKey ? i18n.global.t(descriptionKey) : undefined;
};
/**
* 检查命令是否已注册
@@ -318,13 +318,13 @@ export const getCommandDescription = (command: KeyBindingCommand) => {
* @returns 是否已注册
*/
export const isCommandRegistered = (command: KeyBindingCommand): boolean => {
return command in commandRegistry
}
return command in commandRegistry;
};
/**
* 获取所有已注册的命令
* @returns 已注册的命令列表
*/
export const getRegisteredCommands = (): KeyBindingCommand[] => {
return Object.keys(commandRegistry) as KeyBindingCommand[]
}
return Object.keys(commandRegistry) as KeyBindingCommand[];
};

View File

@@ -1,46 +1,46 @@
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'
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()
const extensionStore = useExtensionStore()
const keybindingStore = useKeybindingStore();
const extensionStore = useExtensionStore();
// 确保快捷键配置已加载
if (keybindingStore.keyBindings.length === 0) {
await keybindingStore.loadKeyBindings()
await keybindingStore.loadKeyBindings();
}
// 确保扩展配置已加载
if (extensionStore.extensions.length === 0) {
await extensionStore.loadExtensions()
await extensionStore.loadExtensions();
}
// 获取启用的扩展ID列表
const enabledExtensionIds = extensionStore.enabledExtensions.map(ext => ext.id)
const enabledExtensionIds = extensionStore.enabledExtensions.map(ext => ext.id);
return KeymapManager.createKeymapExtension(keybindingStore.keyBindings, enabledExtensionIds)
}
return KeymapManager.createKeymapExtension(keybindingStore.keyBindings, enabledExtensionIds);
};
/**
* 更新快捷键映射
* @param view 编辑器视图
*/
export const updateKeymapExtension = (view: any): void => {
const keybindingStore = useKeybindingStore()
const extensionStore = useExtensionStore()
const keybindingStore = useKeybindingStore();
const extensionStore = useExtensionStore();
// 获取启用的扩展ID列表
const enabledExtensionIds = extensionStore.enabledExtensions.map(ext => ext.id)
const enabledExtensionIds = extensionStore.enabledExtensions.map(ext => ext.id);
KeymapManager.updateKeymap(view, keybindingStore.keyBindings, enabledExtensionIds)
}
KeymapManager.updateKeymap(view, keybindingStore.keyBindings, enabledExtensionIds);
};
/**
* 获取指定扩展的快捷键
@@ -48,11 +48,11 @@ export const updateKeymapExtension = (view: any): void => {
* @returns 该扩展的快捷键列表
*/
export const getExtensionKeyBindings = (extensionId: ExtensionID) => {
const keybindingStore = useKeybindingStore()
return keybindingStore.getKeyBindingsByExtension(extensionId)
}
const keybindingStore = useKeybindingStore();
return keybindingStore.getKeyBindingsByExtension(extensionId);
};
// 导出相关模块
export { KeymapManager } from './keymapManager'
export { commandRegistry, getCommandHandler, getCommandDescription, isCommandRegistered, getRegisteredCommands } from './commandRegistry'
export type { KeyBinding, CommandHandler, CommandDefinition, KeymapResult } from './types'
export { KeymapManager } from './keymapManager';
export { commandRegistry, getCommandHandler, getCommandDescription, isCommandRegistered, getRegisteredCommands } from './commandRegistry';
export type { KeyBinding, CommandHandler, CommandDefinition, KeymapResult } from './types';

View File

@@ -1,15 +1,15 @@
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 {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';
/**
* 快捷键管理器
* 负责将后端配置转换为CodeMirror快捷键扩展
*/
export class KeymapManager {
private static compartment = new Compartment()
private static compartment = new Compartment();
/**
* 将后端快捷键配置转换为CodeMirror快捷键绑定
@@ -18,28 +18,28 @@ export class KeymapManager {
* @returns 转换结果
*/
static convertToKeyBindings(keyBindings: KeyBindingConfig[], enabledExtensions?: ExtensionID[]): KeymapResult {
const result: KeyBinding[] = []
const result: KeyBinding[] = [];
for (const binding of keyBindings) {
// 跳过禁用的快捷键
if (!binding.enabled) {
continue
continue;
}
// 如果提供了扩展列表,则只处理启用扩展的快捷键
if (enabledExtensions && !enabledExtensions.includes(binding.extension)) {
continue
continue;
}
// 检查命令是否已注册
if (!isCommandRegistered(binding.command)) {
continue
continue;
}
// 获取命令处理函数
const handler = getCommandHandler(binding.command)
const handler = getCommandHandler(binding.command);
if (!handler) {
continue
continue;
}
// 转换为CodeMirror快捷键格式
@@ -47,12 +47,12 @@ export class KeymapManager {
key: binding.key,
run: handler,
preventDefault: true
}
};
result.push(keyBinding)
result.push(keyBinding);
}
return {keyBindings: result}
return {keyBindings: result};
}
/**
@@ -63,9 +63,9 @@ export class KeymapManager {
*/
static createKeymapExtension(keyBindings: KeyBindingConfig[], enabledExtensions?: ExtensionID[]): Extension {
const {keyBindings: cmKeyBindings} =
this.convertToKeyBindings(keyBindings, enabledExtensions)
this.convertToKeyBindings(keyBindings, enabledExtensions);
return this.compartment.of(keymap.of(cmKeyBindings))
return this.compartment.of(keymap.of(cmKeyBindings));
}
/**
@@ -76,11 +76,11 @@ export class KeymapManager {
*/
static updateKeymap(view: any, keyBindings: KeyBindingConfig[], enabledExtensions: ExtensionID[]): void {
const {keyBindings: cmKeyBindings} =
this.convertToKeyBindings(keyBindings, enabledExtensions)
this.convertToKeyBindings(keyBindings, enabledExtensions);
view.dispatch({
effects: this.compartment.reconfigure(keymap.of(cmKeyBindings))
})
});
}
/**
@@ -89,16 +89,16 @@ export class KeymapManager {
* @returns 按扩展分组的快捷键映射
*/
static groupByExtension(keyBindings: KeyBindingConfig[]): Map<ExtensionID, KeyBindingConfig[]> {
const groups = new Map<ExtensionID, KeyBindingConfig[]>()
const groups = new Map<ExtensionID, KeyBindingConfig[]>();
for (const binding of keyBindings) {
if (!groups.has(binding.extension)) {
groups.set(binding.extension, [])
groups.set(binding.extension, []);
}
groups.get(binding.extension)!.push(binding)
groups.get(binding.extension)!.push(binding);
}
return groups
return groups;
}
/**
@@ -110,17 +110,17 @@ export class KeymapManager {
valid: KeyBindingConfig[]
invalid: KeyBindingConfig[]
} {
const valid: KeyBindingConfig[] = []
const invalid: KeyBindingConfig[] = []
const valid: KeyBindingConfig[] = [];
const invalid: KeyBindingConfig[] = [];
for (const binding of keyBindings) {
if (binding.enabled && binding.key && isCommandRegistered(binding.command)) {
valid.push(binding)
valid.push(binding);
} else {
invalid.push(binding)
invalid.push(binding);
}
}
return {valid, invalid}
return {valid, invalid};
}
}

View File

@@ -1,4 +1,4 @@
import {Command} from '@codemirror/view'
import {Command} from '@codemirror/view';
/**
* CodeMirror快捷键绑定格式