What every app's page runtime entry did by hand (moved from wayBetter for X's inject/entry.tsx): - siteRuntime.ts: pageInvoke (the IPC bridge read at call time), createFeatureFlags (parts that install on enable and dispose on disable, the flags native code delivers, a deck column's overrides on top of them, recorded at once and applied when <html> exists) and installSiteRuntime (the runtime object with the members native code calls, the plugin API views, developer-mode errors, its window names, the queued native deliveries, the command report). - pageStrings.ts: the page's language (English built in, deliveries checked as data, the bundled-plugin namespace rule). - panelButton.ts: the way into the panel from the page (the site's own navigation entry when the adapter mounts one, else a floating button that hides under the site's overlays and sits above its bottom bar). - commandReport.ts: the palette command report and developer-mode start errors. - tests/siteRuntime.test.ts.
97 lines
4.5 KiB
TypeScript
97 lines
4.5 KiB
TypeScript
// The way from a site page into the app's panel. The site adapter puts an entry into the site's own
|
|
// navigation when it has one (`mountNav`: X clones its "More" item); where the page draws no
|
|
// navigation (a full-pane viewer opened by URL, signed-out pages, a mobile layout) a small floating
|
|
// button stands in. The button must never cover the site's own UI: it hides while the site layers
|
|
// something over the page and sits above the site's bottom bar. Both are re-checked on every DOM
|
|
// change through the shared DOM hub.
|
|
|
|
import { watchDom, type DomWindow } from './domHub.ts';
|
|
|
|
/** Gap between the button and the pane edge or the site's bottom bar. */
|
|
export const BUTTON_GAP = 16;
|
|
|
|
/**
|
|
* Pure: the button's `bottom` offset. `barTop` is the top edge of the site's bottom bar, null when
|
|
* it has none; a zero-height or implausibly tall bar (a desktop layout may keep an empty one at the
|
|
* bottom edge) adds nothing.
|
|
*/
|
|
export function buttonBottom(barTop: number | null, viewportHeight: number, gap = BUTTON_GAP): number {
|
|
if (barTop === null) return gap;
|
|
const bar = viewportHeight - barTop;
|
|
return bar > 0 && bar < viewportHeight / 3 ? Math.round(bar) + gap : gap;
|
|
}
|
|
|
|
export type PanelButtonSite = {
|
|
/** The floating button's DOM id. */
|
|
id: string;
|
|
/** The app's logo (an image URL, usually a data URI: the site's CSP may block others). */
|
|
logo: string;
|
|
/** The button's tooltip and accessible name, in the owner's language. */
|
|
title(): string;
|
|
/** Puts the entry into the site's own navigation (re-attaching it when the site re-renders); true while it is there. */
|
|
mountNav?(open: () => void): boolean;
|
|
/** Pages where the button stays hidden (a full-pane photo or video viewer whose actions sit at the bottom). */
|
|
hiddenOn?(pathname: string): boolean;
|
|
/** The site is showing a modal sheet or a dialog layer: the button hides under it. */
|
|
overlaySelector?: string;
|
|
/** The site's bottom bar (a mobile layout): the button sits above it. */
|
|
bottomBarSelector?: string;
|
|
};
|
|
|
|
/** Pure: whether the button stays hidden on this route / with a site overlay open. */
|
|
export function buttonHidden(site: Pick<PanelButtonSite, 'hiddenOn'>, pathname: string, overlayOpen: boolean): boolean {
|
|
return overlayOpen || !!site.hiddenOn?.(pathname);
|
|
}
|
|
|
|
/**
|
|
* Keeps the panel entry on the page: the site's navigation item when `mountNav` places one, else
|
|
* the floating button. `open` opens the panel (the runtime's `open()`).
|
|
*/
|
|
export function installPanelButton(win: Window & typeof globalThis, site: PanelButtonSite, open: () => void): { sync(): void } {
|
|
const doc = win.document;
|
|
|
|
const create = (): HTMLButtonElement => {
|
|
const button = doc.createElement('button');
|
|
button.id = site.id;
|
|
button.type = 'button';
|
|
button.title = site.title();
|
|
button.setAttribute('aria-label', button.title);
|
|
const img = doc.createElement('img');
|
|
img.src = site.logo;
|
|
img.alt = '';
|
|
Object.assign(img.style, { width: '100%', height: '100%', borderRadius: '22%', display: 'block' });
|
|
button.appendChild(img);
|
|
Object.assign(button.style, {
|
|
position: 'fixed', left: '16px', bottom: '16px', zIndex: '2147483646', width: '40px', height: '40px',
|
|
padding: '0', border: '0', background: 'transparent', cursor: 'pointer', boxShadow: '0 4px 14px rgba(0,0,0,.35)', borderRadius: '22%',
|
|
});
|
|
button.addEventListener('click', () => open());
|
|
return button;
|
|
};
|
|
|
|
const syncButton = (navMounted: boolean) => {
|
|
const existing = doc.getElementById(site.id);
|
|
if (navMounted) { existing?.remove(); return; }
|
|
const button = existing ?? create();
|
|
const overlay = site.overlaySelector ? !!doc.querySelector(site.overlaySelector) : false;
|
|
const barTop = site.bottomBarSelector ? doc.querySelector(site.bottomBarSelector)?.getBoundingClientRect().top ?? null : null;
|
|
const display = buttonHidden(site, win.location.pathname, overlay) ? 'none' : 'block';
|
|
const bottom = `${buttonBottom(barTop, win.innerHeight)}px`;
|
|
if (button.style.display !== display) button.style.display = display;
|
|
if (button.style.bottom !== bottom) button.style.bottom = bottom;
|
|
if (!existing) doc.body.appendChild(button);
|
|
};
|
|
|
|
const sync = () => {
|
|
if (!doc.body) return;
|
|
syncButton(site.mountNav?.(open) ?? false);
|
|
};
|
|
const start = () => {
|
|
sync();
|
|
// Sites re-render their navigation on route and layout changes; re-attach when the entry disappears.
|
|
watchDom(win as unknown as DomWindow, sync);
|
|
};
|
|
if (doc.readyState === 'loading') doc.addEventListener('DOMContentLoaded', start, { once: true });
|
|
else start();
|
|
return { sync };
|
|
}
|