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 image preview styling #493

Merged
merged 3 commits into from
Sep 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
3 changes: 3 additions & 0 deletions src/MarkdownTextInputDecoratorViewNativeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ interface MarkdownStyle {
backgroundColor: ColorValue;
};
inlineImage: {
minWidth: Float;
minHeight: Float;
maxWidth: Float;
maxHeight: Float;
marginTop: Float;
marginBottom: Float;
borderRadius: Float;
};
loadingIndicatorContainer?: {
backgroundColor?: ColorValue;
Expand Down
3 changes: 3 additions & 0 deletions src/styleUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,13 @@ function makeDefaultMarkdownStyle(): MarkdownStyle {
backgroundColor: 'pink',
},
inlineImage: {
minWidth: 50,
minHeight: 50,
maxWidth: 150,
maxHeight: 150,
marginTop: 5,
marginBottom: 0,
borderRadius: 5,
},
loadingIndicator: {
primaryColor: 'gray',
Expand Down
28 changes: 14 additions & 14 deletions src/web/inputElements/inlineImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ function createImageElement(url: string, callback: (img: HTMLElement) => void) {
}

/** Adds already loaded image element from current input content to the tree node */
function updateImageTreeNode(targetNode: TreeNode, newElement: HTMLMarkdownElement) {
const paddingBottom = `${newElement.style.height}`;
function updateImageTreeNode(targetNode: TreeNode, newElement: HTMLMarkdownElement, imageMarginTop = 0) {
const paddingBottom = `${parseStringWithUnitToNumber(newElement.style.height) + imageMarginTop}px`;
targetNode.element.appendChild(newElement);

let currentParent = targetNode.element;
Expand All @@ -49,12 +49,15 @@ function addInlineImagePreview(currentInput: MarkdownTextInputElement, targetNod
imageHref = text.substring(linkRange.start, linkRange.start + linkRange.length);
}

const imageMarginTop = parseStringWithUnitToNumber(`${markdownStyle.inlineImage?.marginTop}`);
const imageMarginBottom = parseStringWithUnitToNumber(`${markdownStyle.inlineImage?.marginBottom}`);

// 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 loadedImageContainer = alreadyLoadedPreview?.parentElement;
if (loadedImageContainer && loadedImageContainer.getAttribute('data-type') === 'inline-container') {
return updateImageTreeNode(targetNode, loadedImageContainer as HTMLMarkdownElement);
return updateImageTreeNode(targetNode, loadedImageContainer as HTMLMarkdownElement, imageMarginTop);
}

// Add a loading spinner
Expand All @@ -63,11 +66,6 @@ function addInlineImagePreview(currentInput: MarkdownTextInputElement, targetNod
targetNode.element.appendChild(spinner);
}

const maxWidth = markdownStyle.inlineImage?.maxWidth;
const maxHeight = markdownStyle.inlineImage?.maxHeight;
const imageMarginTop = parseStringWithUnitToNumber(`${markdownStyle.inlineImage?.marginTop}`);
const imageMarginBottom = parseStringWithUnitToNumber(`${markdownStyle.inlineImage?.marginBottom}`);

Object.assign(targetNode.element.style, {
display: 'block',
marginBottom: `${imageMarginBottom}px`,
Expand All @@ -82,28 +80,30 @@ function addInlineImagePreview(currentInput: MarkdownTextInputElement, targetNod
currentSpinner.remove();
}

const img = imageContainer.firstChild as HTMLImageElement;

// Set the image styles
Object.assign(imageContainer.style, {
...inlineImageDefaultStyles,
maxHeight,
maxWidth,
});

const img = imageContainer.firstChild as HTMLImageElement;
const {minHeight, minWidth, maxHeight, maxWidth, borderRadius} = markdownStyle.inlineImage || {};
Object.assign(img.style, {
minHeight,
minWidth,
maxHeight,
maxWidth,
borderRadius,
});

targetNode.element.appendChild(imageContainer);

const imageHeight = `${imageContainer.clientHeight + imageMarginTop}px`;
Object.assign(imageContainer.style, {
height: imageHeight,
height: `${imageContainer.clientHeight}px`,
});
// Set paddingBottom to the height of the image so it's displayed under the block
Object.assign(targetNode.element.style, {
paddingBottom: imageHeight,
paddingBottom: `${imageContainer.clientHeight + imageMarginTop}px`,
});
});

Expand Down
Loading