|
@@ -1,14 +1,13 @@
|
|
|
-import React, {useEffect, useState} from 'react';
|
|
|
-import {Button, message, Modal} from 'antd';
|
|
|
-import Quill from "@/pages/Memory/components/Quill";
|
|
|
-import service from "@/pages/Memory/service";
|
|
|
-import {useSingleState} from "nice-hooks";
|
|
|
-import {getTextFromHtml} from "@/utils/utils";
|
|
|
-import {TableListItem} from '../../MemoryList/data.d';
|
|
|
+import React, { useEffect, useState } from 'react';
|
|
|
+import { Button, message, Modal } from 'antd';
|
|
|
+import Quill from '@/pages/Memory/components/Quill';
|
|
|
+import service from '@/pages/Memory/service';
|
|
|
+import { useSingleState } from 'nice-hooks';
|
|
|
+import { getTextFromHtml } from '@/utils/utils';
|
|
|
+import { TableListItem } from '../../MemoryList/data.d';
|
|
|
|
|
|
// 表单特殊字段
|
|
|
-export interface FormValueType extends Partial<TableListItem> {
|
|
|
-}
|
|
|
+export interface FormValueType extends Partial<TableListItem> {}
|
|
|
|
|
|
export interface UpdateFormProps {
|
|
|
onCancel: (fresh?: boolean) => void;
|
|
@@ -21,21 +20,21 @@ const dealImage = (base64: string, targetWidth: number, targetSize: number, call
|
|
|
let quality = 0.8;
|
|
|
newImage.src = base64;
|
|
|
// url为外域时需要
|
|
|
- newImage.setAttribute("crossOrigin", 'Anonymous');
|
|
|
+ newImage.setAttribute('crossOrigin', 'Anonymous');
|
|
|
let imgWidth;
|
|
|
let imgHeight;
|
|
|
newImage.onload = function () {
|
|
|
imgWidth = newImage.width;
|
|
|
imgHeight = newImage.height;
|
|
|
- const canvas = document.createElement("canvas");
|
|
|
- const ctx = canvas.getContext("2d");
|
|
|
+ const canvas = document.createElement('canvas');
|
|
|
+ const ctx = canvas.getContext('2d');
|
|
|
if (Math.max(imgWidth, imgHeight) > targetWidth) {
|
|
|
if (imgWidth > imgHeight) {
|
|
|
canvas.width = targetWidth;
|
|
|
- canvas.height = targetWidth * imgHeight / imgWidth;
|
|
|
+ canvas.height = (targetWidth * imgHeight) / imgWidth;
|
|
|
} else {
|
|
|
canvas.height = targetWidth;
|
|
|
- canvas.width = targetWidth * imgWidth / imgHeight;
|
|
|
+ canvas.width = (targetWidth * imgWidth) / imgHeight;
|
|
|
}
|
|
|
} else {
|
|
|
canvas.width = imgWidth;
|
|
@@ -49,66 +48,101 @@ const dealImage = (base64: string, targetWidth: number, targetSize: number, call
|
|
|
while (base64.length / 1024 > targetSize) {
|
|
|
quality -= 0.01;
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
- base64 = canvas.toDataURL("image/jpeg", quality);
|
|
|
+ base64 = canvas.toDataURL('image/jpeg', quality);
|
|
|
}
|
|
|
// 防止最后一次压缩低于最低尺寸,只要quality递减合理,无需考虑
|
|
|
// while (base64.length / 1024 < 50) {
|
|
|
// quality += 0.001;
|
|
|
// base64 = canvas.toDataURL("image/jpeg", quality);
|
|
|
// }
|
|
|
- callback(base64);// 必须通过回调函数返回,否则无法及时拿到该值
|
|
|
- }
|
|
|
-}
|
|
|
+ callback(base64); // 必须通过回调函数返回,否则无法及时拿到该值
|
|
|
+ };
|
|
|
+};
|
|
|
|
|
|
/**
|
|
|
* 压缩html中的base64
|
|
|
*/
|
|
|
-const dealHtml = (html: string,callBack:Function) => {
|
|
|
+const dealHtml = (html: string, callBack: Function) => {
|
|
|
callBack(html);
|
|
|
- const htmlDoc = new DOMParser().parseFromString(html, "text/html");
|
|
|
- const imgs = htmlDoc.querySelectorAll("img");
|
|
|
+ const htmlDoc = new DOMParser().parseFromString(html, 'text/html');
|
|
|
+ const imgs = htmlDoc.querySelectorAll('img');
|
|
|
for (let i = 0; i < imgs.length; i += 1) {
|
|
|
- const {src} = imgs[i];
|
|
|
- if (src.startsWith("data")) {
|
|
|
+ const { src } = imgs[i];
|
|
|
+ if (src.startsWith('data')) {
|
|
|
dealImage(src, 500, 150, (base64: string) => {
|
|
|
imgs[i].src = base64;
|
|
|
- callBack(htmlDoc.body.innerHTML)
|
|
|
- console.log(`压缩图片${i}`)
|
|
|
- })
|
|
|
+ callBack(htmlDoc.body.innerHTML);
|
|
|
+ console.log(`压缩图片${i}`);
|
|
|
+ });
|
|
|
}
|
|
|
}
|
|
|
-}
|
|
|
-
|
|
|
+};
|
|
|
|
|
|
const UpdateForm: React.FC<UpdateFormProps> = (props) => {
|
|
|
const [btnDisable, setBtnDisable] = useState(false);
|
|
|
|
|
|
const [formValues, setFormValues] = useSingleState<TableListItem>({
|
|
|
- back: "",
|
|
|
- front: "",
|
|
|
+ back: '',
|
|
|
+ front: '',
|
|
|
id: undefined,
|
|
|
period: 0,
|
|
|
remindTime: new Date(),
|
|
|
- tag: "",
|
|
|
- updateTime: "",
|
|
|
- userId: 0, ...props.values
|
|
|
+ tag: '',
|
|
|
+ updateTime: '',
|
|
|
+ userId: 0,
|
|
|
+ ...props.values,
|
|
|
});
|
|
|
|
|
|
+ const { onCancel: hideModal, modalVisible } = props;
|
|
|
|
|
|
- const {
|
|
|
- onCancel: hideModal,
|
|
|
- modalVisible,
|
|
|
- } = props;
|
|
|
+ const addHandle = async () => {
|
|
|
+ /**
|
|
|
+ * 判断
|
|
|
+ */
|
|
|
+ console.log(formValues);
|
|
|
+ if (getTextFromHtml(formValues.front + formValues.back).length <= 1) {
|
|
|
+ message.info('你是不是没有输入文字?');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (formValues.front.length >= 300000 || formValues.back.length >= 300000) {
|
|
|
+ message.info('文字太长了,减少一些吧!');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ setBtnDisable(true); // 禁用按钮
|
|
|
+ let res;
|
|
|
+ if (formValues.id !== undefined) {
|
|
|
+ res = await service.update(formValues);
|
|
|
+ } else {
|
|
|
+ res = await service.insert(formValues);
|
|
|
+ }
|
|
|
+ setBtnDisable(false); // 释放按钮
|
|
|
+ if (res.success) {
|
|
|
+ message.info('操作成功');
|
|
|
+ if (formValues.id !== undefined) {
|
|
|
+ hideModal(true);
|
|
|
+ } else {
|
|
|
+ setFormValues({ front: '', back: '' });
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ message.info(`操作失败: ${res.message}`);
|
|
|
+ }
|
|
|
+ };
|
|
|
|
|
|
useEffect(() => {
|
|
|
console.log(props);
|
|
|
- }, [])
|
|
|
-
|
|
|
+ document.onkeydown = (ev) => {
|
|
|
+ if (ev.ctrlKey && ev.key.toLowerCase() === 'enter') {
|
|
|
+ addHandle();
|
|
|
+ } else if (ev.key.toLowerCase() === 'escape') {
|
|
|
+ hideModal(false);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }, []);
|
|
|
|
|
|
return (
|
|
|
<Modal
|
|
|
width={640}
|
|
|
- bodyStyle={{padding: '15px 15px 15px'}}
|
|
|
+ bodyStyle={{ padding: '15px 15px 15px' }}
|
|
|
destroyOnClose
|
|
|
title="配置模型"
|
|
|
visible={modalVisible}
|
|
@@ -120,69 +154,37 @@ const UpdateForm: React.FC<UpdateFormProps> = (props) => {
|
|
|
<Quill
|
|
|
readonly={false}
|
|
|
onChange={(value) => {
|
|
|
- dealHtml(value,(html:string)=>{
|
|
|
- setFormValues({front: html});
|
|
|
- })
|
|
|
+ dealHtml(value, (html: string) => {
|
|
|
+ setFormValues({ front: html });
|
|
|
+ });
|
|
|
}}
|
|
|
- value={formValues.front}/>
|
|
|
+ value={formValues.front}
|
|
|
+ />
|
|
|
|
|
|
- <p/>
|
|
|
+ <p />
|
|
|
<h4>反面</h4>
|
|
|
<Quill
|
|
|
readonly={false}
|
|
|
onChange={(value) => {
|
|
|
- dealHtml(value,(html:string)=>{
|
|
|
- setFormValues({back: html});
|
|
|
- })
|
|
|
+ dealHtml(value, (html: string) => {
|
|
|
+ setFormValues({ back: html });
|
|
|
+ });
|
|
|
}}
|
|
|
value={formValues.back}
|
|
|
/>
|
|
|
- <p/>
|
|
|
- <Button
|
|
|
- type="primary"
|
|
|
- disabled={btnDisable}
|
|
|
- onClick={async () => {
|
|
|
- /**
|
|
|
- * 判断
|
|
|
- */
|
|
|
- console.log(formValues);
|
|
|
- if (getTextFromHtml(formValues.front + formValues.back).length <= 1) {
|
|
|
- message.info("你是不是没有输入文字?");
|
|
|
- return;
|
|
|
- }
|
|
|
- if (formValues.front.length >= 300000 || formValues.back.length >= 300000) {
|
|
|
- message.info("文字太长了,减少一些吧!");
|
|
|
- return;
|
|
|
- }
|
|
|
- setBtnDisable(true); // 禁用按钮
|
|
|
- let res;
|
|
|
- if (formValues.id != undefined) {
|
|
|
- res = await service.update(formValues);
|
|
|
- } else {
|
|
|
- res = await service.insert(formValues);
|
|
|
- }
|
|
|
- setBtnDisable(false); // 释放按钮
|
|
|
- if (res.success) {
|
|
|
- message.info('操作成功');
|
|
|
- if (formValues.id != undefined) {
|
|
|
- hideModal(true);
|
|
|
- } else {
|
|
|
- setFormValues({front: '', back: ''})
|
|
|
- }
|
|
|
- } else {
|
|
|
- message.info(`操作失败: ${res.message}`);
|
|
|
- }
|
|
|
- }}
|
|
|
- >
|
|
|
+ <p />
|
|
|
+ <Button type="primary" disabled={btnDisable} onClick={addHandle}>
|
|
|
确定
|
|
|
</Button>
|
|
|
<span> </span>
|
|
|
<Button
|
|
|
type="primary"
|
|
|
onClick={() => {
|
|
|
- hideModal(false)
|
|
|
+ hideModal(false);
|
|
|
}}
|
|
|
- >关闭</Button>
|
|
|
+ >
|
|
|
+ 关闭
|
|
|
+ </Button>
|
|
|
</div>
|
|
|
</Modal>
|
|
|
);
|