A folder with main.archify at its top is a model, a set of diagrams that belong together. Opening main.archify shows the model page: the model's name and description, its own overview diagram when the file has one, the diagrams grouped by folder (click to open), and the registry with where each element is used. The page is built by traversing the folder tree and refreshes when files change. *.archify-registry files define shared elements once; diagrams draw them with `ref: <id>` plus position and may override label, sublabel and tag. References are resolved before Archify runs, so Archify is unchanged. Hover, go-to-definition and completion on ref; unknown names, forbidden overrides, duplicate ids and broken registries in Problems; registry edits re-render open diagrams. `diagram:` links on elements and components open the linked diagram on double-click. New command Archify | Open Model, a Model template for New Diagram…, editor schemas for the new keys and for registries, docs/model.md and examples/online-shop. Single diagrams behave exactly as before.
38 lines
1.7 KiB
TypeScript
38 lines
1.7 KiB
TypeScript
import * as vscode from 'vscode';
|
|
import { DiagramSource, readDiagramSource } from '../sources/detect';
|
|
import { ModelProblem, Registry, resolveRefs } from './format';
|
|
import { ModelService } from './service';
|
|
|
|
/**
|
|
* A diagram ready for Archify: the source as written, plus the resolved JSON
|
|
* with registry references expanded, the model problems found on the way,
|
|
* and the diagram links the viewer should offer.
|
|
*/
|
|
export interface PreparedDiagram {
|
|
source: DiagramSource;
|
|
/** Model root when the file is inside a model. */
|
|
root?: vscode.Uri;
|
|
registry?: Registry;
|
|
problems: ModelProblem[];
|
|
/** Node id → diagram path relative to the model root. */
|
|
links: Map<string, string>;
|
|
}
|
|
|
|
/**
|
|
* Reads a diagram and resolves it against its model, if any. Every place
|
|
* that hands a diagram to Archify (preview, Problems, render, browser) goes
|
|
* through here, so they all see the same resolved diagram.
|
|
*/
|
|
export async function prepareDiagram(models: ModelService, uri: vscode.Uri, text: string): Promise<PreparedDiagram | undefined> {
|
|
const source = readDiagramSource(text, uri.path);
|
|
if (!source) return undefined;
|
|
const found = await models.registryFor(uri);
|
|
const resolved = resolveRefs(source.diagram, { registry: found?.registry, fileName: uri.path });
|
|
// Only diagrams that use refs (or carry a model block) need the resolved form.
|
|
const changed = resolved.refs.size > 0 || resolved.problems.length > 0 || 'model' in source.diagram || resolved.links.size > 0;
|
|
const prepared: DiagramSource = changed
|
|
? { ...source, diagram: resolved.diagram, json: JSON.stringify(resolved.diagram, null, 2) }
|
|
: source;
|
|
return { source: prepared, root: found?.root, registry: found?.registry, problems: resolved.problems, links: resolved.links };
|
|
}
|