@navaramap/material-schema 0.0.0-stage → 0.3.2-nightly.0.setup

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/src/index.ts ADDED
@@ -0,0 +1,90 @@
1
+ // Read from package.json, not a build-time define: bundlers that compile
2
+ // this package from `src` (the workspace examples) know no such define.
3
+ // scripts/set-version.mjs stamps the version before a release build.
4
+ import { version } from "../package.json";
5
+
6
+ import schemaJson from "./materialSchema.json";
7
+
8
+ /** The value type of a field, which decides how a UI edits it. */
9
+ export type FieldKind =
10
+ | "boolean"
11
+ | "number"
12
+ /** A `Color`; a hex number such as `0xff0000` in JSON. */
13
+ | "color"
14
+ /** One of `options`. */
15
+ | "enum"
16
+ /** A list of `options`. */
17
+ | "enumList"
18
+ | "string"
19
+ /** `{ x, y }`. */
20
+ | "point"
21
+ /** Any other list or structure, as JSON. */
22
+ | "list";
23
+
24
+ /**
25
+ * When a change of the field takes effect:
26
+ * - `"live"`: as soon as `layer.update()` sets it.
27
+ * - `"reload"`: the engine reads it only when it builds the layer, its tiles
28
+ * or its meshes. What is already drawn changes after the layer is removed
29
+ * and added again; MVT and tiled layers also use it for tiles loaded later.
30
+ * - `"never"`: the engine does not read it on a layer.
31
+ */
32
+ export type FieldApplies = "live" | "reload" | "never";
33
+
34
+ /** One field of a layer material. */
35
+ export type MaterialField = {
36
+ /** The JS-facing name. */
37
+ name: string;
38
+ kind: FieldKind;
39
+ /** The field's doc comment from the engine source. */
40
+ doc?: string;
41
+ /**
42
+ * The engine's value when the field is not set, as JSON; absent when the
43
+ * engine derives it.
44
+ */
45
+ default?: unknown;
46
+ /** Accepted strings for `enum` and `enumList` fields. */
47
+ options?: string[];
48
+ applies: FieldApplies;
49
+ /** More on when the field applies. */
50
+ note?: string;
51
+ };
52
+
53
+ /**
54
+ * Every layer material the engine accepts, generated from its Rust sources
55
+ * by `scripts/generate-material-schema.mjs`. Plain JSON.
56
+ */
57
+ export type MaterialSchema = {
58
+ /**
59
+ * The Navara version this schema describes. Navara packages are released
60
+ * together, so it is also the `@navaramap/three` version it matches.
61
+ */
62
+ version: string;
63
+ /** Layer `type` → the material keys its description carries. */
64
+ layers: Record<string, string[]>;
65
+ /** Material key → its fields, in source order. */
66
+ materials: Record<string, MaterialField[]>;
67
+ };
68
+
69
+ /** Freezes `value` and everything in it. */
70
+ function deepFreeze<T>(value: T): T {
71
+ if (typeof value === "object" && value !== null) {
72
+ for (const v of Object.values(value)) deepFreeze(v);
73
+ Object.freeze(value);
74
+ }
75
+ return value;
76
+ }
77
+
78
+ /** Frozen: shared by every caller, so it cannot be changed in place. */
79
+ export const MATERIAL_SCHEMA: MaterialSchema = deepFreeze({
80
+ version,
81
+ ...(schemaJson as Omit<MaterialSchema, "version">),
82
+ });
83
+
84
+ /** The field `name` of material `key`. */
85
+ export function materialField(
86
+ key: string,
87
+ name: string,
88
+ ): MaterialField | undefined {
89
+ return MATERIAL_SCHEMA.materials[key]?.find((f) => f.name === name);
90
+ }