@elliemae/ds-tree-model 3.16.0-next.2 → 3.16.0-next.20
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/dist/cjs/DSTree.js +5 -1
- package/dist/cjs/DSTree.js.map +2 -2
- package/dist/cjs/Node.js +13 -0
- package/dist/cjs/Node.js.map +2 -2
- package/dist/cjs/index.js +6 -2
- package/dist/cjs/index.js.map +2 -2
- package/dist/cjs/package.json +7 -0
- package/dist/cjs/types.js +4 -0
- package/dist/cjs/types.js.map +2 -2
- package/dist/cjs/useDSTree.js +5 -1
- package/dist/cjs/useDSTree.js.map +2 -2
- package/dist/esm/DSTree.js +1 -1
- package/dist/esm/DSTree.js.map +2 -2
- package/dist/esm/Node.js +9 -0
- package/dist/esm/Node.js.map +2 -2
- package/dist/esm/index.js +2 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/package.json +7 -0
- package/dist/esm/useDSTree.js +1 -1
- package/dist/esm/useDSTree.js.map +1 -1
- package/dist/types/DSTree.d.ts +2 -2
- package/dist/types/Node.d.ts +1 -1
- package/dist/types/index.d.ts +3 -3
- package/dist/types/types.d.ts +2 -2
- package/dist/types/useDSTree.d.ts +13 -13
- package/package.json +2 -2
package/dist/cjs/DSTree.js
CHANGED
|
@@ -18,6 +18,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
return to;
|
|
19
19
|
};
|
|
20
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.
|
|
21
25
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
26
|
mod
|
|
23
27
|
));
|
|
@@ -28,7 +32,7 @@ __export(DSTree_exports, {
|
|
|
28
32
|
});
|
|
29
33
|
module.exports = __toCommonJS(DSTree_exports);
|
|
30
34
|
var React = __toESM(require("react"));
|
|
31
|
-
var import_Node = require("./Node");
|
|
35
|
+
var import_Node = require("./Node.js");
|
|
32
36
|
class DSTree {
|
|
33
37
|
constructor(item, options) {
|
|
34
38
|
this.getRoot = () => this.root;
|
package/dist/cjs/DSTree.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/DSTree.ts", "../../../../scripts/build/transpile/react-shim.js"],
|
|
4
|
-
"sourcesContent": ["import type { AddOptions, Item, MoveOptions, MutateOptions, RemoveOptions } from './types';\nimport { Node } from './Node';\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", "import * as React from 'react';\nexport { React };\n"],
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["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", "import * as React from 'react';\nexport { React };\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADCvB,kBAAqB;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,iBAAK,MAAM;AAAA,QAC1B,YAAY;AAAA,QACZ,OAAO,cAAc,QAAQ;AAAA,QAC7B,QAAQ;AAA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AAI,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
6
|
"names": []
|
|
7
7
|
}
|
package/dist/cjs/Node.js
CHANGED
|
@@ -18,6 +18,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
return to;
|
|
19
19
|
};
|
|
20
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.
|
|
21
25
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
26
|
mod
|
|
23
27
|
));
|
|
@@ -31,6 +35,9 @@ var React = __toESM(require("react"));
|
|
|
31
35
|
var import_lodash = require("lodash");
|
|
32
36
|
class Node {
|
|
33
37
|
constructor(item, { childIndex, depth, parent, tree }) {
|
|
38
|
+
// ===========================================================================
|
|
39
|
+
// INTERNAL METHODS
|
|
40
|
+
// ===========================================================================
|
|
34
41
|
this.fixChildIndexes = () => {
|
|
35
42
|
for (let i = 0; i < this.children.length; i++) {
|
|
36
43
|
this.children[i].childIndex = i;
|
|
@@ -99,6 +106,9 @@ class Node {
|
|
|
99
106
|
...this.plainItem,
|
|
100
107
|
subitems: this.children.map((child) => child.getJson())
|
|
101
108
|
});
|
|
109
|
+
// ===========================================================================
|
|
110
|
+
// WRITE METHODS
|
|
111
|
+
// ===========================================================================
|
|
102
112
|
this.addNode = (node) => {
|
|
103
113
|
const position = node.childIndex;
|
|
104
114
|
if (position < 0 || position > this.children.length) {
|
|
@@ -134,6 +144,9 @@ class Node {
|
|
|
134
144
|
) ?? [];
|
|
135
145
|
this.plainItem = this.getCleanItem(item);
|
|
136
146
|
}
|
|
147
|
+
// ===========================================================================
|
|
148
|
+
// READ METHODS
|
|
149
|
+
// ===========================================================================
|
|
137
150
|
get hash() {
|
|
138
151
|
return this._hash;
|
|
139
152
|
}
|
package/dist/cjs/Node.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/Node.ts", "../../../../scripts/build/transpile/react-shim.js"],
|
|
4
|
-
"sourcesContent": ["/* eslint-disable no-use-before-define */\n/* eslint-disable max-params */\nimport { cloneDeep, omit } from 'lodash';\nimport type { DSTree } from './DSTree';\nimport type { Item, NodeConstructorOptions } from './types';\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", "import * as React from 'react';\nexport { React };\n"],
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["/* 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", "import * as React from 'react';\nexport { React };\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADEvB,oBAAgC;AAIzB,MAAM,KAAqB;AAAA,EAiBhC,YAAY,MAAS,EAAE,YAAY,OAAO,QAAQ,KAAK,GAA8B;AAqBrF;AAAA;AAAA;AAAA,2BAAkB,MAAY;AAC5B,eAAS,IAAI,GAAG,IAAI,KAAK,SAAS,QAAQ,KAAK;AAC7C,aAAK,SAAS,CAAC,EAAE,aAAa;AAAA,MAChC;AACA,WAAK,SAAS;AAAA,IAChB;AAEA,wBAAe,CAAC,SAAe;AAC7B,YAAM,gBAAY,oBAAK,MAAM,CAAC,UAAU,CAAC;AACzC,iBAAO,yBAAU,SAAc;AAAA,IACjC;AAUA,gBAAO,CAAC,aAA+C;AACrD,YAAM,wBAAwB,SAAS,IAAI;AAC3C,UAAI,uBAAuB;AACzB,mBAAW,SAAS,KAAK,UAAU;AACjC,gBAAM,KAAK,QAAQ;AAAA,QACrB;AAAA,MACF;AAAA,IACF;AAEA,uBAAc,CAAC,aAA+C;AAC5D,YAAM,wBAAwB,SAAS,IAAI;AAC3C,UAAI,yBAAyB,KAAK,QAAQ;AACxC,aAAK,OAAO,YAAY,QAAQ;AAAA,MAClC;AAAA,IACF;AAEA,oBAAW,CAAC,aAAyD;AAEnE,UAAI,QAAwB;AAC5B,WAAK,KAAK,CAAC,SAAS;AAElB,YAAI;AAAO,iBAAO;AAGlB,YAAI,SAAS,IAAI;AAAG,kBAAQ;AAC5B,eAAO,CAAC;AAAA,MACV,CAAC;AAED,aAAO;AAAA,IACT;AAEA,wBAAe,CAAC,aAAoD;AAClE,YAAM,QAAmB,CAAC;AAC1B,WAAK,KAAK,CAAC,SAAS;AAClB,YAAI,SAAS,IAAI;AAAG,gBAAM,KAAK,IAAI;AAC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AAK,KAAK,CAAC,UAAU;AACnB,aAAK,KAAK,8BAA8B,KAAK;AAC7C,eAAO;AAAA,MACT,CAAC;AAED,WAAK,SAAS;AAAA,IAChB;AAtJE,SAAK,OAAO,KAAK,YAAY,IAAI;AACjC,SAAK,aAAa;AAClB,SAAK,QAAQ;AACb,SAAK,SAAS,UAAU;AACxB,SAAK,OAAO;AACZ,SAAK,QAAQ;AAEb,SAAK,WACH,KAAK,UAAU;AAAA,MACb,CAAC,SAAS,UAAU,IAAI,KAAK,SAAc,EAAE,YAAY,OAAO,OAAO,QAAQ,GAAG,QAAQ,MAAM,KAAK,CAAC;AAAA,IACxG,KAAK,CAAC;AAGR,SAAK,YAAY,KAAK,aAAa,IAAI;AAAA,EACzC;AAAA;AAAA;AAAA;AAAA,EAsBA,IAAI,OAAe;AACjB,WAAO,KAAK;AAAA,EACd;AAiHF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/cjs/index.js
CHANGED
|
@@ -18,6 +18,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
return to;
|
|
19
19
|
};
|
|
20
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.
|
|
21
25
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
26
|
mod
|
|
23
27
|
));
|
|
@@ -29,6 +33,6 @@ __export(src_exports, {
|
|
|
29
33
|
});
|
|
30
34
|
module.exports = __toCommonJS(src_exports);
|
|
31
35
|
var React = __toESM(require("react"));
|
|
32
|
-
var import_DSTree = require("./DSTree");
|
|
33
|
-
var import_useDSTree = require("./useDSTree");
|
|
36
|
+
var import_DSTree = require("./DSTree.js");
|
|
37
|
+
var import_useDSTree = require("./useDSTree.js");
|
|
34
38
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.ts", "../../../../scripts/build/transpile/react-shim.js"],
|
|
4
|
-
"sourcesContent": ["export type { Item } from './types';\nexport { DSTree } from './DSTree';\nexport { useDSTree } from './useDSTree';\n", "import * as React from 'react';\nexport { React };\n"],
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["export type { Item } from './types.js';\nexport { DSTree } from './DSTree.js';\nexport { useDSTree } from './useDSTree.js';\n", "import * as React from 'react';\nexport { React };\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADCvB,oBAAuB;AACvB,uBAA0B;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/cjs/types.js
CHANGED
|
@@ -14,6 +14,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
14
14
|
return to;
|
|
15
15
|
};
|
|
16
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.
|
|
17
21
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
18
22
|
mod
|
|
19
23
|
));
|
package/dist/cjs/types.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/types.ts", "../../../../scripts/build/transpile/react-shim.js"],
|
|
4
|
-
"sourcesContent": ["import type { DSTree } from './DSTree';\nimport type { Node } from './Node';\n\nexport interface Item {\n subitems?: Item[];\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}\n", "import * as React from 'react';\nexport { React };\n"],
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["import type { DSTree } from './DSTree.js';\nimport type { Node } from './Node.js';\n\nexport interface Item {\n subitems?: Item[];\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}\n", "import * as React from 'react';\nexport { React };\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;ACAA,YAAuB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/cjs/useDSTree.js
CHANGED
|
@@ -18,6 +18,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
return to;
|
|
19
19
|
};
|
|
20
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.
|
|
21
25
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
26
|
mod
|
|
23
27
|
));
|
|
@@ -29,7 +33,7 @@ __export(useDSTree_exports, {
|
|
|
29
33
|
module.exports = __toCommonJS(useDSTree_exports);
|
|
30
34
|
var React = __toESM(require("react"));
|
|
31
35
|
var import_react = require("react");
|
|
32
|
-
var import_DSTree = require("./DSTree");
|
|
36
|
+
var import_DSTree = require("./DSTree.js");
|
|
33
37
|
const useDSTree = (rootItem, opts) => {
|
|
34
38
|
const tree = (0, import_react.useMemo)(() => new import_DSTree.DSTree(rootItem, opts), [opts, rootItem]);
|
|
35
39
|
const [hash, setHash] = (0, import_react.useState)(tree.hash);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/useDSTree.ts", "../../../../scripts/build/transpile/react-shim.js"],
|
|
4
|
-
"sourcesContent": ["import { useCallback, useMemo, useState } from 'react';\nimport { DSTree } from './DSTree';\nimport type { AddOptions, Item, MoveOptions, MutateOptions, RemoveOptions } from './types';\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": "
|
|
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,G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esm/DSTree.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
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';\nimport { Node } from './Node';\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;
|
|
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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,MAAqB;AACjD,WAAO,KAAK,MAAM,KAAK,IAAI;AAAA,EAC7B;AAAA,EAEA,IAAI,OAAe;AACjB,WAAO,KAAK;AAAA,EACd;AAoFF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/esm/Node.js
CHANGED
|
@@ -2,6 +2,9 @@ import * as React from "react";
|
|
|
2
2
|
import { cloneDeep, omit } from "lodash";
|
|
3
3
|
class Node {
|
|
4
4
|
constructor(item, { childIndex, depth, parent, tree }) {
|
|
5
|
+
// ===========================================================================
|
|
6
|
+
// INTERNAL METHODS
|
|
7
|
+
// ===========================================================================
|
|
5
8
|
this.fixChildIndexes = () => {
|
|
6
9
|
for (let i = 0; i < this.children.length; i++) {
|
|
7
10
|
this.children[i].childIndex = i;
|
|
@@ -70,6 +73,9 @@ class Node {
|
|
|
70
73
|
...this.plainItem,
|
|
71
74
|
subitems: this.children.map((child) => child.getJson())
|
|
72
75
|
});
|
|
76
|
+
// ===========================================================================
|
|
77
|
+
// WRITE METHODS
|
|
78
|
+
// ===========================================================================
|
|
73
79
|
this.addNode = (node) => {
|
|
74
80
|
const position = node.childIndex;
|
|
75
81
|
if (position < 0 || position > this.children.length) {
|
|
@@ -105,6 +111,9 @@ class Node {
|
|
|
105
111
|
) ?? [];
|
|
106
112
|
this.plainItem = this.getCleanItem(item);
|
|
107
113
|
}
|
|
114
|
+
// ===========================================================================
|
|
115
|
+
// READ METHODS
|
|
116
|
+
// ===========================================================================
|
|
108
117
|
get hash() {
|
|
109
118
|
return this._hash;
|
|
110
119
|
}
|
package/dist/esm/Node.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
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';\nimport type { Item, NodeConstructorOptions } from './types';\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": "AAAA,YAAY,WAAW;ACEvB,SAAS,WAAW,YAAY;AAIzB,MAAM,KAAqB;AAAA,EAiBhC,YAAY,MAAS,EAAE,YAAY,OAAO,QAAQ,KAAK,GAA8B;AAqBrF,2BAAkB,MAAY;AAC5B,eAAS,IAAI,GAAG,IAAI,KAAK,SAAS,QAAQ,KAAK;AAC7C,aAAK,SAAS,
|
|
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": "AAAA,YAAY,WAAW;ACEvB,SAAS,WAAW,YAAY;AAIzB,MAAM,KAAqB;AAAA,EAiBhC,YAAY,MAAS,EAAE,YAAY,OAAO,QAAQ,KAAK,GAA8B;AAqBrF;AAAA;AAAA;AAAA,2BAAkB,MAAY;AAC5B,eAAS,IAAI,GAAG,IAAI,KAAK,SAAS,QAAQ,KAAK;AAC7C,aAAK,SAAS,CAAC,E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AGR,SAAK,YAAY,KAAK,aAAa,IAAI;AAAA,EACzC;AAAA;AAAA;AAAA;AAAA,EAsBA,IAAI,OAAe;AACjB,WAAO,KAAK;AAAA,EACd;AAiHF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/esm/index.js
CHANGED
package/dist/esm/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../scripts/build/transpile/react-shim.js", "../../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["import * as React from 'react';\nexport { React };\n", "export type { Item } from './types';\nexport { DSTree } from './DSTree';\nexport { useDSTree } from './useDSTree';\n"],
|
|
4
|
+
"sourcesContent": ["import * as React from 'react';\nexport { React };\n", "export type { Item } from './types.js';\nexport { DSTree } from './DSTree.js';\nexport { useDSTree } from './useDSTree.js';\n"],
|
|
5
5
|
"mappings": "AAAA,YAAY,WAAW;ACCvB,SAAS,cAAc;AACvB,SAAS,iBAAiB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/esm/useDSTree.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
2
|
import { useCallback, useMemo, useState } from "react";
|
|
3
|
-
import { DSTree } from "./DSTree";
|
|
3
|
+
import { DSTree } from "./DSTree.js";
|
|
4
4
|
const useDSTree = (rootItem, opts) => {
|
|
5
5
|
const tree = useMemo(() => new DSTree(rootItem, opts), [opts, rootItem]);
|
|
6
6
|
const [hash, setHash] = useState(tree.hash);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../scripts/build/transpile/react-shim.js", "../../src/useDSTree.ts"],
|
|
4
|
-
"sourcesContent": ["import * as React from 'react';\nexport { React };\n", "import { useCallback, useMemo, useState } from 'react';\nimport { DSTree } from './DSTree';\nimport type { AddOptions, Item, MoveOptions, MutateOptions, RemoveOptions } from './types';\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"],
|
|
4
|
+
"sourcesContent": ["import * as React from 'react';\nexport { React };\n", "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"],
|
|
5
5
|
"mappings": "AAAA,YAAY,WAAW;ACAvB,SAAS,aAAa,SAAS,gBAAgB;AAC/C,SAAS,cAAc;AAGhB,MAAM,YAAY,CAAiB,UAAa,SAA+C;AACpG,QAAM,OAAO,QAAQ,MAAM,IAAI,OAAO,UAAU,IAAI,GAAG,CAAC,MAAM,QAAQ,CAAC;AACvE,QAAM,CAAC,MAAM,OAAO,IAAI,SAAS,KAAK,IAAI;AAE1C,QAAM,UAAU;AAAA,IACd,CAAC,MA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A,IACd;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,SAAS,KAAK;AAAA,IACd,YAAY,KAAK;AAAA,EACnB;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/types/DSTree.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { AddOptions, Item, MoveOptions, MutateOptions, RemoveOptions } from './types';
|
|
2
|
-
import { Node } from './Node';
|
|
1
|
+
import type { AddOptions, Item, MoveOptions, MutateOptions, RemoveOptions } from './types.js';
|
|
2
|
+
import { Node } from './Node.js';
|
|
3
3
|
export declare class DSTree<T extends Item> {
|
|
4
4
|
private root;
|
|
5
5
|
private _hash;
|
package/dist/types/Node.d.ts
CHANGED
package/dist/types/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export type { Item } from './types';
|
|
2
|
-
export { DSTree } from './DSTree';
|
|
3
|
-
export { useDSTree } from './useDSTree';
|
|
1
|
+
export type { Item } from './types.js';
|
|
2
|
+
export { DSTree } from './DSTree.js';
|
|
3
|
+
export { useDSTree } from './useDSTree.js';
|
package/dist/types/types.d.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import type { AddOptions, Item, MoveOptions, MutateOptions, RemoveOptions } from './types';
|
|
1
|
+
import type { AddOptions, Item, MoveOptions, MutateOptions, RemoveOptions } from './types.js';
|
|
2
2
|
export declare const useDSTree: <T extends Item>(rootItem: T, opts: {
|
|
3
3
|
getUniqueId: (item: T) => string;
|
|
4
4
|
}) => {
|
|
5
5
|
hash: number;
|
|
6
|
-
getRoot: () => import("./Node").Node<T>;
|
|
7
|
-
getNode: (id: string) => import("./Node").Node<T>;
|
|
8
|
-
walk: (callback: (node: import("./Node").Node<T>) => boolean) => void;
|
|
9
|
-
walkParents: (callback: (node: import("./Node").Node<T>) => boolean) => void;
|
|
10
|
-
findNode: (callback: (node: import("./Node").Node<T>) => boolean) => import("./Node").Node<T> | null;
|
|
11
|
-
findAllNodes: (callback: (node: import("./Node").Node<T>) => boolean) => import("./Node").Node<T>[];
|
|
12
|
-
flatten: () => import("./Node").Node<T>[];
|
|
13
|
-
addNode: (item: T, options?: AddOptions<T> | undefined) => import("./Node").Node<T>;
|
|
14
|
-
removeNode: (id: string, options?: RemoveOptions<T> | undefined) => import("./Node").Node<T>;
|
|
15
|
-
moveNode: (id: string, options?: MoveOptions<T> | undefined) => import("./Node").Node<T>;
|
|
16
|
-
replaceNode: (id: string, item: T, options?: MutateOptions<T> | undefined) => import("./Node").Node<T>;
|
|
17
|
-
getPath: (id: string) => import("./Node").Node<T>[];
|
|
6
|
+
getRoot: () => import("./Node.js").Node<T>;
|
|
7
|
+
getNode: (id: string) => import("./Node.js").Node<T>;
|
|
8
|
+
walk: (callback: (node: import("./Node.js").Node<T>) => boolean) => void;
|
|
9
|
+
walkParents: (callback: (node: import("./Node.js").Node<T>) => boolean) => void;
|
|
10
|
+
findNode: (callback: (node: import("./Node.js").Node<T>) => boolean) => import("./Node.js").Node<T> | null;
|
|
11
|
+
findAllNodes: (callback: (node: import("./Node.js").Node<T>) => boolean) => import("./Node.js").Node<T>[];
|
|
12
|
+
flatten: () => import("./Node.js").Node<T>[];
|
|
13
|
+
addNode: (item: T, options?: AddOptions<T> | undefined) => import("./Node.js").Node<T>;
|
|
14
|
+
removeNode: (id: string, options?: RemoveOptions<T> | undefined) => import("./Node.js").Node<T>;
|
|
15
|
+
moveNode: (id: string, options?: MoveOptions<T> | undefined) => import("./Node.js").Node<T>;
|
|
16
|
+
replaceNode: (id: string, item: T, options?: MutateOptions<T> | undefined) => import("./Node.js").Node<T>;
|
|
17
|
+
getPath: (id: string) => import("./Node.js").Node<T>[];
|
|
18
18
|
getPathIds: (id: string) => string[];
|
|
19
19
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elliemae/ds-tree-model",
|
|
3
|
-
"version": "3.16.0-next.
|
|
3
|
+
"version": "3.16.0-next.20",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "ICE MT - Dimsum - Tree Model",
|
|
6
6
|
"files": [
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"eslint:fix": "eslint --ext='.js,.jsx,.test.js,.ts,.tsx' --fix --config='../../.eslintrc.js' src/",
|
|
53
53
|
"dts": "node ../../scripts/dts.mjs",
|
|
54
54
|
"build": "cross-env NODE_ENV=production node ../../scripts/build/build.mjs",
|
|
55
|
-
"dev:build": "pnpm --filter {.}... build
|
|
55
|
+
"dev:build": "pnpm --filter {.}... build",
|
|
56
56
|
"dev:install": "pnpm --filter {.}... i --no-lockfile && pnpm run dev:build",
|
|
57
57
|
"checkDeps": "npx -yes ../ds-codemods check-missing-packages --projectFolderPath=\"./\" --ignorePackagesGlobPattern=\"\" --ignoreFilesGlobPattern=\"**/test-ables/*,**/tests/*\"",
|
|
58
58
|
"dev": "cross-env NODE_ENV=development node ../../scripts/build/build.mjs --watch"
|