diff options
author | Fabio Berger <me@fabioberger.com> | 2018-03-02 21:40:26 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2018-03-02 21:40:26 +0800 |
commit | 67c834841ea0f8fb4d8d194c0f68802f48e764ee (patch) | |
tree | 1cea9006d0b572318d1d0784f97eee8b85a413c8 /packages/website/ts/pages/shared/markdown_link_block.tsx | |
parent | edaa0b0e34f99b0d34405eecb6aee54c1f6d7c7e (diff) | |
download | dexon-0x-contracts-67c834841ea0f8fb4d8d194c0f68802f48e764ee.tar dexon-0x-contracts-67c834841ea0f8fb4d8d194c0f68802f48e764ee.tar.gz dexon-0x-contracts-67c834841ea0f8fb4d8d194c0f68802f48e764ee.tar.bz2 dexon-0x-contracts-67c834841ea0f8fb4d8d194c0f68802f48e764ee.tar.lz dexon-0x-contracts-67c834841ea0f8fb4d8d194c0f68802f48e764ee.tar.xz dexon-0x-contracts-67c834841ea0f8fb4d8d194c0f68802f48e764ee.tar.zst dexon-0x-contracts-67c834841ea0f8fb4d8d194c0f68802f48e764ee.zip |
Update react-markdown, properly scroll to section for wiki internal links, consolidate scrollTo logic and make external links open in new tabs
Diffstat (limited to 'packages/website/ts/pages/shared/markdown_link_block.tsx')
-rw-r--r-- | packages/website/ts/pages/shared/markdown_link_block.tsx | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/packages/website/ts/pages/shared/markdown_link_block.tsx b/packages/website/ts/pages/shared/markdown_link_block.tsx new file mode 100644 index 000000000..73c447636 --- /dev/null +++ b/packages/website/ts/pages/shared/markdown_link_block.tsx @@ -0,0 +1,41 @@ +import * as _ from 'lodash'; +import * as React from 'react'; +import { configs } from 'ts/utils/configs'; +import { utils } from 'ts/utils/utils'; + +interface MarkdownLinkBlockProps { + href: string; +} + +interface MarkdownLinkBlockState {} + +export class MarkdownLinkBlock extends React.Component<MarkdownLinkBlockProps, MarkdownLinkBlockState> { + // Re-rendering a linkBlock causes any use selection to become de-selected making the link unclickable. + // We therefore noop re-renders on this component if it's props haven't changed. + public shouldComponentUpdate(nextProps: MarkdownLinkBlockProps, nextState: MarkdownLinkBlockState) { + return nextProps.href !== this.props.href; + } + public render() { + const href = this.props.href; + // If protocol is http or https, we can open in a new tab, otherwise don't for security reasons + if (_.startsWith(href, 'http') || _.startsWith(href, 'https')) { + return ( + <a href={href} target="_blank" rel="nofollow noreferrer noopener"> + {this.props.children} + </a> + ); + } else if (_.startsWith(href, '#')) { + return ( + <a style={{ cursor: 'pointer' }} onClick={this._onHashUrlClick.bind(this, href)}> + {this.props.children} + </a> + ); + } else { + return <a href={href}>{this.props.children}</a>; + } + } + private _onHashUrlClick(href: string) { + const hashWithPrefix = `#${href.split('#')[1]}`; + utils.scrollToHash(hashWithPrefix, configs.SCROLL_CONTAINER_ID); + } +} |