1
1
import { useState } from 'react' ;
2
+ import { useStore } from '../store' ;
3
+ import { useShowToolsStore } from '../store/ShowTools' ;
4
+ import { useSectionStore } from '../store/Section' ;
5
+ import { type TagType } from '../store/Types' ;
2
6
3
- export interface CopiedProps < T = object > extends React . SVGProps < SVGSVGElement > {
4
- show ?: boolean ;
5
- text ?: T ;
6
- onCopied ?: ( text : string , obj : T | '' ) => void ;
7
- render ?: ( props : Omit < CopiedProps < T > , 'render' > ) => JSX . Element ;
7
+ export type CopiedOption < T extends object > = {
8
+ value ?: T ;
9
+ copied : boolean ;
10
+ setCopied : React . Dispatch < React . SetStateAction < boolean > > ;
11
+ } ;
12
+
13
+ export interface CopiedProps < T extends object > extends React . SVGProps < SVGSVGElement > {
14
+ value ?: T ;
15
+ keyName : string | number ;
16
+ expandKey : string ;
8
17
}
9
18
10
- export function Copied < T > ( props : CopiedProps < T > ) {
11
- const { children, style, text = '' , onCopied, render, show, ...reset } = props ;
12
- if ( ! show ) return null ;
19
+ export const Copied = < T extends object , K extends TagType > ( props : CopiedProps < T > ) => {
20
+ const { keyName, value, expandKey, ...other } = props ;
21
+ const { onCopied, enableClipboard } = useStore ( ) ;
22
+ const showTools = useShowToolsStore ( ) ;
23
+ const isShowTools = showTools [ expandKey ] ;
13
24
const [ copied , setCopied ] = useState ( false ) ;
25
+ const { Copied : Comp = { } } = useSectionStore ( ) ;
26
+
27
+ if ( enableClipboard === false || ! isShowTools ) return null ;
28
+
14
29
const click = ( event : React . MouseEvent < SVGSVGElement , MouseEvent > ) => {
15
30
event . stopPropagation ( ) ;
16
31
let copyText = JSON . stringify (
17
- text || '' ,
32
+ value ,
18
33
( key , value ) => {
19
34
if ( typeof value === 'bigint' ) {
20
35
return value . toString ( ) ;
@@ -23,13 +38,13 @@ export function Copied<T>(props: CopiedProps<T>) {
23
38
} ,
24
39
2 ,
25
40
) ;
26
-
27
- if ( text === Infinity ) copyText = Infinity ;
28
-
41
+ if ( typeof value === 'number' && value === Infinity ) copyText = 'Infinity' ;
42
+ if ( typeof value === 'number' && isNaN ( value ) ) copyText = 'NaN' ;
43
+ if ( typeof value === 'bigint' ) copyText = value + 'n' ;
29
44
navigator . clipboard
30
45
. writeText ( copyText )
31
46
. then ( ( ) => {
32
- onCopied && onCopied ( copyText , text ) ;
47
+ onCopied && onCopied ( copyText , value ) ;
33
48
setCopied ( true ) ;
34
49
const timer = setTimeout ( ( ) => {
35
50
setCopied ( false ) ;
@@ -38,29 +53,29 @@ export function Copied<T>(props: CopiedProps<T>) {
38
53
} )
39
54
. catch ( ( error ) => { } ) ;
40
55
} ;
41
- const defalutStyle = { ...style , cursor : 'pointer' , verticalAlign : 'middle' , marginLeft : 5 } as React . CSSProperties ;
42
56
const svgProps : React . SVGProps < SVGSVGElement > = {
43
- height : '1em' ,
44
- width : '1em' ,
45
- fill : 'var(--w-rjv-copied-color, currentColor)' ,
57
+ fill : copied ? 'var(--w-rjv-copied-success-color, #28a745)' : 'var(--w-rjv-copied-color, currentColor)' ,
46
58
onClick : click ,
47
- style : defalutStyle ,
48
- className : 'w-rjv-copied' ,
49
- ...reset ,
50
59
} ;
51
- if ( render ) return render ( { ...props , ...svgProps } ) ;
60
+ const { as, render, ...reset } = Comp ;
61
+
62
+ const elmProps : React . SVGProps < SVGSVGElement > = { ...reset , ...other , ...svgProps } as React . SVGProps < SVGSVGElement > ;
63
+ const isRender = render && typeof render === 'function' ;
64
+ const child =
65
+ isRender && render ( { ...elmProps , 'data-copied' : copied } as React . HTMLAttributes < K > , { value, keyName } ) ;
66
+ if ( child ) return child ;
52
67
if ( copied ) {
53
68
return (
54
- < svg viewBox = "0 0 32 36" { ...svgProps } fill = "var(--w-rjv-copied-success-color, #28a745)" >
55
- < path d = "M27.5,33 L2.5,33 L2.5,12.5 L27.5,12.5 L27.5,15.2249049 C29.1403264,13.8627542 29.9736597,13.1778155 30,13.1700887 C30,11.9705278 30,10.0804982 30,7.5 C30,6.1 28.9,5 27.5,5 L20,5 C20,2.2 17.8,0 15,0 C12.2,0 10,2.2 10,5 L2.5,5 C1.1,5 0,6.1 0,7.5 L0,33 C0,34.4 1.1,36 2.5,36 L27.5,36 C28.9,36 30,34.4 30,33 L30,26.1114493 L27.5,28.4926435 L27.5,33 Z M7.5,7.5 L10,7.5 C10,7.5 12.5,6.4 12.5,5 C12.5,3.6 13.6,2.5 15,2.5 C16.4,2.5 17.5,3.6 17.5,5 C17.5,6.4 18.8,7.5 20,7.5 L22.5,7.5 C22.5,7.5 25,8.6 25,10 L5,10 C5,8.5 6.1,7.5 7.5,7.5 Z M5,27.5 L10,27.5 L10,25 L5,25 L5,27.5 Z M28.5589286,16 L32,19.6 L21.0160714,30.5382252 L13.5303571,24.2571429 L17.1303571,20.6571429 L21.0160714,24.5428571 L28.5589286,16 Z M17.5,15 L5,15 L5,17.5 L17.5,17.5 L17.5,15 Z M10,20 L5,20 L5,22.5 L10,22.5 L10,20 Z" > </ path >
69
+ < svg viewBox = "0 0 32 36" { ...elmProps } >
70
+ < path d = "M27.5,33 L2.5,33 L2.5,12.5 L27.5,12.5 L27.5,15.2249049 C29.1403264,13.8627542 29.9736597,13.1778155 30,13.1700887 C30,11.9705278 30,10.0804982 30,7.5 C30,6.1 28.9,5 27.5,5 L20,5 C20,2.2 17.8,0 15,0 C12.2,0 10,2.2 10,5 L2.5,5 C1.1,5 0,6.1 0,7.5 L0,33 C0,34.4 1.1,36 2.5,36 L27.5,36 C28.9,36 30,34.4 30,33 L30,26.1114493 L27.5,28.4926435 L27.5,33 Z M7.5,7.5 L10,7.5 C10,7.5 12.5,6.4 12.5,5 C12.5,3.6 13.6,2.5 15,2.5 C16.4,2.5 17.5,3.6 17.5,5 C17.5,6.4 18.8,7.5 20,7.5 L22.5,7.5 C22.5,7.5 25,8.6 25,10 L5,10 C5,8.5 6.1,7.5 7.5,7.5 Z M5,27.5 L10,27.5 L10,25 L5,25 L5,27.5 Z M28.5589286,16 L32,19.6 L21.0160714,30.5382252 L13.5303571,24.2571429 L17.1303571,20.6571429 L21.0160714,24.5428571 L28.5589286,16 Z M17.5,15 L5,15 L5,17.5 L17.5,17.5 L17.5,15 Z M10,20 L5,20 L5,22.5 L10,22.5 L10,20 Z" / >
56
71
</ svg >
57
72
) ;
58
73
}
59
74
return (
60
- < svg viewBox = "0 0 32 36" { ...svgProps } >
61
- < path d = "M27.5,33 L2.5,33 L2.5,12.5 L27.5,12.5 L27.5,20 L30,20 L30,7.5 C30,6.1 28.9,5 27.5,5 L20,5 C20,2.2 17.8,0 15,0 C12.2,0 10,2.2 10,5 L2.5,5 C1.1,5 0,6.1 0,7.5 L0,33 C0,34.4 1.1,36 2.5,36 L27.5,36 C28.9,36 30,34.4 30,33 L30,29 L27.5,29 L27.5,33 Z M7.5,7.5 L10,7.5 C10,7.5 12.5,6.4 12.5,5 C12.5,3.6 13.6,2.5 15,2.5 C16.4,2.5 17.5,3.6 17.5,5 C17.5,6.4 18.8,7.5 20,7.5 L22.5,7.5 C22.5,7.5 25,8.6 25,10 L5,10 C5,8.5 6.1,7.5 7.5,7.5 Z M5,27.5 L10,27.5 L10,25 L5,25 L5,27.5 Z M22.5,21.5 L22.5,16.5 L12.5,24 L22.5,31.5 L22.5,26.5 L32,26.5 L32,21.5 L22.5,21.5 Z M17.5,15 L5,15 L5,17.5 L17.5,17.5 L17.5,15 Z M10,20 L5,20 L5,22.5 L10,22.5 L10,20 Z" > </ path >
75
+ < svg viewBox = "0 0 32 36" { ...elmProps } >
76
+ < path d = "M27.5,33 L2.5,33 L2.5,12.5 L27.5,12.5 L27.5,20 L30,20 L30,7.5 C30,6.1 28.9,5 27.5,5 L20,5 C20,2.2 17.8,0 15,0 C12.2,0 10,2.2 10,5 L2.5,5 C1.1,5 0,6.1 0,7.5 L0,33 C0,34.4 1.1,36 2.5,36 L27.5,36 C28.9,36 30,34.4 30,33 L30,29 L27.5,29 L27.5,33 Z M7.5,7.5 L10,7.5 C10,7.5 12.5,6.4 12.5,5 C12.5,3.6 13.6,2.5 15,2.5 C16.4,2.5 17.5,3.6 17.5,5 C17.5,6.4 18.8,7.5 20,7.5 L22.5,7.5 C22.5,7.5 25,8.6 25,10 L5,10 C5,8.5 6.1,7.5 7.5,7.5 Z M5,27.5 L10,27.5 L10,25 L5,25 L5,27.5 Z M22.5,21.5 L22.5,16.5 L12.5,24 L22.5,31.5 L22.5,26.5 L32,26.5 L32,21.5 L22.5,21.5 Z M17.5,15 L5,15 L5,17.5 L17.5,17.5 L17.5,15 Z M10,20 L5,20 L5,22.5 L10,22.5 L10,20 Z" / >
62
77
</ svg >
63
78
) ;
64
- }
79
+ } ;
65
80
66
81
Copied . displayName = 'JVR.Copied' ;
0 commit comments