@popcorn245/cornfield 0.46.2 → 0.47.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/explorer/engine/hint.js +1 -0
- package/explorer/engine/keyboard.js +2 -2
- package/explorer/engine/map-style.js +7 -2
- package/explorer/flow-build.js +88 -0
- package/explorer/flows.js +1 -81
- package/explorer/index.html +2 -7
- package/explorer/input.js +8 -1
- package/explorer/page-pieces.js +5 -0
- package/explorer/page.js +11 -20
- package/explorer/pieces/adopt-tokens.js +38 -0
- package/explorer/pieces/map-stage.css +14 -0
- package/explorer/pieces/map.js +332 -0
- package/explorer/renderer.js +1 -0
- package/package.json +1 -1
package/explorer/engine/hint.js
CHANGED
|
@@ -45,6 +45,7 @@ export function dismissHint(inst) {
|
|
|
45
45
|
const m = memory(inst);
|
|
46
46
|
if (m.dismissed) return;
|
|
47
47
|
m.dismissed = true;
|
|
48
|
+
if (!inst.el('instruction-badge-text')) return; // an instance with no hint (a map in another app's page) writes nothing in that page's storage
|
|
48
49
|
try { const store = sessionOf(inst); if (store) store.setItem(HINT_STORAGE_KEY, '1'); } catch { /* storage blocked: it holds for this page load */ }
|
|
49
50
|
syncHint(inst);
|
|
50
51
|
}
|
|
@@ -60,7 +60,7 @@ export function centreOn(inst, node) {
|
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
function land(inst, node, order) {
|
|
63
|
-
if (!node) { announce(inst, 'No nodes are shown. Turn a layer on in the Layers menu.'); return; }
|
|
63
|
+
if (!node) { announce(inst, inst.el('legend-dropdown') ? 'No nodes are shown. Turn a layer on in the Layers menu.' : 'No nodes are shown.'); return; } // the Layers menu, when the instance has one
|
|
64
64
|
selectNode(inst, node, { frame: true, source: 'keyboard' }); // the keyboard's selection frames the node and what it lights (spec 0008 D6)
|
|
65
65
|
announce(inst, K.describeNode(node, order.length, order.indexOf(node)));
|
|
66
66
|
}
|
|
@@ -132,7 +132,7 @@ function onMapKey(inst, e) {
|
|
|
132
132
|
break;
|
|
133
133
|
case 'close':
|
|
134
134
|
clearSelection(inst, { source: 'keyboard' });
|
|
135
|
-
announce(inst, 'Panel closed');
|
|
135
|
+
announce(inst, inst.el('spec-drawer') ? 'Panel closed' : 'Selection cleared'); // a map with no node panel beside it (a component) has none to close
|
|
136
136
|
break;
|
|
137
137
|
case 'move':
|
|
138
138
|
moveSelected(inst, action.dx, action.dy);
|
|
@@ -204,10 +204,12 @@ export function readMapTokens(read, extra = []) {
|
|
|
204
204
|
// Icons
|
|
205
205
|
// ---------------------------------------------------------------------------
|
|
206
206
|
|
|
207
|
-
|
|
207
|
+
/** `crossOrigin`: 'anonymous' asks for the image with CORS and no cookie, which WebGL needs for one from another origin (spec 0011 D9) */
|
|
208
|
+
function loadImage(url, crossOrigin = '') {
|
|
208
209
|
return new Promise((resolve, reject) => {
|
|
209
210
|
if (typeof Image === 'undefined') { reject(new Error('no Image')); return; }
|
|
210
211
|
const img = new Image();
|
|
212
|
+
if (crossOrigin) img.crossOrigin = crossOrigin; // before `src`
|
|
211
213
|
img.onload = () => (img.naturalWidth > 0 && img.naturalHeight > 0 ? resolve(img) : reject(new Error(`${url} has no size`)));
|
|
212
214
|
img.onerror = () => reject(new Error(`${url} did not load`));
|
|
213
215
|
img.src = url;
|
|
@@ -348,7 +350,10 @@ export function createMapStyle(inst) {
|
|
|
348
350
|
setIcons(theme, opts = {}) {
|
|
349
351
|
const urls = theme && theme.icons && typeof theme.icons === 'object' ? theme.icons : {};
|
|
350
352
|
pixelated = !!(theme && theme.pixelated === true);
|
|
351
|
-
|
|
353
|
+
// An instance with a base may fetch from another origin (a component on a sibling's page): WebGL refuses an image it was not given by CORS,
|
|
354
|
+
// so the icons are asked for that way. A page on its own origin (no base) is unchanged.
|
|
355
|
+
const crossOrigin = inst.base ? 'anonymous' : '';
|
|
356
|
+
const load = opts.load || ((url) => loadImage(inst.url(url), crossOrigin));
|
|
352
357
|
icons = Object.keys(urls).length ? createIconStore(urls, { ...opts, load, onChange: () => { changed(); if (opts.onChange) opts.onChange(); } }) : null;
|
|
353
358
|
changed();
|
|
354
359
|
return icons;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module explorer/flow-build
|
|
3
|
+
*
|
|
4
|
+
* The data-flow preset's fold (pure: no state, no DOM), so a piece that runs on an instance (explorer/pieces/map.js) can build the preset without
|
|
5
|
+
* importing flows.js, which still reads the page's shared state. flows.js re-exports it.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Folds the topology into the six tiers of the preset. A page is lifted to the app that owns
|
|
10
|
+
* it (the API's endpoints become the API), edges are lifted with them and de-duplicated, and
|
|
11
|
+
* only what stays inside the tiers is kept:
|
|
12
|
+
* browser the stores that live in the visitor's browser (`tier: 'browser'` on a store, or the id `store-idb`)
|
|
13
|
+
* servers the apps whose pages read or write rows
|
|
14
|
+
* API the apps that own endpoints (pages with `methods`)
|
|
15
|
+
* tables SQLite, the audit_log changefeed and every table
|
|
16
|
+
* services the API's jobs, hooks and webhook handlers
|
|
17
|
+
* outside the systems the API reaches: cloud nodes and the self-hosted containers
|
|
18
|
+
* @returns {{ tiers: {key:string,title:string,note:string,nodes:object[]}[], ids: Set<string>, edges: object[] }}
|
|
19
|
+
*/
|
|
20
|
+
export function buildDataFlow(nodes, edges) {
|
|
21
|
+
const byId = new Map(nodes.map((n) => [n.id, n]));
|
|
22
|
+
const owner = new Map();
|
|
23
|
+
edges.forEach((e) => {
|
|
24
|
+
if (!e.kind && !e.dashed && byId.get(e.from)?.type === 'app' && byId.get(e.to)?.type === 'page') owner.set(e.to, e.from);
|
|
25
|
+
});
|
|
26
|
+
// A self-hosted service is the container that runs it (spec 0031 section 3, CLOUD-03): it stands in the
|
|
27
|
+
// Outside tier, and the worker of its stack that sends its webhook (nextcloud-webhooks) is drawn as it
|
|
28
|
+
const hosted = nodes.filter((n) => n.type === 'container' && n.selfHosted);
|
|
29
|
+
const sender = new Map();
|
|
30
|
+
edges.forEach((e) => {
|
|
31
|
+
const from = byId.get(e.from);
|
|
32
|
+
if (e.kind !== 'delivers' || from?.type !== 'container' || from.selfHosted || byId.get(e.to)?.type !== 'page') return;
|
|
33
|
+
const same = hosted.filter((h) => h.stack === from.stack);
|
|
34
|
+
if (same.length === 1) sender.set(from.id, same[0].id);
|
|
35
|
+
});
|
|
36
|
+
const lift = (id) => (byId.get(id)?.type === 'page' ? owner.get(id) || id : sender.get(id) || id);
|
|
37
|
+
|
|
38
|
+
// The API tier: every app that owns an endpoint (a page with methods)
|
|
39
|
+
const apis = new Set([...owner].filter(([page]) => Array.isArray(byId.get(page)?.methods) && byId.get(page).methods.length).map(([, app]) => app));
|
|
40
|
+
const isBrowserStore = (n) => n.type === 'store' && (n.tier === 'browser' || n.id === 'store-idb');
|
|
41
|
+
const withRows = new Set();
|
|
42
|
+
edges.forEach((e) => {
|
|
43
|
+
if (e.kind === 'reads' || e.kind === 'writes') {
|
|
44
|
+
const page = e.kind === 'reads' ? e.to : e.from;
|
|
45
|
+
if (byId.get(page)?.type === 'page') withRows.add(lift(page));
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
// An app whose own code calls the API (or publishes through an outside system) has a derived `api` flow, rows or not
|
|
49
|
+
edges.forEach((e) => {
|
|
50
|
+
if (e.kind === 'flow' && e.flow === 'api' && byId.get(e.from)?.type === 'app') withRows.add(e.from);
|
|
51
|
+
});
|
|
52
|
+
const apiFlowPairs = new Set(edges.filter((e) => e.kind === 'flow' && e.flow === 'api').map((e) => `${lift(e.from)}|${lift(e.to)}`));
|
|
53
|
+
|
|
54
|
+
const pick = (pred) => nodes.filter(pred).sort((a, b) => String(a.label).localeCompare(String(b.label)));
|
|
55
|
+
const tiers = [
|
|
56
|
+
{ key: 'browser', title: 'Browser', note: 'saved on the device first', nodes: pick(isBrowserStore) },
|
|
57
|
+
{ key: 'servers', title: 'App servers', note: 'pages, no-JS forms', nodes: pick((n) => n.type === 'app' && !apis.has(n.id) && withRows.has(n.id)) },
|
|
58
|
+
{ key: 'api', title: 'API', note: 'where the endpoints are', nodes: pick((n) => apis.has(n.id)) },
|
|
59
|
+
{ key: 'tables', title: 'Tables', note: 'stores and tables', nodes: [...pick((n) => n.type === 'store' && !isBrowserStore(n)), ...pick((n) => n.type === 'table')] },
|
|
60
|
+
{ key: 'services', title: 'Services', note: 'jobs, hooks, webhooks', nodes: pick((n) => n.type === 'service') },
|
|
61
|
+
{ key: 'outside', title: 'Outside', note: 'systems the API reaches', nodes: [...pick((n) => n.type === 'external'), ...pick((n) => n.type === 'container' && n.selfHosted)] },
|
|
62
|
+
];
|
|
63
|
+
const ids = new Set(tiers.flatMap((t) => t.nodes.map((n) => n.id)));
|
|
64
|
+
|
|
65
|
+
const out = [];
|
|
66
|
+
const seen = new Set();
|
|
67
|
+
const add = (e, from, to) => {
|
|
68
|
+
if (from === to || !ids.has(from) || !ids.has(to)) return;
|
|
69
|
+
const key = `${from}|${to}|${e.kind}|${e.flow || ''}`;
|
|
70
|
+
if (seen.has(key)) return;
|
|
71
|
+
seen.add(key);
|
|
72
|
+
out.push({ ...e, from, to, status: e.status || 'live', phase: undefined });
|
|
73
|
+
};
|
|
74
|
+
edges.forEach((e) => {
|
|
75
|
+
// A publisher page's `calls` edge to an outside system is the app's `api` flow already
|
|
76
|
+
if (e.kind === 'calls' && apiFlowPairs.has(`${lift(e.from)}|${lift(e.to)}`)) return;
|
|
77
|
+
// What a container pulls or calls itself (its image registry, its own cloud services) is not the API's data flow
|
|
78
|
+
if (e.kind === 'calls' && byId.get(e.from)?.type === 'container') return;
|
|
79
|
+
if (e.kind === 'flow' || ['calls', 'triggers', 'delivers'].includes(e.kind)) return add(e, lift(e.from), lift(e.to));
|
|
80
|
+
if (e.kind === 'reads' || e.kind === 'writes') {
|
|
81
|
+
const page = e.kind === 'reads' ? e.to : e.from;
|
|
82
|
+
// A page of an app server reaches its rows through the API: only the API's own endpoints and the services touch a table here
|
|
83
|
+
if (byId.get(page)?.type === 'page' && !apis.has(lift(page))) return;
|
|
84
|
+
return add(e, lift(e.from), lift(e.to));
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
return { tiers, ids, edges: out };
|
|
88
|
+
}
|
package/explorer/flows.js
CHANGED
|
@@ -25,87 +25,7 @@ export const flowReduceMotion = typeof window !== 'undefined' && !!(window.match
|
|
|
25
25
|
// The data-flow preset
|
|
26
26
|
// ---------------------------------------------------------------------------
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
* Folds the topology into the six tiers of the preset. A page is lifted to the app that owns
|
|
30
|
-
* it (the API's endpoints become the API), edges are lifted with them and de-duplicated, and
|
|
31
|
-
* only what stays inside the tiers is kept:
|
|
32
|
-
* browser the stores that live in the visitor's browser (`tier: 'browser'` on a store, or the id `store-idb`)
|
|
33
|
-
* servers the apps whose pages read or write rows
|
|
34
|
-
* API the apps that own endpoints (pages with `methods`)
|
|
35
|
-
* tables SQLite, the audit_log changefeed and every table
|
|
36
|
-
* services the API's jobs, hooks and webhook handlers
|
|
37
|
-
* outside the systems the API reaches: cloud nodes and the self-hosted containers
|
|
38
|
-
* @returns {{ tiers: {key:string,title:string,note:string,nodes:object[]}[], ids: Set<string>, edges: object[] }}
|
|
39
|
-
*/
|
|
40
|
-
export function buildDataFlow(nodes, edges) {
|
|
41
|
-
const byId = new Map(nodes.map((n) => [n.id, n]));
|
|
42
|
-
const owner = new Map();
|
|
43
|
-
edges.forEach((e) => {
|
|
44
|
-
if (!e.kind && !e.dashed && byId.get(e.from)?.type === 'app' && byId.get(e.to)?.type === 'page') owner.set(e.to, e.from);
|
|
45
|
-
});
|
|
46
|
-
// A self-hosted service is the container that runs it (spec 0031 section 3, CLOUD-03): it stands in the
|
|
47
|
-
// Outside tier, and the worker of its stack that sends its webhook (nextcloud-webhooks) is drawn as it
|
|
48
|
-
const hosted = nodes.filter((n) => n.type === 'container' && n.selfHosted);
|
|
49
|
-
const sender = new Map();
|
|
50
|
-
edges.forEach((e) => {
|
|
51
|
-
const from = byId.get(e.from);
|
|
52
|
-
if (e.kind !== 'delivers' || from?.type !== 'container' || from.selfHosted || byId.get(e.to)?.type !== 'page') return;
|
|
53
|
-
const same = hosted.filter((h) => h.stack === from.stack);
|
|
54
|
-
if (same.length === 1) sender.set(from.id, same[0].id);
|
|
55
|
-
});
|
|
56
|
-
const lift = (id) => (byId.get(id)?.type === 'page' ? owner.get(id) || id : sender.get(id) || id);
|
|
57
|
-
|
|
58
|
-
// The API tier: every app that owns an endpoint (a page with methods)
|
|
59
|
-
const apis = new Set([...owner].filter(([page]) => Array.isArray(byId.get(page)?.methods) && byId.get(page).methods.length).map(([, app]) => app));
|
|
60
|
-
const isBrowserStore = (n) => n.type === 'store' && (n.tier === 'browser' || n.id === 'store-idb');
|
|
61
|
-
const withRows = new Set();
|
|
62
|
-
edges.forEach((e) => {
|
|
63
|
-
if (e.kind === 'reads' || e.kind === 'writes') {
|
|
64
|
-
const page = e.kind === 'reads' ? e.to : e.from;
|
|
65
|
-
if (byId.get(page)?.type === 'page') withRows.add(lift(page));
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
// An app whose own code calls the API (or publishes through an outside system) has a derived `api` flow, rows or not
|
|
69
|
-
edges.forEach((e) => {
|
|
70
|
-
if (e.kind === 'flow' && e.flow === 'api' && byId.get(e.from)?.type === 'app') withRows.add(e.from);
|
|
71
|
-
});
|
|
72
|
-
const apiFlowPairs = new Set(edges.filter((e) => e.kind === 'flow' && e.flow === 'api').map((e) => `${lift(e.from)}|${lift(e.to)}`));
|
|
73
|
-
|
|
74
|
-
const pick = (pred) => nodes.filter(pred).sort((a, b) => String(a.label).localeCompare(String(b.label)));
|
|
75
|
-
const tiers = [
|
|
76
|
-
{ key: 'browser', title: 'Browser', note: 'saved on the device first', nodes: pick(isBrowserStore) },
|
|
77
|
-
{ key: 'servers', title: 'App servers', note: 'pages, no-JS forms', nodes: pick((n) => n.type === 'app' && !apis.has(n.id) && withRows.has(n.id)) },
|
|
78
|
-
{ key: 'api', title: 'API', note: 'where the endpoints are', nodes: pick((n) => apis.has(n.id)) },
|
|
79
|
-
{ key: 'tables', title: 'Tables', note: 'stores and tables', nodes: [...pick((n) => n.type === 'store' && !isBrowserStore(n)), ...pick((n) => n.type === 'table')] },
|
|
80
|
-
{ key: 'services', title: 'Services', note: 'jobs, hooks, webhooks', nodes: pick((n) => n.type === 'service') },
|
|
81
|
-
{ key: 'outside', title: 'Outside', note: 'systems the API reaches', nodes: [...pick((n) => n.type === 'external'), ...pick((n) => n.type === 'container' && n.selfHosted)] },
|
|
82
|
-
];
|
|
83
|
-
const ids = new Set(tiers.flatMap((t) => t.nodes.map((n) => n.id)));
|
|
84
|
-
|
|
85
|
-
const out = [];
|
|
86
|
-
const seen = new Set();
|
|
87
|
-
const add = (e, from, to) => {
|
|
88
|
-
if (from === to || !ids.has(from) || !ids.has(to)) return;
|
|
89
|
-
const key = `${from}|${to}|${e.kind}|${e.flow || ''}`;
|
|
90
|
-
if (seen.has(key)) return;
|
|
91
|
-
seen.add(key);
|
|
92
|
-
out.push({ ...e, from, to, status: e.status || 'live', phase: undefined });
|
|
93
|
-
};
|
|
94
|
-
edges.forEach((e) => {
|
|
95
|
-
// A publisher page's `calls` edge to an outside system is the app's `api` flow already
|
|
96
|
-
if (e.kind === 'calls' && apiFlowPairs.has(`${lift(e.from)}|${lift(e.to)}`)) return;
|
|
97
|
-
// What a container pulls or calls itself (its image registry, its own cloud services) is not the API's data flow
|
|
98
|
-
if (e.kind === 'calls' && byId.get(e.from)?.type === 'container') return;
|
|
99
|
-
if (e.kind === 'flow' || ['calls', 'triggers', 'delivers'].includes(e.kind)) return add(e, lift(e.from), lift(e.to));
|
|
100
|
-
if (e.kind === 'reads' || e.kind === 'writes') {
|
|
101
|
-
const page = e.kind === 'reads' ? e.to : e.from;
|
|
102
|
-
// A page of an app server reaches its rows through the API: only the API's own endpoints and the services touch a table here
|
|
103
|
-
if (byId.get(page)?.type === 'page' && !apis.has(lift(page))) return;
|
|
104
|
-
return add(e, lift(e.from), lift(e.to));
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
return { tiers, ids, edges: out };
|
|
108
|
-
}
|
|
28
|
+
export { buildDataFlow } from './flow-build.js';
|
|
109
29
|
|
|
110
30
|
// ---------------------------------------------------------------------------
|
|
111
31
|
// Legend and the panel's "Data flows" block (engine/flow-ui.js, on an instance)
|
package/explorer/index.html
CHANGED
|
@@ -171,13 +171,8 @@
|
|
|
171
171
|
|
|
172
172
|
<!-- The map -->
|
|
173
173
|
<div class="hx-stage" role="main" data-corn="stage">
|
|
174
|
-
<!-- The map
|
|
175
|
-
|
|
176
|
-
<p id="map-keys-help" class="hx-sr-only">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. In Freeform, Alt and an arrow moves the selected node.</p>
|
|
177
|
-
<!-- What the keyboard landed on, said by a screen reader (keyboard.js) -->
|
|
178
|
-
<p id="map-announcer" class="hx-sr-only" role="status" aria-live="polite"></p>
|
|
179
|
-
<!-- Every label, drawn on its own transparent canvas over the map (explorer/labels.js); the map canvas still gets the pointer -->
|
|
180
|
-
<canvas id="label-canvas" aria-hidden="true" data-corn="label-canvas"></canvas>
|
|
174
|
+
<!-- The map: its canvas (focusable, its keys read out with its name), the help for its keys, the announcer the keyboard speaks in and the canvas its labels are drawn on, made here by explorer/pieces/map.js -->
|
|
175
|
+
<!--corn-piece:map/stage-->
|
|
181
176
|
<!-- The nodes agent runs are changing, marked over the map (explorer/runs-map.js): a picture; the Runs view says the same in words -->
|
|
182
177
|
<div id="run-overlay" class="hx-run-overlay" aria-hidden="true" hidden data-corn="run-overlay"></div>
|
|
183
178
|
|
package/explorer/input.js
CHANGED
|
@@ -51,10 +51,13 @@ export function bindCanvasInput(inst) {
|
|
|
51
51
|
|
|
52
52
|
const canDragNodes = () => state.isFreeform && state.view === 'orbit';
|
|
53
53
|
|
|
54
|
+
/** Where the pointer is, in the map's own pixels: the canvas's box on the screen may be scaled (a transform or `zoom` on an ancestor in a host's page) */
|
|
54
55
|
function getPointerPos(e) {
|
|
55
56
|
const rect = state.canvas.getBoundingClientRect();
|
|
56
57
|
const point = e.touches ? e.touches[0] : e;
|
|
57
|
-
|
|
58
|
+
const sx = rect.width && state.width ? state.width / rect.width : 1;
|
|
59
|
+
const sy = rect.height && state.height ? state.height / rect.height : 1;
|
|
60
|
+
return { x: (point.clientX - rect.left) * sx, y: (point.clientY - rect.top) * sy };
|
|
58
61
|
}
|
|
59
62
|
|
|
60
63
|
// The topology node with this id (the grid answers with scene nodes, which are copies of the picture)
|
|
@@ -216,7 +219,11 @@ export function bindCanvasInput(inst) {
|
|
|
216
219
|
|
|
217
220
|
|
|
218
221
|
const { canvas } = state;
|
|
222
|
+
// In another app's page (a root that is not the document) the wheel is the page's until the map is asked for it: the map has the focus
|
|
223
|
+
// (a click or Tab put it there), or Ctrl or Cmd is held (a trackpad's pinch comes so). A pointer passing over the map does not stop the page scrolling.
|
|
224
|
+
const embedded = !(inst.root && inst.root.nodeType === 9);
|
|
219
225
|
const onWheel = (e) => {
|
|
226
|
+
if (embedded && !e.ctrlKey && !e.metaKey && inst.active() !== state.canvas) return;
|
|
220
227
|
e.preventDefault();
|
|
221
228
|
stopFlight();
|
|
222
229
|
const p = getPointerPos(e);
|
package/explorer/page-pieces.js
CHANGED
|
@@ -35,6 +35,11 @@ function markersOf(doc, name) {
|
|
|
35
35
|
return found;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
+
/** The places of a piece the page mounts itself (the map: page.js), by slot: `{ stage: Comment }` */
|
|
39
|
+
export function pieceSlots(name) {
|
|
40
|
+
return markersOf(pageInstance().doc, name);
|
|
41
|
+
}
|
|
42
|
+
|
|
38
43
|
/** Mounts each piece at its places in the document, or again where the places were replaced; ends one whose places are gone. Returns the handles by name. */
|
|
39
44
|
export function ensurePagePieces() {
|
|
40
45
|
const inst = pageInstance();
|
package/explorer/page.js
CHANGED
|
@@ -29,19 +29,17 @@
|
|
|
29
29
|
|
|
30
30
|
import { state, startPageInstance } from './state.js';
|
|
31
31
|
import { createMap } from './engine/render.js';
|
|
32
|
-
import {
|
|
32
|
+
import { rendererPreference } from './renderer.js';
|
|
33
33
|
import { clearFreeformPositions, layoutOrbit, loadFreeformPositions } from './engine/layout.js';
|
|
34
34
|
import { measureCovered } from './sheets.js';
|
|
35
|
-
import { stopFlight } from './fly.js';
|
|
36
35
|
import { setProjectLayers } from './layers.js';
|
|
37
36
|
import { closeDrawer } from './panels.js';
|
|
38
|
-
import {
|
|
39
|
-
import { ensurePagePieces } from './page-pieces.js';
|
|
37
|
+
import { ensurePagePieces, pieceSlots } from './page-pieces.js';
|
|
40
38
|
import { initScenarioUi, maybeStartDemo, playOutage } from './scenario-ui.js';
|
|
41
39
|
import { initSheets } from './sheets.js';
|
|
42
40
|
import { initReviewColumn } from './review-column.js';
|
|
43
41
|
import { loadTopology, setView, syncViewUi } from './view.js';
|
|
44
|
-
import {
|
|
42
|
+
import { mount as mountMap, KEYS_HELP_WITH_PANEL } from './pieces/map.js';
|
|
45
43
|
import { initProposals } from './proposal.js';
|
|
46
44
|
import { followDeepLink } from './deep-link.js';
|
|
47
45
|
import { initWhatIf } from './whatif.js';
|
|
@@ -173,28 +171,21 @@ export function startPage() {
|
|
|
173
171
|
map = createMap(inst, { measureCovered }); // the camera, the scene and the frame loop of this instance (engine/render.js)
|
|
174
172
|
const mapStyle = mapStyleOf(inst);
|
|
175
173
|
initTheme(); // light or dark, before the first scene is built (theme.js)
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
map.setRenderer(createRenderer(state.canvas, { prefer: rendererPreference(window.location.search), mapStyle: mapStyle.get }));
|
|
174
|
+
// The map piece (pieces/map.js, also what <corn-map> mounts) makes the map's canvases, their keys' help and the announcer where index.html keeps
|
|
175
|
+
// `<!--corn-piece:map/stage-->`. It watches the theme's tokens (the map reads --corn-map-* from the theme, and again when it changes), makes the
|
|
176
|
+
// renderer (WebGL2, or Canvas 2D when it is missing or the address says ?renderer=canvas2d: the address is read here, not by the renderer), binds
|
|
177
|
+
// the canvas and its keys (engine/keyboard.js), follows the box's size, and answers a tap (a node's panel, the normal view, the end of a flight)
|
|
178
|
+
mountMap(document.body, inst, {
|
|
179
|
+
slots: pieceSlots('map'), measureCovered, keysHelp: KEYS_HELP_WITH_PANEL,
|
|
180
|
+
renderer: rendererPreference(window.location.search) === 'canvas2d' ? 'canvas2d' : 'auto',
|
|
181
|
+
});
|
|
185
182
|
loadFreeformPositions(inst);
|
|
186
183
|
ensurePagePieces(); // the node panel, the Layers menu and the search, mounted where index.html has their places (page-pieces.js)
|
|
187
|
-
// What a tap on the map asks of the page: a node's panel, the normal view, the end of a flight to a match
|
|
188
|
-
inst.on('select', (node) => selectNode(inst, node, { source: 'tap' }));
|
|
189
|
-
inst.on('background', () => clearSelection(inst, { source: 'tap' }));
|
|
190
|
-
inst.on('grab', () => stopFlight());
|
|
191
|
-
bindCanvasInput(inst);
|
|
192
184
|
initKeyboard(); // the map by keyboard (the panel's, the Layers menu's and the search's keys are their pieces') (engine/keyboard.js)
|
|
193
185
|
initAccountMenu(); // the one menu at the right of the header (account-menu.js)
|
|
194
186
|
initSheets();
|
|
195
187
|
initScenarioUi();
|
|
196
188
|
initReviewColumn(); // the review column: the changes, and the personas with their scenarios (review-column.js)
|
|
197
|
-
map.watchSize(); // the map follows its box (a ResizeObserver on the stage), in place of the window's resize event
|
|
198
189
|
|
|
199
190
|
map.resize();
|
|
200
191
|
syncViewUi();
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module explorer/pieces/adopt-tokens
|
|
3
|
+
*
|
|
4
|
+
* The default `--corn-*` tokens for a page that has no cornfield stylesheet of its own (spec 0011 D7). The package generates the sheet from
|
|
5
|
+
* explorer/tokens.css at build time, with every selector at zero specificity (`:where(...)`), so any ordinary rule of the host's overrides it; the
|
|
6
|
+
* first `corn-*` element to connect hands its text to this, and it is added once to the document (and once to a shadow root the element sits in).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** Marks the sheet this module made, so a second copy of the package on the same page finds it and adds nothing */
|
|
10
|
+
const MARK = Symbol.for('cornfield.default-tokens');
|
|
11
|
+
const ATTR = 'data-corn-default-tokens';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Adds `css` to the root node the element is in (a document or a shadow root), once. Returns true when it added it.
|
|
15
|
+
* Uses `adoptedStyleSheets`; a browser without it gets a `<style>` placed first in the head (or the shadow root), which loses to every later rule the same way.
|
|
16
|
+
* @param {Document|ShadowRoot} rootNode `element.getRootNode()`
|
|
17
|
+
* @param {string} css the generated default tokens
|
|
18
|
+
*/
|
|
19
|
+
export function adoptDefaultTokens(rootNode, css) {
|
|
20
|
+
if (!rootNode || !css) return false;
|
|
21
|
+
const doc = rootNode.nodeType === 9 ? rootNode : rootNode.ownerDocument;
|
|
22
|
+
const sheets = rootNode.adoptedStyleSheets;
|
|
23
|
+
if (Array.isArray(sheets) && doc && doc.defaultView && typeof doc.defaultView.CSSStyleSheet === 'function' && 'replaceSync' in doc.defaultView.CSSStyleSheet.prototype) {
|
|
24
|
+
if (sheets.some((s) => s && s[MARK])) return false;
|
|
25
|
+
const sheet = new doc.defaultView.CSSStyleSheet();
|
|
26
|
+
sheet.replaceSync(css);
|
|
27
|
+
sheet[MARK] = true;
|
|
28
|
+
rootNode.adoptedStyleSheets = [...sheets, sheet];
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
const holder = rootNode.nodeType === 9 ? rootNode.head : rootNode;
|
|
32
|
+
if (!holder || holder.querySelector(`style[${ATTR}]`)) return false;
|
|
33
|
+
const style = doc.createElement('style');
|
|
34
|
+
style.setAttribute(ATTR, '');
|
|
35
|
+
style.textContent = css;
|
|
36
|
+
holder.prepend(style);
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/* The map piece's own box, for a shadow root (explorer/pieces/map.js; components/src/corn-map). cornfield's page styles the same
|
|
2
|
+
elements from explorer.css (#graph-canvas, .hx-stage), so nothing here loads there. Every colour is a --corn-* token; a host
|
|
3
|
+
restyles an element with ::part(map-stage), ::part(map-canvas), ::part(label-canvas). */
|
|
4
|
+
/* As tall as the host makes it, and never under 16rem: a map dropped in with no height is visible. A host that wants it lower says so on the
|
|
5
|
+
part: corn-map::part(map-stage) { min-height: 0 } */
|
|
6
|
+
[data-corn="map-stage"] { display: block; position: relative; width: 100%; height: 100%; min-height: 16rem; overflow: hidden; background: var(--corn-ground-image), var(--corn-ground); }
|
|
7
|
+
/* The canvas is out of flow: its own pixel size (set by the renderer) must not become the element's minimum width in a grid or a flex row */
|
|
8
|
+
[data-corn="map-canvas"] { position: absolute; inset: 0; display: block; width: 100%; height: 100%; cursor: grab; touch-action: none; }
|
|
9
|
+
[data-corn="map-canvas"]:active { cursor: grabbing; }
|
|
10
|
+
[data-corn="map-canvas"]:focus-visible { outline: var(--corn-focus-width) solid var(--corn-focus); outline-offset: calc(var(--corn-focus-width) * -1); }
|
|
11
|
+
[data-corn="label-canvas"] { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
|
|
12
|
+
/* The Canvas 2D that stands in for the map canvas when its WebGL2 shaders fail (renderer.js): same place, pointer goes through */
|
|
13
|
+
[data-corn="map-overlay"] { position: absolute; left: 0; top: 0; width: 100%; height: 100%; pointer-events: none; }
|
|
14
|
+
.hx-sr-only { position: absolute !important; width: 1px; height: 1px; margin: 0; padding: 0; overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; border: 0; }
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module explorer/pieces/map
|
|
3
|
+
*
|
|
4
|
+
* The map as a piece (spec 0011 D3): its two canvases, the help for its keys and its announcer, made, wired and ended in one place, for cornfield's
|
|
5
|
+
* own page (explorer/page.js mounts it where index.html keeps `<!--corn-piece:map/stage-->`) and for the `<corn-map>` component (components/src,
|
|
6
|
+
* which mounts it in its shadow root). There is one code path: the keys are engine/keyboard.js's, a tap selects through engine/selection.js, and
|
|
7
|
+
* the page and a component differ only in who listens to the instance's events (the page's node panel follows `node:show`; a component turns
|
|
8
|
+
* `selection` into `cornSelect`).
|
|
9
|
+
*
|
|
10
|
+
* const piece = mount(root, inst, options) -> { update, destroy, ... }
|
|
11
|
+
*
|
|
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
|
|
14
|
+
* options see below; `update(next)` takes the same keys and applies what changed
|
|
15
|
+
*
|
|
16
|
+
* Options, all optional:
|
|
17
|
+
* topology {nodes, edges}: the map to draw (the object topology.json holds). The page does not pass it: it fetches and fills the state itself (view.js)
|
|
18
|
+
* view 'map' | 'dataflow'
|
|
19
|
+
* node the id of the selected node, or null/'' for none. A node the map has not got yet is selected when a topology brings it
|
|
20
|
+
* 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
|
+
* renderer 'auto' | 'canvas2d'
|
|
22
|
+
* base, fetch, strings for a piece that makes its own instance
|
|
23
|
+
* 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
|
+
* 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
|
+
* 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)
|
|
26
|
+
* measureCovered () => px of the bottom of the map something covers (the page's phone sheets); default none
|
|
27
|
+
* onSelect ({ id, kind, source, from?, to? }) once per change of the selection, from the instance's `selection` event, whoever made it:
|
|
28
|
+
* kind 'node' (id: the node's), 'edge' (id: 'from->to', and from, to), or null with id null (cleared); source as the event says
|
|
29
|
+
* onReady () once, after the first frame with a map in it is drawn
|
|
30
|
+
*
|
|
31
|
+
* What it does for a root that is not the document (the traps in test/explorer/piece-map.test.js):
|
|
32
|
+
* a drag released outside the root ends (pointer capture on the canvas), theme icons are fetched with CORS when the instance has a base,
|
|
33
|
+
* a change of theme reads the tokens again (`refresh()`), freeform positions are never stored, and `destroy()` always ends the instance's map.
|
|
34
|
+
*
|
|
35
|
+
* It imports the instance and the engine, and none of the bridges (state.js, render.js, map-style.js, layout.js): see instance-tripwires.test.js.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
import { createInstance } from '../instance.js';
|
|
39
|
+
import { createMap } from '../engine/render.js';
|
|
40
|
+
import { mapStyleOf } from '../engine/map-style.js';
|
|
41
|
+
import { computeDegreeCentrality, computeLaneLayout, layoutOrbit } from '../engine/layout.js';
|
|
42
|
+
import { initKeyboard, resetKeyboard } from '../engine/keyboard.js';
|
|
43
|
+
import { selectNode, clearSelection } from '../engine/selection.js';
|
|
44
|
+
import { stopFlight } from '../engine/fly.js';
|
|
45
|
+
import { scopeOf, place } from '../engine/piece.js';
|
|
46
|
+
import { createRenderer } from '../renderer.js';
|
|
47
|
+
import { bindCanvasInput } from '../input.js';
|
|
48
|
+
import { buildDataFlow } from '../flow-build.js';
|
|
49
|
+
import { resolveTheme } from '../theme-model.js';
|
|
50
|
+
import { escapeHtml } from '../../core/text.js';
|
|
51
|
+
import { adoptDefaultTokens } from './adopt-tokens.js';
|
|
52
|
+
|
|
53
|
+
/** The keys of a map on its own (engine/keyboard.js): what the canvas is described by */
|
|
54
|
+
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.';
|
|
55
|
+
/** The same keys where the map has the node panel and Freeform beside it (cornfield's page) */
|
|
56
|
+
export const KEYS_HELP_WITH_PANEL = "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. In Freeform, Alt and an arrow moves the selected node.";
|
|
57
|
+
|
|
58
|
+
/** The map canvas: one per mount, and one per change of renderer (a canvas that gave a WebGL context can never give a 2d one) */
|
|
59
|
+
const CANVAS = '<canvas id="graph-canvas" data-corn="map-canvas" tabindex="0" role="application" aria-roledescription="map" aria-label="Topology map" aria-describedby="map-keys-help"></canvas>';
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The markup of the piece (`stage`): the map canvas, the help for its keys, the announcer the keyboard speaks in (engine/keyboard.js), and the
|
|
63
|
+
* canvas the labels are drawn on (labels.js) over the map; the map canvas still gets the pointer. Hooks get their parts when placed (engine/piece.js).
|
|
64
|
+
* @param {{ keysHelp?: string }} [opts]
|
|
65
|
+
*/
|
|
66
|
+
export const markup = ({ keysHelp = KEYS_HELP } = {}) => ({
|
|
67
|
+
stage: `${CANVAS}
|
|
68
|
+
<p id="map-keys-help" class="hx-sr-only">${escapeHtml(keysHelp)}</p>
|
|
69
|
+
<p id="map-announcer" class="hx-sr-only" role="status" aria-live="polite"></p>
|
|
70
|
+
<canvas id="label-canvas" data-corn="label-canvas" aria-hidden="true"></canvas>`,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
/** The document or shadow root the map's host sits in: where the default tokens go */
|
|
74
|
+
function hostRootOf(root) {
|
|
75
|
+
const host = root.nodeType === 11 ? /** @type {ShadowRoot} */ (root).host : root;
|
|
76
|
+
return host && typeof host.getRootNode === 'function' ? host.getRootNode() : null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @param {Element|ShadowRoot} root
|
|
81
|
+
* @param {ReturnType<typeof createInstance>|null} inst
|
|
82
|
+
* @param {object} [options]
|
|
83
|
+
*/
|
|
84
|
+
export function mount(root, inst, options = {}) {
|
|
85
|
+
const ownsInstance = !inst;
|
|
86
|
+
if (ownsInstance) inst = createInstance({ root, base: options.base || '', fetch: options.fetch, strings: options.strings });
|
|
87
|
+
const { state } = inst;
|
|
88
|
+
if (options.tokens) {
|
|
89
|
+
// 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
|
+
// 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
|
|
91
|
+
const where = hostRootOf(root);
|
|
92
|
+
if (where) adoptDefaultTokens(where.nodeType === 9 ? where : where.ownerDocument, options.tokens);
|
|
93
|
+
if (where && where.nodeType === 11) adoptDefaultTokens(where, options.tokens);
|
|
94
|
+
}
|
|
95
|
+
/** What the piece set up, undone by destroy() (the instance's own end runs it too, if the piece is still there) */
|
|
96
|
+
const scope = scopeOf(inst);
|
|
97
|
+
const rendererOf = (v) => (v === 'canvas2d' ? 'canvas2d' : 'auto');
|
|
98
|
+
const themeOf = (v) => (v === 'light' || v === 'dark' ? v : 'auto');
|
|
99
|
+
const current = { renderer: rendererOf(options.renderer), theme: themeOf(options.theme), topology: undefined };
|
|
100
|
+
let ended = false;
|
|
101
|
+
let ready = false;
|
|
102
|
+
|
|
103
|
+
// -- elements ---------------------------------------------------------------------------------------------------------------------
|
|
104
|
+
const html = markup({ keysHelp: options.keysHelp }).stage;
|
|
105
|
+
const slots = options.slots && options.slots.stage ? options.slots : null;
|
|
106
|
+
const made = place(inst, root, slots ? html : `<div data-corn="map-stage">${html}</div>`, slots, 'stage');
|
|
107
|
+
const find = (sel) => made.map((el) => (el.matches(sel) ? el : el.querySelector(sel))).find(Boolean);
|
|
108
|
+
scope.own(() => { for (const el of [...made, state.canvas]) if (el) el.remove(); });
|
|
109
|
+
|
|
110
|
+
// -- what the engine needs on the state, in this order ------------------------------------------------------------------------------
|
|
111
|
+
state.canvas = find('[data-corn="map-canvas"]');
|
|
112
|
+
state.labelCanvas = find('[data-corn="label-canvas"]');
|
|
113
|
+
// A 2d context that only measures text (layout.js labelWidth): the map canvas may be WebGL, and a canvas that gave WebGL can never give 2d
|
|
114
|
+
state.ctx = inst.doc.createElement('canvas').getContext('2d');
|
|
115
|
+
const map = createMap(inst, options.measureCovered ? { measureCovered: options.measureCovered } : {});
|
|
116
|
+
const style = mapStyleOf(inst);
|
|
117
|
+
scope.own(style.watch());
|
|
118
|
+
scope.own(style.onChange(() => map.render()));
|
|
119
|
+
const prefer = () => (current.renderer === 'canvas2d' ? 'canvas2d' : null);
|
|
120
|
+
const installRenderer = () => map.setRenderer(createRenderer(state.canvas, { prefer: prefer(), mapStyle: style.get }));
|
|
121
|
+
|
|
122
|
+
// -- the ground: light or dark ------------------------------------------------------------------------------------------------------
|
|
123
|
+
const lightQuery = inst.win && inst.win.matchMedia ? inst.win.matchMedia('(prefers-color-scheme: light)') : null;
|
|
124
|
+
/** The mode the map is drawn for: the element's own `theme`, else the document's `data-theme`, else the system's */
|
|
125
|
+
function mode() {
|
|
126
|
+
if (current.theme === 'light' || current.theme === 'dark') return current.theme;
|
|
127
|
+
const root$ = inst.doc && inst.doc.documentElement;
|
|
128
|
+
const own = root$ ? root$.getAttribute('data-theme') : null;
|
|
129
|
+
if (own === 'light' || own === 'dark') return own;
|
|
130
|
+
return resolveTheme('system', !!(lightQuery && lightQuery.matches));
|
|
131
|
+
}
|
|
132
|
+
/** Draws for the mode now in force and reads the tokens again (they differ by mode, and the host's `<html>` and head are not watched by the style) */
|
|
133
|
+
function refresh() {
|
|
134
|
+
if (ended) return;
|
|
135
|
+
const next = mode();
|
|
136
|
+
style.refresh();
|
|
137
|
+
if (state.theme !== next) { state.theme = next; inst.touchScene(); }
|
|
138
|
+
map.render();
|
|
139
|
+
}
|
|
140
|
+
state.theme = mode();
|
|
141
|
+
if (lightQuery && lightQuery.addEventListener) { lightQuery.addEventListener('change', refresh); scope.own(() => lightQuery.removeEventListener('change', refresh)); }
|
|
142
|
+
const Mutation = inst.win && inst.win.MutationObserver;
|
|
143
|
+
if (Mutation && inst.doc && inst.doc.documentElement) { // the host page switching its own light and dark
|
|
144
|
+
const watch = new Mutation(refresh);
|
|
145
|
+
watch.observe(inst.doc.documentElement, { attributes: true, attributeFilter: ['data-theme', 'class'] });
|
|
146
|
+
scope.own(() => watch.disconnect());
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// -- the renderer, the layout --------------------------------------------------------------------------------------------------------
|
|
150
|
+
installRenderer();
|
|
151
|
+
|
|
152
|
+
/** Where nodes sit for the current view */
|
|
153
|
+
function applyLayout() {
|
|
154
|
+
computeDegreeCentrality(inst);
|
|
155
|
+
if (state.view === 'dataflow' && state.flow) {
|
|
156
|
+
state.flow.tiers.forEach((tier) => tier.nodes.forEach((n) => { n.r = Math.min(n.r, 13); })); // all small in the lanes
|
|
157
|
+
computeLaneLayout(inst, state.flow);
|
|
158
|
+
} else layoutOrbit(inst);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** A fit asked for while the map has no box (a parent `display: none`, a box of no size) waits for one: fitted to nothing, the camera would be lost */
|
|
162
|
+
let fitWhenSized = false;
|
|
163
|
+
const fit = () => {
|
|
164
|
+
if (!state.nodes.length) return;
|
|
165
|
+
fitWhenSized = !state.width || !state.height;
|
|
166
|
+
if (!fitWhenSized) map.fitGraphToViewport(map.fitPadding());
|
|
167
|
+
};
|
|
168
|
+
const nodeById = (id) => state.nodes.find((n) => n.id === id) || null;
|
|
169
|
+
|
|
170
|
+
// -- selection: one path for the page and a component ---------------------------------------------------------------------------------
|
|
171
|
+
/** A node the host asked for that the map has not got (yet): selected when a topology brings it */
|
|
172
|
+
let pendingNode = null;
|
|
173
|
+
// A tap on a node selects it, a tap on the bare map clears it, a hand on the camera ends a flight (input.js asks; engine/selection.js does)
|
|
174
|
+
scope.on('select', (node) => selectNode(inst, node, { source: 'tap' }));
|
|
175
|
+
scope.on('background', () => clearSelection(inst, { source: 'tap' }));
|
|
176
|
+
scope.on('grab', () => stopFlight(inst));
|
|
177
|
+
// The instance says each change once, whoever made it (a tap, a key, a search, `node`, an assignment to the state: an edge picked on a proposal)
|
|
178
|
+
scope.on('selection', ({ node, edge, source }) => {
|
|
179
|
+
if (ended) return;
|
|
180
|
+
if (node || edge) pendingNode = null;
|
|
181
|
+
if (!options.onSelect) return;
|
|
182
|
+
if (node) options.onSelect({ id: node.id, kind: 'node', source });
|
|
183
|
+
else if (edge) options.onSelect({ id: `${edge.from}->${edge.to}`, kind: 'edge', from: edge.from, to: edge.to, source });
|
|
184
|
+
else options.onSelect({ id: null, kind: null, source });
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
/** Selects the node with this id and brings it into view; false when the map has no such node */
|
|
188
|
+
function focusNode(id) {
|
|
189
|
+
const node = nodeById(String(id));
|
|
190
|
+
if (!node || ended) return false;
|
|
191
|
+
selectNode(inst, node, { frame: true, source: 'api' });
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// -- pointer and keys on the canvas ---------------------------------------------------------------------------------------------------
|
|
196
|
+
let stopInput = () => {};
|
|
197
|
+
const inDocument = !!inst.root && inst.root.nodeType === 9;
|
|
198
|
+
/** Binds the canvas now in `state.canvas`: map input, a mouse drag that follows the pointer out of the root, the keys. Once more when the canvas is replaced. */
|
|
199
|
+
function bind() {
|
|
200
|
+
const canvas = state.canvas;
|
|
201
|
+
const offs = [bindCanvasInput(inst)];
|
|
202
|
+
if (!inDocument) {
|
|
203
|
+
// input.js hears the release on the root, so a drag let go outside an element root would stick: the canvas holds the pointer until it is let go
|
|
204
|
+
offs.push(inst.listen(canvas, 'pointerdown', (e) => { if (e.pointerType === 'mouse' && canvas.setPointerCapture) { try { canvas.setPointerCapture(e.pointerId); } catch { /* the pointer is gone */ } } }));
|
|
205
|
+
}
|
|
206
|
+
resetKeyboard(inst); // the keys are the canvas's (engine/keyboard.js): a new canvas takes them over
|
|
207
|
+
initKeyboard(inst);
|
|
208
|
+
offs.push(() => resetKeyboard(inst));
|
|
209
|
+
stopInput = () => offs.forEach((off) => off());
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// -- the first frame ---------------------------------------------------------------------------------------------------------------------
|
|
213
|
+
scope.own(map.onPaint(() => {
|
|
214
|
+
if (fitWhenSized && state.width && state.height) fit();
|
|
215
|
+
if (ready || !state.nodes.length) return;
|
|
216
|
+
ready = true;
|
|
217
|
+
if (options.onReady) options.onReady();
|
|
218
|
+
}));
|
|
219
|
+
|
|
220
|
+
bind();
|
|
221
|
+
scope.own(() => stopInput());
|
|
222
|
+
scope.own(map.watchSize());
|
|
223
|
+
map.resize();
|
|
224
|
+
|
|
225
|
+
// -- changes ---------------------------------------------------------------------------------------------------------------------------------
|
|
226
|
+
function setTopology(topology) {
|
|
227
|
+
const nodes = topology && Array.isArray(topology.nodes) ? topology.nodes.filter((n) => n && typeof n.id === 'string') : [];
|
|
228
|
+
const edges = topology && Array.isArray(topology.edges) ? topology.edges.filter((e) => e && typeof e.from === 'string' && typeof e.to === 'string') : [];
|
|
229
|
+
const keep = state.selectedNode ? state.selectedNode.id : null; // the selection outlives a new topology that has its node
|
|
230
|
+
// The host's object is never changed: the map works on copies (the layout writes positions on them)
|
|
231
|
+
state.baseNodes = nodes;
|
|
232
|
+
state.baseEdges = edges;
|
|
233
|
+
state.nodes = nodes.map((n) => ({ ...n }));
|
|
234
|
+
state.edges = edges.map((e) => ({ ...e }));
|
|
235
|
+
state.mapNodes = state.nodes;
|
|
236
|
+
state.mapEdges = state.edges;
|
|
237
|
+
state.flow = buildDataFlow(state.nodes, state.edges);
|
|
238
|
+
const again = keep ? nodeById(keep) : null;
|
|
239
|
+
// The same id is the same selection (the instance says nothing); none is a change it says once
|
|
240
|
+
if (again) { state.selectedEdge = null; state.selectedNode = again; } else if (state.selectedNode || state.selectedEdge) { state.selectedNode = null; state.selectedEdge = null; }
|
|
241
|
+
inst.touchScene();
|
|
242
|
+
if (state.nodes.length) { applyLayout(); fit(); }
|
|
243
|
+
const asked = !again && pendingNode ? nodeById(pendingNode) : null; // the node the host asked for before the map had it
|
|
244
|
+
if (asked) selectNode(inst, asked, { source: 'api' });
|
|
245
|
+
map.render();
|
|
246
|
+
map.ensureFlowLoop();
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function setView(view) {
|
|
250
|
+
const next = view === 'dataflow' ? 'dataflow' : 'orbit';
|
|
251
|
+
if (state.view === next) return;
|
|
252
|
+
state.view = next;
|
|
253
|
+
inst.touchScene();
|
|
254
|
+
if (state.nodes.length) { applyLayout(); fit(); map.ensureFlowLoop(); }
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** A canvas that has given a context cannot give the other kind: the map gets a new one, the old one gives its WebGL context back */
|
|
258
|
+
function replaceRenderer() {
|
|
259
|
+
const previous = map.getRenderer();
|
|
260
|
+
if (previous.release) previous.release(); else previous.destroy(); // before the swap: a renderer already destroyed does not give its context back
|
|
261
|
+
stopInput();
|
|
262
|
+
const holder = inst.doc.createElement('template');
|
|
263
|
+
holder.innerHTML = CANVAS;
|
|
264
|
+
const next = /** @type {HTMLCanvasElement} */ (holder.content.firstElementChild);
|
|
265
|
+
next.setAttribute('part', 'map-canvas');
|
|
266
|
+
const hadFocus = inst.active() === state.canvas;
|
|
267
|
+
state.canvas.replaceWith(next);
|
|
268
|
+
state.canvas = next;
|
|
269
|
+
bind();
|
|
270
|
+
installRenderer();
|
|
271
|
+
map.resize();
|
|
272
|
+
if (hadFocus) next.focus();
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Applies what changed in `next` (the options above). Only the keys present are looked at.
|
|
277
|
+
* @param {object} [next]
|
|
278
|
+
*/
|
|
279
|
+
function update(next = {}) {
|
|
280
|
+
if (ended) return;
|
|
281
|
+
if ('renderer' in next) {
|
|
282
|
+
const value = rendererOf(next.renderer);
|
|
283
|
+
if (value !== current.renderer) { current.renderer = value; replaceRenderer(); }
|
|
284
|
+
}
|
|
285
|
+
if ('theme' in next) {
|
|
286
|
+
const value = themeOf(next.theme);
|
|
287
|
+
if (value !== current.theme) { current.theme = value; refresh(); }
|
|
288
|
+
}
|
|
289
|
+
if ('topology' in next && next.topology !== current.topology) { current.topology = next.topology; setTopology(next.topology); }
|
|
290
|
+
if ('view' in next) setView(next.view === 'dataflow' ? 'dataflow' : 'orbit');
|
|
291
|
+
if ('node' in next) {
|
|
292
|
+
const id = next.node == null || next.node === '' ? null : String(next.node);
|
|
293
|
+
if (id === null) {
|
|
294
|
+
pendingNode = null;
|
|
295
|
+
if (state.selectedNode || state.selectedEdge) clearSelection(inst, { source: 'api' });
|
|
296
|
+
} else {
|
|
297
|
+
const node = nodeById(id);
|
|
298
|
+
if (!node) pendingNode = id;
|
|
299
|
+
else if (!state.selectedNode || state.selectedNode.id !== id) selectNode(inst, node, { source: 'api' }); // the same node again changes nothing and says nothing
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** Ends the piece: its listeners, observers and timers, its elements, and the map (the renderer gives its WebGL context back) */
|
|
305
|
+
function destroy() {
|
|
306
|
+
if (ended) return;
|
|
307
|
+
ended = true;
|
|
308
|
+
scope.destroy();
|
|
309
|
+
map.destroy();
|
|
310
|
+
if (ownsInstance) inst.destroy();
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const first = {};
|
|
314
|
+
for (const key of ['topology', 'view', 'node']) if (options[key] !== undefined) first[key] = options[key];
|
|
315
|
+
update(first);
|
|
316
|
+
refresh();
|
|
317
|
+
|
|
318
|
+
return {
|
|
319
|
+
inst,
|
|
320
|
+
map,
|
|
321
|
+
update,
|
|
322
|
+
destroy,
|
|
323
|
+
/** The whole map in view */
|
|
324
|
+
fit,
|
|
325
|
+
/** Selects a node by id and brings it into view; false when there is none */
|
|
326
|
+
focusNode,
|
|
327
|
+
/** Reads the tokens again and draws: for a change the style cannot see (the host's `<html>`, a stylesheet in its head) */
|
|
328
|
+
refresh,
|
|
329
|
+
/** Starts the theme's icons (`config.theme` of /cornfield/config.json), fetched with CORS when the instance has a base */
|
|
330
|
+
applyTheme: (theme) => style.applyTheme(theme),
|
|
331
|
+
};
|
|
332
|
+
}
|
package/explorer/renderer.js
CHANGED
|
@@ -42,6 +42,7 @@ function addStandIn(canvas) {
|
|
|
42
42
|
const standIn = canvas.ownerDocument.createElement('canvas');
|
|
43
43
|
standIn.className = 'hx-overlay';
|
|
44
44
|
standIn.setAttribute('data-corn', 'map-overlay');
|
|
45
|
+
standIn.setAttribute('part', 'map-overlay'); // a host styles it from outside a shadow root: every hook is a part too
|
|
45
46
|
standIn.setAttribute('aria-hidden', 'true');
|
|
46
47
|
canvas.after(standIn);
|
|
47
48
|
return standIn;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@popcorn245/cornfield",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.47.0",
|
|
4
4
|
"description": "A self-hostable dev dashboard with the topology map at its centre: proposals on the map, scenarios, outages and what-if edits, for any repo.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "popcorn245 (https://github.com/popcorn245)",
|