Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lexical-playground][image-node] Bug Fix: Load image error UI #6111

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading