diff --git a/client/src/assets/icons/copy.svg b/client/src/assets/icons/copy.svg new file mode 100644 index 000000000..0097dc17f --- /dev/null +++ b/client/src/assets/icons/copy.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/client/src/components/views/ConfigView/CustomVariable.tsx b/client/src/components/views/ConfigView/CustomVariable.tsx index 7f91c0b5b..e773ec20c 100644 --- a/client/src/components/views/ConfigView/CustomVariable.tsx +++ b/client/src/components/views/ConfigView/CustomVariable.tsx @@ -1,4 +1,4 @@ -import { Component, ReactNode } from 'react'; +import { Component, MouseEvent, ReactNode } from 'react'; import clsx from 'clsx'; import BasicVariable from './BasicVariable'; @@ -11,6 +11,9 @@ import { CustomVarState, } from '@/store/types/config'; +import { ReactComponent as CopySVG } from '@/assets/icons/copy.svg'; +import { BaseViewIconButton } from '@/components/views/BaseView'; + interface Props { name: string; path: string; @@ -41,6 +44,55 @@ class CustomVariable extends Component { } renderHelper(name: string, children: ReactNode) { + const copyConfig = (evt: MouseEvent) => { + evt.stopPropagation(); + + const value = this.props.state.__value; + if (value == null) return; + + const configStr = Object.entries(value) + .sort() + .map(([name, val]) => { + if (val.__type === 'custom') return ''; + + let str = 'public static '; + + if (val.__type === 'enum') { + const enumClass = val.__enumClass.split('.').at(-1); + str += + enumClass + + ' ' + + name + + ' = ' + + enumClass + + '.' + + val.__newValue + + ';\n'; + } else { + str += val.__type + ' ' + name + ' = ' + val.__newValue + ';\n'; + } + + return str; + }) + .join(''); + + if (window.isSecureContext) { + navigator.clipboard.writeText(configStr); + } else { + const textArea = document.createElement('textarea'); + textArea.value = configStr; + document.body.append(textArea); + textArea.select(); + try { + document.execCommand('copy'); + } catch (err) { + console.error(err); + } + document.body.removeChild(textArea); + } + return; + }; + return ( @@ -61,6 +113,12 @@ class CustomVariable extends Component {

{name}

+ + + {this.state.expanded && (