diff --git a/packages/lexical-playground/src/images/image-broken.svg b/packages/lexical-playground/src/images/image-broken.svg new file mode 100644 index 00000000000..58e4aa9a859 --- /dev/null +++ b/packages/lexical-playground/src/images/image-broken.svg @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/packages/lexical-playground/src/nodes/ImageComponent.tsx b/packages/lexical-playground/src/nodes/ImageComponent.tsx index 42e8249ec66..df684aed286 100644 --- a/packages/lexical-playground/src/nodes/ImageComponent.tsx +++ b/packages/lexical-playground/src/nodes/ImageComponent.tsx @@ -48,6 +48,7 @@ import {Suspense, useCallback, useEffect, useRef, useState} from 'react'; import {createWebsocketProvider} from '../collaboration'; import {useSettings} from '../context/SettingsContext'; import {useSharedHistoryContext} from '../context/SharedHistoryContext'; +import brokenImage from '../images/image-broken.svg'; import EmojisPlugin from '../plugins/EmojisPlugin'; import KeywordsPlugin from '../plugins/KeywordsPlugin'; import LinkPlugin from '../plugins/LinkPlugin'; @@ -72,6 +73,9 @@ function useSuspenseImage(src: string) { imageCache.add(src); resolve(null); }; + img.onerror = () => { + imageCache.add(src); + }; }); } } @@ -84,6 +88,7 @@ function LazyImage({ width, height, maxWidth, + onError, }: { altText: string; className: string | null; @@ -92,6 +97,7 @@ function LazyImage({ maxWidth: number; src: string; width: 'inherit' | number; + onError: () => void; }): JSX.Element { useSuspenseImage(src); return ( @@ -105,6 +111,21 @@ function LazyImage({ maxWidth, width, }} + onError={onError} + draggable="false" + /> + ); +} + +function BrokenImage(): JSX.Element { + return ( + ); @@ -142,6 +163,7 @@ export default function ImageComponent({ const [editor] = useLexicalComposerContext(); const [selection, setSelection] = useState(null); const activeEditorRef = useRef(null); + const [isLoadError, setIsLoadError] = useState(false); const $onDelete = useCallback( (payload: KeyboardEvent) => { @@ -371,20 +393,26 @@ export default function ImageComponent({ <>
- + {isLoadError ? ( + + ) : ( + setIsLoadError(true)} + /> + )}
+ {showCaption && (
@@ -428,7 +456,7 @@ export default function ImageComponent({ maxWidth={maxWidth} onResizeStart={onResizeStart} onResizeEnd={onResizeEnd} - captionsEnabled={captionsEnabled} + captionsEnabled={!isLoadError && captionsEnabled} /> )}