Skip to content

Let's use AI to Earn!

feat: Add cropping ratio presets to ImageEditorModal

gaoSirb00217fb4e08
diff --git a/.eslintignore b/.eslintignore
new file mode 100644
index 00000000..1ca4c483
--- /dev/null
+++ b/.eslintignore
@@ -0,0 +1 @@
+/src/components/ImageEditorModal/tui-image-editor/
diff --git a/@types/resources.d.ts b/@types/resources.d.ts
index 4c3545df..71d78811 100644
--- a/@types/resources.d.ts
+++ b/@types/resources.d.ts
@@ -855,7 +855,8 @@ interface Resources {
"Redo": "重做",
"Delete": "去除",
"DeleteAll": "全部去除",
- "Blend": "混合"
+ "Blend": "混合",
+ "imageEditing": "图像编辑"
},
"income": {
"title": "我的收入",
diff --git a/src/app/i18n/locales/en/imageEditor.json b/src/app/i18n/locales/en/imageEditor.json
new file mode 100644
index 00000000..7e5342ac
--- /dev/null
+++ b/src/app/i18n/locales/en/imageEditor.json
@@ -0,0 +1,3 @@
+{
+ "imageEditing": "image editing"
+}
\ No newline at end of file
diff --git a/src/app/i18n/locales/zh-CN/imageEditor.json b/src/app/i18n/locales/zh-CN/imageEditor.json
index 7f305549..f1837ec4 100644
--- a/src/app/i18n/locales/zh-CN/imageEditor.json
+++ b/src/app/i18n/locales/zh-CN/imageEditor.json
@@ -74,5 +74,6 @@
"Redo": "重做",
"Delete": "去除",
"DeleteAll": "全部去除",
- "Blend": "混合"
+ "Blend": "混合",
+ "imageEditing": "图像编辑"
}
\ No newline at end of file
diff --git a/src/components/ImageEditorModal/index.tsx b/src/components/ImageEditorModal/index.tsx
index ab09d75e..5517ae68 100644
--- a/src/components/ImageEditorModal/index.tsx
+++ b/src/components/ImageEditorModal/index.tsx
@@ -39,6 +39,7 @@ const ImageEditorModal = memo(
const { t } = useTransClient("imageEditor");
const imageEditorRef = useRef<any>();
const [uploadLoading, setUploadLoading] = useState(false);
+ const isInit = useRef(false);
const imageEditorLocale = useMemo(() => {
if (lng === "en") return undefined;
@@ -63,9 +64,47 @@ const ImageEditorModal = memo(
}
}, [imgFile, open]);
+ // 添加裁剪预设
+ function addCropPreset(newClass: string, label: string, mode: number) {
+ if (document.querySelector(`.${newClass}`)) return;
+ const originClass = "preset-16-9";
+ const cropCon = document.querySelector(".tie-crop-preset-button")!;
+ const originPreset = cropCon.querySelector(`.${originClass}`)!;
+ // 克隆节点并清除所有事件
+ const cleanPreset = originPreset.cloneNode(true) as HTMLElement;
+ const newPreset = cleanPreset.cloneNode(true) as HTMLElement;
+ newPreset.querySelector("label")!.innerText = label;
+ newPreset.classList.remove(originClass);
+ newPreset.classList.add(newClass);
+ cropCon.appendChild(newPreset);
+ cropCon.addEventListener("click", (e) => {
+ const target = e.target as HTMLElement;
+ if (target.closest(`.${newClass}`)) {
+ const inst = imageEditorRef.current?.getInstance();
+ inst.stopDrawingMode();
+ inst.startDrawingMode("CROPPER");
+ inst.setCropzoneRect(mode);
+ }
+ });
+ }
+
+ useEffect(() => {
+ const timeId = setInterval(() => {
+ if (
+ !isInit.current &&
+ document.querySelector(".tie-crop-preset-button")
+ ) {
+ addCropPreset("preset-4-5", "4:5", 4 / 5);
+ addCropPreset("preset-1-1", "1:1", 1);
+ addCropPreset("preset-1_9-1", "1.19:1", 1.19);
+ clearInterval(timeId);
+ }
+ }, 100);
+ }, []);
+
return (
<Modal
- title={"图像编辑"}
+ title={t("imageEditing")}
open={open}
onCancel={onCancel}
width={1100}