2022-08-05 10:22:55 +08:00
|
|
|
import React, { useCallback, useEffect, useState } 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';
|
2022-11-09 12:28:04 +08:00
|
|
|
import { useIntl } from 'dumi';
|
2019-09-03 12:15:43 +08:00
|
|
|
import * as AntdIcons from '@ant-design/icons';
|
|
|
|
|
2022-08-04 10:04:37 +08:00
|
|
|
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
|
|
|
|
|
|
|
interface PicSearcherState {
|
2019-08-23 16:42:50 +08:00
|
|
|
loading: boolean;
|
2022-08-23 16:55:57 +08:00
|
|
|
modalOpen: boolean;
|
2019-08-23 16:42:50 +08:00
|
|
|
popoverVisible: boolean;
|
2022-08-04 10:04:37 +08:00
|
|
|
icons: iconObject[];
|
|
|
|
fileList: 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;
|
|
|
|
}
|
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
const PicSearcher: React.FC = () => {
|
|
|
|
const intl = useIntl();
|
2022-08-04 10:04:37 +08:00
|
|
|
const [state, setState] = useState<PicSearcherState>({
|
2019-08-22 17:53:31 +08:00
|
|
|
loading: false,
|
2022-08-23 16:55:57 +08:00
|
|
|
modalOpen: 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,
|
2022-08-04 10:04:37 +08:00
|
|
|
});
|
|
|
|
const predict = (imgEl: HTMLImageElement) => {
|
|
|
|
try {
|
|
|
|
let icons: any[] = window.antdIconClassifier.predict(imgEl);
|
|
|
|
if (gtag && icons.length) {
|
|
|
|
gtag('event', 'icon', {
|
|
|
|
event_category: 'search-by-image',
|
|
|
|
event_label: icons[0].className,
|
|
|
|
});
|
2019-08-22 17:53:31 +08:00
|
|
|
}
|
2022-08-04 10:04:37 +08:00
|
|
|
icons = icons.map(i => ({ score: i.score, type: i.className.replace(/\s/g, '-') }));
|
|
|
|
setState(prev => ({ ...prev, loading: false, error: false, icons }));
|
|
|
|
} catch {
|
|
|
|
setState(prev => ({ ...prev, loading: false, error: true }));
|
2019-08-22 17:53:31 +08:00
|
|
|
}
|
|
|
|
};
|
2021-10-15 11:19:14 +08:00
|
|
|
// eslint-disable-next-line class-methods-use-this
|
2022-11-09 12:28:04 +08:00
|
|
|
const toImage = (url: string): Promise<HTMLImageElement> =>
|
2020-12-09 17:12:32 +08:00
|
|
|
new Promise(resolve => {
|
2019-08-23 16:33:17 +08:00
|
|
|
const img = new Image();
|
|
|
|
img.setAttribute('crossOrigin', 'anonymous');
|
|
|
|
img.src = url;
|
2022-08-04 10:04:37 +08:00
|
|
|
img.onload = () => {
|
2019-10-25 11:30:55 +08:00
|
|
|
resolve(img);
|
2019-08-23 16:33:17 +08:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-08-05 10:22:55 +08:00
|
|
|
const uploadFile = useCallback((file: File) => {
|
2022-08-04 10:04:37 +08:00
|
|
|
setState(prev => ({ ...prev, loading: true }));
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.onload = () => {
|
|
|
|
toImage(reader.result as string).then(predict);
|
|
|
|
setState(prev => ({
|
|
|
|
...prev,
|
|
|
|
fileList: [{ uid: 1, name: file.name, status: 'done', url: reader.result }],
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
reader.readAsDataURL(file);
|
2022-08-05 10:22:55 +08:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
const onPaste = useCallback((event: ClipboardEvent) => {
|
2022-08-04 10:04:37 +08:00
|
|
|
const items = event.clipboardData && event.clipboardData.items;
|
|
|
|
let file = null;
|
|
|
|
if (items && items.length) {
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
if (items[i].type.includes('image')) {
|
|
|
|
file = items[i].getAsFile();
|
|
|
|
break;
|
|
|
|
}
|
2019-08-27 20:44:29 +08:00
|
|
|
}
|
2022-08-04 10:04:37 +08:00
|
|
|
}
|
|
|
|
if (file) {
|
|
|
|
uploadFile(file);
|
2019-08-22 19:25:32 +08:00
|
|
|
}
|
2022-08-05 10:22:55 +08:00
|
|
|
}, []);
|
|
|
|
const toggleModal = useCallback(() => {
|
2022-08-04 10:04:37 +08:00
|
|
|
setState(prev => ({
|
|
|
|
...prev,
|
2022-08-23 16:55:57 +08:00
|
|
|
modalOpen: !prev.modalOpen,
|
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
|
|
|
}
|
2022-08-05 10:22:55 +08:00
|
|
|
}, []);
|
2021-10-15 11:19:14 +08:00
|
|
|
// eslint-disable-next-line class-methods-use-this
|
2022-08-05 10:22:55 +08:00
|
|
|
const onCopied = useCallback((text: string) => {
|
2019-08-22 19:09:51 +08:00
|
|
|
message.success(
|
|
|
|
<span>
|
|
|
|
<code className="copied-code">{text}</code> copied 🎉
|
|
|
|
</span>,
|
|
|
|
);
|
2022-08-05 10:22:55 +08:00
|
|
|
}, []);
|
2022-08-04 10:04:37 +08:00
|
|
|
useEffect(() => {
|
|
|
|
const script = document.createElement('script');
|
|
|
|
script.onload = async () => {
|
|
|
|
await window.antdIconClassifier.load();
|
|
|
|
setState(prev => ({ ...prev, modelLoaded: true }));
|
|
|
|
document.addEventListener('paste', onPaste);
|
|
|
|
};
|
|
|
|
script.src = 'https://cdn.jsdelivr.net/gh/lewis617/antd-icon-classifier@0.0/dist/main.js';
|
|
|
|
document.head.appendChild(script);
|
|
|
|
setState(prev => ({ ...prev, popoverVisible: !localStorage.getItem('disableIconTip') }));
|
|
|
|
return () => {
|
|
|
|
document.removeEventListener('paste', onPaste);
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="icon-pic-searcher">
|
|
|
|
<Popover
|
2022-11-09 12:28:04 +08:00
|
|
|
content={intl.formatMessage({ id: `app.docs.components.icon.pic-searcher.intro` })}
|
2022-08-24 21:34:17 +08:00
|
|
|
open={state.popoverVisible}
|
2022-08-04 10:04:37 +08:00
|
|
|
>
|
|
|
|
<AntdIcons.CameraOutlined className="icon-pic-btn" onClick={toggleModal} />
|
|
|
|
</Popover>
|
|
|
|
<Modal
|
2022-11-09 12:28:04 +08:00
|
|
|
title={intl.formatMessage({ id: `app.docs.components.icon.pic-searcher.title` })}
|
2022-08-23 16:55:57 +08:00
|
|
|
open={state.modalOpen}
|
2022-08-04 10:04:37 +08:00
|
|
|
onCancel={toggleModal}
|
|
|
|
footer={null}
|
|
|
|
>
|
|
|
|
{state.modelLoaded || (
|
|
|
|
<Spin
|
|
|
|
spinning={!state.modelLoaded}
|
2022-11-09 12:28:04 +08:00
|
|
|
tip={intl.formatMessage({ id: 'app.docs.components.icon.pic-searcher.modelloading' })}
|
2022-08-04 10:04:37 +08:00
|
|
|
>
|
|
|
|
<div style={{ height: 100 }} />
|
|
|
|
</Spin>
|
|
|
|
)}
|
|
|
|
{state.modelLoaded && (
|
|
|
|
<Dragger
|
|
|
|
accept="image/jpeg, image/png"
|
|
|
|
listType="picture"
|
|
|
|
customRequest={o => uploadFile(o.file as File)}
|
|
|
|
fileList={state.fileList}
|
|
|
|
showUploadList={{ showPreviewIcon: false, showRemoveIcon: false }}
|
|
|
|
>
|
|
|
|
<p className="ant-upload-drag-icon">
|
|
|
|
<AntdIcons.InboxOutlined />
|
|
|
|
</p>
|
|
|
|
<p className="ant-upload-text">
|
2022-11-09 12:28:04 +08:00
|
|
|
{intl.formatMessage({ id: 'app.docs.components.icon.pic-searcher.upload-text' })}
|
2022-08-04 10:04:37 +08:00
|
|
|
</p>
|
|
|
|
<p className="ant-upload-hint">
|
2022-11-09 12:28:04 +08:00
|
|
|
{intl.formatMessage({ id: 'app.docs.components.icon.pic-searcher.upload-hint' })}
|
2022-08-04 10:04:37 +08:00
|
|
|
</p>
|
|
|
|
</Dragger>
|
|
|
|
)}
|
|
|
|
<Spin
|
|
|
|
spinning={state.loading}
|
2022-11-09 12:28:04 +08:00
|
|
|
tip={intl.formatMessage({ id: 'app.docs.components.icon.pic-searcher.matching' })}
|
2019-08-22 17:53:31 +08:00
|
|
|
>
|
2022-08-04 10:04:37 +08:00
|
|
|
<div className="icon-pic-search-result">
|
|
|
|
{state.icons.length > 0 && (
|
|
|
|
<div className="result-tip">
|
2022-11-09 12:28:04 +08:00
|
|
|
{intl.formatMessage({ id: 'app.docs.components.icon.pic-searcher.result-tip' })}
|
2022-08-04 10:04:37 +08:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<table>
|
|
|
|
{state.icons.length > 0 && (
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th className="col-icon">
|
2022-11-09 12:28:04 +08:00
|
|
|
{intl.formatMessage({ id: 'app.docs.components.icon.pic-searcher.th-icon' })}
|
|
|
|
</th>
|
|
|
|
<th>
|
|
|
|
{intl.formatMessage({ id: 'app.docs.components.icon.pic-searcher.th-score' })}
|
2022-08-04 10:04:37 +08:00
|
|
|
</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
2019-08-22 22:07:02 +08:00
|
|
|
)}
|
2022-08-04 10:04:37 +08:00
|
|
|
<tbody>
|
|
|
|
{state.icons.map(icon => {
|
|
|
|
const { type } = icon;
|
|
|
|
const iconName = `${type
|
|
|
|
.split('-')
|
|
|
|
.map(str => `${str[0].toUpperCase()}${str.slice(1)}`)
|
|
|
|
.join('')}Outlined`;
|
|
|
|
return (
|
|
|
|
<tr key={iconName}>
|
|
|
|
<td className="col-icon">
|
|
|
|
<CopyToClipboard text={`<${iconName} />`} onCopy={onCopied}>
|
|
|
|
<Tooltip title={icon.type} placement="right">
|
|
|
|
{React.createElement(allIcons[iconName])}
|
|
|
|
</Tooltip>
|
|
|
|
</CopyToClipboard>
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<Progress percent={Math.ceil(icon.score * 100)} />
|
|
|
|
</td>
|
2019-08-23 14:49:11 +08:00
|
|
|
</tr>
|
2022-08-04 10:04:37 +08:00
|
|
|
);
|
|
|
|
})}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
{state.error && (
|
|
|
|
<Result
|
|
|
|
status="500"
|
|
|
|
title="503"
|
2022-11-09 12:28:04 +08:00
|
|
|
subTitle={intl.formatMessage({
|
|
|
|
id: 'app.docs.components.icon.pic-searcher.server-error',
|
|
|
|
})}
|
2022-08-04 10:04:37 +08:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</Spin>
|
|
|
|
</Modal>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2019-08-22 17:53:31 +08:00
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
export default PicSearcher;
|