🎨 Optimize code

This commit is contained in:
2026-01-02 00:03:50 +08:00
parent 76f6c30b9d
commit 009274e4ad
14 changed files with 590 additions and 487 deletions

View File

@@ -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(); // 调用注入的回调
});
}
}

View File

@@ -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);
}
}
);

View File

@@ -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';
// 更新编辑器文档统计信息