🎨 Optimize code
This commit is contained in:
@@ -32,9 +32,9 @@ onMounted(async () => {
|
||||
|
||||
editorStore.setEditorContainer(editorElement.value);
|
||||
|
||||
const currentDoc = documentStore.currentDocument;
|
||||
if (currentDoc && currentDoc.id !== undefined) {
|
||||
await editorStore.loadEditor(currentDoc.id, currentDoc.content || '');
|
||||
const currentDocId = documentStore.currentDocumentId;
|
||||
if (currentDocId) {
|
||||
await editorStore.switchToEditor(currentDocId);
|
||||
}
|
||||
|
||||
await tabStore.initTab();
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import {EditorView, ViewPlugin, ViewUpdate} from '@codemirror/view';
|
||||
import type {Text} from '@codemirror/state';
|
||||
import {useEditorStore} from '@/stores/editorStore';
|
||||
|
||||
/**
|
||||
* 内容变化监听扩展
|
||||
* 通过回调函数解耦,不直接依赖 Store
|
||||
*/
|
||||
export function createContentChangePlugin() {
|
||||
export function createContentChangePlugin(onContentChange: () => void) {
|
||||
return ViewPlugin.fromClass(
|
||||
class ContentChangePlugin {
|
||||
private readonly editorStore = useEditorStore();
|
||||
private lastDoc: Text;
|
||||
private rafId: number | null = null;
|
||||
private pendingNotification = false;
|
||||
@@ -40,7 +40,7 @@ export function createContentChangePlugin() {
|
||||
this.rafId = requestAnimationFrame(() => {
|
||||
this.pendingNotification = false;
|
||||
this.rafId = null;
|
||||
this.editorStore.onContentChange();
|
||||
onContentChange(); // 调用注入的回调
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
import {EditorView, ViewPlugin, ViewUpdate} from '@codemirror/view';
|
||||
import {useDocumentStore} from '@/stores/documentStore';
|
||||
import {useEditorStateStore} from '@/stores/editorStateStore';
|
||||
import {createDebounce} from '@/common/utils/debounce';
|
||||
|
||||
/**
|
||||
* 光标位置持久化扩展
|
||||
* 实时监听光标位置变化并持久化到 documentStore
|
||||
* 实时监听光标位置变化并持久化到 editorStateStore
|
||||
*/
|
||||
export function createCursorPositionExtension(documentId: number) {
|
||||
return ViewPlugin.fromClass(
|
||||
class CursorPositionPlugin {
|
||||
private readonly documentStore = useDocumentStore();
|
||||
private readonly editorStateStore = useEditorStateStore();
|
||||
private readonly debouncedSave;
|
||||
|
||||
constructor(private view: EditorView) {
|
||||
@@ -42,11 +42,7 @@ export function createCursorPositionExtension(documentId: number) {
|
||||
|
||||
private saveCursorPosition() {
|
||||
const cursorPos = this.view.state.selection.main.head;
|
||||
if (!this.documentStore.documentStates[documentId]) {
|
||||
this.documentStore.documentStates[documentId] = {cursorPos};
|
||||
} else {
|
||||
this.documentStore.documentStates[documentId].cursorPos = cursorPos;
|
||||
}
|
||||
this.editorStateStore.saveCursorPosition(documentId, cursorPos);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {Extension} from '@codemirror/state';
|
||||
import {EditorView} from '@codemirror/view';
|
||||
import {DocumentStats} from '@/stores/editorStore';
|
||||
import {DocumentStats} from '@/stores/editorStateStore';
|
||||
import {getActiveNoteBlock} from '@/views/editor/extensions/codeblock/state';
|
||||
|
||||
// 更新编辑器文档统计信息
|
||||
|
||||
@@ -3,6 +3,7 @@ import {computed, onMounted, ref} from 'vue';
|
||||
import {useI18n} from 'vue-i18n';
|
||||
import {useEditorStore} from '@/stores/editorStore';
|
||||
import {useExtensionStore} from '@/stores/extensionStore';
|
||||
import {useKeybindingStore} from '@/stores/keybindingStore';
|
||||
import {ExtensionService} from '@/../bindings/voidraft/internal/services';
|
||||
import {
|
||||
getExtensionDefaultConfig,
|
||||
@@ -10,6 +11,7 @@ import {
|
||||
getExtensionDisplayName, getExtensionsMap,
|
||||
hasExtensionConfig
|
||||
} from '@/views/editor/manager/extensions';
|
||||
import {getExtensionManager} from '@/views/editor/manager';
|
||||
import SettingSection from '../components/SettingSection.vue';
|
||||
import SettingItem from '../components/SettingItem.vue';
|
||||
import ToggleSwitch from '../components/ToggleSwitch.vue';
|
||||
@@ -17,6 +19,7 @@ import ToggleSwitch from '../components/ToggleSwitch.vue';
|
||||
const {t} = useI18n();
|
||||
const editorStore = useEditorStore();
|
||||
const extensionStore = useExtensionStore();
|
||||
const keybindingStore = useKeybindingStore();
|
||||
|
||||
// 页面初始化时加载扩展数据
|
||||
onMounted(async () => {
|
||||
@@ -55,7 +58,25 @@ const toggleExpanded = (extensionId: number) => {
|
||||
// 更新扩展状态
|
||||
const updateExtension = async (extensionId: number, enabled: boolean) => {
|
||||
try {
|
||||
await editorStore.updateExtension(extensionId, enabled);
|
||||
// 更新后端
|
||||
await ExtensionService.UpdateExtensionEnabled(extensionId, enabled);
|
||||
|
||||
// 重新加载各个 Store 的状态
|
||||
await extensionStore.loadExtensions();
|
||||
await keybindingStore.loadKeyBindings();
|
||||
|
||||
// 获取更新后的扩展
|
||||
const extension = extensionStore.extensions.find(ext => ext.id === extensionId);
|
||||
if (!extension) return;
|
||||
|
||||
// 应用到编辑器
|
||||
const manager = getExtensionManager();
|
||||
if (manager) {
|
||||
manager.updateExtension(extension.name, enabled, extension.config);
|
||||
}
|
||||
|
||||
// 更新快捷键
|
||||
await editorStore.applyKeymapSettings();
|
||||
} catch (error) {
|
||||
console.error('Failed to update extension:', error);
|
||||
}
|
||||
@@ -75,9 +96,18 @@ const updateExtensionConfig = async (extensionId: number, configKey: string, val
|
||||
} else {
|
||||
updatedConfig[configKey] = value;
|
||||
}
|
||||
// 使用editorStore的updateExtension方法更新,确保应用到所有编辑器实例
|
||||
await editorStore.updateExtension(extensionId, extension.enabled ?? false, updatedConfig);
|
||||
|
||||
|
||||
// 更新后端配置
|
||||
await ExtensionService.UpdateExtensionConfig(extensionId, updatedConfig);
|
||||
|
||||
// 重新加载状态
|
||||
await extensionStore.loadExtensions();
|
||||
|
||||
// 应用到编辑器
|
||||
const manager = getExtensionManager();
|
||||
if (manager) {
|
||||
manager.updateExtension(extension.name, extension.enabled ?? false, updatedConfig);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to update extension config:', error);
|
||||
}
|
||||
@@ -89,14 +119,16 @@ const resetExtension = async (extensionId: number) => {
|
||||
// 重置到默认配置
|
||||
await ExtensionService.ResetExtensionConfig(extensionId);
|
||||
|
||||
// 重新加载扩展状态以获取最新配置
|
||||
// 重新加载扩展状态
|
||||
await extensionStore.loadExtensions();
|
||||
|
||||
// 获取重置后的状态,立即应用到所有编辑器视图
|
||||
// 获取重置后的状态,应用到编辑器
|
||||
const extension = extensionStore.extensions.find(ext => ext.id === extensionId);
|
||||
if (extension) {
|
||||
// 通过editorStore更新,确保所有视图都能同步
|
||||
await editorStore.updateExtension(extensionId, extension.enabled ?? false, extension.config);
|
||||
const manager = getExtensionManager();
|
||||
if (manager) {
|
||||
manager.updateExtension(extension.name, extension.enabled ?? false, extension.config);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to reset extension:', error);
|
||||
|
||||
Reference in New Issue
Block a user