✨ Added code collapse state persistence
This commit is contained in:
@@ -7,6 +7,16 @@ export interface DocumentStats {
|
||||
selectedCharacters: number;
|
||||
}
|
||||
|
||||
export interface FoldRange {
|
||||
// 字符偏移(备用)
|
||||
from: number;
|
||||
to: number;
|
||||
|
||||
// 行号
|
||||
fromLine: number;
|
||||
toLine: number;
|
||||
}
|
||||
|
||||
export const useEditorStateStore = defineStore('editorState', () => {
|
||||
// 光标位置存储 Record<docId, cursorPosition>
|
||||
const cursorPositions = ref<Record<number, number>>({});
|
||||
@@ -14,6 +24,9 @@ export const useEditorStateStore = defineStore('editorState', () => {
|
||||
// 文档统计数据存储 Record<docId, DocumentStats>
|
||||
const documentStats = ref<Record<number, DocumentStats>>({});
|
||||
|
||||
// 折叠状态存储 Record<docId, FoldRange[]>
|
||||
const foldStates = ref<Record<number, FoldRange[]>>({});
|
||||
|
||||
// 保存光标位置
|
||||
const saveCursorPosition = (docId: number, position: number) => {
|
||||
cursorPositions.value[docId] = position;
|
||||
@@ -38,25 +51,40 @@ export const useEditorStateStore = defineStore('editorState', () => {
|
||||
};
|
||||
};
|
||||
|
||||
// 保存折叠状态
|
||||
const saveFoldState = (docId: number, foldRanges: FoldRange[]) => {
|
||||
foldStates.value[docId] = foldRanges;
|
||||
};
|
||||
|
||||
// 获取折叠状态
|
||||
const getFoldState = (docId: number): FoldRange[] => {
|
||||
return foldStates.value[docId] || [];
|
||||
};
|
||||
|
||||
// 清除文档状态
|
||||
const clearDocumentState = (docId: number) => {
|
||||
delete cursorPositions.value[docId];
|
||||
delete documentStats.value[docId];
|
||||
delete foldStates.value[docId];
|
||||
};
|
||||
|
||||
// 清除所有状态
|
||||
const clearAllStates = () => {
|
||||
cursorPositions.value = {};
|
||||
documentStats.value = {};
|
||||
foldStates.value = {};
|
||||
};
|
||||
|
||||
return {
|
||||
cursorPositions,
|
||||
documentStats,
|
||||
foldStates,
|
||||
saveCursorPosition,
|
||||
getCursorPosition,
|
||||
saveDocumentStats,
|
||||
getDocumentStats,
|
||||
saveFoldState,
|
||||
getFoldState,
|
||||
clearDocumentState,
|
||||
clearAllStates
|
||||
};
|
||||
@@ -64,7 +92,7 @@ export const useEditorStateStore = defineStore('editorState', () => {
|
||||
persist: {
|
||||
key: 'voidraft-editor-state',
|
||||
storage: localStorage,
|
||||
pick: ['cursorPositions']
|
||||
pick: ['cursorPositions', 'foldStates']
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import {createStatsUpdateExtension} from '@/views/editor/basic/statsExtension';
|
||||
import {createContentChangePlugin} from '@/views/editor/basic/contentChangeExtension';
|
||||
import {createWheelZoomExtension} from '@/views/editor/basic/wheelZoomExtension';
|
||||
import {createCursorPositionExtension, scrollToCursor} from '@/views/editor/basic/cursorPositionExtension';
|
||||
import {createFoldStateExtension, restoreFoldState} from '@/views/editor/basic/foldStateExtension';
|
||||
import {createDynamicKeymapExtension, updateKeymapExtension} from '@/views/editor/keymap';
|
||||
import {
|
||||
createDynamicExtensions,
|
||||
@@ -118,6 +119,9 @@ export const useEditorStore = defineStore('editor', () => {
|
||||
// 光标位置持久化扩展
|
||||
const cursorPositionExtension = createCursorPositionExtension(docId);
|
||||
|
||||
// 折叠状态持久化扩展
|
||||
const foldStateExtension = createFoldStateExtension(docId);
|
||||
|
||||
// 快捷键扩展
|
||||
const keymapExtension = await createDynamicKeymapExtension();
|
||||
|
||||
@@ -136,6 +140,7 @@ export const useEditorStore = defineStore('editor', () => {
|
||||
contentChangeExtension,
|
||||
codeBlockExtension,
|
||||
cursorPositionExtension,
|
||||
foldStateExtension,
|
||||
...dynamicExtensions,
|
||||
];
|
||||
|
||||
@@ -227,6 +232,12 @@ export const useEditorStore = defineStore('editor', () => {
|
||||
requestAnimationFrame(() => {
|
||||
scrollToCursor(instance.view);
|
||||
instance.view.focus();
|
||||
|
||||
// 恢复折叠状态
|
||||
const savedFoldState = editorStateStore.getFoldState(instance.documentId);
|
||||
if (savedFoldState.length > 0) {
|
||||
restoreFoldState(instance.view, savedFoldState);
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error showing editor:', error);
|
||||
|
||||
Reference in New Issue
Block a user