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

import { FormattedMessage } from 'umi-plugin-react/locale';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
4 5
import React from 'react';
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 }) => {
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 44
      <Typography.Paragraph
        copyable={{
          text: blockUrl,
          onCopy: () => onBlockCopy(url),
        }}
      >
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
45 46 47 48 49 50
        <code className={styles['copy-block-code']}>{blockUrl}</code>
      </Typography.Paragraph>
    </div>
  );
};

51 52 53 54 55
interface RoutingType {
  location: {
    pathname: string;
  };
}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

export default connect(({ routing }: { routing: RoutingType }) => ({
  location: routing.location,
}))(({ location }: RoutingType) => {
  const url = location.pathname;
  return (
    <Popover
      title={<FormattedMessage id="app.preview.down.block" defaultMessage="ไธ‹่ฝฝๆญค้กต้ขๅˆฐๆœฌๅœฐ้กน็›ฎ" />}
      placement="topLeft"
      content={<BlockCodeView url={url} />}
      trigger="click"
    >
      <div className={styles['copy-block']}>
        <Icon type="download" />
      </div>
    </Popover>
  );
});