@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.
@@ -301,12 +301,16 @@ export function mount(root, inst, options = {}) {
301
301
  scope.on('layers:redraw', renderLayerToggles);
302
302
  scope.on('theme', renderLayerToggles); // the dots and the legends are drawn in the theme's colours
303
303
  scope.on('view', syncView);
304
+ // The marks legend is there while a proposal is open (the proposal's bar says so: engine/proposal.js)
305
+ const showLegend = (open) => { const legend = own('proposal-legend'); if (legend) legend.hidden = !open; };
306
+ scope.on('proposal:draw', ({ open }) => showLegend(open));
304
307
  // ... and when the map's own tokens change (a named theme, its stylesheet arriving, light and dark): the same signal the map is drawn again on
305
308
  scope.own(mapStyleOf(inst).onChange(() => { if (!redrawing(inst)) renderLayerToggles(); }));
306
309
  scope.own(() => made.forEach((el) => el.remove()));
307
310
 
308
311
  syncView();
309
312
  renderLayerToggles();
313
+ showLegend(!!inst.state.proposal);
310
314
 
311
315
  return {
312
316
  elements: made,
@@ -10,7 +10,9 @@
10
10
  * const piece = mount(root, inst, options) -> { update, destroy, ... }
11
11
  *
12
12
  * root where the piece's elements go when `options.slots` names no place: an element or a shadow root (they go in a stage of their own there)
13
- * inst the instance (instance.js) the map runs on; null makes one over `root`, which `destroy()` then ends too
13
+ * inst the instance (instance.js) the map runs on; null makes one over `root`, which `destroy()` then ends too. A scope's instance (pieces/scope.js):
14
+ * the map attaches its root to it (one map per scope: a second mount draws nothing and says why), takes the topology the scope's source
15
+ * holds unless it has one of its own, and does not end the instance when it ends
14
16
  * options see below; `update(next)` takes the same keys and applies what changed
15
17
  *
16
18
  * Options, all optional:
@@ -20,6 +22,11 @@
20
22
  * theme 'light' | 'dark' | 'auto': the ground the map is drawn for. 'auto' (and nothing) follows the document's own `data-theme`, else prefers-color-scheme
21
23
  * renderer 'auto' | 'canvas2d'
22
24
  * base, fetch, strings for a piece that makes its own instance
25
+ * src the base address of a cornfield server, for a piece with no scope around it: it makes a private scope of its own (pieces/scope.js) that reads
26
+ * the config and the topology from there, with the maintainer's session, and shows the states of that load (pieces/status.js). Not applied
27
+ * by `update`: a new server is a new instance, so the shell mounts the piece again. pieces/scope.js and what it loads are imported then,
28
+ * a chunk of the package's own, so a map that is handed its topology never loads them (the piece answers at once; see mountWithSource)
29
+ * useTheme, version, pageUrl, onAuth, onError for that private scope (see mountScope)
23
30
  * slots { stage: Node }: the place the elements go before (cornfield's page: the marker in its stage). Without it they go in a stage of their own in `root`
24
31
  * keysHelp the words the canvas is described by; default KEYS_HELP. cornfield's page, which has the node panel and Freeform, says KEYS_HELP_WITH_PANEL
25
32
  * tokens the default `--corn-*` tokens (CSS text) to add, once, to the document, and to the shadow root the map's host is in if it is in one (adopt-tokens.js)
@@ -49,6 +56,7 @@ import { buildDataFlow } from '../flow-build.js';
49
56
  import { resolveTheme } from '../theme-model.js';
50
57
  import { escapeHtml } from '../../core/text.js';
51
58
  import { adoptDefaultTokens } from './adopt-tokens.js';
59
+ import { scopeInfo } from './scope-link.js';
52
60
 
53
61
  /** The keys of a map on its own (engine/keyboard.js): what the canvas is described by */
54
62
  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. Escape clears the selection.';
@@ -70,6 +78,28 @@ export const markup = ({ keysHelp = KEYS_HELP } = {}) => ({
70
78
  <canvas id="label-canvas" data-corn="label-canvas" aria-hidden="true"></canvas>`,
71
79
  });
72
80
 
81
+ /**
82
+ * The piece of a second map in a scope: the engine holds one map per instance (its state has one canvas, one camera, one selection), so this one draws
83
+ * nothing. It says why, in its own stage (pieces/status.js), and tells the scope, which says it as `cornError` `{ kind: 'second-map' }`.
84
+ */
85
+ function refused(root, inst, info) {
86
+ const made = place(inst, root, '<div data-corn="map-stage"></div>', null, 'stage');
87
+ const status = info.mountStatus(made[0], inst, { follow: false });
88
+ status.refuse('second-map');
89
+ inst.emit('piece:refused', { piece: 'map', reason: 'second-map', message: 'A scope holds one map.', host: root.nodeType === 11 ? /** @type {ShadowRoot} */ (root).host : root });
90
+ const none = () => {};
91
+ return {
92
+ inst,
93
+ refused: true,
94
+ update: none,
95
+ fit: none,
96
+ refresh: none,
97
+ focusNode: () => false,
98
+ applyTheme: none,
99
+ destroy() { status.destroy(); for (const el of made) el.remove(); },
100
+ };
101
+ }
102
+
73
103
  /** The document or shadow root the map's host sits in: where the default tokens go */
74
104
  function hostRootOf(root) {
75
105
  const host = root.nodeType === 11 ? /** @type {ShadowRoot} */ (root).host : root;
@@ -82,9 +112,63 @@ function hostRootOf(root) {
82
112
  * @param {object} [options]
83
113
  */
84
114
  export function mount(root, inst, options = {}) {
115
+ if (!inst && options.src !== undefined && options.src !== null && options.src !== '') return mountWithSource(root, options);
116
+ return mountOn(root, inst, options, null);
117
+ }
118
+
119
+ /**
120
+ * A map with a `src` and no scope around it makes a private scope on its own element: the same loading, the same states, one tag. The scope's code
121
+ * (pieces/scope.js and what it loads: the reads, the states, the server's theme) is a chunk of the package's own, imported the first time a map has
122
+ * a `src` (a map that is handed its topology never loads it). The piece answers at once: what it is told before the chunk is there is applied when it
123
+ * is, `fit`, `refresh` and `focusNode` do nothing until then (there is no map to act on yet), and `destroy` before then makes nothing.
124
+ */
125
+ function mountWithSource(root, options) {
126
+ const host = root.nodeType === 11 ? /** @type {ShadowRoot} */ (root).host : root;
127
+ const pending = {};
128
+ let real = null;
129
+ let ended = false;
130
+ import('./scope.js').then(({ mountScope }) => {
131
+ if (ended) return;
132
+ const privateScope = mountScope(host, {
133
+ src: options.src, theme: options.theme, useTheme: options.useTheme, tokens: options.tokens, version: options.version, pageUrl: options.pageUrl,
134
+ onAuth: options.onAuth, onError: options.onError,
135
+ });
136
+ real = mountOn(root, privateScope.instance, { ...options, ...pending }, privateScope);
137
+ }, (err) => {
138
+ if (!ended && options.onError) options.onError({ kind: 'offline', message: `the map's loader did not load: ${err && err.message ? err.message : err}` });
139
+ });
140
+ return {
141
+ get inst() { return real ? real.inst : null; },
142
+ get map() { return real ? real.map : null; },
143
+ update(next = {}) { if (real) real.update(next); else Object.assign(pending, next); },
144
+ fit() { if (real) real.fit(); },
145
+ focusNode(id) { return real ? real.focusNode(id) : false; },
146
+ refresh() { if (real) real.refresh(); },
147
+ applyTheme(theme) { return real ? real.applyTheme(theme) : null; },
148
+ destroy() {
149
+ if (ended) return;
150
+ ended = true;
151
+ if (real) real.destroy();
152
+ real = null;
153
+ },
154
+ };
155
+ }
156
+
157
+ /** The map on an instance: a scope's, its own private scope's (`privateScope`, which it ends when it ends), or one it makes */
158
+ function mountOn(root, inst, options, privateScope) {
85
159
  const ownsInstance = !inst;
86
160
  if (ownsInstance) inst = createInstance({ root, base: options.base || '', fetch: options.fetch, strings: options.strings });
161
+ // An instance that belongs to a scope holds one map: the map attaches its shadow root to it, and a second map is told why it has no map
162
+ const info = scopeInfo(inst);
163
+ let releaseRoot = () => {};
164
+ if (info) {
165
+ try { releaseRoot = inst.attach(root); } catch {
166
+ if (privateScope) privateScope.destroy();
167
+ return refused(root, inst, info);
168
+ }
169
+ }
87
170
  const { state } = inst;
171
+ const source = info ? info.source : null;
88
172
  if (options.tokens) {
89
173
  // The defaults go on the document, whose :root every element inherits from (through every shadow root); where the map's host sits in a
90
174
  // shadow root of the page's own, that root gets the sheet too, for the rules on the element's `theme` attribute, which match only there
@@ -94,9 +178,13 @@ export function mount(root, inst, options = {}) {
94
178
  }
95
179
  /** What the piece set up, undone by destroy() (the instance's own end runs it too, if the piece is still there) */
96
180
  const scope = scopeOf(inst);
181
+ scope.own(releaseRoot);
182
+ if (privateScope) scope.own(() => privateScope.destroy());
97
183
  const rendererOf = (v) => (v === 'canvas2d' ? 'canvas2d' : 'auto');
98
184
  const themeOf = (v) => (v === 'light' || v === 'dark' ? v : 'auto');
99
185
  const current = { renderer: rendererOf(options.renderer), theme: themeOf(options.theme), topology: undefined };
186
+ /** A topology the host handed to this map itself: it wins over the scope's source (the host's property is the host's choice) */
187
+ let ownTopology = options.topology !== undefined && options.topology !== null;
100
188
  let ended = false;
101
189
  let ready = false;
102
190
 
@@ -106,6 +194,12 @@ export function mount(root, inst, options = {}) {
106
194
  const made = place(inst, root, slots ? html : `<div data-corn="map-stage">${html}</div>`, slots, 'stage');
107
195
  const find = (sel) => made.map((el) => (el.matches(sel) ? el : el.querySelector(sel))).find(Boolean);
108
196
  scope.own(() => { for (const el of [...made, state.canvas]) if (el) el.remove(); });
197
+ // What the person sees while the data loads and when it cannot be had (pieces/status.js): only a map that has a source
198
+ const stage = made.find((el) => el.matches('[data-corn="map-stage"]')) || null;
199
+ if (source && stage) {
200
+ const status = info.mountStatus(stage, inst, { onRetry: () => source.load(), focusMap: () => { if (state.canvas) state.canvas.focus(); } });
201
+ scope.own(status.destroy);
202
+ }
109
203
 
110
204
  // -- what the engine needs on the state, in this order ------------------------------------------------------------------------------
111
205
  state.canvas = find('[data-corn="map-canvas"]');
@@ -124,6 +218,7 @@ export function mount(root, inst, options = {}) {
124
218
  /** The mode the map is drawn for: the element's own `theme`, else the document's `data-theme`, else the system's */
125
219
  function mode() {
126
220
  if (current.theme === 'light' || current.theme === 'dark') return current.theme;
221
+ if (info && (info.theme === 'light' || info.theme === 'dark')) return info.theme; // the scope's, when the map has none of its own
127
222
  const root$ = inst.doc && inst.doc.documentElement;
128
223
  const own = root$ ? root$.getAttribute('data-theme') : null;
129
224
  if (own === 'light' || own === 'dark') return own;
@@ -138,6 +233,9 @@ export function mount(root, inst, options = {}) {
138
233
  map.render();
139
234
  }
140
235
  state.theme = mode();
236
+ // The scope's server theme: its sheet goes into this root, and its tokens (on the scope's element) are read again once it is there or goes
237
+ if (info) scope.own(info.serverTheme.adopt(root));
238
+ if (info) { scope.on('theme', () => refresh()); scope.on('scope:theme', () => refresh()); }
141
239
  if (lightQuery && lightQuery.addEventListener) { lightQuery.addEventListener('change', refresh); scope.own(() => lightQuery.removeEventListener('change', refresh)); }
142
240
  const Mutation = inst.win && inst.win.MutationObserver;
143
241
  if (Mutation && inst.doc && inst.doc.documentElement) { // the host page switching its own light and dark
@@ -219,6 +317,13 @@ export function mount(root, inst, options = {}) {
219
317
 
220
318
  bind();
221
319
  scope.own(() => stopInput());
320
+ // The topology a scope's source holds (read from its server, or handed to the scope), unless this map has one of its own
321
+ const takeSource = () => {
322
+ if (ownTopology || !source || !source.topology || source.topology === current.topology) return;
323
+ current.topology = source.topology;
324
+ setTopology(source.topology);
325
+ };
326
+ if (source) scope.on('source', (detail) => { if (detail.state === 'ready') takeSource(); });
222
327
  scope.own(map.watchSize());
223
328
  map.resize();
224
329
 
@@ -286,7 +391,12 @@ export function mount(root, inst, options = {}) {
286
391
  const value = themeOf(next.theme);
287
392
  if (value !== current.theme) { current.theme = value; refresh(); }
288
393
  }
289
- if ('topology' in next && next.topology !== current.topology) { current.topology = next.topology; setTopology(next.topology); }
394
+ if ('topology' in next) {
395
+ ownTopology = next.topology !== undefined && next.topology !== null;
396
+ if (ownTopology && next.topology !== current.topology) { current.topology = next.topology; setTopology(next.topology); }
397
+ else if (!ownTopology && source && source.topology) { current.topology = undefined; takeSource(); } // the host took its map back: the scope's again
398
+ else if (!ownTopology && next.topology !== current.topology) { current.topology = next.topology; setTopology(next.topology); }
399
+ }
290
400
  if ('view' in next) setView(next.view === 'dataflow' ? 'dataflow' : 'orbit');
291
401
  if ('node' in next) {
292
402
  const id = next.node == null || next.node === '' ? null : String(next.node);
@@ -313,6 +423,7 @@ export function mount(root, inst, options = {}) {
313
423
  const first = {};
314
424
  for (const key of ['topology', 'view', 'node']) if (options[key] !== undefined) first[key] = options[key];
315
425
  update(first);
426
+ takeSource();
316
427
  refresh();
317
428
 
318
429
  return {
@@ -25,7 +25,7 @@
25
25
 
26
26
  import { escapeHtml as escHtml } from '../../core/text.js';
27
27
  import { flowSummary } from '../engine/flow-ui.js';
28
- import { kindOfMark, fieldRows, MARK_WORDS } from '../proposal-model.js';
28
+ import { kindOfMark, fieldRows, MARK_WORDS, proposalPanelHtml } from '../proposal-model.js';
29
29
  import { nodeMockupsHtml } from '../mockups-model.js';
30
30
  import { collapseSheet, initSheets, isPhone, measureCovered, onCoveredChange, registerSheet, unregisterSheet, showSheet, onBackdrop } from '../engine/sheets.js';
31
31
  import { panIntoView } from '../camera.js';
@@ -437,6 +437,38 @@ export function mount(root, inst, options = {}) {
437
437
  inst.emit('node:painted', { node });
438
438
  }
439
439
 
440
+ /**
441
+ * Fills the panel for an edge and opens it: what it is, and what the proposal says about it (the engine's `showEdge` selects it and says `node:show-edge`).
442
+ * As ever, an edge alone gets no sheet on a phone (the panel is opened, not `showSheet`ed), and Escape on the map does not close it.
443
+ */
444
+ function showEdge(edge) {
445
+ if (!edge || gone) return;
446
+ const row = (label, value) => (value ? `<div class="hx-fact" data-corn="section"><div class="hx-fact-k">${escHtml(label)}</div><div class="hx-fact-v">${escHtml(value)}</div></div>` : '');
447
+ const kind = edge.kind || (edge.dashed ? 'usage' : 'ownership');
448
+ const facts = row('Kind', kind) + row('Flow', edge.flow) + row('Label', edge.label) + row('Reads', Array.isArray(edge.keys) ? edge.keys.join(', ') : '');
449
+ const title = own('drawer-title');
450
+ title.textContent = `${sections.nameOf(edge.from)} → ${sections.nameOf(edge.to)}`;
451
+ title.classList.remove('hx-mono');
452
+ own('drawer-badge').textContent = 'Edge';
453
+ own('drawer-host').textContent = '';
454
+ own('drawer-diff').hidden = true;
455
+ const lead = own('drawer-description');
456
+ lead.textContent = '';
457
+ lead.hidden = true;
458
+ metaList.innerHTML = `${facts ? `<div class="hx-facts">${facts}</div>` : ''}
459
+ <div class="hx-facts">
460
+ <div class="hx-fact" data-corn="section"><div class="hx-fact-k">From</div><div class="hx-fact-v"><button data-corn="node-link" type="button" class="hx-linkbtn" data-action="select-node" data-id="${escHtml(edge.from)}">${escHtml(sections.nameOf(edge.from))}</button></div></div>
461
+ <div class="hx-fact" data-corn="section"><div class="hx-fact-k">To</div><div class="hx-fact-v"><button data-corn="node-link" type="button" class="hx-linkbtn" data-action="select-node" data-id="${escHtml(edge.to)}">${escHtml(sections.nameOf(edge.to))}</button></div></div>
462
+ </div>${proposalPanelHtml(edge)}`;
463
+ addParts(metaList);
464
+ own('drawer-open-btn').hidden = true;
465
+ own('drawer-file-btn').hidden = true;
466
+ own('drawer-run-btn').hidden = true;
467
+ drawer.classList.add('drawer-open');
468
+ drawer.inert = false; // closed, the panel is inert (MAP-02): open for an edge, its links and its Close are reached as a node's are
469
+ backdrop.hidden = false;
470
+ }
471
+
440
472
  /**
441
473
  * On a phone the node's sheet covers the bottom of the map (engine/sheets.js): when the selected node is under it, or
442
474
  * gets covered as the sheet expands, pan the map (never zoom it) so the node is in the part still showing (F32)
@@ -498,6 +530,7 @@ export function mount(root, inst, options = {}) {
498
530
 
499
531
  scope.on('node:show', ({ node, frame }) => show(node, { frame }));
500
532
  scope.on('node:close', close);
533
+ scope.on('node:show-edge', ({ edge }) => showEdge(edge));
501
534
  // Enter on the map goes into the open panel: its title takes the focus, so a screen reader starts there and Tab goes on into it
502
535
  scope.on('node:focus', () => {
503
536
  const title = own('drawer-title');