Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / ucode static checks (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Blocked by required conditions
- The personal page loads faster and costs the router less: its script and styles come compressed, stay in the browser until the next update, and the icon comes with the page instead of a request of its own - The page uses the system's own font (SF Pro on Apple devices, Segoe UI on Windows, Roboto on Android) instead of downloading one - The top of the page shows the server's name alone, so it fits on a phone - Text can be selected only where it is there to be copied (addresses, file names, the configuration), with the name field's own selection: a soft highlight with handles that glides after the pointer - Sheets scroll without a scrollbar - Red buttons (Remove) light up under the pointer, as the others do - Demo: "Demo" and GitHub are one button; on a phone it floats at the bottom of the screen, and switching scenarios no longer makes the edge fades jump
172 lines
6.2 KiB
TypeScript
172 lines
6.2 KiB
TypeScript
// What a tap answers with: a toast, a copy (and the check on the control that
|
|
// copied), a file shared or downloaded, a button held busy until the router
|
|
// answers. Toasts sit in one bottom slot, one at a time.
|
|
|
|
import { h, ic } from '../dom';
|
|
import { BOUNCY, EASE_IN, POP, calm, play } from '../motion';
|
|
import { platform } from '../state';
|
|
import { BUSY_IN, busyOff, busyOn } from './busy';
|
|
import { morphText, textOf } from './morph';
|
|
|
|
let slot: HTMLElement | null = null;
|
|
let shown: HTMLElement | null = null;
|
|
let timer = 0;
|
|
|
|
/** A toast goes from where it stands: one still rising (a new one took its place) goes from there. */
|
|
function leave(t: HTMLElement, to: Keyframe, ms: number) {
|
|
const s = getComputedStyle(t);
|
|
const a = play(t, [{ opacity: s.opacity, transform: s.transform, filter: s.filter }, to], {
|
|
duration: ms,
|
|
easing: EASE_IN,
|
|
fill: 'forwards',
|
|
});
|
|
t.getAnimations().forEach((x) => x !== a && x.cancel());
|
|
a.onfinish = () => t.remove();
|
|
}
|
|
|
|
/** One toast at a time: a new one sends the last away first. */
|
|
export function toast(msg: string, bad = false) {
|
|
slot ??= document.body.appendChild(h('div', { class: 'toasts' }));
|
|
if (shown) leave(shown, { opacity: 0, transform: 'scale(.96)', filter: 'blur(4px)' }, 140);
|
|
const t = h('div', { class: `toast${bad ? ' bad' : ''}`, role: 'status' }, ic(bad ? 'alert' : 'check'), h('span', {}, msg));
|
|
slot.appendChild(t);
|
|
shown = t;
|
|
play(
|
|
t,
|
|
[
|
|
{ opacity: 0, transform: 'translateY(18px) scale(.9)', filter: 'blur(6px)' },
|
|
{ opacity: 1, transform: 'none', filter: 'none' },
|
|
],
|
|
{
|
|
duration: BOUNCY.duration,
|
|
easing: BOUNCY.easing,
|
|
},
|
|
);
|
|
clearTimeout(timer);
|
|
timer = window.setTimeout(() => {
|
|
if (shown !== t) return;
|
|
shown = null;
|
|
leave(t, { opacity: 0, transform: 'translateY(6px) scale(.96)', filter: 'blur(4px)' }, 180);
|
|
}, 2400);
|
|
}
|
|
|
|
/** An icon that changes in place: the new one (by name, or one to put back) pops in where the old one stood. */
|
|
function swapIcon(icon: Element, to: string | Element): Element {
|
|
const next = typeof to === 'string' ? ic(to, icon.getAttribute('class') ?? 'i') : to;
|
|
icon.replaceWith(next);
|
|
play(
|
|
next,
|
|
[
|
|
{ opacity: 0, transform: 'scale(.6)', filter: 'blur(3px)' },
|
|
{ opacity: 1, transform: 'none', filter: 'none' },
|
|
],
|
|
{
|
|
duration: POP.duration,
|
|
easing: POP.easing,
|
|
},
|
|
);
|
|
return next;
|
|
}
|
|
|
|
/**
|
|
* The control that copied says so for a moment: its icon turns into a check
|
|
* and its "Copy…" label (`.lbl`, a badge's main line) into "Copied", then both
|
|
* turn back the way they came. Every control that copies with a label says
|
|
* "Copy…" (in any language), so the label is not read for the word.
|
|
*/
|
|
function copied(from: Element) {
|
|
if (from.classList.contains('copied')) return;
|
|
const icon = from.querySelector('svg.i');
|
|
const label = from.querySelector<HTMLElement>('.lbl, .b-main');
|
|
const was = label ? textOf(label) : '';
|
|
from.classList.add('copied');
|
|
const check = icon ? swapIcon(icon, 'check') : null;
|
|
if (label) morphText(label, _('Copied'));
|
|
setTimeout(() => {
|
|
if (check && icon) swapIcon(check, icon);
|
|
if (label) morphText(label, was);
|
|
from.classList.remove('copied');
|
|
}, 1600);
|
|
}
|
|
|
|
/** `from`: the control that copied, which then shows a check for a moment. */
|
|
export async function copy(text: string, what = _('Copied'), from?: EventTarget | null) {
|
|
try {
|
|
await navigator.clipboard.writeText(text);
|
|
} catch {
|
|
const ta = h('textarea', { class: 'offscreen' }, text) as HTMLTextAreaElement;
|
|
document.body.appendChild(ta);
|
|
ta.select();
|
|
const ok = document.execCommand('copy');
|
|
ta.remove();
|
|
if (!ok) return toast(_('Copying is blocked here. Select the text and copy it by hand.'), true);
|
|
}
|
|
if (from instanceof Element) copied(from);
|
|
toast(what);
|
|
}
|
|
|
|
/**
|
|
* On an iPhone a share sheet hands the file straight to the VPN app, where a
|
|
* download would land in Files. Returns the action, or null where files
|
|
* cannot be shared.
|
|
*/
|
|
export function shareFile(name: string, text: string): (() => Promise<void>) | null {
|
|
const nav = navigator as Navigator & { canShare?: (d: unknown) => boolean };
|
|
// Only iOS: there AmneziaWG, WireGuard and AmneziaVPN take a file from the
|
|
// share sheet. On Android neither app is a share target (and Chrome does not
|
|
// share .conf files), so a download goes into the app's own file picker;
|
|
// desktop browsers claim they can share files and then do nothing.
|
|
if (platform() !== 'ios') return null;
|
|
if (typeof nav.share !== 'function' || typeof File === 'undefined') return null;
|
|
const file = new File([text], name, { type: 'application/octet-stream' });
|
|
if (!nav.canShare?.({ files: [file] })) return null;
|
|
return async () => {
|
|
try {
|
|
// The file alone: with a title, "Save to Files" also writes the title as a .txt.
|
|
await nav.share({ files: [file] });
|
|
} catch (e) {
|
|
// Cancelled: nothing to do. Refused by the browser: the file still has to get out.
|
|
if ((e as Error)?.name !== 'AbortError') download(name, text);
|
|
}
|
|
};
|
|
}
|
|
|
|
export function download(name: string, text: string) {
|
|
// Not text/plain: Safari would save "name.conf.txt".
|
|
const url = URL.createObjectURL(new Blob([text], { type: 'application/octet-stream' }));
|
|
const a = h('a', { href: url, download: name, class: 'offscreen' });
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
setTimeout(() => {
|
|
URL.revokeObjectURL(url);
|
|
a.remove();
|
|
}, 1000);
|
|
}
|
|
|
|
/** The file out: the share sheet where the browser hands it to an app, a download elsewhere. */
|
|
export function sendFile(name: string, text: string) {
|
|
const share = shareFile(name, text);
|
|
if (share) void share();
|
|
else download(name, text);
|
|
}
|
|
|
|
/**
|
|
* Run an action once: its button goes busy (the orb) on the click and stays
|
|
* disabled until the router answers; a quick answer keeps the orb until it is
|
|
* all the way in.
|
|
*/
|
|
export async function once(btn: HTMLButtonElement, fn: () => Promise<void>) {
|
|
if (btn.disabled) return;
|
|
btn.disabled = true;
|
|
busyOn(btn);
|
|
const t0 = performance.now();
|
|
try {
|
|
await fn();
|
|
} finally {
|
|
const wait = calm() ? 0 : BUSY_IN - (performance.now() - t0);
|
|
if (wait > 0) await new Promise((r) => setTimeout(r, wait));
|
|
busyOff(btn);
|
|
btn.disabled = false;
|
|
}
|
|
}
|