index.tsx 2.04 KB
Newer Older
1 2 3
import { Icon, Popover, Typography } from 'antd';

import { FormattedMessage } from 'umi-plugin-react/locale';
4
import React, { useRef } from 'react';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
5
import { connect } from 'dva';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
6
import { isAntDesignPro } from '@/utils/utils';
7
import styles from './index.less';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
8

9 10
const firstUpperCase = (pathString: string): string =>
  pathString
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
11
    .replace('.', '')
12 13 14
    .split(/\/|-/)
    .map((s): string => s.toLowerCase().replace(/( |^)[a-z]/g, L => L.toUpperCase()))
    .filter((s): boolean => !!s)
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
15
    .join('');
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
16 17 18 19 20 21

// when  click block copy, send block url to  ga
const onBlockCopy = (label: string) => {
  if (!isAntDesignPro()) {
    return;
  }
22

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
23 24 25 26 27 28 29 30 31 32
  const ga = window && (window as any).ga;
  if (ga) {
    ga('send', 'event', {
      eventCategory: 'block',
      eventAction: 'copy',
      eventLabel: label,
    });
  }
};

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
33 34 35
const BlockCodeView: React.SFC<{
  url: string;
}> = ({ url }) => {
ๅๅณ's avatar
ๅๅณ committed
36
  const blockUrl = `npx umi block add ${firstUpperCase(url)} --path=${url}`;
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
37 38
  return (
    <div className={styles['copy-block-view']}>
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
39 40 41 42 43
      <Typography.Paragraph
        copyable={{
          text: blockUrl,
          onCopy: () => onBlockCopy(url),
        }}
44 45 46
        style={{
          display: 'flex',
        }}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
47
      >
48 49 50
        <pre>
          <code className={styles['copy-block-code']}>{blockUrl}</code>
        </pre>
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
51 52 53 54 55
      </Typography.Paragraph>
    </div>
  );
};

56 57 58 59 60
interface RoutingType {
  location: {
    pathname: string;
  };
}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
61 62 63 64 65

export default connect(({ routing }: { routing: RoutingType }) => ({
  location: routing.location,
}))(({ location }: RoutingType) => {
  const url = location.pathname;
66
  const divDom = useRef<HTMLDivElement>(null);
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
67 68 69 70 71 72
  return (
    <Popover
      title={<FormattedMessage id="app.preview.down.block" defaultMessage="ไธ‹่ฝฝๆญค้กต้ขๅˆฐๆœฌๅœฐ้กน็›ฎ" />}
      placement="topLeft"
      content={<BlockCodeView url={url} />}
      trigger="click"
73
      getPopupContainer={dom => (divDom.current ? divDom.current : dom)}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
74
    >
75
      <div className={styles['copy-block']} ref={divDom}>
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
76 77 78 79 80
        <Icon type="download" />
      </div>
    </Popover>
  );
});