Skip to content

Commit 6fd3128

Browse files
committed
fix(mdviewer): use morphdom for incremental DOM updates to prevent scroll jump
Replace full innerHTML replacement with morphdom diffing so unchanged elements (images, code blocks) stay in the DOM during CM→preview sync. Prevents image reloads and layout shifts when editing text.
1 parent 41a09c3 commit 6fd3128

3 files changed

Lines changed: 13 additions & 8 deletions

File tree

src-mdviewer/package-lock.json

Lines changed: 8 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-mdviewer/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"medium-zoom": "^1.1.0",
2121
"mermaid": "^11.12.2",
2222
"mitt": "^3.0.1",
23+
"morphdom": "^2.7.8",
2324
"prismjs": "^1.29.0",
2425
"turndown": "^7.2.0",
2526
"turndown-plugin-gfm": "^1.0.2"

src-mdviewer/src/components/viewer.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Prism.manual = true;
44
import "../core/prism-languages.js";
55
import "../styles/syntax/prism-light.css";
66
import "../styles/syntax/prism-dark.css";
7+
import morphdom from "morphdom";
78
import { on } from "../core/events.js";
89
import { getState } from "../core/state.js";
910
import { t } from "../core/i18n.js";
@@ -56,7 +57,9 @@ export function initViewer() {
5657
const content = getContentEl();
5758
if (!content) return;
5859

59-
content.innerHTML = parseResult.html;
60+
const newContent = document.createElement("div");
61+
newContent.innerHTML = parseResult.html;
62+
morphdom(content, newContent, { childrenOnly: true });
6063
content.dir = "auto";
6164

6265
wrapTables();

0 commit comments

Comments
 (0)