From: Stephen Smith Date: Fri, 5 Apr 2024 23:21:19 +0000 (-0400) Subject: 21651: Create CopyResultToClipboard to format cmd for clipboard using callback X-Git-Url: https://git.arvados.org/arvados.git/commitdiff_plain/ff1759078071c52b857afdbe64713ebf0833fae4 21651: Create CopyResultToClipboard to format cmd for clipboard using callback Performs formatting on large text only when clicked Arvados-DCO-1.1-Signed-off-by: Stephen Smith --- diff --git a/services/workbench2/src/components/copy-to-clipboard/copy-result-to-clipboard.ts b/services/workbench2/src/components/copy-to-clipboard/copy-result-to-clipboard.ts new file mode 100644 index 0000000000..129002b247 --- /dev/null +++ b/services/workbench2/src/components/copy-to-clipboard/copy-result-to-clipboard.ts @@ -0,0 +1,63 @@ +// Copyright (C) The Arvados Authors. All rights reserved. +// +// SPDX-License-Identifier: AGPL-3.0 + +import React from 'react'; +import { ReactElementLike } from 'prop-types'; +import copy from 'copy-to-clipboard'; + +interface CopyToClipboardProps { + getText: (() => string); + children: ReactElementLike; + onCopy?(text: string, result: boolean): void; + options?: { + debug?: boolean; + message?: string; + format?: string; // MIME type + }; +} + +export default class CopyResultToClipboard extends React.PureComponent { + static defaultProps = { + onCopy: undefined, + options: undefined + }; + + onClick = event => { + const { + getText, + onCopy, + children, + options + } = this.props; + + const elem = React.Children.only(children); + + const text = getText(); + + const result = copy(text, options); + + if (onCopy) { + onCopy(text, result); + } + + // Bypass onClick if it was present + if (elem && elem.props && typeof elem.props.onClick === 'function') { + elem.props.onClick(event); + } + }; + + + render() { + const { + getText: _getText, + onCopy: _onCopy, + options: _options, + children, + ...props + } = this.props; + const elem = React.Children.only(children); + + return React.cloneElement(elem, {...props, onClick: this.onClick}); + } +} diff --git a/services/workbench2/src/views/process-panel/process-cmd-card.tsx b/services/workbench2/src/views/process-panel/process-cmd-card.tsx index 488b8d7fb4..6cef09b4a8 100644 --- a/services/workbench2/src/views/process-panel/process-cmd-card.tsx +++ b/services/workbench2/src/views/process-panel/process-cmd-card.tsx @@ -21,7 +21,7 @@ import { MPVPanelProps } from 'components/multi-panel-view/multi-panel-view'; import { DefaultVirtualCodeSnippet } from 'components/default-code-snippet/default-virtual-code-snippet'; import { Process } from 'store/processes/process'; import shellescape from 'shell-escape'; -import CopyToClipboard from 'react-copy-to-clipboard'; +import CopyResultToClipboard from 'components/copy-to-clipboard/copy-result-to-clipboard'; type CssRules = 'card' | 'content' | 'title' | 'header' | 'avatar' | 'iconHeader'; @@ -83,6 +83,12 @@ export const ProcessCmdCard = withStyles(styles)( return `${indent}${line}${lineBreak}`; }; + const formatClipboardText = (command: string[]) => (): string => ( + command.map((v) => + shellescape([v]) // Escape each arg separately + ).join(' ') + ); + return ( - onCopy("Command copied to clipboard")} > - +