Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inline images flickering #516

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading