PluginProbe
Code Block Pro – Beautiful Syntax Highlighting / 1.28.0
Code Block Pro – Beautiful Syntax Highlighting v1.28.0
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.28.0, at src/editor/components/buttons/copy/CopyButton.tsx

93 lines 2.6 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 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