modern-json-react 1.0.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.
- package/LICENSE +21 -0
- package/README.md +243 -0
- package/dist/index.d.mts +440 -0
- package/dist/index.d.ts +440 -0
- package/dist/index.js +1691 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1670 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +67 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1691 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var react = require('react');
|
|
4
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
5
|
+
|
|
6
|
+
// src/JsonEditor.tsx
|
|
7
|
+
var MODE_OPTIONS = [
|
|
8
|
+
{ value: "code", label: "Code" },
|
|
9
|
+
{ value: "tree", label: "Tree" },
|
|
10
|
+
{ value: "split", label: "Split" }
|
|
11
|
+
];
|
|
12
|
+
var Toolbar = ({
|
|
13
|
+
mode,
|
|
14
|
+
onModeChange,
|
|
15
|
+
canUndo,
|
|
16
|
+
canRedo,
|
|
17
|
+
onUndo,
|
|
18
|
+
onRedo,
|
|
19
|
+
onFormat,
|
|
20
|
+
searchable,
|
|
21
|
+
isSearchOpen,
|
|
22
|
+
onToggleSearch,
|
|
23
|
+
readOnly,
|
|
24
|
+
className = ""
|
|
25
|
+
}) => {
|
|
26
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: `mjr-toolbar ${className}`, role: "toolbar", "aria-label": "Editor controls", children: [
|
|
27
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { className: "mjr-toolbar__modes", role: "tablist", "aria-label": "Editor mode", children: MODE_OPTIONS.map(({ value, label }) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
28
|
+
"button",
|
|
29
|
+
{
|
|
30
|
+
role: "tab",
|
|
31
|
+
"aria-selected": mode === value,
|
|
32
|
+
className: `mjr-toolbar__mode-btn ${mode === value ? "mjr-toolbar__mode-btn--active" : ""}`,
|
|
33
|
+
onClick: () => onModeChange(value),
|
|
34
|
+
children: label
|
|
35
|
+
},
|
|
36
|
+
value
|
|
37
|
+
)) }),
|
|
38
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { className: "mjr-toolbar__separator", "aria-hidden": "true" }),
|
|
39
|
+
searchable && /* @__PURE__ */ jsxRuntime.jsx(
|
|
40
|
+
"button",
|
|
41
|
+
{
|
|
42
|
+
className: `mjr-toolbar__btn ${isSearchOpen ? "mjr-toolbar__btn--active" : ""}`,
|
|
43
|
+
onClick: onToggleSearch,
|
|
44
|
+
"aria-label": "Toggle search",
|
|
45
|
+
"aria-pressed": isSearchOpen,
|
|
46
|
+
title: "Search (Ctrl+F)",
|
|
47
|
+
children: "\u{1F50D}"
|
|
48
|
+
}
|
|
49
|
+
),
|
|
50
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { className: "mjr-toolbar__separator", "aria-hidden": "true" }),
|
|
51
|
+
!readOnly && /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
52
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
53
|
+
"button",
|
|
54
|
+
{
|
|
55
|
+
className: "mjr-toolbar__btn",
|
|
56
|
+
onClick: onUndo,
|
|
57
|
+
disabled: !canUndo,
|
|
58
|
+
"aria-label": "Undo",
|
|
59
|
+
title: "Undo (Ctrl+Z)",
|
|
60
|
+
children: "\u21B6"
|
|
61
|
+
}
|
|
62
|
+
),
|
|
63
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
64
|
+
"button",
|
|
65
|
+
{
|
|
66
|
+
className: "mjr-toolbar__btn",
|
|
67
|
+
onClick: onRedo,
|
|
68
|
+
disabled: !canRedo,
|
|
69
|
+
"aria-label": "Redo",
|
|
70
|
+
title: "Redo (Ctrl+Shift+Z)",
|
|
71
|
+
children: "\u21B7"
|
|
72
|
+
}
|
|
73
|
+
)
|
|
74
|
+
] }),
|
|
75
|
+
!readOnly && /* @__PURE__ */ jsxRuntime.jsx(
|
|
76
|
+
"button",
|
|
77
|
+
{
|
|
78
|
+
className: "mjr-toolbar__btn",
|
|
79
|
+
onClick: onFormat,
|
|
80
|
+
"aria-label": "Format document",
|
|
81
|
+
title: "Format (Ctrl+Shift+P)",
|
|
82
|
+
children: "{ }"
|
|
83
|
+
}
|
|
84
|
+
)
|
|
85
|
+
] });
|
|
86
|
+
};
|
|
87
|
+
var CodeEditor = ({
|
|
88
|
+
value,
|
|
89
|
+
onChange,
|
|
90
|
+
parseError,
|
|
91
|
+
readOnly,
|
|
92
|
+
lineNumbers,
|
|
93
|
+
searchMatches,
|
|
94
|
+
currentMatchIndex,
|
|
95
|
+
onCursorChange,
|
|
96
|
+
className = ""
|
|
97
|
+
}) => {
|
|
98
|
+
const textareaRef = react.useRef(null);
|
|
99
|
+
const lines = react.useMemo(() => value.split("\n"), [value]);
|
|
100
|
+
const handleChange = react.useCallback(
|
|
101
|
+
(e) => {
|
|
102
|
+
if (readOnly) return;
|
|
103
|
+
onChange(e.target.value);
|
|
104
|
+
},
|
|
105
|
+
[onChange, readOnly]
|
|
106
|
+
);
|
|
107
|
+
const handleKeyDown = react.useCallback(
|
|
108
|
+
(e) => {
|
|
109
|
+
if (readOnly) return;
|
|
110
|
+
if (e.key === "Tab") {
|
|
111
|
+
e.preventDefault();
|
|
112
|
+
const textarea = textareaRef.current;
|
|
113
|
+
if (!textarea) return;
|
|
114
|
+
const start = textarea.selectionStart;
|
|
115
|
+
const end = textarea.selectionEnd;
|
|
116
|
+
const indent = " ";
|
|
117
|
+
const newValue = value.substring(0, start) + indent + value.substring(end);
|
|
118
|
+
onChange(newValue);
|
|
119
|
+
requestAnimationFrame(() => {
|
|
120
|
+
textarea.selectionStart = textarea.selectionEnd = start + indent.length;
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
if (e.key === "{" || e.key === "[" || e.key === '"') {
|
|
124
|
+
const textarea = textareaRef.current;
|
|
125
|
+
if (!textarea) return;
|
|
126
|
+
const start = textarea.selectionStart;
|
|
127
|
+
const end = textarea.selectionEnd;
|
|
128
|
+
const closeChar = e.key === "{" ? "}" : e.key === "[" ? "]" : '"';
|
|
129
|
+
if (start === end) {
|
|
130
|
+
e.preventDefault();
|
|
131
|
+
const newValue = value.substring(0, start) + e.key + closeChar + value.substring(end);
|
|
132
|
+
onChange(newValue);
|
|
133
|
+
requestAnimationFrame(() => {
|
|
134
|
+
textarea.selectionStart = textarea.selectionEnd = start + 1;
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
[value, onChange, readOnly]
|
|
140
|
+
);
|
|
141
|
+
const handleCursorMove = react.useCallback(() => {
|
|
142
|
+
const textarea = textareaRef.current;
|
|
143
|
+
if (!textarea) return;
|
|
144
|
+
const pos = textarea.selectionStart;
|
|
145
|
+
const textBefore = value.substring(0, pos);
|
|
146
|
+
const linesBefore = textBefore.split("\n");
|
|
147
|
+
const line = linesBefore.length;
|
|
148
|
+
const column = linesBefore[linesBefore.length - 1].length + 1;
|
|
149
|
+
onCursorChange({ line, column, offset: pos });
|
|
150
|
+
}, [value, onCursorChange]);
|
|
151
|
+
const highlightedLines = react.useMemo(() => {
|
|
152
|
+
return lines.map((line, idx) => {
|
|
153
|
+
const lineNum = idx + 1;
|
|
154
|
+
const isErrorLine = parseError?.line === lineNum;
|
|
155
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(
|
|
156
|
+
"div",
|
|
157
|
+
{
|
|
158
|
+
className: `mjr-code__line ${isErrorLine ? "mjr-code__line--error" : ""}`,
|
|
159
|
+
children: [
|
|
160
|
+
lineNumbers && /* @__PURE__ */ jsxRuntime.jsx("span", { className: "mjr-code__line-number", "aria-hidden": "true", children: lineNum }),
|
|
161
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { className: "mjr-code__line-content", children: highlightJsonLine(line) })
|
|
162
|
+
]
|
|
163
|
+
},
|
|
164
|
+
idx
|
|
165
|
+
);
|
|
166
|
+
});
|
|
167
|
+
}, [lines, lineNumbers, parseError]);
|
|
168
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: `mjr-code-editor ${className}`, children: [
|
|
169
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { className: "mjr-code__display", "aria-hidden": "true", children: highlightedLines }),
|
|
170
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
171
|
+
"textarea",
|
|
172
|
+
{
|
|
173
|
+
ref: textareaRef,
|
|
174
|
+
className: "mjr-code__textarea",
|
|
175
|
+
value,
|
|
176
|
+
onChange: handleChange,
|
|
177
|
+
onKeyDown: handleKeyDown,
|
|
178
|
+
onKeyUp: handleCursorMove,
|
|
179
|
+
onClick: handleCursorMove,
|
|
180
|
+
readOnly,
|
|
181
|
+
spellCheck: false,
|
|
182
|
+
autoCapitalize: "off",
|
|
183
|
+
autoComplete: "off",
|
|
184
|
+
autoCorrect: "off",
|
|
185
|
+
"aria-label": "JSON code editor",
|
|
186
|
+
"aria-multiline": "true",
|
|
187
|
+
"aria-readonly": readOnly,
|
|
188
|
+
"data-testid": "code-editor-textarea"
|
|
189
|
+
}
|
|
190
|
+
),
|
|
191
|
+
parseError && /* @__PURE__ */ jsxRuntime.jsxs(
|
|
192
|
+
"div",
|
|
193
|
+
{
|
|
194
|
+
className: "mjr-code__error-tooltip",
|
|
195
|
+
role: "alert",
|
|
196
|
+
style: { top: `${(parseError.line - 1) * 1.5}em` },
|
|
197
|
+
children: [
|
|
198
|
+
"Line ",
|
|
199
|
+
parseError.line,
|
|
200
|
+
", Col ",
|
|
201
|
+
parseError.column,
|
|
202
|
+
": ",
|
|
203
|
+
parseError.message
|
|
204
|
+
]
|
|
205
|
+
}
|
|
206
|
+
)
|
|
207
|
+
] });
|
|
208
|
+
};
|
|
209
|
+
function highlightJsonLine(line) {
|
|
210
|
+
const tokens = [];
|
|
211
|
+
let i = 0;
|
|
212
|
+
let key = 0;
|
|
213
|
+
while (i < line.length) {
|
|
214
|
+
const ch = line[i];
|
|
215
|
+
if (ch === " " || ch === " ") {
|
|
216
|
+
let ws = "";
|
|
217
|
+
while (i < line.length && (line[i] === " " || line[i] === " ")) {
|
|
218
|
+
ws += line[i];
|
|
219
|
+
i++;
|
|
220
|
+
}
|
|
221
|
+
tokens.push(/* @__PURE__ */ jsxRuntime.jsx("span", { children: ws }, key++));
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
if (ch === '"') {
|
|
225
|
+
let str = '"';
|
|
226
|
+
i++;
|
|
227
|
+
while (i < line.length) {
|
|
228
|
+
if (line[i] === "\\") {
|
|
229
|
+
str += line[i] + (line[i + 1] || "");
|
|
230
|
+
i += 2;
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
str += line[i];
|
|
234
|
+
if (line[i] === '"') {
|
|
235
|
+
i++;
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
i++;
|
|
239
|
+
}
|
|
240
|
+
const rest = line.substring(i).trimStart();
|
|
241
|
+
const isKey = rest.startsWith(":");
|
|
242
|
+
tokens.push(
|
|
243
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { className: isKey ? "mjr-syn-key" : "mjr-syn-string", children: str }, key++)
|
|
244
|
+
);
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
if (ch === "-" || ch >= "0" && ch <= "9") {
|
|
248
|
+
let num = "";
|
|
249
|
+
while (i < line.length && /[\d.eE+\-]/.test(line[i])) {
|
|
250
|
+
num += line[i];
|
|
251
|
+
i++;
|
|
252
|
+
}
|
|
253
|
+
tokens.push(
|
|
254
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { className: "mjr-syn-number", children: num }, key++)
|
|
255
|
+
);
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
if (line.substring(i, i + 4) === "true") {
|
|
259
|
+
tokens.push(/* @__PURE__ */ jsxRuntime.jsx("span", { className: "mjr-syn-boolean", children: "true" }, key++));
|
|
260
|
+
i += 4;
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
if (line.substring(i, i + 5) === "false") {
|
|
264
|
+
tokens.push(/* @__PURE__ */ jsxRuntime.jsx("span", { className: "mjr-syn-boolean", children: "false" }, key++));
|
|
265
|
+
i += 5;
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
if (line.substring(i, i + 4) === "null") {
|
|
269
|
+
tokens.push(/* @__PURE__ */ jsxRuntime.jsx("span", { className: "mjr-syn-null", children: "null" }, key++));
|
|
270
|
+
i += 4;
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
if (ch === "{" || ch === "}" || ch === "[" || ch === "]") {
|
|
274
|
+
tokens.push(/* @__PURE__ */ jsxRuntime.jsx("span", { className: "mjr-syn-bracket", children: ch }, key++));
|
|
275
|
+
i++;
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
tokens.push(/* @__PURE__ */ jsxRuntime.jsx("span", { className: "mjr-syn-punctuation", children: ch }, key++));
|
|
279
|
+
i++;
|
|
280
|
+
}
|
|
281
|
+
return tokens;
|
|
282
|
+
}
|
|
283
|
+
var TYPE_LABELS = {
|
|
284
|
+
string: "str",
|
|
285
|
+
number: "num",
|
|
286
|
+
boolean: "bool",
|
|
287
|
+
null: "null",
|
|
288
|
+
object: "obj",
|
|
289
|
+
array: "arr"
|
|
290
|
+
};
|
|
291
|
+
var TreeNodeComponent = ({
|
|
292
|
+
node,
|
|
293
|
+
onToggle,
|
|
294
|
+
onValueChange,
|
|
295
|
+
onKeyChange,
|
|
296
|
+
onDelete,
|
|
297
|
+
onTypeChange,
|
|
298
|
+
readOnly
|
|
299
|
+
}) => {
|
|
300
|
+
const [isEditing, setIsEditing] = react.useState(false);
|
|
301
|
+
const [editValue, setEditValue] = react.useState("");
|
|
302
|
+
const [isEditingKey, setIsEditingKey] = react.useState(false);
|
|
303
|
+
const [editKey, setEditKey] = react.useState("");
|
|
304
|
+
const isExpandable = node.type === "object" || node.type === "array";
|
|
305
|
+
const handleToggle = react.useCallback(() => {
|
|
306
|
+
if (isExpandable) {
|
|
307
|
+
onToggle(node.id);
|
|
308
|
+
}
|
|
309
|
+
}, [isExpandable, node.id, onToggle]);
|
|
310
|
+
const handleStartEdit = react.useCallback(() => {
|
|
311
|
+
if (readOnly || isExpandable) return;
|
|
312
|
+
setEditValue(node.type === "string" ? String(node.value) : JSON.stringify(node.value));
|
|
313
|
+
setIsEditing(true);
|
|
314
|
+
}, [readOnly, isExpandable, node.value, node.type]);
|
|
315
|
+
const handleFinishEdit = react.useCallback(() => {
|
|
316
|
+
setIsEditing(false);
|
|
317
|
+
let newValue;
|
|
318
|
+
if (node.type === "string") {
|
|
319
|
+
newValue = editValue;
|
|
320
|
+
} else if (node.type === "number") {
|
|
321
|
+
const num = Number(editValue);
|
|
322
|
+
newValue = isNaN(num) ? node.value : num;
|
|
323
|
+
} else if (node.type === "boolean") {
|
|
324
|
+
newValue = editValue === "true";
|
|
325
|
+
} else if (node.type === "null") {
|
|
326
|
+
newValue = null;
|
|
327
|
+
} else {
|
|
328
|
+
try {
|
|
329
|
+
newValue = JSON.parse(editValue);
|
|
330
|
+
} catch {
|
|
331
|
+
newValue = node.value;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
onValueChange(node.path, newValue);
|
|
335
|
+
}, [editValue, node.type, node.value, node.path, onValueChange]);
|
|
336
|
+
const handleKeyEdit = react.useCallback(() => {
|
|
337
|
+
if (readOnly || typeof node.key === "number") return;
|
|
338
|
+
setEditKey(String(node.key));
|
|
339
|
+
setIsEditingKey(true);
|
|
340
|
+
}, [readOnly, node.key]);
|
|
341
|
+
const handleFinishKeyEdit = react.useCallback(() => {
|
|
342
|
+
setIsEditingKey(false);
|
|
343
|
+
if (editKey !== String(node.key)) {
|
|
344
|
+
onKeyChange(node.path, String(node.key), editKey);
|
|
345
|
+
}
|
|
346
|
+
}, [editKey, node.key, node.path, onKeyChange]);
|
|
347
|
+
const handleTypeChange = react.useCallback(
|
|
348
|
+
(e) => {
|
|
349
|
+
const newType = e.target.value;
|
|
350
|
+
onTypeChange(node.path, newType);
|
|
351
|
+
},
|
|
352
|
+
[node.path, onTypeChange]
|
|
353
|
+
);
|
|
354
|
+
const renderValue = () => {
|
|
355
|
+
if (isEditing) {
|
|
356
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
357
|
+
"input",
|
|
358
|
+
{
|
|
359
|
+
className: "mjr-tree-node__edit-input",
|
|
360
|
+
value: editValue,
|
|
361
|
+
onChange: (e) => setEditValue(e.target.value),
|
|
362
|
+
onBlur: handleFinishEdit,
|
|
363
|
+
onKeyDown: (e) => {
|
|
364
|
+
if (e.key === "Enter") handleFinishEdit();
|
|
365
|
+
if (e.key === "Escape") setIsEditing(false);
|
|
366
|
+
},
|
|
367
|
+
autoFocus: true,
|
|
368
|
+
"aria-label": `Edit value for ${node.key}`,
|
|
369
|
+
"data-testid": `edit-value-${node.path}`
|
|
370
|
+
}
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
if (isExpandable) {
|
|
374
|
+
const symbol = node.type === "object" ? "{}" : "[]";
|
|
375
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("span", { className: "mjr-tree-node__preview", children: [
|
|
376
|
+
node.expanded ? "" : `${symbol}`,
|
|
377
|
+
!node.expanded && /* @__PURE__ */ jsxRuntime.jsxs("span", { className: "mjr-tree-node__count", children: [
|
|
378
|
+
"(",
|
|
379
|
+
node.childCount,
|
|
380
|
+
" ",
|
|
381
|
+
node.childCount === 1 ? "item" : "items",
|
|
382
|
+
")"
|
|
383
|
+
] })
|
|
384
|
+
] });
|
|
385
|
+
}
|
|
386
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
387
|
+
"span",
|
|
388
|
+
{
|
|
389
|
+
className: `mjr-tree-node__value mjr-tree-node__value--${node.type}`,
|
|
390
|
+
onDoubleClick: handleStartEdit,
|
|
391
|
+
role: "button",
|
|
392
|
+
tabIndex: 0,
|
|
393
|
+
"aria-label": `Value: ${formatDisplayValue(node.value, node.type)}. Double-click to edit.`,
|
|
394
|
+
"data-testid": `value-${node.path}`,
|
|
395
|
+
children: formatDisplayValue(node.value, node.type)
|
|
396
|
+
}
|
|
397
|
+
);
|
|
398
|
+
};
|
|
399
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(
|
|
400
|
+
"div",
|
|
401
|
+
{
|
|
402
|
+
className: "mjr-tree-node",
|
|
403
|
+
role: "treeitem",
|
|
404
|
+
"aria-expanded": isExpandable ? node.expanded : void 0,
|
|
405
|
+
"aria-level": node.depth + 1,
|
|
406
|
+
"aria-label": `${node.key}: ${node.type}`,
|
|
407
|
+
style: { paddingLeft: `${node.depth * 20}px` },
|
|
408
|
+
"data-testid": `tree-node-${node.path}`,
|
|
409
|
+
children: [
|
|
410
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
411
|
+
"span",
|
|
412
|
+
{
|
|
413
|
+
className: `mjr-tree-node__arrow ${isExpandable ? "mjr-tree-node__arrow--expandable" : ""} ${node.expanded ? "mjr-tree-node__arrow--expanded" : ""}`,
|
|
414
|
+
onClick: handleToggle,
|
|
415
|
+
"aria-hidden": "true",
|
|
416
|
+
children: isExpandable ? node.expanded ? "\u25BC" : "\u25B6" : "\xA0"
|
|
417
|
+
}
|
|
418
|
+
),
|
|
419
|
+
isEditingKey ? /* @__PURE__ */ jsxRuntime.jsx(
|
|
420
|
+
"input",
|
|
421
|
+
{
|
|
422
|
+
className: "mjr-tree-node__key-input",
|
|
423
|
+
value: editKey,
|
|
424
|
+
onChange: (e) => setEditKey(e.target.value),
|
|
425
|
+
onBlur: handleFinishKeyEdit,
|
|
426
|
+
onKeyDown: (e) => {
|
|
427
|
+
if (e.key === "Enter") handleFinishKeyEdit();
|
|
428
|
+
if (e.key === "Escape") setIsEditingKey(false);
|
|
429
|
+
},
|
|
430
|
+
autoFocus: true,
|
|
431
|
+
"aria-label": `Edit key name`
|
|
432
|
+
}
|
|
433
|
+
) : /* @__PURE__ */ jsxRuntime.jsx(
|
|
434
|
+
"span",
|
|
435
|
+
{
|
|
436
|
+
className: "mjr-tree-node__key",
|
|
437
|
+
onDoubleClick: handleKeyEdit,
|
|
438
|
+
"data-testid": `key-${node.path}`,
|
|
439
|
+
children: typeof node.key === "number" ? node.key : `"${node.key}"`
|
|
440
|
+
}
|
|
441
|
+
),
|
|
442
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { className: "mjr-tree-node__colon", "aria-hidden": "true", children: ":" }),
|
|
443
|
+
renderValue(),
|
|
444
|
+
!readOnly ? /* @__PURE__ */ jsxRuntime.jsx(
|
|
445
|
+
"select",
|
|
446
|
+
{
|
|
447
|
+
className: "mjr-tree-node__type-badge",
|
|
448
|
+
value: node.type,
|
|
449
|
+
onChange: handleTypeChange,
|
|
450
|
+
"aria-label": `Type for ${node.key}`,
|
|
451
|
+
"data-testid": `type-${node.path}`,
|
|
452
|
+
children: Object.entries(TYPE_LABELS).map(([type, label]) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: type, children: label }, type))
|
|
453
|
+
}
|
|
454
|
+
) : /* @__PURE__ */ jsxRuntime.jsx("span", { className: "mjr-tree-node__type-badge-ro", children: TYPE_LABELS[node.type] }),
|
|
455
|
+
!readOnly && /* @__PURE__ */ jsxRuntime.jsx(
|
|
456
|
+
"button",
|
|
457
|
+
{
|
|
458
|
+
className: "mjr-tree-node__delete",
|
|
459
|
+
onClick: () => onDelete(node.path),
|
|
460
|
+
"aria-label": `Delete ${node.key}`,
|
|
461
|
+
title: "Delete",
|
|
462
|
+
"data-testid": `delete-${node.path}`,
|
|
463
|
+
children: "\\u2715"
|
|
464
|
+
}
|
|
465
|
+
),
|
|
466
|
+
isExpandable && node.expanded && node.children.length > 0 && /* @__PURE__ */ jsxRuntime.jsx("div", { role: "group", className: "mjr-tree-node__children", children: node.children.map((child) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
467
|
+
TreeNodeComponent,
|
|
468
|
+
{
|
|
469
|
+
node: child,
|
|
470
|
+
onToggle,
|
|
471
|
+
onValueChange,
|
|
472
|
+
onKeyChange,
|
|
473
|
+
onDelete,
|
|
474
|
+
onTypeChange,
|
|
475
|
+
readOnly
|
|
476
|
+
},
|
|
477
|
+
child.id
|
|
478
|
+
)) })
|
|
479
|
+
]
|
|
480
|
+
}
|
|
481
|
+
);
|
|
482
|
+
};
|
|
483
|
+
function formatDisplayValue(value, type) {
|
|
484
|
+
if (type === "null") return "null";
|
|
485
|
+
if (type === "string") return `"${String(value)}"`;
|
|
486
|
+
if (type === "boolean") return String(value);
|
|
487
|
+
if (type === "number") return String(value);
|
|
488
|
+
return "";
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
// src/core/path.ts
|
|
492
|
+
function getByPath(obj, path) {
|
|
493
|
+
if (path === "$" || path === "") return obj;
|
|
494
|
+
const segments = parsePath(path);
|
|
495
|
+
let current = obj;
|
|
496
|
+
for (const segment of segments) {
|
|
497
|
+
if (current === null || current === void 0) return void 0;
|
|
498
|
+
if (Array.isArray(current)) {
|
|
499
|
+
const index = parseInt(segment, 10);
|
|
500
|
+
if (isNaN(index)) return void 0;
|
|
501
|
+
current = current[index];
|
|
502
|
+
} else if (typeof current === "object") {
|
|
503
|
+
current = current[segment];
|
|
504
|
+
} else {
|
|
505
|
+
return void 0;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
return current;
|
|
509
|
+
}
|
|
510
|
+
function setByPath(obj, path, value) {
|
|
511
|
+
if (path === "$" || path === "") return value;
|
|
512
|
+
const segments = parsePath(path);
|
|
513
|
+
return setBySegments(obj, segments, 0, value);
|
|
514
|
+
}
|
|
515
|
+
function setBySegments(current, segments, index, value) {
|
|
516
|
+
if (index === segments.length) return value;
|
|
517
|
+
const segment = segments[index];
|
|
518
|
+
if (Array.isArray(current)) {
|
|
519
|
+
const arrIndex = parseInt(segment, 10);
|
|
520
|
+
const newArr = [...current];
|
|
521
|
+
newArr[arrIndex] = setBySegments(current[arrIndex], segments, index + 1, value);
|
|
522
|
+
return newArr;
|
|
523
|
+
}
|
|
524
|
+
if (typeof current === "object" && current !== null) {
|
|
525
|
+
return {
|
|
526
|
+
...current,
|
|
527
|
+
[segment]: setBySegments(
|
|
528
|
+
current[segment],
|
|
529
|
+
segments,
|
|
530
|
+
index + 1,
|
|
531
|
+
value
|
|
532
|
+
)
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
const nextSegment = segments[index + 1];
|
|
536
|
+
const isArrayIndex = nextSegment !== void 0 && /^\d+$/.test(nextSegment);
|
|
537
|
+
const container = isArrayIndex ? [] : {};
|
|
538
|
+
return {
|
|
539
|
+
[segment]: setBySegments(container, segments, index + 1, value)
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
function deleteByPath(obj, path) {
|
|
543
|
+
if (path === "$" || path === "") return void 0;
|
|
544
|
+
const segments = parsePath(path);
|
|
545
|
+
return deleteBySegments(obj, segments, 0);
|
|
546
|
+
}
|
|
547
|
+
function deleteBySegments(current, segments, index) {
|
|
548
|
+
if (index === segments.length - 1) {
|
|
549
|
+
const segment2 = segments[index];
|
|
550
|
+
if (Array.isArray(current)) {
|
|
551
|
+
const arrIndex = parseInt(segment2, 10);
|
|
552
|
+
return current.filter((_, i) => i !== arrIndex);
|
|
553
|
+
}
|
|
554
|
+
if (typeof current === "object" && current !== null) {
|
|
555
|
+
const { [segment2]: _, ...rest } = current;
|
|
556
|
+
return rest;
|
|
557
|
+
}
|
|
558
|
+
return current;
|
|
559
|
+
}
|
|
560
|
+
const segment = segments[index];
|
|
561
|
+
if (Array.isArray(current)) {
|
|
562
|
+
const arrIndex = parseInt(segment, 10);
|
|
563
|
+
const newArr = [...current];
|
|
564
|
+
newArr[arrIndex] = deleteBySegments(current[arrIndex], segments, index + 1);
|
|
565
|
+
return newArr;
|
|
566
|
+
}
|
|
567
|
+
if (typeof current === "object" && current !== null) {
|
|
568
|
+
return {
|
|
569
|
+
...current,
|
|
570
|
+
[segment]: deleteBySegments(
|
|
571
|
+
current[segment],
|
|
572
|
+
segments,
|
|
573
|
+
index + 1
|
|
574
|
+
)
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
return current;
|
|
578
|
+
}
|
|
579
|
+
function parsePath(path) {
|
|
580
|
+
let normalized = path.replace(/^\$\.?/, "");
|
|
581
|
+
normalized = normalized.replace(/\[(\d+)\]/g, ".$1");
|
|
582
|
+
normalized = normalized.replace(/\["([^"]+)"\]/g, ".$1");
|
|
583
|
+
normalized = normalized.replace(/\['([^']+)'\]/g, ".$1");
|
|
584
|
+
return normalized.split(".").filter((s) => s !== "");
|
|
585
|
+
}
|
|
586
|
+
function buildPath(segments) {
|
|
587
|
+
if (segments.length === 0) return "$";
|
|
588
|
+
return "$." + segments.map((s) => typeof s === "number" ? `[${s}]` : s).join(".");
|
|
589
|
+
}
|
|
590
|
+
var TreeEditor = ({
|
|
591
|
+
value,
|
|
592
|
+
onChange,
|
|
593
|
+
readOnly,
|
|
594
|
+
className = ""
|
|
595
|
+
}) => {
|
|
596
|
+
const [expandedPaths, setExpandedPaths] = react.useState(/* @__PURE__ */ new Set(["$"]));
|
|
597
|
+
const tree = react.useMemo(() => {
|
|
598
|
+
return buildTree(value, "$", "root", 0, expandedPaths);
|
|
599
|
+
}, [value, expandedPaths]);
|
|
600
|
+
const handleToggle = react.useCallback((id) => {
|
|
601
|
+
setExpandedPaths((prev) => {
|
|
602
|
+
const next = new Set(prev);
|
|
603
|
+
const path = id;
|
|
604
|
+
if (next.has(path)) {
|
|
605
|
+
next.delete(path);
|
|
606
|
+
} else {
|
|
607
|
+
next.add(path);
|
|
608
|
+
}
|
|
609
|
+
return next;
|
|
610
|
+
});
|
|
611
|
+
}, []);
|
|
612
|
+
const handleValueChange = react.useCallback(
|
|
613
|
+
(path, newValue) => {
|
|
614
|
+
const updated = setByPath(value, path, newValue);
|
|
615
|
+
onChange(updated);
|
|
616
|
+
},
|
|
617
|
+
[value, onChange]
|
|
618
|
+
);
|
|
619
|
+
const handleKeyChange = react.useCallback(
|
|
620
|
+
(path, oldKey, newKey) => {
|
|
621
|
+
const segments = path.split(".");
|
|
622
|
+
const parentPath = segments.slice(0, -1).join(".") || "$";
|
|
623
|
+
const currentValue = getNestedValue(value, path);
|
|
624
|
+
let updated = deleteByPath(value, path);
|
|
625
|
+
const newPath = parentPath === "$" ? `$.${newKey}` : `${parentPath}.${newKey}`;
|
|
626
|
+
updated = setByPath(updated, newPath, currentValue);
|
|
627
|
+
onChange(updated);
|
|
628
|
+
},
|
|
629
|
+
[value, onChange]
|
|
630
|
+
);
|
|
631
|
+
const handleDelete = react.useCallback(
|
|
632
|
+
(path) => {
|
|
633
|
+
const updated = deleteByPath(value, path);
|
|
634
|
+
onChange(updated);
|
|
635
|
+
},
|
|
636
|
+
[value, onChange]
|
|
637
|
+
);
|
|
638
|
+
const handleTypeChange = react.useCallback(
|
|
639
|
+
(path, newType) => {
|
|
640
|
+
const defaults = {
|
|
641
|
+
string: "",
|
|
642
|
+
number: 0,
|
|
643
|
+
boolean: false,
|
|
644
|
+
null: null,
|
|
645
|
+
object: {},
|
|
646
|
+
array: []
|
|
647
|
+
};
|
|
648
|
+
const updated = setByPath(value, path, defaults[newType]);
|
|
649
|
+
onChange(updated);
|
|
650
|
+
},
|
|
651
|
+
[value, onChange]
|
|
652
|
+
);
|
|
653
|
+
const handleAddProperty = react.useCallback(() => {
|
|
654
|
+
if (value === void 0 || value === null) {
|
|
655
|
+
onChange({});
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
if (typeof value === "object" && !Array.isArray(value)) {
|
|
659
|
+
const obj = value;
|
|
660
|
+
let newKey = "newKey";
|
|
661
|
+
let counter = 1;
|
|
662
|
+
while (newKey in obj) {
|
|
663
|
+
newKey = `newKey${counter++}`;
|
|
664
|
+
}
|
|
665
|
+
onChange({ ...obj, [newKey]: "" });
|
|
666
|
+
} else if (Array.isArray(value)) {
|
|
667
|
+
onChange([...value, ""]);
|
|
668
|
+
}
|
|
669
|
+
}, [value, onChange]);
|
|
670
|
+
if (tree === null) {
|
|
671
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: `mjr-tree-editor mjr-tree-editor--empty ${className}`, children: [
|
|
672
|
+
/* @__PURE__ */ jsxRuntime.jsx("p", { className: "mjr-tree-editor__empty-msg", children: "No valid JSON to display" }),
|
|
673
|
+
!readOnly && /* @__PURE__ */ jsxRuntime.jsx("button", { className: "mjr-tree-editor__add-btn", onClick: () => onChange({}), children: "+ Create empty object" })
|
|
674
|
+
] });
|
|
675
|
+
}
|
|
676
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(
|
|
677
|
+
"div",
|
|
678
|
+
{
|
|
679
|
+
className: `mjr-tree-editor ${className}`,
|
|
680
|
+
role: "tree",
|
|
681
|
+
"aria-label": "JSON tree editor",
|
|
682
|
+
"data-testid": "tree-editor",
|
|
683
|
+
children: [
|
|
684
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
685
|
+
TreeNodeComponent,
|
|
686
|
+
{
|
|
687
|
+
node: tree,
|
|
688
|
+
onToggle: handleToggle,
|
|
689
|
+
onValueChange: handleValueChange,
|
|
690
|
+
onKeyChange: handleKeyChange,
|
|
691
|
+
onDelete: handleDelete,
|
|
692
|
+
onTypeChange: handleTypeChange,
|
|
693
|
+
readOnly
|
|
694
|
+
}
|
|
695
|
+
),
|
|
696
|
+
!readOnly && /* @__PURE__ */ jsxRuntime.jsx(
|
|
697
|
+
"button",
|
|
698
|
+
{
|
|
699
|
+
className: "mjr-tree-editor__add-root",
|
|
700
|
+
onClick: handleAddProperty,
|
|
701
|
+
"data-testid": "add-property",
|
|
702
|
+
children: "+ Add property"
|
|
703
|
+
}
|
|
704
|
+
)
|
|
705
|
+
]
|
|
706
|
+
}
|
|
707
|
+
);
|
|
708
|
+
};
|
|
709
|
+
function buildTree(value, path, key, depth, expandedPaths) {
|
|
710
|
+
if (value === void 0) return null;
|
|
711
|
+
const type = getType(value);
|
|
712
|
+
const expanded = expandedPaths.has(path);
|
|
713
|
+
const id = path;
|
|
714
|
+
const node = {
|
|
715
|
+
id,
|
|
716
|
+
key,
|
|
717
|
+
value,
|
|
718
|
+
type,
|
|
719
|
+
path,
|
|
720
|
+
depth,
|
|
721
|
+
expanded,
|
|
722
|
+
children: [],
|
|
723
|
+
childCount: 0
|
|
724
|
+
};
|
|
725
|
+
if (type === "object" && value !== null) {
|
|
726
|
+
const obj = value;
|
|
727
|
+
const keys = Object.keys(obj);
|
|
728
|
+
node.childCount = keys.length;
|
|
729
|
+
if (expanded) {
|
|
730
|
+
node.children = keys.map(
|
|
731
|
+
(k) => buildTree(obj[k], `${path}.${k}`, k, depth + 1, expandedPaths)
|
|
732
|
+
).filter(Boolean);
|
|
733
|
+
}
|
|
734
|
+
} else if (type === "array") {
|
|
735
|
+
const arr = value;
|
|
736
|
+
node.childCount = arr.length;
|
|
737
|
+
if (expanded) {
|
|
738
|
+
node.children = arr.map(
|
|
739
|
+
(item, i) => buildTree(item, `${path}[${i}]`, i, depth + 1, expandedPaths)
|
|
740
|
+
).filter(Boolean);
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
return node;
|
|
744
|
+
}
|
|
745
|
+
function getType(value) {
|
|
746
|
+
if (value === null) return "null";
|
|
747
|
+
if (Array.isArray(value)) return "array";
|
|
748
|
+
const t = typeof value;
|
|
749
|
+
if (t === "string") return "string";
|
|
750
|
+
if (t === "number") return "number";
|
|
751
|
+
if (t === "boolean") return "boolean";
|
|
752
|
+
if (t === "object") return "object";
|
|
753
|
+
return "string";
|
|
754
|
+
}
|
|
755
|
+
function getNestedValue(obj, path) {
|
|
756
|
+
const segments = path.replace(/^\$\.?/, "").replace(/\[(\d+)\]/g, ".$1").split(".").filter(Boolean);
|
|
757
|
+
let current = obj;
|
|
758
|
+
for (const seg of segments) {
|
|
759
|
+
if (current === null || current === void 0) return void 0;
|
|
760
|
+
if (Array.isArray(current)) {
|
|
761
|
+
current = current[parseInt(seg, 10)];
|
|
762
|
+
} else if (typeof current === "object") {
|
|
763
|
+
current = current[seg];
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
return current;
|
|
767
|
+
}
|
|
768
|
+
var StatusBar = ({
|
|
769
|
+
parseError,
|
|
770
|
+
validationErrors,
|
|
771
|
+
cursor,
|
|
772
|
+
stats,
|
|
773
|
+
className = ""
|
|
774
|
+
}) => {
|
|
775
|
+
const hasErrors = parseError !== null;
|
|
776
|
+
const hasWarnings = validationErrors.length > 0;
|
|
777
|
+
let statusIcon;
|
|
778
|
+
let statusText;
|
|
779
|
+
let statusClass;
|
|
780
|
+
if (hasErrors) {
|
|
781
|
+
statusIcon = "\u2715";
|
|
782
|
+
statusText = `Invalid JSON (line ${parseError.line})`;
|
|
783
|
+
statusClass = "mjr-status--error";
|
|
784
|
+
} else if (hasWarnings) {
|
|
785
|
+
statusIcon = "\u26A0";
|
|
786
|
+
statusText = `${validationErrors.length} validation ${validationErrors.length === 1 ? "issue" : "issues"}`;
|
|
787
|
+
statusClass = "mjr-status--warning";
|
|
788
|
+
} else {
|
|
789
|
+
statusIcon = "\u2713";
|
|
790
|
+
statusText = "Valid JSON";
|
|
791
|
+
statusClass = "mjr-status--valid";
|
|
792
|
+
}
|
|
793
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(
|
|
794
|
+
"div",
|
|
795
|
+
{
|
|
796
|
+
className: `mjr-status-bar ${statusClass} ${className}`,
|
|
797
|
+
role: "status",
|
|
798
|
+
"aria-live": "polite",
|
|
799
|
+
"aria-atomic": "true",
|
|
800
|
+
children: [
|
|
801
|
+
/* @__PURE__ */ jsxRuntime.jsxs("span", { className: "mjr-status-bar__indicator", children: [
|
|
802
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { className: "mjr-status-bar__icon", "aria-hidden": "true", children: statusIcon }),
|
|
803
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { className: "mjr-status-bar__text", children: statusText })
|
|
804
|
+
] }),
|
|
805
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { className: "mjr-status-bar__separator", "aria-hidden": "true", children: "|" }),
|
|
806
|
+
/* @__PURE__ */ jsxRuntime.jsxs("span", { className: "mjr-status-bar__cursor", children: [
|
|
807
|
+
"Ln ",
|
|
808
|
+
cursor.line,
|
|
809
|
+
", Col ",
|
|
810
|
+
cursor.column
|
|
811
|
+
] }),
|
|
812
|
+
stats && /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
813
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { className: "mjr-status-bar__separator", "aria-hidden": "true", children: "|" }),
|
|
814
|
+
/* @__PURE__ */ jsxRuntime.jsxs("span", { className: "mjr-status-bar__stats", children: [
|
|
815
|
+
stats.properties,
|
|
816
|
+
" ",
|
|
817
|
+
stats.properties === 1 ? "property" : "properties",
|
|
818
|
+
stats.arrays > 0 && `, ${stats.arrays} ${stats.arrays === 1 ? "array" : "arrays"}`
|
|
819
|
+
] })
|
|
820
|
+
] }),
|
|
821
|
+
stats && stats.byteSize > 0 && /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
822
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { className: "mjr-status-bar__separator", "aria-hidden": "true", children: "|" }),
|
|
823
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { className: "mjr-status-bar__size", children: formatBytes(stats.byteSize) })
|
|
824
|
+
] })
|
|
825
|
+
]
|
|
826
|
+
}
|
|
827
|
+
);
|
|
828
|
+
};
|
|
829
|
+
function formatBytes(bytes) {
|
|
830
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
831
|
+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
|
832
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
// src/core/parser.ts
|
|
836
|
+
function parseJson(text) {
|
|
837
|
+
if (text.trim() === "") {
|
|
838
|
+
return { value: void 0, error: null };
|
|
839
|
+
}
|
|
840
|
+
try {
|
|
841
|
+
const value = JSON.parse(text);
|
|
842
|
+
return { value, error: null };
|
|
843
|
+
} catch (e) {
|
|
844
|
+
const error = extractParseError(e, text);
|
|
845
|
+
return { value: void 0, error };
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
function extractParseError(err, text) {
|
|
849
|
+
const message = err.message;
|
|
850
|
+
let offset = -1;
|
|
851
|
+
let line = 1;
|
|
852
|
+
let column = 1;
|
|
853
|
+
const posMatch = message.match(/at position (\d+)/);
|
|
854
|
+
const lineColMatch = message.match(/at line (\d+) column (\d+)/);
|
|
855
|
+
if (lineColMatch) {
|
|
856
|
+
line = parseInt(lineColMatch[1], 10);
|
|
857
|
+
column = parseInt(lineColMatch[2], 10);
|
|
858
|
+
offset = getOffsetFromLineCol(text, line, column);
|
|
859
|
+
} else if (posMatch) {
|
|
860
|
+
offset = parseInt(posMatch[1], 10);
|
|
861
|
+
const loc = getLineColFromOffset(text, offset);
|
|
862
|
+
line = loc.line;
|
|
863
|
+
column = loc.column;
|
|
864
|
+
}
|
|
865
|
+
const cleanMessage = message.replace(/^JSON\.parse: /, "").replace(/ at position \d+.*$/, "").replace(/ at line \d+ column \d+.*$/, "");
|
|
866
|
+
return {
|
|
867
|
+
message: cleanMessage || "Invalid JSON",
|
|
868
|
+
line,
|
|
869
|
+
column,
|
|
870
|
+
offset: Math.max(0, offset)
|
|
871
|
+
};
|
|
872
|
+
}
|
|
873
|
+
function getLineColFromOffset(text, offset) {
|
|
874
|
+
let line = 1;
|
|
875
|
+
let lastNewline = -1;
|
|
876
|
+
for (let i = 0; i < offset && i < text.length; i++) {
|
|
877
|
+
if (text[i] === "\n") {
|
|
878
|
+
line++;
|
|
879
|
+
lastNewline = i;
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
return { line, column: offset - lastNewline };
|
|
883
|
+
}
|
|
884
|
+
function getOffsetFromLineCol(text, line, column) {
|
|
885
|
+
let currentLine = 1;
|
|
886
|
+
for (let i = 0; i < text.length; i++) {
|
|
887
|
+
if (currentLine === line) {
|
|
888
|
+
return i + column - 1;
|
|
889
|
+
}
|
|
890
|
+
if (text[i] === "\n") {
|
|
891
|
+
currentLine++;
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
return text.length;
|
|
895
|
+
}
|
|
896
|
+
function stringifyJson(value, indent = 2) {
|
|
897
|
+
if (value === void 0) return "";
|
|
898
|
+
try {
|
|
899
|
+
return JSON.stringify(value, null, indent);
|
|
900
|
+
} catch {
|
|
901
|
+
return "";
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
function isValidJson(text) {
|
|
905
|
+
try {
|
|
906
|
+
JSON.parse(text);
|
|
907
|
+
return true;
|
|
908
|
+
} catch {
|
|
909
|
+
return false;
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
// src/core/validator.ts
|
|
914
|
+
function validateSchema(value, schema, path = "$") {
|
|
915
|
+
const errors = [];
|
|
916
|
+
if (value === void 0 || value === null) {
|
|
917
|
+
if (schema.type && schema.type !== "null") {
|
|
918
|
+
errors.push(makeError(`Expected type "${schema.type}", got null`, path, "type", schema.type));
|
|
919
|
+
}
|
|
920
|
+
return { valid: errors.length === 0, errors };
|
|
921
|
+
}
|
|
922
|
+
if (schema.type) {
|
|
923
|
+
const actualType = getJsonType(value);
|
|
924
|
+
const allowedTypes = Array.isArray(schema.type) ? schema.type : [schema.type];
|
|
925
|
+
const typeMatches = allowedTypes.includes(actualType) || actualType === "integer" && allowedTypes.includes("number");
|
|
926
|
+
if (!typeMatches) {
|
|
927
|
+
errors.push(
|
|
928
|
+
makeError(
|
|
929
|
+
`Expected type "${allowedTypes.join(" | ")}", got "${actualType}"`,
|
|
930
|
+
path,
|
|
931
|
+
"type",
|
|
932
|
+
schema.type,
|
|
933
|
+
value
|
|
934
|
+
)
|
|
935
|
+
);
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
if (schema.enum && Array.isArray(schema.enum)) {
|
|
939
|
+
if (!schema.enum.some((e) => deepEqual(e, value))) {
|
|
940
|
+
errors.push(
|
|
941
|
+
makeError(
|
|
942
|
+
`Value must be one of: ${schema.enum.map((e) => JSON.stringify(e)).join(", ")}`,
|
|
943
|
+
path,
|
|
944
|
+
"enum",
|
|
945
|
+
schema.enum,
|
|
946
|
+
value
|
|
947
|
+
)
|
|
948
|
+
);
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
if (typeof value === "string") {
|
|
952
|
+
if (schema.minLength !== void 0 && value.length < schema.minLength) {
|
|
953
|
+
errors.push(
|
|
954
|
+
makeError(
|
|
955
|
+
`String must be at least ${schema.minLength} characters`,
|
|
956
|
+
path,
|
|
957
|
+
"minLength",
|
|
958
|
+
schema.minLength,
|
|
959
|
+
value
|
|
960
|
+
)
|
|
961
|
+
);
|
|
962
|
+
}
|
|
963
|
+
if (schema.maxLength !== void 0 && value.length > schema.maxLength) {
|
|
964
|
+
errors.push(
|
|
965
|
+
makeError(
|
|
966
|
+
`String must be at most ${schema.maxLength} characters`,
|
|
967
|
+
path,
|
|
968
|
+
"maxLength",
|
|
969
|
+
schema.maxLength,
|
|
970
|
+
value
|
|
971
|
+
)
|
|
972
|
+
);
|
|
973
|
+
}
|
|
974
|
+
if (schema.pattern) {
|
|
975
|
+
const re = new RegExp(schema.pattern);
|
|
976
|
+
if (!re.test(value)) {
|
|
977
|
+
errors.push(
|
|
978
|
+
makeError(`String must match pattern "${schema.pattern}"`, path, "pattern", schema.pattern, value)
|
|
979
|
+
);
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
if (typeof value === "number") {
|
|
984
|
+
if (schema.minimum !== void 0 && value < schema.minimum) {
|
|
985
|
+
errors.push(
|
|
986
|
+
makeError(`Value must be >= ${schema.minimum}`, path, "minimum", schema.minimum, value)
|
|
987
|
+
);
|
|
988
|
+
}
|
|
989
|
+
if (schema.maximum !== void 0 && value > schema.maximum) {
|
|
990
|
+
errors.push(
|
|
991
|
+
makeError(`Value must be <= ${schema.maximum}`, path, "maximum", schema.maximum, value)
|
|
992
|
+
);
|
|
993
|
+
}
|
|
994
|
+
if (schema.exclusiveMinimum !== void 0 && value <= schema.exclusiveMinimum) {
|
|
995
|
+
errors.push(
|
|
996
|
+
makeError(
|
|
997
|
+
`Value must be > ${schema.exclusiveMinimum}`,
|
|
998
|
+
path,
|
|
999
|
+
"exclusiveMinimum",
|
|
1000
|
+
schema.exclusiveMinimum,
|
|
1001
|
+
value
|
|
1002
|
+
)
|
|
1003
|
+
);
|
|
1004
|
+
}
|
|
1005
|
+
if (schema.exclusiveMaximum !== void 0 && value >= schema.exclusiveMaximum) {
|
|
1006
|
+
errors.push(
|
|
1007
|
+
makeError(
|
|
1008
|
+
`Value must be < ${schema.exclusiveMaximum}`,
|
|
1009
|
+
path,
|
|
1010
|
+
"exclusiveMaximum",
|
|
1011
|
+
schema.exclusiveMaximum,
|
|
1012
|
+
value
|
|
1013
|
+
)
|
|
1014
|
+
);
|
|
1015
|
+
}
|
|
1016
|
+
if (schema.multipleOf !== void 0 && value % schema.multipleOf !== 0) {
|
|
1017
|
+
errors.push(
|
|
1018
|
+
makeError(`Value must be a multiple of ${schema.multipleOf}`, path, "multipleOf", schema.multipleOf, value)
|
|
1019
|
+
);
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
if (Array.isArray(value)) {
|
|
1023
|
+
if (schema.minItems !== void 0 && value.length < schema.minItems) {
|
|
1024
|
+
errors.push(
|
|
1025
|
+
makeError(`Array must have at least ${schema.minItems} items`, path, "minItems", schema.minItems, value)
|
|
1026
|
+
);
|
|
1027
|
+
}
|
|
1028
|
+
if (schema.maxItems !== void 0 && value.length > schema.maxItems) {
|
|
1029
|
+
errors.push(
|
|
1030
|
+
makeError(`Array must have at most ${schema.maxItems} items`, path, "maxItems", schema.maxItems, value)
|
|
1031
|
+
);
|
|
1032
|
+
}
|
|
1033
|
+
if (schema.uniqueItems && new Set(value.map((v) => JSON.stringify(v))).size !== value.length) {
|
|
1034
|
+
errors.push(makeError("Array items must be unique", path, "uniqueItems", true, value));
|
|
1035
|
+
}
|
|
1036
|
+
if (schema.items && !Array.isArray(schema.items)) {
|
|
1037
|
+
value.forEach((item, index) => {
|
|
1038
|
+
const result = validateSchema(item, schema.items, `${path}[${index}]`);
|
|
1039
|
+
errors.push(...result.errors);
|
|
1040
|
+
});
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
if (isPlainObject(value)) {
|
|
1044
|
+
const obj = value;
|
|
1045
|
+
const keys = Object.keys(obj);
|
|
1046
|
+
if (schema.required && Array.isArray(schema.required)) {
|
|
1047
|
+
for (const req of schema.required) {
|
|
1048
|
+
if (!(req in obj)) {
|
|
1049
|
+
errors.push(
|
|
1050
|
+
makeError(`Missing required property "${req}"`, path, "required", schema.required)
|
|
1051
|
+
);
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
if (schema.minProperties !== void 0 && keys.length < schema.minProperties) {
|
|
1056
|
+
errors.push(
|
|
1057
|
+
makeError(
|
|
1058
|
+
`Object must have at least ${schema.minProperties} properties`,
|
|
1059
|
+
path,
|
|
1060
|
+
"minProperties",
|
|
1061
|
+
schema.minProperties,
|
|
1062
|
+
value
|
|
1063
|
+
)
|
|
1064
|
+
);
|
|
1065
|
+
}
|
|
1066
|
+
if (schema.maxProperties !== void 0 && keys.length > schema.maxProperties) {
|
|
1067
|
+
errors.push(
|
|
1068
|
+
makeError(
|
|
1069
|
+
`Object must have at most ${schema.maxProperties} properties`,
|
|
1070
|
+
path,
|
|
1071
|
+
"maxProperties",
|
|
1072
|
+
schema.maxProperties,
|
|
1073
|
+
value
|
|
1074
|
+
)
|
|
1075
|
+
);
|
|
1076
|
+
}
|
|
1077
|
+
if (schema.properties) {
|
|
1078
|
+
const props = schema.properties;
|
|
1079
|
+
for (const key of keys) {
|
|
1080
|
+
if (props[key]) {
|
|
1081
|
+
const result = validateSchema(obj[key], props[key], `${path}.${key}`);
|
|
1082
|
+
errors.push(...result.errors);
|
|
1083
|
+
} else if (schema.additionalProperties === false) {
|
|
1084
|
+
errors.push(
|
|
1085
|
+
makeError(
|
|
1086
|
+
`Unexpected property "${key}"`,
|
|
1087
|
+
`${path}.${key}`,
|
|
1088
|
+
"additionalProperties",
|
|
1089
|
+
false,
|
|
1090
|
+
obj[key]
|
|
1091
|
+
)
|
|
1092
|
+
);
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
return { valid: errors.length === 0, errors };
|
|
1098
|
+
}
|
|
1099
|
+
async function runCustomValidators(value, validators, path = "$") {
|
|
1100
|
+
const results = await Promise.all(validators.map((v) => v(value, path)));
|
|
1101
|
+
return results.flat();
|
|
1102
|
+
}
|
|
1103
|
+
function getJsonType(value) {
|
|
1104
|
+
if (value === null) return "null";
|
|
1105
|
+
if (Array.isArray(value)) return "array";
|
|
1106
|
+
if (typeof value === "number") {
|
|
1107
|
+
return Number.isInteger(value) ? "integer" : "number";
|
|
1108
|
+
}
|
|
1109
|
+
return typeof value;
|
|
1110
|
+
}
|
|
1111
|
+
function makeError(message, path, schemaKeyword, schemaRule, actualValue, severity = "error") {
|
|
1112
|
+
return { message, path, severity, schemaKeyword, schemaRule, actualValue };
|
|
1113
|
+
}
|
|
1114
|
+
function isPlainObject(val) {
|
|
1115
|
+
return typeof val === "object" && val !== null && !Array.isArray(val);
|
|
1116
|
+
}
|
|
1117
|
+
function deepEqual(a, b) {
|
|
1118
|
+
if (a === b) return true;
|
|
1119
|
+
if (typeof a !== typeof b) return false;
|
|
1120
|
+
if (a === null || b === null) return false;
|
|
1121
|
+
if (typeof a !== "object") return false;
|
|
1122
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
// src/hooks/useJsonParser.ts
|
|
1126
|
+
function useJsonParser(initialValue, options = {}) {
|
|
1127
|
+
const { schema, validators, debounce = 300 } = options;
|
|
1128
|
+
const [text, setText] = react.useState(
|
|
1129
|
+
() => initialValue !== void 0 ? stringifyJson(initialValue) : ""
|
|
1130
|
+
);
|
|
1131
|
+
const [parsedValue, setParsedValue] = react.useState(initialValue);
|
|
1132
|
+
const [parseError, setParseError] = react.useState(null);
|
|
1133
|
+
const [validationErrors, setValidationErrors] = react.useState([]);
|
|
1134
|
+
const debounceTimer = react.useRef();
|
|
1135
|
+
react.useEffect(() => {
|
|
1136
|
+
const result = parseJson(text);
|
|
1137
|
+
setParsedValue(result.value);
|
|
1138
|
+
setParseError(result.error);
|
|
1139
|
+
if (!result.error && result.value !== void 0) {
|
|
1140
|
+
if (debounceTimer.current) clearTimeout(debounceTimer.current);
|
|
1141
|
+
debounceTimer.current = setTimeout(async () => {
|
|
1142
|
+
let errors = [];
|
|
1143
|
+
if (schema) {
|
|
1144
|
+
const schemaResult = validateSchema(result.value, schema);
|
|
1145
|
+
errors = [...schemaResult.errors];
|
|
1146
|
+
}
|
|
1147
|
+
if (validators && validators.length > 0) {
|
|
1148
|
+
const customErrors = await runCustomValidators(result.value, validators);
|
|
1149
|
+
errors = [...errors, ...customErrors];
|
|
1150
|
+
}
|
|
1151
|
+
setValidationErrors(errors);
|
|
1152
|
+
}, debounce);
|
|
1153
|
+
} else {
|
|
1154
|
+
setValidationErrors([]);
|
|
1155
|
+
}
|
|
1156
|
+
return () => {
|
|
1157
|
+
if (debounceTimer.current) clearTimeout(debounceTimer.current);
|
|
1158
|
+
};
|
|
1159
|
+
}, [text, schema, validators, debounce]);
|
|
1160
|
+
const handleSetText = react.useCallback((newText) => {
|
|
1161
|
+
setText(newText);
|
|
1162
|
+
}, []);
|
|
1163
|
+
const handleSetValue = react.useCallback((value) => {
|
|
1164
|
+
const newText = stringifyJson(value);
|
|
1165
|
+
setText(newText);
|
|
1166
|
+
}, []);
|
|
1167
|
+
const format = react.useCallback(
|
|
1168
|
+
(indent = 2) => {
|
|
1169
|
+
if (parsedValue !== void 0) {
|
|
1170
|
+
setText(stringifyJson(parsedValue, indent));
|
|
1171
|
+
}
|
|
1172
|
+
},
|
|
1173
|
+
[parsedValue]
|
|
1174
|
+
);
|
|
1175
|
+
return {
|
|
1176
|
+
text,
|
|
1177
|
+
parsedValue,
|
|
1178
|
+
parseError,
|
|
1179
|
+
validationErrors,
|
|
1180
|
+
isValid: parseError === null && validationErrors.length === 0,
|
|
1181
|
+
setText: handleSetText,
|
|
1182
|
+
setValue: handleSetValue,
|
|
1183
|
+
format
|
|
1184
|
+
};
|
|
1185
|
+
}
|
|
1186
|
+
function useUndoRedo(initialValue, options = {}) {
|
|
1187
|
+
const { maxHistory = 100, groupingInterval = 300 } = options;
|
|
1188
|
+
const historyRef = react.useRef([initialValue]);
|
|
1189
|
+
const positionRef = react.useRef(0);
|
|
1190
|
+
const lastUpdateTime = react.useRef(0);
|
|
1191
|
+
const [, forceUpdate] = react.useState(0);
|
|
1192
|
+
const rerender = react.useCallback(() => forceUpdate((n) => n + 1), []);
|
|
1193
|
+
const current = historyRef.current[positionRef.current];
|
|
1194
|
+
const set = react.useCallback(
|
|
1195
|
+
(value) => {
|
|
1196
|
+
const now = Date.now();
|
|
1197
|
+
const shouldGroup = now - lastUpdateTime.current < groupingInterval;
|
|
1198
|
+
lastUpdateTime.current = now;
|
|
1199
|
+
const prev = historyRef.current;
|
|
1200
|
+
const pos = positionRef.current;
|
|
1201
|
+
let newHistory;
|
|
1202
|
+
let newPos;
|
|
1203
|
+
if (shouldGroup && pos > 0) {
|
|
1204
|
+
newHistory = [...prev.slice(0, pos), value];
|
|
1205
|
+
newPos = pos;
|
|
1206
|
+
} else {
|
|
1207
|
+
newHistory = [...prev.slice(0, pos + 1), value];
|
|
1208
|
+
newPos = pos + 1;
|
|
1209
|
+
}
|
|
1210
|
+
if (newHistory.length > maxHistory) {
|
|
1211
|
+
const trimCount = newHistory.length - maxHistory;
|
|
1212
|
+
newHistory = newHistory.slice(trimCount);
|
|
1213
|
+
newPos = newPos - trimCount;
|
|
1214
|
+
}
|
|
1215
|
+
historyRef.current = newHistory;
|
|
1216
|
+
positionRef.current = Math.max(0, newPos);
|
|
1217
|
+
rerender();
|
|
1218
|
+
},
|
|
1219
|
+
[maxHistory, groupingInterval, rerender]
|
|
1220
|
+
);
|
|
1221
|
+
const undo = react.useCallback(() => {
|
|
1222
|
+
if (positionRef.current > 0) {
|
|
1223
|
+
positionRef.current -= 1;
|
|
1224
|
+
rerender();
|
|
1225
|
+
}
|
|
1226
|
+
}, [rerender]);
|
|
1227
|
+
const redo = react.useCallback(() => {
|
|
1228
|
+
if (positionRef.current < historyRef.current.length - 1) {
|
|
1229
|
+
positionRef.current += 1;
|
|
1230
|
+
rerender();
|
|
1231
|
+
}
|
|
1232
|
+
}, [rerender]);
|
|
1233
|
+
const reset = react.useCallback(
|
|
1234
|
+
(value) => {
|
|
1235
|
+
historyRef.current = [value];
|
|
1236
|
+
positionRef.current = 0;
|
|
1237
|
+
lastUpdateTime.current = 0;
|
|
1238
|
+
rerender();
|
|
1239
|
+
},
|
|
1240
|
+
[rerender]
|
|
1241
|
+
);
|
|
1242
|
+
return {
|
|
1243
|
+
current,
|
|
1244
|
+
canUndo: positionRef.current > 0,
|
|
1245
|
+
canRedo: positionRef.current < historyRef.current.length - 1,
|
|
1246
|
+
historyLength: historyRef.current.length,
|
|
1247
|
+
set,
|
|
1248
|
+
undo,
|
|
1249
|
+
redo,
|
|
1250
|
+
reset
|
|
1251
|
+
};
|
|
1252
|
+
}
|
|
1253
|
+
function useSearch(text) {
|
|
1254
|
+
const [query, setQuery] = react.useState("");
|
|
1255
|
+
const [currentMatchIndex, setCurrentMatchIndex] = react.useState(0);
|
|
1256
|
+
const [isActive, setIsActive] = react.useState(false);
|
|
1257
|
+
const [options, setOptionsState] = react.useState({
|
|
1258
|
+
caseSensitive: false,
|
|
1259
|
+
useRegex: false,
|
|
1260
|
+
searchKeys: true,
|
|
1261
|
+
searchValues: true
|
|
1262
|
+
});
|
|
1263
|
+
const matches = react.useMemo(() => {
|
|
1264
|
+
if (!query || !text) return [];
|
|
1265
|
+
try {
|
|
1266
|
+
const flags = options.caseSensitive ? "g" : "gi";
|
|
1267
|
+
const pattern = options.useRegex ? query : escapeRegex(query);
|
|
1268
|
+
const regex = new RegExp(pattern, flags);
|
|
1269
|
+
const results = [];
|
|
1270
|
+
const lines = text.split("\n");
|
|
1271
|
+
for (let lineIdx = 0; lineIdx < lines.length; lineIdx++) {
|
|
1272
|
+
const line = lines[lineIdx];
|
|
1273
|
+
let match;
|
|
1274
|
+
regex.lastIndex = 0;
|
|
1275
|
+
while ((match = regex.exec(line)) !== null) {
|
|
1276
|
+
results.push({
|
|
1277
|
+
line: lineIdx + 1,
|
|
1278
|
+
columnStart: match.index,
|
|
1279
|
+
columnEnd: match.index + match[0].length,
|
|
1280
|
+
matchText: match[0]
|
|
1281
|
+
});
|
|
1282
|
+
if (match.index === regex.lastIndex) regex.lastIndex++;
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
return results;
|
|
1286
|
+
} catch {
|
|
1287
|
+
return [];
|
|
1288
|
+
}
|
|
1289
|
+
}, [query, text, options.caseSensitive, options.useRegex]);
|
|
1290
|
+
const goToNext = react.useCallback(() => {
|
|
1291
|
+
if (matches.length === 0) return;
|
|
1292
|
+
setCurrentMatchIndex((i) => (i + 1) % matches.length);
|
|
1293
|
+
}, [matches.length]);
|
|
1294
|
+
const goToPrevious = react.useCallback(() => {
|
|
1295
|
+
if (matches.length === 0) return;
|
|
1296
|
+
setCurrentMatchIndex((i) => (i - 1 + matches.length) % matches.length);
|
|
1297
|
+
}, [matches.length]);
|
|
1298
|
+
const setOptions = react.useCallback((opts) => {
|
|
1299
|
+
setOptionsState((prev) => ({ ...prev, ...opts }));
|
|
1300
|
+
setCurrentMatchIndex(0);
|
|
1301
|
+
}, []);
|
|
1302
|
+
const open = react.useCallback(() => setIsActive(true), []);
|
|
1303
|
+
const close = react.useCallback(() => {
|
|
1304
|
+
setIsActive(false);
|
|
1305
|
+
setQuery("");
|
|
1306
|
+
setCurrentMatchIndex(0);
|
|
1307
|
+
}, []);
|
|
1308
|
+
return {
|
|
1309
|
+
query,
|
|
1310
|
+
setQuery: (q) => {
|
|
1311
|
+
setQuery(q);
|
|
1312
|
+
setCurrentMatchIndex(0);
|
|
1313
|
+
},
|
|
1314
|
+
matches,
|
|
1315
|
+
currentMatchIndex,
|
|
1316
|
+
totalMatches: matches.length,
|
|
1317
|
+
goToNext,
|
|
1318
|
+
goToPrevious,
|
|
1319
|
+
options,
|
|
1320
|
+
setOptions,
|
|
1321
|
+
isActive,
|
|
1322
|
+
open,
|
|
1323
|
+
close
|
|
1324
|
+
};
|
|
1325
|
+
}
|
|
1326
|
+
function escapeRegex(str) {
|
|
1327
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
1328
|
+
}
|
|
1329
|
+
|
|
1330
|
+
// src/core/formatter.ts
|
|
1331
|
+
function formatJson(text, indent = 2) {
|
|
1332
|
+
try {
|
|
1333
|
+
const parsed = JSON.parse(text);
|
|
1334
|
+
const space = indent === "tab" ? " " : indent;
|
|
1335
|
+
return JSON.stringify(parsed, null, space);
|
|
1336
|
+
} catch {
|
|
1337
|
+
return text;
|
|
1338
|
+
}
|
|
1339
|
+
}
|
|
1340
|
+
function minifyJson(text) {
|
|
1341
|
+
try {
|
|
1342
|
+
return JSON.stringify(JSON.parse(text));
|
|
1343
|
+
} catch {
|
|
1344
|
+
return text;
|
|
1345
|
+
}
|
|
1346
|
+
}
|
|
1347
|
+
function sortJsonKeys(text, order = "asc", indent = 2) {
|
|
1348
|
+
try {
|
|
1349
|
+
const parsed = JSON.parse(text);
|
|
1350
|
+
const sorted = deepSortKeys(parsed, order);
|
|
1351
|
+
const space = indent === "tab" ? " " : indent;
|
|
1352
|
+
return JSON.stringify(sorted, null, space);
|
|
1353
|
+
} catch {
|
|
1354
|
+
return text;
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
function deepSortKeys(value, order) {
|
|
1358
|
+
if (Array.isArray(value)) {
|
|
1359
|
+
return value.map((item) => deepSortKeys(item, order));
|
|
1360
|
+
}
|
|
1361
|
+
if (value !== null && typeof value === "object") {
|
|
1362
|
+
const obj = value;
|
|
1363
|
+
const comparator = typeof order === "function" ? order : order === "desc" ? (a, b) => b.localeCompare(a) : (a, b) => a.localeCompare(b);
|
|
1364
|
+
const sorted = {};
|
|
1365
|
+
const keys = Object.keys(obj).sort(comparator);
|
|
1366
|
+
for (const key of keys) {
|
|
1367
|
+
sorted[key] = deepSortKeys(obj[key], order);
|
|
1368
|
+
}
|
|
1369
|
+
return sorted;
|
|
1370
|
+
}
|
|
1371
|
+
return value;
|
|
1372
|
+
}
|
|
1373
|
+
function computeStats(value, text) {
|
|
1374
|
+
const stats = {
|
|
1375
|
+
properties: 0,
|
|
1376
|
+
arrays: 0,
|
|
1377
|
+
totalNodes: 0,
|
|
1378
|
+
maxDepth: 0,
|
|
1379
|
+
byteSize: text ? new TextEncoder().encode(text).length : 0
|
|
1380
|
+
};
|
|
1381
|
+
function walk(val, depth) {
|
|
1382
|
+
stats.totalNodes++;
|
|
1383
|
+
stats.maxDepth = Math.max(stats.maxDepth, depth);
|
|
1384
|
+
if (Array.isArray(val)) {
|
|
1385
|
+
stats.arrays++;
|
|
1386
|
+
val.forEach((item) => walk(item, depth + 1));
|
|
1387
|
+
} else if (val !== null && typeof val === "object") {
|
|
1388
|
+
const keys = Object.keys(val);
|
|
1389
|
+
stats.properties += keys.length;
|
|
1390
|
+
keys.forEach((key) => walk(val[key], depth + 1));
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
if (value !== void 0) {
|
|
1394
|
+
walk(value, 0);
|
|
1395
|
+
}
|
|
1396
|
+
return stats;
|
|
1397
|
+
}
|
|
1398
|
+
|
|
1399
|
+
// src/themes/light.ts
|
|
1400
|
+
var lightTheme = {
|
|
1401
|
+
name: "light",
|
|
1402
|
+
bg: "#ffffff",
|
|
1403
|
+
fg: "#1e1e1e",
|
|
1404
|
+
border: "#e0e0e0",
|
|
1405
|
+
gutterBg: "#f5f5f5",
|
|
1406
|
+
gutterFg: "#999999",
|
|
1407
|
+
selection: "#add6ff",
|
|
1408
|
+
cursor: "#000000",
|
|
1409
|
+
keyColor: "#0451a5",
|
|
1410
|
+
stringColor: "#0a7e07",
|
|
1411
|
+
numberColor: "#098658",
|
|
1412
|
+
booleanColor: "#7a3e9d",
|
|
1413
|
+
nullColor: "#808080",
|
|
1414
|
+
bracketColor: "#333333",
|
|
1415
|
+
bracketMatchBg: "#bad0f847",
|
|
1416
|
+
errorColor: "#e51400",
|
|
1417
|
+
warningColor: "#bf8803",
|
|
1418
|
+
successColor: "#16825d",
|
|
1419
|
+
treeLine: "#d0d0d0",
|
|
1420
|
+
treeHover: "#f0f4ff",
|
|
1421
|
+
treeSelected: "#e0ecff",
|
|
1422
|
+
typeBadgeBg: "#eef2f7"
|
|
1423
|
+
};
|
|
1424
|
+
|
|
1425
|
+
// src/themes/dark.ts
|
|
1426
|
+
var darkTheme = {
|
|
1427
|
+
name: "dark",
|
|
1428
|
+
bg: "#1e1e1e",
|
|
1429
|
+
fg: "#d4d4d4",
|
|
1430
|
+
border: "#3c3c3c",
|
|
1431
|
+
gutterBg: "#252526",
|
|
1432
|
+
gutterFg: "#858585",
|
|
1433
|
+
selection: "#264f78",
|
|
1434
|
+
cursor: "#ffffff",
|
|
1435
|
+
keyColor: "#9cdcfe",
|
|
1436
|
+
stringColor: "#ce9178",
|
|
1437
|
+
numberColor: "#b5cea8",
|
|
1438
|
+
booleanColor: "#569cd6",
|
|
1439
|
+
nullColor: "#808080",
|
|
1440
|
+
bracketColor: "#cccccc",
|
|
1441
|
+
bracketMatchBg: "#0064001a",
|
|
1442
|
+
errorColor: "#f14c4c",
|
|
1443
|
+
warningColor: "#cca700",
|
|
1444
|
+
successColor: "#89d185",
|
|
1445
|
+
treeLine: "#4a4a4a",
|
|
1446
|
+
treeHover: "#2a2d2e",
|
|
1447
|
+
treeSelected: "#094771",
|
|
1448
|
+
typeBadgeBg: "#2d2d30"
|
|
1449
|
+
};
|
|
1450
|
+
var JsonEditor = ({
|
|
1451
|
+
value: externalValue,
|
|
1452
|
+
onChange,
|
|
1453
|
+
mode: controlledMode,
|
|
1454
|
+
onModeChange,
|
|
1455
|
+
schema,
|
|
1456
|
+
validators,
|
|
1457
|
+
validationMode = "onChange",
|
|
1458
|
+
onValidate,
|
|
1459
|
+
theme = "light",
|
|
1460
|
+
height = 400,
|
|
1461
|
+
readOnly = false,
|
|
1462
|
+
searchable = true,
|
|
1463
|
+
sortable = true,
|
|
1464
|
+
indentation = 2,
|
|
1465
|
+
lineNumbers = true,
|
|
1466
|
+
bracketMatching = true,
|
|
1467
|
+
maxSize,
|
|
1468
|
+
virtualize = "auto",
|
|
1469
|
+
onError,
|
|
1470
|
+
onFocus,
|
|
1471
|
+
onBlur,
|
|
1472
|
+
className = "",
|
|
1473
|
+
style,
|
|
1474
|
+
"aria-label": ariaLabel = "JSON Editor"
|
|
1475
|
+
}) => {
|
|
1476
|
+
const [internalMode, setInternalMode] = react.useState("code");
|
|
1477
|
+
const mode = controlledMode ?? internalMode;
|
|
1478
|
+
const handleModeChange = react.useCallback(
|
|
1479
|
+
(newMode) => {
|
|
1480
|
+
if (onModeChange) onModeChange(newMode);
|
|
1481
|
+
else setInternalMode(newMode);
|
|
1482
|
+
},
|
|
1483
|
+
[onModeChange]
|
|
1484
|
+
);
|
|
1485
|
+
const parser = useJsonParser(externalValue, {
|
|
1486
|
+
schema,
|
|
1487
|
+
validators,
|
|
1488
|
+
debounce: validationMode === "onChange" ? 300 : void 0
|
|
1489
|
+
});
|
|
1490
|
+
const history = useUndoRedo(parser.text, { maxHistory: 100 });
|
|
1491
|
+
react.useEffect(() => {
|
|
1492
|
+
if (externalValue !== void 0) {
|
|
1493
|
+
const text = typeof externalValue === "string" ? externalValue : JSON.stringify(externalValue, null, indentation === "tab" ? " " : indentation);
|
|
1494
|
+
if (text !== parser.text) {
|
|
1495
|
+
parser.setText(text);
|
|
1496
|
+
history.reset(text);
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
}, [externalValue]);
|
|
1500
|
+
const handleTextChange = react.useCallback(
|
|
1501
|
+
(text) => {
|
|
1502
|
+
parser.setText(text);
|
|
1503
|
+
history.set(text);
|
|
1504
|
+
if (onChange) {
|
|
1505
|
+
try {
|
|
1506
|
+
const parsed = JSON.parse(text);
|
|
1507
|
+
onChange(parsed, text);
|
|
1508
|
+
} catch {
|
|
1509
|
+
onChange(void 0, text);
|
|
1510
|
+
}
|
|
1511
|
+
}
|
|
1512
|
+
},
|
|
1513
|
+
[parser, history, onChange]
|
|
1514
|
+
);
|
|
1515
|
+
const handleTreeChange = react.useCallback(
|
|
1516
|
+
(newValue) => {
|
|
1517
|
+
const indent = indentation === "tab" ? " " : indentation;
|
|
1518
|
+
const text = JSON.stringify(newValue, null, indent);
|
|
1519
|
+
handleTextChange(text);
|
|
1520
|
+
},
|
|
1521
|
+
[handleTextChange, indentation]
|
|
1522
|
+
);
|
|
1523
|
+
react.useEffect(() => {
|
|
1524
|
+
if (onValidate) {
|
|
1525
|
+
onValidate(parser.validationErrors);
|
|
1526
|
+
}
|
|
1527
|
+
}, [parser.validationErrors, onValidate]);
|
|
1528
|
+
const search = useSearch(parser.text);
|
|
1529
|
+
const [cursor, setCursor] = react.useState({ line: 1, column: 1, offset: 0 });
|
|
1530
|
+
const stats = react.useMemo(
|
|
1531
|
+
() => parser.parsedValue !== void 0 ? computeStats(parser.parsedValue, parser.text) : null,
|
|
1532
|
+
[parser.parsedValue, parser.text]
|
|
1533
|
+
);
|
|
1534
|
+
const resolvedTheme = react.useMemo(() => {
|
|
1535
|
+
if (typeof theme === "object") return theme;
|
|
1536
|
+
if (theme === "dark") return darkTheme;
|
|
1537
|
+
if (theme === "auto") {
|
|
1538
|
+
if (typeof window !== "undefined" && window.matchMedia?.("(prefers-color-scheme: dark)").matches) {
|
|
1539
|
+
return darkTheme;
|
|
1540
|
+
}
|
|
1541
|
+
return lightTheme;
|
|
1542
|
+
}
|
|
1543
|
+
return lightTheme;
|
|
1544
|
+
}, [theme]);
|
|
1545
|
+
const themeVars = react.useMemo(() => {
|
|
1546
|
+
const vars = {};
|
|
1547
|
+
vars["--mjr-bg"] = resolvedTheme.bg;
|
|
1548
|
+
vars["--mjr-fg"] = resolvedTheme.fg;
|
|
1549
|
+
vars["--mjr-border"] = resolvedTheme.border;
|
|
1550
|
+
vars["--mjr-gutter-bg"] = resolvedTheme.gutterBg;
|
|
1551
|
+
vars["--mjr-gutter-fg"] = resolvedTheme.gutterFg;
|
|
1552
|
+
vars["--mjr-selection"] = resolvedTheme.selection;
|
|
1553
|
+
vars["--mjr-cursor"] = resolvedTheme.cursor;
|
|
1554
|
+
vars["--mjr-key"] = resolvedTheme.keyColor;
|
|
1555
|
+
vars["--mjr-string"] = resolvedTheme.stringColor;
|
|
1556
|
+
vars["--mjr-number"] = resolvedTheme.numberColor;
|
|
1557
|
+
vars["--mjr-boolean"] = resolvedTheme.booleanColor;
|
|
1558
|
+
vars["--mjr-null"] = resolvedTheme.nullColor;
|
|
1559
|
+
vars["--mjr-bracket"] = resolvedTheme.bracketColor;
|
|
1560
|
+
vars["--mjr-bracket-match"] = resolvedTheme.bracketMatchBg;
|
|
1561
|
+
vars["--mjr-error"] = resolvedTheme.errorColor;
|
|
1562
|
+
vars["--mjr-warning"] = resolvedTheme.warningColor;
|
|
1563
|
+
vars["--mjr-success"] = resolvedTheme.successColor;
|
|
1564
|
+
vars["--mjr-tree-line"] = resolvedTheme.treeLine;
|
|
1565
|
+
vars["--mjr-tree-hover"] = resolvedTheme.treeHover;
|
|
1566
|
+
vars["--mjr-tree-selected"] = resolvedTheme.treeSelected;
|
|
1567
|
+
vars["--mjr-type-badge-bg"] = resolvedTheme.typeBadgeBg;
|
|
1568
|
+
return vars;
|
|
1569
|
+
}, [resolvedTheme]);
|
|
1570
|
+
react.useEffect(() => {
|
|
1571
|
+
const handleKeyDown = (e) => {
|
|
1572
|
+
const mod = e.metaKey || e.ctrlKey;
|
|
1573
|
+
if (mod && e.key === "z" && !e.shiftKey) {
|
|
1574
|
+
e.preventDefault();
|
|
1575
|
+
history.undo();
|
|
1576
|
+
const undoneText = history.current;
|
|
1577
|
+
if (undoneText !== void 0) {
|
|
1578
|
+
parser.setText(undoneText);
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
if (mod && e.key === "z" && e.shiftKey) {
|
|
1582
|
+
e.preventDefault();
|
|
1583
|
+
history.redo();
|
|
1584
|
+
const redoneText = history.current;
|
|
1585
|
+
if (redoneText !== void 0) {
|
|
1586
|
+
parser.setText(redoneText);
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
if (mod && e.key === "f") {
|
|
1590
|
+
e.preventDefault();
|
|
1591
|
+
if (searchable) search.open();
|
|
1592
|
+
}
|
|
1593
|
+
if (e.key === "Escape") {
|
|
1594
|
+
search.close();
|
|
1595
|
+
}
|
|
1596
|
+
};
|
|
1597
|
+
document.addEventListener("keydown", handleKeyDown);
|
|
1598
|
+
return () => document.removeEventListener("keydown", handleKeyDown);
|
|
1599
|
+
}, [history, parser, search, searchable]);
|
|
1600
|
+
const heightStyle = typeof height === "number" ? `${height}px` : height;
|
|
1601
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(
|
|
1602
|
+
"div",
|
|
1603
|
+
{
|
|
1604
|
+
className: `mjr-editor ${className}`,
|
|
1605
|
+
style: { ...themeVars, height: heightStyle, ...style },
|
|
1606
|
+
role: "application",
|
|
1607
|
+
"aria-label": ariaLabel,
|
|
1608
|
+
onFocus,
|
|
1609
|
+
onBlur,
|
|
1610
|
+
"data-testid": "json-editor",
|
|
1611
|
+
children: [
|
|
1612
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1613
|
+
Toolbar,
|
|
1614
|
+
{
|
|
1615
|
+
mode,
|
|
1616
|
+
onModeChange: handleModeChange,
|
|
1617
|
+
canUndo: history.canUndo,
|
|
1618
|
+
canRedo: history.canRedo,
|
|
1619
|
+
onUndo: () => {
|
|
1620
|
+
history.undo();
|
|
1621
|
+
},
|
|
1622
|
+
onRedo: () => {
|
|
1623
|
+
history.redo();
|
|
1624
|
+
},
|
|
1625
|
+
onFormat: () => parser.format(indentation === "tab" ? " " : indentation),
|
|
1626
|
+
searchable,
|
|
1627
|
+
isSearchOpen: search.isActive,
|
|
1628
|
+
onToggleSearch: () => search.isActive ? search.close() : search.open(),
|
|
1629
|
+
readOnly
|
|
1630
|
+
}
|
|
1631
|
+
),
|
|
1632
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "mjr-editor__content", children: [
|
|
1633
|
+
(mode === "code" || mode === "split") && /* @__PURE__ */ jsxRuntime.jsx("div", { className: `mjr-editor__panel ${mode === "split" ? "mjr-editor__panel--half" : ""}`, children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
1634
|
+
CodeEditor,
|
|
1635
|
+
{
|
|
1636
|
+
value: parser.text,
|
|
1637
|
+
onChange: handleTextChange,
|
|
1638
|
+
parseError: parser.parseError,
|
|
1639
|
+
readOnly,
|
|
1640
|
+
lineNumbers,
|
|
1641
|
+
bracketMatching,
|
|
1642
|
+
searchMatches: search.matches,
|
|
1643
|
+
currentMatchIndex: search.currentMatchIndex,
|
|
1644
|
+
onCursorChange: setCursor
|
|
1645
|
+
}
|
|
1646
|
+
) }),
|
|
1647
|
+
(mode === "tree" || mode === "split") && /* @__PURE__ */ jsxRuntime.jsx("div", { className: `mjr-editor__panel ${mode === "split" ? "mjr-editor__panel--half" : ""}`, children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
1648
|
+
TreeEditor,
|
|
1649
|
+
{
|
|
1650
|
+
value: parser.parsedValue,
|
|
1651
|
+
onChange: handleTreeChange,
|
|
1652
|
+
readOnly
|
|
1653
|
+
}
|
|
1654
|
+
) })
|
|
1655
|
+
] }),
|
|
1656
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1657
|
+
StatusBar,
|
|
1658
|
+
{
|
|
1659
|
+
parseError: parser.parseError,
|
|
1660
|
+
validationErrors: parser.validationErrors,
|
|
1661
|
+
cursor,
|
|
1662
|
+
stats
|
|
1663
|
+
}
|
|
1664
|
+
)
|
|
1665
|
+
]
|
|
1666
|
+
}
|
|
1667
|
+
);
|
|
1668
|
+
};
|
|
1669
|
+
|
|
1670
|
+
exports.JsonEditor = JsonEditor;
|
|
1671
|
+
exports.buildPath = buildPath;
|
|
1672
|
+
exports.computeStats = computeStats;
|
|
1673
|
+
exports.darkTheme = darkTheme;
|
|
1674
|
+
exports.deleteByPath = deleteByPath;
|
|
1675
|
+
exports.formatJson = formatJson;
|
|
1676
|
+
exports.getByPath = getByPath;
|
|
1677
|
+
exports.isValidJson = isValidJson;
|
|
1678
|
+
exports.lightTheme = lightTheme;
|
|
1679
|
+
exports.minifyJson = minifyJson;
|
|
1680
|
+
exports.parseJson = parseJson;
|
|
1681
|
+
exports.parsePath = parsePath;
|
|
1682
|
+
exports.runCustomValidators = runCustomValidators;
|
|
1683
|
+
exports.setByPath = setByPath;
|
|
1684
|
+
exports.sortJsonKeys = sortJsonKeys;
|
|
1685
|
+
exports.stringifyJson = stringifyJson;
|
|
1686
|
+
exports.useJsonParser = useJsonParser;
|
|
1687
|
+
exports.useSearch = useSearch;
|
|
1688
|
+
exports.useUndoRedo = useUndoRedo;
|
|
1689
|
+
exports.validateSchema = validateSchema;
|
|
1690
|
+
//# sourceMappingURL=index.js.map
|
|
1691
|
+
//# sourceMappingURL=index.js.map
|