mirror of
https://github.com/priyanshujain/margin.git
synced 2026-10-03 03:27:03 +00:00
fix(editor): persist per-chapter cursor and scroll across restarts
Positions were kept in an in-memory Map, so they were lost on quit and the cursor jumped to the top on reopen. Store them in localStorage (keyed by book + chapter, like theme/width) and restore on app open, not just on in-session chapter switches. Also save the current chapter continuously (debounced on selection + scroll) so closing the app records where you left off, and re-apply scroll after fonts load.
This commit is contained in:
1 parent
39609fbd09
commit
8b83f80d52
3 files changed
+67
-22
No files matched your search
@@ -0,0 +1,27 @@
|
||||
export interface ChapterPosition {
|
||||
from: number;
|
||||
to: number;
|
||||
scroll: number;
|
||||
}
|
||||
|
||||
const KEY = "margin-positions";
|
||||
|
||||
type Store = Record<string, Record<string, ChapterPosition>>;
|
||||
|
||||
function readAll(): Store {
|
||||
try {
|
||||
return JSON.parse(localStorage.getItem(KEY) || "{}") as Store;
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
export function loadPosition(bookId: string, chapterId: string): ChapterPosition | null {
|
||||
return readAll()[bookId]?.[chapterId] ?? null;
|
||||
}
|
||||
|
||||
export function savePosition(bookId: string, chapterId: string, position: ChapterPosition): void {
|
||||
const all = readAll();
|
||||
(all[bookId] ||= {})[chapterId] = position;
|
||||
localStorage.setItem(KEY, JSON.stringify(all));
|
||||
}
|
||||
Reference in new issue
Block a user