ant-design/site/theme/template/IconDisplay/IconPicSearcher.tsx

186 lines
5.5 KiB
TypeScript
Raw Normal View History

2019-08-22 17:53:31 +08:00
import React, { Component } from 'react';
2019-08-22 19:15:14 +08:00
import { Upload, Tooltip, Popover, Icon, Modal, Progress, message, Spin } from 'antd';
2019-08-22 18:08:20 +08:00
import CopyToClipboard from 'react-copy-to-clipboard';
2019-08-22 19:09:51 +08:00
import { injectIntl } from 'react-intl';
2019-08-22 17:53:31 +08:00
const { Dragger } = Upload;
2019-08-22 19:09:51 +08:00
interface PicSearcherProps {
intl: any;
}
2019-08-22 17:53:31 +08:00
interface PicSearcherState {
loading: Boolean;
modalVisible: Boolean;
icons: Array<string>;
fileList: Array<any>;
}
interface iconObject {
2019-08-22 18:08:20 +08:00
type: string;
2019-08-22 17:53:31 +08:00
score: number;
}
class PicSearcher extends Component<PicSearcherProps, PicSearcherState> {
state = {
loading: false,
modalVisible: false,
icons: [],
fileList: [],
};
componentDidMount() {
document.addEventListener('paste', this.onPaste);
}
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.predict(reader.result);
this.setState(() => ({
fileList: [{ uid: 1, name: file.name, status: 'done', url: reader.result }],
}));
};
reader.readAsDataURL(file);
};
predict = async (imageBase64: any) => {
2019-08-22 19:25:32 +08:00
const {
intl: { messages },
} = this.props;
2019-08-22 17:53:31 +08:00
this.setState(() => ({ loading: true }));
2019-08-22 19:25:32 +08:00
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 }));
}
2019-08-22 17:53:31 +08:00
};
toggleModal = () => {
2019-08-22 19:09:51 +08:00
this.setState(prev => ({
modalVisible: !prev.modalVisible,
fileList: [],
icons: [],
}));
2019-08-23 14:03:16 +08:00
if (!window.localStorage.getItem('disableIconTip')) {
window.localStorage.setItem('disableIconTip', 'true');
}
2019-08-22 17:53:31 +08:00
};
2019-08-22 19:09:51 +08:00
onCopied = (text: string) => {
message.success(
<span>
<code className="copied-code">{text}</code> copied 🎉
</span>,
);
2019-08-22 17:53:31 +08:00
};
render() {
2019-08-22 19:09:51 +08:00
const {
intl: { messages },
} = this.props;
2019-08-22 20:13:54 +08:00
const { modalVisible, icons, fileList, loading } = this.state;
2019-08-22 17:53:31 +08:00
return (
<div className="icon-pic-searcher">
<Popover
content={messages[`app.docs.components.icon.pic-searcher.intro`]}
2019-08-23 14:03:16 +08:00
visible={!window.localStorage.getItem('disableIconTip')}
>
2019-08-22 17:53:31 +08:00
<Icon type="camera" className="icon-pic-btn" onClick={this.toggleModal} />
2019-08-22 19:09:51 +08:00
</Popover>
2019-08-22 17:53:31 +08:00
<Modal
2019-08-22 19:09:51 +08:00
title={messages[`app.docs.components.icon.pic-searcher.title`]}
2019-08-22 17:53:31 +08:00
visible={modalVisible}
onCancel={this.toggleModal}
2019-08-23 11:08:12 +08:00
footer={null}
2019-08-22 17:53:31 +08:00
>
<Dragger
2019-08-22 20:03:59 +08:00
accept="image/jpeg, image/png"
2019-08-22 17:53:31 +08:00
listType="picture"
2019-08-22 19:09:51 +08:00
customRequest={(o: any) => this.uploadFile(o.file)}
2019-08-22 17:53:31 +08:00
fileList={fileList}
showUploadList={{ showPreviewIcon: false, showRemoveIcon: false }}
>
<p className="ant-upload-drag-icon">
<Icon type="inbox" />
</p>
2019-08-22 19:09:51 +08:00
<p className="ant-upload-text">
2019-08-23 11:08:12 +08:00
{messages['app.docs.components.icon.pic-searcher.upload-text']}
</p>
<p className="ant-upload-hint">
{messages['app.docs.components.icon.pic-searcher.upload-hint']}
2019-08-22 19:09:51 +08:00
</p>
2019-08-22 17:53:31 +08:00
</Dragger>
2019-08-22 22:07:02 +08:00
<Spin spinning={loading} tip={messages['app.docs.components.icon.pic-searcher.matching']}>
2019-08-22 19:09:51 +08:00
<div className="icon-pic-search-result">
2019-08-22 22:07:02 +08:00
{icons.length > 0 && (
<div className="result-tip">
{messages['app.docs.components.icon.pic-searcher.result-tip']}
2019-08-22 19:09:51 +08:00
</div>
2019-08-22 22:07:02 +08:00
)}
<table>
{icons.length > 0 && (
<tr>
<th className="col-icon">
{messages['app.docs.components.icon.pic-searcher.th-icon']}
</th>
<th>{messages['app.docs.components.icon.pic-searcher.th-score']}</th>
</tr>
)}
{icons.map((icon: iconObject) => (
<tr key={icon.type}>
<td className="col-icon">
<CopyToClipboard text={`<Icon type="${icon.type}" />`} onCopy={this.onCopied}>
<Tooltip title={icon.type} placement="right">
<Icon type={icon.type} />
</Tooltip>
</CopyToClipboard>
</td>
<td>
<Progress percent={Math.ceil(icon.score * 100)} />
</td>
</tr>
))}
</table>
2019-08-22 19:09:51 +08:00
</div>
</Spin>
2019-08-22 17:53:31 +08:00
</Modal>
</div>
);
}
}
2019-08-22 19:09:51 +08:00
export default injectIntl(PicSearcher);