mirror of
https://github.com/ONLYOFFICE/server.git
synced 2026-02-10 18:05:07 +08:00
Merge pull request '[fix] Implement function for clipboard copying; Fix bug 79836' (#110) from fix/79836 into release/v9.3.0
Reviewed-on: https://git.onlyoffice.com/ONLYOFFICE/server/pulls/110
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
import {useMemo} from 'react';
|
||||
import {useSelector} from 'react-redux';
|
||||
import {selectConfig, selectConfigLoading, selectConfigError} from '../../store/slices/configSlice';
|
||||
import {copyToClipboard} from '../../utils/copyToClipboard';
|
||||
import Button from '../Button/Button';
|
||||
import styles from './ConfigViewer.module.scss';
|
||||
|
||||
@ -13,10 +14,7 @@ const ConfigViewer = () => {
|
||||
return config ? JSON.stringify(config, null, 2) : '';
|
||||
}, [config]);
|
||||
|
||||
const copyToClipboard = async () => {
|
||||
if (!jsonString) return;
|
||||
await navigator.clipboard.writeText(jsonString);
|
||||
};
|
||||
const handleCopy = () => copyToClipboard(jsonString);
|
||||
|
||||
if (isLoading) {
|
||||
return <div className={styles.loading}>Loading configuration...</div>;
|
||||
@ -36,7 +34,7 @@ const ConfigViewer = () => {
|
||||
<p className={styles.description}>
|
||||
Sensitive parameters (passwords, keys, secrets) are shown as <span className={styles.redactedBadge}>REDACTED</span>.
|
||||
</p>
|
||||
<Button onClick={copyToClipboard}>Copy JSON</Button>
|
||||
<Button onClick={handleCopy}>Copy JSON</Button>
|
||||
</div>
|
||||
<div className={styles.configContent}>
|
||||
<pre className={styles.jsonPre}>{jsonString}</pre>
|
||||
|
||||
35
AdminPanel/client/src/utils/copyToClipboard.js
Normal file
35
AdminPanel/client/src/utils/copyToClipboard.js
Normal file
@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copies the given text to the clipboard.
|
||||
* Uses Clipboard API when available, falls back to DOM (execCommand).
|
||||
* @param {string} text - Text to copy.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export const copyToClipboard = async text => {
|
||||
if (!text) return;
|
||||
const copyViaDOM = () => {
|
||||
const textarea = document.createElement('textarea');
|
||||
textarea.value = text;
|
||||
textarea.style.position = 'fixed';
|
||||
textarea.style.left = '-9999px';
|
||||
textarea.setAttribute('readonly', '');
|
||||
document.body.appendChild(textarea);
|
||||
textarea.focus();
|
||||
textarea.setSelectionRange(0, text.length);
|
||||
let copied = false;
|
||||
try {
|
||||
copied = document.execCommand('copy');
|
||||
} finally {
|
||||
document.body.removeChild(textarea);
|
||||
}
|
||||
if (!copied) throw new Error('Copy failed');
|
||||
};
|
||||
if (navigator.clipboard?.writeText) {
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
} catch {
|
||||
copyViaDOM();
|
||||
}
|
||||
} else {
|
||||
copyViaDOM();
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user