@derivesome/tree-web 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/.package.json.~undo-tree~ +10 -0
  2. package/.tsconfig.json.~undo-tree~ +6 -0
  3. package/dist/cjs/css.d.ts +5 -0
  4. package/dist/cjs/css.d.ts.map +1 -0
  5. package/dist/cjs/css.js +14 -0
  6. package/dist/cjs/css.js.map +1 -0
  7. package/dist/cjs/index.d.ts +5 -0
  8. package/dist/cjs/index.d.ts.map +1 -0
  9. package/dist/cjs/index.js +21 -0
  10. package/dist/cjs/index.js.map +1 -0
  11. package/dist/cjs/jsx-globals.d.ts +826 -0
  12. package/dist/cjs/jsx-globals.d.ts.map +1 -0
  13. package/dist/cjs/jsx-globals.js +18 -0
  14. package/dist/cjs/jsx-globals.js.map +1 -0
  15. package/dist/cjs/mount.d.ts +2 -0
  16. package/dist/cjs/mount.d.ts.map +1 -0
  17. package/dist/cjs/mount.js +9 -0
  18. package/dist/cjs/mount.js.map +1 -0
  19. package/dist/cjs/renderer.d.ts +3 -0
  20. package/dist/cjs/renderer.d.ts.map +1 -0
  21. package/dist/cjs/renderer.js +123 -0
  22. package/dist/cjs/renderer.js.map +1 -0
  23. package/dist/esm/css.d.ts +5 -0
  24. package/dist/esm/css.d.ts.map +1 -0
  25. package/dist/esm/css.js +14 -0
  26. package/dist/esm/css.js.map +1 -0
  27. package/dist/esm/index.d.ts +5 -0
  28. package/dist/esm/index.d.ts.map +1 -0
  29. package/dist/esm/index.js +21 -0
  30. package/dist/esm/index.js.map +1 -0
  31. package/dist/esm/jsx-globals.d.ts +826 -0
  32. package/dist/esm/jsx-globals.d.ts.map +1 -0
  33. package/dist/esm/jsx-globals.js +18 -0
  34. package/dist/esm/jsx-globals.js.map +1 -0
  35. package/dist/esm/mount.d.ts +2 -0
  36. package/dist/esm/mount.d.ts.map +1 -0
  37. package/dist/esm/mount.js +9 -0
  38. package/dist/esm/mount.js.map +1 -0
  39. package/dist/esm/renderer.d.ts +3 -0
  40. package/dist/esm/renderer.d.ts.map +1 -0
  41. package/dist/esm/renderer.js +123 -0
  42. package/dist/esm/renderer.js.map +1 -0
  43. package/package.json +47 -0
  44. package/package.json~ +53 -0
  45. package/src/.context.ts.~undo-tree~ +6 -0
  46. package/src/.css.ts.~undo-tree~ +6 -0
  47. package/src/.index.ts.~undo-tree~ +8 -0
  48. package/src/.jsx-globals.ts.~undo-tree~ +192 -0
  49. package/src/.jsx-types.ts.~undo-tree~ +23 -0
  50. package/src/.mount.test.ts.~undo-tree~ +438 -0
  51. package/src/.mount.ts.~undo-tree~ +16 -0
  52. package/src/.renderer.ts.~undo-tree~ +96 -0
  53. package/src/.tree.ts.~undo-tree~ +489 -0
  54. package/src/.velement.ts.~undo-tree~ +1738 -0
  55. package/src/context.ts~ +0 -0
  56. package/src/css.ts +13 -0
  57. package/src/css.ts~ +13 -0
  58. package/src/index.ts +4 -0
  59. package/src/index.ts~ +4 -0
  60. package/src/jsx-globals.ts +861 -0
  61. package/src/jsx-globals.ts~ +854 -0
  62. package/src/jsx-types.ts~ +772 -0
  63. package/src/mount.test.ts~ +375 -0
  64. package/src/mount.ts +6 -0
  65. package/src/mount.ts~ +10 -0
  66. package/src/renderer.ts +133 -0
  67. package/src/renderer.ts~ +133 -0
  68. package/src/tree.ts~ +212 -0
  69. package/src/velement.ts~ +856 -0
  70. package/tsconfig.cjs.json +10 -0
  71. package/tsconfig.esm.json +10 -0
  72. package/tsconfig.json +23 -0
  73. package/tsconfig.json~ +23 -0
package/src/tree.ts~ ADDED
@@ -0,0 +1,212 @@
1
+ import {
2
+ derived,
3
+ has,
4
+ isReference,
5
+ LooseDict,
6
+ ref,
7
+ Reference,
8
+ } from "@derivesome/core";
9
+
10
+ export const TreeNodeBrand = "TreeNode" as const;
11
+ export type TreeNodeBrand = typeof TreeNodeBrand;
12
+
13
+ export type TreeNodeProps = LooseDict & {
14
+ children?: unknown;
15
+ style?: unknown;
16
+ id?: unknown;
17
+ key?: string | number;
18
+ };
19
+
20
+ export type TreeNodeFn<T extends TreeNodeProps = TreeNodeProps> = (
21
+ props: TreeNodeProps & T,
22
+ ) => unknown;
23
+
24
+ export interface TreeNodeBase<Type extends string> {
25
+ type: Type;
26
+ props: TreeNodeProps;
27
+ [TreeNodeBrand]: true;
28
+ }
29
+
30
+ export interface TreeNodeElement extends TreeNodeBase<"element"> {
31
+ tag: string;
32
+ children: TreeNode[];
33
+ }
34
+
35
+ export interface TreeNodeValue extends TreeNodeBase<"value"> {
36
+ value: unknown;
37
+ }
38
+
39
+ export interface TreeNodeVoid extends TreeNodeBase<"void"> {}
40
+
41
+ export interface TreeNodeFunction<
42
+ P extends TreeNodeProps = TreeNodeProps,
43
+ > extends TreeNodeBase<"function"> {
44
+ fn: TreeNodeFn<P>;
45
+ }
46
+
47
+ export interface TreeNodeList<T = any> extends TreeNodeBase<"list"> {
48
+ props: Omit<TreeNodeProps, "children"> & {
49
+ children: Reference<T[]>;
50
+ };
51
+ items: Reference<TreeNode[]>;
52
+ map: (item: T, idx: number) => TreeNode;
53
+ }
54
+
55
+ export type TreeNode =
56
+ | TreeNodeElement
57
+ | TreeNodeValue
58
+ | TreeNodeVoid
59
+ | TreeNodeFunction
60
+ | TreeNodeList;
61
+
62
+ const makeBase = <Type extends string>(
63
+ t: Type,
64
+ ): { type: Type; [TreeNodeBrand]: true } => {
65
+ return {
66
+ type: t,
67
+ [TreeNodeBrand]: true,
68
+ };
69
+ };
70
+
71
+ export function isTreeNode(x: unknown): x is TreeNode {
72
+ return has(x, TreeNodeBrand) && x[TreeNodeBrand] === true;
73
+ }
74
+
75
+ // tn - short for (TreeNode)
76
+ export namespace tn {
77
+ export function none(): TreeNodeVoid {
78
+ return {
79
+ ...makeBase("void"),
80
+ props: {},
81
+ };
82
+ }
83
+
84
+ export function list<T>(
85
+ items: Reference<T[]> | T[],
86
+ map?: (item: T, idx: number) => TreeNode,
87
+ ): TreeNodeList {
88
+ const mapFn = map || ((x) => tnu.normalizeOne(x));
89
+ const its = isReference(items) ? items : ref(items);
90
+ return {
91
+ ...makeBase("list"),
92
+ props: {
93
+ children: its,
94
+ },
95
+ map: mapFn,
96
+ items: derived(() => its.get().map(mapFn)),
97
+ };
98
+ }
99
+
100
+ export function value(v: unknown): TreeNodeValue {
101
+ return {
102
+ ...makeBase("value"),
103
+ props: {},
104
+ value: v,
105
+ };
106
+ }
107
+
108
+ export function element(tag: string, props: TreeNodeProps): TreeNodeElement {
109
+ return {
110
+ ...makeBase("element"),
111
+ tag,
112
+ props,
113
+ children: tnu.normalizeChildren(props.children),
114
+ };
115
+ }
116
+
117
+ export function fn(
118
+ fun: TreeNodeFn,
119
+ props: TreeNodeProps = {},
120
+ ): TreeNodeFunction {
121
+ return {
122
+ ...makeBase("function"),
123
+ fn: fun,
124
+ props: props,
125
+ };
126
+ }
127
+ }
128
+
129
+ // tnu - short for (TreeNode Utils)
130
+ export namespace tnu {
131
+ export const normalizeOne = (x: unknown): TreeNode => {
132
+ if (isTreeNode(x)) return x;
133
+ if (typeof x === "undefined" || x === null || x === false) return tn.none();
134
+ if (
135
+ typeof x === "string" ||
136
+ typeof x === "number" ||
137
+ typeof x === "boolean"
138
+ )
139
+ return tn.value(x);
140
+ if (Array.isArray(x)) {
141
+ if (x.length === 1) {
142
+ const first = x[0]!;
143
+ if (isTreeNode(first) && first.type === "list") return first;
144
+ }
145
+ return tn.list(x.map((v) => normalizeOne(v)));
146
+ }
147
+ if (typeof x === "function")
148
+ return tn.fn((props) => normalizeOne((x as TreeNodeFn)(props)));
149
+ throw new Error(`normalizeOne: unhandled ${typeof x}`);
150
+ };
151
+
152
+ export const normalizeChildren = (x: unknown): TreeNode[] => {
153
+ if (isTreeNode(x)) return [x];
154
+ if (typeof x === "function") return [tn.value(x)];
155
+ if (Array.isArray(x)) return x.flatMap((v) => normalizeChildren(v));
156
+ if (isReference(x)) {
157
+ const val = x.peek();
158
+ if (Array.isArray(val))
159
+ return [
160
+ tn.list(
161
+ derived(() =>
162
+ ((x.get() || []) as Array<unknown>).flatMap((v) =>
163
+ tnu.normalizeChildren(v),
164
+ ),
165
+ ),
166
+ ),
167
+ ];
168
+ return [tn.value(x)];
169
+ }
170
+ return [normalizeOne(x)];
171
+ };
172
+
173
+ export const assertGetKey = (x: TreeNode): string | number => {
174
+ const k = x.props.key;
175
+ if (!(typeof k === "string" || typeof k === "number"))
176
+ throw new Error(`Each item in a list must have a key`);
177
+ return k;
178
+ };
179
+ }
180
+
181
+ export function createNode(
182
+ tag: string,
183
+ props?: TreeNodeProps | null,
184
+ ...children: unknown[]
185
+ ): TreeNodeElement;
186
+ export function createNode<P extends TreeNodeProps>(
187
+ tag: TreeNodeFn<P>,
188
+ props?: P | null,
189
+ ...children: unknown[]
190
+ ): TreeNodeFunction;
191
+ export function createNode(
192
+ tag: string | TreeNodeFn,
193
+ props?: TreeNodeProps | null,
194
+ ...children: unknown[]
195
+ ): TreeNode {
196
+ const normalizedProps: TreeNodeProps = props ?? {};
197
+ const normalizedChildren = tnu.normalizeChildren(children);
198
+
199
+ if (typeof tag === "function") {
200
+ return tn.fn(tag, {
201
+ ...normalizedProps,
202
+ ...(normalizedChildren.length > 0
203
+ ? { children: normalizedChildren }
204
+ : {}),
205
+ });
206
+ }
207
+
208
+ return tn.element(tag, {
209
+ ...props,
210
+ children: normalizedChildren,
211
+ });
212
+ }