-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat: persist scroll across tabs #7695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 11 commits
9e2d181
94a7995
c92c3cd
b39f4e1
1e1eee7
ef6c496
076f2a7
03c8a88
d664ce4
aa2fd02
cb9056d
3fcd5ee
a78a624
5902e14
e8cbda2
529486e
83d4b06
9f7bd93
36141b6
309a381
84959d3
097b843
e6c2864
0e9ccfe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,13 +2,14 @@ import 'github-markdown-css/github-markdown.css'; | |
| import get from 'lodash/get'; | ||
| import { updateFolderDocs } from 'providers/ReduxStore/slices/collections'; | ||
| import { useTheme } from 'providers/Theme'; | ||
| import { useState } from 'react'; | ||
| import { useRef, useState } from 'react'; | ||
| import { useDispatch, useSelector } from 'react-redux'; | ||
| import { saveFolderRoot } from 'providers/ReduxStore/slices/collections/actions'; | ||
| import Markdown from 'components/MarkDown'; | ||
| import CodeEditor from 'components/CodeEditor'; | ||
| import Button from 'ui/Button'; | ||
| import StyledWrapper from './StyledWrapper'; | ||
| import { usePersistedContainerScroll } from 'hooks/usePersistedState/usePersistedContainerScroll'; | ||
|
|
||
| const Documentation = ({ collection, folder }) => { | ||
| const dispatch = useDispatch(); | ||
|
|
@@ -17,6 +18,19 @@ const Documentation = ({ collection, folder }) => { | |
| const [isEditing, setIsEditing] = useState(false); | ||
| const docs = folder.draft ? get(folder, 'draft.docs', '') : get(folder, 'root.docs', ''); | ||
|
|
||
| // Scroll persistence for both edit (CodeMirror) and preview (Markdown) modes using one shared key. | ||
| // Preview mode: hook tracks .folder-settings-content scroll (enabled only when not editing). | ||
| // Edit mode: CodeEditor's onScroll/initialScroll props write/read the same localStorage key. | ||
| const wrapperRef = useRef(null); | ||
| const storageKey = usePersistedContainerScroll(wrapperRef, '.folder-settings-content', `folder-docs-scroll-${folder.uid}`, !isEditing); | ||
|
|
||
| const readScroll = () => { | ||
| try { | ||
| const raw = localStorage.getItem(storageKey); | ||
| return raw !== null ? JSON.parse(raw) || 0 : 0; | ||
| } catch { return 0; } | ||
| }; | ||
|
Comment on lines
+21
to
+32
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Folder docs preview has the same stale-scroll race. This path also reads from localStorage during the render that switches into edit mode, while the preview scroll hook persists its latest position in cleanup. If the user scrolls and immediately clicks Edit, the editor can reopen at the older saved offset. Also applies to: 72-73 🤖 Prompt for AI Agents |
||
|
|
||
| const toggleViewMode = () => { | ||
| setIsEditing((prev) => !prev); | ||
| }; | ||
|
|
@@ -38,7 +52,7 @@ const Documentation = ({ collection, folder }) => { | |
| } | ||
|
|
||
| return ( | ||
| <StyledWrapper className="w-full relative flex flex-col"> | ||
| <StyledWrapper className="w-full relative flex flex-col" ref={wrapperRef}> | ||
| <div className="editing-mode flex justify-between items-center flex-shrink-0" role="tab" onClick={toggleViewMode}> | ||
| {isEditing ? 'Preview' : 'Edit'} | ||
| </div> | ||
|
|
@@ -55,6 +69,8 @@ const Documentation = ({ collection, folder }) => { | |
| font={get(preferences, 'font.codeFont', 'default')} | ||
| fontSize={get(preferences, 'font.codeFontSize')} | ||
| mode="application/text" | ||
| initialScroll={readScroll()} | ||
| onScroll={(editor) => localStorage.setItem(storageKey, JSON.stringify(editor.doc.scrollTop))} | ||
| /> | ||
| </div> | ||
| <div className="mt-6 flex-shrink-0"> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preview → Edit can mount the editor with a stale scroll.
readScroll()runs during the render that mountsCodeEditor, butusePersistedContainerScroll()only flushes the latest previewscrollTopin its effect cleanup. On a quick scroll-then-toggle, that cleanup happens after this render, so edit mode restores the previous position instead of the one the user just left.Also applies to: 75-76
🤖 Prompt for AI Agents