-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor structure of modal component
- Loading branch information
1 parent
bf74463
commit fc018dc
Showing
26 changed files
with
987 additions
and
871 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Modal as SemiUIModal } from "@douyinfe/semi-ui"; | ||
import { useTranslation } from "react-i18next"; | ||
|
||
export default function Modal({ | ||
children, | ||
modalTitle, | ||
okText, | ||
onOk, | ||
onCancel, | ||
okBtnDisabled, | ||
width, | ||
}) { | ||
const { t } = useTranslation(); | ||
|
||
return ( | ||
<SemiUIModal | ||
title={modalTitle || ""} | ||
visible={true} | ||
onOk={onOk} | ||
onCancel={onCancel} | ||
centered | ||
closeOnEsc={true} | ||
okText={okText || t("confirm")} | ||
okButtonProps={{ | ||
disabled: okBtnDisabled, | ||
}} | ||
cancelText={t("cancel")} | ||
width={width || 600} | ||
// bodyStyle={{ maxHeight: window.innerHeight - 280, overflow: "auto" }} | ||
> | ||
{children} | ||
</SemiUIModal> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { useEffect, useState } from "react"; | ||
import { toPng, toJpeg, toSvg } from "html-to-image"; | ||
import { Image, Select, Spin } from "@douyinfe/semi-ui"; | ||
import { useTranslation } from "react-i18next"; | ||
import { IMAGE_TYPES } from "../../../data/constants"; | ||
import ExportModal from "./ExportModal"; | ||
|
||
export default function ExportImage({ title, hideModal }) { | ||
const { t } = useTranslation(); | ||
const [exportData, setExportData] = useState({ | ||
data: null, | ||
filename: `${title}_${new Date().toISOString()}`, | ||
extension: IMAGE_TYPES[0], | ||
}); | ||
|
||
const changeType = async (type) => { | ||
setExportData((prev) => ({ | ||
...prev, | ||
data: null, | ||
extension: type, | ||
})); | ||
|
||
const canvasElm = document.getElementById("canvas"); | ||
|
||
let dataUrl; | ||
switch (type) { | ||
case "png": | ||
dataUrl = await toPng(canvasElm); | ||
break; | ||
case "jpeg": | ||
dataUrl = await toJpeg(canvasElm, { quality: 0.95 }); | ||
break; | ||
case "svg": | ||
dataUrl = await toSvg(canvasElm, { | ||
filter: (node) => node.tagName !== "i", | ||
}); | ||
break; | ||
} | ||
|
||
setExportData((prev) => ({ | ||
...prev, | ||
data: dataUrl, | ||
})); | ||
}; | ||
|
||
useEffect(() => { | ||
changeType(IMAGE_TYPES[0]); | ||
}, []); | ||
|
||
return ( | ||
<ExportModal | ||
modalTitle={t("export_image")} | ||
onCancel={hideModal} | ||
exportData={exportData} | ||
setExportData={setExportData} | ||
> | ||
<div className="font-semibold mb-1">{t("format")}:</div> | ||
<Select | ||
className="w-full mb-2" | ||
optionList={IMAGE_TYPES.map((type) => ({ | ||
label: type.toUpperCase(), | ||
value: type, | ||
}))} | ||
value={exportData.extension} | ||
onChange={changeType} | ||
/> | ||
<div className="text-center my-3 h-[280px] flex flex-col justify-center items-center"> | ||
{exportData.data ? ( | ||
<Image | ||
src={exportData.data} | ||
alt="Diagram" | ||
className="overflow-auto" | ||
/> | ||
) : ( | ||
<Spin size="large" /> | ||
)} | ||
</div> | ||
</ExportModal> | ||
); | ||
} |
Oops, something went wrong.