From b8124a48c876686d6c00e539504a66bf73ae60d9 Mon Sep 17 00:00:00 2001 From: Palanikannan M Date: Thu, 3 Oct 2024 16:24:19 +0530 Subject: [PATCH] fix: error handling and removing the events in read only mode --- .../custom-image/components/image-block.tsx | 23 ++++++++++++++++--- .../components/image-uploader.tsx | 5 ++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/packages/editor/src/core/extensions/custom-image/components/image-block.tsx b/packages/editor/src/core/extensions/custom-image/components/image-block.tsx index 42b51de5fb7..1301380e40a 100644 --- a/packages/editor/src/core/extensions/custom-image/components/image-block.tsx +++ b/packages/editor/src/core/extensions/custom-image/components/image-block.tsx @@ -71,6 +71,17 @@ export const CustomImageBlock: React.FC = (props) => { const containerRect = useRef(null); const imageRef = useRef(null); + const updateAttributesSafely = useCallback( + (attributes: Partial, errorMessage: string) => { + try { + updateAttributes(attributes); + } catch (error) { + console.error(`${errorMessage}:`, error); + } + }, + [updateAttributes] + ); + const handleImageLoad = useCallback(() => { const img = imageRef.current; if (!img) return; @@ -105,12 +116,18 @@ export const CustomImageBlock: React.FC = (props) => { }; setSize(initialComputedSize); - updateAttributes(initialComputedSize); + updateAttributesSafely( + initialComputedSize, + "Failed to update attributes while initializing an image for the first time:" + ); } else { // as the aspect ratio in not stored for old images, we need to update the attrs setSize((prevSize) => { const newSize = { ...prevSize, aspectRatio }; - updateAttributes(newSize); + updateAttributesSafely( + newSize, + "Failed to update attributes while initializing images with width but no aspect ratio:" + ); return newSize; }); } @@ -142,7 +159,7 @@ export const CustomImageBlock: React.FC = (props) => { const handleResizeEnd = useCallback(() => { setIsResizing(false); - updateAttributes(size); + updateAttributesSafely(size, "Failed to update attributes at the end of resizing:"); }, [size, updateAttributes]); const handleResizeStart = useCallback((e: React.MouseEvent | React.TouchEvent) => { diff --git a/packages/editor/src/core/extensions/custom-image/components/image-uploader.tsx b/packages/editor/src/core/extensions/custom-image/components/image-uploader.tsx index 89cf36ca52b..f92bfc79ab7 100644 --- a/packages/editor/src/core/extensions/custom-image/components/image-uploader.tsx +++ b/packages/editor/src/core/extensions/custom-image/components/image-uploader.tsx @@ -139,11 +139,12 @@ export const CustomImageUploader = (props: { return (
{ - if (!failedToLoadImage) { + if (!failedToLoadImage && editor.isEditable) { fileInputRef.current?.click(); } }}