@nustackdev/ui-core 0.2.0 → 0.2.2
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 +6 -1
- package/src/tree.test.ts +118 -2
- package/src/tree.ts +39 -4
package/package.json
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nustackdev/ui-core",
|
|
3
3
|
"description": "Wire protocol for the Nu.",
|
|
4
|
-
"
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "git+https://github.com/nustackdev/nu.git",
|
|
7
|
+
"directory": "pkgs/ts/ui-core"
|
|
8
|
+
},
|
|
9
|
+
"version": "0.2.2",
|
|
5
10
|
"type": "module",
|
|
6
11
|
"main": "./src/index.ts",
|
|
7
12
|
"types": "./src/index.ts",
|
package/src/tree.test.ts
CHANGED
|
@@ -304,6 +304,38 @@ describe("dispatch", () => {
|
|
|
304
304
|
expect(store.getState().getIn(["t"])?.props.rows).toEqual([{ id: 1 }, { id: 2 }]);
|
|
305
305
|
});
|
|
306
306
|
|
|
307
|
+
it("creates the node from a custom op's chain, then runs its handler", () => {
|
|
308
|
+
const behaviours: Record<string, Behaviour> = {
|
|
309
|
+
Card: {
|
|
310
|
+
handlers: {
|
|
311
|
+
set_title: (ctx, payload) =>
|
|
312
|
+
ctx.update((props) => {
|
|
313
|
+
props.title = String(payload);
|
|
314
|
+
}),
|
|
315
|
+
},
|
|
316
|
+
},
|
|
317
|
+
};
|
|
318
|
+
const store = createTreeStore({ resolve: (t) => behaviours[t] });
|
|
319
|
+
store
|
|
320
|
+
.getState()
|
|
321
|
+
.dispatch(
|
|
322
|
+
frame("set_title", ["col", "card"], "Hi", [
|
|
323
|
+
step("col", "Column"),
|
|
324
|
+
step("card", "Card", { title: "", footer: "" }),
|
|
325
|
+
]),
|
|
326
|
+
);
|
|
327
|
+
expect(store.getState().getIn(["col"])?.type).toBe("Column");
|
|
328
|
+
expect(store.getState().getIn(["col", "card"])?.props).toEqual({ title: "Hi", footer: "" });
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it("still reports a custom op with no chain on a missing node", () => {
|
|
332
|
+
const onError = vi.fn();
|
|
333
|
+
const store = createTreeStore({ onError });
|
|
334
|
+
store.getState().dispatch(frame("set_title", ["nope"], "Hi"));
|
|
335
|
+
expect(onError).toHaveBeenCalledOnce();
|
|
336
|
+
expect(store.getState().getIn(["nope"])).toBeNull();
|
|
337
|
+
});
|
|
338
|
+
|
|
307
339
|
it("gives a type's own write handler the raw payload", () => {
|
|
308
340
|
const behaviours: Record<string, Behaviour> = {
|
|
309
341
|
InputRef: {
|
|
@@ -348,14 +380,14 @@ describe("dispatch", () => {
|
|
|
348
380
|
it("lets a type answer a read itself", () => {
|
|
349
381
|
const sent: TreeFrame[] = [];
|
|
350
382
|
const behaviours: Record<string, Behaviour> = {
|
|
351
|
-
|
|
383
|
+
MarkdownRef: {
|
|
352
384
|
handlers: {
|
|
353
385
|
read: (ctx) => ctx.send(OPS.read, `md:${ctx.node.props.value}`, ctx.frame.id),
|
|
354
386
|
},
|
|
355
387
|
},
|
|
356
388
|
};
|
|
357
389
|
const store = createTreeStore({ send: (f) => sent.push(f), resolve: (t) => behaviours[t] });
|
|
358
|
-
store.getState().write([step("p", "
|
|
390
|
+
store.getState().write([step("p", "MarkdownRef", { value: "hi" })]);
|
|
359
391
|
store.getState().dispatch({ op: OPS.read, ref: ["p"], payload: null, id: "f2" });
|
|
360
392
|
expect(sent).toEqual([{ op: OPS.read, ref: ["p"], payload: "md:hi", id: "f2" }]);
|
|
361
393
|
});
|
|
@@ -376,6 +408,90 @@ describe("dispatch", () => {
|
|
|
376
408
|
});
|
|
377
409
|
});
|
|
378
410
|
|
|
411
|
+
describe("visibility", () => {
|
|
412
|
+
it("hides a node and keeps its props and children", () => {
|
|
413
|
+
const store = createTreeStore();
|
|
414
|
+
store.getState().write([step("a", "Card", { title: "T" }), step("b", "TextRef")], { value: "x" });
|
|
415
|
+
store.getState().dispatch(frame(OPS.hide, ["a"], null, [step("a", "Card", { title: "T" })]));
|
|
416
|
+
const a = store.getState().getIn(["a"]);
|
|
417
|
+
expect(a?.hidden).toBe(true);
|
|
418
|
+
expect(a?.props).toEqual({ title: "T" });
|
|
419
|
+
expect(store.getState().getIn(["a", "b"])?.props).toEqual({ value: "x" });
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
it("shows it again on display", () => {
|
|
423
|
+
const store = createTreeStore();
|
|
424
|
+
store.getState().write([step("a", "TextRef")]);
|
|
425
|
+
store.getState().dispatch(frame(OPS.hide, ["a"], null, [step("a", "TextRef")]));
|
|
426
|
+
store.getState().dispatch(frame(OPS.display, ["a"], null, [step("a", "TextRef")]));
|
|
427
|
+
expect(store.getState().getIn(["a"])?.hidden).toBeUndefined();
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
it("brings a node into being hidden when the hide is the first frame", () => {
|
|
431
|
+
const store = createTreeStore();
|
|
432
|
+
store.getState().dispatch(
|
|
433
|
+
frame(OPS.hide, ["a", "b"], null, [step("a", "Column"), step("b", "TextRef", { value: "seed" })]),
|
|
434
|
+
);
|
|
435
|
+
const b = store.getState().getIn(["a", "b"]);
|
|
436
|
+
expect(b?.type).toBe("TextRef");
|
|
437
|
+
expect(b?.props).toEqual({ value: "seed" });
|
|
438
|
+
expect(b?.hidden).toBe(true);
|
|
439
|
+
expect(store.getState().getIn(["a"])?.hidden).toBeUndefined();
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
it("keeps the flag through prop merges and a type's own write handler", () => {
|
|
443
|
+
const behaviours: Record<string, Behaviour> = {
|
|
444
|
+
HeadingRef: {
|
|
445
|
+
handlers: {
|
|
446
|
+
write: (ctx, payload) =>
|
|
447
|
+
ctx.update((props) => {
|
|
448
|
+
props.label = String(payload);
|
|
449
|
+
}),
|
|
450
|
+
},
|
|
451
|
+
},
|
|
452
|
+
};
|
|
453
|
+
const store = createTreeStore({ resolve: (t) => behaviours[t] });
|
|
454
|
+
store.getState().write([step("h", "HeadingRef"), step("t", "TextRef")]);
|
|
455
|
+
store.getState().dispatch(frame(OPS.hide, ["h"], null, [step("h", "HeadingRef")]));
|
|
456
|
+
store.getState().dispatch(frame(OPS.hide, ["h", "t"], null, [step("h", "HeadingRef"), step("t", "TextRef")]));
|
|
457
|
+
store.getState().dispatch(frame(OPS.write, ["h"], "hello", [step("h", "HeadingRef")]));
|
|
458
|
+
store.getState().dispatch(
|
|
459
|
+
frame(OPS.write, ["h", "t"], { value: 1, hidden: false }, [step("h", "HeadingRef"), step("t", "TextRef")]),
|
|
460
|
+
);
|
|
461
|
+
expect(store.getState().getIn(["h"])?.hidden).toBe(true);
|
|
462
|
+
expect(store.getState().getIn(["h"])?.props.label).toBe("hello");
|
|
463
|
+
expect(store.getState().getIn(["h", "t"])?.hidden).toBe(true);
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
it("never asks a type's handlers", () => {
|
|
467
|
+
const errors: string[] = [];
|
|
468
|
+
const hide = vi.fn();
|
|
469
|
+
const behaviours: Record<string, Behaviour> = { TextRef: { handlers: { hide } } };
|
|
470
|
+
const store = createTreeStore({ resolve: (t) => behaviours[t], onError: (m) => errors.push(m) });
|
|
471
|
+
store.getState().write([step("a", "TextRef")]);
|
|
472
|
+
store.getState().dispatch(frame(OPS.hide, ["a"], null, [step("a", "TextRef")]));
|
|
473
|
+
expect(hide).not.toHaveBeenCalled();
|
|
474
|
+
expect(errors).toEqual([]);
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
it("leaves an untouched node's identity alone on a repeat", () => {
|
|
478
|
+
const store = createTreeStore();
|
|
479
|
+
store.getState().write([step("a", "TextRef")]);
|
|
480
|
+
store.getState().dispatch(frame(OPS.hide, ["a"], null, [step("a", "TextRef")]));
|
|
481
|
+
const root = store.getState().root;
|
|
482
|
+
store.getState().dispatch(frame(OPS.hide, ["a"], null, [step("a", "TextRef")]));
|
|
483
|
+
expect(store.getState().root).toBe(root);
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
it("still removes a hidden node", () => {
|
|
487
|
+
const store = createTreeStore();
|
|
488
|
+
store.getState().write([step("a", "TextRef")]);
|
|
489
|
+
store.getState().dispatch(frame(OPS.hide, ["a"], null, [step("a", "TextRef")]));
|
|
490
|
+
store.getState().dispatch(frame(OPS.remove, ["a"]));
|
|
491
|
+
expect(store.getState().getIn(["a"])).toBeNull();
|
|
492
|
+
});
|
|
493
|
+
});
|
|
494
|
+
|
|
379
495
|
describe("send", () => {
|
|
380
496
|
it("drops frames until a sender is set, then delivers", () => {
|
|
381
497
|
const sent: TreeFrame[] = [];
|
package/src/tree.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
// The browser store, as a tree.
|
|
2
2
|
//
|
|
3
|
-
// One node is
|
|
4
|
-
// keyed by segment
|
|
5
|
-
//
|
|
6
|
-
// own -- a node's current value
|
|
3
|
+
// One node is three things: a `type`, a bag of `props`, and `children`
|
|
4
|
+
// keyed by segment (plus the `hidden` flag the visibility ops set). `type` is
|
|
5
|
+
// null for a node that exists only because something below it was written.
|
|
6
|
+
// `value` is just a prop, not a field of its own -- a node's current value
|
|
7
|
+
// lives at `props.value` like everything else.
|
|
7
8
|
//
|
|
8
9
|
// Writes autovivify. A frame carries a `chain`, root-first, one triple per
|
|
9
10
|
// level, so a write to `a.b.c` creates `a` and `b` with the type and props
|
|
@@ -31,10 +32,14 @@ export type Props = Record<string, unknown>;
|
|
|
31
32
|
// A Map and not a plain object: render order is insertion order, and an
|
|
32
33
|
// object hoists integer-like keys to the front, so a segment like "0" or an
|
|
33
34
|
// index from a dynamic ref would silently jump the queue.
|
|
35
|
+
//
|
|
36
|
+
// `hidden` lives on the node and not in its props, so no prop merge and no
|
|
37
|
+
// type's own write handler can clobber it. Unset means shown.
|
|
34
38
|
export type Node = {
|
|
35
39
|
type: string | null;
|
|
36
40
|
props: Props;
|
|
37
41
|
children: Map<string, Node>;
|
|
42
|
+
hidden?: boolean;
|
|
38
43
|
};
|
|
39
44
|
|
|
40
45
|
/** Alias for consumers where the DOM's `Node` is also in scope. */
|
|
@@ -63,6 +68,11 @@ export const OPS = {
|
|
|
63
68
|
// write to it. What a boot batch is made of.
|
|
64
69
|
init: "init",
|
|
65
70
|
remove: "remove",
|
|
71
|
+
// Chain only, no payload, like init. Hide keeps the node, its props and
|
|
72
|
+
// its children (state survives, the browser just draws nothing); display
|
|
73
|
+
// shows it again. Every type takes them, none handles them.
|
|
74
|
+
hide: "hide",
|
|
75
|
+
display: "display",
|
|
66
76
|
read: "read",
|
|
67
77
|
notify: "notify",
|
|
68
78
|
} as const;
|
|
@@ -101,6 +111,8 @@ export type TreeActions = {
|
|
|
101
111
|
getIn: (path: Path) => Node | null;
|
|
102
112
|
/** Merge props into an existing node. No-op when the node is not there. */
|
|
103
113
|
setProps: (path: Path, props: Props) => void;
|
|
114
|
+
/** Hide or show an existing node. No-op when the node is not there. */
|
|
115
|
+
setHidden: (path: Path, hidden: boolean) => void;
|
|
104
116
|
/** Route an inbound frame to its node, by node type and frame op. */
|
|
105
117
|
dispatch: (frame: TreeFrame) => void;
|
|
106
118
|
/** Outbound. No-op until a sender is set. */
|
|
@@ -266,6 +278,17 @@ export function createTreeStore(options: TreeStoreOptions = {}): TreeStore {
|
|
|
266
278
|
Object.assign(node.props, props);
|
|
267
279
|
}),
|
|
268
280
|
|
|
281
|
+
setHidden: (path, hidden) => {
|
|
282
|
+
const node = getNode(get().root, path);
|
|
283
|
+
if (!node || Boolean(node.hidden) === hidden) return;
|
|
284
|
+
set((draft) => {
|
|
285
|
+
const target = getNode(draft.root as Node, path);
|
|
286
|
+
if (!target) return;
|
|
287
|
+
if (hidden) target.hidden = true;
|
|
288
|
+
else delete target.hidden;
|
|
289
|
+
});
|
|
290
|
+
},
|
|
291
|
+
|
|
269
292
|
send: (frame) => {
|
|
270
293
|
if (outbound) outbound(frame);
|
|
271
294
|
},
|
|
@@ -291,6 +314,13 @@ export function createTreeStore(options: TreeStoreOptions = {}): TreeStore {
|
|
|
291
314
|
api.write(chain);
|
|
292
315
|
return;
|
|
293
316
|
}
|
|
317
|
+
if (frame.op === OPS.hide || frame.op === OPS.display) {
|
|
318
|
+
// Structure first (the node may not exist yet), then the
|
|
319
|
+
// flag. Generic for every type, never a handler's call.
|
|
320
|
+
api.write(chain);
|
|
321
|
+
api.setHidden(frame.ref, frame.op === OPS.hide);
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
294
324
|
const handlerFor = (node: Node | null) => {
|
|
295
325
|
const behaviour = node?.type ? resolve(node.type) : undefined;
|
|
296
326
|
return behaviour?.handlers?.[frame.op];
|
|
@@ -312,6 +342,11 @@ export function createTreeStore(options: TreeStoreOptions = {}): TreeStore {
|
|
|
312
342
|
}
|
|
313
343
|
return;
|
|
314
344
|
}
|
|
345
|
+
// Any other op that carries a chain brings its node into being
|
|
346
|
+
// first, the same way a write does: a Card's `set_title` may be
|
|
347
|
+
// the first frame that ever names that Card. A frame with no
|
|
348
|
+
// chain is addressed at a node that should already be there.
|
|
349
|
+
if (frame.chain) api.write(chain);
|
|
315
350
|
const node = api.getIn(frame.ref);
|
|
316
351
|
if (!node) {
|
|
317
352
|
onError(`no node for op "${frame.op}"`, frame.ref);
|