@0001-labs/idv1 1.0.5 → 1.0.6
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/README.md +1 -1
- package/package.json +1 -1
- package/src/util/explorer.ts +48 -12
package/README.md
CHANGED
|
@@ -36,7 +36,7 @@ The package ships TypeScript source. Bundlers and `tsx` read it as is; for a pla
|
|
|
36
36
|
|
|
37
37
|
## Explorer utility
|
|
38
38
|
|
|
39
|
-
Import `createExplorer` and `mountExplorer` from `@0001-labs/idv1/explorer`, and `@0001-labs/idv1/explorer.css` for the browser view. Supply `ExplorerData`: namespace names, shapes, taxonomies, identifiers, nodes and their links. The utility owns drill-down, counts, identifier resolution, taxonomy highlighting, keyboard navigation and URL history. It contains no application records or network requests.
|
|
39
|
+
Import `createExplorer` and `mountExplorer` from `@0001-labs/idv1/explorer`, and `@0001-labs/idv1/explorer.css` for the browser view. Supply `ExplorerData`: namespace names, shapes, taxonomies, identifiers, nodes and their links. The utility owns drill-down, counts, identifier resolution, taxonomy highlighting, keyboard navigation and URL history. Optional `collections` carry existing source records and lazy `load` functions; records may link to their host’s details with `href`. It contains no application records or network requests.
|
|
40
40
|
|
|
41
41
|
`createExplorer(data)` returns `entries(path)`, `title(path)` and `stepName(path, depth)` for any renderer. `mountExplorer(element, data)` renders that model and returns a cleanup function. Theme it with the `--idv1-explorer-*` CSS variables; its host owns loading and authorization.
|
|
42
42
|
|
package/package.json
CHANGED
package/src/util/explorer.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export type ExplorerRecord = { key: string; id?: string; label: string; kind?: string; href?: string; children?: ExplorerRecord[]; load?: () => Promise<ExplorerRecord[]> };
|
|
1
2
|
export type ExplorerNode = { qualifiedId: string; namespace: string; shape: string; label: string; group: string; at?: number };
|
|
2
3
|
export type ExplorerLink = { subject: string; relation: string; object: string };
|
|
3
4
|
export type ExplorerTaxonomy = { tag: string; name: string; shape: string; pattern: string };
|
|
@@ -12,11 +13,12 @@ export type ExplorerData = {
|
|
|
12
13
|
taxonomy: ExplorerTaxonomy[];
|
|
13
14
|
shapes: ExplorerShape[];
|
|
14
15
|
identifiers: ExplorerIdentifier[];
|
|
16
|
+
collections?: ExplorerRecord[];
|
|
15
17
|
};
|
|
16
18
|
type Node = ExplorerNode;
|
|
17
19
|
type Taxonomy = ExplorerTaxonomy;
|
|
18
20
|
type Identifier = ExplorerIdentifier;
|
|
19
|
-
export type ExplorerEntry = { path: string[]; id: string; label: string; kind: string; count: number };
|
|
21
|
+
export type ExplorerEntry = { path: string[]; id: string; label: string; kind: string; count: number; href?: string };
|
|
20
22
|
type Entry = ExplorerEntry;
|
|
21
23
|
|
|
22
24
|
export function createExplorer(data: ExplorerData) {
|
|
@@ -64,6 +66,23 @@ export function createExplorer(data: ExplorerData) {
|
|
|
64
66
|
path: [...path, node.qualifiedId], id: idIn(node.qualifiedId, scope(path)), label: node.label, kind: kindOf(node), count: children.get(node.qualifiedId)?.length ?? 0,
|
|
65
67
|
}));
|
|
66
68
|
|
|
69
|
+
const record = (path: string[]) => {
|
|
70
|
+
let records = data.collections ?? [];
|
|
71
|
+
let found: ExplorerRecord | undefined;
|
|
72
|
+
for (const step of path) {
|
|
73
|
+
found = records.find(entry => `rc:${encodeURIComponent(entry.key)}` === step);
|
|
74
|
+
if (!found) return undefined;
|
|
75
|
+
records = found.children ?? [];
|
|
76
|
+
}
|
|
77
|
+
return found;
|
|
78
|
+
};
|
|
79
|
+
const recordEntries = (path: string[], records: ExplorerRecord[]) => records.map(entry => ({ path: [...path, `rc:${encodeURIComponent(entry.key)}`], id: entry.id ?? '', label: entry.label, kind: entry.kind ?? '', count: entry.children?.length ?? 0, href: entry.href }));
|
|
80
|
+
const load = async (path: string[]) => {
|
|
81
|
+
for (let depth = 1; depth <= path.length; depth++) {
|
|
82
|
+
const entry = record(path.slice(0, depth));
|
|
83
|
+
if (entry?.load && !entry.children) entry.children = await entry.load();
|
|
84
|
+
}
|
|
85
|
+
};
|
|
67
86
|
function entries(path: string[]): Entry[] {
|
|
68
87
|
const at = path.at(-1);
|
|
69
88
|
if (!at) return [
|
|
@@ -71,7 +90,9 @@ export function createExplorer(data: ExplorerData) {
|
|
|
71
90
|
{ path: ['namespaces'], id: '', label: 'Namespaces', kind: '', count: namespaces().length },
|
|
72
91
|
{ path: ['taxonomies'], id: '', label: 'Taxonomies', kind: '', count: taxonomy.length },
|
|
73
92
|
{ path: ['identifiers'], id: '', label: 'Identifiers', kind: '', count: identifiers.length },
|
|
93
|
+
...recordEntries([], data.collections ?? []),
|
|
74
94
|
];
|
|
95
|
+
if (at.startsWith('rc:')) return recordEntries(path, record(path)?.children ?? []);
|
|
75
96
|
if (at === 'namespaces') return namespaces().map(namespace => ({
|
|
76
97
|
path: [namespace], id: namespace, label: owner(namespace)?.name ?? namespace, kind: 'Namespace', count: topLevel(namespace).length,
|
|
77
98
|
}));
|
|
@@ -89,11 +110,12 @@ export function createExplorer(data: ExplorerData) {
|
|
|
89
110
|
return rowsOf(path, (children.get(at) ?? []).flatMap(id => nodes.get(id) ?? []));
|
|
90
111
|
}
|
|
91
112
|
|
|
92
|
-
const stepName = (path: string[], depth: number) => { const step = path[depth - 1]; return lists[step] ?? (step.startsWith('sh:') ? shapeName(step.slice(3)) : step.startsWith('tx:') ? step.slice(3) : step.startsWith('id:') ? idIn(step.slice(3), scope(path.slice(0, depth - 1))) : idIn(step, scope(path.slice(0, depth - 1)))); };
|
|
113
|
+
const stepName = (path: string[], depth: number) => { const step = path[depth - 1]; return record(path.slice(0, depth))?.label ?? lists[step] ?? (step.startsWith('sh:') ? shapeName(step.slice(3)) : step.startsWith('tx:') ? step.slice(3) : step.startsWith('id:') ? idIn(step.slice(3), scope(path.slice(0, depth - 1))) : idIn(step, scope(path.slice(0, depth - 1)))); };
|
|
93
114
|
function title(path: string[]) {
|
|
94
115
|
const at = path.at(-1);
|
|
95
116
|
if (!at) return data.name;
|
|
96
117
|
if (lists[at]) return lists[at];
|
|
118
|
+
if (at.startsWith('rc:')) return record(path)?.label ?? '';
|
|
97
119
|
if (at.startsWith('sh:')) return `${shapeName(at.slice(3))} · Shape`;
|
|
98
120
|
if (at.startsWith('tx:')) return `${taxonomy.find(entry => entry.tag === at.slice(3))?.name ?? at.slice(3)} · Taxonomy`;
|
|
99
121
|
if (at.startsWith('id:')) return `${identifiers.find(entry => identifierKey(entry) === at.slice(3))?.name ?? at.slice(3)} · Identifier`;
|
|
@@ -101,14 +123,14 @@ export function createExplorer(data: ExplorerData) {
|
|
|
101
123
|
const node = nodes.get(at);
|
|
102
124
|
return node ? `${node.label} · ${kindOf(node)}` : '';
|
|
103
125
|
}
|
|
104
|
-
return { entries, title, stepName };
|
|
126
|
+
return { entries, title, stepName, load };
|
|
105
127
|
}
|
|
106
128
|
|
|
107
129
|
export function mountExplorer(surface: HTMLElement, data: ExplorerData) {
|
|
108
130
|
const document = surface.ownerDocument;
|
|
109
131
|
const window = document.defaultView!;
|
|
110
132
|
const abort = new AbortController();
|
|
111
|
-
const { entries, title, stepName } = createExplorer(data);
|
|
133
|
+
const { entries, title, stepName, load } = createExplorer(data);
|
|
112
134
|
const taxonomy = data.taxonomy;
|
|
113
135
|
surface.classList.add('explorer');
|
|
114
136
|
const pathLine = document.createElement('nav');
|
|
@@ -118,13 +140,13 @@ export function mountExplorer(surface: HTMLElement, data: ExplorerData) {
|
|
|
118
140
|
rows.className = 'explorer-rows';
|
|
119
141
|
surface.replaceChildren(pathLine, rows);
|
|
120
142
|
const readPath = () => (new URLSearchParams(window.location.search).get('path') ?? '').split(',').filter(Boolean);
|
|
121
|
-
const href = (path: string[]) => path.length
|
|
143
|
+
const href = (path: string[]) => { const url = new URL(window.location.href); if (path.length) url.searchParams.set('path', path.join(',')); else url.searchParams.delete('path'); return url.pathname + url.search + url.hash; };
|
|
122
144
|
|
|
123
|
-
function link(path: string[], className: string, parts: [string, string][]) {
|
|
145
|
+
function link(path: string[], className: string, parts: [string, string][], target?: string) {
|
|
124
146
|
const anchor = document.createElement('a');
|
|
125
147
|
anchor.className = className;
|
|
126
|
-
anchor.href = href(path);
|
|
127
|
-
anchor.dataset.path = path.join(',');
|
|
148
|
+
anchor.href = target ?? href(path);
|
|
149
|
+
if (!target) anchor.dataset.path = path.join(',');
|
|
128
150
|
for (const [text, part] of parts) {
|
|
129
151
|
const span = document.createElement('span');
|
|
130
152
|
span.className = part;
|
|
@@ -155,7 +177,7 @@ export function mountExplorer(surface: HTMLElement, data: ExplorerData) {
|
|
|
155
177
|
[entry.label, ''],
|
|
156
178
|
[entry.kind, 'explorer-dim'],
|
|
157
179
|
[entry.count ? `${String(entry.count).padStart(3)}/` : ' ', 'explorer-mono explorer-dim'],
|
|
158
|
-
])));
|
|
180
|
+
], entry.href)));
|
|
159
181
|
if (!list.length) {
|
|
160
182
|
const empty = document.createElement('p');
|
|
161
183
|
empty.className = 'explorer-row explorer-dim';
|
|
@@ -165,9 +187,23 @@ export function mountExplorer(surface: HTMLElement, data: ExplorerData) {
|
|
|
165
187
|
if (focusFirst) (rows.querySelector<HTMLElement>('a') ?? pathLine.lastElementChild as HTMLElement)?.focus();
|
|
166
188
|
}
|
|
167
189
|
|
|
190
|
+
let pending = 0;
|
|
191
|
+
async function show(focus = false) {
|
|
192
|
+
const current = ++pending;
|
|
193
|
+
const path = readPath();
|
|
194
|
+
surface.setAttribute('aria-busy', 'true');
|
|
195
|
+
try {
|
|
196
|
+
await load(path);
|
|
197
|
+
if (current === pending && !abort.signal.aborted) render(focus);
|
|
198
|
+
} catch {
|
|
199
|
+
if (current === pending && !abort.signal.aborted) { render(); rows.textContent = 'Could not load these records. Open this list again to retry.'; }
|
|
200
|
+
} finally {
|
|
201
|
+
if (current === pending) surface.removeAttribute('aria-busy');
|
|
202
|
+
}
|
|
203
|
+
}
|
|
168
204
|
function go(path: string[]) {
|
|
169
205
|
window.history.pushState(null, '', href(path));
|
|
170
|
-
|
|
206
|
+
void show(true);
|
|
171
207
|
}
|
|
172
208
|
|
|
173
209
|
surface.addEventListener('click', event => {
|
|
@@ -190,8 +226,8 @@ export function mountExplorer(surface: HTMLElement, data: ExplorerData) {
|
|
|
190
226
|
go(readPath().slice(0, -1));
|
|
191
227
|
}
|
|
192
228
|
}, { signal: abort.signal });
|
|
193
|
-
window.addEventListener('popstate', () =>
|
|
229
|
+
window.addEventListener('popstate', () => { void show(true); }, { signal: abort.signal });
|
|
194
230
|
|
|
195
|
-
|
|
231
|
+
void show();
|
|
196
232
|
return () => { abort.abort(); surface.replaceChildren(); };
|
|
197
233
|
}
|