@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
|
@@ -0,0 +1,627 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { createApp, defineComponent, h, nextTick, ref } from "vue";
|
|
3
|
+
|
|
4
|
+
import HkContextMenuProvider from "./HkContextMenuProvider";
|
|
5
|
+
import { HkJsonTree } from "./HkJsonTree";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* HkJsonTree editable mode — the opt-in menu-driven editing lane.
|
|
9
|
+
*
|
|
10
|
+
* The read-only viewer is pinned by HkJsonTree.test.tsx /
|
|
11
|
+
* HkJsonTree.contract.test.ts; everything here exercises what ONLY exists
|
|
12
|
+
* when a host passes `editable` with a `value` root: the row context menu
|
|
13
|
+
* (through the shared provider machinery), the inline editor (chips,
|
|
14
|
+
* invalid-number blocking, Enter/Escape/blur), the empty-container
|
|
15
|
+
* affordance and the path-keyed expansion set. Mounting uses a live host
|
|
16
|
+
* component (reactive value + update:value handler) because the tree is a
|
|
17
|
+
* CONTROLLED editor: every commit is emitted with the new root and only
|
|
18
|
+
* becomes visible after the host applies it.
|
|
19
|
+
*
|
|
20
|
+
* Sequencing: the context-menu trigger binds through a watcher on the
|
|
21
|
+
* tree element, so every test settles a tick after mount BEFORE dispatching
|
|
22
|
+
* events on rows.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const mounts: Array<{ app: ReturnType<typeof createApp>; container: HTMLElement }> = [];
|
|
26
|
+
|
|
27
|
+
/** A live host: owns the root value, mirrors commits back into the tree. */
|
|
28
|
+
function mountEditableHost(initial: unknown, opts: { provider?: boolean } = {}) {
|
|
29
|
+
const container = document.createElement("div");
|
|
30
|
+
document.body.appendChild(container);
|
|
31
|
+
const value = ref(initial);
|
|
32
|
+
const emitted: unknown[] = [];
|
|
33
|
+
const Inner = defineComponent({
|
|
34
|
+
name: "JtEditableHost",
|
|
35
|
+
setup() {
|
|
36
|
+
return () =>
|
|
37
|
+
h(HkJsonTree, {
|
|
38
|
+
value: value.value,
|
|
39
|
+
editable: true,
|
|
40
|
+
"onUpdate:value": (v: unknown) => {
|
|
41
|
+
emitted.push(v);
|
|
42
|
+
value.value = v;
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
const app = createApp({
|
|
48
|
+
render: () =>
|
|
49
|
+
opts.provider === false ? h(Inner) : h(HkContextMenuProvider, () => h(Inner)),
|
|
50
|
+
});
|
|
51
|
+
app.mount(container);
|
|
52
|
+
mounts.push({ app, container });
|
|
53
|
+
return { container, value, emitted };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function mountTree(props: Record<string, unknown>) {
|
|
57
|
+
const container = document.createElement("div");
|
|
58
|
+
document.body.appendChild(container);
|
|
59
|
+
const app = createApp({ render: () => h(HkJsonTree, props) });
|
|
60
|
+
app.mount(container);
|
|
61
|
+
mounts.push({ app, container });
|
|
62
|
+
return container;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
afterEach(() => {
|
|
66
|
+
for (const { app, container } of mounts.splice(0)) {
|
|
67
|
+
app.unmount();
|
|
68
|
+
container.remove();
|
|
69
|
+
}
|
|
70
|
+
document
|
|
71
|
+
.querySelectorAll(
|
|
72
|
+
".hk-select-popout, .hk-select-sheet-panel, .hk-select-sheet-scrim",
|
|
73
|
+
)
|
|
74
|
+
.forEach((el) => el.remove());
|
|
75
|
+
// HkMenu panels push back-guard history entries (same discipline as
|
|
76
|
+
// HkMenu's own tests); fake timers must never leak into the next test.
|
|
77
|
+
window.history.replaceState(null, "");
|
|
78
|
+
vi.useRealTimers();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
/** nextTick + a macrotask + nextTick — menus mount/unmount through
|
|
82
|
+
* transitions and history, and the trigger binding lands on a watcher
|
|
83
|
+
* flush, so a single nextTick races them. */
|
|
84
|
+
async function settle(): Promise<void> {
|
|
85
|
+
await nextTick();
|
|
86
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
87
|
+
await nextTick();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Past the fold's report window: the leaving block is really gone. */
|
|
91
|
+
async function settleFold() {
|
|
92
|
+
await vi.advanceTimersByTimeAsync(400);
|
|
93
|
+
await nextTick();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Fake-timer twin of `settle()`: flushes Vue and the faked macrotask
|
|
97
|
+
* queue without ever touching real time. */
|
|
98
|
+
async function settleFake(): Promise<void> {
|
|
99
|
+
await nextTick();
|
|
100
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
101
|
+
await nextTick();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const popouts = () =>
|
|
105
|
+
Array.from(document.querySelectorAll(".hk-select-popout")) as HTMLElement[];
|
|
106
|
+
|
|
107
|
+
const menuRows = () =>
|
|
108
|
+
Array.from(document.querySelectorAll(".hk-menu-row")) as HTMLButtonElement[];
|
|
109
|
+
|
|
110
|
+
function menuRow(label: string): HTMLButtonElement | undefined {
|
|
111
|
+
return menuRows().find((r) => r.textContent?.trim() === label);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The row stamped with the node's path (editable mode's menu address).
|
|
115
|
+
* Attribute-value comparison — happy-dom rejects escaped quotes inside
|
|
116
|
+
* CSS attribute selectors, so the path literal never goes near one. */
|
|
117
|
+
function rowByPath(container: HTMLElement, path: unknown[]): Element | null {
|
|
118
|
+
const attr = JSON.stringify(path);
|
|
119
|
+
return (
|
|
120
|
+
Array.from(container.querySelectorAll("[data-jt-node]")).find(
|
|
121
|
+
(el) => el.getAttribute("data-jt-node") === attr,
|
|
122
|
+
) ?? null
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function contextMenu(el: Element, x = 12, y = 12) {
|
|
127
|
+
el.dispatchEvent(
|
|
128
|
+
new MouseEvent("contextmenu", {
|
|
129
|
+
bubbles: true,
|
|
130
|
+
cancelable: true,
|
|
131
|
+
clientX: x,
|
|
132
|
+
clientY: y,
|
|
133
|
+
}),
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function editorRow(container: HTMLElement): HTMLElement | null {
|
|
138
|
+
return container.querySelector(".s-jt-edit");
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function editorInput(container: HTMLElement, label: "Key" | "Value"): HTMLInputElement | null {
|
|
142
|
+
return container.querySelector(`.s-jt-edit input[aria-label="${label}"]`);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Type into an editor input the way a user's keystrokes arrive. */
|
|
146
|
+
function typeInto(input: HTMLInputElement, text: string) {
|
|
147
|
+
input.value = text;
|
|
148
|
+
input.dispatchEvent(new Event("input", { bubbles: true }));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function pressKey(input: HTMLInputElement, key: string) {
|
|
152
|
+
input.dispatchEvent(new KeyboardEvent("keydown", { key, bubbles: true }));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
describe("HkJsonTree editable opt-in surface", () => {
|
|
156
|
+
it("keeps read-only trees byte-identical: no editable class, no row stamps", () => {
|
|
157
|
+
const el = mountTree({ value: { a: { b: 1 } } });
|
|
158
|
+
const root = el.firstElementChild as HTMLElement;
|
|
159
|
+
expect(root.classList.contains("s-jt-editable")).toBe(false);
|
|
160
|
+
expect(el.querySelector("[data-jt-node]")).toBeNull();
|
|
161
|
+
expect(el.querySelector(".s-jt-empty-add")).toBeNull();
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("marks an editable tree and stamps rows with node paths", async () => {
|
|
165
|
+
const { container } = mountEditableHost({ a: { b: 1 } });
|
|
166
|
+
await settle();
|
|
167
|
+
const root = container.firstElementChild as HTMLElement;
|
|
168
|
+
expect(root.classList.contains("s-jt-editable")).toBe(true);
|
|
169
|
+
expect(rowByPath(container, [])).not.toBeNull();
|
|
170
|
+
expect(rowByPath(container, ["a"])).not.toBeNull();
|
|
171
|
+
expect(rowByPath(container, ["a", "b"])).not.toBeNull();
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("stays read-only for text-driven trees even when editable is on", async () => {
|
|
175
|
+
// Editing pairs with the controlled `value` root; a text-only tree has
|
|
176
|
+
// nothing to mutate, so it must not pretend otherwise.
|
|
177
|
+
const el = mountTree({ text: '{"a":1}', editable: true });
|
|
178
|
+
expect((el.firstElementChild as HTMLElement).classList.contains("s-jt-editable")).toBe(false);
|
|
179
|
+
expect(el.querySelector("[data-jt-node]")).toBeNull();
|
|
180
|
+
contextMenu(el.querySelector(".s-jt-row")!);
|
|
181
|
+
await settle();
|
|
182
|
+
expect(popouts()).toHaveLength(0);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("opens nothing and throws nothing without a provider (NOOP contract)", async () => {
|
|
186
|
+
const { container } = mountEditableHost({ a: 1 }, { provider: false });
|
|
187
|
+
await settle();
|
|
188
|
+
contextMenu(rowByPath(container, ["a"])!);
|
|
189
|
+
await settle();
|
|
190
|
+
expect(popouts()).toHaveLength(0);
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
describe("HkJsonTree row menu", () => {
|
|
195
|
+
it("offers edit/rename/delete for an object-member leaf and deletes it", async () => {
|
|
196
|
+
const { container, emitted } = mountEditableHost({ name: "chest", tries: 3 });
|
|
197
|
+
await settle();
|
|
198
|
+
contextMenu(rowByPath(container, ["name"])!);
|
|
199
|
+
await settle();
|
|
200
|
+
expect(popouts()).toHaveLength(1);
|
|
201
|
+
expect(menuRow("Edit value")).toBeTruthy();
|
|
202
|
+
expect(menuRow("Rename key")).toBeTruthy();
|
|
203
|
+
expect(menuRow("Delete")).toBeTruthy();
|
|
204
|
+
menuRow("Delete")!.click();
|
|
205
|
+
await settle();
|
|
206
|
+
expect(emitted.at(-1)).toEqual({ tries: 3 });
|
|
207
|
+
expect(rowByPath(container, ["name"])).toBeNull();
|
|
208
|
+
expect(rowByPath(container, ["tries"])).not.toBeNull();
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("offers only the add-child submenu for the root", async () => {
|
|
212
|
+
const { container } = mountEditableHost({ a: 1 });
|
|
213
|
+
await settle();
|
|
214
|
+
contextMenu(rowByPath(container, [])!);
|
|
215
|
+
await settle();
|
|
216
|
+
expect(menuRow("Edit value")).toBeUndefined();
|
|
217
|
+
expect(menuRow("Rename key")).toBeUndefined();
|
|
218
|
+
expect(menuRow("Delete")).toBeUndefined();
|
|
219
|
+
expect(menuRow("New child")).toBeTruthy();
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it("cascades the six-type submenu and lands the default with an inline editor", async () => {
|
|
223
|
+
const { container, emitted } = mountEditableHost({ a: 1 });
|
|
224
|
+
await settle();
|
|
225
|
+
contextMenu(rowByPath(container, [])!);
|
|
226
|
+
await settle();
|
|
227
|
+
menuRow("New child")!.click();
|
|
228
|
+
await settle();
|
|
229
|
+
// The submenu carries exactly the six JSON types.
|
|
230
|
+
const typeLabels = ["String", "Number", "Boolean", "Null", "Object", "Array"];
|
|
231
|
+
for (const label of typeLabels) expect(menuRow(label), label).toBeTruthy();
|
|
232
|
+
menuRow("Object")!.click();
|
|
233
|
+
await settle();
|
|
234
|
+
expect(emitted.at(-1)).toEqual({ a: 1, key: {} });
|
|
235
|
+
// The fresh child is open for inline editing (key input focused lane).
|
|
236
|
+
const row = editorRow(container);
|
|
237
|
+
expect(row).not.toBeNull();
|
|
238
|
+
expect(editorInput(container, "Key")).not.toBeNull();
|
|
239
|
+
// The editor replaced the child's row: the new key shows as input, and
|
|
240
|
+
// the value side stays hidden for containers.
|
|
241
|
+
expect(editorInput(container, "Value")).toBeNull();
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it("builds container menus with rename + add-child (no edit-value)", async () => {
|
|
245
|
+
const { container } = mountEditableHost({ cfg: { retries: 2 } });
|
|
246
|
+
await settle();
|
|
247
|
+
contextMenu(rowByPath(container, ["cfg"])!);
|
|
248
|
+
await settle();
|
|
249
|
+
expect(menuRow("Edit value")).toBeUndefined();
|
|
250
|
+
expect(menuRow("Rename key")).toBeTruthy();
|
|
251
|
+
expect(menuRow("New child")).toBeTruthy();
|
|
252
|
+
expect(menuRow("Delete")).toBeTruthy();
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("omits rename for array elements but keeps edit + delete", async () => {
|
|
256
|
+
const { container } = mountEditableHost({ list: [1, 2] });
|
|
257
|
+
await settle();
|
|
258
|
+
contextMenu(rowByPath(container, ["list", "0"])!);
|
|
259
|
+
await settle();
|
|
260
|
+
expect(menuRow("Edit value")).toBeTruthy();
|
|
261
|
+
expect(menuRow("Rename key")).toBeUndefined();
|
|
262
|
+
expect(menuRow("Delete")).toBeTruthy();
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
describe("HkJsonTree inline editor", () => {
|
|
267
|
+
it("commits a new string on Enter and emits the new root", async () => {
|
|
268
|
+
const { container, emitted } = mountEditableHost({ name: "chest", tries: 3 });
|
|
269
|
+
await settle();
|
|
270
|
+
contextMenu(rowByPath(container, ["name"])!);
|
|
271
|
+
await settle();
|
|
272
|
+
menuRow("Edit value")!.click();
|
|
273
|
+
await settle();
|
|
274
|
+
const input = editorInput(container, "Value")!;
|
|
275
|
+
expect(input.value).toBe("chest");
|
|
276
|
+
typeInto(input, "krolva");
|
|
277
|
+
pressKey(input, "Enter");
|
|
278
|
+
await settle();
|
|
279
|
+
expect(emitted.at(-1)).toEqual({ name: "krolva", tries: 3 });
|
|
280
|
+
expect(editorRow(container)).toBeNull();
|
|
281
|
+
// The rebuilt tree shows the committed value.
|
|
282
|
+
expect(container.querySelector(".s-jv-str")!.textContent).toBe('"krolva"');
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it("blocks invalid numbers from committing and shows the invalid state", async () => {
|
|
286
|
+
const { container, emitted } = mountEditableHost({ tries: 3 });
|
|
287
|
+
await settle();
|
|
288
|
+
contextMenu(rowByPath(container, ["tries"])!);
|
|
289
|
+
await settle();
|
|
290
|
+
menuRow("Edit value")!.click();
|
|
291
|
+
await settle();
|
|
292
|
+
// The draft type derives from the committed value: number is active.
|
|
293
|
+
const active = container.querySelector(".s-jt-chip[data-on]")!;
|
|
294
|
+
expect(active.textContent).toBe("Number");
|
|
295
|
+
const input = editorInput(container, "Value")!;
|
|
296
|
+
typeInto(input, "abc");
|
|
297
|
+
await nextTick();
|
|
298
|
+
expect(input.hasAttribute("data-invalid")).toBe(true);
|
|
299
|
+
expect(container.querySelector(".s-jt-invalid")?.textContent).toBe("Invalid number");
|
|
300
|
+
pressKey(input, "Enter");
|
|
301
|
+
await settle();
|
|
302
|
+
expect(emitted).toHaveLength(0); // blocked
|
|
303
|
+
typeInto(input, "42");
|
|
304
|
+
await nextTick();
|
|
305
|
+
expect(input.hasAttribute("data-invalid")).toBe(false);
|
|
306
|
+
pressKey(input, "Enter");
|
|
307
|
+
await settle();
|
|
308
|
+
expect(emitted.at(-1)).toEqual({ tries: 42 });
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it("switches leaf type through the chips (boolean, null)", async () => {
|
|
312
|
+
const { container, emitted } = mountEditableHost({ flag: true });
|
|
313
|
+
await settle();
|
|
314
|
+
contextMenu(rowByPath(container, ["flag"])!);
|
|
315
|
+
await settle();
|
|
316
|
+
menuRow("Edit value")!.click();
|
|
317
|
+
await settle();
|
|
318
|
+
const pick = (label: string) =>
|
|
319
|
+
(Array.from(container.querySelectorAll(".s-jt-chip")) as HTMLButtonElement[])
|
|
320
|
+
.find((c) => c.textContent?.trim() === label)!
|
|
321
|
+
.click();
|
|
322
|
+
pick("Boolean");
|
|
323
|
+
await nextTick();
|
|
324
|
+
// Boolean renders a true/false pair instead of a text input.
|
|
325
|
+
expect(editorInput(container, "Value")).toBeNull();
|
|
326
|
+
const pair = Array.from(container.querySelectorAll(".s-jt-chip")).map((c) =>
|
|
327
|
+
c.textContent?.trim(),
|
|
328
|
+
);
|
|
329
|
+
expect(pair).toContain("true");
|
|
330
|
+
expect(pair).toContain("false");
|
|
331
|
+
pick("false");
|
|
332
|
+
await nextTick();
|
|
333
|
+
pressKey(editorInput(container, "Key")!, "Enter");
|
|
334
|
+
await settle();
|
|
335
|
+
expect(emitted.at(-1)).toEqual({ flag: false });
|
|
336
|
+
|
|
337
|
+
// Null type commits JSON null.
|
|
338
|
+
contextMenu(rowByPath(container, ["flag"])!);
|
|
339
|
+
await settle();
|
|
340
|
+
menuRow("Edit value")!.click();
|
|
341
|
+
await settle();
|
|
342
|
+
pick("Null");
|
|
343
|
+
await nextTick();
|
|
344
|
+
expect(container.querySelector(".s-jt-edit .s-jv-null")?.textContent).toBe("null");
|
|
345
|
+
pressKey(editorInput(container, "Key")!, "Enter");
|
|
346
|
+
await settle();
|
|
347
|
+
expect(emitted.at(-1)).toEqual({ flag: null });
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it("renames an object member key (order may move; content must be exact)", async () => {
|
|
351
|
+
const { container, emitted } = mountEditableHost({ name: "chest", tries: 3 });
|
|
352
|
+
await settle();
|
|
353
|
+
contextMenu(rowByPath(container, ["name"])!);
|
|
354
|
+
await settle();
|
|
355
|
+
menuRow("Rename key")!.click();
|
|
356
|
+
await settle();
|
|
357
|
+
const keyInput = editorInput(container, "Key")!;
|
|
358
|
+
expect(keyInput.value).toBe("name");
|
|
359
|
+
typeInto(keyInput, "title");
|
|
360
|
+
pressKey(keyInput, "Enter");
|
|
361
|
+
await settle();
|
|
362
|
+
expect(emitted.at(-1)).toEqual({ tries: 3, title: "chest" });
|
|
363
|
+
// The rebuilt tree carries the new key.
|
|
364
|
+
expect(rowByPath(container, ["title"])).not.toBeNull();
|
|
365
|
+
expect(rowByPath(container, ["name"])).toBeNull();
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
it("refuses a rename that would overwrite an existing sibling key", async () => {
|
|
369
|
+
const { container, emitted } = mountEditableHost({ name: "chest", tries: 3 });
|
|
370
|
+
await settle();
|
|
371
|
+
contextMenu(rowByPath(container, ["name"])!);
|
|
372
|
+
await settle();
|
|
373
|
+
menuRow("Rename key")!.click();
|
|
374
|
+
await settle();
|
|
375
|
+
const keyInput = editorInput(container, "Key")!;
|
|
376
|
+
// Typing an existing sibling's name raises the invalid state BEFORE
|
|
377
|
+
// any commit attempt…
|
|
378
|
+
typeInto(keyInput, "tries");
|
|
379
|
+
await nextTick();
|
|
380
|
+
expect(keyInput.hasAttribute("data-invalid")).toBe(true);
|
|
381
|
+
expect(container.querySelector(".s-jt-invalid")?.textContent).toBe("Key already in use");
|
|
382
|
+
// …and Enter is refused: no emit, editor stays open, sibling intact.
|
|
383
|
+
pressKey(keyInput, "Enter");
|
|
384
|
+
await settle();
|
|
385
|
+
expect(emitted).toHaveLength(0);
|
|
386
|
+
expect(editorRow(container)).not.toBeNull();
|
|
387
|
+
expect(rowByPath(container, ["tries"])).not.toBeNull(); // sibling survived
|
|
388
|
+
expect(container.querySelector(".s-jv-num")!.textContent).toBe("3");
|
|
389
|
+
// Switching to a unique key unblocks the commit.
|
|
390
|
+
typeInto(keyInput, "title");
|
|
391
|
+
await nextTick();
|
|
392
|
+
expect(keyInput.hasAttribute("data-invalid")).toBe(false);
|
|
393
|
+
pressKey(keyInput, "Enter");
|
|
394
|
+
await settle();
|
|
395
|
+
expect(emitted.at(-1)).toEqual({ tries: 3, title: "chest" });
|
|
396
|
+
expect(editorRow(container)).toBeNull();
|
|
397
|
+
expect(rowByPath(container, ["title"])).not.toBeNull();
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
it("suppresses the emit for a no-op commit (byte-identical rename)", async () => {
|
|
401
|
+
const { container, emitted } = mountEditableHost({ cfg: { retries: 2 } });
|
|
402
|
+
await settle();
|
|
403
|
+
contextMenu(rowByPath(container, ["cfg"])!);
|
|
404
|
+
await settle();
|
|
405
|
+
menuRow("Rename key")!.click();
|
|
406
|
+
await settle();
|
|
407
|
+
// Commit with the key text BYTE-IDENTICAL: the rename composite is
|
|
408
|
+
// skipped (keyText === node.key), a container sets no value, so
|
|
409
|
+
// commitEdit's `next !== props.value` guard (reference identity) sees
|
|
410
|
+
// the untouched root and suppresses the emit entirely.
|
|
411
|
+
pressKey(editorInput(container, "Key")!, "Enter");
|
|
412
|
+
await settle();
|
|
413
|
+
expect(emitted).toHaveLength(0);
|
|
414
|
+
expect(editorRow(container)).toBeNull();
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
it("cancels on Escape without emitting", async () => {
|
|
418
|
+
const { container, emitted } = mountEditableHost({ name: "chest" });
|
|
419
|
+
await settle();
|
|
420
|
+
contextMenu(rowByPath(container, ["name"])!);
|
|
421
|
+
await settle();
|
|
422
|
+
menuRow("Edit value")!.click();
|
|
423
|
+
await settle();
|
|
424
|
+
const input = editorInput(container, "Value")!;
|
|
425
|
+
typeInto(input, "changed");
|
|
426
|
+
pressKey(input, "Escape");
|
|
427
|
+
await settle();
|
|
428
|
+
expect(emitted).toHaveLength(0);
|
|
429
|
+
expect(editorRow(container)).toBeNull();
|
|
430
|
+
expect(container.querySelector(".s-jv-str")!.textContent).toBe('"chest"');
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
it("commits when focus leaves the editor row", async () => {
|
|
434
|
+
const { container, emitted } = mountEditableHost({ name: "chest" });
|
|
435
|
+
await settle();
|
|
436
|
+
contextMenu(rowByPath(container, ["name"])!);
|
|
437
|
+
await settle();
|
|
438
|
+
menuRow("Edit value")!.click();
|
|
439
|
+
await settle();
|
|
440
|
+
const row = editorRow(container)!;
|
|
441
|
+
const input = editorInput(container, "Value")!;
|
|
442
|
+
typeInto(input, "outside");
|
|
443
|
+
// Focus hops INSIDE the row (chip click) must not commit…
|
|
444
|
+
row.dispatchEvent(new FocusEvent("focusout", { relatedTarget: input }));
|
|
445
|
+
await settle();
|
|
446
|
+
expect(emitted).toHaveLength(0);
|
|
447
|
+
expect(editorRow(container)).not.toBeNull();
|
|
448
|
+
// …while focus leaving the row entirely commits.
|
|
449
|
+
row.dispatchEvent(new FocusEvent("focusout", { relatedTarget: document.body }));
|
|
450
|
+
await settle();
|
|
451
|
+
expect(emitted.at(-1)).toEqual({ name: "outside" });
|
|
452
|
+
expect(editorRow(container)).toBeNull();
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
it("keeps the copy-on-click off the editor row and the affordance", async () => {
|
|
456
|
+
const writeText = vi.fn(async () => undefined);
|
|
457
|
+
const original = Object.getOwnPropertyDescriptor(navigator, "clipboard");
|
|
458
|
+
Object.defineProperty(navigator, "clipboard", { configurable: true, value: { writeText } });
|
|
459
|
+
try {
|
|
460
|
+
const { container, emitted } = mountEditableHost({ name: "chest", obj: {} });
|
|
461
|
+
await settle();
|
|
462
|
+
contextMenu(rowByPath(container, ["name"])!);
|
|
463
|
+
await settle();
|
|
464
|
+
menuRow("Edit value")!.click();
|
|
465
|
+
await settle();
|
|
466
|
+
editorRow(container)!.click();
|
|
467
|
+
await nextTick();
|
|
468
|
+
expect(writeText).not.toHaveBeenCalled();
|
|
469
|
+
expect(emitted).toHaveLength(0);
|
|
470
|
+
// The empty-container affordance is editing UI too, not a copy
|
|
471
|
+
// target: clicking it opens the type picker, never the clipboard.
|
|
472
|
+
(container.querySelector(".s-jt-empty-add") as HTMLButtonElement).click();
|
|
473
|
+
await settle();
|
|
474
|
+
expect(writeText).not.toHaveBeenCalled();
|
|
475
|
+
expect(emitted).toHaveLength(0);
|
|
476
|
+
} finally {
|
|
477
|
+
if (original) Object.defineProperty(navigator, "clipboard", original);
|
|
478
|
+
else Reflect.deleteProperty(navigator, "clipboard");
|
|
479
|
+
}
|
|
480
|
+
});
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
describe("HkJsonTree empty-container affordance", () => {
|
|
484
|
+
it("offers the type picker for an open empty object and inserts the default", async () => {
|
|
485
|
+
const { container, emitted } = mountEditableHost({ obj: {} });
|
|
486
|
+
await settle();
|
|
487
|
+
// Empty child containers start expanded (root + direct children).
|
|
488
|
+
const add = container.querySelector(".s-jt-empty-add") as HTMLButtonElement;
|
|
489
|
+
expect(add?.textContent).toContain("New child");
|
|
490
|
+
add.click();
|
|
491
|
+
await settle();
|
|
492
|
+
expect(popouts()).toHaveLength(1);
|
|
493
|
+
expect(menuRow("Array")).toBeTruthy();
|
|
494
|
+
menuRow("String")!.click();
|
|
495
|
+
await settle();
|
|
496
|
+
expect(emitted.at(-1)).toEqual({ obj: { key: "" } });
|
|
497
|
+
// The fresh child opens for inline editing; the affordance row is gone
|
|
498
|
+
// (the container is no longer empty).
|
|
499
|
+
expect(editorInput(container, "Key")).not.toBeNull();
|
|
500
|
+
expect(container.querySelector(".s-jt-empty-add")).toBeNull();
|
|
501
|
+
});
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
describe("HkJsonTree array editing", () => {
|
|
505
|
+
it("appends the default child and opens the value editor (no key input)", async () => {
|
|
506
|
+
const { container, emitted } = mountEditableHost({ list: [1] });
|
|
507
|
+
await settle();
|
|
508
|
+
contextMenu(rowByPath(container, ["list"])!);
|
|
509
|
+
await settle();
|
|
510
|
+
menuRow("New child")!.click();
|
|
511
|
+
await settle();
|
|
512
|
+
menuRow("Number")!.click();
|
|
513
|
+
await settle();
|
|
514
|
+
expect(emitted.at(-1)).toEqual({ list: [1, 0] });
|
|
515
|
+
expect(editorInput(container, "Value")).not.toBeNull();
|
|
516
|
+
expect(editorInput(container, "Key")).toBeNull();
|
|
517
|
+
expect(rowByPath(container, ["list", "1"])).not.toBeNull();
|
|
518
|
+
});
|
|
519
|
+
|
|
520
|
+
it("deletes an array element by index and reindexes", async () => {
|
|
521
|
+
const { container, emitted } = mountEditableHost({ list: [1, 2, 3] });
|
|
522
|
+
await settle();
|
|
523
|
+
contextMenu(rowByPath(container, ["list", "1"])!);
|
|
524
|
+
await settle();
|
|
525
|
+
menuRow("Delete")!.click();
|
|
526
|
+
await settle();
|
|
527
|
+
expect(emitted.at(-1)).toEqual({ list: [1, 3] });
|
|
528
|
+
expect(rowByPath(container, ["list", "0"])).not.toBeNull();
|
|
529
|
+
expect(rowByPath(container, ["list", "1"])).not.toBeNull();
|
|
530
|
+
expect(rowByPath(container, ["list", "2"])).toBeNull();
|
|
531
|
+
});
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
describe("HkJsonTree editable expansion (path-keyed)", () => {
|
|
535
|
+
it("survives a rebuild: a collapsed node stays collapsed after a commit", async () => {
|
|
536
|
+
vi.useFakeTimers();
|
|
537
|
+
const { container, value } = mountEditableHost({ a: { deep: { b: 1 } }, c: 2 });
|
|
538
|
+
await settleFake();
|
|
539
|
+
const groupFor = (path: unknown[]) =>
|
|
540
|
+
rowByPath(container, path)!.closest(".s-jt-group") as HTMLElement;
|
|
541
|
+
// Collapse "a" (fold leaves the children mounted briefly).
|
|
542
|
+
(rowByPath(container, ["a"])! as HTMLElement).click();
|
|
543
|
+
await nextTick();
|
|
544
|
+
await settleFold();
|
|
545
|
+
expect(groupFor(["a"])!.hasAttribute("data-open")).toBe(false);
|
|
546
|
+
// A rebuild (host swaps the value) must NOT re-expand — the expanded
|
|
547
|
+
// set is keyed by path, not by the rebuilt tree's node ids.
|
|
548
|
+
value.value = { a: { deep: { b: 1 } }, c: 3 };
|
|
549
|
+
await nextTick();
|
|
550
|
+
await settleFold();
|
|
551
|
+
expect(groupFor(["a"])!.hasAttribute("data-open")).toBe(false);
|
|
552
|
+
});
|
|
553
|
+
|
|
554
|
+
it("does not reset expansion on an edit commit either", async () => {
|
|
555
|
+
vi.useFakeTimers();
|
|
556
|
+
const { container } = mountEditableHost({ a: { deep: { b: 1 } }, c: 2 });
|
|
557
|
+
await settleFake();
|
|
558
|
+
const groupFor = (path: unknown[]) =>
|
|
559
|
+
rowByPath(container, path)!.closest(".s-jt-group") as HTMLElement;
|
|
560
|
+
(rowByPath(container, ["a"])! as HTMLElement).click();
|
|
561
|
+
await nextTick();
|
|
562
|
+
await settleFold();
|
|
563
|
+
expect(groupFor(["a"])!.hasAttribute("data-open")).toBe(false);
|
|
564
|
+
// Commit an edit on "c" through the real flow: menu → editor → Enter.
|
|
565
|
+
contextMenu(rowByPath(container, ["c"])!);
|
|
566
|
+
await settleFold();
|
|
567
|
+
menuRow("Edit value")!.click();
|
|
568
|
+
await nextTick();
|
|
569
|
+
const input = editorInput(container, "Value")!;
|
|
570
|
+
typeInto(input, "9");
|
|
571
|
+
pressKey(input, "Enter");
|
|
572
|
+
await settleFold();
|
|
573
|
+
expect(editorRow(container)).toBeNull();
|
|
574
|
+
expect(groupFor(["a"])!.hasAttribute("data-open")).toBe(false);
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
it("keeps the non-editable reset behavior untouched (id-keyed reset)", async () => {
|
|
578
|
+
vi.useFakeTimers();
|
|
579
|
+
const container = document.createElement("div");
|
|
580
|
+
document.body.appendChild(container);
|
|
581
|
+
const value = ref({ a: { b: 1 }, c: 2 });
|
|
582
|
+
const app = createApp({
|
|
583
|
+
render: () => h(HkJsonTree, { value: value.value }),
|
|
584
|
+
});
|
|
585
|
+
app.mount(container);
|
|
586
|
+
mounts.push({ app, container });
|
|
587
|
+
await settleFake();
|
|
588
|
+
// Read-only rows carry no path stamps: address the "a" row by its key
|
|
589
|
+
// span (document order makes the first .s-jt-key the root's first
|
|
590
|
+
// member, "a").
|
|
591
|
+
const rowA = container.querySelector(".s-jt-key")!.closest(".s-jt-row")!;
|
|
592
|
+
const groupFor = (el: Element) => el.closest(".s-jt-group") as HTMLElement;
|
|
593
|
+
(rowA as HTMLElement).click();
|
|
594
|
+
await nextTick();
|
|
595
|
+
await settleFold();
|
|
596
|
+
expect(groupFor(rowA).hasAttribute("data-open")).toBe(false);
|
|
597
|
+
value.value = { a: { b: 1 }, c: 3 };
|
|
598
|
+
await nextTick();
|
|
599
|
+
await settleFold();
|
|
600
|
+
// Read-only trees reset to root + direct children on rebuild, exactly
|
|
601
|
+
// as before editable mode existed.
|
|
602
|
+
expect(groupFor(rowA).hasAttribute("data-open")).toBe(true);
|
|
603
|
+
});
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
describe("HkJsonTree long-string editing", () => {
|
|
607
|
+
it("edits the string value from a raw line row's menu", async () => {
|
|
608
|
+
const { container, emitted } = mountEditableHost({ log: "line1\nline2\nline3".padEnd(80, "x") });
|
|
609
|
+
await settle();
|
|
610
|
+
// Direct children start expanded: the raw per-line rows are visible.
|
|
611
|
+
const lineRow = container.querySelector(".s-jv-str-raw")!.closest(".s-jt-row")!;
|
|
612
|
+
expect(lineRow.getAttribute("data-jt-node")).toBe('["log"]');
|
|
613
|
+
contextMenu(lineRow);
|
|
614
|
+
await settle();
|
|
615
|
+
menuRow("Edit value")!.click();
|
|
616
|
+
await settle();
|
|
617
|
+
// The whole long-string group is replaced by the editor row while
|
|
618
|
+
// editing; the raw line rows are gone.
|
|
619
|
+
expect(editorRow(container)).not.toBeNull();
|
|
620
|
+
expect(container.querySelector(".s-jv-str-raw")).toBeNull();
|
|
621
|
+
const input = editorInput(container, "Value")!;
|
|
622
|
+
typeInto(input, "short");
|
|
623
|
+
pressKey(input, "Enter");
|
|
624
|
+
await settle();
|
|
625
|
+
expect(emitted.at(-1)).toEqual({ log: "short" });
|
|
626
|
+
});
|
|
627
|
+
});
|