index.tsx 1.8 KB
Newer Older
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
1 2 3 4 5 6
import React from 'react';
import { Icon, Typography, Popover } from 'antd';
import styles from './index.less';
import { connect } from 'dva';
import * as H from 'history';
import { FormattedMessage } from 'umi-plugin-react/locale';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
7
import { isAntDesignPro } from '@/utils/utils';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
8 9 10 11 12 13 14 15 16

const firstUpperCase = (pathString: string) => {
  return pathString
    .replace('.', '')
    .split(/\/|\-/)
    .map(s => s.toLowerCase().replace(/( |^)[a-z]/g, L => L.toUpperCase()))
    .filter(s => s)
    .join('');
};
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

// when  click block copy, send block url to  ga
const onBlockCopy = (label: string) => {
  if (!isAntDesignPro()) {
    return;
  }
  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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
        <code className={styles['copy-block-code']}>{blockUrl}</code>
      </Typography.Paragraph>
    </div>
  );
};

type RoutingType = { location: H.Location };

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>
  );
});