PluginProbe
Code Block Pro – Beautiful Syntax Highlighting / 1.27.3
Code Block Pro – Beautiful Syntax Highlighting v1.27.3
1.27.1 1.27.2 1.27.3 1.27.4 1.27.5 1.27.6 1.27.7 1.28.0 1.3.0 1.4.0 1.5.0 1.5.1 1.5.2 1.6.0 1.7.0 1.8.0 1.9.0 1.9.1 1.9.2 1.9.3 trunk 1.1.0 1.10.0 1.11.0 1.11.1 All 63 releases
code-block-pro / src / editor / components / buttons / copy / CopyButton.tsx

CopyButton.tsx in Code Block Pro – Beautiful Syntax Highlighting 1.27.3, at src/editor/components/buttons/copy/CopyButton.tsx

81 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 <textarea
71 className="code-block-pro-copy-button-textarea"
72 aria-hidden="true"
73 readOnly
74 value={codeToCopy}
75 />
76 ) : null}
77 <Component text={copyButtonString} />
78 </span>
79 );
80 };
81