Skip to content

Commit

Permalink
[lexical-playground][image-node] Bug Fix: Load image error UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Maksym Plavinskyi authored and Maksym Plavinskyi committed May 15, 2024
1 parent ec18fcf commit cf175ec
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
4 changes: 4 additions & 0 deletions packages/lexical-playground/src/images/image-broken.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 42 additions & 14 deletions packages/lexical-playground/src/nodes/ImageComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -72,6 +73,9 @@ function useSuspenseImage(src: string) {
imageCache.add(src);
resolve(null);
};
img.onerror = () => {
imageCache.add(src);
};
});
}
}
Expand All @@ -84,6 +88,7 @@ function LazyImage({
width,
height,
maxWidth,
onError,
}: {
altText: string;
className: string | null;
Expand All @@ -92,6 +97,7 @@ function LazyImage({
maxWidth: number;
src: string;
width: 'inherit' | number;
onError: () => void;
}): JSX.Element {
useSuspenseImage(src);
return (
Expand All @@ -105,6 +111,21 @@ function LazyImage({
maxWidth,
width,
}}
onError={onError}
draggable="false"
/>
);
}

function BrokenImage(): JSX.Element {
return (
<img
src={brokenImage}
style={{
height: 200,
opacity: 0.2,
width: 200,
}}
draggable="false"
/>
);
Expand Down Expand Up @@ -142,6 +163,7 @@ export default function ImageComponent({
const [editor] = useLexicalComposerContext();
const [selection, setSelection] = useState<BaseSelection | null>(null);
const activeEditorRef = useRef<LexicalEditor | null>(null);
const [isLoadError, setIsLoadError] = useState<boolean>(false);

const $onDelete = useCallback(
(payload: KeyboardEvent) => {
Expand Down Expand Up @@ -371,20 +393,26 @@ export default function ImageComponent({
<Suspense fallback={null}>
<>
<div draggable={draggable}>
<LazyImage
className={
isFocused
? `focused ${$isNodeSelection(selection) ? 'draggable' : ''}`
: null
}
src={src}
altText={altText}
imageRef={imageRef}
width={width}
height={height}
maxWidth={maxWidth}
/>
{isLoadError ? (
<BrokenImage />
) : (
<LazyImage
className={
isFocused
? `focused ${$isNodeSelection(selection) ? 'draggable' : ''}`
: null
}
src={src}
altText={altText}
imageRef={imageRef}
width={width}
height={height}
maxWidth={maxWidth}
onError={() => setIsLoadError(true)}
/>
)}
</div>

{showCaption && (
<div className="image-caption-container">
<LexicalNestedComposer initialEditor={caption}>
Expand Down Expand Up @@ -428,7 +456,7 @@ export default function ImageComponent({
maxWidth={maxWidth}
onResizeStart={onResizeStart}
onResizeEnd={onResizeEnd}
captionsEnabled={captionsEnabled}
captionsEnabled={!isLoadError && captionsEnabled}
/>
)}
</>
Expand Down

0 comments on commit cf175ec

Please sign in to comment.