fix(webui): give Safari a shared viewport and clipboard foundation
Safari 17+ needs dynamic viewport units, a 16px field floor, and clipboard helpers that still work on LAN HTTP.
This commit is contained in:
8 files changed
+171
-44
No files matched your search
@@ -2,10 +2,18 @@ import type { TokenStore } from './types';
|
||||
|
||||
export function localStorageTokenStore(key: string): TokenStore {
|
||||
return {
|
||||
load: () => localStorage.getItem(key),
|
||||
load: () => {
|
||||
try {
|
||||
return localStorage.getItem(key);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
save: (token) => {
|
||||
if (token == null) localStorage.removeItem(key);
|
||||
else localStorage.setItem(key, token);
|
||||
try {
|
||||
if (token == null) localStorage.removeItem(key);
|
||||
else localStorage.setItem(key, token);
|
||||
} catch { /* private mode / storage blocked */ }
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -24,3 +24,57 @@ export function formatUptime(seconds: number): string {
|
||||
if (m || (!d && !h)) parts.push(`${m} 分`);
|
||||
return parts.join(' ');
|
||||
}
|
||||
|
||||
/** Fine desktop pointer only. iPad + mouse still reports `any-pointer: coarse`. */
|
||||
export const FINE_POINTER_QUERY =
|
||||
'(hover: hover) and (pointer: fine) and (not (any-pointer: coarse))';
|
||||
|
||||
/** Safari / iOS WebKit need the `<a>` in the document and a delayed revoke. */
|
||||
export function downloadBlob(blob: Blob, filename: string): void {
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = filename;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
||||
}
|
||||
|
||||
/** Clipboard write that still works on LAN HTTP and inside a modal focus trap. */
|
||||
/** WebKit `<input type="color">` only accepts `#rrggbb`. */
|
||||
export function toColorInputValue(raw: string, fallback = '#0ea5e9'): string {
|
||||
const v = raw.trim();
|
||||
if (/^#[0-9a-fA-F]{6}$/u.test(v)) return v;
|
||||
if (/^#[0-9a-fA-F]{3}$/u.test(v)) {
|
||||
return `#${v[1]}${v[1]}${v[2]}${v[2]}${v[3]}${v[3]}`;
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
export async function copyText(value: string): Promise<boolean> {
|
||||
try {
|
||||
if (navigator.clipboard?.writeText) {
|
||||
await navigator.clipboard.writeText(value);
|
||||
return true;
|
||||
}
|
||||
} catch { /* insecure origin / permission — fall through */ }
|
||||
try {
|
||||
const ta = document.createElement('textarea');
|
||||
ta.value = value;
|
||||
ta.setAttribute('readonly', '');
|
||||
ta.style.position = 'fixed';
|
||||
ta.style.top = '0';
|
||||
ta.style.left = '-9999px';
|
||||
ta.style.opacity = '0.01';
|
||||
const host = document.querySelector('[role="dialog"]') ?? document.body;
|
||||
host.appendChild(ta);
|
||||
ta.focus();
|
||||
ta.setSelectionRange(0, ta.value.length);
|
||||
const ok = document.execCommand('copy');
|
||||
host.removeChild(ta);
|
||||
return ok;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user