From 4096eb845c3f897aaaf02404de38a7adea0487bf Mon Sep 17 00:00:00 2001 From: Nastya Date: Fri, 20 Jun 2025 17:17:45 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=B2=D0=B8=D0=B7=D1=83=D0=B0=D0=BB=20=D0=B7=D0=B0?= =?UTF-8?q?=D0=B3=D1=80=D1=83=D0=B7=D0=BA=D0=B8=20=D0=BA=D0=B0=D1=80=D1=82?= =?UTF-8?q?=D0=B8=D0=BD=D0=BA=D0=B8=20=D0=B4=D0=BB=D1=8F=20images?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../questions/Images/ImageVariant.tsx | 44 ++--- .../questions/Images/OwnImage.tsx | 164 ++++++++++++++++++ 2 files changed, 181 insertions(+), 27 deletions(-) create mode 100644 lib/components/ViewPublicationPage/questions/Images/OwnImage.tsx diff --git a/lib/components/ViewPublicationPage/questions/Images/ImageVariant.tsx b/lib/components/ViewPublicationPage/questions/Images/ImageVariant.tsx index 3d98f6a..7b01ade 100644 --- a/lib/components/ViewPublicationPage/questions/Images/ImageVariant.tsx +++ b/lib/components/ViewPublicationPage/questions/Images/ImageVariant.tsx @@ -1,4 +1,3 @@ -import { CheckboxIcon } from "@/assets/icons/Checkbox"; import type { QuestionVariant, QuestionVariantWithEditedImages } from "@/model/questionTypes/shared"; import { Box, Checkbox, FormControlLabel, Input, Radio, TextareaAutosize, Typography, useTheme } from "@mui/material"; import { useQuizViewStore } from "@stores/quizView"; @@ -9,6 +8,7 @@ import { useMemo, type MouseEvent, useRef, useEffect } from "react"; import { useRootContainerSize } from "@contexts/RootContainerWidthContext"; import { useQuizStore } from "@/stores/useQuizStore"; import { useTranslation } from "react-i18next"; +import { OwnImage } from "./OwnImage"; type ImagesProps = { questionId: string; @@ -27,7 +27,7 @@ interface OwnInputProps { largeCheck: boolean; ownPlaceholder: string; } -const OwnInput = ({ questionId, variant, largeCheck, ownPlaceholder }: OwnInputProps) => { +const OwnInput = ({ variant, largeCheck, ownPlaceholder }: OwnInputProps) => { const theme = useTheme(); const ownVariants = useQuizViewStore((state) => state.ownVariants); const { updateOwnVariant } = useQuizViewStore((state) => state); @@ -97,12 +97,10 @@ export const ImageVariant = ({ const { deleteAnswer, updateAnswer } = useQuizViewStore((state) => state); const theme = useTheme(); const { t } = useTranslation(); - const answers = useQuizViewStore((state) => state.answers); const isMobile = useRootContainerSize() < 450; const isTablet = useRootContainerSize() < 850; const canvasRef = useRef(null); - const containerCanvasRef = useRef(null); const onVariantClick = async (event: MouseEvent) => { event.preventDefault(); @@ -172,29 +170,21 @@ export const ImageVariant = ({ > - {variant.extendedText && ( - - - // + {own ? ( + + ) : ( + variant.extendedText && ( + + ) )} diff --git a/lib/components/ViewPublicationPage/questions/Images/OwnImage.tsx b/lib/components/ViewPublicationPage/questions/Images/OwnImage.tsx new file mode 100644 index 0000000..43acb13 --- /dev/null +++ b/lib/components/ViewPublicationPage/questions/Images/OwnImage.tsx @@ -0,0 +1,164 @@ +import { Box, ButtonBase, IconButton, Typography, useTheme } from "@mui/material"; +import { useState, useRef, useEffect } from "react"; +import CloseIcon from "@mui/icons-material/Close"; + +type OwnImageProps = { + imageUrl?: string; +}; + +export const OwnImage = ({ imageUrl }: OwnImageProps) => { + const theme = useTheme(); + const [selectedFile, setSelectedFile] = useState(null); + const [isDropzoneHighlighted, setIsDropzoneHighlighted] = useState(false); + const fileInputRef = useRef(null); + + // Sync state if external imageUrl changes + useEffect(() => { + if (imageUrl) { + setSelectedFile(null); // Clear local file selection when external URL is provided + } + }, [imageUrl]); + + const handleFileChange = (event: React.ChangeEvent) => { + const file = event.target.files?.[0]; + if (file && file.type.startsWith("image/")) { + setSelectedFile(file); + } + }; + + const handleDrop = (event: React.DragEvent) => { + event.preventDefault(); + event.stopPropagation(); + setIsDropzoneHighlighted(false); + const file = event.dataTransfer.files?.[0]; + if (file && file.type.startsWith("image/")) { + setSelectedFile(file); + } + }; + + const handleDragOver = (event: React.DragEvent) => { + event.preventDefault(); + event.stopPropagation(); + }; + + const handleDragEnter = (event: React.DragEvent) => { + event.preventDefault(); + event.stopPropagation(); + setIsDropzoneHighlighted(true); + }; + + const handleDragLeave = (event: React.DragEvent) => { + event.preventDefault(); + event.stopPropagation(); + setIsDropzoneHighlighted(false); + }; + + const handleClick = (e: React.MouseEvent) => { + e.stopPropagation(); + if (fileInputRef.current) { + fileInputRef.current.value = ""; + } + fileInputRef.current?.click(); + }; + + const handleRemoveImage = (e: React.MouseEvent) => { + e.stopPropagation(); + setSelectedFile(null); + }; + + const imageToDisplay = selectedFile ? URL.createObjectURL(selectedFile) : imageUrl; + + return ( + + + {imageToDisplay ? ( + <> + + Preview + + + {selectedFile && ( + + + + )} + + ) : ( + + + добавьте свою картинку + + + )} + + ); +};