From 268a1ca8c6db359c916112dcc164b1519616dbae Mon Sep 17 00:00:00 2001 From: Roger Barnes Date: Tue, 1 Dec 2026 10:47:25 +1100 Subject: [PATCH] Bump structured-context to ^0.28.1 and honour numeric node order in tree and landscape layouts --- apps/navigator/package.json | 2 +- bun.lock | 20 +++++++------- packages/map-views/package.json | 2 +- .../src/core/layouts/landscape/projection.ts | 26 ++++++++++++++----- .../src/core/layouts/tree/projection.ts | 16 ++++-------- packages/map-views/tests/landscape.test.ts | 18 +++++++++++++ packages/map-views/tests/tree.test.ts | 23 +++++++++++++--- packages/sctx-map-views/package.json | 2 +- 8 files changed, 77 insertions(+), 32 deletions(-) diff --git a/apps/navigator/package.json b/apps/navigator/package.json index 1853bcc..8a8d506 100644 --- a/apps/navigator/package.json +++ b/apps/navigator/package.json @@ -12,7 +12,7 @@ "dependencies": { "react": "^19.3.0", "react-dom": "^19.3.0", - "structured-context": "^0.26.0", + "structured-context": "^0.28.1", "structured-context-map-views": "workspace:*" }, "devDependencies": { diff --git a/bun.lock b/bun.lock index 107bcfd..08e4d05 100644 --- a/bun.lock +++ b/bun.lock @@ -17,7 +17,7 @@ "dependencies": { "react": "^19.3.0", "react-dom": "^19.3.0", - "structured-context": "^0.26.0", + "structured-context": "^0.28.1", "structured-context-map-views": "workspace:*", }, "devDependencies": { @@ -32,7 +32,7 @@ }, "packages/map-views": { "name": "structured-context-map-views", - "version": "0.7.1", + "version": "0.7.2", "bin": { "structured-context-map-views": "dist/cli.js", }, @@ -52,7 +52,7 @@ "@vitejs/plugin-react": "^6.1.1", "react": "^19.3.0", "react-dom": "^19.3.0", - "structured-context": "^0.26.0", + "structured-context": "^0.28.1", "tailwindcss": "^4.3.3", "vite": "^8.3.2", }, @@ -64,9 +64,9 @@ }, "packages/sctx-map-views": { "name": "sctx-map-views", - "version": "0.7.1", + "version": "0.7.2", "devDependencies": { - "structured-context": "^0.26.0", + "structured-context": "^0.28.1", "structured-context-map-views": "workspace:*", }, }, @@ -264,6 +264,8 @@ "ajv": ["ajv@8.20.0", "", { "dependencies": { "fast-deep-equal": "^3.1.3", "fast-uri": "^3.0.1", "json-schema-traverse": "^1.0.0", "require-from-string": "^2.0.2" } }, "sha512-Thbli+OlOj+iMPYFBVBfJ3OmCAnaSyNn4M1vz9T6Gka5Jt9ba/HIR56joy65tY6kx/FCF5VXNB819Y7/GUrBGA=="], + "ajv-formats": ["ajv-formats@3.0.1", "", { "dependencies": { "ajv": "^8.0.0" } }, "sha512-8iUql50EUR+uUcdRQ3HDqa6EVyo3docL8g5WJ3FNcWmu62IbkGUue/pEyLBW8VGKKucTPgqeks4fIU1DA4yowQ=="], + "argparse": ["argparse@3.0.4", "", {}, "sha512-fwWK3Zq22l4XJJjx5mcBHGikIk+hJqO+32dPm0Jur7+rtG5x1scOjSfdT9ZKeWHPpsya/l0HHK4tDImBkTrtvA=="], "bail": ["bail@2.0.2", "", {}, "sha512-0xO6mYd7JB2YesxDKplafRpsiOzPt9V02ddPCLbY1xYGPOX24NTyN50qnUxgCPcSoYMhKpAuBTjQoRZCAkUDRw=="], @@ -276,7 +278,7 @@ "character-entities": ["character-entities@2.0.2", "", {}, "sha512-shx7oQ0Awen/BRIdkjkvz54PnEEI/EjwXDSIZp86/KKdbafHh1Df/RYGBhn4hbe2+uKC9FnT5UCEdyPz3ai9hQ=="], - "chokidar": ["chokidar@5.0.0", "", { "dependencies": { "readdirp": "^5.0.0" } }, "sha512-TQMmc3w+5AxjpL8iIiwebF73dRDF4fBIieAqGn9RGCWaEVwQ6Fb2cGe31Yns0RRIzii5goJ1Y7xbMwo1TxMplw=="], + "chokidar": ["chokidar@6.0.0", "", { "dependencies": { "readdirp": "^5.1.1" } }, "sha512-Ec2Z7wpcKBiMpR7DxteoqS/sq5G5d2A4fpxzPv6+fD7xNNdrTghn7L11YwV5EN1B3i72Erc3XVT5FkWIDx90+A=="], "commander": ["commander@15.0.0", "", {}, "sha512-z67u4ZhzCL/Tydu1lJARtEZYWbWaN7oYLHbsuzocr6y4N6WZAagG3RQ4FW61V1/0+jImpj293XfrcYnd1qxtPg=="], @@ -540,7 +542,7 @@ "strip-bom-string": ["strip-bom-string@1.0.0", "", {}, "sha512-uCC2VHvQRYu+lMh4My/sFNmF2klFymLX1wHJeXnbEJERpV/ZsVuonzerjfrGpIGF7LBVa1O7i9kjiWvJiFck8g=="], - "structured-context": ["structured-context@0.26.0", "", { "dependencies": { "ajv": "^8.20.0", "chokidar": "^5.0.0", "commander": "^15.0.0", "fast-glob": "^3.3.3", "gray-matter": "^4.0.3", "js-yaml": "^5.4.3", "json-schema-to-ts": "^3.1.1", "json5": "^2.2.3", "jsonata": "^2.2.1", "mdast-util-to-string": "^4.0.0", "remark-gfm": "^4.0.1", "remark-parse": "^11.0.0", "sctx-map-views": "^0.6.2", "unified": "^11.0.5", "unist-util-visit": "^5.1.0" }, "peerDependencies": { "typescript": "^5" }, "bin": { "sctx": "dist/index.js" } }, "sha512-GM5QeG0fDoHtyXLA84A915NQxTn0f/2ZlD2mXmxEGty11YBJ+XdmepAh8kgdYBtJwER/Tx78tm9WYg8PJOsUQw=="], + "structured-context": ["structured-context@0.28.1", "", { "dependencies": { "ajv": "^8.20.0", "ajv-formats": "^3.0.1", "chokidar": "^6.0.0", "commander": "^15.0.0", "fast-glob": "^3.3.3", "gray-matter": "^4.0.3", "js-yaml": "^5.4.3", "json-schema-to-ts": "^3.1.1", "json5": "^2.2.3", "jsonata": "^2.2.1", "mdast-util-to-string": "^4.0.0", "remark-gfm": "^4.0.1", "remark-parse": "^11.0.0", "sctx-map-views": "^0.7.1", "unified": "^11.0.5", "unist-util-visit": "^5.1.0", "yaml": "^2.9.1" }, "peerDependencies": { "typescript": "^5" }, "bin": { "sctx": "dist/index.js" } }, "sha512-CGQxQum8cQghKdWvkChMXvBrFYNAf+Y7lIfic//9rfFUr9lVuEAmXsjiPNtESnOPy3wwC/MlBcTWj1wTkXJ88Q=="], "structured-context-map-views": ["structured-context-map-views@workspace:packages/map-views"], @@ -578,6 +580,8 @@ "vite": ["vite@8.3.2", "", { "dependencies": { "lightningcss": "^1.33.0", "picomatch": "^4.0.7", "postcss": "^8.5.28", "rolldown": "~1.2.11", "tinyglobby": "^0.2.17" }, "optionalDependencies": { "fsevents": "~2.3.3" }, "peerDependencies": { "@types/node": "^20.19.0 || >=22.12.0", "@vitejs/devtools": "^0.7.1", "esbuild": "^0.27.0 || ^0.28.0", "jiti": ">=1.21.0", "less": "^4.0.0", "sass": "^1.70.0", "sass-embedded": "^1.70.0", "stylus": ">=0.54.8", "sugarss": "^5.0.0", "terser": "^5.16.0", "tsx": "^4.8.1", "yaml": "^2.4.2" }, "optionalPeers": ["@types/node", "@vitejs/devtools", "esbuild", "jiti", "less", "sass", "sass-embedded", "stylus", "sugarss", "terser", "tsx", "yaml"], "bin": { "vite": "bin/vite.js" } }, "sha512-SQr1x6W5vVSbROg7vsyXIaxK9b0G7zsT68acdWWRmnBUsgDieLCRG+Rep9WdZgcposvv/GSnr4GUUBqB3vXq6w=="], + "yaml": ["yaml@2.9.1", "", { "bin": { "yaml": "bin.mjs" } }, "sha512-3NxN8+78OdzbT7C/WjGsyfPAtJaN3FNDsWxv7Y7mcDsT/oOmgW8BpyQQFFBnvZE3j9Y2Sdz1ULFLezL7Eb2yFw=="], + "zwitch": ["zwitch@2.0.4", "", {}, "sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A=="], "@tailwindcss/node/lightningcss": ["lightningcss@1.32.0", "", { "dependencies": { "detect-libc": "^2.0.3" }, "optionalDependencies": { "lightningcss-android-arm64": "1.32.0", "lightningcss-darwin-arm64": "1.32.0", "lightningcss-darwin-x64": "1.32.0", "lightningcss-freebsd-x64": "1.32.0", "lightningcss-linux-arm-gnueabihf": "1.32.0", "lightningcss-linux-arm64-gnu": "1.32.0", "lightningcss-linux-arm64-musl": "1.32.0", "lightningcss-linux-x64-gnu": "1.32.0", "lightningcss-linux-x64-musl": "1.32.0", "lightningcss-win32-arm64-msvc": "1.32.0", "lightningcss-win32-x64-msvc": "1.32.0" } }, "sha512-NXYBzinNrblfraPGyrbPoD19C1h9lfI/1mzgWYvXUTe414Gz/X1FD2XBZSZM7rRTrMA8JL3OtAaGifrIKhQ5yQ=="], @@ -602,8 +606,6 @@ "micromatch/picomatch": ["picomatch@2.3.2", "", {}, "sha512-V7+vQEJ06Z+c5tSye8S+nHUfI51xoXIXjHQ99cQtKUkQqqO1kO/KCJUfZXuB47h/YBlDhah2H3hdUGXn8ie0oA=="], - "structured-context/sctx-map-views": ["sctx-map-views@0.6.2", "", {}, "sha512-E7ZeoEjbvIXaabD6oc5ukZ767OUB/qfK+PV4DS6vm93+G6pg0v9RC7QLBdzS0sW82zZ86T+GJDLM3pdm9eUO2w=="], - "@tailwindcss/node/lightningcss/detect-libc": ["detect-libc@2.1.2", "", {}, "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ=="], "@tailwindcss/node/lightningcss/lightningcss-android-arm64": ["lightningcss-android-arm64@1.32.0", "", { "os": "android", "cpu": "arm64" }, "sha512-YK7/ClTt4kAK0vo6w3X+Pnm0D2cf2vPHbhOXdoNti1Ga0al1P4TBZhwjATvjNwLEBCnKvjJc2jQgHXH0NEwlAg=="], diff --git a/packages/map-views/package.json b/packages/map-views/package.json index 58f366a..87954a5 100644 --- a/packages/map-views/package.json +++ b/packages/map-views/package.json @@ -79,7 +79,7 @@ "@vitejs/plugin-react": "^6.1.1", "react": "^19.3.0", "react-dom": "^19.3.0", - "structured-context": "^0.26.0", + "structured-context": "^0.28.1", "tailwindcss": "^4.3.3", "vite": "^8.3.2" } diff --git a/packages/map-views/src/core/layouts/landscape/projection.ts b/packages/map-views/src/core/layouts/landscape/projection.ts index 678b2e9..b211513 100644 --- a/packages/map-views/src/core/layouts/landscape/projection.ts +++ b/packages/map-views/src/core/layouts/landscape/projection.ts @@ -76,7 +76,20 @@ type Filter = ValueFilter | NodeFilter; * way a level is joined to the node level above, so `via` applies there too. */ export function buildLandscape(graph: SpaceGraph, regionId: string, layout: LayoutConfig<'landscape'>): Landscape { - const order = new Map([...graph.nodes.keys()].map((title, i) => [title, i])); + const spaceOrder = new Map([...graph.nodes.keys()].map((title, i) => [title, i])); + const nodeOrder = (node: SpaceNode): number | undefined => { + const value = (node.schemaData as Record | undefined)?.order; + return typeof value === 'number' && Number.isFinite(value) ? value : undefined; + }; + /** Sort nodes by numeric `order` ascending (matching SpaceGraph), falling back to space order. */ + const compareNodes = (a: SpaceNode, b: SpaceNode): number => { + const oa = nodeOrder(a); + const ob = nodeOrder(b); + if (oa !== undefined && ob !== undefined && oa !== ob) return oa - ob; + if (oa !== undefined && ob === undefined) return -1; + if (oa === undefined && ob !== undefined) return 1; + return spaceOrder.get(a.id)! - spaceOrder.get(b.id)!; + }; // The first occurrence of each node, across strips, for marking repeats. const first = new Map(); @@ -156,9 +169,10 @@ export function buildLandscape(graph: SpaceGraph, regionId: string, layout: Layo return reached.filter(({ node }) => values.every((f) => valuesOf(node, f.field).includes(f.value))); }; - const ofType = (type: string) => [...graph.nodes.values()].filter((node) => node.resolvedType === type); + const ofType = (type: string) => + [...graph.nodes.values()].filter((node) => node.resolvedType === type).sort(compareNodes); - /** Neighbours of a node with the given type, across edges in either direction, in space order. */ + /** Neighbours of a node with the given type, across edges in either direction, in `order` then space order. */ const neighbours = (title: SpaceNode['id'], type: string, field: string | undefined): SpaceNode[] => { const titles = new Set([ ...childEdges(graph, title) @@ -171,7 +185,7 @@ export function buildLandscape(graph: SpaceGraph, regionId: string, layout: Layo return [...titles] .map((t) => graph.nodes.get(t)!) .filter((node) => node.resolvedType === type) - .sort((a, b) => order.get(a.id)! - order.get(b.id)!); + .sort(compareNodes); }; /** @@ -196,7 +210,7 @@ export function buildLandscape(graph: SpaceGraph, regionId: string, layout: Layo byTitle.set(next.id, candidate); } } - return [...byTitle.values()].sort((a, b) => order.get(a.node.id)! - order.get(b.node.id)!); + return [...byTitle.values()].sort((a, b) => compareNodes(a.node, b.node)); }; /** @@ -205,7 +219,7 @@ export function buildLandscape(graph: SpaceGraph, regionId: string, layout: Layo * types of the first node level of each strip it divides) have, so a value no node has is not a group. * A node with no value, or joined to no node of the dimension, is in none of its groups; it * is shown as ungrouped, the same for both kinds. - * PROTOTYPE: groups come in space order. A field's declared domain, or an `order` field, would give + * PROTOTYPE: field dimension groups come in first-seen order. A field's declared domain would give * the order authors expect and let a field dimension show empty groups too. */ const dimensionGroups = ( diff --git a/packages/map-views/src/core/layouts/tree/projection.ts b/packages/map-views/src/core/layouts/tree/projection.ts index e4424ac..3584d5b 100644 --- a/packages/map-views/src/core/layouts/tree/projection.ts +++ b/packages/map-views/src/core/layouts/tree/projection.ts @@ -1,6 +1,6 @@ import type { SpaceGraph, SpaceNode } from 'structured-context/api'; import type { LayoutConfig } from '../../definition'; -import { type Connector, childEdges, isHierarchyEdge, occurrenceKey, parentEdges } from '../helpers'; +import { type Connector, childEdges, isHierarchyEdge, occurrenceKey } from '../helpers'; /** One rendered appearance of a node in a tree. */ export type TreeOccurrence = { @@ -44,14 +44,10 @@ export type Tree = { export function buildTree(graph: SpaceGraph, regionId: string, dag: LayoutConfig<'tree'>['dag'] = 'repeat'): Tree { const { hierarchy } = graph; const levelTypes = new Set(hierarchy?.levels.map((level) => level.type)); - const rootType = hierarchy?.levels[0]?.type; const primary = new Map(); // Parent occurrence → child title, resolved to the child's primary occurrence afterwards. const pendingCross: { from: string; child: string; field: string }[] = []; - const hierarchyParents = (title: SpaceNode['id']) => - parentEdges(graph, title).filter((edge) => isHierarchyEdge(edge, hierarchy)); - const visit = (node: SpaceNode, path: readonly string[]): TreeOccurrence => { const nodePath = [...path, node.id]; const key = occurrenceKey(regionId, nodePath); @@ -79,14 +75,12 @@ export function buildTree(graph: SpaceGraph, regionId: string, dag: LayoutConfig return { key, node, children }; }; - const levelNodes = [...graph.nodes.values()].filter((node) => levelTypes.has(node.resolvedType)); - const unparented = levelNodes.filter((node) => hierarchyParents(node.id).length === 0); - const roots = unparented.filter((node) => node.resolvedType === rootType).map((node) => visit(node, [])); - const orphans = unparented.filter((node) => node.resolvedType !== rootType).map((node) => visit(node, [])); + const roots = graph.hierarchyRoots.map((node) => visit(node, [])); + const orphans = graph.orphans.map((node) => visit(node, [])); // Nodes only reachable through a cycle have a parent but no path from a root. Rather than // drop them, enter each such cycle at its first node in space order. - for (const node of levelNodes) { - if (!primary.has(node.id)) orphans.push(visit(node, [])); + for (const node of graph.nodes.values()) { + if (levelTypes.has(node.resolvedType) && !primary.has(node.id)) orphans.push(visit(node, [])); } const crossConnectors = pendingCross.map(({ from, child, field }) => ({ diff --git a/packages/map-views/tests/landscape.test.ts b/packages/map-views/tests/landscape.test.ts index 3783bc8..f4816aa 100644 --- a/packages/map-views/tests/landscape.test.ts +++ b/packages/map-views/tests/landscape.test.ts @@ -1,6 +1,8 @@ import { describe, expect, test } from 'bun:test'; +import { buildSpaceGraph } from 'structured-context/api'; import type { LayoutConfig } from '../src/core/definition'; import { buildLandscape, type LandscapeOccurrence } from '../src/core/layouts/landscape/projection'; +import { makeParentRef, makeSpaceNode } from './builders'; import { fixtureGraph } from './fixture-graph'; import sample from './fixtures/launch-playbook.json'; @@ -81,4 +83,20 @@ describe('sample landscape', () => { ); expect(new Set(occurrences.map((o) => o.key)).size).toBe(occurrences.length); }); + + test('orders type-level containers and their children by numeric order with unordered last', () => { + const orderedGraph = buildSpaceGraph( + [ + makeSpaceNode('P-unordered', 'Phase'), + makeSpaceNode('P-2', 'Phase', { order: 2 }), + makeSpaceNode('P-1', 'Phase', { order: 1 }), + makeSpaceNode('A-2', 'Activity', { order: 20 }, undefined, [makeParentRef('P-1')]), + makeSpaceNode('A-1', 'Activity', { order: 10 }, undefined, [makeParentRef('P-1')]), + ], + undefined, + ); + const { groups } = plainLandscape(orderedGraph, 'main', layout([level('Phase'), level('Activity')])); + expect(groups.map(title)).toEqual(['P-1', 'P-2', 'P-unordered']); + expect(groups[0]!.children.map(title)).toEqual(['A-1', 'A-2']); + }); }); diff --git a/packages/map-views/tests/tree.test.ts b/packages/map-views/tests/tree.test.ts index 3413626..c6a08c5 100644 --- a/packages/map-views/tests/tree.test.ts +++ b/packages/map-views/tests/tree.test.ts @@ -14,9 +14,9 @@ describe('sample tree', () => { const tree = buildTree(graph, 'main'); const all = occurrences([...tree.roots, ...tree.orphans]); - test('roots are the Phases', () => { - const phases = [...graph.nodes.values()].filter((n) => n.resolvedType === 'Phases'); - expect(tree.roots.map((o) => o.node.id)).toEqual(phases.map((n) => n.id)); + test('roots are the Phases, in graph order', () => { + expect(tree.roots.map((o) => o.node.id)).toEqual(graph.hierarchyRoots.map((n) => n.id)); + expect(tree.roots.every((o) => o.node.resolvedType === 'Phases')).toBe(true); }); test('occurrence keys are unique even though nodes repeat', () => { @@ -106,4 +106,21 @@ describe('edge cases', () => { const tree = buildTree(graph(nodes), 'r'); expect(tree.roots[0]!.children.map((o) => o.node.title)).toEqual(['O']); }); + + test('roots, orphans, and children follow SpaceGraph numeric order', () => { + const ref = (parent: string) => makeParentRef(parent, { kind: 'hierarchy', hierarchy: hierarchy.name }); + const nodes = [ + makeSpaceNode('G-unordered', 'goal'), + makeSpaceNode('G-second', 'goal', { order: 2 }), + makeSpaceNode('G-first', 'goal', { order: 1 }), + makeSpaceNode('C-late', 'opportunity', { order: 20 }, undefined, [ref('G-first')]), + makeSpaceNode('C-early', 'opportunity', { order: 5 }, undefined, [ref('G-first')]), + makeSpaceNode('O-late', 'opportunity', { order: 2 }), + makeSpaceNode('O-early', 'opportunity', { order: 1 }), + ]; + const tree = buildTree(graph(nodes), 'r'); + expect(tree.roots.map((o) => o.node.title)).toEqual(['G-first', 'G-second', 'G-unordered']); + expect(tree.roots[0]!.children.map((o) => o.node.title)).toEqual(['C-early', 'C-late']); + expect(tree.orphans.map((o) => o.node.title)).toEqual(['O-early', 'O-late']); + }); }); diff --git a/packages/sctx-map-views/package.json b/packages/sctx-map-views/package.json index 7811c3d..f1dad81 100644 --- a/packages/sctx-map-views/package.json +++ b/packages/sctx-map-views/package.json @@ -33,7 +33,7 @@ "bundle": "bun run clean && bun scripts/build.ts && tsc -p tsconfig.build.json" }, "devDependencies": { - "structured-context": "^0.26.0", + "structured-context": "^0.28.1", "structured-context-map-views": "workspace:*" } }