You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
7.2 KiB

import React, { Component } from 'react';
import { Upload, Tooltip, Popover, Modal, Progress, message, Spin, Result } from 'antd';
import CopyToClipboard from 'react-copy-to-clipboard';
5 years ago
import { injectIntl } from 'react-intl';
import * as AntdIcons from '@ant-design/icons';
const allIcons: {
[key: string]: any;
} = AntdIcons;
const { Dragger } = Upload;
5 years ago
interface PicSearcherProps {
intl: any;
}
interface PicSearcherState {
loading: boolean;
modalVisible: boolean;
popoverVisible: boolean;
icons: Array<string>;
fileList: Array<any>;
error: boolean;
}
interface iconObject {
type: string;
score: number;
}
class PicSearcher extends Component<PicSearcherProps, PicSearcherState> {
state = {
loading: false,
modalVisible: false,
popoverVisible: false,
icons: [],
fileList: [],
error: false,
};
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 onload() {
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) => {
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();
if (gtag && icons.length >= 1) {
gtag('event', 'icon', {
event_category: 'search-by-image',
event_label: icons[0].class_name,
});
}
icons = icons.map((i: any) => ({ score: i.score, type: i.class_name.replace(/\s/g, '-') }));
this.setState(() => ({ icons, loading: false, error: false }));
} catch (err) {
this.setState(() => ({ loading: false, error: true }));
}
};
toggleModal = () => {
5 years ago
this.setState(prev => ({
modalVisible: !prev.modalVisible,
popoverVisible: false,
5 years ago
fileList: [],
icons: [],
}));
if (!localStorage.getItem('disableIconTip')) {
localStorage.setItem('disableIconTip', 'true');
}
};
5 years ago
onCopied = (text: string) => {
message.success(
<span>
<code className="copied-code">{text}</code> copied 🎉
</span>,
);
};
render() {
5 years ago
const {
intl: { messages },
} = this.props;
const { modalVisible, popoverVisible, icons, fileList, loading, error } = this.state;
return (
<div className="icon-pic-searcher">
<Popover
content={messages[`app.docs.components.icon.pic-searcher.intro`]}
visible={popoverVisible}
>
5 years ago
<AntdIcons.Camera className="icon-pic-btn" onClick={this.toggleModal} />
5 years ago
</Popover>
<Modal
5 years ago
title={messages[`app.docs.components.icon.pic-searcher.title`]}
visible={modalVisible}
onCancel={this.toggleModal}
footer={null}
>
<Dragger
accept="image/jpeg, image/png"
listType="picture"
5 years ago
customRequest={(o: any) => this.uploadFile(o.file)}
fileList={fileList}
showUploadList={{ showPreviewIcon: false, showRemoveIcon: false }}
>
<p className="ant-upload-drag-icon">
5 years ago
<AntdIcons.Inbox />
</p>
5 years ago
<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']}
5 years ago
</p>
</Dragger>
<Spin spinning={loading} tip={messages['app.docs.components.icon.pic-searcher.matching']}>
5 years ago
<div className="icon-pic-search-result">
{icons.length > 0 && (
<div className="result-tip">
{messages['app.docs.components.icon.pic-searcher.result-tip']}
5 years ago
</div>
)}
<table>
{icons.length > 0 && (
<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>
)}
<tbody>
{icons.map((icon: iconObject) => {
const { type } = icon;
const iconName = type
.split('-')
.map(str => `${str[0].toUpperCase()}${str.slice(1)}`)
.join('');
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>
);
})}
</tbody>
</table>
{error && (
<Result
status="500"
5 years ago
title="503"
subTitle={messages['app.docs.components.icon.pic-searcher.server-error']}
/>
)}
5 years ago
</div>
</Spin>
</Modal>
</div>
);
}
}
5 years ago
export default injectIntl(PicSearcher);