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