Skip to content

Commit 9d1c8ae

Browse files
committed
Copy button
1 parent 6f27bf2 commit 9d1c8ae

4 files changed

Lines changed: 31 additions & 23 deletions

File tree

src/routes/posts/+layout.svelte

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,19 @@
3434
}
3535
}
3636
37+
let copyTimeout: number;
3738
function onCopyClick(event: MouseEvent): void {
38-
if (event.target instanceof Element && event.target.closest('.rehype-pretty-copy')) {
39-
const code = event.target.closest<HTMLElement>('code');
40-
if (code) {
41-
navigator.clipboard.writeText(code.textContent);
39+
const button = event.target;
40+
if (button instanceof Element && button.closest('.code-copy-button')) {
41+
const figure = button.closest<HTMLElement>('figure');
42+
if (figure) {
43+
navigator.clipboard.writeText(figure.textContent);
44+
45+
button.classList.add('code-copy-button_copied');
46+
clearTimeout(copyTimeout);
47+
copyTimeout = window.setTimeout(() => {
48+
button.classList.remove('code-copy-button_copied');
49+
}, 3000);
4250
}
4351
}
4452
}

src/routes/posts/copied.svg

Lines changed: 1 addition & 0 deletions
Loading

src/routes/posts/markdown.css

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,14 @@
7676
}
7777
}
7878

79-
.rehype-pretty-copy {
80-
display: none;
79+
figure[data-rehype-pretty-code-figure] {
80+
position: relative;
81+
}
8182

83+
.code-copy-button {
8284
position: absolute;
83-
top: -.4em;
84-
right: -.4em;
85+
top: .6em;
86+
right: .6em;
8587

8688
width: 24px;
8789
height: 24px;
@@ -96,11 +98,15 @@
9698
transition: opacity .3s ease-in-out;
9799
}
98100

99-
[data-rehype-pretty-code-figure]:hover .rehype-pretty-copy {
101+
.code-copy-button_copied {
102+
background-image: url(./copied.svg);
103+
}
104+
105+
[data-rehype-pretty-code-figure]:hover .code-copy-button {
100106
opacity: .4;
101107
}
102108

103-
[data-rehype-pretty-code-figure]:hover .rehype-pretty-copy:hover {
109+
[data-rehype-pretty-code-figure]:hover .code-copy-button:hover {
104110
opacity: .8;
105111
}
106112

svelte.config.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,14 @@ import rehypeToc from '@jsdevtools/rehype-toc';
66
import rehypeSlug from 'rehype-slug';
77
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
88
import { createHighlighter } from '@svelte-dev/pretty-code';
9-
import { transformerCopyButton } from '@rehype-pretty/transformers';
109

11-
function fixCopyButton() {
10+
function codeCopyButton() {
1211
return {
13-
name: 'fix-copy-button',
14-
code(node) {
15-
for (const it of node.children) {
16-
if (it.properties?.class === 'rehype-pretty-copy') {
17-
delete it.properties.onclick;
18-
}
19-
}
20-
node.children = node.children.filter(it => it.tagName !== 'style');
12+
name: 'code-copy-button',
13+
postprocess(node) {
14+
return '<div>' + node + '<button class="code-copy-button" title="Копировать"></button></div>';
2115
}
22-
};
16+
}
2317
}
2418

2519
/**
@@ -173,8 +167,7 @@ const config = {
173167
keepBackground: false,
174168
bypassInlineCode: true,
175169
transformers: [
176-
transformerCopyButton(),
177-
fixCopyButton()
170+
codeCopyButton()
178171
]
179172
})
180173
},

0 commit comments

Comments
 (0)