@cisri/json-schema-editor 1.0.0 → 1.0.1

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/dist/index.js CHANGED
@@ -1,425 +1,188 @@
1
- import { jsx as n, jsxs as p, Fragment as L } from "react/jsx-runtime";
2
- import * as h from "react";
3
- import { useState as C, useRef as M, useEffect as F, useMemo as Ne, useCallback as x } from "react";
4
- import { Slot as ve } from "@radix-ui/react-slot";
5
- import { cva as Se } from "class-variance-authority";
6
- import { cn as d } from "@cisri/core";
7
- import * as P from "@radix-ui/react-checkbox";
8
- import { Check as G, ChevronDown as Q, Undo2 as je, Redo2 as Te, Minus as ke, Plus as Ce, Trash2 as Re } from "lucide-react";
9
- import * as f from "@radix-ui/react-select";
10
- const Ie = Se(
11
- "inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50",
12
- {
13
- variants: {
14
- variant: {
15
- default: "bg-primary text-primary-foreground shadow hover:bg-primary/90",
16
- destructive: "bg-destructive text-destructive-foreground shadow-sm hover:bg-destructive/90",
17
- outline: "border border-input bg-background shadow-sm hover:bg-accent hover:text-accent-foreground",
18
- secondary: "bg-secondary text-secondary-foreground shadow-sm hover:bg-secondary/80",
19
- ghost: "hover:bg-accent hover:text-accent-foreground",
20
- link: "text-primary underline-offset-4 hover:underline"
21
- },
22
- size: {
23
- default: "h-9 px-4 py-2",
24
- sm: "h-8 rounded-md px-3 text-xs",
25
- lg: "h-10 rounded-md px-8",
26
- icon: "h-9 w-9"
27
- }
28
- },
29
- defaultVariants: {
30
- variant: "default",
31
- size: "default"
32
- }
33
- }
34
- ), w = h.forwardRef(
35
- ({ className: e, variant: r, size: t, asChild: a = !1, ...o }, c) => /* @__PURE__ */ n(
36
- a ? ve : "button",
37
- {
38
- className: d(Ie({ variant: r, size: t, className: e })),
39
- ref: c,
40
- ...o
41
- }
42
- )
43
- );
44
- w.displayName = "Button";
45
- const W = h.forwardRef(({ className: e, ...r }, t) => /* @__PURE__ */ n(
46
- P.Root,
47
- {
48
- ref: t,
49
- className: d(
50
- "peer h-4 w-4 shrink-0 rounded-sm border border-primary shadow focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground",
51
- e
52
- ),
53
- ...r,
54
- children: /* @__PURE__ */ n(
55
- P.Indicator,
56
- {
57
- className: d("flex items-center justify-center text-current"),
58
- children: /* @__PURE__ */ n(G, { className: "h-4 w-4" })
59
- }
60
- )
61
- }
62
- ));
63
- W.displayName = P.Root.displayName;
64
- const H = h.forwardRef(
65
- ({ className: e, type: r, ...t }, a) => /* @__PURE__ */ n(
66
- "input",
67
- {
68
- type: r,
69
- className: d(
70
- "flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-sm shadow-sm transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50",
71
- e
72
- ),
73
- ref: a,
74
- ...t
75
- }
76
- )
77
- );
78
- H.displayName = "Input";
79
- const Ae = f.Root, Fe = f.Value, X = h.forwardRef(({ className: e, children: r, ...t }, a) => /* @__PURE__ */ p(
80
- f.Trigger,
81
- {
82
- ref: a,
83
- className: d(
84
- "flex h-9 w-full items-center justify-between whitespace-nowrap rounded-md border border-input bg-transparent px-3 py-2 text-sm shadow-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-1 focus:ring-ring disabled:cursor-not-allowed disabled:opacity-50 [&>span>]:line-clamp-1",
85
- e
86
- ),
87
- ...t,
88
- children: [
89
- r,
90
- /* @__PURE__ */ n(f.Icon, { asChild: !0, children: /* @__PURE__ */ n(Q, { className: "h-4 w-4 opacity-50" }) })
91
- ]
92
- }
93
- ));
94
- X.displayName = f.Trigger.displayName;
95
- const Z = h.forwardRef(({ className: e, children: r, position: t = "popper", ...a }, o) => /* @__PURE__ */ n(f.Portal, { children: /* @__PURE__ */ n(
96
- f.Content,
97
- {
98
- ref: o,
99
- className: d(
100
- "relative z-50 max-h-96 min-w-[8rem] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
101
- t === "popper" && "data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1",
102
- e
103
- ),
104
- position: t,
105
- ...a,
106
- children: /* @__PURE__ */ n(
107
- f.Viewport,
108
- {
109
- className: d(
110
- "p-1",
111
- t === "popper" && "h-[var(--radix-select-trigger-height)] w-full min-w-[var(--radix-select-trigger-width)]"
112
- ),
113
- children: r
114
- }
115
- )
116
- }
117
- ) }));
118
- Z.displayName = f.Content.displayName;
119
- const ee = h.forwardRef(({ className: e, children: r, ...t }, a) => /* @__PURE__ */ p(
120
- f.Item,
121
- {
122
- ref: a,
123
- className: d(
124
- "relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 pl-2 pr-8 text-sm outline-none focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
125
- e
126
- ),
127
- ...t,
128
- children: [
129
- /* @__PURE__ */ n("span", { className: "absolute right-2 flex h-3.5 w-3.5 items-center justify-center", children: /* @__PURE__ */ n(f.ItemIndicator, { children: /* @__PURE__ */ n(G, { className: "h-4 w-4" }) }) }),
130
- /* @__PURE__ */ n(f.ItemText, { children: r })
131
- ]
132
- }
133
- ));
134
- ee.displayName = f.Item.displayName;
135
- const te = h.forwardRef(({ className: e, ...r }, t) => /* @__PURE__ */ n("div", { className: "relative w-full overflow-auto", children: /* @__PURE__ */ n(
136
- "table",
137
- {
138
- ref: t,
139
- className: d("w-full caption-bottom text-sm", e),
140
- ...r
141
- }
142
- ) }));
143
- te.displayName = "Table";
144
- const re = h.forwardRef(({ className: e, ...r }, t) => /* @__PURE__ */ n("thead", { ref: t, className: d("[&_tr]:border-b", e), ...r }));
145
- re.displayName = "TableHeader";
146
- const ne = h.forwardRef(({ className: e, ...r }, t) => /* @__PURE__ */ n(
147
- "tbody",
148
- {
149
- ref: t,
150
- className: d("[&_tr:last-child]:border-0", e),
151
- ...r
152
- }
153
- ));
154
- ne.displayName = "TableBody";
155
- const qe = h.forwardRef(({ className: e, ...r }, t) => /* @__PURE__ */ n(
156
- "tfoot",
157
- {
158
- ref: t,
159
- className: d(
160
- "border-t bg-muted/50 font-medium [&>tr]:last:border-b-0",
161
- e
162
- ),
163
- ...r
164
- }
165
- ));
166
- qe.displayName = "TableFooter";
167
- const U = h.forwardRef(({ className: e, ...r }, t) => /* @__PURE__ */ n(
168
- "tr",
169
- {
170
- ref: t,
171
- className: d(
172
- "border-b transition-colors hover:bg-muted/50 data-[state=selected]:bg-muted",
173
- e
174
- ),
175
- ...r
176
- }
177
- ));
178
- U.displayName = "TableRow";
179
- const T = h.forwardRef(({ className: e, ...r }, t) => /* @__PURE__ */ n(
180
- "th",
181
- {
182
- ref: t,
183
- className: d(
184
- "h-9 px-2 text-left align-middle font-medium text-muted-foreground [&:has([role=checkbox])]:pr-0",
185
- e
186
- ),
187
- ...r
188
- }
189
- ));
190
- T.displayName = "TableHead";
191
- const k = h.forwardRef(({ className: e, ...r }, t) => /* @__PURE__ */ n(
192
- "td",
193
- {
194
- ref: t,
195
- className: d(
196
- "p-2 align-middle [&:has([role=checkbox])]:pr-0",
197
- e
198
- ),
199
- ...r
200
- }
201
- ));
202
- k.displayName = "TableCell";
203
- const Je = h.forwardRef(({ className: e, ...r }, t) => /* @__PURE__ */ n(
204
- "caption",
205
- {
206
- ref: t,
207
- className: d("mt-4 text-sm text-muted-foreground", e),
208
- ...r
209
- }
210
- ));
211
- Je.displayName = "TableCaption";
212
- const _ = h.forwardRef(
213
- ({ className: e, ...r }, t) => /* @__PURE__ */ n(
214
- "textarea",
215
- {
216
- className: d(
217
- "flex min-h-[60px] w-full rounded-md border border-input bg-transparent px-3 py-2 text-sm shadow-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50",
218
- e
219
- ),
220
- ref: t,
221
- ...r
222
- }
223
- )
224
- );
225
- _.displayName = "Textarea";
226
- const Ee = [
227
- "object",
228
- "array",
229
- "string",
230
- "number",
231
- "integer",
232
- "boolean"
233
- ];
234
- let ze = 0;
235
- function $e() {
236
- return `sf-${++ze}`;
1
+ import { jsxs as m, jsx as r, Fragment as D } from "react/jsx-runtime";
2
+ import { useState as v, useRef as M, useEffect as A, useMemo as de, useCallback as y } from "react";
3
+ import { Button as x, Textarea as P, Table as le, TableHeader as ue, TableRow as K, TableHead as T, TableBody as he, TableCell as C, Input as U, Checkbox as pe, Select as me, SelectTrigger as be, SelectValue as ye, SelectContent as xe, SelectItem as ge } from "@cisri/shadcn";
4
+ import { cn as S } from "@cisri/core";
5
+ import { Undo2 as fe, Redo2 as we, ChevronDown as Ne, Minus as Se, Plus as je, Trash2 as ve } from "lucide-react";
6
+ import { generateSampleData as H } from "@cisri/json-schema-core";
7
+ let Te = 0;
8
+ function Ce() {
9
+ return `sf-${++Te}`;
237
10
  }
238
- function Be(e) {
239
- return e === "object" ? [v("field1", { type: "string" }, /* @__PURE__ */ new Set())] : e === "array" ? [v("ITEMS", { type: "string" }, /* @__PURE__ */ new Set(), !0)] : [];
11
+ function ke(e) {
12
+ return e === "object" ? [f("field1", { type: "string" }, /* @__PURE__ */ new Set())] : e === "array" ? [f("ITEMS", { type: "string" }, /* @__PURE__ */ new Set(), !0)] : [];
240
13
  }
241
- function v(e, r, t, a = !1) {
242
- const o = r.type ?? "object", c = [];
243
- if (o === "object" && r.properties) {
244
- const m = new Set(r.required ?? []);
245
- for (const [l, b] of Object.entries(r.properties))
246
- c.push(v(l, b, m));
247
- } else o === "array" && r.items && c.push(v("ITEMS", r.items, /* @__PURE__ */ new Set(), !0));
14
+ function f(e, t, n, i = !1, o = !1) {
15
+ const a = t.type ?? "object", l = [];
16
+ if (a === "object" && t.properties) {
17
+ const u = new Set(t.required ?? []);
18
+ for (const [p, h] of Object.entries(t.properties))
19
+ l.push(f(p, h, u));
20
+ } else a === "array" && t.items && l.push(f("ITEMS", t.items, /* @__PURE__ */ new Set(), !0));
248
21
  return {
249
- id: $e(),
22
+ id: Ce(),
250
23
  name: e,
251
- type: o,
252
- required: t.has(e),
253
- description: r.description,
254
- schema: r,
255
- children: c,
24
+ type: a,
25
+ required: n.has(e),
26
+ description: t.description,
27
+ schema: t,
28
+ children: l,
256
29
  expanded: !0,
257
- isArrayItem: a
30
+ isArrayItem: i,
31
+ isRoot: o
258
32
  };
259
33
  }
260
- function D(e) {
261
- if ((e.type ?? "object") !== "object" || !e.properties)
262
- return [];
263
- const t = new Set(e.required ?? []);
264
- return Object.entries(e.properties).map(
265
- ([a, o]) => v(a, o, t)
266
- );
267
- }
268
- function Oe() {
269
- return v("", { type: "string" }, /* @__PURE__ */ new Set());
34
+ function O(e) {
35
+ const t = f(e.title ?? "", e, /* @__PURE__ */ new Set(), !1, !0);
36
+ return t.type === "object" && t.children.length === 0 && t.children.push(f("field1", { type: "string" }, /* @__PURE__ */ new Set())), [t];
270
37
  }
271
- function q(e) {
272
- return e.length > 0 ? e : [Oe()];
38
+ function Ie() {
39
+ return f("", { type: "string" }, /* @__PURE__ */ new Set());
273
40
  }
274
- function J(e) {
275
- switch (e.type) {
276
- case "string":
277
- return "string";
278
- case "number":
279
- case "integer":
280
- return 0;
281
- case "boolean":
282
- return !0;
283
- case "object": {
284
- const r = {};
285
- if (e.properties)
286
- for (const [t, a] of Object.entries(e.properties))
287
- r[t] = J(a);
288
- return r;
289
- }
290
- case "array":
291
- return e.items ? [J(e.items)] : [];
292
- default:
293
- return null;
294
- }
41
+ function F(e) {
42
+ return e.length > 0 ? e : [Ie()];
295
43
  }
296
44
  function R(e) {
297
- const r = {}, t = [];
298
- for (const a of e)
299
- r[a.name] = ae(a), a.required && t.push(a.name);
300
- return {
301
- type: "object",
302
- properties: r,
303
- required: t.length > 0 ? t : void 0
304
- };
45
+ if (e.length !== 1 || !e[0].isRoot)
46
+ throw new Error("fieldsToSchema expects exactly one root field");
47
+ return V(e[0]);
305
48
  }
306
- function ae(e) {
307
- const r = { ...e.schema };
49
+ function V(e) {
50
+ const t = { ...e.schema };
308
51
  if (e.type === "object") {
309
- const t = R(e.children);
52
+ const n = Ae(e.children);
310
53
  return {
311
- ...r,
54
+ ...t,
312
55
  type: e.type,
56
+ title: e.isRoot ? e.name : t.title,
313
57
  description: e.description,
314
- properties: t.properties,
315
- required: t.required,
58
+ properties: n.properties,
59
+ required: n.required,
316
60
  items: void 0
317
61
  };
318
62
  }
319
63
  if (e.type === "array") {
320
- const t = e.children[0];
64
+ const n = e.children[0];
321
65
  return {
322
- ...r,
66
+ ...t,
323
67
  type: e.type,
68
+ title: e.isRoot ? e.name : t.title,
324
69
  description: e.description,
325
- items: t ? ae(t) : { type: "string" },
70
+ items: n ? V(n) : { type: "string" },
326
71
  properties: void 0,
327
72
  required: void 0
328
73
  };
329
74
  }
330
75
  return {
331
- ...r,
76
+ ...t,
332
77
  type: e.type,
78
+ title: e.isRoot ? e.name : t.title,
333
79
  description: e.description,
334
80
  properties: void 0,
335
81
  items: void 0,
336
82
  required: void 0
337
83
  };
338
84
  }
339
- function oe(e, r, t) {
340
- return e.map((a) => {
341
- if (a.id === r) {
342
- const o = { ...a, ...t };
343
- return t.type && t.type !== a.type && (o.children = Be(t.type)), o;
85
+ function Ae(e) {
86
+ const t = {}, n = [];
87
+ for (const i of e)
88
+ t[i.name] = V(i), i.required && n.push(i.name);
89
+ return {
90
+ properties: t,
91
+ required: n.length > 0 ? n : void 0
92
+ };
93
+ }
94
+ const Fe = [
95
+ "object",
96
+ "array",
97
+ "string",
98
+ "number",
99
+ "integer",
100
+ "boolean"
101
+ ];
102
+ function _(e, t, n) {
103
+ return e.map((i) => {
104
+ if (i.id === t) {
105
+ const o = { ...i, ...n };
106
+ return n.type && n.type !== i.type && (o.children = ke(n.type)), o;
344
107
  }
345
- return a.children.length > 0 ? {
346
- ...a,
347
- children: oe(a.children, r, t)
348
- } : a;
108
+ return i.children.length > 0 ? {
109
+ ...i,
110
+ children: _(i.children, t, n)
111
+ } : i;
349
112
  });
350
113
  }
351
- function se(e, r) {
352
- const t = e.findIndex((a) => a.id === r);
353
- if (t !== -1) {
354
- const a = v(
114
+ function Y(e, t) {
115
+ const n = e.findIndex((i) => i.id === t);
116
+ if (n !== -1) {
117
+ const i = f(
355
118
  `field${e.length + 1}`,
356
119
  { type: "string" },
357
120
  /* @__PURE__ */ new Set()
358
121
  ), o = [...e];
359
- return o.splice(t + 1, 0, a), o;
122
+ return o.splice(n + 1, 0, i), o;
360
123
  }
361
- for (let a = 0; a < e.length; a++)
362
- if (e[a].children.length > 0) {
363
- const o = se(e[a].children, r);
124
+ for (let i = 0; i < e.length; i++)
125
+ if (e[i].children.length > 0) {
126
+ const o = Y(e[i].children, t);
364
127
  if (o)
365
128
  return [
366
- ...e.slice(0, a),
367
- { ...e[a], children: o },
368
- ...e.slice(a + 1)
129
+ ...e.slice(0, i),
130
+ { ...e[i], children: o },
131
+ ...e.slice(i + 1)
369
132
  ];
370
133
  }
371
134
  return null;
372
135
  }
373
- function ie(e, r) {
374
- return e.filter((t) => t.id !== r).map((t) => ({
375
- ...t,
376
- children: ie(t.children, r)
136
+ function G(e, t) {
137
+ return e.filter((n) => n.id !== t).map((n) => ({
138
+ ...n,
139
+ children: G(n.children, t)
377
140
  }));
378
141
  }
379
- function de(e, r) {
380
- return e.map((t) => t.id === r ? { ...t, expanded: !t.expanded } : t.children.length > 0 ? {
381
- ...t,
382
- children: de(t.children, r)
383
- } : t);
142
+ function Q(e, t) {
143
+ return e.map((n) => n.id === t ? { ...n, expanded: !n.expanded } : n.children.length > 0 ? {
144
+ ...n,
145
+ children: Q(n.children, t)
146
+ } : n);
384
147
  }
385
- function E(e, r) {
386
- if (e === r) return !0;
387
- if (typeof e != "object" || typeof r != "object" || e == null || r == null)
148
+ function E(e, t) {
149
+ if (e === t) return !0;
150
+ if (typeof e != "object" || typeof t != "object" || e == null || t == null)
388
151
  return !1;
389
- const t = Array.isArray(e), a = Array.isArray(r);
390
- if (t !== a) return !1;
391
- if (t) {
392
- const b = e, y = r;
393
- return b.length !== y.length ? !1 : b.every((z, $) => E(z, y[$]));
152
+ const n = Array.isArray(e), i = Array.isArray(t);
153
+ if (n !== i) return !1;
154
+ if (n) {
155
+ const p = e, h = t;
156
+ return p.length !== h.length ? !1 : p.every((J, q) => E(J, h[q]));
394
157
  }
395
- const o = e, c = r, m = Object.keys(o), l = Object.keys(c);
396
- return m.length !== l.length ? !1 : m.every((b) => E(o[b], c[b]));
158
+ const o = e, a = t, l = Object.keys(o), u = Object.keys(a);
159
+ return l.length !== u.length ? !1 : l.every((p) => E(o[p], a[p]));
397
160
  }
398
- function Ve({ level: e }) {
399
- return /* @__PURE__ */ n(L, { children: Array.from({ length: e }).map((r, t) => /* @__PURE__ */ n("span", { className: "inline-block w-5 shrink-0" }, t)) });
161
+ function Re({ level: e }) {
162
+ return /* @__PURE__ */ r(D, { children: Array.from({ length: e }).map((t, n) => /* @__PURE__ */ r("span", { className: "inline-block w-5 shrink-0" }, n)) });
400
163
  }
401
- function Me({
164
+ function Ee({
402
165
  value: e,
403
- "aria-label": r,
404
- disabled: t,
405
- onChange: a
166
+ "aria-label": t,
167
+ disabled: n,
168
+ onChange: i
406
169
  }) {
407
- return /* @__PURE__ */ p(Ae, { value: e, onValueChange: (o) => a(o), disabled: t, children: [
408
- /* @__PURE__ */ n(X, { "aria-label": r, className: "h-7 w-28 text-xs", children: /* @__PURE__ */ n(Fe, {}) }),
409
- /* @__PURE__ */ n(Z, { children: Ee.map((o) => /* @__PURE__ */ n(ee, { value: o, className: "text-xs", children: o }, o)) })
170
+ return /* @__PURE__ */ m(me, { value: e, onValueChange: (o) => i(o), disabled: n, children: [
171
+ /* @__PURE__ */ r(be, { "aria-label": t, className: "h-7 w-28 text-xs", children: /* @__PURE__ */ r(ye, {}) }),
172
+ /* @__PURE__ */ r(xe, { children: Fe.map((o) => /* @__PURE__ */ r(ge, { value: o, className: "text-xs", children: o }, o)) })
410
173
  ] });
411
174
  }
412
- function De({
175
+ function Je({
413
176
  fieldName: e,
414
- showAdd: r,
415
- showDelete: t = !0,
416
- disableDelete: a,
177
+ showAdd: t,
178
+ showDelete: n = !0,
179
+ disableDelete: i,
417
180
  onAdd: o,
418
- onDelete: c
181
+ onDelete: a
419
182
  }) {
420
- return /* @__PURE__ */ p("div", { className: "flex items-center justify-end gap-1", children: [
421
- r && /* @__PURE__ */ n(
422
- w,
183
+ return /* @__PURE__ */ m("div", { className: "flex items-center justify-end gap-1", children: [
184
+ t && /* @__PURE__ */ r(
185
+ x,
423
186
  {
424
187
  type: "button",
425
188
  variant: "ghost",
@@ -427,115 +190,115 @@ function De({
427
190
  className: "h-7 w-7",
428
191
  onClick: o,
429
192
  "aria-label": `在字段 ${e || "未命名"} 后添加行`,
430
- children: /* @__PURE__ */ n(Ce, { className: "h-4 w-4" })
193
+ children: /* @__PURE__ */ r(je, { className: "h-4 w-4" })
431
194
  }
432
195
  ),
433
- t && /* @__PURE__ */ n(
434
- w,
196
+ n && /* @__PURE__ */ r(
197
+ x,
435
198
  {
436
199
  type: "button",
437
200
  variant: "ghost",
438
201
  size: "icon",
439
202
  className: "h-7 w-7",
440
- disabled: a,
441
- onClick: c,
203
+ disabled: i,
204
+ onClick: a,
442
205
  "aria-label": `删除字段 ${e || "未命名"}`,
443
- children: /* @__PURE__ */ n(Re, { className: "h-4 w-4 text-destructive" })
206
+ children: /* @__PURE__ */ r(ve, { className: "h-4 w-4 text-destructive" })
444
207
  }
445
208
  )
446
209
  ] });
447
210
  }
448
- function ce({ field: e, level: r, readOnly: t, disableDelete: a, dispatch: o }) {
449
- const c = e.children.length > 0, m = e.name || "未命名";
450
- return /* @__PURE__ */ p(L, { children: [
451
- /* @__PURE__ */ p(U, { children: [
452
- /* @__PURE__ */ n(k, { children: /* @__PURE__ */ p("div", { className: "flex items-center", children: [
453
- /* @__PURE__ */ n(Ve, { level: r }),
454
- c ? /* @__PURE__ */ n(
211
+ function W({ field: e, level: t, readOnly: n, disableDelete: i, dispatch: o }) {
212
+ const a = e.children.length > 0, l = e.name || "未命名";
213
+ return /* @__PURE__ */ m(D, { children: [
214
+ /* @__PURE__ */ m(K, { className: S(e.isRoot && "bg-muted/30"), children: [
215
+ /* @__PURE__ */ r(C, { children: /* @__PURE__ */ m("div", { className: "flex items-center", children: [
216
+ /* @__PURE__ */ r(Re, { level: t }),
217
+ a ? /* @__PURE__ */ r(
455
218
  "button",
456
219
  {
457
220
  type: "button",
458
221
  onClick: () => o({ type: "toggleExpand", id: e.id }),
459
222
  className: "mr-1 inline-flex h-3.5 w-3.5 items-center justify-center text-muted-foreground",
460
223
  "aria-label": e.expanded ? "折叠" : "展开",
461
- children: /* @__PURE__ */ n(
462
- Q,
224
+ children: /* @__PURE__ */ r(
225
+ Ne,
463
226
  {
464
- className: d(
227
+ className: S(
465
228
  "h-3.5 w-3.5 transition-transform",
466
229
  !e.expanded && "-rotate-90"
467
230
  )
468
231
  }
469
232
  )
470
233
  }
471
- ) : /* @__PURE__ */ n(ke, { className: "mr-1 h-3.5 w-3.5 text-muted-foreground/60" }),
472
- /* @__PURE__ */ n(
473
- H,
234
+ ) : /* @__PURE__ */ r(Se, { className: "mr-1 h-3.5 w-3.5 text-muted-foreground/60" }),
235
+ /* @__PURE__ */ r(
236
+ U,
474
237
  {
475
238
  id: `schema-name-${e.id}`,
476
239
  name: `schema-name-${e.id}`,
477
- "aria-label": `字段 ${m} 名称`,
240
+ "aria-label": `字段 ${l} 名称`,
478
241
  autoComplete: "off",
479
- disabled: t || e.isArrayItem,
242
+ disabled: n || e.isArrayItem,
480
243
  className: "h-7 border-transparent bg-transparent px-1 shadow-none focus-visible:bg-background disabled:opacity-100",
481
244
  value: e.name,
482
- onChange: (l) => o({
245
+ onChange: (u) => o({
483
246
  type: "update",
484
247
  id: e.id,
485
- patch: { name: l.target.value }
248
+ patch: { name: u.target.value }
486
249
  })
487
250
  }
488
251
  )
489
252
  ] }) }),
490
- /* @__PURE__ */ n(k, { children: /* @__PURE__ */ n(
491
- Me,
253
+ /* @__PURE__ */ r(C, { children: /* @__PURE__ */ r(
254
+ Ee,
492
255
  {
493
256
  value: e.type,
494
- "aria-label": `字段 ${m} 类型`,
495
- disabled: t,
496
- onChange: (l) => o({ type: "update", id: e.id, patch: { type: l } })
257
+ "aria-label": `字段 ${l} 类型`,
258
+ disabled: n,
259
+ onChange: (u) => o({ type: "update", id: e.id, patch: { type: u } })
497
260
  }
498
261
  ) }),
499
- /* @__PURE__ */ n(k, { children: e.isArrayItem ? null : /* @__PURE__ */ n(
500
- W,
262
+ /* @__PURE__ */ r(C, { children: e.isArrayItem || e.isRoot ? null : /* @__PURE__ */ r(
263
+ pe,
501
264
  {
502
265
  id: `schema-required-${e.id}`,
503
266
  name: `schema-required-${e.id}`,
504
- "aria-label": `字段 ${m} 是否必填`,
267
+ "aria-label": `字段 ${l} 是否必填`,
505
268
  checked: e.required,
506
- disabled: t,
507
- onCheckedChange: (l) => o({
269
+ disabled: n,
270
+ onCheckedChange: (u) => o({
508
271
  type: "update",
509
272
  id: e.id,
510
- patch: { required: l === !0 }
273
+ patch: { required: u === !0 }
511
274
  })
512
275
  }
513
276
  ) }),
514
- /* @__PURE__ */ n(k, { children: /* @__PURE__ */ n(
515
- H,
277
+ /* @__PURE__ */ r(C, { children: /* @__PURE__ */ r(
278
+ U,
516
279
  {
517
280
  id: `schema-description-${e.id}`,
518
281
  name: `schema-description-${e.id}`,
519
- "aria-label": `字段 ${m} 描述`,
282
+ "aria-label": `字段 ${l} 描述`,
520
283
  autoComplete: "off",
521
- disabled: t,
284
+ disabled: n,
522
285
  className: "h-7 border-transparent bg-transparent px-1 shadow-none focus-visible:bg-background disabled:opacity-100",
523
286
  value: e.description ?? "",
524
- onChange: (l) => o({
287
+ onChange: (u) => o({
525
288
  type: "update",
526
289
  id: e.id,
527
- patch: { description: l.target.value }
290
+ patch: { description: u.target.value }
528
291
  })
529
292
  }
530
293
  ) }),
531
- /* @__PURE__ */ n(k, { children: !t && /* @__PURE__ */ n(
532
- De,
294
+ /* @__PURE__ */ r(C, { children: !n && !e.isRoot && /* @__PURE__ */ r(
295
+ Je,
533
296
  {
534
297
  fieldId: e.id,
535
298
  fieldName: e.name,
536
299
  showAdd: !e.isArrayItem,
537
300
  showDelete: !e.isArrayItem,
538
- disableDelete: a,
301
+ disableDelete: i,
539
302
  onAdd: () => o({
540
303
  type: "addSibling",
541
304
  id: e.id
@@ -544,262 +307,264 @@ function ce({ field: e, level: r, readOnly: t, disableDelete: a, dispatch: o })
544
307
  }
545
308
  ) })
546
309
  ] }),
547
- e.expanded && e.children.map((l) => /* @__PURE__ */ n(
548
- ce,
310
+ e.expanded && e.children.map((u) => /* @__PURE__ */ r(
311
+ W,
549
312
  {
550
- field: l,
551
- level: r + 1,
552
- readOnly: t,
313
+ field: u,
314
+ level: t + 1,
315
+ readOnly: n,
553
316
  disableDelete: e.type === "object" && e.children.length <= 1,
554
317
  dispatch: o
555
318
  },
556
- l.id
319
+ u.id
557
320
  ))
558
321
  ] });
559
322
  }
560
- function Ye({
323
+ function De({
561
324
  value: e,
562
- onChange: r,
563
- onSave: t,
564
- readOnly: a,
325
+ onChange: t,
326
+ onSave: n,
327
+ readOnly: i,
565
328
  className: o
566
329
  }) {
567
- const [c, m] = C(() => ({
568
- history: [q(D(e))],
330
+ const [a, l] = v(() => ({
331
+ history: [F(O(e))],
569
332
  index: 0
570
- })), l = c.history[c.index], b = M(c);
571
- F(() => {
572
- b.current = c;
573
- }, [c]);
574
- const [y, z] = C("table"), [$, I] = C(
333
+ })), u = a.history[a.index], p = M(a);
334
+ A(() => {
335
+ p.current = a;
336
+ }, [a]);
337
+ const [h, J] = v("table"), [q, k] = v(
575
338
  () => JSON.stringify(e, null, 2)
576
- ), [K, S] = C(null), [le, B] = C(null), O = M(!1), j = M(e), N = Ne(() => JSON.stringify(e, null, 2), [e]);
577
- F(() => {
578
- if (!E(e, j.current)) {
579
- const s = q(D(e));
580
- m({ history: [s], index: 0 }), j.current = e;
339
+ ), [L, w] = v(null), [X, $] = v(null), z = M(!1), N = M(e), g = de(() => JSON.stringify(e, null, 2), [e]);
340
+ A(() => {
341
+ if (!E(e, N.current)) {
342
+ const s = F(O(e));
343
+ l({ history: [s], index: 0 }), N.current = e;
581
344
  }
582
- }, [e]), F(() => {
583
- O.current || (I(N), S(null));
584
- }, [N]), F(() => {
585
- B(null);
586
- }, [y]);
587
- const A = x((s) => {
588
- m((i) => E(i.history[i.index], s) ? i : {
589
- history: [...i.history.slice(0, i.index + 1), s],
590
- index: i.index + 1
345
+ }, [e]), A(() => {
346
+ z.current || (k(g), w(null));
347
+ }, [g]), A(() => {
348
+ $(null);
349
+ }, [h]);
350
+ const I = y((s) => {
351
+ l((c) => E(c.history[c.index], s) ? c : {
352
+ history: [...c.history.slice(0, c.index + 1), s],
353
+ index: c.index + 1
591
354
  });
592
- }, []), ue = x(
355
+ }, []), Z = y(
593
356
  (s) => {
594
- const i = b.current.history[b.current.index];
595
- let u;
357
+ const c = p.current.history[p.current.index], b = c[0];
358
+ if (b && s.id === b.id && (s.type === "delete" || s.type === "addSibling"))
359
+ return;
360
+ let d;
596
361
  switch (s.type) {
597
362
  case "update":
598
- u = oe(i, s.id, s.patch);
363
+ d = _(c, s.id, s.patch);
599
364
  break;
600
365
  case "addSibling":
601
- u = se(i, s.id) ?? i;
366
+ d = Y(c, s.id) ?? c;
602
367
  break;
603
368
  case "delete":
604
- u = ie(i, s.id);
369
+ d = G(c, s.id);
605
370
  break;
606
371
  case "toggleExpand":
607
- u = de(i, s.id);
372
+ d = Q(c, s.id);
608
373
  break;
609
374
  default:
610
375
  return;
611
376
  }
612
- u = q(u);
613
- const g = R(u);
614
- j.current = g, A(u), r(g);
377
+ d = F(d);
378
+ const j = R(d);
379
+ N.current = j, I(d), t(j);
615
380
  },
616
- [r, A]
617
- ), me = c.index > 0, pe = c.index < c.history.length - 1, he = x(() => {
618
- const s = b.current;
381
+ [t, I]
382
+ ), ee = a.index > 0, te = a.index < a.history.length - 1, ne = y(() => {
383
+ const s = p.current;
619
384
  if (s.index <= 0) return;
620
- const i = s.index - 1, u = s.history[i], g = R(u);
621
- m({ ...s, index: i }), j.current = g, r(g);
622
- }, [r]), be = x(() => {
623
- const s = b.current;
385
+ const c = s.index - 1, b = s.history[c], d = R(b);
386
+ l({ ...s, index: c }), N.current = d, t(d);
387
+ }, [t]), re = y(() => {
388
+ const s = p.current;
624
389
  if (s.index >= s.history.length - 1) return;
625
- const i = s.index + 1, u = s.history[i], g = R(u);
626
- m({ ...s, index: i }), j.current = g, r(g);
627
- }, [r]), V = x(
390
+ const c = s.index + 1, b = s.history[c], d = R(b);
391
+ l({ ...s, index: c }), N.current = d, t(d);
392
+ }, [t]), B = y(
628
393
  (s) => () => {
629
- z(s), s === "json" && (I(N), S(null));
394
+ J(s), s === "json" && (k(g), w(null));
630
395
  },
631
- [N]
632
- ), fe = x(
396
+ [g]
397
+ ), ie = y(
633
398
  (s) => {
634
- const i = s.target.value;
635
- I(i);
399
+ const c = s.target.value;
400
+ k(c);
636
401
  try {
637
- const u = JSON.parse(i);
638
- if (u && typeof u == "object" && !Array.isArray(u)) {
639
- const g = q(D(u)), Y = R(g);
640
- j.current = Y, A(g), r(Y), S(null);
402
+ const b = JSON.parse(c);
403
+ if (b && typeof b == "object" && !Array.isArray(b)) {
404
+ const d = F(O(b)), j = R(d);
405
+ N.current = j, I(d), t(j), w(null);
641
406
  } else
642
- S("请输入有效的 JSON 对象。");
407
+ w("请输入有效的 JSON 对象。");
643
408
  } catch {
644
- S("JSON 解析失败,请检查格式。");
409
+ w("JSON 解析失败,请检查格式。");
645
410
  }
646
411
  },
647
- [r, A]
648
- ), ye = x(() => {
649
- O.current = !0;
650
- }, []), ge = x(() => {
651
- O.current = !1, I(N), S(null);
652
- }, [N]), xe = x(async () => {
412
+ [t, I]
413
+ ), se = y(() => {
414
+ z.current = !0;
415
+ }, []), oe = y(() => {
416
+ z.current = !1, k(g), w(null);
417
+ }, [g]), ce = y(async () => {
653
418
  try {
654
- let s = N, i = "已复制 json schema";
655
- y === "preview" && (s = JSON.stringify(J(e), null, 2), i = "已复制 json 示例"), await navigator.clipboard.writeText(s), B(i), window.setTimeout(() => B(null), 1500);
419
+ let s = g, c = "已复制 json schema";
420
+ h === "preview" && (s = JSON.stringify(H(e), null, 2), c = "已复制 json 示例"), await navigator.clipboard.writeText(s), $(c), window.setTimeout(() => $(null), 1500);
656
421
  } catch {
657
422
  }
658
- }, [N, e, y]), we = x(() => {
659
- t == null || t(e);
660
- }, [t, e]);
661
- return /* @__PURE__ */ p(
423
+ }, [g, e, h]), ae = y(() => {
424
+ n == null || n(e);
425
+ }, [n, e]);
426
+ return /* @__PURE__ */ m(
662
427
  "div",
663
428
  {
664
- className: d(
429
+ className: S(
665
430
  "rounded-md border border-border bg-card text-card-foreground",
666
431
  o
667
432
  ),
668
433
  children: [
669
- /* @__PURE__ */ p("div", { className: "flex flex-wrap items-center justify-between gap-2 border-b border-border p-2", children: [
670
- /* @__PURE__ */ p("div", { className: "inline-flex items-center overflow-hidden rounded-md border border-border", children: [
671
- /* @__PURE__ */ n(
672
- w,
434
+ /* @__PURE__ */ m("div", { className: "flex flex-wrap items-center justify-between gap-2 border-b border-border p-2", children: [
435
+ /* @__PURE__ */ m("div", { className: "inline-flex items-center overflow-hidden rounded-md border border-border", children: [
436
+ /* @__PURE__ */ r(
437
+ x,
673
438
  {
674
439
  type: "button",
675
440
  variant: "ghost",
676
441
  size: "sm",
677
- onClick: V("table"),
678
- className: d(
442
+ onClick: B("table"),
443
+ className: S(
679
444
  "h-7 rounded-none border-r border-border text-xs",
680
- y === "table" && "bg-muted"
445
+ h === "table" && "bg-muted"
681
446
  ),
682
447
  children: "表格视图"
683
448
  }
684
449
  ),
685
- /* @__PURE__ */ n(
686
- w,
450
+ /* @__PURE__ */ r(
451
+ x,
687
452
  {
688
453
  type: "button",
689
454
  variant: "ghost",
690
455
  size: "sm",
691
- onClick: V("json"),
692
- className: d(
456
+ onClick: B("json"),
457
+ className: S(
693
458
  "h-7 rounded-none border-r border-border text-xs",
694
- y === "json" && "bg-muted"
459
+ h === "json" && "bg-muted"
695
460
  ),
696
461
  children: "JSON 视图"
697
462
  }
698
463
  ),
699
- /* @__PURE__ */ n(
700
- w,
464
+ /* @__PURE__ */ r(
465
+ x,
701
466
  {
702
467
  type: "button",
703
468
  variant: "ghost",
704
469
  size: "sm",
705
- onClick: V("preview"),
706
- className: d(
470
+ onClick: B("preview"),
471
+ className: S(
707
472
  "h-7 rounded-none text-xs",
708
- y === "preview" && "bg-muted"
473
+ h === "preview" && "bg-muted"
709
474
  ),
710
475
  children: "预览"
711
476
  }
712
477
  )
713
478
  ] }),
714
- /* @__PURE__ */ p("div", { className: "flex flex-wrap items-center gap-2", children: [
715
- !a && t && /* @__PURE__ */ n(
716
- w,
479
+ /* @__PURE__ */ m("div", { className: "flex flex-wrap items-center gap-2", children: [
480
+ !i && n && /* @__PURE__ */ r(
481
+ x,
717
482
  {
718
483
  type: "button",
719
484
  size: "sm",
720
- onClick: we,
485
+ onClick: ae,
721
486
  className: "h-7 text-xs",
722
487
  children: "保存"
723
488
  }
724
489
  ),
725
- y !== "table" && /* @__PURE__ */ n(
726
- w,
490
+ h !== "table" && /* @__PURE__ */ r(
491
+ x,
727
492
  {
728
493
  type: "button",
729
494
  variant: "outline",
730
495
  size: "sm",
731
- onClick: xe,
496
+ onClick: ce,
732
497
  className: "h-7 text-xs",
733
- children: le ?? "复制"
498
+ children: X ?? "复制"
734
499
  }
735
500
  ),
736
- !a && /* @__PURE__ */ p(L, { children: [
737
- /* @__PURE__ */ n(
738
- w,
501
+ !i && /* @__PURE__ */ m(D, { children: [
502
+ /* @__PURE__ */ r(
503
+ x,
739
504
  {
740
505
  type: "button",
741
506
  variant: "ghost",
742
507
  size: "icon",
743
- onClick: he,
744
- disabled: !me,
508
+ onClick: ne,
509
+ disabled: !ee,
745
510
  className: "h-7 w-7",
746
511
  "aria-label": "撤销",
747
- children: /* @__PURE__ */ n(je, { className: "h-4 w-4" })
512
+ children: /* @__PURE__ */ r(fe, { className: "h-4 w-4" })
748
513
  }
749
514
  ),
750
- /* @__PURE__ */ n(
751
- w,
515
+ /* @__PURE__ */ r(
516
+ x,
752
517
  {
753
518
  type: "button",
754
519
  variant: "ghost",
755
520
  size: "icon",
756
- onClick: be,
757
- disabled: !pe,
521
+ onClick: re,
522
+ disabled: !te,
758
523
  className: "h-7 w-7",
759
524
  "aria-label": "恢复",
760
- children: /* @__PURE__ */ n(Te, { className: "h-4 w-4" })
525
+ children: /* @__PURE__ */ r(we, { className: "h-4 w-4" })
761
526
  }
762
527
  )
763
528
  ] })
764
529
  ] })
765
530
  ] }),
766
- y === "json" ? /* @__PURE__ */ p("div", { className: "space-y-2 p-2", children: [
767
- /* @__PURE__ */ n(
768
- _,
531
+ h === "json" ? /* @__PURE__ */ m("div", { className: "space-y-2 p-2", children: [
532
+ /* @__PURE__ */ r(
533
+ P,
769
534
  {
770
- value: $,
771
- onChange: a ? void 0 : fe,
772
- onFocus: a ? void 0 : ye,
773
- onBlur: a ? void 0 : ge,
774
- readOnly: a,
535
+ value: q,
536
+ onChange: i ? void 0 : ie,
537
+ onFocus: i ? void 0 : se,
538
+ onBlur: i ? void 0 : oe,
539
+ readOnly: i,
775
540
  placeholder: "在此编辑或粘贴 JSON Schema...",
776
541
  className: "min-h-[320px] font-mono text-xs"
777
542
  }
778
543
  ),
779
- K && /* @__PURE__ */ n("p", { className: "text-xs text-destructive", children: K })
780
- ] }) : y === "preview" ? /* @__PURE__ */ n("div", { className: "p-2", children: /* @__PURE__ */ n(
781
- _,
544
+ L && /* @__PURE__ */ r("p", { className: "text-xs text-destructive", children: L })
545
+ ] }) : h === "preview" ? /* @__PURE__ */ r("div", { className: "p-2", children: /* @__PURE__ */ r(
546
+ P,
782
547
  {
783
- value: JSON.stringify(J(e), null, 2),
548
+ value: JSON.stringify(H(e), null, 2),
784
549
  readOnly: !0,
785
550
  className: "min-h-[320px] font-mono text-xs"
786
551
  }
787
- ) }) : /* @__PURE__ */ p(te, { children: [
788
- /* @__PURE__ */ n(re, { children: /* @__PURE__ */ p(U, { children: [
789
- /* @__PURE__ */ n(T, { className: "w-[28%]", children: "名称" }),
790
- /* @__PURE__ */ n(T, { className: "w-[18%]", children: "类型" }),
791
- /* @__PURE__ */ n(T, { className: "w-[12%]", children: "必填" }),
792
- /* @__PURE__ */ n(T, { className: "w-[30%]", children: "描述" }),
793
- /* @__PURE__ */ n(T, { className: "w-[12%]" })
552
+ ) }) : /* @__PURE__ */ m(le, { children: [
553
+ /* @__PURE__ */ r(ue, { children: /* @__PURE__ */ m(K, { children: [
554
+ /* @__PURE__ */ r(T, { className: "w-[28%]", children: "名称" }),
555
+ /* @__PURE__ */ r(T, { className: "w-[18%]", children: "类型" }),
556
+ /* @__PURE__ */ r(T, { className: "w-[12%]", children: "必填" }),
557
+ /* @__PURE__ */ r(T, { className: "w-[30%]", children: "描述" }),
558
+ /* @__PURE__ */ r(T, { className: "w-[12%]" })
794
559
  ] }) }),
795
- /* @__PURE__ */ n(ne, { children: l.map((s) => /* @__PURE__ */ n(
796
- ce,
560
+ /* @__PURE__ */ r(he, { children: u.map((s) => /* @__PURE__ */ r(
561
+ W,
797
562
  {
798
563
  field: s,
799
564
  level: 0,
800
- readOnly: a,
801
- disableDelete: l.length <= 1,
802
- dispatch: ue
565
+ readOnly: i,
566
+ disableDelete: !0,
567
+ dispatch: Z
803
568
  },
804
569
  s.id
805
570
  )) })
@@ -809,5 +574,5 @@ function Ye({
809
574
  );
810
575
  }
811
576
  export {
812
- Ye as JsonSchemaEditor
577
+ De as JsonSchemaEditor
813
578
  };