🐛 Fixed assignment issues
This commit is contained in:
@@ -7,48 +7,49 @@ import {createDebounce} from '@/common/utils/debounce';
|
|||||||
* 实时监听光标位置变化并持久化到 documentStore
|
* 实时监听光标位置变化并持久化到 documentStore
|
||||||
*/
|
*/
|
||||||
export function createCursorPositionExtension(documentId: number) {
|
export function createCursorPositionExtension(documentId: number) {
|
||||||
return ViewPlugin.fromClass(
|
return ViewPlugin.fromClass(
|
||||||
class CursorPositionPlugin {
|
class CursorPositionPlugin {
|
||||||
private readonly documentStore = useDocumentStore();
|
private readonly documentStore = useDocumentStore();
|
||||||
private readonly debouncedSave;
|
private readonly debouncedSave;
|
||||||
|
|
||||||
constructor(private view: EditorView) {
|
constructor(private view: EditorView) {
|
||||||
const { debouncedFn, flush } = createDebounce(
|
const {debouncedFn, flush} = createDebounce(
|
||||||
() => this.saveCursorPosition(),
|
() => this.saveCursorPosition(),
|
||||||
{ delay: 400 }
|
{delay: 400}
|
||||||
);
|
);
|
||||||
this.debouncedSave = { fn: debouncedFn, flush };
|
this.debouncedSave = {fn: debouncedFn, flush};
|
||||||
|
|
||||||
// 初始化时保存一次光标位置
|
// 初始化时保存一次光标位置
|
||||||
this.saveCursorPosition();
|
this.saveCursorPosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
update(update: ViewUpdate) {
|
update(update: ViewUpdate) {
|
||||||
// 只在选择变化时触发
|
// 只在选择变化时触发
|
||||||
if (!update.selectionSet) {
|
if (!update.selectionSet) {
|
||||||
return;
|
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 编辑器视图
|
* @param view 编辑器视图
|
||||||
*/
|
*/
|
||||||
export function scrollToCursor(view: EditorView) {
|
export function scrollToCursor(view: EditorView) {
|
||||||
const cursorPos = view.state.selection.main.head;
|
const cursorPos = view.state.selection.main.head;
|
||||||
view.dispatch({
|
view.dispatch({
|
||||||
effects: EditorView.scrollIntoView(cursorPos, {
|
effects: EditorView.scrollIntoView(cursorPos, {
|
||||||
y: 'center',
|
y: 'center',
|
||||||
x: 'center'
|
x: 'center'
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user