From 2d16470982c5a2661129ddd1fb71dfd482d32265 Mon Sep 17 00:00:00 2001 From: Apoorv Mishra Date: Tue, 18 Nov 2025 16:37:20 +0530 Subject: [PATCH] fix: hover preview for image with link --- shared/editor/components/Image.tsx | 77 ++++++++++++++++++++++++------ 1 file changed, 62 insertions(+), 15 deletions(-) diff --git a/shared/editor/components/Image.tsx b/shared/editor/components/Image.tsx index 3da470e19b..8a8a74c1e0 100644 --- a/shared/editor/components/Image.tsx +++ b/shared/editor/components/Image.tsx @@ -11,6 +11,7 @@ import { ResizeLeft, ResizeRight } from "./ResizeHandle"; import useDragResize from "./hooks/useDragResize"; import { useTranslation } from "react-i18next"; import some from "lodash/some"; +import find from "lodash/find"; type Props = ComponentProps & { /** Callback triggered when the image is clicked */ @@ -71,6 +72,9 @@ const Image = (props: Props) => { const sanitizedSrc = sanitizeUrl(src); const hasLink = some(node.attrs.marks ?? [], (mark) => mark.type === "link"); + const imgLink = + find(node.attrs.marks ?? [], (mark) => mark.type === "link")?.attrs.href ?? + ""; const handleOpen = React.useCallback(() => { window.open(sanitizedSrc, "_blank"); @@ -135,14 +139,19 @@ const Image = (props: Props) => { Image failed to load - ) : ( - <> + ) : imgLink && !props.isSelected ? ( + { onClick={handleImageClick} onTouchStart={handleImageTouchStart} /> - {!loaded && width && height && ( - - )} - + + ) : ( + {node.attrs.alt { + setError(true); + setLoaded(true); + }} + onLoad={(ev: React.SyntheticEvent) => { + // 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 + const nw = (ev.target as HTMLImageElement).naturalWidth || 300; + const nh = (ev.target as HTMLImageElement).naturalHeight; + setNaturalWidth(nw); + setNaturalHeight(nh); + setLoaded(true); + + if (!node.attrs.width) { + setSize((state) => ({ + ...state, + width: nw, + })); + } + }} + onClick={handleImageClick} + onTouchStart={handleImageTouchStart} + /> + )} + {!loaded && width && height && ( + )} {isEditable && !isFullWidth && isResizable && ( <>