diff --git a/libs/remix-ui/clipboard/src/lib/copy-to-clipboard/copy-to-clipboard.tsx b/libs/remix-ui/clipboard/src/lib/copy-to-clipboard/copy-to-clipboard.tsx index 88d91d3867..92ef7cf768 100644 --- a/libs/remix-ui/clipboard/src/lib/copy-to-clipboard/copy-to-clipboard.tsx +++ b/libs/remix-ui/clipboard/src/lib/copy-to-clipboard/copy-to-clipboard.tsx @@ -6,33 +6,43 @@ import { Placement } from 'react-bootstrap/esm/Overlay' import './copy-to-clipboard.css' interface ICopyToClipboard { - content: any, + content?: any, tip?: string, icon?: string, direction?: Placement, className?: string, title?: string, - children?: JSX.Element + children?: JSX.Element, + getContent?: () => {} } export const CopyToClipboard = (props: ICopyToClipboard) => { - let { content, tip = 'Copy', icon = 'fa-copy', direction = 'right', children, ...otherProps } = props + let { content, tip = 'Copy', icon = 'fa-copy', direction = 'right', children, getContent, ...otherProps } = props const [message, setMessage] = useState(tip) - const handleClick = (e) => { - if (content && content !== '') { // module `copy` keeps last copied thing in the memory, so don't show tooltip if nothing is copied, because nothing was added to memory - try { - if (typeof content !== 'string') { - content = JSON.stringify(content, null, '\t') - } - copy(content) - setMessage('Copied') - } catch (e) { - console.error(e) + + const copyData = () => { + try { + if (content === '') { + setMessage('Cannot copy empty content!') + return + } + if (typeof content !== 'string') { + content = JSON.stringify(content, null, '\t') } + copy(content) + setMessage('Copied') + } catch (e) { + console.error(e) + } + } + + const handleClick = (e) => { + if (content) { // module `copy` keeps last copied thing in the memory, so don't show tooltip if nothing is copied, because nothing was added to memory + copyData() } else { - setMessage('Cannot copy empty content!') + content = getContent && getContent() + copyData() } e.preventDefault() - return false } const reset = () => { diff --git a/libs/remix-ui/run-tab/src/lib/components/contractGUI.tsx b/libs/remix-ui/run-tab/src/lib/components/contractGUI.tsx index 2006999b35..dd13a983ef 100644 --- a/libs/remix-ui/run-tab/src/lib/components/contractGUI.tsx +++ b/libs/remix-ui/run-tab/src/lib/components/contractGUI.tsx @@ -15,28 +15,8 @@ export function ContractGUI (props: ContractGUIProps) { classList: string, dataId: string }>({ title: '', content: '', classList: '', dataId: '' }) - const [clipboardContent, setClipboardContent] = useState('') const multiFields = useRef>([]) - useEffect(() => { - const multiString = getMultiValsString() - const multiJSON = JSON.parse('[' + multiString + ']') - let encodeObj - - if (props.evmBC) { - encodeObj = txFormat.encodeData(props.funcABI, multiJSON, props.evmBC) - } else { - encodeObj = txFormat.encodeData(props.funcABI, multiJSON, null) - } - if (encodeObj.error) { - console.error(encodeObj.error) - // throw new Error(encodeObj.error) - setClipboardContent(encodeObj.error) - } else { - setClipboardContent(encodeObj.data) - } - }, []) - useEffect(() => { if (props.title) { setTitle(props.title) @@ -75,6 +55,28 @@ export function ContractGUI (props: ContractGUIProps) { } }, [props.lookupOnly, props.funcABI, title]) + const getContentOnCTC = () => { + const multiString = getMultiValsString() + // copy-to-clipboard icon is only visible for method requiring input params + if (!multiString) { + return 'cannot encode empty arguments' + } + const multiJSON = JSON.parse('[' + multiString + ']') + let encodeObj + + if (props.evmBC) { + encodeObj = txFormat.encodeData(props.funcABI, multiJSON, props.evmBC) + } else { + encodeObj = txFormat.encodeData(props.funcABI, multiJSON, null) + } + if (encodeObj.error) { + console.error(encodeObj.error) + return encodeObj.error + } else { + return encodeObj.data + } + } + const switchMethodViewOn = () => { setToggleContainer(true) makeMultiVal() @@ -187,7 +189,7 @@ export function ContractGUI (props: ContractGUIProps) { })}
- +