@celestia-island/hikari 0.55.421 → 0.55.424
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/package.json +1 -1
- package/src/components/HkJsonTree.edit.scss +109 -0
- package/src/components/HkJsonTree.edit.test.tsx +627 -0
- package/src/components/HkJsonTree.tsx +760 -27
- package/src/components/HkNotificationBell.scss +4 -0
- package/src/components/HkNotificationBell.styles.test.ts +11 -0
- package/src/components/HkNotificationBell.test.tsx +106 -8
- package/src/components/HkNotificationBell.tsx +61 -9
- package/src/components/HkUserPicker.scss +46 -0
- package/src/components/HkUserPicker.test.tsx +108 -0
- package/src/components/HkUserPicker.trigger.contract.test.ts +61 -0
- package/src/components/HkUserPicker.tsx +111 -54
- package/src/components/jsonTreeEdit.test.ts +252 -0
- package/src/components/jsonTreeEdit.ts +280 -0
- package/src/i18n/locales/ar/components.json +15 -0
- package/src/i18n/locales/de/components.json +15 -0
- package/src/i18n/locales/en/components.json +15 -0
- package/src/i18n/locales/es/components.json +15 -0
- package/src/i18n/locales/fr/components.json +15 -0
- package/src/i18n/locales/ja/components.json +15 -0
- package/src/i18n/locales/ko/components.json +15 -0
- package/src/i18n/locales/pt/components.json +15 -0
- package/src/i18n/locales/ru/components.json +15 -0
- package/src/i18n/locales/zh-Hans/components.json +15 -0
- package/src/i18n/locales/zh-Hant/components.json +15 -0
- package/src/index.ts +8 -0
|
@@ -1,10 +1,32 @@
|
|
|
1
|
-
import { computed, defineComponent, ref, Transition, watch } from "vue";
|
|
2
|
-
import { ChevronRight } from "lucide-vue-next";
|
|
1
|
+
import { computed, defineComponent, nextTick, ref, Transition, watch } from "vue";
|
|
2
|
+
import { ChevronRight, KeyRound, Pencil, Plus, Trash2 } from "lucide-vue-next";
|
|
3
3
|
import { useClipboard } from "@celestia-island/hikari";
|
|
4
4
|
|
|
5
|
+
import { useI18n } from "../i18n/context";
|
|
6
|
+
import {
|
|
7
|
+
useContextMenu,
|
|
8
|
+
useContextMenuTrigger,
|
|
9
|
+
type ContextMenuRequest,
|
|
10
|
+
type ContextTriggerPoint,
|
|
11
|
+
} from "../composables/useContextMenu";
|
|
12
|
+
import type { HkMenuItem } from "./HkMenu";
|
|
13
|
+
import {
|
|
14
|
+
jsonInsertChildAt,
|
|
15
|
+
jsonIsArrayAt,
|
|
16
|
+
jsonPathKey,
|
|
17
|
+
jsonRemoveAtPath,
|
|
18
|
+
jsonSetAtPath,
|
|
19
|
+
jsonValueAtPath,
|
|
20
|
+
type JsonPath,
|
|
21
|
+
} from "./jsonTreeEdit";
|
|
22
|
+
|
|
5
23
|
import { useReportedTransition } from "../composables/useReportedTransition";
|
|
6
24
|
|
|
7
25
|
import "./HkJsonTree.scss";
|
|
26
|
+
// Editing chrome rides its own sheet: HkJsonTree.scss is compiled into
|
|
27
|
+
// the boot error landing's checked-in CSS, and the editing styles must
|
|
28
|
+
// never re-bloat that boot-critical payload.
|
|
29
|
+
import "./HkJsonTree.edit.scss";
|
|
8
30
|
|
|
9
31
|
/**
|
|
10
32
|
* Parse a JSON text into a container value (object or array). Returns null
|
|
@@ -37,9 +59,17 @@ const JT_FOLD_REPORT_MS = 320;
|
|
|
37
59
|
/** Easing used when the host does not expose `--jt-fold-ease`. */
|
|
38
60
|
const JT_FOLD_FALLBACK_EASE = "cubic-bezier(0.4, 0, 0.2, 1)";
|
|
39
61
|
|
|
40
|
-
/** One node of the interactive JSON result tree.
|
|
62
|
+
/** One node of the interactive JSON result tree.
|
|
63
|
+
*
|
|
64
|
+
* `path` (and its precomputed `pathKey`) locate the node inside the root
|
|
65
|
+
* value — object members carry their key, array elements their index as
|
|
66
|
+
* a string (matching `key`). The editable mode uses them for the row
|
|
67
|
+
* menu target, the inline editor and the path-keyed expansion set;
|
|
68
|
+
* read-only trees simply carry the extra fields. */
|
|
41
69
|
export interface HJsonNode {
|
|
42
70
|
id: number;
|
|
71
|
+
path: JsonPath;
|
|
72
|
+
pathKey: string;
|
|
43
73
|
key: string | null;
|
|
44
74
|
value: unknown;
|
|
45
75
|
depth: number;
|
|
@@ -84,6 +114,7 @@ function buildJsonNode(
|
|
|
84
114
|
depth: number,
|
|
85
115
|
maxDepth: number,
|
|
86
116
|
idSeq: { n: number },
|
|
117
|
+
path: JsonPath,
|
|
87
118
|
): HJsonNode {
|
|
88
119
|
const isContainer = value !== null && typeof value === "object";
|
|
89
120
|
const isLongString = typeof value === "string" && (value as string).length > STR_PREVIEW_LEN;
|
|
@@ -99,7 +130,7 @@ function buildJsonNode(
|
|
|
99
130
|
preview = buildObjectPreview(value, Array.isArray(value));
|
|
100
131
|
if (depth < maxDepth) {
|
|
101
132
|
for (const [k, v] of entries) {
|
|
102
|
-
children.push(buildJsonNode(v, k, depth + 1, maxDepth, idSeq));
|
|
133
|
+
children.push(buildJsonNode(v, k, depth + 1, maxDepth, idSeq, [...path, k]));
|
|
103
134
|
}
|
|
104
135
|
}
|
|
105
136
|
}
|
|
@@ -108,6 +139,8 @@ function buildJsonNode(
|
|
|
108
139
|
idSeq.n += 1;
|
|
109
140
|
return {
|
|
110
141
|
id: nid,
|
|
142
|
+
path,
|
|
143
|
+
pathKey: jsonPathKey(path),
|
|
111
144
|
key,
|
|
112
145
|
value,
|
|
113
146
|
depth,
|
|
@@ -123,11 +156,11 @@ function buildJsonNode(
|
|
|
123
156
|
/**
|
|
124
157
|
* Build the interactive JSON tree for a parsed value. Returns null when the
|
|
125
158
|
* value is not a container (no tree needed). Node ids are unique within the
|
|
126
|
-
* returned tree.
|
|
159
|
+
* returned tree; node paths locate each node inside the root value.
|
|
127
160
|
*/
|
|
128
161
|
export function buildJsonTree(value: unknown, maxDepth = 8): HJsonNode | null {
|
|
129
162
|
if (value === null || typeof value !== "object") return null;
|
|
130
|
-
return buildJsonNode(value, null, 0, maxDepth, { n: 0 });
|
|
163
|
+
return buildJsonNode(value, null, 0, maxDepth, { n: 0 }, []);
|
|
131
164
|
}
|
|
132
165
|
|
|
133
166
|
function initialExpandedSet(root: HJsonNode): Set<number> {
|
|
@@ -139,6 +172,38 @@ function initialExpandedSet(root: HJsonNode): Set<number> {
|
|
|
139
172
|
return set;
|
|
140
173
|
}
|
|
141
174
|
|
|
175
|
+
/** Editable-mode twin of `initialExpandedSet`: the root and its direct
|
|
176
|
+
* children, keyed by PATH (see the expansion contract in HkJsonTree's
|
|
177
|
+
* header docs). */
|
|
178
|
+
function initialExpandedPathSet(root: HJsonNode): Set<string> {
|
|
179
|
+
const set = new Set<string>([root.pathKey]);
|
|
180
|
+
for (const child of root.children) {
|
|
181
|
+
set.add(child.pathKey);
|
|
182
|
+
}
|
|
183
|
+
return set;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/* ── Editable mode: leaf types, insert defaults, labels ─────────────── */
|
|
187
|
+
|
|
188
|
+
/** Scalar types the inline editor's type chips switch between. */
|
|
189
|
+
type JsonLeafType = "string" | "number" | "boolean" | "null";
|
|
190
|
+
/** The six child types the "New child" submenu offers. */
|
|
191
|
+
type JsonInsertType = JsonLeafType | "object" | "array";
|
|
192
|
+
|
|
193
|
+
const LEAF_TYPES: JsonLeafType[] = ["string", "number", "boolean", "null"];
|
|
194
|
+
const INSERT_TYPES: JsonInsertType[] = ["string", "number", "boolean", "null", "object", "array"];
|
|
195
|
+
|
|
196
|
+
/** Landing values for "New child" (spec: `""` / `0` / `false` / `null` /
|
|
197
|
+
* `{}` / `[]`) — committed immediately, then refined inline. */
|
|
198
|
+
const INSERT_DEFAULTS: Record<JsonInsertType, unknown> = {
|
|
199
|
+
string: "",
|
|
200
|
+
number: 0,
|
|
201
|
+
boolean: false,
|
|
202
|
+
null: null,
|
|
203
|
+
object: {},
|
|
204
|
+
array: [],
|
|
205
|
+
};
|
|
206
|
+
|
|
142
207
|
/* ── Fold motion (expand/collapse) ─────────────────────────────────────
|
|
143
208
|
*
|
|
144
209
|
* The fold is JS-driven (height measured, then transitioned) because the
|
|
@@ -377,6 +442,43 @@ function selectionTouches(el: HTMLElement): boolean {
|
|
|
377
442
|
* height (see the fold-motion block above), reported to the animation
|
|
378
443
|
* bus and snapping under reduced motion / the global animation switch.
|
|
379
444
|
* The chevron rotates with the same tokens instead of swapping glyphs.
|
|
445
|
+
*
|
|
446
|
+
* Editing contract (opt-in `editable` prop, 2026-11-19): the tree stays a
|
|
447
|
+
* viewer by default — nothing changes until a host opts in. Editing is
|
|
448
|
+
* then MENU-DRIVEN: right-click / long-press / menu-key on any row (via
|
|
449
|
+
* the shared `useContextMenuTrigger`, so desktop gets the pointer-anchored
|
|
450
|
+
* popout, phones the bottom sheet and hosts without an
|
|
451
|
+
* `HkContextMenuProvider` the documented no-op — the menu simply never
|
|
452
|
+
* opens, never throws) offers edit-value / rename-key / new-child ▸
|
|
453
|
+
* (six-type submenu) / delete per node; the root only grows children.
|
|
454
|
+
* Picking edit or rename flips that row into an inline editor (key input
|
|
455
|
+
* for object members, value input with string/number/boolean/null chips,
|
|
456
|
+
* invalid numbers block submission and show an invalid state); Enter or
|
|
457
|
+
* a blur outside the editor commits, Escape cancels. A rename is the
|
|
458
|
+
* three-op composite insert(newKey, value) + remove(oldKey), which moves
|
|
459
|
+
* the member to the END of its object — content survives, key order in
|
|
460
|
+
* that object does not. New children land
|
|
461
|
+
* their default value (`""`/`0`/`false`/`null`/`{}`/`[]`) immediately and
|
|
462
|
+
* the fresh row opens inline so the default is refined, not imagined.
|
|
463
|
+
* Every commit flows through the pure `jsonTreeEdit` operations
|
|
464
|
+
* (`jsonSetAtPath` / `jsonInsertChildAt` / `jsonRemoveAtPath` — immutable,
|
|
465
|
+
* exported from the package index) and is handed to the host via
|
|
466
|
+
* `update:value` with the NEW root: the tree is a controlled component,
|
|
467
|
+
* editing pairs with the `value` prop (a `text`-only tree has no root to
|
|
468
|
+
* mutate and stays read-only even when `editable` is on). Empty open
|
|
469
|
+
* containers render an inline "New child" affordance — the only way an
|
|
470
|
+
* empty `{}` can ever grow a first child.
|
|
471
|
+
*
|
|
472
|
+
* Editable expansion contract: while `editable` is on, the expanded set is
|
|
473
|
+
* keyed by node PATH (not the rebuilt tree's node ids), so a commit's
|
|
474
|
+
* rebuild keeps the user's folds exactly where they left them — keying by
|
|
475
|
+
* id would collapse the whole tree after every edit and make editing
|
|
476
|
+
* unusable on any payload deeper than one level. Non-editable trees keep
|
|
477
|
+
* the id-keyed set and its reset-on-rebuild behavior untouched.
|
|
478
|
+
*
|
|
479
|
+
* Copy contract note: the tree-wide copy-on-click stays; clicks inside the
|
|
480
|
+
* editor row and the empty-container affordance are excluded so typing and
|
|
481
|
+
* menu buttons never dump the payload into the clipboard.
|
|
380
482
|
*/
|
|
381
483
|
export const HkJsonTree = defineComponent({
|
|
382
484
|
name: "HkJsonTree",
|
|
@@ -390,10 +492,19 @@ export const HkJsonTree = defineComponent({
|
|
|
390
492
|
/** Deliberate opt-in to row text selection (default: none — see the
|
|
391
493
|
* selection contract above). */
|
|
392
494
|
selectable: { type: Boolean, default: false },
|
|
495
|
+
/** Opt-in to menu-driven editing (default: false — see the editing
|
|
496
|
+
* contract above). Pairs with the `value` prop: the host owns the
|
|
497
|
+
* root and receives every commit through `update:value`. */
|
|
498
|
+
editable: { type: Boolean, default: false },
|
|
393
499
|
},
|
|
394
|
-
|
|
500
|
+
emits: ["update:value"],
|
|
501
|
+
setup(props, { emit }) {
|
|
395
502
|
const clipboard = useClipboard();
|
|
503
|
+
const { t } = useI18n();
|
|
396
504
|
const expanded = ref(new Set<number>());
|
|
505
|
+
/** Editable-mode expansion: keyed by node path (see the editable
|
|
506
|
+
* expansion contract in the header docs), so commits survive rebuilds. */
|
|
507
|
+
const expandedPaths = ref(new Set<string>());
|
|
397
508
|
const treeEl = ref<HTMLElement | null>(null);
|
|
398
509
|
|
|
399
510
|
// The report window follows the resolved fold duration (a host may
|
|
@@ -425,15 +536,46 @@ export const HkJsonTree = defineComponent({
|
|
|
425
536
|
return null;
|
|
426
537
|
});
|
|
427
538
|
|
|
539
|
+
/** Editing pairs with the `value` prop: a `text`-only tree has no
|
|
540
|
+
* controlled root to mutate and stays read-only (menu never opens). */
|
|
541
|
+
const canEdit = computed(() => props.editable === true && props.value !== undefined);
|
|
542
|
+
|
|
543
|
+
/** Every built node by its path key — the row menu's target lookup and
|
|
544
|
+
* the stale-editor guard both read this. */
|
|
545
|
+
const nodeIndex = computed(() => {
|
|
546
|
+
const map = new Map<string, HJsonNode>();
|
|
547
|
+
const walk = (n: HJsonNode) => {
|
|
548
|
+
map.set(n.pathKey, n);
|
|
549
|
+
for (const child of n.children) walk(child);
|
|
550
|
+
};
|
|
551
|
+
if (root.value) walk(root.value);
|
|
552
|
+
return map;
|
|
553
|
+
});
|
|
554
|
+
|
|
428
555
|
// Reset expansion whenever the tree is rebuilt (fresh node ids).
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
556
|
+
// Editable mode deliberately does NOT reset: its path-keyed set rides
|
|
557
|
+
// rebuilds so folds survive every commit (header contract). It seeds
|
|
558
|
+
// once per root APPEARANCE (mount, or a host filling in a previously
|
|
559
|
+
// absent value) and keeps the user's word after that. Registered BELOW,
|
|
560
|
+
// after the editor state it reaches into (the immediate run is
|
|
561
|
+
// synchronous and must not touch a not-yet-initialized ref).
|
|
562
|
+
function isOpenState(node: HJsonNode): boolean {
|
|
563
|
+
return props.editable
|
|
564
|
+
? expandedPaths.value.has(node.pathKey)
|
|
565
|
+
: expanded.value.has(node.id);
|
|
566
|
+
}
|
|
432
567
|
|
|
433
|
-
function toggleJsonNode(
|
|
568
|
+
function toggleJsonNode(node: HJsonNode) {
|
|
569
|
+
if (props.editable) {
|
|
570
|
+
const next = new Set(expandedPaths.value);
|
|
571
|
+
if (next.has(node.pathKey)) next.delete(node.pathKey);
|
|
572
|
+
else next.add(node.pathKey);
|
|
573
|
+
expandedPaths.value = next;
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
434
576
|
const next = new Set(expanded.value);
|
|
435
|
-
if (next.has(id)) next.delete(id);
|
|
436
|
-
else next.add(id);
|
|
577
|
+
if (next.has(node.id)) next.delete(node.id);
|
|
578
|
+
else next.add(node.id);
|
|
437
579
|
expanded.value = next;
|
|
438
580
|
}
|
|
439
581
|
|
|
@@ -451,6 +593,415 @@ export const HkJsonTree = defineComponent({
|
|
|
451
593
|
return selectionTouches(treeEl.value);
|
|
452
594
|
}
|
|
453
595
|
|
|
596
|
+
/* ── Editable mode: menu, inline editor, pure-op commits ──────────
|
|
597
|
+
*
|
|
598
|
+
* Labels ride the house i18n convention (useI18n + the 11-locale
|
|
599
|
+
* bundles, English inline fallbacks): that is what ~every hikari
|
|
600
|
+
* component does (HkMenuPanel, HkEditableTable, HkConfirmDialog, …);
|
|
601
|
+
* a host-passed labels prop would be the odd one out, and the
|
|
602
|
+
* flat-key parity test keeps the bundles complete for all locales.
|
|
603
|
+
*/
|
|
604
|
+
|
|
605
|
+
/** The row the inline editor is attached to, if any. The editor always
|
|
606
|
+
* targets a node that EXISTS in the rebuilt tree (inserts commit the
|
|
607
|
+
* default first, then aim the editor at the fresh path). */
|
|
608
|
+
const editing = ref<{ path: JsonPath } | null>(null);
|
|
609
|
+
/** The editor's working state, snapshotted when the editor opens and
|
|
610
|
+
* mutated by the inputs; committed on Enter / outside blur. */
|
|
611
|
+
const draft = ref<JsonEditDraft | null>(null);
|
|
612
|
+
const editKeyEl = ref<HTMLInputElement | null>(null);
|
|
613
|
+
const editValueEl = ref<HTMLInputElement | null>(null);
|
|
614
|
+
/** Focus requested for the editor ("key" for rename / object inserts,
|
|
615
|
+
* "value" for value edits and array-leaf inserts). Cleared once it
|
|
616
|
+
* lands; retried after a rebuild when the row mounts late. */
|
|
617
|
+
let editFocusWanted: "key" | "value" | null = null;
|
|
618
|
+
|
|
619
|
+
interface JsonEditDraft {
|
|
620
|
+
keyText: string;
|
|
621
|
+
type: JsonLeafType;
|
|
622
|
+
/** The string text (also carries the number text while typing). */
|
|
623
|
+
text: string;
|
|
624
|
+
bool: boolean;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
function draftForValue(key: string | null, value: unknown): JsonEditDraft {
|
|
628
|
+
return {
|
|
629
|
+
keyText: key ?? "",
|
|
630
|
+
type:
|
|
631
|
+
typeof value === "number"
|
|
632
|
+
? "number"
|
|
633
|
+
: typeof value === "boolean"
|
|
634
|
+
? "boolean"
|
|
635
|
+
: value === null
|
|
636
|
+
? "null"
|
|
637
|
+
: "string",
|
|
638
|
+
text: typeof value === "string" ? value : typeof value === "number" ? String(value) : "",
|
|
639
|
+
bool: value === true,
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
/** Number-field validity: empty and non-finite texts are invalid —
|
|
644
|
+
* `Number("")` is 0 and would otherwise smuggle empties in as zeros. */
|
|
645
|
+
function isNumberText(text: string): boolean {
|
|
646
|
+
return text.trim() !== "" && Number.isFinite(Number(text));
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
function draftValue(d: JsonEditDraft): unknown {
|
|
650
|
+
if (d.type === "null") return null;
|
|
651
|
+
if (d.type === "boolean") return d.bool;
|
|
652
|
+
if (d.type === "number") return Number(d.text);
|
|
653
|
+
return d.text;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
function typeLabel(type: JsonInsertType): string {
|
|
657
|
+
switch (type) {
|
|
658
|
+
case "string": return t("hikari::jsonTree.typeString", "String");
|
|
659
|
+
case "number": return t("hikari::jsonTree.typeNumber", "Number");
|
|
660
|
+
case "boolean": return t("hikari::jsonTree.typeBoolean", "Boolean");
|
|
661
|
+
case "null": return t("hikari::jsonTree.typeNull", "Null");
|
|
662
|
+
case "object": return t("hikari::jsonTree.typeObject", "Object");
|
|
663
|
+
case "array": return t("hikari::jsonTree.typeArray", "Array");
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
/** Whether `node` is a named member of an OBJECT (rename applies) —
|
|
668
|
+
* array elements carry string index keys that are NOT names. */
|
|
669
|
+
function isObjectMember(node: HJsonNode): boolean {
|
|
670
|
+
return node.path.length > 0
|
|
671
|
+
&& node.key !== null
|
|
672
|
+
&& !jsonIsArrayAt(props.value, node.path.slice(0, -1));
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
function focusEditorSoon(which: "key" | "value") {
|
|
676
|
+
editFocusWanted = which;
|
|
677
|
+
void nextTick(() => focusEditorNow());
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
function focusEditorNow() {
|
|
681
|
+
if (!editing.value) {
|
|
682
|
+
editFocusWanted = null;
|
|
683
|
+
return;
|
|
684
|
+
}
|
|
685
|
+
const el = editFocusWanted === "key" ? editKeyEl.value : editValueEl.value;
|
|
686
|
+
if (el) {
|
|
687
|
+
el.focus();
|
|
688
|
+
// Preselect: the common edit replaces the whole current value/key.
|
|
689
|
+
el.select?.();
|
|
690
|
+
editFocusWanted = null;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
function beginEdit(node: HJsonNode, focus: "key" | "value") {
|
|
695
|
+
if (!canEdit.value) return;
|
|
696
|
+
editing.value = { path: node.path };
|
|
697
|
+
draft.value = draftForValue(node.key, node.value);
|
|
698
|
+
focusEditorSoon(focus);
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
function closeEditor() {
|
|
702
|
+
editing.value = null;
|
|
703
|
+
draft.value = null;
|
|
704
|
+
editFocusWanted = null;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
function cancelEdit() {
|
|
708
|
+
// Synchronous tear-down: `editing` is null BEFORE the row unmounts,
|
|
709
|
+
// so the trailing focusout is a no-op — no flag dance needed.
|
|
710
|
+
if (!editing.value) return;
|
|
711
|
+
closeEditor();
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
function onEditorKeydown(event: KeyboardEvent) {
|
|
715
|
+
if (event.key === "Enter") {
|
|
716
|
+
event.preventDefault();
|
|
717
|
+
commitEdit();
|
|
718
|
+
} else if (event.key === "Escape") {
|
|
719
|
+
event.preventDefault();
|
|
720
|
+
cancelEdit();
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
/** Focus leaving the editor row (relatedTarget outside it) commits —
|
|
725
|
+
* chip/button hops INSIDE the row never do, and a cancelled editor
|
|
726
|
+
* never commits (its row is already gone when the focusout lands). */
|
|
727
|
+
function maybeBlurCommit(event: FocusEvent) {
|
|
728
|
+
if (!editing.value || !draft.value) return;
|
|
729
|
+
const row = event.currentTarget as HTMLElement | null;
|
|
730
|
+
const to = event.relatedTarget as Node | null;
|
|
731
|
+
if (row && to && row.contains(to)) return;
|
|
732
|
+
commitEdit();
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
/** First non-colliding `key`/`key2`/… for a new object member. */
|
|
736
|
+
function nextChildKey(obj: Record<string, unknown>): string {
|
|
737
|
+
let n = 1;
|
|
738
|
+
let key = "key";
|
|
739
|
+
// Own keys only — same prototype hygiene as the edit ops.
|
|
740
|
+
while (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
741
|
+
n += 1;
|
|
742
|
+
key = `key${n}`;
|
|
743
|
+
}
|
|
744
|
+
return key;
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
/** Whether a sibling already claims `key` among the object members at
|
|
748
|
+
* `parentPath` (own keys only — the prototype chain never answers).
|
|
749
|
+
* The rename composite's insert is overwrite semantics, so a collision
|
|
750
|
+
* would silently EAT the sibling — commits must refuse instead. */
|
|
751
|
+
function siblingHasKey(parentPath: JsonPath, key: string): boolean {
|
|
752
|
+
const parent = jsonValueAtPath(props.value, parentPath);
|
|
753
|
+
return parent !== null
|
|
754
|
+
&& typeof parent === "object"
|
|
755
|
+
&& !Array.isArray(parent)
|
|
756
|
+
&& Object.prototype.hasOwnProperty.call(parent, key);
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
/** The editor's rename collides with an existing sibling key — the
|
|
760
|
+
* invalid state the key input shows until the text is unique again. */
|
|
761
|
+
function renameCollision(node: HJsonNode, keyText: string): boolean {
|
|
762
|
+
return isObjectMember(node)
|
|
763
|
+
&& keyText !== node.key
|
|
764
|
+
&& siblingHasKey(node.path.slice(0, -1), keyText);
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
/** Commit the open editor through the pure ops and hand the new root
|
|
768
|
+
* to the host. A rename is the composite insert(newKey, value) +
|
|
769
|
+
* remove(oldKey) — the exported op set stays exactly the three
|
|
770
|
+
* primitives. Order caveat (see jsonTreeEdit's header): the composite
|
|
771
|
+
* MOVES the renamed member to the end of its object; content is
|
|
772
|
+
* preserved, key order inside that object is not. Renaming ONTO an
|
|
773
|
+
* existing sibling key is REFUSED (the insert overwrite would delete
|
|
774
|
+
* the sibling with no undo): the key input shows the invalid state
|
|
775
|
+
* and the editor stays open, exactly like a bad number. */
|
|
776
|
+
function commitEdit() {
|
|
777
|
+
const target = editing.value;
|
|
778
|
+
const d = draft.value;
|
|
779
|
+
if (!target || !d) return;
|
|
780
|
+
const node = nodeIndex.value.get(jsonPathKey(target.path));
|
|
781
|
+
if (!node) {
|
|
782
|
+
closeEditor();
|
|
783
|
+
return;
|
|
784
|
+
}
|
|
785
|
+
const numberBad = d.type === "number" && !isNumberText(d.text);
|
|
786
|
+
const keyCollides = renameCollision(node, d.keyText);
|
|
787
|
+
if (numberBad || keyCollides) return; // blocked; invalid state shows
|
|
788
|
+
let next = props.value;
|
|
789
|
+
let finalPath = node.path;
|
|
790
|
+
if (isObjectMember(node) && d.keyText !== node.key) {
|
|
791
|
+
next = jsonInsertChildAt(next, node.path.slice(0, -1), d.keyText, node.value);
|
|
792
|
+
next = jsonRemoveAtPath(next, node.path);
|
|
793
|
+
finalPath = [...node.path.slice(0, -1), d.keyText];
|
|
794
|
+
migrateExpansion(node.path, d.keyText);
|
|
795
|
+
}
|
|
796
|
+
if (!node.isContainer) {
|
|
797
|
+
next = jsonSetAtPath(next, finalPath, draftValue(d));
|
|
798
|
+
}
|
|
799
|
+
closeEditor();
|
|
800
|
+
// The ops return the input untouched when a guard fired — no point
|
|
801
|
+
// emitting a no-op.
|
|
802
|
+
if (next !== props.value) emit("update:value", next);
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
/** Rewrite the path-keyed expansion set for a rename: the renamed node
|
|
806
|
+
* AND its whole subtree moved to the new key; structural parse (not a
|
|
807
|
+
* string prefix — JSON keys would collide on `["a"]` vs `["ab"]`). */
|
|
808
|
+
function migrateExpansion(oldPath: JsonPath, newTail: string | number) {
|
|
809
|
+
const next = new Set<string>();
|
|
810
|
+
for (const k of expandedPaths.value) {
|
|
811
|
+
try {
|
|
812
|
+
const p = JSON.parse(k) as JsonPath;
|
|
813
|
+
if (p.length >= oldPath.length
|
|
814
|
+
&& oldPath.every((seg, i) => seg === p[i])) {
|
|
815
|
+
next.add(jsonPathKey([...p.slice(0, oldPath.length - 1), newTail, ...p.slice(oldPath.length)]));
|
|
816
|
+
continue;
|
|
817
|
+
}
|
|
818
|
+
} catch {
|
|
819
|
+
/* not one of ours — keep as-is */
|
|
820
|
+
}
|
|
821
|
+
next.add(k);
|
|
822
|
+
}
|
|
823
|
+
expandedPaths.value = next;
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
/** Drop expansion keys under a deleted node (and the node itself).
|
|
827
|
+
* Object members are exact. ARRAY members are approximate by index:
|
|
828
|
+
* a splice shifts later siblings down while their stored path keys
|
|
829
|
+
* stay put, so a fold intended for a shifted element can land on its
|
|
830
|
+
* new neighbor (the expansion of old [2] decorates new [2], which is
|
|
831
|
+
* the element formerly at [3]) — wrong-fold drift confined to the
|
|
832
|
+
* edited array's subtree, never lost data. A faithful fix needs a
|
|
833
|
+
* parallel old→new index migration through every commit; not worth
|
|
834
|
+
* the machinery for a fold state. */
|
|
835
|
+
function pruneExpansion(deletedPath: JsonPath) {
|
|
836
|
+
const next = new Set<string>();
|
|
837
|
+
for (const k of expandedPaths.value) {
|
|
838
|
+
try {
|
|
839
|
+
const p = JSON.parse(k) as JsonPath;
|
|
840
|
+
if (p.length >= deletedPath.length
|
|
841
|
+
&& deletedPath.every((seg, i) => seg === p[i])) {
|
|
842
|
+
continue;
|
|
843
|
+
}
|
|
844
|
+
} catch {
|
|
845
|
+
/* not one of ours — keep as-is */
|
|
846
|
+
}
|
|
847
|
+
next.add(k);
|
|
848
|
+
}
|
|
849
|
+
expandedPaths.value = next;
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
/** "New child": commit the default value immediately (so the child is
|
|
853
|
+
* real, visible and deletable even if the user walks away), then aim
|
|
854
|
+
* the inline editor at the fresh row. */
|
|
855
|
+
function insertChild(node: HJsonNode, type: JsonInsertType) {
|
|
856
|
+
if (!canEdit.value || !node.isContainer) return;
|
|
857
|
+
const value = INSERT_DEFAULTS[type];
|
|
858
|
+
let next = props.value;
|
|
859
|
+
let childPath: JsonPath;
|
|
860
|
+
let childKey: string | null = null;
|
|
861
|
+
if (Array.isArray(node.value)) {
|
|
862
|
+
const idx = node.childCount; // spec: array insert = append
|
|
863
|
+
next = jsonInsertChildAt(next, node.path, idx, value);
|
|
864
|
+
childPath = [...node.path, String(idx)];
|
|
865
|
+
} else {
|
|
866
|
+
const key = nextChildKey(node.value as Record<string, unknown>);
|
|
867
|
+
next = jsonInsertChildAt(next, node.path, key, value);
|
|
868
|
+
childPath = [...node.path, key];
|
|
869
|
+
childKey = key;
|
|
870
|
+
}
|
|
871
|
+
if (!expandedPaths.value.has(node.pathKey)) {
|
|
872
|
+
expandedPaths.value = new Set([...expandedPaths.value, node.pathKey]);
|
|
873
|
+
}
|
|
874
|
+
// Same guard as commitEdit: a guard-fired op returns the input
|
|
875
|
+
// untouched, and re-emitting that is a no-op for the host.
|
|
876
|
+
if (next !== props.value) emit("update:value", next);
|
|
877
|
+
// Inline-refine the fresh child: name it (object member) or fix the
|
|
878
|
+
// value (leaf inside an array). A container inside an array has
|
|
879
|
+
// nothing to type — no editor.
|
|
880
|
+
const isLeaf = type !== "object" && type !== "array";
|
|
881
|
+
if (childKey !== null || isLeaf) {
|
|
882
|
+
editing.value = { path: childPath };
|
|
883
|
+
draft.value = draftForValue(childKey, value);
|
|
884
|
+
focusEditorSoon(childKey !== null ? "key" : "value");
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
/* ── Editable mode: the row context menu ────────────────────────── */
|
|
889
|
+
|
|
890
|
+
const ctxMenuApi = useContextMenu();
|
|
891
|
+
|
|
892
|
+
function menuItemsFor(node: HJsonNode): HkMenuItem[] {
|
|
893
|
+
const items: HkMenuItem[] = [];
|
|
894
|
+
if (!node.isContainer) {
|
|
895
|
+
items.push({ key: "jt:edit", label: t("hikari::jsonTree.editValue", "Edit value"), icon: Pencil });
|
|
896
|
+
}
|
|
897
|
+
if (isObjectMember(node)) {
|
|
898
|
+
items.push({ key: "jt:rename", label: t("hikari::jsonTree.renameKey", "Rename key"), icon: KeyRound });
|
|
899
|
+
}
|
|
900
|
+
if (node.isContainer) {
|
|
901
|
+
items.push({
|
|
902
|
+
key: "jt:add",
|
|
903
|
+
label: t("hikari::jsonTree.addChild", "New child"),
|
|
904
|
+
icon: Plus,
|
|
905
|
+
children: INSERT_TYPES.map((type) => ({ key: `jt:add:${type}`, label: typeLabel(type) })),
|
|
906
|
+
});
|
|
907
|
+
}
|
|
908
|
+
if (node.path.length > 0) {
|
|
909
|
+
items.push({ key: "jt:delete", label: t("hikari::jsonTree.delete", "Delete"), icon: Trash2, danger: true });
|
|
910
|
+
}
|
|
911
|
+
return items;
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
function runMenuAction(rawKey: string, node: HJsonNode) {
|
|
915
|
+
if (!canEdit.value) return;
|
|
916
|
+
if (rawKey === "jt:edit") {
|
|
917
|
+
beginEdit(node, "value");
|
|
918
|
+
return;
|
|
919
|
+
}
|
|
920
|
+
if (rawKey === "jt:rename") {
|
|
921
|
+
beginEdit(node, "key");
|
|
922
|
+
return;
|
|
923
|
+
}
|
|
924
|
+
if (rawKey === "jt:delete") {
|
|
925
|
+
const next = jsonRemoveAtPath(props.value, node.path);
|
|
926
|
+
if (next !== props.value) {
|
|
927
|
+
pruneExpansion(node.path);
|
|
928
|
+
const target = editing.value;
|
|
929
|
+
if (target
|
|
930
|
+
&& target.path.length >= node.path.length
|
|
931
|
+
&& node.path.every((seg, i) => seg === target.path[i])) {
|
|
932
|
+
closeEditor();
|
|
933
|
+
}
|
|
934
|
+
emit("update:value", next);
|
|
935
|
+
}
|
|
936
|
+
return;
|
|
937
|
+
}
|
|
938
|
+
if (rawKey.startsWith("jt:add:")) {
|
|
939
|
+
const type = rawKey.slice("jt:add:".length) as JsonInsertType;
|
|
940
|
+
if (INSERT_TYPES.includes(type)) insertChild(node, type);
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
function buildMenu(point: ContextTriggerPoint): ContextMenuRequest | null {
|
|
945
|
+
if (!canEdit.value) return null;
|
|
946
|
+
const rawTarget = point.target;
|
|
947
|
+
const el = rawTarget instanceof Element ? rawTarget.closest("[data-jt-node]") : null;
|
|
948
|
+
if (!el) return null;
|
|
949
|
+
const pk = el.getAttribute("data-jt-node");
|
|
950
|
+
const node = pk ? nodeIndex.value.get(pk) : undefined;
|
|
951
|
+
if (!node) return null;
|
|
952
|
+
return {
|
|
953
|
+
x: point.x,
|
|
954
|
+
y: point.y,
|
|
955
|
+
title: node.key ?? t("hikari::jsonTree.rootTitle", "Root"),
|
|
956
|
+
items: menuItemsFor(node),
|
|
957
|
+
onSelect: (key) => runMenuAction(key, node),
|
|
958
|
+
onClose: () => {},
|
|
959
|
+
};
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
/** The empty-container affordance opens the SAME six-type picker, as a
|
|
963
|
+
* flat menu anchored to the affordance itself. */
|
|
964
|
+
function openAddMenuFor(node: HJsonNode, at: HTMLElement) {
|
|
965
|
+
if (!canEdit.value) return;
|
|
966
|
+
const rect = at.getBoundingClientRect();
|
|
967
|
+
ctxMenuApi.open({
|
|
968
|
+
x: rect.left,
|
|
969
|
+
y: rect.bottom,
|
|
970
|
+
title: t("hikari::jsonTree.addChild", "New child"),
|
|
971
|
+
items: INSERT_TYPES.map((type) => ({ key: `jt:add:${type}`, label: typeLabel(type) })),
|
|
972
|
+
onSelect: (key) => runMenuAction(key, node),
|
|
973
|
+
onClose: () => {},
|
|
974
|
+
});
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
// Bound only when the host opted in: a non-editable tree must not so
|
|
978
|
+
// much as carry the trigger listeners. The binding is setup-static —
|
|
979
|
+
// `editable` does not flap at runtime.
|
|
980
|
+
if (props.editable) {
|
|
981
|
+
useContextMenuTrigger(treeEl, buildMenu);
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
// ── expansion + editor rebuild watch (see note above the helper) ──
|
|
985
|
+
watch(root, (r, prev) => {
|
|
986
|
+
if (!props.editable) {
|
|
987
|
+
expanded.value = r ? initialExpandedSet(r) : new Set<number>();
|
|
988
|
+
return;
|
|
989
|
+
}
|
|
990
|
+
if (!prev) {
|
|
991
|
+
expandedPaths.value = r ? initialExpandedPathSet(r) : new Set<string>();
|
|
992
|
+
}
|
|
993
|
+
// A rebuild may have removed the node under the open editor (the
|
|
994
|
+
// host replaced the value externally) — drop the editor instead of
|
|
995
|
+
// stranding it on a dead path.
|
|
996
|
+
if (editing.value && (!r || !nodeIndex.value.has(jsonPathKey(editing.value.path)))) {
|
|
997
|
+
closeEditor();
|
|
998
|
+
return;
|
|
999
|
+
}
|
|
1000
|
+
// The insert flow opens the editor before the host's commit lands;
|
|
1001
|
+
// finish the deferred focus once the fresh row has mounted.
|
|
1002
|
+
if (editFocusWanted) focusEditorNow();
|
|
1003
|
+
}, { immediate: true });
|
|
1004
|
+
|
|
454
1005
|
/* ── JSON tree renderers (verbatim from HkToolBlock) ─────────── */
|
|
455
1006
|
function renderJsonValue(value: unknown) {
|
|
456
1007
|
if (value === null) return <><span class="s-jv-null">null</span><span class="s-jt-type">null</span></>;
|
|
@@ -471,25 +1022,170 @@ export const HkJsonTree = defineComponent({
|
|
|
471
1022
|
};
|
|
472
1023
|
}
|
|
473
1024
|
|
|
474
|
-
function onParentRowClick(node: HJsonNode) {
|
|
1025
|
+
function onParentRowClick(node: HJsonNode, event: MouseEvent) {
|
|
475
1026
|
if (preserveSelection()) return;
|
|
1027
|
+
// Clicks inside the open inline editor (container rename) must not
|
|
1028
|
+
// toggle the row under the user's caret.
|
|
1029
|
+
if (props.editable && (event.target as HTMLElement | null)?.closest?.(".s-jt-edit")) return;
|
|
476
1030
|
// Deliberately does NOT stop propagation: the root handler already
|
|
477
1031
|
// skips parent rows for the copy path, and hosts that wrapped the
|
|
478
1032
|
// tree in their own click target keep receiving the event exactly as
|
|
479
1033
|
// before the fold work.
|
|
480
|
-
toggleJsonNode(node
|
|
1034
|
+
toggleJsonNode(node);
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
/** The inline editor row for `node` (leaf value edit / rename / fresh
|
|
1038
|
+
* insert). Commits: Enter, or focus leaving the row. Cancels: Escape.
|
|
1039
|
+
* Carries the node's path stamp so a right-click on the editor row
|
|
1040
|
+
* still resolves the row menu to the node being edited. */
|
|
1041
|
+
function renderEditorRow(node: HJsonNode, indent: number) {
|
|
1042
|
+
const d = draft.value;
|
|
1043
|
+
if (!d) return null;
|
|
1044
|
+
const numBad = d.type === "number" && !isNumberText(d.text);
|
|
1045
|
+
const named = isObjectMember(node);
|
|
1046
|
+
const keyBad = renameCollision(node, d.keyText);
|
|
1047
|
+
return (
|
|
1048
|
+
<div
|
|
1049
|
+
class="s-jt-row s-jt-edit"
|
|
1050
|
+
data-editing
|
|
1051
|
+
data-jt-node={canEdit.value ? node.pathKey : undefined}
|
|
1052
|
+
style={{ paddingLeft: `${indent}px` }}
|
|
1053
|
+
onFocusout={maybeBlurCommit}
|
|
1054
|
+
>
|
|
1055
|
+
<span class="s-jt-toggle">
|
|
1056
|
+
<span class="s-jt-leaf" />
|
|
1057
|
+
</span>
|
|
1058
|
+
{named && (
|
|
1059
|
+
<>
|
|
1060
|
+
<input
|
|
1061
|
+
ref={editKeyEl}
|
|
1062
|
+
class="s-jt-input"
|
|
1063
|
+
data-invalid={keyBad || undefined}
|
|
1064
|
+
aria-invalid={keyBad || undefined}
|
|
1065
|
+
value={d.keyText}
|
|
1066
|
+
aria-label={t("hikari::jsonTree.keyLabel", "Key")}
|
|
1067
|
+
spellcheck={false}
|
|
1068
|
+
onInput={(e) => { d.keyText = (e.target as HTMLInputElement).value; }}
|
|
1069
|
+
onKeydown={onEditorKeydown}
|
|
1070
|
+
/>
|
|
1071
|
+
{keyBad && <span class="s-jt-invalid">{t("hikari::jsonTree.duplicateKey", "Key already in use")}</span>}
|
|
1072
|
+
<span class="s-jt-colon">: </span>
|
|
1073
|
+
</>
|
|
1074
|
+
)}
|
|
1075
|
+
{!node.isContainer && (
|
|
1076
|
+
<span class="s-jt-edit-value">
|
|
1077
|
+
<span class="s-jt-chips" role="group">
|
|
1078
|
+
{LEAF_TYPES.map((type) => (
|
|
1079
|
+
<button
|
|
1080
|
+
key={type}
|
|
1081
|
+
type="button"
|
|
1082
|
+
class="s-jt-chip"
|
|
1083
|
+
data-on={d.type === type || undefined}
|
|
1084
|
+
aria-pressed={d.type === type}
|
|
1085
|
+
onClick={() => { d.type = type; }}
|
|
1086
|
+
>
|
|
1087
|
+
{typeLabel(type)}
|
|
1088
|
+
</button>
|
|
1089
|
+
))}
|
|
1090
|
+
</span>
|
|
1091
|
+
{d.type === "string" && (
|
|
1092
|
+
<input
|
|
1093
|
+
ref={editValueEl}
|
|
1094
|
+
class="s-jt-input"
|
|
1095
|
+
value={d.text}
|
|
1096
|
+
aria-label={t("hikari::jsonTree.valueLabel", "Value")}
|
|
1097
|
+
spellcheck={false}
|
|
1098
|
+
onInput={(e) => { d.text = (e.target as HTMLInputElement).value; }}
|
|
1099
|
+
onKeydown={onEditorKeydown}
|
|
1100
|
+
/>
|
|
1101
|
+
)}
|
|
1102
|
+
{d.type === "number" && (
|
|
1103
|
+
<>
|
|
1104
|
+
<input
|
|
1105
|
+
ref={editValueEl}
|
|
1106
|
+
class="s-jt-input"
|
|
1107
|
+
data-invalid={numBad || undefined}
|
|
1108
|
+
value={d.text}
|
|
1109
|
+
aria-label={t("hikari::jsonTree.valueLabel", "Value")}
|
|
1110
|
+
aria-invalid={numBad || undefined}
|
|
1111
|
+
spellcheck={false}
|
|
1112
|
+
inputmode="decimal"
|
|
1113
|
+
onInput={(e) => { d.text = (e.target as HTMLInputElement).value; }}
|
|
1114
|
+
onKeydown={onEditorKeydown}
|
|
1115
|
+
/>
|
|
1116
|
+
{numBad && <span class="s-jt-invalid">{t("hikari::jsonTree.invalidNumber", "Invalid number")}</span>}
|
|
1117
|
+
</>
|
|
1118
|
+
)}
|
|
1119
|
+
{d.type === "boolean" && (
|
|
1120
|
+
<span class="s-jt-chips" role="group">
|
|
1121
|
+
<button
|
|
1122
|
+
type="button"
|
|
1123
|
+
class="s-jt-chip"
|
|
1124
|
+
data-on={d.bool || undefined}
|
|
1125
|
+
aria-pressed={d.bool}
|
|
1126
|
+
onClick={() => { d.bool = true; }}
|
|
1127
|
+
>
|
|
1128
|
+
true
|
|
1129
|
+
</button>
|
|
1130
|
+
<button
|
|
1131
|
+
type="button"
|
|
1132
|
+
class="s-jt-chip"
|
|
1133
|
+
data-on={!d.bool || undefined}
|
|
1134
|
+
aria-pressed={!d.bool}
|
|
1135
|
+
onClick={() => { d.bool = false; }}
|
|
1136
|
+
>
|
|
1137
|
+
false
|
|
1138
|
+
</button>
|
|
1139
|
+
</span>
|
|
1140
|
+
)}
|
|
1141
|
+
{d.type === "null" && <span class="s-jv-null">null</span>}
|
|
1142
|
+
</span>
|
|
1143
|
+
)}
|
|
1144
|
+
</div>
|
|
1145
|
+
);
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
/** The open-empty container's inline "New child" affordance — without
|
|
1149
|
+
* it an empty `{}` could never grow its first child. Clicking opens
|
|
1150
|
+
* the six-type picker anchored here; right-clicking the row falls
|
|
1151
|
+
* through to the container's row menu. */
|
|
1152
|
+
function renderEmptyAdd(node: HJsonNode) {
|
|
1153
|
+
return (
|
|
1154
|
+
<div class="s-jt-row s-jt-empty-row" data-jt-node={node.pathKey}>
|
|
1155
|
+
<span class="s-jt-toggle">
|
|
1156
|
+
<span class="s-jt-leaf" />
|
|
1157
|
+
</span>
|
|
1158
|
+
<button
|
|
1159
|
+
type="button"
|
|
1160
|
+
class="s-jt-empty-add"
|
|
1161
|
+
onClick={(e) => openAddMenuFor(node, e.currentTarget as HTMLElement)}
|
|
1162
|
+
>
|
|
1163
|
+
<Plus size={10} class="s-jt-add-glyph" />
|
|
1164
|
+
{t("hikari::jsonTree.addChild", "New child")}
|
|
1165
|
+
</button>
|
|
1166
|
+
</div>
|
|
1167
|
+
);
|
|
481
1168
|
}
|
|
482
1169
|
|
|
483
1170
|
function renderJsonNode(node: HJsonNode) {
|
|
484
1171
|
const indent = node.depth * JT_INDENT;
|
|
1172
|
+
const editingThis = canEdit.value
|
|
1173
|
+
&& editing.value !== null
|
|
1174
|
+
&& jsonPathKey(editing.value.path) === node.pathKey;
|
|
1175
|
+
// Editable-only: the row → node address the delegated menu resolves
|
|
1176
|
+
// through. Absent in read-only trees (DOM stays byte-identical) and
|
|
1177
|
+
// in text-driven trees (nothing to mutate — no false affordances).
|
|
1178
|
+
const nodeAttr = canEdit.value ? node.pathKey : undefined;
|
|
485
1179
|
|
|
486
1180
|
if (node.isLongString && !node.isContainer) {
|
|
487
|
-
return
|
|
1181
|
+
if (editingThis) return renderEditorRow(node, indent);
|
|
1182
|
+
return renderLongString(node, indent, nodeAttr);
|
|
488
1183
|
}
|
|
489
1184
|
|
|
490
1185
|
if (!node.isContainer) {
|
|
1186
|
+
if (editingThis) return renderEditorRow(node, indent);
|
|
491
1187
|
return (
|
|
492
|
-
<div class="s-jt-row" style={{ paddingLeft: `${indent}px` }}>
|
|
1188
|
+
<div class="s-jt-row" data-jt-node={nodeAttr} style={{ paddingLeft: `${indent}px` }}>
|
|
493
1189
|
<span class="s-jt-toggle">
|
|
494
1190
|
<span class="s-jt-leaf" />
|
|
495
1191
|
</span>
|
|
@@ -500,7 +1196,7 @@ export const HkJsonTree = defineComponent({
|
|
|
500
1196
|
);
|
|
501
1197
|
}
|
|
502
1198
|
|
|
503
|
-
const isOpen =
|
|
1199
|
+
const isOpen = isOpenState(node);
|
|
504
1200
|
const isArr = Array.isArray(node.value);
|
|
505
1201
|
|
|
506
1202
|
// The group wrapper stays mounted in BOTH states: it is the stable
|
|
@@ -511,16 +1207,42 @@ export const HkJsonTree = defineComponent({
|
|
|
511
1207
|
return (
|
|
512
1208
|
<div class="s-jt-group" data-open={isOpen || undefined}>
|
|
513
1209
|
<div
|
|
514
|
-
class="s-jt-row"
|
|
1210
|
+
class={editingThis ? "s-jt-row s-jt-edit" : "s-jt-row"}
|
|
515
1211
|
data-parent
|
|
1212
|
+
data-editing={editingThis || undefined}
|
|
1213
|
+
data-jt-node={nodeAttr}
|
|
516
1214
|
style={{ paddingLeft: `${indent}px` }}
|
|
517
|
-
onClick={() => onParentRowClick(node)}
|
|
1215
|
+
onClick={(e) => onParentRowClick(node, e)}
|
|
1216
|
+
onFocusout={editingThis ? maybeBlurCommit : undefined}
|
|
518
1217
|
>
|
|
519
1218
|
<span class="s-jt-toggle" data-parent>
|
|
520
1219
|
<ChevronRight size={10} class="s-jt-chevron" />
|
|
521
1220
|
</span>
|
|
522
|
-
{
|
|
523
|
-
|
|
1221
|
+
{editingThis && isObjectMember(node)
|
|
1222
|
+
? (
|
|
1223
|
+
<>
|
|
1224
|
+
<input
|
|
1225
|
+
ref={editKeyEl}
|
|
1226
|
+
class="s-jt-input"
|
|
1227
|
+
data-invalid={renameCollision(node, draft.value?.keyText ?? "") || undefined}
|
|
1228
|
+
aria-invalid={renameCollision(node, draft.value?.keyText ?? "") || undefined}
|
|
1229
|
+
value={draft.value?.keyText ?? ""}
|
|
1230
|
+
aria-label={t("hikari::jsonTree.keyLabel", "Key")}
|
|
1231
|
+
spellcheck={false}
|
|
1232
|
+
onInput={(e) => { if (draft.value) draft.value.keyText = (e.target as HTMLInputElement).value; }}
|
|
1233
|
+
onKeydown={onEditorKeydown}
|
|
1234
|
+
/>
|
|
1235
|
+
{renameCollision(node, draft.value?.keyText ?? "")
|
|
1236
|
+
&& <span class="s-jt-invalid">{t("hikari::jsonTree.duplicateKey", "Key already in use")}</span>}
|
|
1237
|
+
<span class="s-jt-colon">: </span>
|
|
1238
|
+
</>
|
|
1239
|
+
)
|
|
1240
|
+
: (
|
|
1241
|
+
<>
|
|
1242
|
+
{node.key !== null && <span class="s-jt-key">{node.key}</span>}
|
|
1243
|
+
{node.key !== null && <span class="s-jt-colon">: </span>}
|
|
1244
|
+
</>
|
|
1245
|
+
)}
|
|
524
1246
|
{isOpen
|
|
525
1247
|
? <span class="s-jt-badge is-open">{isArr ? `Array(${node.childCount})` : `{${node.childCount}}`}</span>
|
|
526
1248
|
: <><span class="s-jt-indent" /><span class="s-jt-preview">{node.preview}</span></>}
|
|
@@ -529,6 +1251,7 @@ export const HkJsonTree = defineComponent({
|
|
|
529
1251
|
{isOpen && (
|
|
530
1252
|
<div class="s-jt-children" style={{ "--guide-left": `${indent + JT_GUIDE_OFFSET}px` }}>
|
|
531
1253
|
{node.children.map(child => renderJsonNode(child))}
|
|
1254
|
+
{canEdit.value && node.childCount === 0 && renderEmptyAdd(node)}
|
|
532
1255
|
</div>
|
|
533
1256
|
)}
|
|
534
1257
|
</Transition>
|
|
@@ -536,16 +1259,17 @@ export const HkJsonTree = defineComponent({
|
|
|
536
1259
|
);
|
|
537
1260
|
}
|
|
538
1261
|
|
|
539
|
-
function renderLongString(node: HJsonNode, indent: number) {
|
|
540
|
-
const isOpen =
|
|
1262
|
+
function renderLongString(node: HJsonNode, indent: number, nodeAttr: string | undefined) {
|
|
1263
|
+
const isOpen = isOpenState(node);
|
|
541
1264
|
|
|
542
1265
|
return (
|
|
543
1266
|
<div class="s-jt-group" data-open={isOpen || undefined}>
|
|
544
1267
|
<div
|
|
545
1268
|
class="s-jt-row"
|
|
546
1269
|
data-parent
|
|
1270
|
+
data-jt-node={nodeAttr}
|
|
547
1271
|
style={{ paddingLeft: `${indent}px` }}
|
|
548
|
-
onClick={() => onParentRowClick(node)}
|
|
1272
|
+
onClick={(e) => onParentRowClick(node, e)}
|
|
549
1273
|
>
|
|
550
1274
|
<span class="s-jt-toggle" data-parent>
|
|
551
1275
|
<ChevronRight size={10} class="s-jt-chevron" />
|
|
@@ -564,7 +1288,12 @@ export const HkJsonTree = defineComponent({
|
|
|
564
1288
|
{isOpen && (
|
|
565
1289
|
<div class="s-jt-children" style={{ "--guide-left": `${indent + JT_GUIDE_OFFSET}px` }}>
|
|
566
1290
|
{node.stringValue.split("\n").map((line, li) => (
|
|
567
|
-
<div
|
|
1291
|
+
<div
|
|
1292
|
+
key={li}
|
|
1293
|
+
class="s-jt-row"
|
|
1294
|
+
data-jt-node={nodeAttr}
|
|
1295
|
+
style={{ paddingLeft: `${(node.depth + 1) * JT_INDENT}px` }}
|
|
1296
|
+
>
|
|
568
1297
|
<span class="s-jt-toggle">
|
|
569
1298
|
<span class="s-jt-leaf" />
|
|
570
1299
|
</span>
|
|
@@ -583,13 +1312,17 @@ export const HkJsonTree = defineComponent({
|
|
|
583
1312
|
return (
|
|
584
1313
|
<div
|
|
585
1314
|
ref={treeEl}
|
|
586
|
-
class={
|
|
1315
|
+
class={["s-tool-json-tree", props.selectable && "s-jt-selectable", canEdit.value && "s-jt-editable"]}
|
|
587
1316
|
aria-label={props.ariaLabel || undefined}
|
|
588
1317
|
role="tree"
|
|
589
1318
|
onClick={(e) => {
|
|
590
1319
|
if (preserveSelection()) return;
|
|
591
1320
|
const target = e.target as HTMLElement;
|
|
592
1321
|
if (target.closest(".s-jt-row[data-parent]")) return;
|
|
1322
|
+
// Editor rows and the empty-container affordance are editing
|
|
1323
|
+
// UI, not copy targets (editable-only; read-only trees never
|
|
1324
|
+
// contain either).
|
|
1325
|
+
if (props.editable && target.closest(".s-jt-edit, .s-jt-empty-row")) return;
|
|
593
1326
|
e.stopPropagation();
|
|
594
1327
|
copyTree();
|
|
595
1328
|
}}
|