diff --git a/frontend/src/views/editor/basic/cursorPositionExtension.ts b/frontend/src/views/editor/basic/cursorPositionExtension.ts index 9592253..4423e3b 100644 --- a/frontend/src/views/editor/basic/cursorPositionExtension.ts +++ b/frontend/src/views/editor/basic/cursorPositionExtension.ts @@ -7,48 +7,49 @@ import {createDebounce} from '@/common/utils/debounce'; * 实时监听光标位置变化并持久化到 documentStore */ export function createCursorPositionExtension(documentId: number) { - return ViewPlugin.fromClass( - class CursorPositionPlugin { - private readonly documentStore = useDocumentStore(); - private readonly debouncedSave; + return ViewPlugin.fromClass( + class CursorPositionPlugin { + private readonly documentStore = useDocumentStore(); + private readonly debouncedSave; - constructor(private view: EditorView) { - const { debouncedFn, flush } = createDebounce( - () => this.saveCursorPosition(), - { delay: 400 } - ); - this.debouncedSave = { fn: debouncedFn, flush }; + constructor(private view: EditorView) { + const {debouncedFn, flush} = createDebounce( + () => this.saveCursorPosition(), + {delay: 400} + ); + this.debouncedSave = {fn: debouncedFn, flush}; - // 初始化时保存一次光标位置 - this.saveCursorPosition(); - } + // 初始化时保存一次光标位置 + this.saveCursorPosition(); + } - update(update: ViewUpdate) { - // 只在选择变化时触发 - if (!update.selectionSet) { - return; + update(update: ViewUpdate) { + // 只在选择变化时触发 + if (!update.selectionSet) { + return; + } + + // 防抖保存光标位置 + this.debouncedSave.fn(); + } + + destroy() { + // 销毁时立即执行待保存的操作 + this.debouncedSave.flush(); + // 再保存一次确保最新状态 + this.saveCursorPosition(); + } + + 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.debouncedSave.fn(); - } - - destroy() { - // 销毁时立即执行待保存的操作 - this.debouncedSave.flush(); - // 再保存一次确保最新状态 - this.saveCursorPosition(); - } - - private saveCursorPosition() { - const cursorPos = this.view.state.selection.main.head; - // 持久化到 documentStore - this.documentStore.documentStates[documentId] = { - cursorPos - }; - } - } - ); + ); } /** @@ -56,12 +57,12 @@ export function createCursorPositionExtension(documentId: number) { * @param view 编辑器视图 */ export function scrollToCursor(view: EditorView) { - const cursorPos = view.state.selection.main.head; - view.dispatch({ - effects: EditorView.scrollIntoView(cursorPos, { - y: 'center', - x: 'center' - }) - }); + const cursorPos = view.state.selection.main.head; + view.dispatch({ + effects: EditorView.scrollIntoView(cursorPos, { + y: 'center', + x: 'center' + }) + }); }