Repository navigation
fix: #4151 preserve scroll position on external file change - #4152
Conversation
…ll position / XSS / contextIsolation - fix: 编辑菜单截图分割线仅 macOS 可见(Windows 不再出现双分割线) - fix(marktext#4093): 深色主题打开窗口预设背景色,消除白闪 - feat(marktext#4134): capitalizeAccelerator - 自定义快捷键修饰符大写修复 - fix(marktext#4146): 搜索打开文件后首次 muya 序列化不触发未保存状态 - fix(marktext#4075): 外部文件变更:已保存时静默加载,未保存时才提示 - fix(marktext#4152): 切换 Tab 保留滚动位置;打开文件光标定位到开头 - fix(marktext#4206): 图片属性 HTML 转义防 XSS/RCE - fix(marktext#4192): contextIsolation=true 防渲染进程调用 Node.js API
|
This project recently merged a large refactor (electron-vite migration, PR #4001) that upgraded dependencies and fixed several issues. Please review this PR and let us know if it is still needed, or if the changes have already been addressed upstream.
|
- store/editor.js#loadChange: back up tab.scrollTop alongside the other entries so Object.assign(tab, newFileState) no longer wipes it. - sourceCode.vue#handleFileChange: skip prepareTabSwitch on same-tab reload so tabId stays valid for consecutive reloads, and restore scroll on the same tick, nextTick, and next animation frame.
a90348e to
1cce719
Compare
Thanks @Jocs! #4001 partially addresses this (tab switching, default cursor on open), Verified: WYSIWYG reload, source-code reload, consecutive source-code |
There was a problem hiding this comment.
Pull request overview
This PR aims to preserve the scroll position when a file open in MarkText is modified externally and reloaded, fixing issue #4151. The change backs up tab.scrollTop before reloading the document state and restores it after, so the emitted file-changed event carries the previous scroll offset. The sourceCode (CodeMirror) view adds a same-tab reload code path that snapshots the scroll offset of every plausible scrollable element and re-applies it after setValue, scheduled across the current tick, next tick, and next animation frame to survive layout side-effects.
Changes:
- Back up and restore
tab.scrollTopacross theloadChangedocument recreation in the editor store. - Branch
handleFileChangeinsourceCode.vueinto a same-tab reload path that preserves and restores scroll on multiple scrollable ancestors instead of running tab-switch commit logic. - Import
nextTickto support multi-phase scroll restoration.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/renderer/src/store/editor.js | Snapshot tab.scrollTop before Object.assign(tab, newFileState) and reassign it after, so the emitted file-changed event keeps the previous scroll offset. |
| src/renderer/src/components/editorWithTabs/sourceCode.vue | Detect same-tab reloads, snapshot scroll offsets of the container, CodeMirror scroller, and nearest scrollable ancestor, and restore them across nextTick + RAF after setValue. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -229,6 +231,7 @@ export const useEditorStore = defineStore('editor', { | |||
| Object.assign(tab, newFileState) | |||
| tab.id = oldId | |||
| tab.notifications = oldNotifications | |||
| tab.scrollTop = oldScrollTop | |||
|
@Wordbe Thanks for your contribution! |
…arktext#4152) - store/editor.js#loadChange: back up tab.scrollTop alongside the other entries so Object.assign(tab, newFileState) no longer wipes it. - sourceCode.vue#handleFileChange: skip prepareTabSwitch on same-tab reload so tabId stays valid for consecutive reloads, and restore scroll on the same tick, nextTick, and next animation frame.
Summary
Fixes #4151
External file changes were reloading the document and resetting scroll.
This preserves scroll in WYSIWYG and source-code modes.
Background
#4001 (electron-vite migration) already added scroll-preserve plumbing
on the file-changed event. Two follow-on bugs still lost scroll on
external reload:
tab.scrollTopwhile reloading, so theWYSIWYG handler always received 0.
tab-switch commit on same-tab reload which broke the same-tab check
after the first reload.
Fix
tab.scrollTopacross the documentreload. The WYSIWYG handler then restores scroll on its own.
snapshot scroll before
setValue, restore on the same tick, nextTick,and next animation frame to survive layout side-effects.
Test plan