import React, { Component } from 'react'; import { Upload, Tooltip, Popover, Icon, Modal, Progress, message, Spin } from 'antd'; import CopyToClipboard from 'react-copy-to-clipboard'; import { injectIntl } from 'react-intl'; const { Dragger } = Upload; interface PicSearcherProps { intl: any; } interface PicSearcherState { loading: Boolean; modalVisible: Boolean; popoverVisible: Boolean; icons: Array; fileList: Array; } interface iconObject { type: string; score: number; } class PicSearcher extends Component { state = { loading: false, modalVisible: false, popoverVisible: false, icons: [], fileList: [], }; componentDidMount() { document.addEventListener('paste', this.onPaste); this.setState({ popoverVisible: !localStorage.getItem('disableIconTip') }); } componentWillUnmount() { document.removeEventListener('paste', this.onPaste); } onPaste = (event: ClipboardEvent) => { const items = event.clipboardData && event.clipboardData.items; let file = null; if (items && items.length) { for (let i = 0; i < items.length; i += 1) { if (items[i].type.indexOf('image') !== -1) { file = items[i].getAsFile(); break; } } } if (file) this.uploadFile(file); }; uploadFile = (file: File) => { const reader: FileReader = new FileReader(); reader.onload = () => { this.downscaleImage(reader.result).then(this.predict); this.setState(() => ({ fileList: [{ uid: 1, name: file.name, status: 'done', url: reader.result }], })); }; reader.readAsDataURL(file); }; downscaleImage = (url: any) => { return new Promise(resolve => { const img = new Image(); img.setAttribute('crossOrigin', 'anonymous'); img.src = url; img.onload = function() { const scale = Math.min(1, 300 / Math.max(img.width, img.height)); const canvas = document.createElement('canvas'); canvas.width = img.width * scale; canvas.height = img.height * scale; const ctx = canvas.getContext('2d'); (ctx as CanvasRenderingContext2D).drawImage(img, 0, 0, canvas.width, canvas.height); const newDataUrl = canvas.toDataURL('image/jpeg'); resolve(newDataUrl); }; }); }; predict = async (imageBase64: any) => { const { intl: { messages }, } = this.props; this.setState(() => ({ loading: true })); try { const res = await fetch( '//1647796581073291.cn-shanghai.fc.aliyuncs.com/2016-08-15/proxy/cr-sh.cr-fc-predict__stable/cr-fc-predict/', { method: 'post', body: JSON.stringify({ modelId: 'data_icon', type: 'ic', imageBase64, }), }, ); let icons = await res.json(); icons = icons.map((i: any) => ({ score: i.score, type: i.class_name.replace(/\s/g, '-') })); this.setState(() => ({ icons, loading: false })); } catch (err) { message.error(messages['app.docs.components.icon.pic-searcher.server-error']); this.setState(() => ({ loading: false })); } }; toggleModal = () => { this.setState(prev => ({ modalVisible: !prev.modalVisible, popoverVisible: false, fileList: [], icons: [], })); if (!localStorage.getItem('disableIconTip')) { localStorage.setItem('disableIconTip', 'true'); } }; onCopied = (text: string) => { message.success( {text} copied 🎉 , ); }; render() { const { intl: { messages }, } = this.props; const { modalVisible, popoverVisible, icons, fileList, loading } = this.state; return (
this.uploadFile(o.file)} fileList={fileList} showUploadList={{ showPreviewIcon: false, showRemoveIcon: false }} >

{messages['app.docs.components.icon.pic-searcher.upload-text']}

{messages['app.docs.components.icon.pic-searcher.upload-hint']}

{icons.length > 0 && (
{messages['app.docs.components.icon.pic-searcher.result-tip']}
)} {icons.length > 0 && ( )} {icons.map((icon: iconObject) => ( ))}
{messages['app.docs.components.icon.pic-searcher.th-icon']} {messages['app.docs.components.icon.pic-searcher.th-score']}
`} onCopy={this.onCopied} >
); } } export default injectIntl(PicSearcher);