Skip to content

Commit

Permalink
Fix inline images flickering (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skalakid authored Oct 16, 2024
1 parent 5a7985f commit e312fb8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/MarkdownTextInput.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type MarkdownTextInputElement = HTMLDivElement &
HTMLInputElement & {
tree: TreeNode;
selection: Selection;
imageElements: HTMLImageElement[];
};

type HTMLMarkdownElement = HTMLElement & {
Expand Down
10 changes: 8 additions & 2 deletions src/web/inputElements/inlineImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,17 @@ function handleOnLoad(
justifyContent: 'center',
}),
});

Object.assign(img.style, !err && imgStyle);

targetElement.appendChild(imageContainer);

const currentInputElement = currentInput;
if (currentInput.imageElements) {
currentInputElement.imageElements.push(img);
} else {
currentInputElement.imageElements = [img];
}

const imageClientHeight = Math.max(img.clientHeight, imageContainer.clientHeight);
Object.assign(imageContainer.style, {
height: `${imageClientHeight}px`,
Expand Down Expand Up @@ -164,7 +170,7 @@ function addInlineImagePreview(

// If the inline image markdown with the same href exists in the current input, use it instead of creating new one.
// Prevents from image flickering and layout jumps
const alreadyLoadedPreview = currentInput.querySelector(`img[src="${imageHref}"]`);
const alreadyLoadedPreview = currentInput.imageElements?.find((el) => el?.src === imageHref);
const loadedImageContainer = alreadyLoadedPreview?.parentElement;

if (loadedImageContainer && loadedImageContainer.getAttribute('data-type') === 'inline-container') {
Expand Down

0 comments on commit e312fb8

Please sign in to comment.