diff --git a/shared/editor/components/Image.tsx b/shared/editor/components/Image.tsx index d8306ba1e5..b8ae529d4d 100644 --- a/shared/editor/components/Image.tsx +++ b/shared/editor/components/Image.tsx @@ -21,10 +21,12 @@ type Props = ComponentProps & { /** The editor view */ view: EditorView; children?: React.ReactElement; + isDownloading?: boolean; }; const Image = (props: Props) => { - const { isSelected, node, isEditable, onChangeSize, onClick } = props; + const { isSelected, node, isEditable, onChangeSize, onClick, isDownloading } = + props; const { src, layoutClass } = node.attrs; const { t } = useTranslation(); const className = layoutClass ? `image image-${layoutClass}` : "image"; @@ -102,7 +104,11 @@ const Image = (props: Props) => { )} - @@ -224,7 +230,7 @@ const Button = styled.button` width: 24px; height: 24px; display: inline-block; - cursor: var(--pointer) !important; + cursor: var(--pointer); transition: opacity 150ms ease-in-out; &:first-child:not(:last-child) { @@ -244,6 +250,20 @@ const Button = styled.button` &:hover { color: ${s("text")}; } + + &:disabled { + opacity: 0.5; + cursor: wait; + pointer-events: none; + + &:hover { + color: ${s("textSecondary")}; + } + + &:active { + transform: none; + } + } `; const ImageWrapper = styled.div<{ isFullWidth: boolean }>` diff --git a/shared/editor/nodes/Image.tsx b/shared/editor/nodes/Image.tsx index 451f92dcbc..f02e0611b0 100644 --- a/shared/editor/nodes/Image.tsx +++ b/shared/editor/nodes/Image.tsx @@ -55,22 +55,35 @@ const parseTitleAttribute = (tokenTitle: string): TitleAttributes => { return attributes; }; -export const downloadImageNode = async (node: ProsemirrorNode) => { - const image = await fetch(node.attrs.src); - const imageBlob = await image.blob(); - const imageURL = URL.createObjectURL(imageBlob); - const extension = imageBlob.type.split(/\/|\+/g)[1]; - const potentialName = node.attrs.alt || "image"; +export const downloadImageNode = async ( + node: ProsemirrorNode, + cache?: RequestCache +) => { + try { + const image = await fetch(node.attrs.src, { + cache, + }); + const imageBlob = await image.blob(); + const imageURL = URL.createObjectURL(imageBlob); + const extension = imageBlob.type.split(/\/|\+/g)[1]; + const potentialName = node.attrs.alt || "image"; - // create a temporary link node and click it with our image data - const link = document.createElement("a"); - link.href = imageURL; - link.download = `${potentialName}.${extension}`; - document.body.appendChild(link); - link.click(); + // create a temporary link node and click it with our image data + const link = document.createElement("a"); + link.href = imageURL; + link.download = `${potentialName}.${extension}`; + document.body.appendChild(link); + link.click(); - // cleanup - document.body.removeChild(link); + // cleanup + document.body.removeChild(link); + } catch { + if (cache !== "reload") { + downloadImageNode(node, "reload"); + } else { + window.open(sanitizeUrl(node.attrs.src), "_blank"); + } + } }; export default class Image extends SimpleImage { @@ -255,14 +268,6 @@ export default class Image extends SimpleImage { }); }; - handleDownload = - ({ node }: ComponentProps) => - (event: React.MouseEvent) => { - event.preventDefault(); - event.stopPropagation(); - void downloadImageNode(node); - }; - handleCaptionKeyDown = ({ node, getPos }: ComponentProps) => (event: React.KeyboardEvent) => { @@ -322,24 +327,44 @@ export default class Image extends SimpleImage { this.editor.updateActiveLightbox(getPos()); }; - component = (props: ComponentProps) => ( - - { + const [isDownloading, setIsDownloading] = React.useState(false); + + const handleDownload = React.useCallback( + async (event: React.MouseEvent) => { + event.preventDefault(); + event.stopPropagation(); + + if (isDownloading) { + return; + } + setIsDownloading(true); + await downloadImageNode(props.node); + setIsDownloading(false); + }, + [isDownloading, props] + ); + + return ( + - {props.node.attrs.alt} - - - ); + + {props.node.attrs.alt} + + + ); + }; toMarkdown(state: MarkdownSerializerState, node: ProsemirrorNode) { // Skip the preceding space for images at the start of a list item or Markdown parsers may