Skip to content

Commit

Permalink
fix: Fix code copy button issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Oct 20, 2022
1 parent de60a85 commit 5adef63
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/index.tsx
Expand Up @@ -12,6 +12,7 @@ import rehypePrism from 'rehype-prism-plus';
import rehypeRewrite, { getCodeString, RehypeRewriteOptions } from 'rehype-rewrite';
import { octiconLink } from './nodes/octiconLink';
import { copyElement } from './nodes/copy';
import { useCopied } from './plugins/useCopied';
import './styles/markdown.less';

import { reservedMeta } from './plugins/reservedMeta';
Expand Down Expand Up @@ -50,9 +51,10 @@ export default React.forwardRef<MarkdownPreviewRef, MarkdownPreviewProps>((props
warpperElement = {},
...other
} = props;
const mdp = React.createRef<HTMLDivElement>();
const mdp = React.useRef<HTMLDivElement>(null);
useImperativeHandle(ref, () => ({ ...props, mdp }), [mdp, props]);
const cls = `${prefixCls || ''} ${className || ''}`;
useCopied(mdp);

const rehypeRewriteHandle: RehypeRewriteOptions['rewrite'] = (node, index, parent) => {
if (node.type === 'element' && parent && parent.type === 'root' && /h(1|2|3|4|5|6)/.test(node.tagName)) {
Expand Down
13 changes: 1 addition & 12 deletions src/nodes/copy.ts
@@ -1,23 +1,12 @@
import { Element } from 'hast';
import copyTextToClipboard from '@uiw/copy-to-clipboard';

export function copyElement(str: string = ''): Element {
return {
type: 'element',
tagName: 'div',
properties: {
// @ts-ignore
onClick: (event) => {
const target = event.currentTarget || event.target;
target.classList.add('active');
copyTextToClipboard(target.dataset.code as string, function () {
setTimeout(() => {
target.classList.remove('active');
}, 2000);
});
},
'data-code': str,
class: 'copied',
'data-code': str,
},
children: [
{
Expand Down
23 changes: 23 additions & 0 deletions src/plugins/useCopied.tsx
@@ -0,0 +1,23 @@
import copyTextToClipboard from '@uiw/copy-to-clipboard';
import { useCallback, useEffect } from 'react';

export function useCopied(container: React.RefObject<HTMLDivElement>) {
const handle = useCallback((event: Event) => {
const target = (event.currentTarget || event.target) as HTMLDivElement;
target.classList.add('active');
copyTextToClipboard(target.dataset.code as string, function () {
setTimeout(() => {
target.classList.remove('active');
}, 2000);
});
}, []);
useEffect(() => {
const btns = container.current?.querySelectorAll('pre code + div.copied');
btns && Array.from(btns).forEach((elm) => elm.addEventListener('click', handle, false));
btns && Array.from(btns).forEach((elm) => elm.addEventListener('click', (evn) => {}, false));
return () => {
btns && Array.from(btns).forEach((elm) => elm.removeEventListener('click', handle));
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [container]);
}

0 comments on commit 5adef63

Please sign in to comment.