From 0cf4026a08713de9fe2deb6aa626c92c827466f9 Mon Sep 17 00:00:00 2001 From: Majd_dk Date: Tue, 23 Sep 2025 15:40:30 +0300 Subject: [PATCH] #67 --- src/Components/LatextInput/AddImageModal.tsx | 95 ++++++++++++++++++++ src/api/image.ts | 20 +++++ src/utils/parseTextAndLatex.ts | 28 +++--- 3 files changed, 130 insertions(+), 13 deletions(-) create mode 100644 src/Components/LatextInput/AddImageModal.tsx create mode 100644 src/api/image.ts diff --git a/src/Components/LatextInput/AddImageModal.tsx b/src/Components/LatextInput/AddImageModal.tsx new file mode 100644 index 0000000..262c42b --- /dev/null +++ b/src/Components/LatextInput/AddImageModal.tsx @@ -0,0 +1,95 @@ +import { Button, Modal, Spin, Switch } from "antd"; +import TextArea from "antd/es/input/TextArea"; +import { Field, getIn, useFormikContext } from "formik"; +import React, { useEffect, useState } from "react"; +import { convertMathMLToLaTeX } from "../../utils/convertMathMLToLaTeX"; +import { useTranslation } from "react-i18next"; +import ImageBoxFieldMemo from "../CustomFields/ImageBoxField/ImageBoxFieldMemo"; +import { useGetUrl } from "../../api/image"; +import Loading from "../Utils/Loading/Loading"; +import { Spinner } from "reactstrap"; + +const AddImageModal = ({ + name, + setIsModalOpen, + isModalOpen, + setCurrentValue, +}: { + name: string; + setIsModalOpen: (value: boolean) => void; + isModalOpen: boolean; + setCurrentValue: (value: string) => void; +}) => { + const { values, setFieldValue, getFieldProps } = useFormikContext(); + const [imageUrl,setImageUrl] = useState("") + + const {isSuccess,data,isLoading,mutateAsync:sendFile}= useGetUrl() + + + + + const currentValue = getFieldProps(name).value; + +const File = getIn(values, `${name}_image_with_content`); + + const handleOk = async () => { + + if(!File)return; + await sendFile({image:File}).then((response:any) => { + const oldValue = currentValue ?? ""; + const imageUrl = response?.data?.url + + const final = oldValue + `<> ${imageUrl} <>` + + setFieldValue(name, final); + setCurrentValue(final); + setFieldValue(`${name}_image_with_content`, null); + + setIsModalOpen(false); + }) + + }; + + const handleCancel = () => { + setIsModalOpen(false); + // setLatex(""); + }; + + const handleChangeInputLatex = ( + e: React.ChangeEvent, + ) => { + const newValue = e.target.value; + // setLatex(newValue); + + }; + + + + const [t] = useTranslation(); + return ( + +
+ +
+ +
+ +
+
+ {t("practical.cancel")} +
+ +
+
+
+ ); +}; + +export default AddImageModal; diff --git a/src/api/image.ts b/src/api/image.ts new file mode 100644 index 0000000..4542dfd --- /dev/null +++ b/src/api/image.ts @@ -0,0 +1,20 @@ +import useAddMutation from "./helper/useAddMutation"; +import useDeleteMutation from "./helper/useDeleteMutation"; +import useGetQuery from "./helper/useGetQuery"; +import useUpdateMutation from "./helper/useUpdateMutation"; + +const API = { + GET: "/image", + ADD: "/image", + DELETE: "/image", + UPDATE: "/image", +}; + +const KEY = "image"; + +export const useGetAllImages = (params?: any, options?: any) => + useGetQuery(KEY, API.GET, params, options); +export const useGetUrl = () => useAddMutation(KEY, API.ADD); +export const useUpdateImage = (params?: any) => useUpdateMutation(KEY, API.GET); +export const useDeleteImage = (params?: any) => + useDeleteMutation(KEY, API.DELETE); diff --git a/src/utils/parseTextAndLatex.ts b/src/utils/parseTextAndLatex.ts index af93912..7e17803 100644 --- a/src/utils/parseTextAndLatex.ts +++ b/src/utils/parseTextAndLatex.ts @@ -1,25 +1,27 @@ -interface TextLatexPart { - text: string; +interface TextPart { + text: string; isLatex: boolean; + isImage: boolean; key: number; } -export const parseTextAndLatex = (input: string): TextLatexPart[] => { - const result: TextLatexPart[] = []; +export const parseTextAndLatex = (input: string = ""): TextPart[] => { + const out: TextPart[] = []; - console.log(input) - const parts = input?.split(/(\$\$[^$]+\$\$)/g); - console.log(parts) + const parts = input.split(/(\$\$[\s\S]+?\$\$|<>[\s\S]+?<>)/g); parts.forEach((part, index) => { + if (!part) return; + if (part.startsWith("$$") && part.endsWith("$$")) { - result.push({ text: part.slice(2, -2), isLatex: true, key: index }); - } - else { - result.push({ text: part, isLatex: false, key: index }); + out.push({ text: part.slice(2, -2), isLatex: true, isImage: false, key: index }); + } else if (part.startsWith("<>") && part.endsWith("<>")) { + const url = part.slice("<>".length, -"<>".length).trim(); + out.push({ text: url, isLatex: false, isImage: true, key: index }); + } else { + out.push({ text: part, isLatex: false, isImage: false, key: index }); } }); - console.log(result) - return result; + return out; };