Added code collapse state persistence

This commit is contained in:
2026-01-03 23:06:08 +08:00
parent aae86d8b4e
commit 532d30aa93
8 changed files with 222 additions and 45 deletions

View File

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