Skip to content

fix: #4151 preserve scroll position on external file change - #4152

Merged
Jocs merged 1 commit into
marktext:developfrom
Wordbe:fix/preserve-scroll-on-external-change
May 18, 2026
Merged

Jocs merged 1 commit into
marktext:developfrom
Wordbe:fix/preserve-scroll-on-external-change

Conversation

@Wordbe

@Wordbe Wordbe commented Mar 22, 2026 •

Copy link
Copy Markdown
Contributor

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:

  1. The editor store overwrote tab.scrollTop while reloading, so the
    WYSIWYG handler always received 0.
  2. Source-code mode had no scroll preservation, and was running the
    tab-switch commit on same-tab reload which broke the same-tab check
    after the first reload.

Fix

  • Editor store: back up and restore tab.scrollTop across the document
    reload. The WYSIWYG handler then restores scroll on its own.
  • Source-code component: skip the tab-switch commit on same-tab reload,
    snapshot scroll before setValue, restore on the same tick, nextTick,
    and next animation frame to survive layout side-effects.

Test plan

  • WYSIWYG external reload, scroll preserved
  • Source-code external reload, scroll preserved
  • Source-code consecutive reloads, preserved every time
  • WYSIWYG regression, no change

UCHIHAHA103 pushed a commit to UCHIHAHA103/marktext that referenced this pull request May 14, 2026
…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
@Jocs

Jocs commented May 15, 2026

Copy link
Copy Markdown
Member

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.

Note: This PR currently has merge conflicts with the develop branch. If you decide to continue with this PR, please rebase or merge the latest develop branch to resolve the conflicts before it can be reviewed.

- 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.
@Wordbe
Wordbe force-pushed the fix/preserve-scroll-on-external-change branch from a90348e to 1cce719 Compare May 16, 2026 07:26
@Wordbe

Wordbe commented May 16, 2026

Copy link
Copy Markdown
Contributor Author

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.

Note: This PR currently has merge conflicts with the develop branch. If you decide to continue with this PR, please rebase or merge the latest develop branch to resolve the conflicts before it can be reviewed.

Thanks @Jocs!

#4001 partially addresses this (tab switching, default cursor on open),
but external file reload still loses scroll position. The relevant code
paths changed entirely with the electron-vite migration, so I rebased
onto develop and rewrote the fix. Force-pushed.

Verified: WYSIWYG reload, source-code reload, consecutive source-code
reloads, and WYSIWYG regression. All preserve scroll.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.scrollTop across the loadChange document recreation in the editor store.
  • Branch handleFileChange in sourceCode.vue into a same-tab reload path that preserves and restores scroll on multiple scrollable ancestors instead of running tab-switch commit logic.
  • Import nextTick to 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.

Comment on lines 214 to +234
@@ -229,6 +231,7 @@ export const useEditorStore = defineStore('editor', {
Object.assign(tab, newFileState)
tab.id = oldId
tab.notifications = oldNotifications
tab.scrollTop = oldScrollTop
@Jocs

Jocs commented May 18, 2026

Copy link
Copy Markdown
Member

@Wordbe Thanks for your contribution!

@Jocs
Jocs merged commit 818a2fc into marktext:develop May 18, 2026
13 checks passed
thimbleberrysystems pushed a commit to thimbleberrysystems/WordBird that referenced this pull request Jun 21, 2026
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Scroll position resets to end of document on external file change

3 participants