fix copy heading/hash
This commit is contained in:
parent
88b0ab3087
commit
5af0920035
|
@ -10,14 +10,10 @@ import { visit } from 'unist-util-visit'
|
||||||
import reactStringReplace from 'react-string-replace'
|
import reactStringReplace from 'react-string-replace'
|
||||||
import React, { useEffect, useState } from 'react'
|
import React, { useEffect, useState } from 'react'
|
||||||
import GithubSlugger from 'github-slugger'
|
import GithubSlugger from 'github-slugger'
|
||||||
import Link from '../svgs/link.svg'
|
import LinkIcon from '../svgs/link.svg'
|
||||||
|
import Thumb from '../svgs/thumb-up-fill.svg'
|
||||||
import {toString} from 'mdast-util-to-string'
|
import {toString} from 'mdast-util-to-string'
|
||||||
|
import copy from 'clipboard-copy'
|
||||||
function copyToClipboard (id) {
|
|
||||||
if (navigator && navigator.clipboard && navigator.clipboard.writeText)
|
|
||||||
return navigator.clipboard.writeText(str);
|
|
||||||
return Promise.reject('The Clipboard API is not available.');
|
|
||||||
}
|
|
||||||
|
|
||||||
function myRemarkPlugin () {
|
function myRemarkPlugin () {
|
||||||
return (tree) => {
|
return (tree) => {
|
||||||
|
@ -39,17 +35,28 @@ function myRemarkPlugin () {
|
||||||
|
|
||||||
|
|
||||||
function Heading ({ h, slugger, noFragments, topLevel, children, node, ...props }) {
|
function Heading ({ h, slugger, noFragments, topLevel, children, node, ...props }) {
|
||||||
const id = noFragments ? undefined : slugger.slug(toString(node).replace(/[^\w\-\s]+/gi, ''))
|
const [copied, setCopied] = useState(false)
|
||||||
|
const [id] = useState(noFragments ? undefined : slugger.slug(toString(node).replace(/[^\w\-\s]+/gi, '')))
|
||||||
|
|
||||||
|
const Icon = copied ? Thumb : LinkIcon
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.heading}>
|
<div className={styles.heading}>
|
||||||
{React.createElement(h, { id, ...props }, children)}
|
{React.createElement(h, { id, ...props }, children)}
|
||||||
{!noFragments && topLevel && <a className={styles.headingLink} href={`#${id}`}><Link
|
{!noFragments && topLevel &&
|
||||||
onClick={() => copyToClipboard(id)}
|
<a className={`${styles.headingLink} ${copied ? styles.copied : ''}`} href={`#${id}`}>
|
||||||
width={18}
|
<Icon
|
||||||
height={18}
|
onClick={() => {
|
||||||
className='fill-grey'
|
const location = new URL(window.location)
|
||||||
/>
|
location.hash = `${id}`
|
||||||
|
copy(location.href)
|
||||||
|
setTimeout(() => setCopied(false), 1500)
|
||||||
|
setCopied(true)
|
||||||
|
}}
|
||||||
|
width={18}
|
||||||
|
height={18}
|
||||||
|
className='fill-grey'
|
||||||
|
/>
|
||||||
</a>}
|
</a>}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|
|
@ -25,6 +25,10 @@
|
||||||
height: 100%;
|
height: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.headingLink.copied {
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
|
||||||
.headingLink svg {
|
.headingLink svg {
|
||||||
align-self: center;
|
align-self: center;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue