import React from "react"; import { Button, Col, Image, Modal, Popconfirm, Row } from "antd"; import { VEControlProps } from "../../tmpl/interface"; import SignatureCanvas from "react-signature-canvas"; import { HighlightOutlined } from "@ant-design/icons"; import HcserviceV3 from "../../tmpl/hcservice-v3"; import { LocaleContext } from "../../locale/LocaleProvider"; interface SignatureProps extends VEControlProps {} interface SignatureState { modalOpen?: boolean; molFileLoaded?: boolean; trimmedDataURL?: any; } export default class Signature extends React.PureComponent< SignatureProps, SignatureState > { static defaultProps = { viewOrEdit: "view", }; state = { modalOpen: false, molFileLoaded: false, molFile: undefined, trimmedDataURL: undefined, }; static contextType = LocaleContext; context: React.ContextType; componentDidMount() { this.loadData(); } signCanvas = undefined; loadData = async () => { const { value, serverKey } = this.props; if (value) { let val = JSON.parse(value); let file; if (val.valid == true) { //此处要优化 if (/^image\/png;base64/.test(val.base.type)) { file = await HcserviceV3.loadFileTxt(serverKey, val.base.path); } } else if (val.valid == "new") { file = val.body; } else { file = undefined; } //console.log('file:', file); this.setState({ trimmedDataURL: file, }); } }; saveMolfile = async () => { let value = this.signCanvas.getTrimmedCanvas().toDataURL("image/png"); // let val=value.replace(/^data:image\/\w+;base64,/, ""); // let bytes = window.atob(val); // // 处理异常,将ascii码小于0的转换为大于0 // let ab = new ArrayBuffer(bytes.length); // // 生成视图(直接针对内存):8位无符号整数,长度1个字节 // let ia = new Uint8Array(ab); // for (let i = 0; i < bytes.length; i++) { // ia[i] = bytes.charCodeAt(i); // } // let b= new Blob([ia], { // type: 'image/png' // }); //console.log('DataURL value:', value); // let data = this.signCanvas.toData(); // console.log('data data:', value); this.triggerChange(value); this.setState({ modalOpen: false, trimmedDataURL: value, }); }; triggerChange = (changedValue) => { const { onChange } = this.props; if (onChange) { onChange( JSON.stringify({ valid: "new", body: changedValue, suffix: "image/png;base64", fileName: "signature.png", }) ); } }; showView = async () => { this.signCanvas.clear(); this.signCanvas.fromDataURL(this.state.trimmedDataURL); this.setState({ modalOpen: true, }); }; onCancel = () => { this.setState({ modalOpen: false, }); }; onClear = () => { this.signCanvas.clear(); }; render() { const { id, disabled, viewOrEdit, value } = this.props; const { molFile, trimmedDataURL } = this.state; const { translate } = this.context; return ( {trimmedDataURL ? ( ) : ( "" )} {viewOrEdit == "edit" ? ( ) : ( "" )} { this.onCancel(); }} footer={ disabled ? [ , ] : [ , , , ] } bodyStyle={{ width: "900px" }} > { this.signCanvas = ref; }} penColor="#000" canvasProps={{ width: 860, height: 400, className: "sigCanvas", }} > ); } }