fix: minor fixes

This commit is contained in:
Palanikannan M
2024-09-19 14:22:38 +05:30
parent d94046e69a
commit 295fba6a0d
3 changed files with 12 additions and 14 deletions

View File

@@ -23,13 +23,14 @@ export const CustomImageBlock: React.FC<CustomImageNodeViewProps> = (props) => {
const [isLoading, setIsLoading] = useState(true);
const [initialResizeComplete, setInitialResizeComplete] = useState(false);
const isShimmerVisible = isLoading || !initialResizeComplete;
const [editorContainer, setEditorContainer] = useState<HTMLElement | null>(null);
const [isResizing, setIsResizing] = useState(false);
const containerRef = useRef<HTMLDivElement>(null);
const containerRect = useRef<DOMRect | null>(null);
const imageRef = useRef<HTMLImageElement>(null);
const [isResizing, setIsResizing] = useState(false);
const isShimmerVisible = isLoading || !initialResizeComplete;
const handleImageLoad = useCallback(() => {
const img = imageRef.current;

View File

@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useRef, useState } from "react";
import { useCallback, useEffect, useRef, useState, useMemo } from "react";
import { Node as ProsemirrorNode } from "@tiptap/pm/model";
import { Editor, NodeViewWrapper } from "@tiptap/react";
// extensions
@@ -33,16 +33,14 @@ export const CustomImageNode = (props: CustomImageNodeViewProps) => {
const id = node.attrs.id as string;
const editorStorage = editor.storage.imageComponent as UploadImageExtensionStorage | undefined;
const getUploadEntity = useCallback(
(): UploadEntity | undefined => editorStorage?.fileMap.get(id),
[editorStorage, id]
);
const getUploadEntity = useCallback((): UploadEntity | undefined => {
return editorStorage?.fileMap.get(id);
}, [editorStorage, id]);
const onUpload = useCallback(
(url: string) => {
if (url) {
setIsUploaded(true);
// Update the node view's src attribute
updateAttributes({ src: url });
editorStorage?.fileMap.delete(id);
}
@@ -65,16 +63,16 @@ export const CustomImageNode = (props: CustomImageNodeViewProps) => {
console.error("Error uploading file:", error);
}
},
[editor.commands, onUpload]
[editor, onUpload]
);
useEffect(() => {
const uploadEntity = getUploadEntity();
if (uploadEntity) {
if (uploadEntity && !hasTriggeredFilePickerRef.current) {
if (uploadEntity.event === "drop" && "file" in uploadEntity) {
uploadFile(uploadEntity.file);
} else if (uploadEntity.event === "insert" && fileInputRef.current && !hasTriggeredFilePickerRef.current) {
} else if (uploadEntity.event === "insert" && fileInputRef.current) {
const entity = editorStorage?.fileMap.get(id);
if (entity && entity.hasOpenedFileInputOnce) return;
fileInputRef.current.click();
@@ -83,7 +81,7 @@ export const CustomImageNode = (props: CustomImageNodeViewProps) => {
editorStorage?.fileMap.set(id, { ...entity, hasOpenedFileInputOnce: true });
}
}
}, [getUploadEntity, uploadFile]);
}, [getUploadEntity, uploadFile, editorStorage?.fileMap, id]);
useEffect(() => {
if (node.attrs.src) {
@@ -91,7 +89,7 @@ export const CustomImageNode = (props: CustomImageNodeViewProps) => {
}
}, [node.attrs.src]);
const existingFile = React.useMemo(() => {
const existingFile = useMemo(() => {
const entity = getUploadEntity();
return entity && entity.event === "drop" ? entity.file : undefined;
}, [getUploadEntity]);

View File

@@ -53,7 +53,6 @@ export const CustomImageUploader = (props: {
const file = e.target.files?.[0];
if (file) {
if (isFileValid(file)) {
editor.storage.image.uploadInProgress = true;
const reader = new FileReader();
reader.onload = () => {
setPreviewUrl(reader.result as string);