2019-08-22 17:53:31 +08:00
|
|
|
import React, { Component } from 'react';
|
2019-09-03 12:15:43 +08:00
|
|
|
import { Upload, Tooltip, Popover, Modal, Progress, message, Spin, Result } 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-09-03 12:15:43 +08:00
|
|
|
import * as AntdIcons from '@ant-design/icons';
|
|
|
|
|
|
|
|
const allIcons: {
|
|
|
|
[key: string]: any;
|
|
|
|
} = AntdIcons;
|
2019-08-22 17:53:31 +08:00
|
|
|
|
|
|
|
const { Dragger } = Upload;
|
2019-10-25 11:30:55 +08:00
|
|
|
interface AntdIconClassifier {
|
|
|
|
load: Function;
|
|
|
|
predict: Function;
|
|
|
|
}
|
|
|
|
declare global {
|
|
|
|
interface Window {
|
|
|
|
antdIconClassifier: AntdIconClassifier;
|
|
|
|
}
|
|
|
|
}
|
2019-08-22 17:53:31 +08:00
|
|
|
|
2019-08-22 19:09:51 +08:00
|
|
|
interface PicSearcherProps {
|
|
|
|
intl: any;
|
|
|
|
}
|
2019-08-22 17:53:31 +08:00
|
|
|
|
|
|
|
interface PicSearcherState {
|
2019-08-23 16:42:50 +08:00
|
|
|
loading: boolean;
|
|
|
|
modalVisible: boolean;
|
|
|
|
popoverVisible: boolean;
|
2019-08-22 17:53:31 +08:00
|
|
|
icons: Array<string>;
|
|
|
|
fileList: Array<any>;
|
2019-08-23 16:42:50 +08:00
|
|
|
error: boolean;
|
2019-10-25 11:30:55 +08:00
|
|
|
modelLoaded: boolean;
|
2019-08-22 17:53:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2019-08-23 14:49:11 +08:00
|
|
|
popoverVisible: false,
|
2019-08-22 17:53:31 +08:00
|
|
|
icons: [],
|
|
|
|
fileList: [],
|
2019-08-23 16:42:50 +08:00
|
|
|
error: false,
|
2019-10-25 11:30:55 +08:00
|
|
|
modelLoaded: false,
|
2019-08-22 17:53:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount() {
|
2019-10-25 11:30:55 +08:00
|
|
|
this.loadModel();
|
2019-08-23 14:49:11 +08:00
|
|
|
this.setState({ popoverVisible: !localStorage.getItem('disableIconTip') });
|
2019-08-22 17:53:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
document.removeEventListener('paste', this.onPaste);
|
|
|
|
}
|
|
|
|
|
2019-10-25 11:30:55 +08:00
|
|
|
loadModel = () => {
|
|
|
|
const script = document.createElement('script');
|
|
|
|
script.onload = async () => {
|
|
|
|
await window.antdIconClassifier.load();
|
|
|
|
this.setState({ modelLoaded: true });
|
|
|
|
document.addEventListener('paste', this.onPaste);
|
|
|
|
};
|
|
|
|
script.src = 'https://cdn.jsdelivr.net/gh/lewis617/antd-icon-classifier@0.0/dist/main.js';
|
|
|
|
document.head.appendChild(script);
|
|
|
|
};
|
|
|
|
|
2019-08-22 17:53:31 +08:00
|
|
|
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) => {
|
2019-10-25 11:30:55 +08:00
|
|
|
this.setState(() => ({ loading: true }));
|
2019-08-22 17:53:31 +08:00
|
|
|
const reader: FileReader = new FileReader();
|
|
|
|
reader.onload = () => {
|
2019-10-25 11:30:55 +08:00
|
|
|
this.toImage(reader.result).then(this.predict);
|
2019-08-22 17:53:31 +08:00
|
|
|
this.setState(() => ({
|
|
|
|
fileList: [{ uid: 1, name: file.name, status: 'done', url: reader.result }],
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
};
|
|
|
|
|
2020-12-09 17:12:32 +08:00
|
|
|
toImage = (url: any) =>
|
|
|
|
new Promise(resolve => {
|
2019-08-23 16:33:17 +08:00
|
|
|
const img = new Image();
|
|
|
|
img.setAttribute('crossOrigin', 'anonymous');
|
|
|
|
img.src = url;
|
2019-08-28 11:56:18 +08:00
|
|
|
img.onload = function onload() {
|
2019-10-25 11:30:55 +08:00
|
|
|
resolve(img);
|
2019-08-23 16:33:17 +08:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2019-10-25 11:30:55 +08:00
|
|
|
predict = (imgEl: any) => {
|
2019-08-22 19:25:32 +08:00
|
|
|
try {
|
2019-10-25 11:30:55 +08:00
|
|
|
let icons = window.antdIconClassifier.predict(imgEl);
|
2019-08-27 20:44:29 +08:00
|
|
|
if (gtag && icons.length >= 1) {
|
|
|
|
gtag('event', 'icon', {
|
|
|
|
event_category: 'search-by-image',
|
2019-10-25 11:30:55 +08:00
|
|
|
event_label: icons[0].className,
|
2019-08-27 20:44:29 +08:00
|
|
|
});
|
|
|
|
}
|
2019-10-25 11:30:55 +08:00
|
|
|
icons = icons.map((i: any) => ({ score: i.score, type: i.className.replace(/\s/g, '-') }));
|
2019-08-23 16:42:50 +08:00
|
|
|
this.setState(() => ({ icons, loading: false, error: false }));
|
2019-08-22 19:25:32 +08:00
|
|
|
} catch (err) {
|
2019-08-23 16:42:50 +08:00
|
|
|
this.setState(() => ({ loading: false, error: true }));
|
2019-08-22 19:25:32 +08:00
|
|
|
}
|
2019-08-22 17:53:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
toggleModal = () => {
|
2019-08-22 19:09:51 +08:00
|
|
|
this.setState(prev => ({
|
|
|
|
modalVisible: !prev.modalVisible,
|
2019-08-23 14:49:11 +08:00
|
|
|
popoverVisible: false,
|
2019-08-22 19:09:51 +08:00
|
|
|
fileList: [],
|
|
|
|
icons: [],
|
|
|
|
}));
|
2019-08-23 14:49:11 +08:00
|
|
|
if (!localStorage.getItem('disableIconTip')) {
|
2019-08-23 14:16:30 +08:00
|
|
|
localStorage.setItem('disableIconTip', 'true');
|
2019-08-23 13:27:04 +08:00
|
|
|
}
|
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-10-25 11:30:55 +08:00
|
|
|
const {
|
|
|
|
modalVisible,
|
|
|
|
popoverVisible,
|
|
|
|
icons,
|
|
|
|
fileList,
|
|
|
|
loading,
|
|
|
|
modelLoaded,
|
|
|
|
error,
|
|
|
|
} = this.state;
|
2019-08-22 17:53:31 +08:00
|
|
|
return (
|
|
|
|
<div className="icon-pic-searcher">
|
2019-08-23 13:27:04 +08:00
|
|
|
<Popover
|
|
|
|
content={messages[`app.docs.components.icon.pic-searcher.intro`]}
|
2019-08-23 14:49:11 +08:00
|
|
|
visible={popoverVisible}
|
2019-08-23 13:27:04 +08:00
|
|
|
>
|
2019-11-28 12:34:33 +08:00
|
|
|
<AntdIcons.CameraOutlined 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
|
|
|
>
|
2019-10-25 11:30:55 +08:00
|
|
|
{modelLoaded || (
|
|
|
|
<Spin
|
|
|
|
spinning={!modelLoaded}
|
|
|
|
tip={messages['app.docs.components.icon.pic-searcher.modelloading']}
|
|
|
|
>
|
|
|
|
<div style={{ height: 100 }} />
|
|
|
|
</Spin>
|
|
|
|
)}
|
|
|
|
{modelLoaded && (
|
|
|
|
<Dragger
|
|
|
|
accept="image/jpeg, image/png"
|
|
|
|
listType="picture"
|
|
|
|
customRequest={(o: any) => this.uploadFile(o.file)}
|
|
|
|
fileList={fileList}
|
|
|
|
showUploadList={{ showPreviewIcon: false, showRemoveIcon: false }}
|
|
|
|
>
|
|
|
|
<p className="ant-upload-drag-icon">
|
2019-11-28 12:34:33 +08:00
|
|
|
<AntdIcons.InboxOutlined />
|
2019-10-25 11:30:55 +08:00
|
|
|
</p>
|
|
|
|
<p className="ant-upload-text">
|
|
|
|
{messages['app.docs.components.icon.pic-searcher.upload-text']}
|
|
|
|
</p>
|
|
|
|
<p className="ant-upload-hint">
|
|
|
|
{messages['app.docs.components.icon.pic-searcher.upload-hint']}
|
|
|
|
</p>
|
|
|
|
</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 && (
|
2019-08-23 14:49:11 +08:00
|
|
|
<thead>
|
|
|
|
<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>
|
|
|
|
</thead>
|
2019-08-22 22:07:02 +08:00
|
|
|
)}
|
2019-08-23 14:49:11 +08:00
|
|
|
<tbody>
|
2019-09-03 12:15:43 +08:00
|
|
|
{icons.map((icon: iconObject) => {
|
|
|
|
const { type } = icon;
|
2020-01-16 18:19:13 +08:00
|
|
|
const iconName = `${type
|
2019-09-03 12:15:43 +08:00
|
|
|
.split('-')
|
|
|
|
.map(str => `${str[0].toUpperCase()}${str.slice(1)}`)
|
2020-01-16 18:19:13 +08:00
|
|
|
.join('')}Outlined`;
|
2019-09-03 12:15:43 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<tr key={iconName}>
|
|
|
|
<td className="col-icon">
|
|
|
|
<CopyToClipboard text={`<${iconName} />`} onCopy={this.onCopied}>
|
|
|
|
<Tooltip title={icon.type} placement="right">
|
|
|
|
{React.createElement(allIcons[iconName])}
|
|
|
|
</Tooltip>
|
|
|
|
</CopyToClipboard>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<Progress percent={Math.ceil(icon.score * 100)} />
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
);
|
|
|
|
})}
|
2019-08-23 14:49:11 +08:00
|
|
|
</tbody>
|
2019-08-22 22:07:02 +08:00
|
|
|
</table>
|
2019-08-23 16:42:50 +08:00
|
|
|
{error && (
|
|
|
|
<Result
|
|
|
|
status="500"
|
2019-08-23 16:52:33 +08:00
|
|
|
title="503"
|
2019-08-23 16:42:50 +08:00
|
|
|
subTitle={messages['app.docs.components.icon.pic-searcher.server-error']}
|
|
|
|
/>
|
|
|
|
)}
|
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);
|