cornfield 0.48.0 → 0.50.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/css/review.css +49 -0
- package/explorer/decision-model.js +6 -0
- package/explorer/engine/checks.js +252 -0
- package/explorer/engine/connection.js +36 -0
- package/explorer/engine/layers.js +1 -0
- package/explorer/engine/layout.js +8 -3
- package/explorer/engine/player.js +489 -0
- package/explorer/engine/proposal.js +373 -0
- package/explorer/engine/render.js +1 -0
- package/explorer/engine/sheets.js +40 -7
- package/explorer/engine/source.js +315 -0
- package/explorer/engine/theme-sheet.js +444 -0
- package/explorer/engine/topology.js +126 -0
- package/explorer/index.html +6 -59
- package/explorer/instance.js +87 -7
- 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 +495 -0
- package/explorer/pieces/decision.js +664 -0
- package/explorer/pieces/layers.js +4 -0
- package/explorer/pieces/map.js +117 -25
- package/explorer/pieces/node-panel.js +34 -1
- package/explorer/pieces/proposal.js +488 -0
- package/explorer/pieces/review.js +280 -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 +12 -64
- package/explorer/waiting-line.js +7 -50
- package/package.json +1 -1
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module explorer/pieces/review
|
|
3
|
+
*
|
|
4
|
+
* The review as one piece (spec 0011 D3, D4, D8): the engine half of the coming `<corn-review>`. It lays out, in ONE root and on ONE instance, what a
|
|
5
|
+
* review needs, and mounts the pieces of the explorer into it through their own `mount(root, inst, { slots })`: the map (pieces/map.js), the proposal's
|
|
6
|
+
* bar (pieces/proposal.js, without the what-if editor's buttons: the editor is not converted), the change list with the scenario player
|
|
7
|
+
* (pieces/changes.js), the decision's foot (pieces/decision.js, in the column's foot) and the node panel (pieces/node-panel.js). It is not cornfield's
|
|
8
|
+
* page: no header, Work panel, search, Layers chip, account menu or hint, and cornfield's page does not use it (it keeps its own layout).
|
|
9
|
+
*
|
|
10
|
+
* const review = mount(root, inst, options) -> { update, destroy, ... }
|
|
11
|
+
*
|
|
12
|
+
* root the host's element (or shadow root): the piece makes ONE box in it (`[data-corn="review"]`), and everything it makes is inside that box
|
|
13
|
+
* inst the instance; null makes one over the box, which `destroy()` then ends too. One instance for every piece inside: the lookups by id that
|
|
14
|
+
* still cross pieces (the dock, the bar, the review column, the node panel, the player's place) find their elements because there is one root
|
|
15
|
+
*
|
|
16
|
+
* Options, all optional; `update(next)` takes the same keys and applies what is present:
|
|
17
|
+
* topology {nodes, edges}: the map. The piece does not load it: it takes what it is given (engine/topology.js `setTopology`, which the map piece calls too)
|
|
18
|
+
* proposal a slug (fetched through `inst.fetch`, so a `base` applies; `source` names the copy), or the proposal object (nothing is fetched: `slug`,
|
|
19
|
+
* `source` and `sha256` may name it, which a decision needs; with no `base` and no `fetch` they are ignored, so nothing is sent anywhere), or null: closes it. Held until a topology is there
|
|
20
|
+
* source where the server found the proposal ('checkout', 'pull/24', a worktree)
|
|
21
|
+
* view 'before' | 'diff' | 'after': the proposal's view; or 'map' | 'dataflow': the map's. The two sets of words do not meet
|
|
22
|
+
* node the selected node's id (null clears it)
|
|
23
|
+
* config { theme, layers }: the repo's client config as /cornfield/config.json carries it, which this piece takes and does not fetch: `theme` starts the
|
|
24
|
+
* theme's map icons (they are fetched without the session, with CORS), `layers` are the project's layers
|
|
25
|
+
* strings the host's own words (core/strings.js keys): the instance speaks in them
|
|
26
|
+
* theme 'light' | 'dark' | 'auto' (the map's ground), checker (a plugin's checker for the scenarios), now (the decision's clock)
|
|
27
|
+
* base, fetch, tokens, renderer, onSelect, onReady as pieces/map.js; `renderer` is read at mount only (a canvas that is replaced loses the edge picking)
|
|
28
|
+
* label the box's accessible name (a region; 'Proposal review' by default), read at mount
|
|
29
|
+
*
|
|
30
|
+
* What a host hears is on the instance already (instance.js): `selection`, `proposal`, `decision`, `auth`, `scenario:player`. This piece adds nothing.
|
|
31
|
+
*
|
|
32
|
+
* Where it lives. The box declares itself the query container its stylesheets ask (`container: corn-box corn-page / size`, css/review.css) and is the
|
|
33
|
+
* containing block of its own `position: fixed` descendants (`contain: layout`), so it needs a host that gives it a height (it fills 100% of it and is never
|
|
34
|
+
* under 24rem). Under 768px by the box's own width, not the window's, it is the page's phone layout with its sheets (engine/sheets.js `setPhoneBox`).
|
|
35
|
+
* The stylesheets to load, in order, after the default tokens and before a theme: `STYLESHEETS` below (explorer/css/).
|
|
36
|
+
*
|
|
37
|
+
* Nothing leaves the box: no listener on `document` or `window`, nothing appended outside it, no id looked up outside it, keys act only with the focus
|
|
38
|
+
* inside, no `location`, no `history`; the wheel is the page's until the map has the focus or Ctrl or Cmd is held (input.js).
|
|
39
|
+
*
|
|
40
|
+
* Not in it: the what-if editor, the mockups, the Work panel, search, the Layers menu, the Proposals menu (its markup is kept in a spare, hidden place: the
|
|
41
|
+
* proposal piece makes it), signing in (the decision says `auth`).
|
|
42
|
+
*
|
|
43
|
+
* @param {Element|ShadowRoot} root
|
|
44
|
+
* @param {ReturnType<typeof import('../instance.js').createInstance>|null} inst
|
|
45
|
+
* @param {object} [options]
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
import { createInstance } from '../instance.js';
|
|
49
|
+
import { mount as mountMap } from './map.js';
|
|
50
|
+
import { mount as mountNodePanel } from './node-panel.js';
|
|
51
|
+
import { mount as mountProposal } from './proposal.js';
|
|
52
|
+
import { mount as mountChanges } from './changes.js';
|
|
53
|
+
import { mount as mountDecision } from './decision.js';
|
|
54
|
+
import { setTopology, layoutMap, setMap } from '../engine/topology.js';
|
|
55
|
+
import { closeProposal, rebuildProposalMap } from '../engine/proposal.js';
|
|
56
|
+
import { measureCovered, setPhoneBox } from '../engine/sheets.js';
|
|
57
|
+
import { setProjectLayers } from '../engine/layers.js';
|
|
58
|
+
import { applyStrings, setStrings } from '../engine/strings.js';
|
|
59
|
+
import { withParts } from '../engine/piece.js';
|
|
60
|
+
import { PROPOSAL_VIEWS } from '../proposal-model.js';
|
|
61
|
+
import { NOTHING_TO_DECIDE } from '../decision-model.js';
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The files of explorer/css/ the review needs, in the order to load them (the cascade's): after the default tokens (explorer/tokens.css, or the
|
|
65
|
+
* generated sheet `tokens` hands the map) and before a theme. Never page.css, which is cornfield's own page. The shell reads this list.
|
|
66
|
+
*/
|
|
67
|
+
export const STYLESHEETS = ['base.css', 'map.css', 'node-panel.css', 'proposal.css', 'changes.css', 'decision.css', 'sheets.css', 'review.css'];
|
|
68
|
+
|
|
69
|
+
/** The keys of the map in the review: the page's, without Freeform (the review has none) */
|
|
70
|
+
export const KEYS_HELP = "Arrow keys pan, plus and minus zoom, 0 fits the map. N and P select the next and the previous node, Home and End the first and the last. F frames the selected node and what it lights. Enter goes to the selected node's panel, Escape closes it.";
|
|
71
|
+
|
|
72
|
+
const MAP_VIEWS = ['map', 'orbit', 'dataflow'];
|
|
73
|
+
|
|
74
|
+
/** What the box says over a map with no nodes */
|
|
75
|
+
export const EMPTY_WORDS = 'There is no map to show: the topology has no nodes.';
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The layout: a stage (the map, and over it the dock, which holds the bar, the review column and the node panel, and the tap-away behind the panel on a
|
|
79
|
+
* phone), as cornfield's page has, with each piece's place marked by a comment where its elements go before. The Proposals menu has a spare place of its own.
|
|
80
|
+
*/
|
|
81
|
+
const LAYOUT = '<div class="hx-stage" data-corn="stage">'
|
|
82
|
+
+ '<!--corn-piece:map/stage-->'
|
|
83
|
+
+ '<div id="sheet-dock" class="hx-dock" data-corn="dock">'
|
|
84
|
+
+ '<!--corn-piece:proposal/bar-->'
|
|
85
|
+
+ '<!--corn-piece:changes/column-->'
|
|
86
|
+
+ '<!--corn-piece:node-panel/drawer-->'
|
|
87
|
+
+ '</div>'
|
|
88
|
+
+ '<!--corn-piece:node-panel/backdrop-->'
|
|
89
|
+
+ '<p class="hx-review-empty" data-corn="empty" role="status" hidden></p>'
|
|
90
|
+
+ '</div>'
|
|
91
|
+
+ '<div class="hx-review-spare" hidden><!--corn-piece:proposal/menu--></div>';
|
|
92
|
+
|
|
93
|
+
/** The marker comments under `el`, by `name/slot` */
|
|
94
|
+
function markersIn(doc, el) {
|
|
95
|
+
const found = {};
|
|
96
|
+
const walker = doc.createTreeWalker(el, 128 /* NodeFilter.SHOW_COMMENT */);
|
|
97
|
+
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
|
|
98
|
+
if (node.data.startsWith('corn-piece:')) found[node.data.slice('corn-piece:'.length)] = node;
|
|
99
|
+
}
|
|
100
|
+
return found;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function mount(root, inst, options = {}) {
|
|
104
|
+
const doc = root.ownerDocument || root;
|
|
105
|
+
// -- the box: ONE root for everything the review makes ---------------------------------------------------------------------------------
|
|
106
|
+
const box = doc.createElement('div');
|
|
107
|
+
box.setAttribute('data-corn', 'review');
|
|
108
|
+
box.setAttribute('part', 'review');
|
|
109
|
+
// A named region: a screen reader finds the review as one place in the host's page (its headings start at h2: the proposal's title and the node's)
|
|
110
|
+
box.setAttribute('role', 'region');
|
|
111
|
+
box.setAttribute('aria-label', typeof options.label === 'string' && options.label.trim() ? options.label.trim() : 'Proposal review');
|
|
112
|
+
box.innerHTML = withParts(LAYOUT);
|
|
113
|
+
root.append(box);
|
|
114
|
+
|
|
115
|
+
const ownsInstance = !inst;
|
|
116
|
+
if (ownsInstance) inst = createInstance({ root: box, base: options.base || '', fetch: options.fetch, strings: options.strings });
|
|
117
|
+
else if (options.strings !== undefined) setStrings(inst, options.strings);
|
|
118
|
+
const { state } = inst;
|
|
119
|
+
setPhoneBox(inst, box); // a phone is a narrow box, whatever the window is (engine/sheets.js)
|
|
120
|
+
applyStrings(inst, box);
|
|
121
|
+
let ended = false;
|
|
122
|
+
|
|
123
|
+
// -- the pieces, in the order the page mounts them: the map first, the node panel, the bar, the column, the decision in the column's foot -----
|
|
124
|
+
const at = markersIn(doc, box);
|
|
125
|
+
const mapPiece = mountMap(box, inst, {
|
|
126
|
+
slots: { stage: at['map/stage'] },
|
|
127
|
+
measureCovered: () => measureCovered(inst),
|
|
128
|
+
keysHelp: KEYS_HELP,
|
|
129
|
+
renderer: options.renderer,
|
|
130
|
+
theme: options.theme,
|
|
131
|
+
tokens: options.tokens,
|
|
132
|
+
onSelect: options.onSelect,
|
|
133
|
+
onReady: options.onReady,
|
|
134
|
+
});
|
|
135
|
+
const nodePanel = mountNodePanel(box, inst, { slots: { drawer: at['node-panel/drawer'], backdrop: at['node-panel/backdrop'] } });
|
|
136
|
+
const proposalPiece = mountProposal(box, inst, { slots: { bar: at['proposal/bar'], menu: at['proposal/menu'] }, edit: false });
|
|
137
|
+
const changesPiece = mountChanges(box, inst, { slots: { column: at['changes/column'] }, checker: options.checker });
|
|
138
|
+
const foot = markersIn(doc, box)['decision/foot']; // the column keeps a place for it
|
|
139
|
+
const decisionPiece = mountDecision(box, inst, { slots: { foot }, now: options.now, unnamed: NOTHING_TO_DECIDE }); // handed an object with no server, it says so
|
|
140
|
+
|
|
141
|
+
// The review lays the map out from what the engine hands over (a proposal's nodes and edges are the base with marks): nodes that were there keep their place
|
|
142
|
+
const stopAsking = inst.on('map:set', ({ nodes, edges }) => { setMap(inst, nodes, edges); return true; });
|
|
143
|
+
|
|
144
|
+
// -- what the host asked for ---------------------------------------------------------------------------------------------------------------
|
|
145
|
+
/** What has been handed in, so an `update` that repeats a value changes nothing */
|
|
146
|
+
const serverless = !options.base && !options.fetch;
|
|
147
|
+
const current = { topology: undefined, proposal: null, source: null, slug: null, sha256: null, proposalView: null, shown: null, hasTopology: false, held: false };
|
|
148
|
+
|
|
149
|
+
const redraw = () => { proposalPiece.update(); changesPiece.update(); decisionPiece.update(); nodePanel.update(); };
|
|
150
|
+
|
|
151
|
+
/** The proposal asked for, opened (or closed) once there is a map to open it on */
|
|
152
|
+
function applyProposal() {
|
|
153
|
+
if (ended) return undefined;
|
|
154
|
+
const wanted = current.proposal;
|
|
155
|
+
if (wanted === null || wanted === undefined || wanted === '') {
|
|
156
|
+
current.held = false;
|
|
157
|
+
current.shown = null;
|
|
158
|
+
if (state.proposal) closeProposal(inst);
|
|
159
|
+
return undefined;
|
|
160
|
+
}
|
|
161
|
+
if (!current.hasTopology) { current.held = true; return undefined; } // a proposal is laid over a map: it opens when there is one
|
|
162
|
+
current.held = false;
|
|
163
|
+
const view = current.proposalView;
|
|
164
|
+
if (typeof wanted === 'string') {
|
|
165
|
+
const key = `${wanted}\u0000${current.source || ''}`;
|
|
166
|
+
if (current.shown === key && state.proposal) return undefined; // already open (the piece answers true too)
|
|
167
|
+
current.shown = key;
|
|
168
|
+
return proposalPiece.update({ proposal: wanted, source: current.source, view });
|
|
169
|
+
}
|
|
170
|
+
if (current.shown === wanted && state.proposal) return undefined;
|
|
171
|
+
current.shown = wanted;
|
|
172
|
+
// With no server to ask (no `base`, no `fetch`) the object is all there is: its name and source would only send the pieces to the host's own origin for a PR's checks and reviews
|
|
173
|
+
const named = serverless ? {} : { source: current.source, slug: current.slug, sha256: current.sha256 };
|
|
174
|
+
proposalPiece.update({ proposal: wanted, ...named, view });
|
|
175
|
+
return undefined;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** The topology changed: the base map is the host's new one, laid out and fitted; an open proposal is laid over it again */
|
|
179
|
+
function applyTopology(topology) {
|
|
180
|
+
current.hasTopology = setTopology(inst, topology);
|
|
181
|
+
// A topology with nothing on it says so in words, never a blank box (no word before a topology is handed in: the host may still be loading it)
|
|
182
|
+
const empty = box.querySelector('.hx-review-empty');
|
|
183
|
+
empty.textContent = current.hasTopology ? '' : EMPTY_WORDS;
|
|
184
|
+
empty.hidden = current.hasTopology;
|
|
185
|
+
if (!current.hasTopology) { inst.map.render(); return; }
|
|
186
|
+
layoutMap(inst);
|
|
187
|
+
if (state.proposal) rebuildProposalMap(inst, undefined, { fit: true });
|
|
188
|
+
else mapPiece.fit();
|
|
189
|
+
inst.map.render();
|
|
190
|
+
inst.map.ensureFlowLoop();
|
|
191
|
+
if (current.held) applyProposal();
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** The map's view (whole map or the data-flow lanes): laid out again, the camera fitted, a playing scenario ended (as the page's switch does) */
|
|
195
|
+
function setMapView(view) {
|
|
196
|
+
const next = view === 'dataflow' ? 'dataflow' : 'orbit';
|
|
197
|
+
if (state.view === next) return;
|
|
198
|
+
state.view = next;
|
|
199
|
+
inst.emit('scenario:clear');
|
|
200
|
+
inst.touchScene();
|
|
201
|
+
if (state.nodes.length) {
|
|
202
|
+
setMap(inst, state.mapNodes, state.mapEdges);
|
|
203
|
+
mapPiece.fit();
|
|
204
|
+
inst.map.ensureFlowLoop();
|
|
205
|
+
}
|
|
206
|
+
inst.emit('view', state.view);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** The words of `view`: the proposal's (before, diff, after) or the map's (map, dataflow); the two sets do not meet */
|
|
210
|
+
function applyView(view) {
|
|
211
|
+
if (PROPOSAL_VIEWS.includes(view)) {
|
|
212
|
+
current.proposalView = view;
|
|
213
|
+
if (state.proposal) proposalPiece.update({ view });
|
|
214
|
+
} else if (MAP_VIEWS.includes(view)) setMapView(view);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function applyConfig(config) {
|
|
218
|
+
if (!config || typeof config !== 'object') return;
|
|
219
|
+
if ('layers' in config) setProjectLayers(inst, config.layers);
|
|
220
|
+
if (config.theme) mapPiece.applyTheme(config.theme);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Applies what changed in `next` (the options above). Only the keys present are looked at.
|
|
225
|
+
* @returns {Promise<boolean>|undefined} when a proposal is fetched: whether it opened
|
|
226
|
+
*/
|
|
227
|
+
function update(next = {}) {
|
|
228
|
+
if (ended) return undefined;
|
|
229
|
+
let again = false;
|
|
230
|
+
if ('strings' in next) { setStrings(inst, next.strings); applyStrings(inst, box); again = true; }
|
|
231
|
+
if ('config' in next) applyConfig(next.config);
|
|
232
|
+
if ('theme' in next) mapPiece.update({ theme: next.theme });
|
|
233
|
+
if ('checker' in next) changesPiece.update({ checker: next.checker });
|
|
234
|
+
if ('topology' in next && next.topology !== current.topology) { current.topology = next.topology; applyTopology(next.topology); }
|
|
235
|
+
let result;
|
|
236
|
+
const asked = ['proposal', 'source', 'slug', 'sha256'].some((k) => k in next);
|
|
237
|
+
if (asked) {
|
|
238
|
+
if ('source' in next) current.source = next.source || null;
|
|
239
|
+
if ('slug' in next) current.slug = next.slug || null;
|
|
240
|
+
if ('sha256' in next) current.sha256 = next.sha256 || null;
|
|
241
|
+
if ('proposal' in next) { current.proposal = next.proposal; current.shown = null; }
|
|
242
|
+
if ('view' in next && PROPOSAL_VIEWS.includes(next.view)) current.proposalView = next.view;
|
|
243
|
+
result = applyProposal();
|
|
244
|
+
}
|
|
245
|
+
if ('view' in next && !(asked && PROPOSAL_VIEWS.includes(next.view))) applyView(next.view);
|
|
246
|
+
if ('node' in next) mapPiece.update({ node: next.node });
|
|
247
|
+
if (again) redraw();
|
|
248
|
+
return result;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** Ends the piece: every piece in it, the box, and the instance if the piece made it. Safe to call twice. */
|
|
252
|
+
function destroy() {
|
|
253
|
+
if (ended) return;
|
|
254
|
+
ended = true;
|
|
255
|
+
stopAsking();
|
|
256
|
+
for (const piece of [decisionPiece, changesPiece, proposalPiece, nodePanel, mapPiece]) piece.destroy();
|
|
257
|
+
setPhoneBox(inst, null);
|
|
258
|
+
box.remove();
|
|
259
|
+
if (ownsInstance) inst.destroy();
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// What was handed in at mount is applied as an update, in the order update() keeps (strings, theme and the checker were taken above)
|
|
263
|
+
const first = {};
|
|
264
|
+
for (const key of ['config', 'topology', 'proposal', 'source', 'slug', 'sha256', 'view', 'node']) if (options[key] !== undefined) first[key] = options[key];
|
|
265
|
+
update(first);
|
|
266
|
+
|
|
267
|
+
return {
|
|
268
|
+
inst,
|
|
269
|
+
elements: [box],
|
|
270
|
+
map: mapPiece,
|
|
271
|
+
proposal: proposalPiece,
|
|
272
|
+
changes: changesPiece,
|
|
273
|
+
decision: decisionPiece,
|
|
274
|
+
nodePanel,
|
|
275
|
+
update,
|
|
276
|
+
destroy,
|
|
277
|
+
/** The whole map in view */
|
|
278
|
+
fit: () => mapPiece.fit(),
|
|
279
|
+
};
|
|
280
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module explorer/pieces/scope-link
|
|
3
|
+
*
|
|
4
|
+
* The part of the scope protocol (pieces/scope.js) a piece needs, and nothing of the scope itself: how a piece asks for the scope around it
|
|
5
|
+
* (`joinScope`), the two event names, and what a scope's instance tells its pieces (`scopeInfo`). A map that never meets a scope or a `src` loads
|
|
6
|
+
* only this; the loading, the states, the server's theme (pieces/scope.js, engine/source.js, pieces/status.js, engine/theme-sheet.js) come with
|
|
7
|
+
* `<corn-scope>`, or the first time a map has a `src` (pieces/map.js imports them then, as a chunk of the package's own).
|
|
8
|
+
*
|
|
9
|
+
* const join = joinScope(el, onChange) -> { inst, scope, leave } (a piece's shell calls it for the scope around it, if any)
|
|
10
|
+
* scopeInfo(inst) -> { theme, serverTheme, host, source, mountStatus } for a scope's instance, else null
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export const REQUEST = 'corn-scope-request';
|
|
14
|
+
export const CHANGED = 'corn-scope-changed';
|
|
15
|
+
|
|
16
|
+
/** What a piece may ask a scope for, per instance: set when a scope makes the instance */
|
|
17
|
+
export const infoOf = (inst) => inst.slot('scope', () => ({ theme: 'auto', serverTheme: null, host: null, source: null, mountStatus: null }));
|
|
18
|
+
/**
|
|
19
|
+
* The scope's side of an instance, or null for a private one: its `theme`, its server theme (to adopt into a root), its element, its source (the
|
|
20
|
+
* load: engine/source.js) and how a piece shows the load's states (pieces/status.js `mount`)
|
|
21
|
+
*/
|
|
22
|
+
export const scopeInfo = (inst) => {
|
|
23
|
+
const info = infoOf(inst);
|
|
24
|
+
return info.host ? info : null;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A piece's side: asks for the scope around `el`. `onChange` runs when the answer may be different (a new instance, a scope gone or come): the
|
|
29
|
+
* shell leaves, ends its piece and joins again. `inst` is null when no scope answered (the piece makes a private instance); `leave` is idempotent.
|
|
30
|
+
* @param {Element} el
|
|
31
|
+
* @param {() => void} onChange
|
|
32
|
+
* @returns {{ inst: ReturnType<typeof import('../instance.js').createInstance> | null, scope: object | null, leave: () => void }}
|
|
33
|
+
*/
|
|
34
|
+
export function joinScope(el, onChange) {
|
|
35
|
+
let handle = null;
|
|
36
|
+
el.dispatchEvent(new CustomEvent(REQUEST, { bubbles: true, composed: true, detail: { el, claim: (h) => { handle = h; } } }));
|
|
37
|
+
const changed = () => onChange();
|
|
38
|
+
el.addEventListener(CHANGED, changed);
|
|
39
|
+
let left = false;
|
|
40
|
+
return {
|
|
41
|
+
inst: handle ? handle.inst : null,
|
|
42
|
+
scope: handle ? handle.scope : null,
|
|
43
|
+
leave() {
|
|
44
|
+
if (left) return;
|
|
45
|
+
left = true;
|
|
46
|
+
el.removeEventListener(CHANGED, changed);
|
|
47
|
+
if (handle) handle.release();
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
@@ -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; }
|