@niscorp/loom 0.5.0 → 0.6.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.
Files changed (79) hide show
  1. package/CHANGELOG.md +104 -0
  2. package/DESIGN.md +48 -10
  3. package/README.md +63 -11
  4. package/dist/{chunk-HHPSU7IA.js → chunk-3UI7CG24.js} +3 -4
  5. package/dist/chunk-3UI7CG24.js.map +1 -0
  6. package/dist/{chunk-LVDY6FUH.js → chunk-BYOKCNIJ.js} +3 -3
  7. package/dist/{chunk-LVDY6FUH.js.map → chunk-BYOKCNIJ.js.map} +1 -1
  8. package/dist/chunk-GXA57JFR.js +409 -0
  9. package/dist/chunk-GXA57JFR.js.map +1 -0
  10. package/dist/chunk-J5SYOIR2.js +100 -0
  11. package/dist/chunk-J5SYOIR2.js.map +1 -0
  12. package/dist/{chunk-VMOYMYMV.js → chunk-LAQIW27Q.js} +3 -3
  13. package/dist/chunk-LAQIW27Q.js.map +1 -0
  14. package/dist/{chunk-IQMWPPHW.js → chunk-OVTQEAV3.js} +290 -23
  15. package/dist/chunk-OVTQEAV3.js.map +1 -0
  16. package/dist/index.cjs +74 -32
  17. package/dist/index.cjs.map +1 -1
  18. package/dist/index.d.cts +9 -9
  19. package/dist/index.d.ts +9 -9
  20. package/dist/index.js +1 -4
  21. package/dist/loom-editor-DlNYrfT9.d.ts +15 -0
  22. package/dist/loom-editor-DtGAf8eH.d.cts +15 -0
  23. package/dist/plugins/nova/index.cjs +6 -3
  24. package/dist/plugins/nova/index.cjs.map +1 -1
  25. package/dist/plugins/nova/index.d.cts +1 -1
  26. package/dist/plugins/nova/index.d.ts +1 -1
  27. package/dist/plugins/nova/index.js +2 -3
  28. package/dist/plugins/nova/react/index.cjs +21 -7
  29. package/dist/plugins/nova/react/index.cjs.map +1 -1
  30. package/dist/plugins/nova/react/index.d.cts +2 -2
  31. package/dist/plugins/nova/react/index.d.ts +2 -2
  32. package/dist/plugins/nova/react/index.js +5 -5
  33. package/dist/plugins/nova/react/index.js.map +1 -1
  34. package/dist/plugins/prism/index.cjs +2 -0
  35. package/dist/plugins/prism/index.cjs.map +1 -1
  36. package/dist/plugins/prism/index.d.cts +1 -1
  37. package/dist/plugins/prism/index.d.ts +1 -1
  38. package/dist/plugins/prism/index.js +2 -2
  39. package/dist/plugins/prism/react/index.cjs +200 -189
  40. package/dist/plugins/prism/react/index.cjs.map +1 -1
  41. package/dist/plugins/prism/react/index.d.cts +3 -3
  42. package/dist/plugins/prism/react/index.d.ts +3 -3
  43. package/dist/plugins/prism/react/index.js +48 -108
  44. package/dist/plugins/prism/react/index.js.map +1 -1
  45. package/dist/plugins/vex/index.cjs +2 -0
  46. package/dist/plugins/vex/index.cjs.map +1 -1
  47. package/dist/plugins/vex/index.d.cts +1 -1
  48. package/dist/plugins/vex/index.d.ts +1 -1
  49. package/dist/plugins/vex/index.js +2 -2
  50. package/dist/plugins/vex/react/index.cjs +154 -96
  51. package/dist/plugins/vex/react/index.cjs.map +1 -1
  52. package/dist/plugins/vex/react/index.d.cts +3 -3
  53. package/dist/plugins/vex/react/index.d.ts +3 -3
  54. package/dist/plugins/vex/react/index.js +42 -29
  55. package/dist/plugins/vex/react/index.js.map +1 -1
  56. package/dist/react/index.cjs +153 -92
  57. package/dist/react/index.cjs.map +1 -1
  58. package/dist/react/index.d.cts +7 -3
  59. package/dist/react/index.d.ts +7 -3
  60. package/dist/react/index.js +3 -118
  61. package/dist/react/index.js.map +1 -1
  62. package/dist/{types-V5kJWZJn.d.ts → types-BWIIThEy.d.cts} +2 -0
  63. package/dist/{types-V5kJWZJn.d.cts → types-BWIIThEy.d.ts} +2 -0
  64. package/package.json +7 -7
  65. package/dist/chunk-27OMOR7I.js +0 -17
  66. package/dist/chunk-27OMOR7I.js.map +0 -1
  67. package/dist/chunk-67D727GH.js +0 -47
  68. package/dist/chunk-67D727GH.js.map +0 -1
  69. package/dist/chunk-BU5TULRP.js +0 -132
  70. package/dist/chunk-BU5TULRP.js.map +0 -1
  71. package/dist/chunk-DEZGKUTO.js +0 -170
  72. package/dist/chunk-DEZGKUTO.js.map +0 -1
  73. package/dist/chunk-HHPSU7IA.js.map +0 -1
  74. package/dist/chunk-IQMWPPHW.js.map +0 -1
  75. package/dist/chunk-OFKHIREC.js +0 -212
  76. package/dist/chunk-OFKHIREC.js.map +0 -1
  77. package/dist/chunk-VMOYMYMV.js.map +0 -1
  78. package/dist/loom-editor-BthwIGIZ.d.cts +0 -24
  79. package/dist/loom-editor-CHtjjiBp.d.ts +0 -24
@@ -25,24 +25,41 @@ var isInteger = (schema) => {
25
25
  return (def.checks ?? []).some((check) => check.def?.format === INTEGER_FORMAT);
26
26
  };
27
27
  var enumOptions = (schema) => Object.values(defOf(schema).entries ?? {}).map((value) => ({ value, label: String(value) }));
28
+ var acceptsAbsence = (schema) => {
29
+ try {
30
+ return schema.safeParse(void 0).success;
31
+ } catch {
32
+ return false;
33
+ }
34
+ };
28
35
  var objectFields = (schema, walk) => Object.entries(defOf(schema).shape ?? {}).map(([key, value]) => ({
29
36
  key,
30
- required: !value.safeParse(void 0).success,
37
+ required: !acceptsAbsence(value),
31
38
  field: parseField(value, walk)
32
39
  }));
33
- var branchKeys = (branch) => Object.keys(defOf(branch).shape ?? {});
40
+ var shapeOf = (branch) => defOf(unwrap(branch).core).shape ?? {};
41
+ var branchKeys = (branch) => Object.keys(shapeOf(branch));
34
42
  var JSON_TYPES = /* @__PURE__ */ new Set(["string", "number", "boolean", "object", "array", "null"]);
35
43
  var jsonType = (branch) => {
36
- const type = defOf(branch).type;
44
+ const type = defOf(unwrap(branch).core).type;
37
45
  return JSON_TYPES.has(type) ? type : void 0;
38
46
  };
47
+ var tagPattern = (key, discriminator) => {
48
+ const def = discriminator === void 0 ? void 0 : defOf(discriminator);
49
+ const values = def === void 0 ? [] : def.values !== void 0 ? [...def.values] : Object.values(def.entries ?? {});
50
+ return values.length > 1 ? { kind: "tag", key, value: values[0], values } : { kind: "tag", key, value: values[0] };
51
+ };
39
52
  var distinguish = (branch, siblings, tag) => {
53
+ const shape = shapeOf(branch);
40
54
  if (tag !== void 0) {
41
- const literal2 = (defOf(branch).shape ?? {})[tag];
42
- return { kind: "tag", key: tag, value: literal2 !== void 0 ? defOf(literal2).values?.[0] : void 0 };
55
+ return tagPattern(tag, shape[tag]);
43
56
  }
44
57
  const elsewhere = new Set(siblings.flatMap(branchKeys));
45
- const key = branchKeys(branch).find((k) => !elsewhere.has(k));
58
+ const own = Object.keys(shape).filter((k) => !elsewhere.has(k));
59
+ const key = own.find((k) => {
60
+ const schema = shape[k];
61
+ return schema !== void 0 && !acceptsAbsence(schema);
62
+ }) ?? own[0];
46
63
  if (key !== void 0) return { kind: "key", key };
47
64
  const type = jsonType(branch);
48
65
  if (type !== void 0 && siblings.every((s) => jsonType(s) !== type)) return { kind: "type", type };
@@ -64,6 +81,10 @@ var parseByType = {
64
81
  number: (schema) => isInteger(schema) ? { kind: "number", integer: true } : { kind: "number" },
65
82
  boolean: () => ({ kind: "boolean" }),
66
83
  enum: (schema) => ({ kind: "enum", options: enumOptions(schema) }),
84
+ literal: (schema) => {
85
+ const values = defOf(schema).values ?? [];
86
+ return { kind: "enum", options: values.map((value) => ({ value, label: String(value) })), default: values[0] };
87
+ },
67
88
  object: (schema, walk) => {
68
89
  const def = defOf(schema);
69
90
  walk.stack.add(def);
@@ -108,15 +129,17 @@ var resolveLazy = (lazy) => {
108
129
  var unwrap = (schema) => {
109
130
  let core = schema;
110
131
  let fallback;
132
+ let nullable = false;
111
133
  let def = defOf(core);
112
134
  while (WRAPPERS.has(def.type) || def.type === "lazy") {
135
+ if (def.type === "nullable") nullable = true;
113
136
  const inner = def.type === "lazy" ? resolveLazy(core) : def.innerType;
114
137
  if (inner === void 0) break;
115
- if ("defaultValue" in def) fallback = def.defaultValue;
138
+ if ("defaultValue" in def && fallback === void 0) fallback = def.defaultValue;
116
139
  core = inner;
117
140
  def = defOf(core);
118
141
  }
119
- return fallback !== void 0 ? { core, default: fallback } : { core };
142
+ return { core, ...fallback !== void 0 ? { default: fallback } : {}, ...nullable ? { nullable: true } : {} };
120
143
  };
121
144
  var readMeta = (schema) => {
122
145
  const meta = {};
@@ -126,14 +149,14 @@ var readMeta = (schema) => {
126
149
  return meta;
127
150
  };
128
151
  var parseField = (schema, walk) => {
129
- const { core, default: fallback } = unwrap(schema);
152
+ const { core, default: fallback, nullable } = unwrap(schema);
130
153
  if (walk.stack.has(defOf(core))) {
131
154
  walk.targets.add(defOf(core));
132
155
  return { kind: "self" };
133
156
  }
134
157
  const body = (parseByType[defOf(core).type] ?? unknownField)(core, walk);
135
158
  const meta = { ...readMeta(core), ...readMeta(schema) };
136
- return { ...body, ...meta, ...fallback !== void 0 ? { default: fallback } : {} };
159
+ return { ...body, ...meta, ...fallback !== void 0 ? { default: fallback } : {}, ...nullable === true ? { nullable } : {} };
137
160
  };
138
161
  var parse = (schema) => parseField(schema, { stack: /* @__PURE__ */ new Set(), targets: /* @__PURE__ */ new Set() });
139
162
  var EMPTY = {
@@ -158,10 +181,10 @@ var EMPTY = {
158
181
  }
159
182
  return document;
160
183
  },
161
- union: (field) => {
184
+ union: (field, options) => {
162
185
  if (field.kind !== "union") return {};
163
186
  const first = field.variants[0];
164
- return first === void 0 ? {} : variantDefaults(first);
187
+ return first === void 0 ? {} : variantDefaults(first, options);
165
188
  }
166
189
  };
167
190
  var buildDocument = (field, options) => {
@@ -169,8 +192,8 @@ var buildDocument = (field, options) => {
169
192
  return (options.empty?.[field.kind] ?? EMPTY[field.kind])(field, options);
170
193
  };
171
194
  var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
172
- var variantDefaults = (variant) => {
173
- const defaults = buildDocument(variant.field, {});
195
+ var variantDefaults = (variant, options) => {
196
+ const defaults = buildDocument(variant.field, options);
174
197
  if (variant.pattern.kind !== "tag") return defaults;
175
198
  return { [variant.pattern.key]: variant.pattern.value, ...isRecord(defaults) ? defaults : {} };
176
199
  };
@@ -214,9 +237,11 @@ var wrapField = (loc, prop, ctx) => {
214
237
  const error = errorBinding(loc.expr);
215
238
  return {
216
239
  component: Roles.field,
240
+ ...prop.required ? {} : { model: loc.expr },
217
241
  props: {
218
242
  label: prop.field.title ?? prop.key,
219
243
  required: prop.required,
244
+ ...prop.required ? {} : { optional: true },
220
245
  ...error !== void 0 ? { error } : {},
221
246
  ...prop.field.description !== void 0 ? { description: prop.field.description } : {}
222
247
  },
@@ -226,7 +251,7 @@ var wrapField = (loc, prop, ctx) => {
226
251
  var titleProps = (field) => field.title !== void 0 ? { title: field.title } : {};
227
252
  var withTitle = (node, field) => withProps(node, titleProps(field));
228
253
  var stringBuild = (field, loc) => control(Roles.text, loc.expr, field.format !== void 0 ? { format: field.format } : {});
229
- var numberBuild = (field, loc) => control(Roles.number, loc.expr, field.integer ? { integer: true } : {});
254
+ var numberBuild = (field, loc) => control(Roles.number, loc.expr, { ...field.integer ? { integer: true } : {}, ...field.nullable ? { nullable: true } : {} });
230
255
  var booleanBuild = (_field, loc) => control(Roles.checkbox, loc.expr);
231
256
  var enumBuild = (field, loc) => control(Roles.select, loc.expr, { options: field.options });
232
257
  var unknownBuild = (_field, loc) => control(Roles.raw, loc.expr);
@@ -237,7 +262,9 @@ var buildGroup = (field, loc, ctx) => withTitle(
237
262
  },
238
263
  field
239
264
  );
265
+ var hasDottedKey = (field) => field.fields.some((f) => f.key.includes("."));
240
266
  var objectBuild = (field, loc, ctx) => {
267
+ if (hasDottedKey(field) && loc.expr !== ROOT.expr) return control(Roles.raw, loc.expr);
241
268
  if (field.recursive !== true) return buildGroup(field, loc, ctx);
242
269
  const name = ctx.name();
243
270
  ctx.templates.push({ name, defaultChild: buildDocument(field, ctx.options), containers: [] });
@@ -265,9 +292,9 @@ var childrenKey = (field) => {
265
292
  if (field.kind !== "object") return void 0;
266
293
  return field.fields.find((f) => f.field.kind === "array" && f.field.item.kind === "self")?.key;
267
294
  };
268
- var containersOf = (variants) => variants.flatMap((variant) => {
295
+ var containersOf = (variants, options) => variants.flatMap((variant) => {
269
296
  const key = childrenKey(variant.field);
270
- return key === void 0 ? [] : [{ label: variant.label, pattern: variant.pattern, defaults: variantDefaults(variant), key }];
297
+ return key === void 0 ? [] : [{ label: variant.label, pattern: variant.pattern, defaults: variantDefaults(variant, options), key }];
271
298
  });
272
299
  var arrayBuild = (field, loc, ctx) => {
273
300
  const containers = field.item.kind === "self" ? ctx.templates[ctx.templates.length - 1]?.containers ?? [] : [];
@@ -308,7 +335,7 @@ var buildVariant = (field, loc, ctx) => withProps(
308
335
  field.variants.map((variant) => ({
309
336
  label: variant.label,
310
337
  pattern: variant.pattern,
311
- defaults: variantDefaults(variant),
338
+ defaults: variantDefaults(variant, ctx.options),
312
339
  ...childrenKey(variant.field) !== void 0 ? { childrenKey: childrenKey(variant.field) } : {}
313
340
  }))
314
341
  ),
@@ -319,7 +346,11 @@ var variantBuild = (field, loc, ctx) => {
319
346
  if (field.recursive !== true) return buildVariant(field, loc, ctx);
320
347
  const name = ctx.name();
321
348
  const first = field.variants[0];
322
- ctx.templates.push({ name, defaultChild: first !== void 0 ? variantDefaults(first) : {}, containers: containersOf(field.variants) });
349
+ ctx.templates.push({
350
+ name,
351
+ defaultChild: first !== void 0 ? variantDefaults(first, ctx.options) : {},
352
+ containers: containersOf(field.variants, ctx.options)
353
+ });
323
354
  ctx.register(name, buildVariant(field, TEMPLATE_ROOT, ctx));
324
355
  const inline = buildVariant(field, loc, ctx);
325
356
  ctx.templates.pop();
@@ -353,6 +384,7 @@ var buildLayout = (field, loc, ctx) => {
353
384
  };
354
385
  var toNova = (ir, options = {}) => {
355
386
  const layouts = {};
387
+ const id = options.id ?? "loom-form";
356
388
  let counter = 0;
357
389
  const matchers = options.widgets ?? [];
358
390
  const ctx = {
@@ -360,7 +392,7 @@ var toNova = (ir, options = {}) => {
360
392
  layouts[name] = node;
361
393
  },
362
394
  templates: [],
363
- name: () => `loom:tree:${counter++}`,
395
+ name: () => `loom:tree:${id}:${counter++}`,
364
396
  widgetRole: (field, path) => matchers.find(
365
397
  (matcher) => matcher.match({ kind: field.kind, path, title: field.title, description: field.description })
366
398
  )?.role,
@@ -374,7 +406,7 @@ var toNova = (ir, options = {}) => {
374
406
  throw new Error("loom.toNova: the root document must be an object (or use rootKey).");
375
407
  }
376
408
  const action = {
377
- id: options.id ?? "loom-form",
409
+ id,
378
410
  data: document,
379
411
  layout
380
412
  };
@@ -382,15 +414,19 @@ var toNova = (ir, options = {}) => {
382
414
  };
383
415
 
384
416
  // src/validate.ts
417
+ var writeOwn = (node, key, value) => {
418
+ Object.defineProperty(node, key, { value, enumerable: true, writable: true, configurable: true });
419
+ return value;
420
+ };
385
421
  var setPath = (root, path, message) => {
386
422
  let node = root;
387
423
  for (let i = 0; i < path.length - 1; i += 1) {
388
424
  const key = String(path[i]);
389
- const next = node[key];
390
- node = typeof next === "object" && next !== null ? next : node[key] = {};
425
+ const next = Object.hasOwn(node, key) ? node[key] : void 0;
426
+ node = typeof next === "object" && next !== null ? next : writeOwn(node, key, {});
391
427
  }
392
428
  const leaf = String(path[path.length - 1]);
393
- if (!(leaf in node)) node[leaf] = message;
429
+ if (!Object.hasOwn(node, leaf)) writeOwn(node, leaf, message);
394
430
  };
395
431
  var errorTree = (issues) => {
396
432
  const tree = {};
@@ -465,11 +501,7 @@ var mountView = (editor, action, read) => {
465
501
  // src/editor/editor.ts
466
502
  var formCanvas = (document) => `form:${document}`;
467
503
  var ROOT_KEY = "$root";
468
- var needsWrap = (document) => parse(document).kind !== "object";
469
- var stripErrors2 = (data) => {
470
- const { [ERROR_NAMESPACE]: _omit, ...document } = data;
471
- return document;
472
- };
504
+ var needsWrap = (ir) => ir.kind !== "object" || ir.fields.some((field) => field.key === ERROR_NAMESPACE || field.key.includes("."));
473
505
  var createLoomEditor = (config) => {
474
506
  const plugins = /* @__PURE__ */ new Map();
475
507
  const listeners = { change: /* @__PURE__ */ new Set(), open: /* @__PURE__ */ new Set() };
@@ -493,7 +525,7 @@ var createLoomEditor = (config) => {
493
525
  const nextProblems = {};
494
526
  for (const [name, instanceId] of Object.entries(formInstances)) {
495
527
  const data = shell.getRuntime(instanceId)?.getData() ?? {};
496
- const document = stripErrors2(data);
528
+ const document = stripErrors(data);
497
529
  const problems = data[ERROR_NAMESPACE] ?? {};
498
530
  if (wrappedDocs.has(name)) {
499
531
  nextDocs[name] = document[ROOT_KEY];
@@ -527,7 +559,7 @@ var createLoomEditor = (config) => {
527
559
  action = { id: canvasId, data: isRecord(value) ? value : {}, layout: document(value) };
528
560
  } else {
529
561
  const ir = parse(document);
530
- const wrap = needsWrap(document);
562
+ const wrap = needsWrap(ir);
531
563
  if (wrap) wrappedDocs.add(name);
532
564
  const seed = artifact.documents?.[name];
533
565
  const value = seed ?? buildDocument(ir, {});
@@ -538,7 +570,7 @@ var createLoomEditor = (config) => {
538
570
  widgets: plugin.widgets ?? []
539
571
  });
540
572
  for (const [id, node] of Object.entries(built.layouts)) store.set(id, node);
541
- const seedsRoot = wrap && seed !== void 0 && seed !== null && !isRecord(seed) && document.safeParse(seed).success;
573
+ const seedsRoot = wrap && seed !== void 0 && seed !== null && !isRecord(seed);
542
574
  action = seedsRoot ? { ...built.action, data: { [ROOT_KEY]: seed } } : built.action;
543
575
  }
544
576
  shell.registerAction(action);
@@ -547,6 +579,14 @@ var createLoomEditor = (config) => {
547
579
  const instanceId = shell.getCanvasState(canvasId).active?.id;
548
580
  if (instanceId !== void 0) formInstances[name] = instanceId;
549
581
  }
582
+ let tick = 0;
583
+ let lastPublished = 0;
584
+ const begun = /* @__PURE__ */ new WeakMap();
585
+ detach.push(
586
+ shell.onDataChange((change) => {
587
+ begun.set(change, tick += 1);
588
+ })
589
+ );
550
590
  for (const [name, instanceId] of Object.entries(formInstances)) {
551
591
  const document = plugin.documents[name];
552
592
  if (typeof document !== "function" && document !== void 0) {
@@ -557,6 +597,8 @@ var createLoomEditor = (config) => {
557
597
  detach.push(
558
598
  shell.onDataChange((change) => {
559
599
  if (!Object.values(formInstances).includes(change.instanceId)) return;
600
+ if (lastPublished > (begun.get(change) ?? 0)) return;
601
+ lastPublished = tick += 1;
560
602
  refresh();
561
603
  emit("change");
562
604
  })
@@ -597,37 +639,58 @@ var createLoomEditor = (config) => {
597
639
  };
598
640
  return editor;
599
641
  };
642
+ var useModelWrite = (model) => {
643
+ const dispatch = react$1.useNovaDispatch();
644
+ return (value) => {
645
+ if (model !== void 0) dispatch({ type: "ui:model", ref: model.ref, payload: value });
646
+ };
647
+ };
648
+
649
+ // src/react/kit/tokens.ts
650
+ var COLORS = {
651
+ border: "var(--loom-border, #d1d5db)",
652
+ borderSubtle: "var(--loom-border-subtle, #e5e7eb)",
653
+ surface: "var(--loom-surface, #fff)",
654
+ surfaceMuted: "var(--loom-surface-muted, #f9fafb)",
655
+ fill: "var(--loom-fill, #f3f4f6)",
656
+ text: "var(--loom-text, #111827)",
657
+ label: "var(--loom-label, #374151)",
658
+ muted: "var(--loom-muted, #6b7280)",
659
+ faint: "var(--loom-faint, #9ca3af)",
660
+ accent: "var(--loom-accent, #1d4ed8)",
661
+ danger: "var(--loom-danger, #dc2626)",
662
+ dangerFill: "var(--loom-danger-fill, #fef2f2)",
663
+ shadow: "var(--loom-shadow, rgba(0,0,0,0.12))"};
600
664
  var groupStyle = { display: "flex", flexDirection: "column" };
601
- var titleStyle = { fontSize: 15, fontWeight: 700, marginBottom: 8, color: "#111827" };
665
+ var titleStyle = { fontSize: 15, fontWeight: 700, marginBottom: 8, color: COLORS.text };
602
666
  var fieldStyle = { display: "flex", flexDirection: "column", gap: 4, marginBottom: 12 };
603
- var labelStyle = { fontSize: 13, fontWeight: 600, color: "#374151" };
604
- var hintStyle = { fontSize: 12, color: "#6b7280" };
667
+ var labelStyle = { fontSize: 13, fontWeight: 600, color: COLORS.label };
668
+ var hintStyle = { fontSize: 12, color: COLORS.muted };
669
+ var removeStyle = { marginLeft: 8, fontSize: 12, border: "none", background: "none", color: COLORS.accent, cursor: "pointer", padding: 0 };
605
670
  var LoomGroup = ({ title, children }) => /* @__PURE__ */ jsxRuntime.jsxs("div", { style: groupStyle, children: [
606
671
  title !== void 0 ? /* @__PURE__ */ jsxRuntime.jsx("div", { style: titleStyle, children: title }) : null,
607
672
  children
608
673
  ] });
609
- var LoomField = ({ label, description, required, error, children }) => /* @__PURE__ */ jsxRuntime.jsxs("div", { style: fieldStyle, children: [
610
- /* @__PURE__ */ jsxRuntime.jsxs("span", { style: labelStyle, children: [
611
- label,
612
- required ? /* @__PURE__ */ jsxRuntime.jsx("span", { style: { color: "#dc2626" }, children: " *" }) : null
613
- ] }),
614
- description !== void 0 ? /* @__PURE__ */ jsxRuntime.jsx("span", { style: hintStyle, children: description }) : null,
615
- children,
616
- typeof error === "string" ? /* @__PURE__ */ jsxRuntime.jsx("span", { style: { ...hintStyle, color: "#dc2626" }, children: error }) : null
617
- ] });
618
- var useModelWrite = (model) => {
619
- const dispatch = react$1.useNovaDispatch();
620
- return (value) => {
621
- if (model !== void 0) dispatch({ type: "ui:model", ref: model.ref, payload: value });
622
- };
674
+ var LoomField = ({ label, description, required, optional, error, value, novaModel, children }) => {
675
+ const set = useModelWrite(novaModel);
676
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { style: fieldStyle, children: [
677
+ /* @__PURE__ */ jsxRuntime.jsxs("span", { style: labelStyle, children: [
678
+ label,
679
+ required ? /* @__PURE__ */ jsxRuntime.jsx("span", { style: { color: COLORS.danger }, children: " *" }) : null,
680
+ optional === true && value !== void 0 ? /* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", style: removeStyle, onClick: () => set(void 0), children: "Remove" }) : null
681
+ ] }),
682
+ description !== void 0 ? /* @__PURE__ */ jsxRuntime.jsx("span", { style: hintStyle, children: description }) : null,
683
+ children,
684
+ typeof error === "string" ? /* @__PURE__ */ jsxRuntime.jsx("span", { style: { ...hintStyle, color: COLORS.danger }, children: error }) : null
685
+ ] });
623
686
  };
624
687
 
625
688
  // src/react/kit/shared.ts
626
- var inputStyle = { padding: "6px 10px", border: "1px solid #d1d5db", borderRadius: 6, fontSize: 14, width: "100%", boxSizing: "border-box" };
689
+ var inputStyle = { padding: "6px 10px", border: `1px solid ${COLORS.border}`, borderRadius: 6, fontSize: 14, width: "100%", boxSizing: "border-box" };
627
690
  var jsType = (value) => Array.isArray(value) ? "array" : value === null ? "null" : typeof value;
628
691
  var decodeLiteral = (value) => typeof value === "string" ? JSON.parse(value) : value;
629
692
  var matches = (value, pattern) => {
630
- if (pattern.kind === "tag") return isRecord(value) && value[pattern.key] === pattern.value;
693
+ if (pattern.kind === "tag") return isRecord(value) && (pattern.values ?? [pattern.value]).includes(value[pattern.key]);
631
694
  if (pattern.kind === "key") return isRecord(value) && pattern.key in value;
632
695
  if (pattern.kind === "type") return jsType(value) === pattern.type;
633
696
  return false;
@@ -677,7 +740,7 @@ var JsonEditor = ({ value, onChange }) => {
677
740
  setValid(false);
678
741
  }
679
742
  },
680
- style: valid ? rawStyle : { ...rawStyle, borderColor: "#dc2626", background: "#fef2f2" }
743
+ style: valid ? rawStyle : { ...rawStyle, borderColor: COLORS.danger, background: COLORS.dangerFill }
681
744
  }
682
745
  );
683
746
  };
@@ -700,16 +763,17 @@ var LoomText = ({
700
763
  }
701
764
  );
702
765
  };
703
- var LoomNumber = ({ value, novaModel }) => {
766
+ var LoomNumber = ({ value, integer, nullable, novaModel }) => {
704
767
  const set = useModelWrite(novaModel);
705
768
  return /* @__PURE__ */ jsxRuntime.jsx(
706
769
  "input",
707
770
  {
708
771
  type: "number",
772
+ step: integer === true ? 1 : "any",
709
773
  value: Number.isFinite(value) ? value : "",
710
774
  onChange: (event) => {
711
775
  const raw = event.target.value;
712
- set(raw ? Number(raw) : null);
776
+ set(raw ? Number(raw) : nullable === true ? null : void 0);
713
777
  },
714
778
  style: inputStyle
715
779
  }
@@ -733,7 +797,11 @@ var LoomSelect = ({ value, options, novaModel }) => {
733
797
  const chosen = (options ?? []).find((option) => String(option.value) === text);
734
798
  set(chosen === void 0 ? text : chosen.value);
735
799
  };
736
- return /* @__PURE__ */ jsxRuntime.jsx("select", { value: String(value ?? ""), onChange: (event) => choose(event.target.value), style: inputStyle, children: (options ?? []).map((option) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: String(option.value), children: option.label }, String(option.value))) });
800
+ const hasValue = (options ?? []).some((option) => option.value === value);
801
+ return /* @__PURE__ */ jsxRuntime.jsxs("select", { value: hasValue ? String(value) : "", onChange: (event) => choose(event.target.value), style: inputStyle, children: [
802
+ hasValue ? null : /* @__PURE__ */ jsxRuntime.jsx("option", { value: "", disabled: true, children: "\u2014" }),
803
+ (options ?? []).map((option) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: String(option.value), children: option.label }, String(option.value)))
804
+ ] });
737
805
  };
738
806
  var LoomRaw = ({ value, novaModel }) => {
739
807
  const set = useModelWrite(novaModel);
@@ -752,7 +820,7 @@ var LoomVariant = ({
752
820
  const options = decodeLiteral(branches) ?? [];
753
821
  const matched = options.findIndex((branch) => matches(value, branch.pattern));
754
822
  const fallback = options.findIndex((branch) => branch.pattern.kind === "fallback");
755
- const active = matched >= 0 ? matched : Math.max(0, fallback);
823
+ const active = matched >= 0 ? matched : fallback;
756
824
  const switchTo = (chosen) => {
757
825
  const activeKey = options[active]?.childrenKey;
758
826
  const oldChildren = activeKey !== void 0 && isRecord(value) ? value[activeKey] : void 0;
@@ -764,30 +832,33 @@ var LoomVariant = ({
764
832
  set(chosen.defaults);
765
833
  };
766
834
  return /* @__PURE__ */ jsxRuntime.jsxs("div", { style: variantStyle, children: [
767
- /* @__PURE__ */ jsxRuntime.jsx(
835
+ /* @__PURE__ */ jsxRuntime.jsxs(
768
836
  "select",
769
837
  {
770
- value: active,
838
+ value: active < 0 ? "" : active,
771
839
  onChange: (event) => {
772
840
  const chosen = options[Number(event.target.value)];
773
841
  if (chosen !== void 0) switchTo(chosen);
774
842
  },
775
843
  style: inputStyle,
776
- children: options.map((branch, index) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: index, children: branch.label }, index))
844
+ children: [
845
+ active < 0 ? /* @__PURE__ */ jsxRuntime.jsx("option", { value: "", disabled: true, children: "\u2014" }) : null,
846
+ options.map((branch, index) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: index, children: branch.label }, index))
847
+ ]
777
848
  }
778
849
  ),
779
- /* @__PURE__ */ jsxRuntime.jsx(ActiveBranch.Provider, { value: active, children })
850
+ active < 0 ? /* @__PURE__ */ jsxRuntime.jsx(JsonEditor, { value, onChange: set }) : /* @__PURE__ */ jsxRuntime.jsx(ActiveBranch.Provider, { value: active, children })
780
851
  ] });
781
852
  };
782
- var trigger = { padding: "4px 8px", border: "1px solid #d1d5db", borderRadius: 6, background: "#fff", cursor: "pointer", fontSize: 13, lineHeight: 1 };
783
- var base = { position: "absolute", zIndex: 30, minWidth: 170, display: "flex", flexDirection: "column", background: "#fff", border: "1px solid #d1d5db", borderRadius: 6, boxShadow: "0 6px 16px rgba(0,0,0,0.12)", padding: 4 };
853
+ var trigger = { padding: "4px 8px", border: `1px solid ${COLORS.border}`, borderRadius: 6, background: COLORS.surface, cursor: "pointer", fontSize: 13, lineHeight: 1 };
854
+ var base = { position: "absolute", zIndex: 30, minWidth: 170, display: "flex", flexDirection: "column", background: COLORS.surface, border: `1px solid ${COLORS.border}`, borderRadius: 6, boxShadow: `0 6px 16px ${COLORS.shadow}`, padding: 4 };
784
855
  var menuRight = { ...base, top: "100%", right: 0, marginTop: 4 };
785
856
  var menuLeft = { ...base, top: "100%", left: 0, marginTop: 4 };
786
857
  var flyLeft = { ...base, top: 0, right: "100%", maxHeight: 280, overflowY: "auto" };
787
858
  var flyRight = { ...base, top: 0, left: "100%", maxHeight: 280, overflowY: "auto" };
788
859
  var itemStyle = { width: "100%", boxSizing: "border-box", textAlign: "left", padding: "6px 10px", cursor: "pointer", fontSize: 13, borderRadius: 4, whiteSpace: "nowrap", display: "flex", justifyContent: "space-between", gap: 12 };
789
- var itemDisabled = { ...itemStyle, color: "#9ca3af", cursor: "default" };
790
- var MENU_CSS = ".loom-menu-item{background:transparent;border:none;}.loom-menu-item:hover:not(:disabled){background:#f3f4f6;}";
860
+ var itemDisabled = { ...itemStyle, color: COLORS.faint, cursor: "default" };
861
+ var MENU_CSS = `.loom-menu-item{background:transparent;border:none;}.loom-menu-item:hover:not(:disabled){background:${COLORS.fill};}`;
791
862
  var MenuContext = react.createContext({ close: () => {
792
863
  }, anchor: "right" });
793
864
  var ActionMenu = ({
@@ -836,10 +907,10 @@ var SubMenu = ({ label, children }) => {
836
907
  open ? /* @__PURE__ */ jsxRuntime.jsx("div", { style: anchor === "right" ? flyLeft : flyRight, children }) : null
837
908
  ] });
838
909
  };
839
- var arrayStyle = { display: "flex", flexDirection: "column", gap: 8, padding: 12, border: "1px dashed #d1d5db", borderRadius: 6 };
910
+ var arrayStyle = { display: "flex", flexDirection: "column", gap: 8, padding: 12, border: `1px dashed ${COLORS.border}`, borderRadius: 6 };
840
911
  var arrayItemStyle = { display: "flex", gap: 6, alignItems: "flex-start" };
841
912
  var itemEditorStyle = { flex: 1, minWidth: 0 };
842
- var addButtonStyle = { ...inputStyle, alignSelf: "flex-start", cursor: "pointer", background: "#f3f4f6" };
913
+ var addButtonStyle = { ...inputStyle, alignSelf: "flex-start", cursor: "pointer", background: COLORS.fill };
843
914
  var LoomArray = ({ children }) => /* @__PURE__ */ jsxRuntime.jsx("div", { style: arrayStyle, children });
844
915
  var LoomArrayItem = ({ children }) => /* @__PURE__ */ jsxRuntime.jsx("div", { style: arrayItemStyle, children });
845
916
  var LoomBox = ({ children }) => /* @__PURE__ */ jsxRuntime.jsx("div", { style: itemEditorStyle, children });
@@ -897,6 +968,13 @@ var LoomRowMenu = ({ index, containers, value, novaModel }) => {
897
968
  /* @__PURE__ */ jsxRuntime.jsx(MenuItem, { label: "\u2715 Remove", onSelect: () => set(list.filter((_, j) => j !== i)) })
898
969
  ] });
899
970
  };
971
+ var panel = { border: `1px solid ${COLORS.borderSubtle}`, borderRadius: 8, overflow: "hidden" };
972
+ var head = { padding: "6px 12px", borderBottom: `1px solid ${COLORS.borderSubtle}`, background: COLORS.surfaceMuted, fontSize: 12, fontWeight: 600, color: COLORS.label };
973
+ var pre = { margin: 0, padding: 12, fontSize: 12, fontFamily: "ui-monospace, Menlo, monospace", whiteSpace: "pre-wrap", wordBreak: "break-word", maxHeight: 320, overflow: "auto" };
974
+ var JsonPane = ({ title, value }) => /* @__PURE__ */ jsxRuntime.jsxs("div", { style: panel, children: [
975
+ title !== void 0 ? /* @__PURE__ */ jsxRuntime.jsx("div", { style: head, children: title }) : null,
976
+ /* @__PURE__ */ jsxRuntime.jsx("pre", { style: pre, children: JSON.stringify(value ?? {}, null, 2) })
977
+ ] });
900
978
 
901
979
  // src/react/kit/index.ts
902
980
  var loomComponents = {
@@ -926,27 +1004,24 @@ var useLoomDocument = () => react.useContext(LoomDocumentContext);
926
1004
  var LoomEditor = ({ plugins, artifact, onChange }) => {
927
1005
  const [api, setApi] = react.useState(null);
928
1006
  const [documents, setDocuments] = react.useState({});
1007
+ const onChangeRef = react.useRef(onChange);
1008
+ react.useEffect(() => {
1009
+ onChangeRef.current = onChange;
1010
+ });
929
1011
  react.useEffect(() => {
930
1012
  const registry = createLoomRegistry();
931
1013
  for (const plugin of plugins) {
932
1014
  for (const [role, component] of Object.entries(plugin.components ?? {})) registry.register(role, component);
933
1015
  }
934
1016
  const editor = createLoomEditor({ registry });
935
- for (const plugin of plugins) {
936
- editor.loadPlugin({
937
- name: plugin.name,
938
- documents: plugin.documents,
939
- ...plugin.widgets ? { widgets: plugin.widgets.map((w) => ({ role: w.role, match: w.match })) } : {},
940
- ...plugin.mount ? { mount: plugin.mount } : {}
941
- });
942
- }
1017
+ for (const plugin of plugins) editor.loadPlugin(plugin);
943
1018
  editor.open(artifact);
944
1019
  setApi(editor);
945
1020
  setDocuments(editor.documents);
946
- onChange?.(editor.documents);
1021
+ onChangeRef.current?.(editor.documents);
947
1022
  const off = editor.on("change", () => {
948
1023
  setDocuments(editor.documents);
949
- onChange?.(editor.documents);
1024
+ onChangeRef.current?.(editor.documents);
950
1025
  });
951
1026
  return () => {
952
1027
  off();
@@ -978,18 +1053,11 @@ var jsonviewerPlugin = () => ({
978
1053
  documents: {},
979
1054
  mount: (editor) => mountView(editor, jsonviewerAction, (e) => ({ value: e.documents }))
980
1055
  });
981
- var panel = { border: "1px solid #e5e7eb", borderRadius: 8, overflow: "hidden" };
982
- var head = { padding: "6px 12px", borderBottom: "1px solid #e5e7eb", background: "#f9fafb", fontSize: 12, fontWeight: 600, color: "#374151" };
983
- var pre = { margin: 0, padding: 12, fontSize: 12, fontFamily: "ui-monospace, Menlo, monospace", whiteSpace: "pre-wrap", wordBreak: "break-word", maxHeight: 320, overflow: "auto" };
984
- var JsonViewer = ({ title, value }) => /* @__PURE__ */ jsxRuntime.jsxs("div", { style: panel, children: [
985
- title !== void 0 ? /* @__PURE__ */ jsxRuntime.jsx("div", { style: head, children: title }) : null,
986
- /* @__PURE__ */ jsxRuntime.jsx("pre", { style: pre, children: JSON.stringify(value ?? {}, null, 2) })
987
- ] });
988
1056
 
989
1057
  // src/plugins/jsonviewer/react/index.ts
990
1058
  var jsonviewer = () => {
991
1059
  const { name, documents, mount } = jsonviewerPlugin();
992
- return { name, documents, mount, components: { [JSONVIEWER]: JsonViewer } };
1060
+ return { name, documents, mount, components: { [JSONVIEWER]: JsonPane } };
993
1061
  };
994
1062
 
995
1063
  // src/plugins/validation/validation.layout.ts
@@ -1012,18 +1080,11 @@ var validationPlugin = () => ({
1012
1080
  documents: {},
1013
1081
  mount: (editor) => mountView(editor, validationAction, (e) => ({ value: e.validations }))
1014
1082
  });
1015
- var panel2 = { border: "1px solid #e5e7eb", borderRadius: 8, overflow: "hidden" };
1016
- var head2 = { padding: "6px 12px", borderBottom: "1px solid #e5e7eb", background: "#f9fafb", fontSize: 12, fontWeight: 600, color: "#374151" };
1017
- var pre2 = { margin: 0, padding: 12, fontSize: 12, fontFamily: "ui-monospace, Menlo, monospace", whiteSpace: "pre-wrap", wordBreak: "break-word", maxHeight: 320, overflow: "auto" };
1018
- var Validation = ({ title, value }) => /* @__PURE__ */ jsxRuntime.jsxs("div", { style: panel2, children: [
1019
- title !== void 0 ? /* @__PURE__ */ jsxRuntime.jsx("div", { style: head2, children: title }) : null,
1020
- /* @__PURE__ */ jsxRuntime.jsx("pre", { style: pre2, children: JSON.stringify(value ?? {}, null, 2) })
1021
- ] });
1022
1083
 
1023
1084
  // src/plugins/validation/react/index.ts
1024
1085
  var validation = () => {
1025
1086
  const { name, documents, mount } = validationPlugin();
1026
- return { name, documents, mount, components: { [VALIDATION]: Validation } };
1087
+ return { name, documents, mount, components: { [VALIDATION]: JsonPane } };
1027
1088
  };
1028
1089
 
1029
1090
  // src/react/editor/defaults.ts