0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-05-07 18:48:39 +00:00

store scroll positions

This commit is contained in:
Víctor Losada Hernández
2026-05-04 00:31:43 +02:00
parent 4edae9ef5a
commit 72465317f3
+15 -5
View File
@@ -148,12 +148,13 @@ const CodeEditor = forwardRef(
const editorRef = useRef(null); const editorRef = useRef(null);
const viewRef = useRef(null); const viewRef = useRef(null);
const docsRef = useRef({}); const docsRef = useRef({});
const tabRef = useRef(tab);
const prevTabRef = useRef(tab); const prevTabRef = useRef(tab);
const scrollRef = useRef({});
const pageMap = useRef([]); const pageMap = useRef([]);
const recomputePages = (doc)=>{ const recomputePages = (doc)=>{
if (tab !== 'brewText') return; if(tab !== 'brewText') return;
const pages = [0]; const pages = [0];
const text = doc.toString(); const text = doc.toString();
let offset = 0; let offset = 0;
@@ -265,11 +266,10 @@ const CodeEditor = forwardRef(
ticking = true; ticking = true;
requestAnimationFrame(()=>{ requestAnimationFrame(()=>{
const top = view.scrollDOM.scrollTop; const top = view.scrollDOM.scrollTop;
scrollRef.current[tabRef.current] = top;
const block = view.lineBlockAtHeight(top); const block = view.lineBlockAtHeight(top);
const page = findPageFromPos(block.from);
const page = findPageFromPos(block.from); // CHANGED
onViewChange(page); onViewChange(page);
ticking = false; ticking = false;
}); });
}; };
@@ -288,6 +288,7 @@ const CodeEditor = forwardRef(
const view = viewRef.current; const view = viewRef.current;
if(!view) return; if(!view) return;
tabRef.current = tab;
const prevTab = prevTabRef.current; const prevTab = prevTabRef.current;
if(prevTab !== tab) { if(prevTab !== tab) {
@@ -303,6 +304,15 @@ const CodeEditor = forwardRef(
} }
view.setState(nextState); view.setState(nextState);
const savedScroll = scrollRef.current[tab];
if(savedScroll != null) {
requestAnimationFrame(()=>{
view.scrollDOM.scrollTop = savedScroll;
});
}
prevTabRef.current = tab; prevTabRef.current = tab;
} }
view.focus(); view.focus();