@elliemae/ds-legacy-tree-model 1.0.0-rc.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 (54) hide show
  1. package/dist/cjs/DSTree.js +111 -0
  2. package/dist/cjs/DSTree.js.map +7 -0
  3. package/dist/cjs/Node.js +154 -0
  4. package/dist/cjs/Node.js.map +7 -0
  5. package/dist/cjs/adapters/arraish-tree/fromDSTreeToTreeishArray.js +83 -0
  6. package/dist/cjs/adapters/arraish-tree/fromDSTreeToTreeishArray.js.map +7 -0
  7. package/dist/cjs/adapters/arraish-tree/fromTreeishArrayToDSTree.js +82 -0
  8. package/dist/cjs/adapters/arraish-tree/fromTreeishArrayToDSTree.js.map +7 -0
  9. package/dist/cjs/adapters/arraish-tree/useArraishAsDSTree.js +116 -0
  10. package/dist/cjs/adapters/arraish-tree/useArraishAsDSTree.js.map +7 -0
  11. package/dist/cjs/adapters/treeview/useTreeviewAsDSTree.js +47 -0
  12. package/dist/cjs/adapters/treeview/useTreeviewAsDSTree.js.map +7 -0
  13. package/dist/cjs/index.js +46 -0
  14. package/dist/cjs/index.js.map +7 -0
  15. package/dist/cjs/package.json +7 -0
  16. package/dist/cjs/types.js +28 -0
  17. package/dist/cjs/types.js.map +7 -0
  18. package/dist/cjs/useDSTree.js +97 -0
  19. package/dist/cjs/useDSTree.js.map +7 -0
  20. package/dist/esm/DSTree.js +81 -0
  21. package/dist/esm/DSTree.js.map +7 -0
  22. package/dist/esm/Node.js +124 -0
  23. package/dist/esm/Node.js.map +7 -0
  24. package/dist/esm/adapters/arraish-tree/fromDSTreeToTreeishArray.js +53 -0
  25. package/dist/esm/adapters/arraish-tree/fromDSTreeToTreeishArray.js.map +7 -0
  26. package/dist/esm/adapters/arraish-tree/fromTreeishArrayToDSTree.js +52 -0
  27. package/dist/esm/adapters/arraish-tree/fromTreeishArrayToDSTree.js.map +7 -0
  28. package/dist/esm/adapters/arraish-tree/useArraishAsDSTree.js +86 -0
  29. package/dist/esm/adapters/arraish-tree/useArraishAsDSTree.js.map +7 -0
  30. package/dist/esm/adapters/treeview/useTreeviewAsDSTree.js +17 -0
  31. package/dist/esm/adapters/treeview/useTreeviewAsDSTree.js.map +7 -0
  32. package/dist/esm/index.js +16 -0
  33. package/dist/esm/index.js.map +7 -0
  34. package/dist/esm/package.json +7 -0
  35. package/dist/esm/types.js +2 -0
  36. package/dist/esm/types.js.map +7 -0
  37. package/dist/esm/useDSTree.js +67 -0
  38. package/dist/esm/useDSTree.js.map +7 -0
  39. package/dist/types/DSTree.d.ts +28 -0
  40. package/dist/types/Node.d.ts +25 -0
  41. package/dist/types/adapters/arraish-tree/fromDSTreeToTreeishArray.d.ts +9 -0
  42. package/dist/types/adapters/arraish-tree/fromTreeishArrayToDSTree.d.ts +9 -0
  43. package/dist/types/adapters/arraish-tree/useArraishAsDSTree.d.ts +22 -0
  44. package/dist/types/adapters/treeview/useTreeviewAsDSTree.d.ts +23 -0
  45. package/dist/types/index.d.ts +8 -0
  46. package/dist/types/tests/adapters/arraish-tree/fromDSTreeToTreeishArray.test.d.ts +1 -0
  47. package/dist/types/tests/adapters/arraish-tree/fromTreeishArrayToDSTree.test.d.ts +1 -0
  48. package/dist/types/tests/model.read.test.d.ts +1 -0
  49. package/dist/types/tests/model.write.test.d.ts +1 -0
  50. package/dist/types/tests/tree.read.test.d.ts +1 -0
  51. package/dist/types/tests/tree.write.test.d.ts +1 -0
  52. package/dist/types/types.d.ts +33 -0
  53. package/dist/types/useDSTree.d.ts +19 -0
  54. package/package.json +61 -0
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var useTreeviewAsDSTree_exports = {};
30
+ __export(useTreeviewAsDSTree_exports, {
31
+ useTreeviewAsDSTree: () => useTreeviewAsDSTree
32
+ });
33
+ module.exports = __toCommonJS(useTreeviewAsDSTree_exports);
34
+ var React = __toESM(require("react"));
35
+ var import_react = require("react");
36
+ var import_useArraishAsDSTree = require("../arraish-tree/useArraishAsDSTree.js");
37
+ const useTreeviewAsDSTree = (treeData) => {
38
+ const opts = (0, import_react.useMemo)(
39
+ () => ({
40
+ getChildrenKey: () => "children",
41
+ getUniqueId: (node) => `${node.id}`
42
+ }),
43
+ []
44
+ );
45
+ return (0, import_useArraishAsDSTree.useArraishAsDSTree)(treeData, opts);
46
+ };
47
+ //# sourceMappingURL=useTreeviewAsDSTree.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../src/adapters/treeview/useTreeviewAsDSTree.ts", "../../../../../../../scripts/build/transpile/react-shim.js"],
4
+ "sourcesContent": ["import { useMemo } from 'react';\nimport { useArraishAsDSTree } from '../arraish-tree/useArraishAsDSTree.js';\n\ntype TreeviewItem<T extends object = object> = T & {\n id: string | number;\n children?: TreeviewItem[];\n};\n\nexport const useTreeviewAsDSTree = <T extends TreeviewItem<object> = TreeviewItem<object>>(treeData: T[]) => {\n const opts = useMemo(\n () => ({\n getChildrenKey: () => 'children',\n getUniqueId: (node: T) => `${node.id}`,\n }),\n [],\n );\n return useArraishAsDSTree(treeData, opts);\n};\n", "import * as React from 'react';\nexport { React };\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADAvB,mBAAwB;AACxB,gCAAmC;AAO5B,MAAM,sBAAsB,CAAwD,aAAkB;AAC3G,QAAM,WAAO;AAAA,IACX,OAAO;AAAA,MACL,gBAAgB,MAAM;AAAA,MACtB,aAAa,CAAC,SAAY,GAAG,KAAK;AAAA,IACpC;AAAA,IACA,CAAC;AAAA,EACH;AACA,aAAO,8CAAmB,UAAU,IAAI;AAC1C;",
6
+ "names": []
7
+ }
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var src_exports = {};
30
+ __export(src_exports, {
31
+ DSTree: () => import_DSTree.DSTree,
32
+ fromDSTreeToTreeishArray: () => import_fromDSTreeToTreeishArray.fromDSTreeToTreeishArray,
33
+ fromTreeishArrayToDSTree: () => import_fromTreeishArrayToDSTree.fromTreeishArrayToDSTree,
34
+ useArraishAsDSTree: () => import_useArraishAsDSTree.useArraishAsDSTree,
35
+ useDSTree: () => import_useDSTree.useDSTree,
36
+ useTreeviewAsDSTree: () => import_useTreeviewAsDSTree.useTreeviewAsDSTree
37
+ });
38
+ module.exports = __toCommonJS(src_exports);
39
+ var React = __toESM(require("react"));
40
+ var import_DSTree = require("./DSTree.js");
41
+ var import_useDSTree = require("./useDSTree.js");
42
+ var import_fromTreeishArrayToDSTree = require("./adapters/arraish-tree/fromTreeishArrayToDSTree.js");
43
+ var import_fromDSTreeToTreeishArray = require("./adapters/arraish-tree/fromDSTreeToTreeishArray.js");
44
+ var import_useArraishAsDSTree = require("./adapters/arraish-tree/useArraishAsDSTree.js");
45
+ var import_useTreeviewAsDSTree = require("./adapters/treeview/useTreeviewAsDSTree.js");
46
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/index.ts", "../../../../../scripts/build/transpile/react-shim.js"],
4
+ "sourcesContent": ["export type { Item } from './types.js';\nexport type { Node } from './Node.js';\nexport { DSTree } from './DSTree.js';\nexport { useDSTree } from './useDSTree.js';\nexport { fromTreeishArrayToDSTree } from './adapters/arraish-tree/fromTreeishArrayToDSTree.js';\nexport { fromDSTreeToTreeishArray } from './adapters/arraish-tree/fromDSTreeToTreeishArray.js';\nexport { useArraishAsDSTree } from './adapters/arraish-tree/useArraishAsDSTree.js';\nexport { useTreeviewAsDSTree } from './adapters/treeview/useTreeviewAsDSTree.js';\n", "import * as React from 'react';\nexport { React };\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADEvB,oBAAuB;AACvB,uBAA0B;AAC1B,sCAAyC;AACzC,sCAAyC;AACzC,gCAAmC;AACnC,iCAAoC;",
6
+ "names": []
7
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "type": "commonjs",
3
+ "sideEffects": [
4
+ "*.css",
5
+ "*.scss"
6
+ ]
7
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (let key of __getOwnPropNames(from))
11
+ if (!__hasOwnProp.call(to, key) && key !== except)
12
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
+ }
14
+ return to;
15
+ };
16
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
+ // If the importer is in node compatibility mode or this is not an ESM
18
+ // file that has been converted to a CommonJS file using a Babel-
19
+ // compatible transform (i.e. "__esModule" has not been set), then set
20
+ // "default" to the CommonJS "module.exports" for node compatibility.
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
24
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
+ var types_exports = {};
26
+ module.exports = __toCommonJS(types_exports);
27
+ var React = __toESM(require("react"));
28
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/types.ts", "../../../../../scripts/build/transpile/react-shim.js"],
4
+ "sourcesContent": ["import type { DSTree } from './DSTree.js';\nimport type { Node } from './Node.js';\n\nexport interface Item {\n subitems?: Item[];\n [key: string]: unknown;\n}\n\nexport interface NodeConstructorOptions<T extends Item> {\n childIndex: number;\n depth: number;\n tree: DSTree<T>;\n parent?: Node<T> | null;\n}\n\nexport interface AddOptions<T extends Item> {\n position?: number;\n parent?: Node<T> | null;\n callback?: (node: Node<T>, tree: DSTree<T>) => void;\n}\n\nexport interface RemoveOptions<T extends Item> {\n callback?: (node: Node<T>, tree: DSTree<T>) => void;\n}\n\nexport interface MoveOptions<T extends Item> {\n position?: number;\n parent?: Node<T>;\n callback?: (node: Node<T>, tree: DSTree<T>) => void;\n}\n\nexport interface MutateOptions<T extends Item> {\n callback?: (node: Node<T>, tree: DSTree<T>) => void;\n}\nexport interface OverloadedItem<T extends object = object> extends Item {\n id: string;\n subitems: OverloadedItem<T>[];\n originalNodeData: T;\n}\n", "import * as React from 'react';\nexport { React };\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;ACAA,YAAuB;",
6
+ "names": []
7
+ }
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var useDSTree_exports = {};
30
+ __export(useDSTree_exports, {
31
+ useDSTree: () => useDSTree
32
+ });
33
+ module.exports = __toCommonJS(useDSTree_exports);
34
+ var React = __toESM(require("react"));
35
+ var import_react = require("react");
36
+ var import_DSTree = require("./DSTree.js");
37
+ const useDSTree = (rootItem, opts) => {
38
+ const tree = (0, import_react.useMemo)(() => new import_DSTree.DSTree(rootItem, opts), [opts, rootItem]);
39
+ const [hash, setHash] = (0, import_react.useState)(tree.hash);
40
+ const addNode = (0, import_react.useCallback)(
41
+ (item, options) => {
42
+ const cb = (node, treecb) => {
43
+ options?.callback?.(node, treecb);
44
+ setHash(treecb.hash);
45
+ };
46
+ return tree.addNode(item, { ...options, callback: cb });
47
+ },
48
+ [tree]
49
+ );
50
+ const removeNode = (0, import_react.useCallback)(
51
+ (id, options) => {
52
+ const cb = (node, treecb) => {
53
+ options?.callback?.(node, treecb);
54
+ setHash(treecb.hash);
55
+ };
56
+ return tree.removeNode(id, { ...options, callback: cb });
57
+ },
58
+ [tree]
59
+ );
60
+ const moveNode = (0, import_react.useCallback)(
61
+ (id, options) => {
62
+ const cb = (node, treecb) => {
63
+ options?.callback?.(node, treecb);
64
+ setHash(treecb.hash);
65
+ };
66
+ return tree.moveNode(id, { ...options, callback: cb });
67
+ },
68
+ [tree]
69
+ );
70
+ const replaceNode = (0, import_react.useCallback)(
71
+ (id, item, options) => {
72
+ const cb = (node, treecb) => {
73
+ options?.callback?.(node, treecb);
74
+ setHash(treecb.hash);
75
+ };
76
+ return tree.replaceNode(id, item, { ...options, callback: cb });
77
+ },
78
+ [tree]
79
+ );
80
+ return {
81
+ hash,
82
+ getRoot: tree.getRoot,
83
+ getNode: tree.getNode,
84
+ walk: tree.walk,
85
+ walkParents: tree.walkParents,
86
+ findNode: tree.findNode,
87
+ findAllNodes: tree.findAllNodes,
88
+ flatten: tree.flatten,
89
+ addNode,
90
+ removeNode,
91
+ moveNode,
92
+ replaceNode,
93
+ getPath: tree.getPath,
94
+ getPathIds: tree.getPathIds
95
+ };
96
+ };
97
+ //# sourceMappingURL=useDSTree.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/useDSTree.ts", "../../../../../scripts/build/transpile/react-shim.js"],
4
+ "sourcesContent": ["import { useCallback, useMemo, useState } from 'react';\nimport { DSTree } from './DSTree.js';\nimport type { AddOptions, Item, MoveOptions, MutateOptions, RemoveOptions } from './types.js';\n\nexport const useDSTree = <T extends Item>(rootItem: T, opts: { getUniqueId: (item: T) => string }) => {\n const tree = useMemo(() => new DSTree(rootItem, opts), [opts, rootItem]);\n const [hash, setHash] = useState(tree.hash);\n\n const addNode = useCallback(\n (item: T, options?: AddOptions<T>) => {\n const cb: AddOptions<T>['callback'] = (node, treecb) => {\n options?.callback?.(node, treecb);\n setHash(treecb.hash);\n };\n return tree.addNode(item, { ...options, callback: cb });\n },\n [tree],\n );\n\n const removeNode = useCallback(\n (id: string, options?: RemoveOptions<T>) => {\n const cb: RemoveOptions<T>['callback'] = (node, treecb) => {\n options?.callback?.(node, treecb);\n setHash(treecb.hash);\n };\n return tree.removeNode(id, { ...options, callback: cb });\n },\n [tree],\n );\n\n const moveNode = useCallback(\n (id: string, options?: MoveOptions<T>) => {\n const cb: MoveOptions<T>['callback'] = (node, treecb) => {\n options?.callback?.(node, treecb);\n setHash(treecb.hash);\n };\n return tree.moveNode(id, { ...options, callback: cb });\n },\n [tree],\n );\n\n const replaceNode = useCallback(\n (id: string, item: T, options?: MutateOptions<T>) => {\n const cb: MutateOptions<T>['callback'] = (node, treecb) => {\n options?.callback?.(node, treecb);\n setHash(treecb.hash);\n };\n return tree.replaceNode(id, item, { ...options, callback: cb });\n },\n [tree],\n );\n\n return {\n hash,\n getRoot: tree.getRoot,\n getNode: tree.getNode,\n walk: tree.walk,\n walkParents: tree.walkParents,\n findNode: tree.findNode,\n findAllNodes: tree.findAllNodes,\n flatten: tree.flatten,\n addNode,\n removeNode,\n moveNode,\n replaceNode,\n getPath: tree.getPath,\n getPathIds: tree.getPathIds,\n };\n};\n", "import * as React from 'react';\nexport { React };\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADAvB,mBAA+C;AAC/C,oBAAuB;AAGhB,MAAM,YAAY,CAAiB,UAAa,SAA+C;AACpG,QAAM,WAAO,sBAAQ,MAAM,IAAI,qBAAO,UAAU,IAAI,GAAG,CAAC,MAAM,QAAQ,CAAC;AACvE,QAAM,CAAC,MAAM,OAAO,QAAI,uBAAS,KAAK,IAAI;AAE1C,QAAM,cAAU;AAAA,IACd,CAAC,MAAS,YAA4B;AACpC,YAAM,KAAgC,CAAC,MAAM,WAAW;AACtD,iBAAS,WAAW,MAAM,MAAM;AAChC,gBAAQ,OAAO,IAAI;AAAA,MACrB;AACA,aAAO,KAAK,QAAQ,MAAM,EAAE,GAAG,SAAS,UAAU,GAAG,CAAC;AAAA,IACxD;AAAA,IACA,CAAC,IAAI;AAAA,EACP;AAEA,QAAM,iBAAa;AAAA,IACjB,CAAC,IAAY,YAA+B;AAC1C,YAAM,KAAmC,CAAC,MAAM,WAAW;AACzD,iBAAS,WAAW,MAAM,MAAM;AAChC,gBAAQ,OAAO,IAAI;AAAA,MACrB;AACA,aAAO,KAAK,WAAW,IAAI,EAAE,GAAG,SAAS,UAAU,GAAG,CAAC;AAAA,IACzD;AAAA,IACA,CAAC,IAAI;AAAA,EACP;AAEA,QAAM,eAAW;AAAA,IACf,CAAC,IAAY,YAA6B;AACxC,YAAM,KAAiC,CAAC,MAAM,WAAW;AACvD,iBAAS,WAAW,MAAM,MAAM;AAChC,gBAAQ,OAAO,IAAI;AAAA,MACrB;AACA,aAAO,KAAK,SAAS,IAAI,EAAE,GAAG,SAAS,UAAU,GAAG,CAAC;AAAA,IACvD;AAAA,IACA,CAAC,IAAI;AAAA,EACP;AAEA,QAAM,kBAAc;AAAA,IAClB,CAAC,IAAY,MAAS,YAA+B;AACnD,YAAM,KAAmC,CAAC,MAAM,WAAW;AACzD,iBAAS,WAAW,MAAM,MAAM;AAChC,gBAAQ,OAAO,IAAI;AAAA,MACrB;AACA,aAAO,KAAK,YAAY,IAAI,MAAM,EAAE,GAAG,SAAS,UAAU,GAAG,CAAC;AAAA,IAChE;AAAA,IACA,CAAC,IAAI;AAAA,EACP;AAEA,SAAO;AAAA,IACL;AAAA,IACA,SAAS,KAAK;AAAA,IACd,SAAS,KAAK;AAAA,IACd,MAAM,KAAK;AAAA,IACX,aAAa,KAAK;AAAA,IAClB,UAAU,KAAK;AAAA,IACf,cAAc,KAAK;AAAA,IACnB,SAAS,KAAK;AAAA,IACd;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,SAAS,KAAK;AAAA,IACd,YAAY,KAAK;AAAA,EACnB;AACF;",
6
+ "names": []
7
+ }
@@ -0,0 +1,81 @@
1
+ import * as React from "react";
2
+ import { Node } from "./Node.js";
3
+ class DSTree {
4
+ constructor(item, options) {
5
+ this.getRoot = () => this.root;
6
+ this.getNode = (id) => this.nodes[id];
7
+ this.getNodes = () => this.nodes;
8
+ this.walk = (callback) => {
9
+ this.root.walk(callback);
10
+ };
11
+ this.walkParents = (callback) => {
12
+ this.root.walkParents(callback);
13
+ };
14
+ this.findNode = (callback) => this.root.findNode(callback);
15
+ this.findAllNodes = (callback) => this.root.findAllNodes(callback);
16
+ this.flatten = () => this.root.flatten();
17
+ this.addNode = (item, options = {}) => {
18
+ const { position, parent, callback } = options;
19
+ const parentDefault = parent || this.root;
20
+ const positionDefault = position ?? parentDefault.children.length;
21
+ const node = new Node(item, {
22
+ childIndex: positionDefault,
23
+ depth: parentDefault.depth + 1,
24
+ parent: parentDefault,
25
+ tree: this
26
+ });
27
+ this.getNode(parentDefault.dsId).addNode(node);
28
+ this._hash += 1;
29
+ callback?.(node, this);
30
+ return node;
31
+ };
32
+ this.removeNode = (id, options = {}) => {
33
+ const node = this.getNode(id);
34
+ if (!node)
35
+ throw new Error(`Node with id ${id} not found`);
36
+ node.removeNode();
37
+ this._hash += 1;
38
+ options.callback?.(node, this);
39
+ return node;
40
+ };
41
+ this.moveNode = (id, options = {}) => {
42
+ const removedNode = this.removeNode(id);
43
+ const node = this.addNode(removedNode.getJson(), options);
44
+ this._hash += 1;
45
+ return node;
46
+ };
47
+ this.replaceNode = (id, item, options = {}) => {
48
+ const removedNode = this.removeNode(id);
49
+ const node = this.addNode(item, { parent: removedNode.parent, position: removedNode.childIndex });
50
+ this._hash += 1;
51
+ options.callback?.(node, this);
52
+ return node;
53
+ };
54
+ this.getPath = (id) => this.getRoot().getPath(id);
55
+ this.getPathIds = (id) => this.getRoot().getPathIds(id);
56
+ this.getUniqueId = options.getUniqueId;
57
+ this.root = new Node(item, { childIndex: 0, depth: 0, tree: this });
58
+ this.nodes = {};
59
+ this._hash = 0;
60
+ this.root.walk((node) => {
61
+ this.nodes[node.dsId] = node;
62
+ return true;
63
+ });
64
+ }
65
+ addNodeToNodesDictionary(node) {
66
+ if (this.nodes[node.dsId]) {
67
+ throw new Error(`DSTree: repeated dsId ${node.dsId}`);
68
+ }
69
+ this.nodes[node.dsId] = node;
70
+ }
71
+ removeNodeFromNodesDictionary(node) {
72
+ delete this.nodes[node.dsId];
73
+ }
74
+ get hash() {
75
+ return this._hash;
76
+ }
77
+ }
78
+ export {
79
+ DSTree
80
+ };
81
+ //# sourceMappingURL=DSTree.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../../scripts/build/transpile/react-shim.js", "../../src/DSTree.ts"],
4
+ "sourcesContent": ["import * as React from 'react';\nexport { React };\n", "import type { AddOptions, Item, MoveOptions, MutateOptions, RemoveOptions } from './types.js';\nimport { Node } from './Node.js';\n\nexport class DSTree<T extends Item> {\n private root: Node<T>;\n\n private _hash: number;\n\n private nodes: Record<string, Node<T>>;\n\n getUniqueId: (item: T) => string;\n\n constructor(item: T, options: { getUniqueId: (item: T) => string }) {\n this.getUniqueId = options.getUniqueId;\n this.root = new Node(item, { childIndex: 0, depth: 0, tree: this });\n this.nodes = {};\n this._hash = 0;\n\n // autocalculate a dictionary where dsId are keys and nodes are values\n // and add the tree link to each node\n this.root.walk((node) => {\n this.nodes[node.dsId] = node;\n return true;\n });\n }\n\n addNodeToNodesDictionary(node: Node<T>): void {\n // check that we have no repeated ids\n if (this.nodes[node.dsId]) {\n throw new Error(`DSTree: repeated dsId ${node.dsId}`);\n }\n\n this.nodes[node.dsId] = node;\n }\n\n removeNodeFromNodesDictionary(node: Node<T>): void {\n delete this.nodes[node.dsId];\n }\n\n get hash(): number {\n return this._hash;\n }\n\n getRoot = (): Node<T> => this.root;\n\n getNode = (id: string): Node<T> => this.nodes[id];\n\n getNodes = (): Record<string, Node<T>> => this.nodes;\n\n walk = (callback: (node: Node<T>) => boolean): void => {\n this.root.walk(callback);\n };\n\n walkParents = (callback: (node: Node<T>) => boolean): void => {\n this.root.walkParents(callback);\n };\n\n findNode = (callback: (node: Node<T>) => boolean): Node<T> | null => this.root.findNode(callback);\n\n findAllNodes = (callback: (node: Node<T>) => boolean): Node<T>[] => this.root.findAllNodes(callback);\n\n flatten = (): Node<T>[] => this.root.flatten();\n\n addNode = (item: T, options: AddOptions<T> = {}): Node<T> => {\n const { position, parent, callback } = options;\n const parentDefault = parent || this.root;\n const positionDefault = position ?? parentDefault.children.length;\n\n // create node and add it to the parent\n const node = new Node(item, {\n childIndex: positionDefault,\n depth: parentDefault.depth + 1,\n parent: parentDefault,\n tree: this,\n });\n this.getNode(parentDefault.dsId).addNode(node);\n\n // change hash\n this._hash += 1;\n\n // invoke the callback if present\n callback?.(node, this);\n\n return node;\n };\n\n removeNode = (id: string, options: RemoveOptions<T> = {}): Node<T> => {\n const node = this.getNode(id);\n if (!node) throw new Error(`Node with id ${id} not found`);\n\n node.removeNode();\n\n // change hash\n this._hash += 1;\n\n // invoke the callback if present\n options.callback?.(node, this);\n\n return node;\n };\n\n moveNode = (id: string, options: MoveOptions<T> = {}): Node<T> => {\n const removedNode = this.removeNode(id);\n const node = this.addNode(removedNode.getJson(), options);\n\n this._hash += 1;\n\n return node;\n };\n\n replaceNode = (id: string, item: T, options: MutateOptions<T> = {}): Node<T> => {\n const removedNode = this.removeNode(id);\n const node = this.addNode(item, { parent: removedNode.parent, position: removedNode.childIndex });\n\n this._hash += 1;\n\n // invoke the callback if present\n options.callback?.(node, this);\n\n return node;\n };\n\n getPath = (id: string): Node<T>[] => this.getRoot().getPath(id);\n\n getPathIds = (id: string): string[] => this.getRoot().getPathIds(id);\n}\n"],
5
+ "mappings": "AAAA,YAAY,WAAW;ACCvB,SAAS,YAAY;AAEd,MAAM,OAAuB;AAAA,EASlC,YAAY,MAAS,SAA+C;AA+BpE,mBAAU,MAAe,KAAK;AAE9B,mBAAU,CAAC,OAAwB,KAAK,MAAM,EAAE;AAEhD,oBAAW,MAA+B,KAAK;AAE/C,gBAAO,CAAC,aAA+C;AACrD,WAAK,KAAK,KAAK,QAAQ;AAAA,IACzB;AAEA,uBAAc,CAAC,aAA+C;AAC5D,WAAK,KAAK,YAAY,QAAQ;AAAA,IAChC;AAEA,oBAAW,CAAC,aAAyD,KAAK,KAAK,SAAS,QAAQ;AAEhG,wBAAe,CAAC,aAAoD,KAAK,KAAK,aAAa,QAAQ;AAEnG,mBAAU,MAAiB,KAAK,KAAK,QAAQ;AAE7C,mBAAU,CAAC,MAAS,UAAyB,CAAC,MAAe;AAC3D,YAAM,EAAE,UAAU,QAAQ,SAAS,IAAI;AACvC,YAAM,gBAAgB,UAAU,KAAK;AACrC,YAAM,kBAAkB,YAAY,cAAc,SAAS;AAG3D,YAAM,OAAO,IAAI,KAAK,MAAM;AAAA,QAC1B,YAAY;AAAA,QACZ,OAAO,cAAc,QAAQ;AAAA,QAC7B,QAAQ;AAAA,QACR,MAAM;AAAA,MACR,CAAC;AACD,WAAK,QAAQ,cAAc,IAAI,EAAE,QAAQ,IAAI;AAG7C,WAAK,SAAS;AAGd,iBAAW,MAAM,IAAI;AAErB,aAAO;AAAA,IACT;AAEA,sBAAa,CAAC,IAAY,UAA4B,CAAC,MAAe;AACpE,YAAM,OAAO,KAAK,QAAQ,EAAE;AAC5B,UAAI,CAAC;AAAM,cAAM,IAAI,MAAM,gBAAgB,cAAc;AAEzD,WAAK,WAAW;AAGhB,WAAK,SAAS;AAGd,cAAQ,WAAW,MAAM,IAAI;AAE7B,aAAO;AAAA,IACT;AAEA,oBAAW,CAAC,IAAY,UAA0B,CAAC,MAAe;AAChE,YAAM,cAAc,KAAK,WAAW,EAAE;AACtC,YAAM,OAAO,KAAK,QAAQ,YAAY,QAAQ,GAAG,OAAO;AAExD,WAAK,SAAS;AAEd,aAAO;AAAA,IACT;AAEA,uBAAc,CAAC,IAAY,MAAS,UAA4B,CAAC,MAAe;AAC9E,YAAM,cAAc,KAAK,WAAW,EAAE;AACtC,YAAM,OAAO,KAAK,QAAQ,MAAM,EAAE,QAAQ,YAAY,QAAQ,UAAU,YAAY,WAAW,CAAC;AAEhG,WAAK,SAAS;AAGd,cAAQ,WAAW,MAAM,IAAI;AAE7B,aAAO;AAAA,IACT;AAEA,mBAAU,CAAC,OAA0B,KAAK,QAAQ,EAAE,QAAQ,EAAE;AAE9D,sBAAa,CAAC,OAAyB,KAAK,QAAQ,EAAE,WAAW,EAAE;AA/GjE,SAAK,cAAc,QAAQ;AAC3B,SAAK,OAAO,IAAI,KAAK,MAAM,EAAE,YAAY,GAAG,OAAO,GAAG,MAAM,KAAK,CAAC;AAClE,SAAK,QAAQ,CAAC;AACd,SAAK,QAAQ;AAIb,SAAK,KAAK,KAAK,CAAC,SAAS;AACvB,WAAK,MAAM,KAAK,IAAI,IAAI;AACxB,aAAO;AAAA,IACT,CAAC;AAAA,EACH;AAAA,EAEA,yBAAyB,MAAqB;AAE5C,QAAI,KAAK,MAAM,KAAK,IAAI,GAAG;AACzB,YAAM,IAAI,MAAM,yBAAyB,KAAK,MAAM;AAAA,IACtD;AAEA,SAAK,MAAM,KAAK,IAAI,IAAI;AAAA,EAC1B;AAAA,EAEA,8BAA8B,MAAqB;AACjD,WAAO,KAAK,MAAM,KAAK,IAAI;AAAA,EAC7B;AAAA,EAEA,IAAI,OAAe;AACjB,WAAO,KAAK;AAAA,EACd;AAoFF;",
6
+ "names": []
7
+ }
@@ -0,0 +1,124 @@
1
+ import * as React from "react";
2
+ import { cloneDeep, omit } from "lodash";
3
+ class Node {
4
+ constructor(item, { childIndex, depth, parent, tree }) {
5
+ // ===========================================================================
6
+ // INTERNAL METHODS
7
+ // ===========================================================================
8
+ this.fixChildIndexes = () => {
9
+ for (let i = 0; i < this.children.length; i++) {
10
+ this.children[i].childIndex = i;
11
+ }
12
+ this._hash += 1;
13
+ };
14
+ this.getCleanItem = (item) => {
15
+ const plainItem = omit(item, ["subitems"]);
16
+ return cloneDeep(plainItem);
17
+ };
18
+ this.walk = (callback) => {
19
+ const shouldContinueWalking = callback(this);
20
+ if (shouldContinueWalking) {
21
+ for (const child of this.children) {
22
+ child.walk(callback);
23
+ }
24
+ }
25
+ };
26
+ this.walkParents = (callback) => {
27
+ const shouldContinueWalking = callback(this);
28
+ if (shouldContinueWalking && this.parent) {
29
+ this.parent.walkParents(callback);
30
+ }
31
+ };
32
+ this.findNode = (callback) => {
33
+ let found = null;
34
+ this.walk((node) => {
35
+ if (found)
36
+ return false;
37
+ if (callback(node))
38
+ found = node;
39
+ return !found;
40
+ });
41
+ return found;
42
+ };
43
+ this.findAllNodes = (callback) => {
44
+ const found = [];
45
+ this.walk((node) => {
46
+ if (callback(node))
47
+ found.push(node);
48
+ return true;
49
+ });
50
+ return found;
51
+ };
52
+ this.flatten = () => {
53
+ const flattened = [];
54
+ this.walk((node) => {
55
+ flattened.push(node);
56
+ return true;
57
+ });
58
+ return flattened;
59
+ };
60
+ this.getPath = (id) => {
61
+ const path = [];
62
+ const node = this.tree.getNode(id);
63
+ if (!node)
64
+ return path;
65
+ node.walkParents((parent) => {
66
+ path.push(parent);
67
+ return parent.dsId !== this.dsId;
68
+ });
69
+ return path.reverse();
70
+ };
71
+ this.getPathIds = (id) => this.getPath(id).map((node) => node.dsId);
72
+ this.getJson = () => ({
73
+ ...this.plainItem,
74
+ subitems: this.children.map((child) => child.getJson())
75
+ });
76
+ // ===========================================================================
77
+ // WRITE METHODS
78
+ // ===========================================================================
79
+ this.addNode = (node) => {
80
+ const position = node.childIndex;
81
+ if (position < 0 || position > this.children.length) {
82
+ throw new Error(`Invalid position ${position} for parent ${this.dsId} with item ${node.dsId}`);
83
+ }
84
+ this.children.splice(position, 0, node);
85
+ this.fixChildIndexes();
86
+ node.walk((child) => {
87
+ this.tree.addNodeToNodesDictionary(child);
88
+ return true;
89
+ });
90
+ this._hash += 1;
91
+ };
92
+ this.removeNode = () => {
93
+ if (!this.parent)
94
+ throw new Error(`Cannot remove root node with id ${this.dsId}`);
95
+ this.parent.children.splice(this.childIndex, 1);
96
+ this.parent.fixChildIndexes();
97
+ this.walk((child) => {
98
+ this.tree.removeNodeFromNodesDictionary(child);
99
+ return true;
100
+ });
101
+ this._hash += 1;
102
+ };
103
+ this.dsId = tree.getUniqueId(item);
104
+ this.childIndex = childIndex;
105
+ this.depth = depth;
106
+ this.parent = parent ?? null;
107
+ this.tree = tree;
108
+ this._hash = 0;
109
+ this.children = item.subitems?.map(
110
+ (subitem, index) => new Node(subitem, { childIndex: index, depth: depth + 1, parent: this, tree })
111
+ ) ?? [];
112
+ this.plainItem = this.getCleanItem(item);
113
+ }
114
+ // ===========================================================================
115
+ // READ METHODS
116
+ // ===========================================================================
117
+ get hash() {
118
+ return this._hash;
119
+ }
120
+ }
121
+ export {
122
+ Node
123
+ };
124
+ //# sourceMappingURL=Node.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../../scripts/build/transpile/react-shim.js", "../../src/Node.ts"],
4
+ "sourcesContent": ["import * as React from 'react';\nexport { React };\n", "/* eslint-disable no-use-before-define */\n/* eslint-disable max-params */\nimport { cloneDeep, omit } from 'lodash';\nimport type { DSTree } from './DSTree.js';\nimport type { Item, NodeConstructorOptions } from './types.js';\n\nexport class Node<T extends Item> {\n dsId: string;\n\n plainItem: T;\n\n childIndex: number;\n\n depth: number;\n\n parent: Node<T> | null;\n\n children: Node<T>[];\n\n private tree!: DSTree<T>;\n\n private _hash: number;\n\n constructor(item: T, { childIndex, depth, parent, tree }: NodeConstructorOptions<T>) {\n this.dsId = tree.getUniqueId(item);\n this.childIndex = childIndex;\n this.depth = depth;\n this.parent = parent ?? null;\n this.tree = tree;\n this._hash = 0;\n\n this.children =\n item.subitems?.map(\n (subitem, index) => new Node(subitem as T, { childIndex: index, depth: depth + 1, parent: this, tree }),\n ) ?? [];\n\n // Save the item without the subitems\n this.plainItem = this.getCleanItem(item);\n }\n\n // ===========================================================================\n // INTERNAL METHODS\n // ===========================================================================\n\n fixChildIndexes = (): void => {\n for (let i = 0; i < this.children.length; i++) {\n this.children[i].childIndex = i;\n }\n this._hash += 1;\n };\n\n getCleanItem = (item: T): T => {\n const plainItem = omit(item, ['subitems']);\n return cloneDeep(plainItem as T);\n };\n\n // ===========================================================================\n // READ METHODS\n // ===========================================================================\n\n get hash(): number {\n return this._hash;\n }\n\n walk = (callback: (node: Node<T>) => boolean): void => {\n const shouldContinueWalking = callback(this);\n if (shouldContinueWalking) {\n for (const child of this.children) {\n child.walk(callback);\n }\n }\n };\n\n walkParents = (callback: (node: Node<T>) => boolean): void => {\n const shouldContinueWalking = callback(this);\n if (shouldContinueWalking && this.parent) {\n this.parent.walkParents(callback);\n }\n };\n\n findNode = (callback: (node: Node<T>) => boolean): Node<T> | null => {\n // find first node using walk\n let found: Node<T> | null = null;\n this.walk((node) => {\n // If we already found it, don't look anymore\n if (found) return false;\n\n // Otherwise keep looking\n if (callback(node)) found = node;\n return !found;\n });\n\n return found;\n };\n\n findAllNodes = (callback: (node: Node<T>) => boolean): Node<T>[] => {\n const found: Node<T>[] = [];\n this.walk((node) => {\n if (callback(node)) found.push(node);\n return true;\n });\n return found;\n };\n\n flatten = (): Node<T>[] => {\n const flattened: Node<T>[] = [];\n this.walk((node) => {\n flattened.push(node);\n return true;\n });\n return flattened;\n };\n\n getPath = (id: string): Node<T>[] => {\n const path: Node<T>[] = [];\n const node = this.tree.getNode(id);\n if (!node) return path;\n node.walkParents((parent) => {\n path.push(parent);\n return parent.dsId !== this.dsId;\n });\n return path.reverse();\n };\n\n getPathIds = (id: string): string[] => this.getPath(id).map((node) => node.dsId);\n\n getJson = (): T => ({\n ...this.plainItem,\n subitems: this.children.map((child) => child.getJson()),\n });\n\n // ===========================================================================\n // WRITE METHODS\n // ===========================================================================\n\n addNode = (node: Node<T>) => {\n // check that the position is valid for that parent\n const position = node.childIndex;\n if (position < 0 || position > this.children.length) {\n throw new Error(`Invalid position ${position} for parent ${this.dsId} with item ${node.dsId}`);\n }\n\n // add the new node\n this.children.splice(position, 0, node);\n\n // fix the childIndex of the nodes after the new one\n this.fixChildIndexes();\n\n // add subtree of the new node to the node dictionary\n node.walk((child) => {\n this.tree.addNodeToNodesDictionary(child);\n return true;\n });\n\n this._hash += 1;\n };\n\n removeNode = () => {\n // if no parent throw error\n if (!this.parent) throw new Error(`Cannot remove root node with id ${this.dsId}`);\n\n // remove from parent\n this.parent.children.splice(this.childIndex, 1);\n\n // fix the childIndex of the nodes of the parent\n this.parent.fixChildIndexes();\n\n // remove subtree from the node dictionary\n this.walk((child) => {\n this.tree.removeNodeFromNodesDictionary(child);\n return true;\n });\n\n this._hash += 1;\n };\n}\n"],
5
+ "mappings": "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",
6
+ "names": []
7
+ }
@@ -0,0 +1,53 @@
1
+ import * as React from "react";
2
+ const fromDSTreeToTreeishArray = (args) => {
3
+ const { tree, opts } = args;
4
+ const { getChildrenKey, getUniqueId } = opts;
5
+ const root = tree.getRoot();
6
+ const restoredTreeish = [];
7
+ const callback = (node) => {
8
+ if (node.depth === 0)
9
+ return true;
10
+ const nodeJson = node.getJson();
11
+ const originalNode = structuredClone(nodeJson.originalNodeData);
12
+ const nodeChildrenKey = getChildrenKey(originalNode);
13
+ if (!Object.hasOwnProperty.call(originalNode, nodeChildrenKey)) {
14
+ Object.defineProperty(originalNode, nodeChildrenKey, {
15
+ value: [],
16
+ writable: true,
17
+ enumerable: true,
18
+ configurable: true
19
+ });
20
+ }
21
+ if (node.depth === 1) {
22
+ restoredTreeish.push(originalNode);
23
+ return true;
24
+ }
25
+ const parents = root.getPathIds(node.dsId);
26
+ parents.shift();
27
+ parents.pop();
28
+ let parent = restoredTreeish.find((item) => `${getUniqueId(item)}` === parents[0]);
29
+ if (!parent)
30
+ throw new Error(
31
+ `Unable to find parent node with id ${parents[0]}, have you provided the correct getUniqueId function?`
32
+ );
33
+ let parentChildrenKey = getChildrenKey(parent);
34
+ for (let i = 1; i < parents.length; i++) {
35
+ const childrens = parent[parentChildrenKey];
36
+ if (!childrens)
37
+ throw new Error(
38
+ `Unable to children node with id ${parents[i]}, have you provided the correct getChildrenKey function?`
39
+ );
40
+ parent = childrens.find((item) => `${getUniqueId(item)}` === parents[i]);
41
+ parentChildrenKey = getChildrenKey(parent);
42
+ }
43
+ const parentChildrenArray = parent[parentChildrenKey];
44
+ parentChildrenArray.push(originalNode);
45
+ return true;
46
+ };
47
+ root.walk(callback);
48
+ return restoredTreeish;
49
+ };
50
+ export {
51
+ fromDSTreeToTreeishArray
52
+ };
53
+ //# sourceMappingURL=fromDSTreeToTreeishArray.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../../../../scripts/build/transpile/react-shim.js", "../../../../src/adapters/arraish-tree/fromDSTreeToTreeishArray.ts"],
4
+ "sourcesContent": ["import * as React from 'react';\nexport { React };\n", "/* eslint-disable max-statements */\nimport type { DSTree } from '../../DSTree.js';\nimport type { Node } from '../../Node.js';\nimport type { OverloadedItem } from '../../types.js';\n\n/*\n * a function that given a DSTree datastructure and some options returns a treeish array that compltely preserves the original data.\n * this is meant to revert the operation done by fromTreeishArrayToDSTree in a seamless way,\n * as such makes, some assumptions:\n * - the original data lives under the \"originalNodeData\" property of the node\n * - the root node is a pseudoRoot node that will not be part of the final treeish array\n * NOTE: This create a deep clone of the node original data, so it's not suitable for large trees and will not preserve any reference in the nodes original data\n * NOTE2: Nodes that have no children will be restored with an empty \"children\" array, this may be a data-loss, but this can't be easily avoided,\n * if this is important for your use-case you should implement your own conversion function with your requirements\n * @param {DSTree} tree - a DSTree datastructure (generated with a structure exactly the same as the one resulting from invoking fromTreeishArrayToDSTree)\n * @param {Object} opts - an object that governs how the treeish array will be converted to a DSTree datastructure\n * @param {Function} opts.getChildrenKey - a function that given a node returns the key used to access the node children\n * @param {Function} opts.getUniqueId - a function that given a node returns a unique string used to identify the node\n * @returns {Array} - a treeish array\n */\n\nexport const fromDSTreeToTreeishArray = <T extends object = object>(args: {\n tree: DSTree<OverloadedItem<T>>;\n opts: {\n getChildrenKey: (node: T) => keyof T;\n getUniqueId: (node: T) => string;\n };\n}): T[] => {\n const { tree, opts } = args;\n const { getChildrenKey, getUniqueId } = opts;\n const root = tree.getRoot();\n const restoredTreeish: T[] = [];\n const callback = (node: Node<OverloadedItem<T>>) => {\n // everytime we step in a node we want to:\n // - understand if the node has a parent\n // - if it does, we want to find the parent in the restoredTreeish array and add the node to the parent's children\n // the \"children\" are stored under the key returned by the getChildrenKey function\n // - if it doesn't, we make the assumption that this is a \"first-level\" node and we add it to the restoredTreeish array\n // - what ends up in the restoredTreeish array are only value from the \"originalNodeData\" property of the node + childrens under the key returned by the getChildrenKey function\n // we skip the root node\n if (node.depth === 0) return true;\n // we create the original node from the originalNodeData property of the node\n const nodeJson = node.getJson();\n const originalNode = structuredClone(nodeJson.originalNodeData);\n const nodeChildrenKey = getChildrenKey(originalNode);\n // we add a \"children\" array, creating it if doesn't exist\n if (!Object.hasOwnProperty.call(originalNode, nodeChildrenKey)) {\n Object.defineProperty(originalNode, nodeChildrenKey, {\n value: [] as T[],\n writable: true,\n enumerable: true,\n configurable: true,\n });\n }\n // if depth is 1, we are dealing with a first-level node, so we add it to the restoredTreeish array as is\n if (node.depth === 1) {\n restoredTreeish.push(originalNode);\n return true;\n }\n // if depth is > 1, we are dealing with a node that has a parent, so we need to find the parent in the restoredTreeish array\n // and add the node to the parent's children\n const parents = root.getPathIds(node.dsId);\n parents.shift(); // we added the pseudo-root, we skip it based on the assumption\n parents.pop(); // getPathIds also include the current searched for at the end of the array, we don't need that\n\n // we now find the parent in the restoredTreeish array\n // if this find fails this is an unrecoverable error, something is wrong in the opts callbacks or at app-level\n // the only other alternative is that the \"walk\" method from DSTree changed the order of the nodes, which would be a breaking change\n let parent = restoredTreeish.find((item: T) => `${getUniqueId(item)}` === parents[0]) as T;\n if (!parent)\n throw new Error(\n `Unable to find parent node with id ${parents[0]}, have you provided the correct getUniqueId function?`,\n );\n let parentChildrenKey = getChildrenKey(parent);\n // we now walk the parents array, skipping the first element (we already found the parent),\n // we must use the for loop to be sure that we are invoking code sequentially\n for (let i = 1; i < parents.length; i++) {\n // if this loop fails this is an unrecoverable error, something is wrong in the opts callbacks or at app-level\n // the only other alternative is that the \"walk\" method from DSTree changed the order of the nodes, which would be a breaking change\n const childrens = parent[parentChildrenKey] as T[];\n if (!childrens)\n throw new Error(\n `Unable to children node with id ${parents[i]}, have you provided the correct getChildrenKey function?`,\n );\n parent = childrens.find((item: T) => `${getUniqueId(item)}` === parents[i]) as T;\n parentChildrenKey = getChildrenKey(parent);\n }\n\n const parentChildrenArray = parent[parentChildrenKey] as T[];\n parentChildrenArray.push(originalNode);\n\n return true; // we will walk all the nodes no matter what\n };\n root.walk(callback);\n\n return restoredTreeish;\n};\n"],
5
+ "mappings": "AAAA,YAAY,WAAW;ACqBhB,MAAM,2BAA2B,CAA4B,SAMzD;AACT,QAAM,EAAE,MAAM,KAAK,IAAI;AACvB,QAAM,EAAE,gBAAgB,YAAY,IAAI;AACxC,QAAM,OAAO,KAAK,QAAQ;AAC1B,QAAM,kBAAuB,CAAC;AAC9B,QAAM,WAAW,CAAC,SAAkC;AAQlD,QAAI,KAAK,UAAU;AAAG,aAAO;AAE7B,UAAM,WAAW,KAAK,QAAQ;AAC9B,UAAM,eAAe,gBAAgB,SAAS,gBAAgB;AAC9D,UAAM,kBAAkB,eAAe,YAAY;AAEnD,QAAI,CAAC,OAAO,eAAe,KAAK,cAAc,eAAe,GAAG;AAC9D,aAAO,eAAe,cAAc,iBAAiB;AAAA,QACnD,OAAO,CAAC;AAAA,QACR,UAAU;AAAA,QACV,YAAY;AAAA,QACZ,cAAc;AAAA,MAChB,CAAC;AAAA,IACH;AAEA,QAAI,KAAK,UAAU,GAAG;AACpB,sBAAgB,KAAK,YAAY;AACjC,aAAO;AAAA,IACT;AAGA,UAAM,UAAU,KAAK,WAAW,KAAK,IAAI;AACzC,YAAQ,MAAM;AACd,YAAQ,IAAI;AAKZ,QAAI,SAAS,gBAAgB,KAAK,CAAC,SAAY,GAAG,YAAY,IAAI,QAAQ,QAAQ,CAAC,CAAC;AACpF,QAAI,CAAC;AACH,YAAM,IAAI;AAAA,QACR,sCAAsC,QAAQ,CAAC;AAAA,MACjD;AACF,QAAI,oBAAoB,eAAe,MAAM;AAG7C,aAAS,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;AAGvC,YAAM,YAAY,OAAO,iBAAiB;AAC1C,UAAI,CAAC;AACH,cAAM,IAAI;AAAA,UACR,mCAAmC,QAAQ,CAAC;AAAA,QAC9C;AACF,eAAS,UAAU,KAAK,CAAC,SAAY,GAAG,YAAY,IAAI,QAAQ,QAAQ,CAAC,CAAC;AAC1E,0BAAoB,eAAe,MAAM;AAAA,IAC3C;AAEA,UAAM,sBAAsB,OAAO,iBAAiB;AACpD,wBAAoB,KAAK,YAAY;AAErC,WAAO;AAAA,EACT;AACA,OAAK,KAAK,QAAQ;AAElB,SAAO;AACT;",
6
+ "names": []
7
+ }
@@ -0,0 +1,52 @@
1
+ import * as React from "react";
2
+ import { DSTree } from "../../DSTree.js";
3
+ const unsecureUuid = () => "xxxxxxxx-xxxx-xxxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
4
+ const r = Math.random() * 16 | 0;
5
+ const v = c == "x" ? r : r & 3 | 8;
6
+ return v.toString(16);
7
+ });
8
+ const walkTreeish = ({
9
+ callback,
10
+ node,
11
+ opts
12
+ }) => {
13
+ callback(node);
14
+ const children = opts?.getChildren(node);
15
+ if (Array.isArray(children) && children.length > 0) {
16
+ for (let i = 0; i < children.length; i += 1) {
17
+ walkTreeish({ callback, node: children[i], opts });
18
+ }
19
+ }
20
+ };
21
+ const fromTreeishArrayToDSTree = (args) => {
22
+ const rootId = `_autogenerated-ds-pseudoRoot-${unsecureUuid()}`;
23
+ const { opts, treeish } = args;
24
+ const { getUniqueId, getChildrenKey } = opts;
25
+ const pseudoRoot = {
26
+ id: rootId,
27
+ subitems: structuredClone(treeish),
28
+ originalNodeData: {}
29
+ };
30
+ const overloadedGetChildren = (node) => node.subitems;
31
+ const overloadedGetUniqueId = (node) => node.id;
32
+ const callback = (node) => {
33
+ if (node.id === rootId)
34
+ return;
35
+ const originalNodeData = structuredClone(node);
36
+ Object.keys(node).forEach((key) => {
37
+ delete node[key];
38
+ });
39
+ const overloadedNode = node;
40
+ const childrenKey = getChildrenKey(originalNodeData);
41
+ overloadedNode.id = getUniqueId(originalNodeData);
42
+ overloadedNode.subitems = originalNodeData[childrenKey];
43
+ delete originalNodeData[childrenKey];
44
+ overloadedNode.originalNodeData = originalNodeData;
45
+ };
46
+ walkTreeish({ callback, node: pseudoRoot, opts: { getChildren: overloadedGetChildren } });
47
+ return new DSTree(pseudoRoot, { getUniqueId: overloadedGetUniqueId });
48
+ };
49
+ export {
50
+ fromTreeishArrayToDSTree
51
+ };
52
+ //# sourceMappingURL=fromTreeishArrayToDSTree.js.map