@vieo-io/ui 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +65 -5
- package/dist/app-switcher.css +1 -0
- package/dist/controls.css +5 -0
- package/dist/core/index.d.ts +10 -0
- package/dist/core/index.js +9 -0
- package/dist/core/reveal.d.ts +10 -0
- package/dist/core/reveal.js +65 -0
- package/dist/core/search.d.ts +5 -0
- package/dist/core/search.js +12 -0
- package/dist/core/settingsSearch.d.ts +21 -0
- package/dist/core/settingsSearch.js +37 -0
- package/dist/core/update/build.d.ts +48 -0
- package/dist/core/update/build.js +90 -0
- package/dist/core/update/follow.d.ts +39 -0
- package/dist/core/update/follow.js +49 -0
- package/dist/core/update/notNow.d.ts +4 -0
- package/dist/core/update/notNow.js +18 -0
- package/dist/core/update/offer.d.ts +32 -0
- package/dist/core/update/offer.js +19 -0
- package/dist/core/update/reload.d.ts +12 -0
- package/dist/core/update/reload.js +16 -0
- package/dist/core/update/types.d.ts +33 -0
- package/dist/field.css +7 -0
- package/dist/react/index.d.ts +4 -0
- package/dist/react/index.js +4 -0
- package/dist/react/settingsSearch.d.ts +35 -0
- package/dist/react/settingsSearch.js +97 -0
- package/dist/react/update/Offer.d.ts +9 -0
- package/dist/react/update/Offer.js +4 -0
- package/dist/react/update/UpdateNotice.d.ts +37 -0
- package/dist/react/update/UpdateNotice.js +141 -0
- package/dist/react/update/hooks.d.ts +9 -0
- package/dist/react/update/hooks.js +51 -0
- package/dist/search.css +83 -0
- package/dist/setting.css +26 -0
- package/dist/shell.css +31 -0
- package/dist/{AppShell.d.ts → solid/AppShell.d.ts} +7 -0
- package/dist/{AppShell.jsx → solid/AppShell.jsx} +1 -1
- package/dist/{CommandPalette.d.ts → solid/CommandPalette.d.ts} +1 -1
- package/dist/{CommandPalette.jsx → solid/CommandPalette.jsx} +5 -10
- package/dist/{Dialog.jsx → solid/Dialog.jsx} +1 -1
- package/dist/{Icon.d.ts → solid/Icon.d.ts} +1 -1
- package/dist/{Icon.jsx → solid/Icon.jsx} +1 -1
- package/dist/{Listbox.jsx → solid/Listbox.jsx} +2 -2
- package/dist/{Menu.jsx → solid/Menu.jsx} +3 -3
- package/dist/{Popover.jsx → solid/Popover.jsx} +2 -2
- package/dist/{SegmentedControl.jsx → solid/SegmentedControl.jsx} +2 -2
- package/dist/{Sidebar.jsx → solid/Sidebar.jsx} +1 -1
- package/dist/{Tabs.jsx → solid/Tabs.jsx} +2 -2
- package/dist/{TextField.jsx → solid/TextField.jsx} +1 -1
- package/dist/{Toolbar.jsx → solid/Toolbar.jsx} +1 -1
- package/dist/{Tooltip.jsx → solid/Tooltip.jsx} +1 -1
- package/dist/{Tree.d.ts → solid/Tree.d.ts} +8 -1
- package/dist/{Tree.jsx → solid/Tree.jsx} +27 -8
- package/dist/{index.d.ts → solid/index.d.ts} +1 -1
- package/dist/{index.js → solid/index.js} +1 -1
- package/dist/solid/trigger.js +1 -0
- package/dist/styles.css +2 -0
- package/dist/toolbar.css +8 -0
- package/dist/tree.css +38 -1
- package/dist/update.css +118 -0
- package/package.json +39 -8
- /package/dist/{aria.d.ts → core/aria.d.ts} +0 -0
- /package/dist/{aria.js → core/aria.js} +0 -0
- /package/dist/{layers.d.ts → core/layers.d.ts} +0 -0
- /package/dist/{layers.js → core/layers.js} +0 -0
- /package/dist/{paths.d.ts → core/paths.d.ts} +0 -0
- /package/dist/{paths.js → core/paths.js} +0 -0
- /package/dist/{roving.d.ts → core/roving.d.ts} +0 -0
- /package/dist/{roving.js → core/roving.js} +0 -0
- /package/dist/{trigger.js → core/update/types.js} +0 -0
- /package/dist/{AppSwitcher.d.ts → solid/AppSwitcher.d.ts} +0 -0
- /package/dist/{AppSwitcher.jsx → solid/AppSwitcher.jsx} +0 -0
- /package/dist/{Badge.d.ts → solid/Badge.d.ts} +0 -0
- /package/dist/{Badge.jsx → solid/Badge.jsx} +0 -0
- /package/dist/{Button.d.ts → solid/Button.d.ts} +0 -0
- /package/dist/{Button.jsx → solid/Button.jsx} +0 -0
- /package/dist/{Checkbox.d.ts → solid/Checkbox.d.ts} +0 -0
- /package/dist/{Checkbox.jsx → solid/Checkbox.jsx} +0 -0
- /package/dist/{Dialog.d.ts → solid/Dialog.d.ts} +0 -0
- /package/dist/{EmptyState.d.ts → solid/EmptyState.d.ts} +0 -0
- /package/dist/{EmptyState.jsx → solid/EmptyState.jsx} +0 -0
- /package/dist/{IconButton.d.ts → solid/IconButton.d.ts} +0 -0
- /package/dist/{IconButton.jsx → solid/IconButton.jsx} +0 -0
- /package/dist/{Kbd.d.ts → solid/Kbd.d.ts} +0 -0
- /package/dist/{Kbd.jsx → solid/Kbd.jsx} +0 -0
- /package/dist/{Listbox.d.ts → solid/Listbox.d.ts} +0 -0
- /package/dist/{Menu.d.ts → solid/Menu.d.ts} +0 -0
- /package/dist/{ObjectCard.d.ts → solid/ObjectCard.d.ts} +0 -0
- /package/dist/{ObjectCard.jsx → solid/ObjectCard.jsx} +0 -0
- /package/dist/{Popover.d.ts → solid/Popover.d.ts} +0 -0
- /package/dist/{Resizer.d.ts → solid/Resizer.d.ts} +0 -0
- /package/dist/{Resizer.jsx → solid/Resizer.jsx} +0 -0
- /package/dist/{SegmentedControl.d.ts → solid/SegmentedControl.d.ts} +0 -0
- /package/dist/{Select.d.ts → solid/Select.d.ts} +0 -0
- /package/dist/{Select.jsx → solid/Select.jsx} +0 -0
- /package/dist/{SettingRow.d.ts → solid/SettingRow.d.ts} +0 -0
- /package/dist/{SettingRow.jsx → solid/SettingRow.jsx} +0 -0
- /package/dist/{Sidebar.d.ts → solid/Sidebar.d.ts} +0 -0
- /package/dist/{Switch.d.ts → solid/Switch.d.ts} +0 -0
- /package/dist/{Switch.jsx → solid/Switch.jsx} +0 -0
- /package/dist/{Tabs.d.ts → solid/Tabs.d.ts} +0 -0
- /package/dist/{TextField.d.ts → solid/TextField.d.ts} +0 -0
- /package/dist/{Toast.d.ts → solid/Toast.d.ts} +0 -0
- /package/dist/{Toast.jsx → solid/Toast.jsx} +0 -0
- /package/dist/{Toolbar.d.ts → solid/Toolbar.d.ts} +0 -0
- /package/dist/{Tooltip.d.ts → solid/Tooltip.d.ts} +0 -0
- /package/dist/{trigger.d.ts → solid/trigger.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
# @vieo-io/ui
|
|
2
2
|
|
|
3
|
-
Vieo's
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
Vieo's shared UI: behaviour with no framework, the Solid components, React bindings, and one stylesheet for all of
|
|
4
|
+
them. Everything is styled by `@vieo-io/tokens`, so it follows whichever design, palette, and theme the root element
|
|
5
|
+
carries.
|
|
6
|
+
|
|
7
|
+
| Entry | Holds |
|
|
8
|
+
| ------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------- |
|
|
9
|
+
| `@vieo-io/ui` or `@vieo-io/ui/solid` | The Solid components: `Button`, `Icon`, `Dialog`, `Popover`, `Tooltip`, `Menu`, `Tabs`, `Listbox`, `CommandPalette`, and the rest |
|
|
10
|
+
| `@vieo-io/ui/react` | `SettingsSearch` and its row hook, and the update card |
|
|
11
|
+
| `@vieo-io/ui/core` | What the bindings share: settings search and its ranking, revealing a setting, the update flow, and `aria` |
|
|
12
|
+
| `@vieo-io/ui/styles.css` | Every style, for either binding |
|
|
6
13
|
|
|
7
14
|
```css
|
|
8
15
|
@import "@vieo-io/tokens/tokens.css";
|
|
@@ -11,6 +18,10 @@ element carries.
|
|
|
11
18
|
@import "@vieo-io/ui/styles.css";
|
|
12
19
|
```
|
|
13
20
|
|
|
21
|
+
`solid-js`, `@solidjs/web`, and `react` are optional peers. Install the ones your binding needs.
|
|
22
|
+
|
|
23
|
+
## Solid
|
|
24
|
+
|
|
14
25
|
```tsx
|
|
15
26
|
import { Button, Menu } from '@vieo-io/ui'
|
|
16
27
|
|
|
@@ -21,9 +32,58 @@ import { Button, Menu } from '@vieo-io/ui'
|
|
|
21
32
|
/>
|
|
22
33
|
```
|
|
23
34
|
|
|
24
|
-
The
|
|
25
|
-
`@solidjs/vite-plugin` adds that condition
|
|
35
|
+
The Solid entry ships JSX source under the `solid` export condition, which the app compiles with its own settings.
|
|
36
|
+
`@solidjs/vite-plugin` adds that condition, and `solid-js` and `@solidjs/web` must be 2.0.3 or later.
|
|
26
37
|
|
|
27
38
|
Components that open something take a `trigger` render function and hand it the props to spread on the trigger
|
|
28
39
|
element: its ref, ARIA state, and handlers. `Dialog` is controlled by `open` and `onOpenChange` instead, so any
|
|
29
40
|
button can open it.
|
|
41
|
+
|
|
42
|
+
## Settings search
|
|
43
|
+
|
|
44
|
+
An app lists its settings once, as `SettingItem`s: each row's `id`, `title`, and section, with an optional `hint`,
|
|
45
|
+
extra search `terms`, and `secondary` for a section's own entry, which sorts after single settings.
|
|
46
|
+
|
|
47
|
+
```tsx
|
|
48
|
+
import { SettingsSearch, SettingTargetProvider, useSettingTarget } from '@vieo-io/ui/react'
|
|
49
|
+
|
|
50
|
+
// In the settings sidebar. onOpen navigates with the app's own router, carrying item.id.
|
|
51
|
+
<SettingsSearch items={SETTING_ITEMS} onOpen={openSetting}>
|
|
52
|
+
<SectionLinks />
|
|
53
|
+
</SettingsSearch>
|
|
54
|
+
|
|
55
|
+
// Around the open section, with the id the router carried.
|
|
56
|
+
<SettingTargetProvider target={target} onReached={clearTarget}>
|
|
57
|
+
<Section />
|
|
58
|
+
</SettingTargetProvider>
|
|
59
|
+
|
|
60
|
+
// On each row.
|
|
61
|
+
<div className='setting' {...useSettingTarget('text-size')}>…</div>
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
While the box holds text, the results replace its children. Up and Down move through them, Enter or a click opens
|
|
65
|
+
one, Escape clears the box, and `/` focuses it from anywhere but a text field. Once the target row mounts, it opens
|
|
66
|
+
any closed `<details>` around it, centres, takes focus, and pulses twice, or shows its focus ring under reduced motion.
|
|
67
|
+
`searchSettings` in `/core` ranks the same list for a command palette.
|
|
68
|
+
|
|
69
|
+
## The update card
|
|
70
|
+
|
|
71
|
+
The card Vieo's apps show when something newer is ready, and the flow behind it. One press of **Refresh to update**
|
|
72
|
+
updates the server, follows the update, and reloads once into the new build.
|
|
73
|
+
|
|
74
|
+
```tsx
|
|
75
|
+
import { UpdateNotice, useStaleBuild, useUpdateStatus } from '@vieo-io/ui/react'
|
|
76
|
+
|
|
77
|
+
const stale = useStaleBuild(build)
|
|
78
|
+
const [status, readAgain] = useUpdateStatus(source, build.checkEvent)
|
|
79
|
+
return <UpdateNotice source={source} status={status} readAgain={readAgain} build={build} stale={stale} app={app} />
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
- `build` is a `BuildWatch`: the key of the build the page loaded, how to read the key the server serves now, and the
|
|
83
|
+
event that asks for a check. Keep it stable across renders.
|
|
84
|
+
- `source` is the app's `UpdateSource` over its own update endpoint, or null for a viewer who may only refresh.
|
|
85
|
+
- `app` gives the What's new link, the storage key for Not now, and optionally a confirm for when the status has
|
|
86
|
+
blockers, a step to run after an update, and the reload.
|
|
87
|
+
- `first` and `last` add the app's own offers ahead of the server's update and after the refresh.
|
|
88
|
+
|
|
89
|
+
The card's distance from the top comes from `--vieo-update-notice-top`, so each app puts it under its own bar.
|
package/dist/app-switcher.css
CHANGED
package/dist/controls.css
CHANGED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { aria } from './aria.js';
|
|
2
|
+
export { revealSetting } from './reveal.js';
|
|
3
|
+
export { matchWords, normalise } from './search.js';
|
|
4
|
+
export { type SettingItem, searchSettings, settingAnchor } from './settingsSearch.js';
|
|
5
|
+
export { type BuildWatch, checkForNewBuild, fetchJson, hourlyInView, watchForNewBuild, } from './update/build.js';
|
|
6
|
+
export { FOLLOW_FOR_MS, followUpdate, type UpdateEnd, type UpdateProgress, updateProgress, } from './update/follow.js';
|
|
7
|
+
export { isPutOff, putOff } from './update/notNow.js';
|
|
8
|
+
export { type AppOffer, type Offer, pickOffer } from './update/offer.js';
|
|
9
|
+
export { reloadWhenServing } from './update/reload.js';
|
|
10
|
+
export type { StartAnswer, UpdateOutcome, UpdateSource, UpdateStatus } from './update/types.js';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { aria } from './aria.js';
|
|
2
|
+
export { revealSetting } from './reveal.js';
|
|
3
|
+
export { matchWords, normalise } from './search.js';
|
|
4
|
+
export { searchSettings, settingAnchor } from './settingsSearch.js';
|
|
5
|
+
export { checkForNewBuild, fetchJson, hourlyInView, watchForNewBuild, } from './update/build.js';
|
|
6
|
+
export { FOLLOW_FOR_MS, followUpdate, updateProgress, } from './update/follow.js';
|
|
7
|
+
export { isPutOff, putOff } from './update/notNow.js';
|
|
8
|
+
export { pickOffer } from './update/offer.js';
|
|
9
|
+
export { reloadWhenServing } from './update/reload.js';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** The class a revealed row takes while it pulses. The stylesheet animates it. */
|
|
2
|
+
export declare const PULSE = "vds-setting-pulse";
|
|
3
|
+
/**
|
|
4
|
+
* Brings a setting's row into view: opens any closed `<details>` around it, centres it, focuses it, and pulses it.
|
|
5
|
+
* Rows that draw late above it move it, so it is centred again whenever the content grows, for a second or until the
|
|
6
|
+
* person clicks, touches, scrolls, or presses a key.
|
|
7
|
+
*/
|
|
8
|
+
export declare function revealSetting(element: HTMLElement, { reducedMotion }: {
|
|
9
|
+
reducedMotion: boolean;
|
|
10
|
+
}): void;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/** The class a revealed row takes while it pulses. The stylesheet animates it. */
|
|
2
|
+
export const PULSE = 'vds-setting-pulse';
|
|
3
|
+
/** Input that means the person has taken over the page, by pointer, touch, wheel, or keyboard. */
|
|
4
|
+
const HAND_SCROLLS = ['keydown', 'pointerdown', 'touchmove', 'wheel'];
|
|
5
|
+
/** How long a revealed row stays centred while rows above it draw. */
|
|
6
|
+
const SETTLE_MS = 1000;
|
|
7
|
+
/**
|
|
8
|
+
* Brings a setting's row into view: opens any closed `<details>` around it, centres it, focuses it, and pulses it.
|
|
9
|
+
* Rows that draw late above it move it, so it is centred again whenever the content grows, for a second or until the
|
|
10
|
+
* person clicks, touches, scrolls, or presses a key.
|
|
11
|
+
*/
|
|
12
|
+
export function revealSetting(element, { reducedMotion }) {
|
|
13
|
+
for (let parent = element.parentElement; parent; parent = parent.parentElement) {
|
|
14
|
+
if (parent instanceof HTMLDetailsElement)
|
|
15
|
+
parent.open = true;
|
|
16
|
+
}
|
|
17
|
+
const scroll = () => element.scrollIntoView({ block: 'center', behavior: reducedMotion ? 'auto' : 'smooth' });
|
|
18
|
+
scroll();
|
|
19
|
+
scrollWhileSettling(element, scroll);
|
|
20
|
+
element.focus({ preventScroll: true });
|
|
21
|
+
pulse(element);
|
|
22
|
+
}
|
|
23
|
+
function pulse(element) {
|
|
24
|
+
element.classList.remove(PULSE);
|
|
25
|
+
// Reading the layout restarts the animation when the class goes back on.
|
|
26
|
+
void element.offsetWidth;
|
|
27
|
+
element.classList.add(PULSE);
|
|
28
|
+
const end = () => {
|
|
29
|
+
element.classList.remove(PULSE);
|
|
30
|
+
element.removeEventListener('animationend', end);
|
|
31
|
+
element.removeEventListener('blur', end);
|
|
32
|
+
};
|
|
33
|
+
element.addEventListener('animationend', end);
|
|
34
|
+
element.addEventListener('blur', end);
|
|
35
|
+
}
|
|
36
|
+
function scrollerOf(element) {
|
|
37
|
+
for (let parent = element.parentElement; parent; parent = parent.parentElement) {
|
|
38
|
+
if (/auto|scroll/.test(getComputedStyle(parent).overflowY))
|
|
39
|
+
return parent;
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
function scrollWhileSettling(element, scroll) {
|
|
44
|
+
const content = scrollerOf(element)?.firstElementChild ?? element.parentElement;
|
|
45
|
+
if (!content)
|
|
46
|
+
return;
|
|
47
|
+
let first = true;
|
|
48
|
+
const resized = new ResizeObserver(() => {
|
|
49
|
+
// An observer reports the size it starts with, which needs no second scroll.
|
|
50
|
+
if (first)
|
|
51
|
+
first = false;
|
|
52
|
+
else
|
|
53
|
+
scroll();
|
|
54
|
+
});
|
|
55
|
+
resized.observe(content);
|
|
56
|
+
const stop = () => {
|
|
57
|
+
resized.disconnect();
|
|
58
|
+
clearTimeout(timer);
|
|
59
|
+
for (const type of HAND_SCROLLS)
|
|
60
|
+
removeEventListener(type, stop, true);
|
|
61
|
+
};
|
|
62
|
+
const timer = setTimeout(stop, SETTLE_MS);
|
|
63
|
+
for (const type of HAND_SCROLLS)
|
|
64
|
+
addEventListener(type, stop, true);
|
|
65
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** Whether every word of `query` appears somewhere in `fields`. An empty query matches everything. */
|
|
2
|
+
export declare function matchWords(query: string, fields: readonly string[]): boolean;
|
|
3
|
+
/** `text` lowercased, with its accents and runs of whitespace folded, so "Café Crème" reads as "cafe creme". */
|
|
4
|
+
export declare function normalise(text: string): string;
|
|
5
|
+
export declare function words(query: string): string[];
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Whether every word of `query` appears somewhere in `fields`. An empty query matches everything. */
|
|
2
|
+
export function matchWords(query, fields) {
|
|
3
|
+
const text = normalise(fields.join(' '));
|
|
4
|
+
return words(query).every((word) => text.includes(word));
|
|
5
|
+
}
|
|
6
|
+
/** `text` lowercased, with its accents and runs of whitespace folded, so "Café Crème" reads as "cafe creme". */
|
|
7
|
+
export function normalise(text) {
|
|
8
|
+
return text.normalize('NFKD').replace(/\p{M}/gu, '').toLowerCase().replace(/\s+/g, ' ').trim();
|
|
9
|
+
}
|
|
10
|
+
export function words(query) {
|
|
11
|
+
return normalise(query).split(' ').filter(Boolean);
|
|
12
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One setting an app's search can find: a row in a section of its settings, or with `secondary`, a section itself.
|
|
3
|
+
* `id` names the row the search opens, as `settingAnchor(id)`.
|
|
4
|
+
*/
|
|
5
|
+
export type SettingItem<Section extends string = string, Id extends string = string> = {
|
|
6
|
+
id: Id;
|
|
7
|
+
title: string;
|
|
8
|
+
section: Section;
|
|
9
|
+
sectionTitle: string;
|
|
10
|
+
hint?: string;
|
|
11
|
+
terms?: readonly string[];
|
|
12
|
+
secondary?: boolean;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* The items every word of `query` appears in, best first: a title that is the query, then one starting with it, then
|
|
16
|
+
* one containing it, then one holding every word, then the rest. Secondary items go last, and ties keep list order.
|
|
17
|
+
* An empty query finds nothing.
|
|
18
|
+
*/
|
|
19
|
+
export declare function searchSettings<Item extends SettingItem>(items: readonly Item[], query: string, limit?: number): Item[];
|
|
20
|
+
/** The element id of the row that shows the setting `id`. */
|
|
21
|
+
export declare function settingAnchor(id: string): string;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { matchWords, normalise, words } from './search.js';
|
|
2
|
+
/**
|
|
3
|
+
* The items every word of `query` appears in, best first: a title that is the query, then one starting with it, then
|
|
4
|
+
* one containing it, then one holding every word, then the rest. Secondary items go last, and ties keep list order.
|
|
5
|
+
* An empty query finds nothing.
|
|
6
|
+
*/
|
|
7
|
+
export function searchSettings(items, query, limit = Number.POSITIVE_INFINITY) {
|
|
8
|
+
const wanted = words(query);
|
|
9
|
+
if (!wanted.length)
|
|
10
|
+
return [];
|
|
11
|
+
const phrase = wanted.join(' ');
|
|
12
|
+
const ranked = items.flatMap((item, order) => {
|
|
13
|
+
const fields = [item.title, item.sectionTitle, item.hint ?? '', ...(item.terms ?? [])];
|
|
14
|
+
if (!matchWords(query, fields))
|
|
15
|
+
return [];
|
|
16
|
+
return [{ item, order, rank: titleRank(normalise(item.title), phrase, wanted) }];
|
|
17
|
+
});
|
|
18
|
+
ranked.sort((a, b) => Number(a.item.secondary ?? false) - Number(b.item.secondary ?? false) ||
|
|
19
|
+
b.rank - a.rank ||
|
|
20
|
+
a.order - b.order);
|
|
21
|
+
return ranked.slice(0, limit).map(({ item }) => item);
|
|
22
|
+
}
|
|
23
|
+
/** The element id of the row that shows the setting `id`. */
|
|
24
|
+
export function settingAnchor(id) {
|
|
25
|
+
return `setting-${id}`;
|
|
26
|
+
}
|
|
27
|
+
function titleRank(title, phrase, wanted) {
|
|
28
|
+
if (title === phrase)
|
|
29
|
+
return 5;
|
|
30
|
+
if (title.startsWith(phrase))
|
|
31
|
+
return 4;
|
|
32
|
+
if (title.includes(phrase))
|
|
33
|
+
return 3;
|
|
34
|
+
if (wanted.every((word) => title.includes(word)))
|
|
35
|
+
return 2;
|
|
36
|
+
return 1;
|
|
37
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How a page finds the build its server serves.
|
|
3
|
+
* A build key names one build of the web app, such as the time it was built, and differs between any two builds.
|
|
4
|
+
*/
|
|
5
|
+
export type BuildWatch = {
|
|
6
|
+
/** The key of the build this page loaded, or null outside a built bundle, which is never stale. */
|
|
7
|
+
loaded: string | null;
|
|
8
|
+
/** Resolves to the key of the build the server serves now, or null when it cannot be read. */
|
|
9
|
+
read: () => Promise<string | null>;
|
|
10
|
+
/** Dispatched on `window` to check now rather than within the hour. */
|
|
11
|
+
checkEvent: string;
|
|
12
|
+
/** A `BroadcastChannel` on which other tabs of the app ask for a check. */
|
|
13
|
+
channel?: string;
|
|
14
|
+
};
|
|
15
|
+
type Events = Pick<EventTarget, 'addEventListener' | 'removeEventListener'>;
|
|
16
|
+
type Page = Pick<Document, 'addEventListener' | 'removeEventListener' | 'visibilityState'>;
|
|
17
|
+
type Schedule = {
|
|
18
|
+
now: () => void;
|
|
19
|
+
stop: () => void;
|
|
20
|
+
};
|
|
21
|
+
type WatchOptions = {
|
|
22
|
+
loaded: string;
|
|
23
|
+
onStale: (served: string | null) => void;
|
|
24
|
+
read: () => Promise<string | null>;
|
|
25
|
+
checkEvent: string;
|
|
26
|
+
channel?: Pick<BroadcastChannel, 'addEventListener' | 'close'>;
|
|
27
|
+
online?: () => boolean;
|
|
28
|
+
page?: Page;
|
|
29
|
+
/** Where `checkEvent` and failed module loads arrive. */
|
|
30
|
+
events?: Events;
|
|
31
|
+
};
|
|
32
|
+
/** Asks this tab, and with `channel` the app's other open tabs, to look for a new build now. */
|
|
33
|
+
export declare function checkForNewBuild(checkEvent: string, channel?: string): void;
|
|
34
|
+
/** The JSON at `path`, read past every cache, or null when it cannot be read. */
|
|
35
|
+
export declare function fetchJson(path: string): Promise<unknown>;
|
|
36
|
+
/**
|
|
37
|
+
* Runs `check` hourly while the page is in view, and on `checkEvent`.
|
|
38
|
+
* A check due while the page is hidden runs when it is shown again, as does one an hour after the last.
|
|
39
|
+
*/
|
|
40
|
+
export declare function hourlyInView(check: () => void, checkEvent: string, page?: Page, events?: Events): Schedule;
|
|
41
|
+
/**
|
|
42
|
+
* Calls `onStale` with each new build the server serves, or with null once it serves `loaded` again.
|
|
43
|
+
* Checks on {@link hourlyInView}'s schedule, and when another tab asks on `channel`.
|
|
44
|
+
* A module that fails to load triggers a check, since a newer build can have replaced it.
|
|
45
|
+
* A build that cannot be read counts as no change, so being offline cannot raise it.
|
|
46
|
+
*/
|
|
47
|
+
export declare function watchForNewBuild({ loaded, onStale, read, checkEvent, channel, online, page, events, }: WatchOptions): () => void;
|
|
48
|
+
export {};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
const CHECK_EVERY = 60 * 60_000;
|
|
2
|
+
/** Asks this tab, and with `channel` the app's other open tabs, to look for a new build now. */
|
|
3
|
+
export function checkForNewBuild(checkEvent, channel) {
|
|
4
|
+
window.dispatchEvent(new Event(checkEvent));
|
|
5
|
+
if (!channel || typeof BroadcastChannel !== 'function')
|
|
6
|
+
return;
|
|
7
|
+
const open = new BroadcastChannel(channel);
|
|
8
|
+
open.postMessage('check');
|
|
9
|
+
open.close();
|
|
10
|
+
}
|
|
11
|
+
/** The JSON at `path`, read past every cache, or null when it cannot be read. */
|
|
12
|
+
export async function fetchJson(path) {
|
|
13
|
+
try {
|
|
14
|
+
const response = await fetch(path, { cache: 'no-store' });
|
|
15
|
+
return response.ok ? await response.json() : null;
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Runs `check` hourly while the page is in view, and on `checkEvent`.
|
|
23
|
+
* A check due while the page is hidden runs when it is shown again, as does one an hour after the last.
|
|
24
|
+
*/
|
|
25
|
+
export function hourlyInView(check, checkEvent, page = document, events = window) {
|
|
26
|
+
let checkedAt = Date.now();
|
|
27
|
+
let missed = false;
|
|
28
|
+
function now() {
|
|
29
|
+
missed = page.visibilityState === 'hidden';
|
|
30
|
+
if (missed)
|
|
31
|
+
return;
|
|
32
|
+
checkedAt = Date.now();
|
|
33
|
+
check();
|
|
34
|
+
}
|
|
35
|
+
function shown() {
|
|
36
|
+
if (missed || Date.now() - checkedAt >= CHECK_EVERY)
|
|
37
|
+
now();
|
|
38
|
+
}
|
|
39
|
+
const timer = setInterval(now, CHECK_EVERY);
|
|
40
|
+
page.addEventListener('visibilitychange', shown);
|
|
41
|
+
events.addEventListener(checkEvent, now);
|
|
42
|
+
return {
|
|
43
|
+
now,
|
|
44
|
+
stop() {
|
|
45
|
+
clearInterval(timer);
|
|
46
|
+
page.removeEventListener('visibilitychange', shown);
|
|
47
|
+
events.removeEventListener(checkEvent, now);
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Calls `onStale` with each new build the server serves, or with null once it serves `loaded` again.
|
|
53
|
+
* Checks on {@link hourlyInView}'s schedule, and when another tab asks on `channel`.
|
|
54
|
+
* A module that fails to load triggers a check, since a newer build can have replaced it.
|
|
55
|
+
* A build that cannot be read counts as no change, so being offline cannot raise it.
|
|
56
|
+
*/
|
|
57
|
+
export function watchForNewBuild({ loaded, onStale, read, checkEvent, channel, online = () => navigator.onLine !== false, page = document, events = window, }) {
|
|
58
|
+
let stopped = false;
|
|
59
|
+
let reported = loaded;
|
|
60
|
+
async function check() {
|
|
61
|
+
if (!online())
|
|
62
|
+
return;
|
|
63
|
+
const latest = await read();
|
|
64
|
+
if (stopped || !latest || latest === reported)
|
|
65
|
+
return;
|
|
66
|
+
reported = latest;
|
|
67
|
+
onStale(latest === loaded ? null : latest);
|
|
68
|
+
}
|
|
69
|
+
const schedule = hourlyInView(() => void check(), checkEvent, page, events);
|
|
70
|
+
function rejected(event) {
|
|
71
|
+
if (isModuleFailure(event))
|
|
72
|
+
schedule.now();
|
|
73
|
+
}
|
|
74
|
+
events.addEventListener('unhandledrejection', rejected);
|
|
75
|
+
events.addEventListener('vite:preloadError', schedule.now);
|
|
76
|
+
channel?.addEventListener('message', schedule.now);
|
|
77
|
+
return () => {
|
|
78
|
+
stopped = true;
|
|
79
|
+
schedule.stop();
|
|
80
|
+
events.removeEventListener('unhandledrejection', rejected);
|
|
81
|
+
events.removeEventListener('vite:preloadError', schedule.now);
|
|
82
|
+
channel?.close();
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/** A dynamic import whose file is gone, as when the server has replaced the bundle this page came from. */
|
|
86
|
+
function isModuleFailure(event) {
|
|
87
|
+
const reason = 'reason' in event ? event.reason : null;
|
|
88
|
+
return (reason instanceof TypeError &&
|
|
89
|
+
/dynamically imported module|Importing a module script failed/.test(reason.message));
|
|
90
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { UpdateSource, UpdateStatus } from './types.js';
|
|
2
|
+
/** How an update ended, with the server's answer that showed it. */
|
|
3
|
+
export type UpdateEnd = {
|
|
4
|
+
progress: Exclude<UpdateProgress, {
|
|
5
|
+
kind: 'waiting';
|
|
6
|
+
}>;
|
|
7
|
+
status: UpdateStatus;
|
|
8
|
+
};
|
|
9
|
+
/** Where an update stands, from what the server answers while the page follows it. */
|
|
10
|
+
export type UpdateProgress = {
|
|
11
|
+
kind: 'waiting';
|
|
12
|
+
} | {
|
|
13
|
+
kind: 'updated';
|
|
14
|
+
version: string;
|
|
15
|
+
} | {
|
|
16
|
+
kind: 'current';
|
|
17
|
+
} | {
|
|
18
|
+
kind: 'failed';
|
|
19
|
+
reason: string;
|
|
20
|
+
};
|
|
21
|
+
type FollowOptions = {
|
|
22
|
+
/** Called each time the server doesn't answer, as while it restarts. */
|
|
23
|
+
away?: () => void;
|
|
24
|
+
wait?: (ms: number) => Promise<void>;
|
|
25
|
+
now?: () => number;
|
|
26
|
+
};
|
|
27
|
+
/** How long an update is followed before the card says it hasn't finished. */
|
|
28
|
+
export declare const FOLLOW_FOR_MS: number;
|
|
29
|
+
/**
|
|
30
|
+
* Waits for the update `before` was read ahead of to end, polling `source`.
|
|
31
|
+
* Resolves to null when the server hasn't answered with an end in {@link FOLLOW_FOR_MS}.
|
|
32
|
+
*/
|
|
33
|
+
export declare function followUpdate(source: UpdateSource, before: UpdateStatus, { away, wait, now, }?: FollowOptions): Promise<UpdateEnd | null>;
|
|
34
|
+
/**
|
|
35
|
+
* Compares the server's answer with the one from before the update: a new version, or a run that ended.
|
|
36
|
+
* A new version counts as soon as the server reports it, even while the run that installed it is still finishing.
|
|
37
|
+
*/
|
|
38
|
+
export declare function updateProgress(before: UpdateStatus, now: UpdateStatus): UpdateProgress;
|
|
39
|
+
export {};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** How long an update is followed before the card says it hasn't finished. */
|
|
2
|
+
export const FOLLOW_FOR_MS = 12 * 60_000;
|
|
3
|
+
const POLL_MS = 2000;
|
|
4
|
+
/**
|
|
5
|
+
* Waits for the update `before` was read ahead of to end, polling `source`.
|
|
6
|
+
* Resolves to null when the server hasn't answered with an end in {@link FOLLOW_FOR_MS}.
|
|
7
|
+
*/
|
|
8
|
+
export async function followUpdate(source, before, { away, wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms)), now = Date.now, } = {}) {
|
|
9
|
+
const deadline = now() + FOLLOW_FOR_MS;
|
|
10
|
+
while (now() < deadline) {
|
|
11
|
+
await wait(POLL_MS);
|
|
12
|
+
let status;
|
|
13
|
+
try {
|
|
14
|
+
status = await source.read();
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
away?.();
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
const progress = updateProgress(before, status);
|
|
21
|
+
if (progress.kind !== 'waiting')
|
|
22
|
+
return { progress, status };
|
|
23
|
+
}
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Compares the server's answer with the one from before the update: a new version, or a run that ended.
|
|
28
|
+
* A new version counts as soon as the server reports it, even while the run that installed it is still finishing.
|
|
29
|
+
*/
|
|
30
|
+
export function updateProgress(before, now) {
|
|
31
|
+
if (now.installed !== null && now.installed !== before.installed)
|
|
32
|
+
return { kind: 'updated', version: now.installed };
|
|
33
|
+
const last = now.last;
|
|
34
|
+
if (now.running || !last || last.state === 'running' || last.state === 'replaced')
|
|
35
|
+
return { kind: 'waiting' };
|
|
36
|
+
const previousRun = last.at === before.last?.at && last.state === before.last?.state;
|
|
37
|
+
if (previousRun)
|
|
38
|
+
return { kind: 'waiting' };
|
|
39
|
+
return last.state === 'current'
|
|
40
|
+
? { kind: 'current' }
|
|
41
|
+
: { kind: 'failed', reason: lastLine(last.output) ?? `exit ${last.exit}` };
|
|
42
|
+
}
|
|
43
|
+
/** A package manager prints its error last, after the lines that led up to it. */
|
|
44
|
+
function lastLine(output) {
|
|
45
|
+
return (output
|
|
46
|
+
.split('\n')
|
|
47
|
+
.map((line) => line.trim())
|
|
48
|
+
.findLast((line) => line !== '') ?? null);
|
|
49
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Whether the card was put off for `offer`. */
|
|
2
|
+
export declare function isPutOff(storageKey: string, offer: string, storage?: Storage): boolean;
|
|
3
|
+
/** Puts the card off until it offers something other than `offer`. */
|
|
4
|
+
export declare function putOff(storageKey: string, offer: string, storage?: Storage): void;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Whether the card was put off for `offer`. */
|
|
2
|
+
export function isPutOff(storageKey, offer, storage = localStorage) {
|
|
3
|
+
try {
|
|
4
|
+
return storage.getItem(storageKey) === offer;
|
|
5
|
+
}
|
|
6
|
+
catch {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
/** Puts the card off until it offers something other than `offer`. */
|
|
11
|
+
export function putOff(storageKey, offer, storage = localStorage) {
|
|
12
|
+
try {
|
|
13
|
+
storage.setItem(storageKey, offer);
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
// Storage is blocked, as in a private window, so nothing is kept.
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { UpdateStatus } from './types.js';
|
|
2
|
+
/** An offer an app adds around the two the package makes, such as a restart into a replaced desktop app. */
|
|
3
|
+
export type AppOffer<Node = unknown> = {
|
|
4
|
+
key: string;
|
|
5
|
+
node: Node;
|
|
6
|
+
};
|
|
7
|
+
/** What the card offers. The key names the offer and its version, so Not now puts off only that one. */
|
|
8
|
+
export type Offer<Node = unknown> = {
|
|
9
|
+
kind: 'app';
|
|
10
|
+
key: string;
|
|
11
|
+
offer: AppOffer<Node>;
|
|
12
|
+
} | {
|
|
13
|
+
kind: 'refresh';
|
|
14
|
+
key: string;
|
|
15
|
+
} | {
|
|
16
|
+
kind: 'server';
|
|
17
|
+
key: string;
|
|
18
|
+
status: UpdateStatus;
|
|
19
|
+
};
|
|
20
|
+
type OfferInput<Node> = {
|
|
21
|
+
first?: AppOffer<Node> | null;
|
|
22
|
+
last?: AppOffer<Node> | null;
|
|
23
|
+
server: UpdateStatus | null;
|
|
24
|
+
stale: string | null;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* The first that holds: the app's first offer, the server's update, a refresh into the build the server serves, then
|
|
28
|
+
* the app's last offer.
|
|
29
|
+
* The server's update shows only to a viewer who may start one, while a newer release waits or one runs.
|
|
30
|
+
*/
|
|
31
|
+
export declare function pickOffer<Node>({ first, last, server, stale, }: OfferInput<Node>): Offer<Node> | null;
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The first that holds: the app's first offer, the server's update, a refresh into the build the server serves, then
|
|
3
|
+
* the app's last offer.
|
|
4
|
+
* The server's update shows only to a viewer who may start one, while a newer release waits or one runs.
|
|
5
|
+
*/
|
|
6
|
+
export function pickOffer({ first, last, server, stale, }) {
|
|
7
|
+
if (first)
|
|
8
|
+
return { kind: 'app', key: first.key, offer: first };
|
|
9
|
+
if (server && offersServerUpdate(server))
|
|
10
|
+
return { kind: 'server', key: `server:${server.latest}`, status: server };
|
|
11
|
+
if (stale)
|
|
12
|
+
return { kind: 'refresh', key: `refresh:${stale}` };
|
|
13
|
+
if (last)
|
|
14
|
+
return { kind: 'app', key: last.key, offer: last };
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
function offersServerUpdate(status) {
|
|
18
|
+
return status.mayStart && (status.running || (status.newer && status.latest !== null));
|
|
19
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
type ReloadOptions = {
|
|
2
|
+
loaded: string | null;
|
|
3
|
+
read: () => Promise<string | null>;
|
|
4
|
+
reload: () => void;
|
|
5
|
+
wait?: (ms: number) => Promise<void>;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Reloads once the server serves a build other than `loaded`, so the reload cannot come back to the old one.
|
|
9
|
+
* Reloads anyway once the reads run out.
|
|
10
|
+
*/
|
|
11
|
+
export declare function reloadWhenServing({ loaded, read, reload, wait, }: ReloadOptions): Promise<void>;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** About 100 seconds of reads: one, two, and four seconds apart, then eight seconds apart. */
|
|
2
|
+
const READS = 15;
|
|
3
|
+
/**
|
|
4
|
+
* Reloads once the server serves a build other than `loaded`, so the reload cannot come back to the old one.
|
|
5
|
+
* Reloads anyway once the reads run out.
|
|
6
|
+
*/
|
|
7
|
+
export async function reloadWhenServing({ loaded, read, reload, wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms)), }) {
|
|
8
|
+
for (let attempt = 0; attempt < READS; attempt++) {
|
|
9
|
+
const served = await read().catch(() => null);
|
|
10
|
+
if (served !== null && served !== loaded)
|
|
11
|
+
break;
|
|
12
|
+
if (attempt < READS - 1)
|
|
13
|
+
await wait(Math.min(1000 * 2 ** attempt, 8000));
|
|
14
|
+
}
|
|
15
|
+
reload();
|
|
16
|
+
}
|