@popcorn245/cornfield 0.48.0 → 0.49.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/core/strings.js +14 -0
- package/explorer/check.js +7 -227
- package/explorer/connection.js +3 -3
- package/explorer/engine/checks.js +252 -0
- package/explorer/engine/connection.js +36 -0
- package/explorer/engine/layers.js +1 -0
- package/explorer/engine/player.js +489 -0
- package/explorer/engine/proposal.js +373 -0
- package/explorer/engine/render.js +1 -0
- package/explorer/engine/source.js +315 -0
- package/explorer/engine/theme-sheet.js +444 -0
- package/explorer/index.html +6 -59
- package/explorer/instance.js +82 -6
- package/explorer/page-address.js +59 -0
- package/explorer/page-pieces.js +33 -5
- package/explorer/page.js +20 -20
- package/explorer/panels.js +1 -3
- package/explorer/pieces/changes.js +502 -0
- package/explorer/pieces/decision.js +657 -0
- package/explorer/pieces/layers.js +4 -0
- package/explorer/pieces/map.js +113 -2
- package/explorer/pieces/node-panel.js +34 -1
- package/explorer/pieces/proposal.js +488 -0
- package/explorer/pieces/scope-link.js +50 -0
- package/explorer/pieces/scope.js +247 -0
- package/explorer/pieces/status.css +24 -0
- package/explorer/pieces/status.js +144 -0
- package/explorer/plugins.js +0 -10
- package/explorer/proposal.js +64 -697
- package/explorer/review-column.js +24 -439
- package/explorer/review.js +27 -673
- package/explorer/scenario-ui.js +32 -478
- package/explorer/sheets.js +8 -7
- package/explorer/view.js +6 -5
- package/explorer/waiting-line.js +7 -50
- package/package.json +1 -1
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module explorer/pieces/scope
|
|
3
|
+
*
|
|
4
|
+
* What the pieces inside a `<corn-scope>` share (spec 0011 D5): one instance (one selection, one camera, one topology), and where its data comes
|
|
5
|
+
* from. A scope shows nothing of its own and has no shadow root; its element is only the place the instance is rooted and the tokens (and, with
|
|
6
|
+
* `use-theme`, the server's) are set, which every piece inside inherits.
|
|
7
|
+
*
|
|
8
|
+
* const scope = mountScope(host, options) -> { update, destroy, instance, source } (the `<corn-scope>` component calls it)
|
|
9
|
+
* const join = joinScope(el, onChange) -> { inst, scope, leave } (a piece's shell calls it for the scope around it, if any;
|
|
10
|
+
* it lives in pieces/scope-link.js, so a map alone does not load this module)
|
|
11
|
+
*
|
|
12
|
+
* HOW A PIECE FINDS ITS SCOPE. A piece dispatches `corn-scope-request`, a composed, bubbling event, from its own element when it connects. The
|
|
13
|
+
* nearest scope above it hears it (it crosses every shadow root between them, which `closest()` cannot), answers by handing over a handle (the
|
|
14
|
+
* instance) and stops the event, so a scope inside a scope is the nearer one's. A piece that hears no answer makes a private instance, as
|
|
15
|
+
* `<corn-map>` alone always did. Three things change the answer, and each is the same message in the other direction, `corn-scope-changed`,
|
|
16
|
+
* dispatched at the piece's own element (it does not bubble), after which the piece asks again:
|
|
17
|
+
*
|
|
18
|
+
* a scope whose `src` changes a new instance for the new server (the address is the instance's base and does not change): the scope makes
|
|
19
|
+
* it, tells every piece it holds, and ends the old one once they have left it
|
|
20
|
+
* a scope removed from the page its pieces are told and ask again (to a scope further up, or to none)
|
|
21
|
+
* a scope added around pieces the scope walks its descendants (the light tree and the open shadow roots of other elements) and tells each `corn-`
|
|
22
|
+
* that are already there piece it finds; a piece that connected before its scope was defined asks again and is answered
|
|
23
|
+
*
|
|
24
|
+
* Pieces in a closed shadow root of someone else's component cannot be found by the walk. A piece that is removed leaves (its shell calls `leave`).
|
|
25
|
+
* A second map in a scope is refused (the engine holds one map per instance); when the map that has the scope leaves, the first refused map that
|
|
26
|
+
* is still in the page is told to ask again, and takes it.
|
|
27
|
+
*
|
|
28
|
+
* It reads no `location`: the page's address is a function the shell passes in (`pageUrl`), used for the sign-in's way back and to name the
|
|
29
|
+
* origin a "not listed" message asks the server to list. It never navigates the window.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
import { createInstance } from '../instance.js';
|
|
33
|
+
import { adoptDefaultTokens } from './adopt-tokens.js';
|
|
34
|
+
import { createSource, parseSource } from '../engine/source.js';
|
|
35
|
+
import { createServerTheme } from '../engine/theme-sheet.js';
|
|
36
|
+
import { keepLayersIn } from '../engine/layers.js';
|
|
37
|
+
import { resolveTheme } from '../theme-model.js';
|
|
38
|
+
import { mount as mountStatus } from './status.js';
|
|
39
|
+
import { CHANGED, REQUEST, infoOf } from './scope-link.js';
|
|
40
|
+
|
|
41
|
+
export { REQUEST, CHANGED, joinScope, scopeInfo } from './scope-link.js';
|
|
42
|
+
/** The theme choices of a scope (and of a map) */
|
|
43
|
+
const THEMES = ['light', 'dark', 'auto'];
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @param {Element} host the `<corn-scope>` element
|
|
48
|
+
* @param {object} [options]
|
|
49
|
+
* @param {string} [options.src] the base address of a cornfield server
|
|
50
|
+
* @param {'light'|'dark'|'auto'} [options.theme]
|
|
51
|
+
* @param {boolean} [options.useTheme] use the server's theme (fetch `theme.css`, adopt it into every piece, copy its tokens here)
|
|
52
|
+
* @param {object | null} [options.topology] @param {object | null} [options.config] @param {object | null} [options.strings] handed in by a host that fetches for itself
|
|
53
|
+
* @param {string} [options.tokens] the default `--corn-*` tokens (CSS text), added once to the document
|
|
54
|
+
* @param {string} [options.version] the package's version, compared with the server's
|
|
55
|
+
* @param {() => string} [options.pageUrl] the page's address
|
|
56
|
+
* @param {(detail: { status: number, signIn: string }) => void} [options.onAuth] the server wants a sign-in
|
|
57
|
+
* @param {(detail: { kind: 'not-listed' | 'offline' | 'server' | 'address' | 'version' | 'theme' | 'second-map', message?: string }) => void} [options.onError]
|
|
58
|
+
* @param {() => void} [options.onReady] the scope has its data (loaded, or handed in)
|
|
59
|
+
*/
|
|
60
|
+
export function mountScope(host, options = {}) {
|
|
61
|
+
const props = { src: options.src, theme: options.theme, useTheme: !!options.useTheme, topology: options.topology, config: options.config, strings: options.strings };
|
|
62
|
+
const pageUrl = options.pageUrl || (() => '');
|
|
63
|
+
/** The pieces that took this scope's answer, by their element: what is told when the instance changes */
|
|
64
|
+
const handles = new Set();
|
|
65
|
+
let inst = null;
|
|
66
|
+
let source = null;
|
|
67
|
+
let serverTheme = null;
|
|
68
|
+
let readyFired = false;
|
|
69
|
+
let ended = false;
|
|
70
|
+
|
|
71
|
+
// -- the answer to a piece ---------------------------------------------------------------------------------------------------------------
|
|
72
|
+
const answer = (event) => {
|
|
73
|
+
if (ended || !inst || !event.detail || typeof event.detail.claim !== 'function') return;
|
|
74
|
+
event.stopPropagation(); // the nearest scope answers, and no other
|
|
75
|
+
const handle = {
|
|
76
|
+
el: event.detail.el,
|
|
77
|
+
inst,
|
|
78
|
+
scope: api,
|
|
79
|
+
release: () => handles.delete(handle),
|
|
80
|
+
};
|
|
81
|
+
handles.add(handle);
|
|
82
|
+
event.detail.claim(handle);
|
|
83
|
+
};
|
|
84
|
+
host.addEventListener(REQUEST, answer);
|
|
85
|
+
|
|
86
|
+
/** Tells every piece this scope has handed the instance to, and every `corn-` piece below it that has not asked, to ask again (once each) */
|
|
87
|
+
function tellPieces() {
|
|
88
|
+
const told = new Set(piecesBelow(host));
|
|
89
|
+
for (const h of [...handles]) { handles.delete(h); if (h.el) told.add(h.el); }
|
|
90
|
+
for (const el of told) if (el.isConnected !== false) el.dispatchEvent(new Event(CHANGED));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// -- the mode (light or dark) the server's tokens are for ------------------------------------------------------------------------------
|
|
94
|
+
function modeNow() {
|
|
95
|
+
const choice = THEMES.includes(props.theme) ? props.theme : 'auto';
|
|
96
|
+
if (choice === 'light' || choice === 'dark') return choice;
|
|
97
|
+
const root = inst.doc && inst.doc.documentElement;
|
|
98
|
+
const own = root ? root.getAttribute('data-theme') : null;
|
|
99
|
+
if (own === 'light' || own === 'dark') return own;
|
|
100
|
+
const query = inst.win && inst.win.matchMedia ? inst.win.matchMedia('(prefers-color-scheme: light)') : null;
|
|
101
|
+
return resolveTheme('system', !!(query && query.matches));
|
|
102
|
+
}
|
|
103
|
+
function followMode() {
|
|
104
|
+
if (serverTheme) serverTheme.setMode(modeNow());
|
|
105
|
+
if (inst) inst.emit('scope:theme');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// -- one instance per server ---------------------------------------------------------------------------------------------------------------
|
|
109
|
+
function build() {
|
|
110
|
+
const parsed = props.src === undefined || props.src === null || props.src === '' ? null : parseSource(props.src);
|
|
111
|
+
inst = createInstance({ root: host, base: parsed && parsed.ok ? parsed.base : '' });
|
|
112
|
+
keepLayersIn(inst, null); // a map in another app's page keeps nothing in that page's storage
|
|
113
|
+
const info = infoOf(inst);
|
|
114
|
+
info.host = host;
|
|
115
|
+
info.theme = THEMES.includes(props.theme) ? props.theme : 'auto';
|
|
116
|
+
const st = createServerTheme(inst, { defaults: options.tokens || '' });
|
|
117
|
+
serverTheme = st;
|
|
118
|
+
info.serverTheme = st;
|
|
119
|
+
st.tokensOn(host);
|
|
120
|
+
inst.own(() => st.destroy());
|
|
121
|
+
source = createSource(inst, {
|
|
122
|
+
version: options.version || '',
|
|
123
|
+
pageUrl,
|
|
124
|
+
onConfig: (config) => { if (props.useTheme) st.load(config); },
|
|
125
|
+
});
|
|
126
|
+
info.source = source;
|
|
127
|
+
info.mountStatus = mountStatus;
|
|
128
|
+
readyFired = false;
|
|
129
|
+
inst.on('source', onSource);
|
|
130
|
+
inst.on('theme', onTheme);
|
|
131
|
+
// A second map waits: when the one that has the instance's root gives it back, the first that waits and is still in the page asks again
|
|
132
|
+
// (after the leaving map has finished ending, so the two never hold the instance's state at once)
|
|
133
|
+
const waiting = [];
|
|
134
|
+
inst.on('piece:refused', (d) => {
|
|
135
|
+
if (d.host && !waiting.includes(d.host)) waiting.push(d.host);
|
|
136
|
+
if (options.onError) options.onError({ kind: d.reason, message: d.message });
|
|
137
|
+
});
|
|
138
|
+
const mine = inst;
|
|
139
|
+
inst.on('attach:released', () => {
|
|
140
|
+
Promise.resolve().then(() => {
|
|
141
|
+
if (ended || inst !== mine) return;
|
|
142
|
+
while (waiting.length) {
|
|
143
|
+
const el = waiting.shift();
|
|
144
|
+
if (el.isConnected) { el.dispatchEvent(new Event(CHANGED)); return; }
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
// The host's own light and dark and the system's: the tokens follow
|
|
149
|
+
const watch = inst.win && inst.win.MutationObserver && inst.doc && inst.doc.documentElement ? new inst.win.MutationObserver(followMode) : null;
|
|
150
|
+
if (watch) { watch.observe(inst.doc.documentElement, { attributes: true, attributeFilter: ['data-theme'] }); inst.own(() => watch.disconnect()); }
|
|
151
|
+
const query = inst.win && inst.win.matchMedia ? inst.win.matchMedia('(prefers-color-scheme: light)') : null;
|
|
152
|
+
if (query && query.addEventListener) { query.addEventListener('change', followMode); inst.own(() => query.removeEventListener('change', followMode)); }
|
|
153
|
+
serverTheme.setMode(modeNow());
|
|
154
|
+
if (parsed && !parsed.ok) source.say({ state: 'address', problem: parsed.problem });
|
|
155
|
+
source.provide({ topology: props.topology || undefined, config: props.config || undefined, strings: props.strings || undefined });
|
|
156
|
+
if (parsed && parsed.ok) source.load();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function onSource(detail) {
|
|
160
|
+
if (ended) return;
|
|
161
|
+
switch (detail.state) {
|
|
162
|
+
case 'ready':
|
|
163
|
+
if (!readyFired) { readyFired = true; if (options.onReady) options.onReady(); }
|
|
164
|
+
break;
|
|
165
|
+
case 'sign-in':
|
|
166
|
+
if (options.onAuth) options.onAuth({ status: detail.status, signIn: detail.signIn });
|
|
167
|
+
break;
|
|
168
|
+
case 'not-allowed': if (options.onError) options.onError({ kind: 'not-listed', message: detail.origin }); break;
|
|
169
|
+
case 'offline': if (options.onError) options.onError({ kind: 'offline' }); break;
|
|
170
|
+
case 'server': if (options.onError) options.onError({ kind: 'server', message: detail.message }); break;
|
|
171
|
+
case 'address': if (options.onError) options.onError({ kind: 'address', message: detail.problem }); break;
|
|
172
|
+
case 'version': if (options.onError) options.onError({ kind: 'version', message: `${detail.package} / ${detail.server}` }); break;
|
|
173
|
+
default:
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
function onTheme(detail) {
|
|
177
|
+
if (!ended && detail.state === 'failed' && options.onError) options.onError({ kind: 'theme', message: detail.reason });
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function release() {
|
|
181
|
+
tellPieces(); // they leave this instance (and ask again) before it ends
|
|
182
|
+
if (inst) inst.destroy();
|
|
183
|
+
inst = null;
|
|
184
|
+
source = null;
|
|
185
|
+
serverTheme = null;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const api = {
|
|
189
|
+
/** Applies the properties that changed (only the keys present are looked at) */
|
|
190
|
+
update(next = {}) {
|
|
191
|
+
if (ended) return;
|
|
192
|
+
let rebuild = false;
|
|
193
|
+
if ('src' in next && next.src !== props.src) { props.src = next.src; rebuild = true; }
|
|
194
|
+
if ('theme' in next && next.theme !== props.theme) {
|
|
195
|
+
props.theme = next.theme;
|
|
196
|
+
infoOf(inst).theme = THEMES.includes(props.theme) ? props.theme : 'auto';
|
|
197
|
+
followMode();
|
|
198
|
+
}
|
|
199
|
+
if ('useTheme' in next && !!next.useTheme !== props.useTheme) {
|
|
200
|
+
props.useTheme = !!next.useTheme;
|
|
201
|
+
if (props.useTheme) { if (source.config) serverTheme.load(source.config); } else serverTheme.unload();
|
|
202
|
+
}
|
|
203
|
+
if (rebuild) {
|
|
204
|
+
// The new instance is made first, the pieces are told to take it, and the old one ends once they have gone
|
|
205
|
+
const previous = inst;
|
|
206
|
+
serverTheme.destroy(); // its tokens leave this element before the new server's are put on it
|
|
207
|
+
build();
|
|
208
|
+
tellPieces();
|
|
209
|
+
previous.destroy();
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
const hand = {};
|
|
213
|
+
for (const key of ['topology', 'config', 'strings']) if (key in next && next[key] !== props[key]) { props[key] = next[key]; hand[key] = next[key] === undefined ? null : next[key]; }
|
|
214
|
+
if (Object.keys(hand).length) source.provide(hand);
|
|
215
|
+
},
|
|
216
|
+
destroy() {
|
|
217
|
+
if (ended) return;
|
|
218
|
+
host.removeEventListener(REQUEST, answer);
|
|
219
|
+
release();
|
|
220
|
+
ended = true;
|
|
221
|
+
handles.clear();
|
|
222
|
+
},
|
|
223
|
+
/** The instance now in force (a new one after `src` changes) */
|
|
224
|
+
get instance() { return inst; },
|
|
225
|
+
get source() { return source; },
|
|
226
|
+
};
|
|
227
|
+
// The page: the default tokens, a first instance, and the pieces already below it (a scope put around pieces that are already there)
|
|
228
|
+
if (options.tokens) adoptDefaultTokens(host.getRootNode(), options.tokens);
|
|
229
|
+
build();
|
|
230
|
+
tellPieces();
|
|
231
|
+
return api;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** The `corn-` elements below `root`: its light tree and the open shadow roots of other elements, not into a scope of their own */
|
|
235
|
+
function piecesBelow(root) {
|
|
236
|
+
const out = [];
|
|
237
|
+
const walk = (node) => {
|
|
238
|
+
for (const el of node.children || []) {
|
|
239
|
+
const tag = el.localName || '';
|
|
240
|
+
if (tag.startsWith('corn-')) { if (tag === 'corn-scope') continue; out.push(el); }
|
|
241
|
+
if (el.shadowRoot) walk(el.shadowRoot);
|
|
242
|
+
walk(el);
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
walk(root);
|
|
246
|
+
return out;
|
|
247
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/* The status of a map that loads from a server (explorer/pieces/status.js; components/src corn-map): the card in the middle of the stage while it
|
|
2
|
+
loads and when it cannot, and the quiet line of the version notice. Every colour, size and shape is a --corn-* token; a host restyles an element
|
|
3
|
+
with ::part(source-status), ::part(source-title), ::part(source-text), ::part(source-origin), ::part(source-sign-in), ::part(source-retry),
|
|
4
|
+
::part(source-notice). cornfield's page has no source to load from and loads none of this. */
|
|
5
|
+
[data-corn="source-status"] { position: absolute; z-index: 2; left: 50%; top: 50%; transform: translate(-50%, -50%); box-sizing: border-box; display: flex; flex-direction: column; align-items: center; gap: var(--corn-space-3); width: min(26rem, calc(100% - var(--corn-space-6) * 2)); padding: var(--corn-space-6); border: var(--corn-border) solid var(--corn-line); border-radius: var(--corn-radius-lg); background: var(--corn-surface-1); color: var(--corn-text-2); box-shadow: var(--corn-shadow); text-align: center; font-family: var(--corn-font); font-size: var(--corn-text-md); line-height: 1.5; }
|
|
6
|
+
[data-corn="source-status"][hidden] { display: none; }
|
|
7
|
+
[data-corn="source-status"]:focus-visible { outline: var(--corn-focus-width) solid var(--corn-focus); outline-offset: var(--corn-focus-offset); }
|
|
8
|
+
/* Loading (`running`) is quiet: a line of text on the ground, no card */
|
|
9
|
+
[data-corn="source-status"][data-corn-state="running"] { padding: 0; border-color: transparent; background: transparent; box-shadow: none; color: var(--corn-text-3); }
|
|
10
|
+
[data-corn="source-status"][data-corn-state="running"] [data-corn="source-title"] { font-size: var(--corn-text-md); font-weight: 400; color: var(--corn-text-3); }
|
|
11
|
+
[data-corn="source-status"][data-corn-state="failure"] [data-corn="source-title"] { color: var(--corn-bad); }
|
|
12
|
+
[data-corn="source-status"][data-corn-state="waiting"] [data-corn="source-title"] { color: var(--corn-accent-text); }
|
|
13
|
+
[data-corn="source-title"], [data-corn="source-text"] { margin: 0; }
|
|
14
|
+
[data-corn="source-title"] { font-size: var(--corn-text-lg); font-weight: 600; color: var(--corn-text); }
|
|
15
|
+
[data-corn="source-text"] { overflow-wrap: anywhere; user-select: text; }
|
|
16
|
+
[data-corn="source-origin"] { font-family: var(--corn-mono); color: var(--corn-warn); overflow-wrap: anywhere; user-select: all; }
|
|
17
|
+
[data-corn="source-sign-in"], [data-corn="source-retry"] { box-sizing: border-box; display: inline-flex; align-items: center; justify-content: center; min-height: max(var(--corn-control-height), 2.75rem); padding: 0 var(--corn-space-6); border: var(--corn-border) solid var(--corn-line); border-radius: var(--corn-radius); background: var(--corn-surface-2); color: var(--corn-text); font-family: inherit; font-size: inherit; text-decoration: none; cursor: pointer; }
|
|
18
|
+
[data-corn="source-sign-in"] { border-color: var(--corn-accent); background: var(--corn-accent); color: var(--corn-text-on-accent); font-weight: 600; }
|
|
19
|
+
[data-corn="source-sign-in"]:hover { background: var(--corn-accent-hover); }
|
|
20
|
+
[data-corn="source-retry"]:hover { background: var(--corn-surface-3); }
|
|
21
|
+
[data-corn="source-sign-in"]:focus-visible, [data-corn="source-retry"]:focus-visible { outline: var(--corn-focus-width) solid var(--corn-focus); outline-offset: var(--corn-focus-offset); }
|
|
22
|
+
/* The version notice: one quiet line along the bottom of the stage; the map goes on under it */
|
|
23
|
+
[data-corn="source-notice"] { position: absolute; z-index: 2; left: 0; right: 0; bottom: 0; box-sizing: border-box; margin: 0; padding: var(--corn-space-3) var(--corn-space-6); background: var(--corn-surface-1); border-top: var(--corn-border) solid var(--corn-line-soft); color: var(--corn-text-2); font-family: var(--corn-font); font-size: var(--corn-text-sm); text-align: center; overflow-wrap: anywhere; }
|
|
24
|
+
[data-corn="source-notice"][hidden] { display: none; }
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module explorer/pieces/status
|
|
3
|
+
*
|
|
4
|
+
* What a person sees while a map loads from a server, and when it cannot (spec 0011 D5, D6, D11): the quiet "Loading", "You are not signed in to
|
|
5
|
+
* cornfield" (or, for a 403, "This map is for the project's maintainers") with a real link "Sign in to cornfield" to the server's sign-in, "This page
|
|
6
|
+
* is not listed by that cornfield" with the origin it should list, offline with Retry, a server's own error, a `src` that is not an address, and the notice that the package and the server are different versions. It is engine markup, mounted by the
|
|
7
|
+
* map piece inside its stage (pieces/map.js); a shell holds none of it. Words are `strings` keys (core/strings.js), each element has a `data-corn`
|
|
8
|
+
* hook and so a `part`, and nothing here is built from data except through `escapeHtml`, `textContent` and a link checked to be http(s).
|
|
9
|
+
*
|
|
10
|
+
* const status = mount(container, inst, { onRetry }) -> { destroy, refuse }
|
|
11
|
+
*
|
|
12
|
+
* It follows the instance's `source` event (engine/source.js) and reads where the load is now from `sourceOf(inst)`, so a piece that joins late
|
|
13
|
+
* shows the right thing. A polite live region, apart from the card, says each state once (a second `server` error says itself again: it is a
|
|
14
|
+
* second answer). Focus is never taken; the one exception is the person's own Retry, after which focus goes to the card, or to the map when it loads.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { escapeHtml } from '../../core/text.js';
|
|
18
|
+
import { sourceOf } from '../engine/source.js';
|
|
19
|
+
import { t } from '../engine/strings.js';
|
|
20
|
+
import { withParts } from '../engine/piece.js';
|
|
21
|
+
|
|
22
|
+
/** Each state's public `data-corn-state` (docs/theming.md "States"): loading is running, a sign-in is waiting on the person, a refusal is a failure, offline is stopped */
|
|
23
|
+
const STATE_OF = Object.freeze({ loading: 'running', 'sign-in': 'waiting', 'not-allowed': 'failure', offline: 'stopped', server: 'failure', address: 'failure', 'second-map': 'failure' });
|
|
24
|
+
|
|
25
|
+
/** The words of a state: `title` and `text` are `strings` keys, `extra` is data the state adds (escaped when shown) */
|
|
26
|
+
function wordsOf(inst, detail) {
|
|
27
|
+
switch (detail.state) {
|
|
28
|
+
case 'loading': return { title: t(inst, 'sourceLoading'), text: '' };
|
|
29
|
+
// What is the case is the title (signed out, or signed in as someone who is not a maintainer: a 403); what to do is the link
|
|
30
|
+
case 'sign-in': return detail.status === 403
|
|
31
|
+
? { title: t(inst, 'sourceNotMaintainer'), text: t(inst, 'sourceNotMaintainerText'), link: detail.signIn }
|
|
32
|
+
: { title: t(inst, 'sourceSignedOut'), text: t(inst, 'sourceSignInText'), link: detail.signIn };
|
|
33
|
+
case 'not-allowed': return { title: t(inst, 'sourceNotListed'), text: t(inst, 'sourceNotListedHint'), origin: detail.origin, retry: true };
|
|
34
|
+
case 'offline': return { title: t(inst, 'sourceOffline'), text: t(inst, 'sourceOfflineText'), retry: true };
|
|
35
|
+
case 'server': return { title: t(inst, 'errorTitle'), text: detail.message || '', retry: true };
|
|
36
|
+
case 'address': return { title: t(inst, 'sourceBadAddress'), text: detail.problem || '' };
|
|
37
|
+
case 'second-map': return { title: t(inst, 'sourceSecondMap'), text: '' };
|
|
38
|
+
default: return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** A link that is safe to put in an `href`: an absolute http(s) address, as the server's sign-in is */
|
|
43
|
+
const safeHref = (href) => (typeof href === 'string' && /^https?:\/\/[^\s\\]+$/i.test(href) ? href : '');
|
|
44
|
+
|
|
45
|
+
/** The markup of one state (the card), escaped */
|
|
46
|
+
export function cardHtml(inst, detail) {
|
|
47
|
+
const words = wordsOf(inst, detail);
|
|
48
|
+
if (!words) return '';
|
|
49
|
+
const href = words.link ? safeHref(words.link) : '';
|
|
50
|
+
const parts = [`<p data-corn="source-title">${escapeHtml(words.title)}</p>`];
|
|
51
|
+
if (words.text) parts.push(`<p data-corn="source-text">${escapeHtml(words.text)}</p>`);
|
|
52
|
+
if (words.origin) parts.push(`<p data-corn="source-text"><code data-corn="source-origin">${escapeHtml(words.origin)}</code></p>`);
|
|
53
|
+
if (href) parts.push(`<a data-corn="source-sign-in" data-corn-kind="primary" href="${escapeHtml(href)}" target="_top" rel="noopener">${escapeHtml(t(inst, 'sourceSignIn'))}</a>`);
|
|
54
|
+
if (words.retry) parts.push(`<button type="button" data-corn="source-retry" data-corn-kind="secondary">${escapeHtml(t(inst, 'retry'))}</button>`);
|
|
55
|
+
return parts.join('');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The words the polite region says for a state: its title and its sentence, once */
|
|
59
|
+
export function announcement(inst, detail) {
|
|
60
|
+
const words = wordsOf(inst, detail);
|
|
61
|
+
return words ? [words.title, words.text, words.origin].filter(Boolean).join('. ') : '';
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @param {Element} container where the card goes (the map's stage)
|
|
66
|
+
* @param {ReturnType<typeof import('../instance.js').createInstance>} inst
|
|
67
|
+
* @param {{ onRetry?: () => void, focusMap?: () => void, follow?: boolean }} [options] `follow: false`: the piece shows only what `refuse` tells it (a second map)
|
|
68
|
+
*/
|
|
69
|
+
export function mount(container, inst, { onRetry = () => {}, focusMap = () => {}, follow = true } = {}) {
|
|
70
|
+
const doc = inst.doc;
|
|
71
|
+
const active = () => { const r = container.getRootNode(); return (r && r.activeElement) || null; };
|
|
72
|
+
const make = (html) => {
|
|
73
|
+
const template = doc.createElement('template');
|
|
74
|
+
template.innerHTML = withParts(html);
|
|
75
|
+
return template.content.firstElementChild;
|
|
76
|
+
};
|
|
77
|
+
const card = make('<div data-corn="source-status" hidden></div>');
|
|
78
|
+
const notice = make('<p data-corn="source-notice" hidden></p>');
|
|
79
|
+
const live = make('<p data-corn="source-announcer" class="hx-sr-only" role="status" aria-live="polite" aria-atomic="true"></p>');
|
|
80
|
+
container.append(card, notice, live);
|
|
81
|
+
let shown = null;
|
|
82
|
+
let ended = false;
|
|
83
|
+
|
|
84
|
+
const render = (detail) => {
|
|
85
|
+
if (ended || !detail) return;
|
|
86
|
+
const words = wordsOf(inst, detail);
|
|
87
|
+
const hadFocus = card.contains(active());
|
|
88
|
+
if (!words) {
|
|
89
|
+
card.hidden = true;
|
|
90
|
+
card.textContent = '';
|
|
91
|
+
shown = detail.state;
|
|
92
|
+
if (hadFocus) focusMap(); // the card the focus was on is gone: it goes to the map
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
card.innerHTML = withParts(cardHtml(inst, detail));
|
|
96
|
+
card.setAttribute('data-corn-state', STATE_OF[detail.state]);
|
|
97
|
+
card.hidden = false;
|
|
98
|
+
const again = shown !== detail.state || detail.state === 'server';
|
|
99
|
+
shown = detail.state;
|
|
100
|
+
if (again) live.textContent = announcement(inst, detail);
|
|
101
|
+
if (hadFocus) { card.setAttribute('tabindex', '-1'); card.focus(); } // the person's own Retry: the focus stays with what they were doing
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const showNotice = (detail) => {
|
|
105
|
+
if (ended) return;
|
|
106
|
+
if (!detail) { notice.hidden = true; notice.textContent = ''; return; }
|
|
107
|
+
notice.textContent = `${t(inst, 'sourceVersion')} (${detail.package} / ${detail.server})`;
|
|
108
|
+
notice.hidden = false;
|
|
109
|
+
// The notice is a quiet line the load goes on under: it is said once, in the same region, after what the load said
|
|
110
|
+
live.textContent = `${live.textContent ? `${live.textContent}. ` : ''}${notice.textContent}`;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const onClick = (event) => {
|
|
114
|
+
const retry = event.target && event.target.closest ? event.target.closest('[data-corn="source-retry"]') : null;
|
|
115
|
+
if (retry && card.contains(retry)) { event.preventDefault(); onRetry(); }
|
|
116
|
+
};
|
|
117
|
+
const offClick = inst.listen(card, 'click', onClick);
|
|
118
|
+
|
|
119
|
+
const off = follow ? inst.on('source', (detail) => {
|
|
120
|
+
if (detail.state === 'version') showNotice(detail); else render(detail);
|
|
121
|
+
}) : () => {};
|
|
122
|
+
|
|
123
|
+
// A piece that joins after the load began shows where it is now
|
|
124
|
+
const source = follow ? sourceOf(inst) : null;
|
|
125
|
+
if (source) {
|
|
126
|
+
const current = source.state();
|
|
127
|
+
if (current && current.state !== 'ready') render(current);
|
|
128
|
+
if (source.notice) showNotice(source.notice);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
/** Says that this piece cannot have the scope it is in (a second map): the card says why, and the piece draws nothing */
|
|
133
|
+
refuse(why = 'second-map') { render({ state: why }); },
|
|
134
|
+
destroy() {
|
|
135
|
+
if (ended) return;
|
|
136
|
+
ended = true;
|
|
137
|
+
off();
|
|
138
|
+
offClick();
|
|
139
|
+
card.remove();
|
|
140
|
+
notice.remove();
|
|
141
|
+
live.remove();
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
}
|
package/explorer/plugins.js
CHANGED
|
@@ -38,16 +38,6 @@ export const failedSource = (slots) => Object.freeze({
|
|
|
38
38
|
},
|
|
39
39
|
});
|
|
40
40
|
|
|
41
|
-
/** The config the server hands the browser; a page opened without the server reads as none */
|
|
42
|
-
export async function loadClientConfig(fetcher = (...a) => globalThis.fetch(...a)) {
|
|
43
|
-
try {
|
|
44
|
-
const res = await fetcher('/cornfield/config.json', { cache: 'no-store' });
|
|
45
|
-
return res.ok ? await res.json() : {};
|
|
46
|
-
} catch {
|
|
47
|
-
return {};
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
41
|
/** An <img> for a brand image (the server's same-origin URL): decorative, the button it sits in carries the name; `alt` only shows if it fails to load */
|
|
52
42
|
function brandImage(doc, src, kind, alt = '') {
|
|
53
43
|
const img = doc.createElement('img');
|