feat: 自行开发设计 ui样式优化 超管端-控制台 相关的页面
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
Copyright (C) 2025 QuantumNous
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
// Keep clipboard access in one place so HTTP deployments can use the fallback.
|
||||
export async function copyText(text) {
|
||||
if (text === null || text === undefined) return false;
|
||||
const value = String(text);
|
||||
try {
|
||||
if (globalThis.navigator?.clipboard?.writeText) {
|
||||
await globalThis.navigator.clipboard.writeText(value);
|
||||
return true;
|
||||
}
|
||||
} catch {
|
||||
// Permission denial can still allow a copy initiated by a user gesture.
|
||||
}
|
||||
|
||||
const doc = globalThis.document;
|
||||
if (!doc?.body || typeof doc.execCommand !== 'function') return false;
|
||||
const activeElement = doc.activeElement;
|
||||
const selection = doc.getSelection?.();
|
||||
const ranges = [];
|
||||
for (let i = 0; i < (selection?.rangeCount || 0); i++) {
|
||||
ranges.push(selection.getRangeAt(i).cloneRange());
|
||||
}
|
||||
const inputSelection =
|
||||
typeof activeElement?.selectionStart === 'number'
|
||||
? [
|
||||
activeElement.selectionStart,
|
||||
activeElement.selectionEnd,
|
||||
activeElement.selectionDirection,
|
||||
]
|
||||
: null;
|
||||
let textarea;
|
||||
try {
|
||||
textarea = doc.createElement('textarea');
|
||||
textarea.value = value;
|
||||
textarea.readOnly = true;
|
||||
textarea.tabIndex = -1;
|
||||
Object.assign(textarea.style, {
|
||||
position: 'fixed',
|
||||
top: '0',
|
||||
left: '0',
|
||||
width: '1px',
|
||||
height: '1px',
|
||||
opacity: '0',
|
||||
fontSize: '16px',
|
||||
pointerEvents: 'none',
|
||||
});
|
||||
// Modal focus traps must allow the temporary input to receive selection.
|
||||
const container = activeElement?.closest?.('[role="dialog"]') || doc.body;
|
||||
container.appendChild(textarea);
|
||||
textarea.focus({ preventScroll: true });
|
||||
textarea.select();
|
||||
textarea.setSelectionRange(0, value.length);
|
||||
return doc.execCommand('copy') === true;
|
||||
} catch {
|
||||
return false;
|
||||
} finally {
|
||||
textarea?.remove();
|
||||
try {
|
||||
activeElement?.focus?.({ preventScroll: true });
|
||||
if (inputSelection) activeElement.setSelectionRange(...inputSelection);
|
||||
if (selection && ranges.length) {
|
||||
selection.removeAllRanges();
|
||||
ranges.forEach((range) => selection.addRange(range));
|
||||
}
|
||||
} catch {
|
||||
// The original field may have unmounted while Clipboard API was pending.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Promise-based callers keep their existing success/error callbacks.
|
||||
export async function writeClipboardText(text) {
|
||||
if (!(await copyText(text))) throw new Error('Clipboard copy failed');
|
||||
}
|
||||
|
||||
export async function writeClipboardImage(blob) {
|
||||
const clipboard = globalThis.navigator?.clipboard;
|
||||
const Item = globalThis.ClipboardItem;
|
||||
if (!clipboard?.write || !Item)
|
||||
throw new Error('Image clipboard unavailable');
|
||||
let png = blob;
|
||||
if (blob.type !== 'image/png') {
|
||||
// Clipboard image support is PNG-only in common browsers; relabeling a JPEG
|
||||
// blob as PNG causes write() to reject it, so convert the bytes as well.
|
||||
const bitmap = await globalThis.createImageBitmap(blob);
|
||||
try {
|
||||
const canvas = document.createElement('canvas');
|
||||
canvas.width = bitmap.width;
|
||||
canvas.height = bitmap.height;
|
||||
canvas.getContext('2d').drawImage(bitmap, 0, 0);
|
||||
png = await new Promise((resolve, reject) => {
|
||||
canvas.toBlob(
|
||||
(result) =>
|
||||
result
|
||||
? resolve(result)
|
||||
: reject(new Error('Image conversion failed')),
|
||||
'image/png',
|
||||
);
|
||||
});
|
||||
} finally {
|
||||
bitmap.close();
|
||||
}
|
||||
}
|
||||
await clipboard.write([new Item({ 'image/png': png })]);
|
||||
}
|
||||
Reference in New Issue
Block a user