Skip to content

Commit 09e5dcf

Browse files
committed
fix(mdviewer): detach images before morphdom to prevent GIF blink
Replace images with placeholder spans before morphdom runs so the diff engine never touches them. After morphdom inserts new img nodes, swap the saved originals back in. Prevents GIF restart and image flicker.
1 parent e73e0ee commit 09e5dcf

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

src-mdviewer/src/components/viewer.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,30 @@ export function initViewer() {
5757
const content = getContentEl();
5858
if (!content) return;
5959

60-
// Save existing image nodes keyed by src before DOM replacement.
61-
// After innerHTML update, find matching <img> by src and swap the
62-
// new node with the saved one so the browser doesn't re-fetch.
60+
// Save existing image nodes before DOM update. Replace each <img> with
61+
// a lightweight placeholder <span> so morphdom doesn't destroy the node.
62+
// After morphdom, swap saved images back in by matching src.
6363
const savedImgs = new Map();
64+
const placeholders = new Map();
6465
content.querySelectorAll("img").forEach(img => {
6566
if (img.src && !img.src.includes("uploading.svg")) {
67+
const placeholder = document.createElement("span");
68+
placeholder.dataset.savedImgSrc = img.src;
69+
img.replaceWith(placeholder);
6670
savedImgs.set(img.src, img);
71+
placeholders.set(img.src, placeholder);
6772
}
6873
});
6974

70-
content.innerHTML = parseResult.html;
75+
const newContent = document.createElement("div");
76+
newContent.innerHTML = parseResult.html;
77+
morphdom(content, newContent, { childrenOnly: true });
7178

72-
// Restore saved image nodes to prevent reload
79+
// Restore saved image nodes — find new <img> by src and swap
7380
if (savedImgs.size > 0) {
7481
content.querySelectorAll("img").forEach(newImg => {
7582
const saved = savedImgs.get(newImg.src);
7683
if (saved) {
77-
// Copy over any changed attributes from the new element
7884
if (saved.alt !== newImg.alt) { saved.alt = newImg.alt; }
7985
if (saved.title !== newImg.title) { saved.title = newImg.title; }
8086
newImg.replaceWith(saved);

0 commit comments

Comments
 (0)