cornfield 0.42.0 → 0.43.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/scene.js CHANGED
@@ -48,7 +48,11 @@
48
48
  * @property {string} group the node's layer in the Layers menu (`app`, `cloud`, `web`, `house`, ...): the node's group for the map tokens
49
49
  * @property {number} x world centre
50
50
  * @property {number} y
51
- * @property {number} r world radius of the filled circle
51
+ * @property {number} r world radius of the node: the circle's, or the silhouette's circumscribing one
52
+ * @property {'circle'|'rounded-square'|'hexagon'|'square'|'diamond'} shape the silhouette (core/schema.js `NODE_SHAPES`)
53
+ * @property {number} [hitR] world radius of the disc a click or tap hits: `r` times how far the silhouette reaches (SHAPE_REACH)
54
+ * @property {string} [glyph] the type whose built-in glyph the node shows (glyphs.js), when the type has one
55
+ * @property {string} [letters] a finer kind's letters (kind-letters.js)
52
56
  * @property {string} color fill, and the glow colour
53
57
  * @property {number} alpha 0..1 for everything of this node (halo, circle, core, label): 1, or 0.07 when dimmed by the focus
54
58
  * @property {number} glow blur radius in px in `color`; 0 = none (the dimmed ones, with no glow)
@@ -91,12 +95,14 @@
91
95
  * @property {string} color stroke
92
96
  * @property {number} width px in world units
93
97
  * @property {number} alpha 0..1 for the whole edge (line, arrowhead, packet)
94
- * @property {number[]|null} dash dash pattern, or null for solid
98
+ * @property {number[]|null} dash dash pattern, or null for solid: PROPOSED_DASH for a proposed edge, USAGE_DASH (dots) for a usage link, nothing else
99
+ * @property {0|1|2} ticks tick marks across the middle: a call that breaks at the next deploy (1) or renewal (2); 0 for every other edge
100
+ * @property {'triangle'|'chevron'|'bar'|'diamond'|null} end the mark at the target (END_MARKS); null for an ownership or usage link
95
101
  * @property {{cx:number, cy:number}|null} curve control point of the quadratic from (x1,y1) to (x2,y2), or null for a straight line
96
- * @property {boolean} arrow an arrowhead at (x2,y2), pointing along (x2-cx, y2-cy); `ARROW` has its size
102
+ * @property {boolean} arrow the edge has an end mark at (x2,y2), pointing along (x2-cx, y2-cy); `end` says which and `ARROW` has its size
97
103
  * @property {number} glow blur px; 0 = none
98
104
  * @property {string|null} glowColor
99
- * @property {{text:string, x:number, y:number, color:string, italic:boolean}|null} label drawn centred, for the selection's edges only
105
+ * @property {{text:string, x:number, y:number, color:string, italic:boolean}|null} label the kind's word, centred at the edge's middle, for the lit edges only (labels.js places it below the node labels)
100
106
  * @property {ScenePacket|null} packet
101
107
  * @property {Mark} mark
102
108
  * @property {boolean} live the edge came from the live layer (an ownership in the house, a phone under its persona, a dashed placement)
@@ -164,12 +170,15 @@ import { flowStyleOf } from './flows.js';
164
170
  import { beforeOf, markIn, showsEdge, showsNode, touchedIds } from './proposal-model.js';
165
171
  import { decorateHouseScene, houseEdgeStyle, houseShowsNode, houseViewEdges } from './house-model.js';
166
172
  import { paintVerdict } from './check-model.js';
167
- import { externalLayer, whenDash } from './cloud-model.js';
173
+ import { externalLayer, whenTicks } from './cloud-model.js';
168
174
  import { lettersOf, projectLayerKey, shownByProposal } from './layer-model.js';
175
+ import { shapeOf } from '../core/schema.js';
176
+ import { hasGlyph } from './glyphs.js';
169
177
  import { outageFor, paintOutage } from './outage-model.js';
170
178
  import { paintForge } from './forge-panel-model.js';
171
179
  import { lightColor, lightenScene, parseColor } from './theme-model.js';
172
180
  import { matchIds } from './search-model.js';
181
+ import { activeSubgraph, kindEdgeWord } from './selection-model.js';
173
182
  import { DIFF_MARK_COLORS, DIFF_LABEL_ALPHA, EDGE_MIN_ALPHA, MARK_SHAPES, REMOVED_ALPHA } from './diff-marks.js';
174
183
 
175
184
  // ---------------------------------------------------------------------------
@@ -191,9 +200,32 @@ export const CORE_COLOR = '#ffffff';
191
200
  export const DIM_ALPHA = 0.07;
192
201
  /** The outline drawn under a label so it reads over the lines */
193
202
  export const LABEL_OUTLINE = { node: { color: 'rgba(9, 13, 22, 0.85)', width: 3.5 }, edge: { color: 'rgba(9, 13, 22, 0.9)', width: 3 } };
203
+ /** Kept for themes and tests that name it; the words on lit edges are drawn at the label minimum (labels.js) */
194
204
  export const EDGE_LABEL_SIZE = 10;
195
- /** An arrowhead: two barbs `length` back from the tip, `spread` radians either side of the line */
205
+ /** An arrowhead: two barbs `length` back from the tip, `spread` radians either side of the line (a chevron has the same barbs, open) */
196
206
  export const ARROW = { length: 9, spread: 0.45 };
207
+ /**
208
+ * What each kind of edge ends in at its target (spec 0008 D7), one mark per family: an open chevron for data, a filled triangle for a
209
+ * service, a bar across the line for a place, an open diamond for a token. Ownership and usage links have none. A dashed edge (proposed)
210
+ * keeps its kind's own mark. `triangle` is the arrowhead renderers have always drawn.
211
+ */
212
+ export const END_MARKS = Object.freeze({ flow: 'chevron', reads: 'chevron', writes: 'chevron', calls: 'triangle', triggers: 'triangle', delivers: 'triangle', 'runs-on': 'bar', uses: 'diamond', extends: 'diamond' });
213
+ /** The shapes of the end marks and the ticks, in world units (the renderers draw them, the legend mirrors them): stroke width, a bar's and a tick's half length, a diamond's half length and half width */
214
+ export const END_SHAPES = Object.freeze({ strokeWidth: 1.8, barHalf: 5, diamondHalf: 6, diamondAcross: 4.5, tickHalf: 4, tickWidth: 1.6, tickGap: 6 });
215
+ /** The sizes of the silhouettes, as fractions of a node's radius: a rounded square's half side and corner, a hexagon's inradius, a square's half side and corner, a diamond's half diagonal (both renderers draw them) */
216
+ export const SHAPE_SIZES = Object.freeze({ roundedHalf: 0.9, roundedCorner: 0.3, hexInradius: 0.9, squareHalf: 0.86, squareCorner: 0.12, diamondHalf: 1.15 });
217
+ /** How far each silhouette reaches from its centre, as a fraction of the node's radius: a click or tap on a square's corner or a diamond's point is on the node (spatial.js hit-tests a disc this wide) */
218
+ export const SHAPE_REACH = Object.freeze({
219
+ circle: 1,
220
+ 'rounded-square': (SHAPE_SIZES.roundedHalf - SHAPE_SIZES.roundedCorner) * Math.SQRT2 + SHAPE_SIZES.roundedCorner,
221
+ hexagon: SHAPE_SIZES.hexInradius / Math.cos(Math.PI / 6),
222
+ square: (SHAPE_SIZES.squareHalf - SHAPE_SIZES.squareCorner) * Math.SQRT2 + SHAPE_SIZES.squareCorner,
223
+ diamond: SHAPE_SIZES.diamondHalf,
224
+ });
225
+ /** The dash a proposed edge is drawn with: the one dashed thing on the map (a usage link is dotted, a `when` is told by ticks) */
226
+ export const PROPOSED_DASH = Object.freeze([7, 5]);
227
+ /** The dash of a usage link: dots */
228
+ export const USAGE_DASH = Object.freeze([1.5, 4]);
197
229
  /** Look of each halo: a filled disc (`fillR` past the node radius) and a ring (`ringR` past it) */
198
230
  export const HALO = {
199
231
  selected: { fill: 'rgba(99, 102, 241, 0.55)', fillR: 8, ring: '#818cf8', ringR: 13, ringWidth: 1.5 },
@@ -271,40 +303,14 @@ export function layerShows(state, node) {
271
303
  const viewEdgesOf = (state) => (state.view === 'dataflow' && state.flow ? state.flow.edges : state.view === 'house' ? houseViewEdges(state) : state.edges);
272
304
 
273
305
  /**
274
- * The focus subgraph: the selected node, its children and every path through them
306
+ * The focus subgraph (spec 0008 D5): the selected node and every node one edge away, of any kind; `throughChildren` adds its
307
+ * children's neighbours, `kind` keeps one kind of line. selection-model.js has the rule.
275
308
  * @param {{id:string}|null} selected
276
309
  * @param {object[]} edges the edges to walk
310
+ * @param {{throughChildren?: boolean, kind?: string|null}} [opts]
277
311
  */
278
- export function getActiveSubgraph(selected, edges) {
279
- if (!selected) return null;
280
-
281
- const connectedNodeIds = new Set([selected.id]);
282
- const connectedEdgeKeys = new Set();
283
- const key = (e) => `${e.from}->${e.to}`;
284
- const owns = (e) => !e.dashed && !e.kind;
285
-
286
- // Direct children (ownership edges out) and the direct parent (ownership edge in)
287
- const childIds = new Set();
288
- edges.forEach((e) => {
289
- if (owns(e) && e.from === selected.id) { childIds.add(e.to); connectedNodeIds.add(e.to); connectedEdgeKeys.add(key(e)); }
290
- if (owns(e) && e.to === selected.id) { connectedNodeIds.add(e.from); connectedEdgeKeys.add(key(e)); }
291
- });
292
-
293
- // Everything connected to the selected node or to any of its children
294
- const focusCore = new Set([selected.id, ...childIds]);
295
- edges.forEach((e) => {
296
- if (focusCore.has(e.from)) { connectedNodeIds.add(e.to); connectedEdgeKeys.add(key(e)); }
297
- if (focusCore.has(e.to)) { connectedNodeIds.add(e.from); connectedEdgeKeys.add(key(e)); }
298
- });
299
- // And the edges between connected nodes that touch the focus
300
- edges.forEach((e) => {
301
- if (connectedNodeIds.has(e.from) && connectedNodeIds.has(e.to)
302
- && (focusCore.has(e.from) || focusCore.has(e.to) || e.from === selected.id || e.to === selected.id)) {
303
- connectedEdgeKeys.add(key(e));
304
- }
305
- });
306
-
307
- return { connectedNodeIds, connectedEdgeKeys, childIds, selectedId: selected.id };
312
+ export function getActiveSubgraph(selected, edges, opts) {
313
+ return activeSubgraph(selected, edges, opts);
308
314
  }
309
315
 
310
316
  /**
@@ -393,12 +399,12 @@ function flowEdge(edge, source, target, subgraph, preset, reduceMotion) {
393
399
  const { a, b, c } = flowGeometry(edge, source, target);
394
400
  const focused = !!subgraph && connected;
395
401
 
396
- // Labels only for the selection: a flow's own node, or for a table's edges (which converge) their page
397
- const pageEnd = edge.kind === 'reads' ? edge.to : edge.from;
402
+ // A lit edge says its kind at its middle ("reads", "calls": spec 0008 D7); the label layer places the words below the node labels (labels.js)
403
+ const kind = edgeKind(edge);
398
404
  let label = null;
399
- if (edge.label && subgraph && connected && (!isData || subgraph.selectedId === pageEnd)) {
405
+ if (subgraph && connected) {
400
406
  const m = quadPoint(a, c, b, 0.5);
401
- label = { text: edge.label, x: m.x, y: m.y - 6, color: style.color, italic: proposed };
407
+ label = { text: edge.count !== undefined && edge.label ? edge.label : kindEdgeWord(kind), x: m.x, y: m.y, color: style.color, italic: proposed }; // the House's rolled-up lines say how many calls they stand for
402
408
  }
403
409
 
404
410
  // A packet moving along the flow: reads travel back, writes travel out
@@ -414,9 +420,11 @@ function flowEdge(edge, source, target, subgraph, preset, reduceMotion) {
414
420
  color: style.color,
415
421
  width: focused ? 2.4 : preset && !isData ? 2 : 1.6,
416
422
  alpha: subgraph ? (connected ? 1 : 0.04) : preset ? (isData ? 0.2 : 0.65) : isData ? 0.12 : 0.45,
417
- dash: proposed ? [7, 5] : whenDash(edge), // a call into a cloud node: dashed when it bites at the next deploy, dotted at the next renewal (spec 0031)
423
+ dash: proposed ? PROPOSED_DASH : null, // a dash means "proposed" and nothing else (spec 0008 D7)
424
+ ticks: whenTicks(edge), // a call into a cloud node: one tick at the middle when it bites at the next deploy, two at the next renewal (spec 0031)
418
425
  curve: { cx: c.x, cy: c.y },
419
426
  arrow: true,
427
+ end: Object.hasOwn(END_MARKS, kind) ? END_MARKS[kind] : 'triangle',
420
428
  glow: focused ? 8 : 0,
421
429
  glowColor: focused ? style.color : null,
422
430
  label, packet, mark: 'none', live: !!edge.live,
@@ -429,20 +437,20 @@ function linkEdge(edge, source, target, subgraph) {
429
437
  let color, width, dash = null, glow = 0, glowColor = null;
430
438
  if (subgraph) {
431
439
  if (connected) {
432
- if (edge.dashed) { color = '#818cf8'; width = 2.0; glow = 8; glowColor = 'rgba(129, 140, 248, 0.7)'; dash = [5, 4]; }
440
+ if (edge.dashed) { color = '#818cf8'; width = 2.0; glow = 8; glowColor = 'rgba(129, 140, 248, 0.7)'; dash = USAGE_DASH; }
433
441
  else { color = '#ffffff'; width = 2.4; glow = 10; glowColor = 'rgba(255, 255, 255, 0.8)'; }
434
442
  } else {
435
- color = 'rgba(255, 255, 255, 0.02)'; width = 0.8; dash = edge.dashed ? [4, 4] : null;
443
+ color = 'rgba(255, 255, 255, 0.02)'; width = 0.8; dash = edge.dashed ? USAGE_DASH : null;
436
444
  }
437
445
  } else if (edge.dashed) {
438
- color = 'rgba(129, 140, 248, 0.35)'; width = 1.2; dash = [4, 4];
446
+ color = 'rgba(129, 140, 248, 0.35)'; width = 1.2; dash = USAGE_DASH;
439
447
  } else {
440
448
  color = 'rgba(255, 255, 255, 0.16)'; width = 1.4;
441
449
  }
442
450
  return {
443
451
  key: edgeKeyOf(edge), from: edge.from, to: edge.to, kind: edgeKind(edge),
444
452
  x1: source.x, y1: source.y, x2: target.x, y2: target.y, r1: source.r, r2: target.r,
445
- color, width, alpha: 1, dash, curve: null, arrow: false, glow, glowColor, label: null, packet: null, mark: 'none', live: !!edge.live,
453
+ color, width, alpha: 1, dash, ticks: 0, curve: null, arrow: false, end: null, glow, glowColor, label: null, packet: null, mark: 'none', live: !!edge.live,
446
454
  };
447
455
  }
448
456
 
@@ -574,6 +582,9 @@ function buildNodes(state, subgraph) {
574
582
  id: node.id, type: shown.type, group: groupOf(shown),
575
583
  ...(typeof shown.kind === 'string' && shown.kind ? { kind: shown.kind } : {}),
576
584
  ...(letters ? { letters } : {}),
585
+ shape: shapeOf(shown.type), // the silhouette of the node's family (spec 0008 D8): circle, rounded-square, hexagon, square, diamond
586
+ hitR: shown.r * SHAPE_REACH[shapeOf(shown.type)], // the disc a click or tap hits: as far as the silhouette reaches (spatial.js)
587
+ ...(hasGlyph(shown.type) ? { glyph: shown.type } : {}), // its built-in glyph, drawn where the core dot is once the node is wide enough (glyphs.js)
577
588
  ...(layer ? { layer } : {}),
578
589
  x: node.x, y: node.y, r: shown.r, color: shown.color,
579
590
  alpha,
@@ -698,7 +709,7 @@ export function emphasiseScenario(scene, scn, opts = {}) {
698
709
  */
699
710
  export function buildScene(state, opts = {}) {
700
711
  const preset = state.view === 'dataflow';
701
- const subgraph = state.scenario ? scenarioSubgraph(state) : state.selectedNode ? getActiveSubgraph(state.selectedNode, viewEdgesOf(state)) : null;
712
+ const subgraph = state.scenario ? scenarioSubgraph(state) : state.selectedNode ? getActiveSubgraph(state.selectedNode, viewEdgesOf(state), { throughChildren: !!state.throughChildren, kind: state.selectedKind || null }) : null;
702
713
 
703
714
  const lanes = preset && state.flow ? buildLanes(state.flow) : [];
704
715
  const rings = !(preset && state.flow) && !state.isFreeform ? buildRings(state) : [];
@@ -0,0 +1,150 @@
1
+ /**
2
+ * @module explorer/selection-model
3
+ *
4
+ * What a selection lights, and where the camera goes for it (spec 0008 D5 and D6). Pure: no DOM, no state.
5
+ *
6
+ * activeSubgraph(selected, edges, opts) the lit set: the node and everything ONE edge away, of any kind (its parent and children
7
+ * included); `throughChildren` adds the children's neighbours (the wider set every
8
+ * selection lit before 0.35); `kind` keeps only the lines of that kind
9
+ * linesByKind(subgraph) "Calls 12 · Reads 9 · Owns 4": the lit lines counted by kind, in a fixed order
10
+ * countOffScreen(ids, camera, rect) how many of the lit nodes are outside the part of the map nothing covers
11
+ * selectionCamera(node, lit, rect, opts) where the camera goes when a selection comes from search, a list, a link or the keyboard:
12
+ * the node and its lit set framed if that fits at zoom 0.5 or closer, else the node
13
+ * centred at zoom 1. A click on the map never asks for it (panels.js)
14
+ *
15
+ * An edge's `kind` is project data: it only ever selects a word from `KIND_WORDS` or is shown as escaped text; it never builds a
16
+ * selector or a token name.
17
+ */
18
+
19
+ import { frameNodes } from './camera.js';
20
+
21
+ /** The kinds a line is drawn as, in the order the panel lists them */
22
+ export const LINE_KINDS = ['ownership', 'usage', 'flow', 'reads', 'writes', 'calls', 'triggers', 'delivers', 'runs-on', 'uses', 'extends'];
23
+
24
+ /** The words for a kind: the panel's plural ("Calls 12"), and the word on a lit edge ("calls") */
25
+ export const KIND_WORDS = Object.freeze({
26
+ ownership: { panel: 'Owns', edge: 'owns' },
27
+ usage: { panel: 'Links', edge: 'links' },
28
+ flow: { panel: 'Flows', edge: 'flow' },
29
+ reads: { panel: 'Reads', edge: 'reads' },
30
+ writes: { panel: 'Writes', edge: 'writes' },
31
+ calls: { panel: 'Calls', edge: 'calls' },
32
+ triggers: { panel: 'Triggers', edge: 'triggers' },
33
+ delivers: { panel: 'Delivers', edge: 'delivers' },
34
+ 'runs-on': { panel: 'Runs on', edge: 'runs on' },
35
+ uses: { panel: 'Uses', edge: 'uses' },
36
+ extends: { panel: 'Extends', edge: 'extends' },
37
+ });
38
+
39
+ /** The kind of a topology edge: an ownership has none, a usage link is `dashed` */
40
+ export const edgeKindOf = (e) => e.kind || (e.dashed ? 'usage' : 'ownership');
41
+ export const edgeKeyOf = (e) => `${e.from}->${e.to}`;
42
+
43
+ /** A kind's panel word; a kind this table does not know (a project's own) is shown as the text it is */
44
+ export const kindPanelWord = (kind) => (Object.hasOwn(KIND_WORDS, kind) ? KIND_WORDS[kind].panel : String(kind));
45
+ export const kindEdgeWord = (kind) => (Object.hasOwn(KIND_WORDS, kind) ? KIND_WORDS[kind].edge : String(kind));
46
+
47
+ /**
48
+ * The focus subgraph (spec 0008 D5).
49
+ * @param {{id:string}|null} selected
50
+ * @param {object[]} edges the edges to walk
51
+ * @param {{throughChildren?: boolean, kind?: string|null}} [opts]
52
+ * @returns {null | {
53
+ * connectedNodeIds: Set<string>, connectedEdgeKeys: Set<string>, childIds: Set<string>, selectedId: string,
54
+ * edges: object[], kinds: Map<string, number>, kind: string|null, throughChildren: boolean }}
55
+ * `kinds` counts every lit line by kind before a `kind` filter, so the panel keeps listing the others; `edges` is the lit lines
56
+ * (after it); `kind` is the filter in force, or null when none was asked for or the asked kind is not among the lit lines
57
+ */
58
+ export function activeSubgraph(selected, edges, { throughChildren = false, kind = null } = {}) {
59
+ if (!selected) return null;
60
+ const id = selected.id;
61
+ const childIds = new Set();
62
+ for (const e of edges) if (!e.kind && !e.dashed && e.from === id) childIds.add(e.to);
63
+ const core = throughChildren ? new Set([id, ...childIds]) : null; // the selected node and its children: with `throughChildren` their neighbours light too
64
+ const lit = [];
65
+ for (const e of edges) {
66
+ if (core ? (core.has(e.from) || core.has(e.to)) : (e.from === id || e.to === id)) lit.push(e);
67
+ }
68
+ const kinds = new Map();
69
+ for (const e of lit) { const k = edgeKindOf(e); kinds.set(k, (kinds.get(k) || 0) + 1); }
70
+ const filter = kind && kinds.has(kind) ? kind : null;
71
+ const shown = filter ? lit.filter((e) => edgeKindOf(e) === filter) : lit;
72
+ const connectedNodeIds = new Set([id]);
73
+ const connectedEdgeKeys = new Set();
74
+ for (const e of shown) { connectedNodeIds.add(e.from); connectedNodeIds.add(e.to); connectedEdgeKeys.add(edgeKeyOf(e)); }
75
+ return { connectedNodeIds, connectedEdgeKeys, childIds, selectedId: id, edges: shown, kinds, kind: filter, throughChildren };
76
+ }
77
+
78
+ /**
79
+ * The lit lines counted by kind, in LINE_KINDS order (a kind of the project's own after them, by name).
80
+ * @param {NonNullable<ReturnType<typeof activeSubgraph>>} sub
81
+ * @returns {{kind: string, count: number, word: string}[]}
82
+ */
83
+ export function linesByKind(sub) {
84
+ const rank = (k) => { const i = LINE_KINDS.indexOf(k); return i < 0 ? LINE_KINDS.length : i; };
85
+ return [...sub.kinds.entries()]
86
+ .sort((a, b) => rank(a[0]) - rank(b[0]) || (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0))
87
+ .map(([kind, count]) => ({ kind, count, word: kindPanelWord(kind) }));
88
+ }
89
+
90
+ /**
91
+ * The lit lines of one kind, as the node at their far end: { node id, whether the line leaves the selection }, in map order.
92
+ * @param {NonNullable<ReturnType<typeof activeSubgraph>>} sub
93
+ * @param {string} kind
94
+ */
95
+ export function farEnds(sub, kind) {
96
+ const rows = [];
97
+ const seen = new Set();
98
+ for (const e of sub.edges) {
99
+ if (edgeKindOf(e) !== kind) continue;
100
+ const out = e.from === sub.selectedId;
101
+ const other = out ? e.to : e.from;
102
+ if (seen.has(other)) continue;
103
+ seen.add(other);
104
+ rows.push({ id: other, out });
105
+ }
106
+ return rows;
107
+ }
108
+
109
+ /** The screen point of a world point */
110
+ const screenOf = (camera, n) => ({ x: camera.x + n.x * camera.zoom, y: camera.y + n.y * camera.zoom });
111
+
112
+ /**
113
+ * How many of `nodes` are outside `rect` (the part of the map nothing covers) under `camera`.
114
+ * @param {{x:number, y:number}[]} nodes
115
+ * @param {{x:number, y:number, zoom:number}} camera
116
+ * @param {{x:number, y:number, width:number, height:number}} rect
117
+ */
118
+ export function countOffScreen(nodes, camera, rect) {
119
+ let off = 0;
120
+ for (const n of nodes) {
121
+ const p = screenOf(camera, n);
122
+ if (p.x < rect.x || p.x > rect.x + rect.width || p.y < rect.y || p.y > rect.y + rect.height) off++;
123
+ }
124
+ return off;
125
+ }
126
+
127
+ /** The zoom a selection is framed at or above: a lit set that needs less than this is not framed, the node is centred at zoom 1 instead */
128
+ export const FRAME_MIN_ZOOM = 0.5;
129
+ /** The zoom a node is centred at when its lit set is too wide to frame */
130
+ export const CENTRE_ZOOM = 1;
131
+
132
+ /**
133
+ * Where the camera goes for a selection that did not come from a click on the map (spec 0008 D6).
134
+ * @param {{x:number, y:number, r?:number}} node
135
+ * @param {{x:number, y:number, r?:number}[]} lit the node and what it lights
136
+ * @param {{x:number, y:number, width:number, height:number}} rect the part of the map nothing covers (sheets.js visibleMapRect)
137
+ * @param {{padding?: number}} [opts]
138
+ * @returns {{x:number, y:number, zoom:number, framed:boolean}}
139
+ */
140
+ export function selectionCamera(node, lit, rect, { padding = 40 } = {}) {
141
+ const all = lit.length ? lit : [node];
142
+ const fit = frameNodes(all, rect, { padding, maxZoom: CENTRE_ZOOM, minZoom: 0.0001 });
143
+ if (fit && fit.zoom >= FRAME_MIN_ZOOM) return { ...fit, framed: true };
144
+ return { x: rect.x + rect.width / 2 - node.x * CENTRE_ZOOM, y: rect.y + rect.height / 2 - node.y * CENTRE_ZOOM, zoom: CENTRE_ZOOM, framed: false };
145
+ }
146
+
147
+ /** The words that say how much of a selection is in view: "27 connected · 9 off screen" */
148
+ export function connectedLine(connected, off) {
149
+ return off > 0 ? `${connected} connected · ${off} off screen` : `${connected} connected · all on screen`;
150
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * @module explorer/selection-view
3
+ *
4
+ * The page side of a selection's reach (spec 0008 D5 and D6; the rules are in selection-model.js): which nodes the selection lights, how
5
+ * many of them are off screen, and the camera that frames them. "Frame them" in the node panel and the `F` key call `frameSelection`;
6
+ * a selection that comes from search, a list, a link or the keyboard calls it too (panels.js), and a click on the map never does.
7
+ */
8
+
9
+ import { state } from './state.js';
10
+ import { getScene, fitPadding } from './render.js';
11
+ import { visibleMapRect } from './sheets.js';
12
+ import { flyTo } from './fly.js';
13
+ import { countOffScreen, selectionCamera } from './selection-model.js';
14
+
15
+ /** The scene's nodes the selection lights (the selected node included), or [] with no selection */
16
+ export function litNodes() {
17
+ if (!state.selectedNode || state.scenario) return [];
18
+ return getScene().nodes.filter((n) => n.connected);
19
+ }
20
+
21
+ /** How many nodes the selection lights, and how many of those are outside the part of the map nothing covers */
22
+ export function reach() {
23
+ if (!state.selectedNode) return { connected: 0, off: 0 };
24
+ const others = litNodes().filter((n) => n.id !== state.selectedNode.id); // the selected node is the one the reader is on
25
+ return { connected: others.length, off: others.length ? countOffScreen(others, state.camera, visibleMapRect()) : 0 };
26
+ }
27
+
28
+ /** Flies the camera to the selection and what it lights; returns the target, or null with no selection */
29
+ export function frameSelection() {
30
+ const node = state.selectedNode;
31
+ if (!node) return null;
32
+ const lit = litNodes();
33
+ const target = selectionCamera(node, lit, visibleMapRect(), { padding: fitPadding() });
34
+ flyTo({ x: target.x, y: target.y, zoom: target.zoom });
35
+ return target;
36
+ }
@@ -210,14 +210,14 @@ export const NODE_VERT = `${HEAD}${TO_CLIP}
210
210
  layout(location = 0) in vec2 a_corner;
211
211
  layout(location = 1) in vec4 a_geom; // x, y, r (world), alpha
212
212
  layout(location = 2) in vec4 a_color; // r, g, b (0..1), glow (px)
213
- layout(location = 3) in vec3 a_kind; // halo code, mark code, colour alpha
213
+ layout(location = 3) in vec4 a_kind; // halo code, mark code, colour alpha, shape code (0 circle, 1 rounded square, 2 hexagon, 3 square, 4 diamond; plus 8 when a glyph takes the core dot's place)
214
214
  uniform vec4 u_haloGeom[4]; // fillR, ringR, ringWidth (world)
215
215
  uniform vec4 u_markGeom[5]; // per mark code: ringR, ringWidth (world), dash on, dash off
216
216
  uniform vec3 u_markMin; // MARK_MIN_PX, in screen px: ring thickness, edge thickness, gap from the node
217
217
  out vec2 v_local;
218
218
  flat out vec4 v_geom;
219
219
  flat out vec4 v_color;
220
- flat out vec3 v_kind;
220
+ flat out vec4 v_kind;
221
221
  void main() {
222
222
  float zoom = u_cam.z;
223
223
  int halo = int(a_kind.x + 0.5);
@@ -226,7 +226,7 @@ void main() {
226
226
  float pad = a_color.w * 1.5 / u_dpr;
227
227
  if (halo > 0) pad = max(pad, (u_haloGeom[halo].y + u_haloGeom[halo].z * 0.5) * zoom);
228
228
  if (mark > 0) pad = max(pad, max(u_markGeom[mark].x * zoom, u_markMin.z) + max(u_markGeom[mark].y * zoom, u_markMin.x) + 1.5);
229
- float ext = a_geom.z * zoom + pad + 2.0;
229
+ float ext = a_geom.z * zoom * 1.2 + pad + 2.0; // a diamond reaches 1.15 radii
230
230
  vec2 centre = u_cam.xy + a_geom.xy * zoom;
231
231
  v_local = a_corner * ext;
232
232
  v_geom = a_geom;
@@ -240,7 +240,10 @@ export const NODE_FRAG = `${HEAD}${COVER}
240
240
  in vec2 v_local;
241
241
  flat in vec4 v_geom;
242
242
  flat in vec4 v_color;
243
- flat in vec3 v_kind;
243
+ flat in vec4 v_kind;
244
+ uniform vec4 u_shape; // rounded square half side, its corner, hexagon inradius, square half side (x radius)
245
+ uniform vec2 u_shape2; // square corner, diamond half diagonal (x radius)
246
+ uniform float u_glyphMin; // GLYPH_MIN_PX: a node this wide on screen shows its glyph (the icon pass) instead of the core dot
244
247
  uniform vec4 u_haloFill[4];
245
248
  uniform vec4 u_haloRing[4];
246
249
  uniform vec4 u_haloGeom[4];
@@ -249,6 +252,25 @@ uniform vec4 u_markGeom[5];
249
252
  uniform vec3 u_markMin;
250
253
  uniform vec4 u_core;
251
254
  uniform float u_coreRatio;
255
+ // Signed distance (px) to a node's silhouette of radius R: the same shapes renderer-canvas2d.js draws (silhouette)
256
+ float sdRoundBox(vec2 p, float h, float c) {
257
+ vec2 q = abs(p) - vec2(h - c);
258
+ return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - c;
259
+ }
260
+ float sdHex(vec2 p, float r) {
261
+ const vec3 k = vec3(-0.866025404, 0.5, 0.577350269);
262
+ p = abs(p);
263
+ p -= 2.0 * min(dot(k.xy, p), 0.0) * k.xy;
264
+ p -= vec2(clamp(p.x, -k.z * r, k.z * r), r);
265
+ return length(p) * sign(p.y);
266
+ }
267
+ float sdShape(vec2 p, float R, int shape, float len) {
268
+ if (shape == 1) return sdRoundBox(p, u_shape.x * R, u_shape.y * R);
269
+ if (shape == 2) return sdHex(p.yx, u_shape.z * R);
270
+ if (shape == 3) return sdRoundBox(p, u_shape.w * R, u_shape2.x * R);
271
+ if (shape == 4) return (abs(p.x) + abs(p.y) - u_shape2.y * R) * 0.70710678;
272
+ return len - R;
273
+ }
252
274
  void main() {
253
275
  float zoom = u_cam.z;
254
276
  float pix = 1.0 / u_dpr;
@@ -271,9 +293,12 @@ void main() {
271
293
  float glow = v_color.w;
272
294
  if (glow > 0.0) over(acc, v_color.rgb, v_kind.z * al * glowDisc(len, R, glow * 0.5 / u_dpr));
273
295
 
274
- // The circle and its white core
275
- over(acc, v_color.rgb, v_kind.z * al * coverEdge(len - R, pix));
276
- over(acc, u_core.rgb, u_core.a * al * coverEdge(len - R * u_coreRatio, pix));
296
+ // The silhouette, and its white core unless the type's glyph is drawn there (glyphs.js: the icon pass draws it once the node is wide enough)
297
+ int shapeCode = int(v_kind.w + 0.5);
298
+ bool glyph = shapeCode >= 8;
299
+ if (glyph) shapeCode -= 8;
300
+ over(acc, v_color.rgb, v_kind.z * al * coverEdge(sdShape(v_local, R, shapeCode, len), pix));
301
+ if (!glyph || 2.0 * R < u_glyphMin) over(acc, u_core.rgb, u_core.a * al * coverEdge(len - R * u_coreRatio, pix));
277
302
 
278
303
  // Mark: a ring with the look of its own mark (dashed when its dash is set: added), and for removed a
279
304
  // strike through the node. However far out the camera is, the ring stays u_markMin.z px from the node
@@ -449,14 +474,15 @@ export const TIP_VERT = `${HEAD}${TO_CLIP}
449
474
  layout(location = 0) in vec2 a_corner;
450
475
  layout(location = 1) in vec4 a_tip; // x, y (world), direction x, y (unit)
451
476
  layout(location = 2) in vec4 a_color;
452
- layout(location = 3) in float a_kind; // 1 = arrowhead (tip at x, y), 2 = strike (centred on x, y)
477
+ layout(location = 3) in float a_kind; // 1 = arrowhead (tip at x, y), 2 = strike (centred on x, y), 3 = chevron, 4 = bar, 5 = diamond (all with their tip at x, y), 6 = tick (centred on x, y)
453
478
  uniform vec4 u_tip; // arrow length, arrow spread (radians), strike half length, strike width (world)
479
+ uniform vec4 u_end; // stroke width of the open marks, bar half length, diamond half length, diamond half width (world)
454
480
  out vec2 v_local;
455
481
  flat out vec4 v_color;
456
482
  flat out float v_kind;
457
483
  void main() {
458
484
  float zoom = u_cam.z;
459
- float ext = max(u_tip.x, max(u_tip.z, u_tip.w)) * zoom + 3.0;
485
+ float ext = max(max(u_tip.x, 2.0 * u_end.z), max(u_tip.z, u_end.y)) * zoom + 3.0;
460
486
  vec2 dir = a_tip.zw;
461
487
  vec2 off = a_corner * ext;
462
488
  v_local = vec2(dot(off, dir), dot(off, vec2(-dir.y, dir.x)));
@@ -471,20 +497,54 @@ in vec2 v_local;
471
497
  flat in vec4 v_color;
472
498
  flat in float v_kind;
473
499
  uniform vec4 u_tip;
500
+ uniform vec4 u_end; // stroke width of the open marks, bar half length, diamond half length, diamond half width (world)
501
+ uniform vec2 u_tick; // tick half length, tick width (world)
502
+ // Distance to the segment a-b
503
+ float sdSeg(vec2 p, vec2 a, vec2 b) {
504
+ vec2 pa = p - a;
505
+ vec2 ba = b - a;
506
+ float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
507
+ return length(pa - ba * h);
508
+ }
509
+ // Distance to a rhombus with half diagonals b (iq's sdRhombus)
510
+ float ndot(vec2 a, vec2 b) { return a.x * b.x - a.y * b.y; }
511
+ float sdRhombus(vec2 p, vec2 b) {
512
+ p = abs(p);
513
+ float h = clamp(ndot(b - 2.0 * p, b) / dot(b, b), -1.0, 1.0);
514
+ float d = length(p - 0.5 * b * vec2(1.0 - h, 1.0 + h));
515
+ return d * sign(p.x * b.y + p.y * b.x - b.x * b.y);
516
+ }
474
517
  void main() {
475
518
  float zoom = u_cam.z;
476
519
  float pix = 1.0 / u_dpr;
477
520
  float sd;
521
+ float L = u_tip.x * zoom;
522
+ float sn = sin(u_tip.y);
523
+ float cs = cos(u_tip.y);
524
+ float w = max(u_end.x * zoom, 1.2) * 0.5; // half the stroke of an open mark: at least 1.2 px thick so it stays visible zoomed out
478
525
  if (v_kind < 1.5) {
479
526
  // The triangle with its tip at the origin and two barbs L back, spread radians either side of the line
480
- float L = u_tip.x * zoom;
481
- float sn = sin(u_tip.y);
482
- float cs = cos(u_tip.y);
483
527
  sd = max(max(sn * v_local.x + cs * v_local.y, sn * v_local.x - cs * v_local.y), -v_local.x - L * cs);
484
- } else {
528
+ } else if (v_kind < 2.5) {
485
529
  // A box across the edge, at least 1.5 px thick so it stays visible zoomed out
486
530
  vec2 q = abs(v_local) - vec2(max(u_tip.w * zoom, 1.5) * 0.5, u_tip.z * zoom);
487
531
  sd = length(max(q, 0.0)) + min(max(q.x, q.y), 0.0);
532
+ } else if (v_kind < 3.5) {
533
+ // The chevron: the arrowhead's two barbs, open (a line each, from the tip back)
534
+ vec2 barb = vec2(-L * cs, L * sn);
535
+ sd = min(sdSeg(v_local, vec2(0.0), barb), sdSeg(v_local, vec2(0.0), vec2(barb.x, -barb.y))) - w;
536
+ } else if (v_kind < 4.5) {
537
+ // The bar: across the line at the target
538
+ vec2 q = abs(v_local - vec2(-w, 0.0)) - vec2(w, u_end.y * zoom);
539
+ sd = length(max(q, 0.0)) + min(max(q.x, q.y), 0.0);
540
+ } else if (v_kind < 5.5) {
541
+ // The diamond: an outline, its front vertex at the tip
542
+ vec2 h = vec2(u_end.z, u_end.w) * zoom;
543
+ sd = abs(sdRhombus(v_local - vec2(-h.x, 0.0), h)) - w;
544
+ } else {
545
+ // The tick: a short box across the edge, centred, at least 1.5 px thick
546
+ vec2 q = abs(v_local) - vec2(max(u_tick.y * zoom, 1.5) * 0.5, u_tick.x * zoom);
547
+ sd = length(max(q, 0.0)) + min(max(q.x, q.y), 0.0);
488
548
  }
489
549
  outColor = vec4(v_color.rgb * v_color.a, v_color.a) * coverEdge(sd, pix);
490
550
  }
@@ -540,9 +600,12 @@ void main() {
540
600
  export const ICON_VERT = `${HEAD}${TO_CLIP}
541
601
  layout(location = 0) in vec2 a_corner;
542
602
  layout(location = 1) in vec4 a_geom; // x, y, side (world), alpha
603
+ uniform float u_minR; // letters and glyphs: the least on-screen radius (px) of a node that shows them; 0 for a theme's icon
604
+ uniform float u_sideRatio; // the image's side as a multiple of the node's radius (LETTER_RATIO, GLYPH_RATIO), to read the radius back
543
605
  out vec2 v_uv;
544
606
  flat out float v_alpha;
545
607
  void main() {
608
+ if (u_minR > 0.0 && a_geom.z / u_sideRatio * u_cam.z < u_minR) { gl_Position = vec4(2.0, 2.0, 2.0, 1.0); v_uv = vec2(0.0); v_alpha = 0.0; return; } // too small on screen: outside the clip space, nothing drawn
546
609
  vec2 centre = u_cam.xy + a_geom.xy * u_cam.z;
547
610
  vec2 pos = centre + a_corner * (a_geom.z * 0.5 * u_cam.z);
548
611
  v_uv = vec2(a_corner.x * 0.5 + 0.5, a_corner.y * 0.5 + 0.5); // screen y runs down and an image's first row is its top, so the corner below the centre reads the last row
@@ -15,7 +15,8 @@
15
15
  * search radius touches, where the radius is the largest hit radius any node can have, so a big
16
16
  * node whose circle spans several cells is still found from any of them.
17
17
  *
18
- * A node's hit radius is `r + slop` for a number slop, or `max(r + slop.pad, slop.min)` for
18
+ * A node's hit radius is measured from `hitR` when it has one (a scene node: as far as its silhouette reaches, scene.js SHAPE_REACH), else `r`:
19
+ * `r + slop` for a number slop, or `max(r + slop.pad, slop.min)` for
19
20
  * `{ pad, min }`: input.js asks for "a few px more than the circle, and never less than a finger"
20
21
  * in world units, which depends on the zoom.
21
22
  *
@@ -28,6 +29,8 @@
28
29
  */
29
30
 
30
31
  export const DEFAULT_CELL_SIZE = 64;
32
+ /** The radius an item is hit within: its silhouette's reach (a square's corner, a diamond's point) when the scene gave one, else its circle */
33
+ const radiusOf = (item) => (typeof item.hitR === 'number' && item.hitR > item.r ? item.hitR : item.r);
31
34
 
32
35
  /**
33
36
  * @template {SpatialItem} T
@@ -41,7 +44,7 @@ export function createSpatialIndex(items, { cellSize = DEFAULT_CELL_SIZE } = {})
41
44
  const key = (cx, cy) => cx * 4194304 + cy; // 2^22: |cy| stays far below it for any map
42
45
  for (let i = 0; i < items.length; i++) {
43
46
  const item = items[i];
44
- if (item.r > maxR) maxR = item.r;
47
+ if (radiusOf(item) > maxR) maxR = radiusOf(item);
45
48
  const k = key(Math.floor(item.x / cellSize), Math.floor(item.y / cellSize));
46
49
  const cell = cells.get(k);
47
50
  if (cell) cell.push(i);
@@ -81,7 +84,7 @@ export function createSpatialIndex(items, { cellSize = DEFAULT_CELL_SIZE } = {})
81
84
  const i = cell[j];
82
85
  if (i <= best) continue;
83
86
  const item = items[i];
84
- const reachOfItem = Math.max(item.r + pad, min);
87
+ const reachOfItem = Math.max(radiusOf(item) + pad, min);
85
88
  if (Math.hypot(item.x - x, item.y - y) <= reachOfItem) best = i;
86
89
  }
87
90
  }
@@ -111,7 +114,7 @@ export function createSpatialIndex(items, { cellSize = DEFAULT_CELL_SIZE } = {})
111
114
  const item = items[i];
112
115
  const dx = item.x - Math.min(Math.max(item.x, minX), maxX);
113
116
  const dy = item.y - Math.min(Math.max(item.y, minY), maxY);
114
- if (dx * dx + dy * dy <= item.r * item.r) found.push(i);
117
+ if (dx * dx + dy * dy <= radiusOf(item) ** 2) found.push(i);
115
118
  }
116
119
  }
117
120
  }
package/explorer/state.js CHANGED
@@ -36,6 +36,10 @@ export const state = {
36
36
 
37
37
  camera: { x: 0, y: 0, zoom: 1 },
38
38
  selectedNode: null,
39
+ /** The node panel's "Include its children's lines" switch (spec 0008 D5): off, a selection lights one hop; on, the children's neighbours too. Kept across selections */
40
+ throughChildren: false,
41
+ /** The edge kind the panel chose to light alone (selection-model.js), or null for every kind; a new selection clears it */
42
+ selectedKind: null,
39
43
  /** The config's `forge` (`{ type, url, repo, baseBranch? }`), or null: where a node's file is opened (file-url.js) */
40
44
  forge: null,
41
45
  searchQuery: '',
@@ -139,6 +139,7 @@
139
139
  --corn-map-glow: auto;
140
140
  /* The smallest a map label is drawn on screen (spec 0008): auto is 11px; a theme may raise it, not lower it. A hub's title is 2px more */
141
141
  --corn-map-label-min: auto;
142
+ --corn-map-glyph: auto;
142
143
  /* Diff, selection and focus */
143
144
  --corn-map-added: auto;
144
145
  --corn-map-changed: auto;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cornfield",
3
- "version": "0.42.0",
3
+ "version": "0.43.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)",