| 1 |
import { __ } from '@wordpress/i18n'; |
| 2 |
import stripAnsi from 'strip-ansi'; |
| 3 |
import { Attributes } from '../../../../types'; |
| 4 |
import { Clipboard } from './Clipboard'; |
| 5 |
import { TextSimple } from './TextSimple'; |
| 6 |
import { TwoSquares } from './TwoSquares'; |
| 7 |
|
| 8 |
export const copyButtonTypes = { |
| 9 |
// Poorly named key here, but it's already there so it stays |
| 10 |
heroicons: { |
| 11 |
label: __('Clipboard', 'code-block-pro'), |
| 12 |
Component: Clipboard, |
| 13 |
}, |
| 14 |
twoSquares: { |
| 15 |
label: __('Two Squares', 'code-block-pro'), |
| 16 |
Component: TwoSquares, |
| 17 |
}, |
| 18 |
textSimple: { |
| 19 |
label: __('Text Simple', 'code-block-pro'), |
| 20 |
// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 21 |
Component: (props: any) => <TextSimple {...props} />, |
| 22 |
}, |
| 23 |
}; |
| 24 |
|
| 25 |
export const CopyButton = ({ attributes }: { attributes: Attributes }) => { |
| 26 |
const { |
| 27 |
copyButtonType, |
| 28 |
copyButtonString, |
| 29 |
copyButtonStringCopied, |
| 30 |
copyButtonUseTextarea, |
| 31 |
useDecodeURI, |
| 32 |
code, |
| 33 |
textColor, |
| 34 |
bgColor: b, |
| 35 |
headerType, |
| 36 |
} = attributes; |
| 37 |
const hasTextButton = copyButtonType === 'textSimple'; |
| 38 |
const color = hasTextButton ? b : textColor; |
| 39 |
const bgColor = hasTextButton ? textColor : undefined; |
| 40 |
const { Component } = |
| 41 |
copyButtonTypes?.[copyButtonType as keyof typeof copyButtonTypes] ?? |
| 42 |
copyButtonTypes.heroicons; |
| 43 |
const codeToCopy = stripAnsi( |
| 44 |
useDecodeURI ? encodeURIComponent(code ?? '') : (code ?? ''), |
| 45 |
); |
| 46 |
return ( |
| 47 |
<span |
| 48 |
// Using a span to prevent aggressive button styling from themes |
| 49 |
role="button" |
| 50 |
tabIndex={0} |
| 51 |
data-encoded={useDecodeURI ? true : undefined} |
| 52 |
data-code={copyButtonUseTextarea ? undefined : codeToCopy} |
| 53 |
style={{ |
| 54 |
color: color ?? 'inherit', |
| 55 |
display: 'none', |
| 56 |
backgroundColor: bgColor, |
| 57 |
}} |
| 58 |
aria-label={copyButtonString ?? __('Copy', 'code-block-pro')} |
| 59 |
data-copied-text={ |
| 60 |
hasTextButton |
| 61 |
? (copyButtonStringCopied ?? |
| 62 |
__('Copied!', 'code-block-pro')) |
| 63 |
: undefined |
| 64 |
} |
| 65 |
data-has-text-button={hasTextButton ? copyButtonType : undefined} |
| 66 |
data-inside-header-type={hasTextButton ? headerType : undefined} |
| 67 |
aria-live={hasTextButton ? 'polite' : undefined} |
| 68 |
className="code-block-pro-copy-button"> |
| 69 |
{copyButtonUseTextarea ? ( |
| 70 |
<pre |
| 71 |
className="code-block-pro-copy-button-pre" |
| 72 |
aria-hidden="true"> |
| 73 |
<textarea |
| 74 |
className="code-block-pro-copy-button-textarea" |
| 75 |
tabIndex={-1} |
| 76 |
aria-hidden="true" |
| 77 |
readOnly |
| 78 |
value={codeToCopy} |
| 79 |
/> |
| 80 |
</pre> |
| 81 |
) : null} |
| 82 |
<Component text={copyButtonString} /> |
| 83 |
</span> |
| 84 |
); |
| 85 |
}; |
| 86 |
|