Debian Codegraph @

node/@patternfly/react-core/dist/js/components/ClipboardCopy/ClipboardCopy.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.ClipboardCopy = exports.ClipboardCopyVariant = exports.clipboardCopyFunc = void 0;
4 const tslib_1 = require("tslib");
5 const jsx_runtime_1 = require("react/jsx-runtime");
6 const react_1 = require("react");
7 const clipboard_copy_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/ClipboardCopy/clipboard-copy"));
8 const react_styles_1 = require("@patternfly/react-styles");
9 const Tooltip_1 = require("../Tooltip");
10 const TextInput_1 = require("../TextInput");
11 const Truncate_1 = require("../Truncate");
12 const GenerateId_1 = require("../../helpers/GenerateId/GenerateId");
13 const ClipboardCopyButton_1 = require("./ClipboardCopyButton");
14 const ClipboardCopyToggle_1 = require("./ClipboardCopyToggle");
15 const ClipboardCopyExpanded_1 = require("./ClipboardCopyExpanded");
16 const helpers_1 = require("../../helpers");
17 const clipboardCopyFunc = (_event, text) => {
18 try {
19 navigator.clipboard.writeText(text.toString());
20 }
21 catch (error) {
22 // eslint-disable-next-line no-console
23 console.warn("Clipboard API not found, this copy function will not work. This is likely because you're using an", "unsupported browser or you're not using HTTPS. \n\nIf you're a developer building an application which needs", "to support copying to the clipboard without the clipboard API, you'll have to create your own copy", 'function and pass it to the ClipboardCopy component as the onCopy prop. For more information see', 'https://developer.mozilla.org/en-US/docs/Web/API/Navigator/clipboard');
24 // eslint-disable-next-line no-console
25 console.error(error);
26 }
27 };
28 exports.clipboardCopyFunc = clipboardCopyFunc;
29 var ClipboardCopyVariant;
30 (function (ClipboardCopyVariant) {
31 ClipboardCopyVariant["inline"] = "inline";
32 ClipboardCopyVariant["expansion"] = "expansion";
33 ClipboardCopyVariant["inlineCompact"] = "inline-compact";
34 })(ClipboardCopyVariant || (exports.ClipboardCopyVariant = ClipboardCopyVariant = {}));
35 class ClipboardCopy extends react_1.Component {
36 constructor(props) {
37 super(props);
38 this.timer = null;
39 // eslint-disable-next-line @typescript-eslint/no-unused-vars
40 this.componentDidUpdate = (prevProps, prevState) => {
41 if (prevProps.children !== this.props.children) {
42 const newText = Array.isArray(this.props.children)
43 ? this.props.children.join(' ')
44 : this.props.children;
45 this.setState({ text: newText, textWhenExpanded: newText });
46 }
47 };
48 this.componentWillUnmount = () => {
49 if (this.timer) {
50 window.clearTimeout(this.timer);
51 }
52 };
53 this.expandContent = (_event) => {
54 this.setState((prevState) => ({
55 expanded: !prevState.expanded
56 }));
57 };
58 this.updateText = (event, text) => {
59 this.setState({ text });
60 this.props.onChange(event, text);
61 };
62 this.updateTextWhenExpanded = (event, text) => {
63 this.setState({ textWhenExpanded: text });
64 this.props.onChange(event, text);
65 };
66 this.render = () => {
67 const _a = this.props, {
68 /* eslint-disable @typescript-eslint/no-unused-vars */
69 isExpanded, onChange, // Don't pass to <div>
70 onInputFocus, // Don't pass to <div>
71 onInputBlur, // Don't pass to <div>
72 /* eslint-enable @typescript-eslint/no-unused-vars */
73 isReadOnly, isCode, isBlock, exitDelay, maxWidth, entryDelay, onCopy, hoverTip, clickTip, textAriaLabel, toggleAriaLabel, inputId, inputName, variant, position, className, additionalActions, truncation, ouiaId, ouiaSafe, copyAriaLabel } = _a, divProps = tslib_1.__rest(_a, ["isExpanded", "onChange", "onInputFocus", "onInputBlur", "isReadOnly", "isCode", "isBlock", "exitDelay", "maxWidth", "entryDelay", "onCopy", "hoverTip", "clickTip", "textAriaLabel", "toggleAriaLabel", "inputId", "inputName", "variant", "position", "className", "additionalActions", "truncation", "ouiaId", "ouiaSafe", "copyAriaLabel"]);
74 const textIdPrefix = 'text-input-';
75 const toggleIdPrefix = 'toggle-';
76 const contentIdPrefix = 'content-';
77 const copyableText = this.state.text;
78 const shouldTruncate = variant === ClipboardCopyVariant.inlineCompact && truncation;
79 const inlineCompactContent = shouldTruncate ? ((0, jsx_runtime_1.jsx)(Truncate_1.Truncate, Object.assign({ tooltipProps: { triggerRef: this.clipboardRef }, content: copyableText }, (typeof truncation === 'object' && truncation)))) : (copyableText);
80 return ((0, jsx_runtime_1.jsxs)("div", Object.assign({ className: (0, react_styles_1.css)(clipboard_copy_1.default.clipboardCopy, variant === ClipboardCopyVariant.inlineCompact && clipboard_copy_1.default.modifiers.inline, isBlock && clipboard_copy_1.default.modifiers.block, this.state.expanded && clipboard_copy_1.default.modifiers.expanded, shouldTruncate && clipboard_copy_1.default.modifiers.truncate, className), ref: this.clipboardRef }, (shouldTruncate && { tabIndex: 0 }), divProps, (0, helpers_1.getOUIAProps)(ClipboardCopy.displayName, ouiaId, ouiaSafe), { children: [variant === ClipboardCopyVariant.inlineCompact && ((0, jsx_runtime_1.jsx)(GenerateId_1.GenerateId, { prefix: "", children: (id) => ((0, jsx_runtime_1.jsxs)(react_1.Fragment, { children: [!isCode && ((0, jsx_runtime_1.jsx)("span", { className: (0, react_styles_1.css)(clipboard_copy_1.default.clipboardCopyText), id: `${textIdPrefix}${id}`, children: inlineCompactContent })), isCode && ((0, jsx_runtime_1.jsx)("code", { className: (0, react_styles_1.css)(clipboard_copy_1.default.clipboardCopyText, clipboard_copy_1.default.modifiers.code), id: `${textIdPrefix}${id}`, children: inlineCompactContent })), (0, jsx_runtime_1.jsxs)("span", { className: (0, react_styles_1.css)(clipboard_copy_1.default.clipboardCopyActions), children: [(0, jsx_runtime_1.jsx)("span", { className: (0, react_styles_1.css)(clipboard_copy_1.default.clipboardCopyActionsItem), children: (0, jsx_runtime_1.jsx)(ClipboardCopyButton_1.ClipboardCopyButton, { variant: "plain", exitDelay: exitDelay, entryDelay: entryDelay, maxWidth: maxWidth, position: position, id: `copy-button-${id}`, "aria-label": copyAriaLabel !== null && copyAriaLabel !== void 0 ? copyAriaLabel : hoverTip, onClick: (event) => {
81 onCopy(event, copyableText);
82 this.setState({ copied: true });
83 }, onTooltipHidden: () => this.setState({ copied: false }), hasNoPadding: true, children: this.state.copied ? clickTip : hoverTip }) }), additionalActions && additionalActions] })] })) })), variant !== ClipboardCopyVariant.inlineCompact && ((0, jsx_runtime_1.jsx)(GenerateId_1.GenerateId, { prefix: "", children: (id) => ((0, jsx_runtime_1.jsxs)(react_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)("div", { className: (0, react_styles_1.css)(clipboard_copy_1.default.clipboardCopyGroup), children: [variant === ClipboardCopyVariant.expansion && ((0, jsx_runtime_1.jsx)(ClipboardCopyToggle_1.ClipboardCopyToggle, { isExpanded: this.state.expanded, onClick: (_event) => {
84 this.expandContent(_event);
85 if (this.state.expanded) {
86 this.setState({ text: this.state.textWhenExpanded });
87 }
88 else {
89 this.setState({ textWhenExpanded: copyableText });
90 }
91 }, id: `${toggleIdPrefix}${id}`, contentId: `${contentIdPrefix}${id}`, "aria-label": toggleAriaLabel })), (0, jsx_runtime_1.jsx)(TextInput_1.TextInput, Object.assign({ readOnlyVariant: isReadOnly || this.state.expanded ? 'default' : undefined, onChange: this.updateText, value: this.state.expanded ? this.state.textWhenExpanded : copyableText, id: inputId !== null && inputId !== void 0 ? inputId : `text-input-${id}`, name: inputName, "aria-label": textAriaLabel, onFocus: onInputFocus, onBlur: onInputBlur }, (isCode && { dir: 'ltr' }))), (0, jsx_runtime_1.jsx)(ClipboardCopyButton_1.ClipboardCopyButton, { exitDelay: exitDelay, entryDelay: entryDelay, maxWidth: maxWidth, position: position, id: `copy-button-${id}`, "aria-label": copyAriaLabel !== null && copyAriaLabel !== void 0 ? copyAriaLabel : hoverTip, onClick: (event) => {
92 onCopy(event, this.state.expanded ? this.state.textWhenExpanded : copyableText);
93 this.setState({ copied: true });
94 }, onTooltipHidden: () => this.setState({ copied: false }), children: this.state.copied ? clickTip : hoverTip })] }), this.state.expanded && ((0, jsx_runtime_1.jsx)(ClipboardCopyExpanded_1.ClipboardCopyExpanded, { isReadOnly: isReadOnly, isCode: isCode, id: `content-${id}`, onChange: this.updateTextWhenExpanded, children: copyableText }))] })) }))] })));
95 };
96 const text = Array.isArray(this.props.children) ? this.props.children.join(' ') : this.props.children;
97 this.state = {
98 text,
99 expanded: this.props.isExpanded,
100 copied: false,
101 textWhenExpanded: text
102 };
103 this.clipboardRef = (0, react_1.createRef)();
104 }
105 }
106 exports.ClipboardCopy = ClipboardCopy;
107 ClipboardCopy.displayName = 'ClipboardCopy';
108 ClipboardCopy.defaultProps = {
109 hoverTip: 'Copy to clipboard',
110 clickTip: 'Successfully copied to clipboard!',
111 isReadOnly: false,
112 isExpanded: false,
113 isCode: false,
114 variant: 'inline',
115 position: Tooltip_1.TooltipPosition.top,
116 maxWidth: '150px',
117 exitDelay: 1500,
118 entryDelay: 300,
119 onCopy: exports.clipboardCopyFunc,
120 onChange: () => undefined,
121 textAriaLabel: 'Copyable input',
122 toggleAriaLabel: 'Show content',
123 additionalActions: null,
124 truncation: false,
125 ouiaSafe: true
126 };
127 //# sourceMappingURL=ClipboardCopy.js.map