Skip to content

Commit 5adef63

Browse files
committedOct 20, 2022
fix: Fix code copy button issue.
1 parent de60a85 commit 5adef63

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed
 

‎src/index.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import rehypePrism from 'rehype-prism-plus';
1212
import rehypeRewrite, { getCodeString, RehypeRewriteOptions } from 'rehype-rewrite';
1313
import { octiconLink } from './nodes/octiconLink';
1414
import { copyElement } from './nodes/copy';
15+
import { useCopied } from './plugins/useCopied';
1516
import './styles/markdown.less';
1617

1718
import { reservedMeta } from './plugins/reservedMeta';
@@ -50,9 +51,10 @@ export default React.forwardRef<MarkdownPreviewRef, MarkdownPreviewProps>((props
5051
warpperElement = {},
5152
...other
5253
} = props;
53-
const mdp = React.createRef<HTMLDivElement>();
54+
const mdp = React.useRef<HTMLDivElement>(null);
5455
useImperativeHandle(ref, () => ({ ...props, mdp }), [mdp, props]);
5556
const cls = `${prefixCls || ''} ${className || ''}`;
57+
useCopied(mdp);
5658

5759
const rehypeRewriteHandle: RehypeRewriteOptions['rewrite'] = (node, index, parent) => {
5860
if (node.type === 'element' && parent && parent.type === 'root' && /h(1|2|3|4|5|6)/.test(node.tagName)) {

‎src/nodes/copy.ts

+1-12
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
11
import { Element } from 'hast';
2-
import copyTextToClipboard from '@uiw/copy-to-clipboard';
32

43
export function copyElement(str: string = ''): Element {
54
return {
65
type: 'element',
76
tagName: 'div',
87
properties: {
9-
// @ts-ignore
10-
onClick: (event) => {
11-
const target = event.currentTarget || event.target;
12-
target.classList.add('active');
13-
copyTextToClipboard(target.dataset.code as string, function () {
14-
setTimeout(() => {
15-
target.classList.remove('active');
16-
}, 2000);
17-
});
18-
},
19-
'data-code': str,
208
class: 'copied',
9+
'data-code': str,
2110
},
2211
children: [
2312
{

‎src/plugins/useCopied.tsx

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import copyTextToClipboard from '@uiw/copy-to-clipboard';
2+
import { useCallback, useEffect } from 'react';
3+
4+
export function useCopied(container: React.RefObject<HTMLDivElement>) {
5+
const handle = useCallback((event: Event) => {
6+
const target = (event.currentTarget || event.target) as HTMLDivElement;
7+
target.classList.add('active');
8+
copyTextToClipboard(target.dataset.code as string, function () {
9+
setTimeout(() => {
10+
target.classList.remove('active');
11+
}, 2000);
12+
});
13+
}, []);
14+
useEffect(() => {
15+
const btns = container.current?.querySelectorAll('pre code + div.copied');
16+
btns && Array.from(btns).forEach((elm) => elm.addEventListener('click', handle, false));
17+
btns && Array.from(btns).forEach((elm) => elm.addEventListener('click', (evn) => {}, false));
18+
return () => {
19+
btns && Array.from(btns).forEach((elm) => elm.removeEventListener('click', handle));
20+
};
21+
// eslint-disable-next-line react-hooks/exhaustive-deps
22+
}, [container]);
23+
}

0 commit comments

Comments
 (0)
Please sign in to comment.