File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 }
Original file line number Diff line number Diff line change 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 : -.4 em ;
84- right : -.4 em ;
85+ top : .6 em ;
86+ right : .6 em ;
8587
8688 width : 24px ;
8789 height : 24px ;
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
Original file line number Diff line number Diff line change @@ -6,20 +6,14 @@ import rehypeToc from '@jsdevtools/rehype-toc';
66import rehypeSlug from 'rehype-slug' ;
77import rehypeAutolinkHeadings from 'rehype-autolink-headings' ;
88import { 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 } ,
You can’t perform that action at this time.
0 commit comments