From 5efeb90fdda3aa972a84bc99ac043084853fffe8 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 9 Mar 2022 20:07:01 -0800 Subject: [PATCH] fix: SVGs without a natural px width are invisible (#3220) --- shared/editor/nodes/Image.tsx | 90 ++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 32 deletions(-) diff --git a/shared/editor/nodes/Image.tsx b/shared/editor/nodes/Image.tsx index 1c5618fe9..73ca580a0 100644 --- a/shared/editor/nodes/Image.tsx +++ b/shared/editor/nodes/Image.tsx @@ -295,35 +295,12 @@ export default class Image extends Node { }; component = (props: ComponentProps) => { - const { theme, isSelected } = props; - const { alt, src, layoutClass } = props.node.attrs; - const className = layoutClass ? `image image-${layoutClass}` : "image"; - return ( -
- - - - + - {alt} + {props.node.attrs.alt} -
+ ); }; @@ -520,6 +497,53 @@ export default class Image extends Node { } } +const ImageComponent = ( + props: ComponentProps & { + onClick: (event: React.MouseEvent) => void; + onDownload: (event: React.MouseEvent) => void; + children: React.ReactNode; + } +) => { + const { theme, isSelected, node } = props; + const { alt, src, layoutClass } = node.attrs; + const className = layoutClass ? `image image-${layoutClass}` : "image"; + const [width, setWidth] = React.useState(0); + + return ( +
+ + + { + // For some SVG's Firefox does not provide the naturalWidth, in this + // rare case we need to provide a default so that the image can be + // seen and is not sized to 0px + setWidth(ev.target.naturalWidth || "50%"); + }, + }} + defaultStyles={{ + overlay: { + backgroundColor: theme.background, + }, + }} + shouldRespectMaxDimension + /> + + {props.children} +
+ ); +}; + const Button = styled.button` position: absolute; top: 8px; @@ -576,10 +600,12 @@ const Caption = styled.p` } `; -const ImageWrapper = styled.span` +const ImageWrapper = styled.div` line-height: 0; - display: inline-block; position: relative; + margin-left: auto; + margin-right: auto; + max-width: 100%; &:hover { ${Button} {