@elliemae/ds-tree-model 3.16.0-next.1 → 3.16.0-next.10
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 +1 -1
- package/dist/cjs/DSTree.js.map +1 -1
- package/dist/cjs/Node.js.map +1 -1
- package/dist/cjs/index.js +2 -2
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/package.json +7 -0
- package/dist/cjs/types.js.map +1 -1
- package/dist/cjs/useDSTree.js +1 -1
- package/dist/cjs/useDSTree.js.map +1 -1
- package/dist/esm/DSTree.js +1 -1
- package/dist/esm/DSTree.js.map +1 -1
- package/dist/esm/Node.js.map +1 -1
- 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
|
@@ -28,7 +28,7 @@ __export(DSTree_exports, {
|
|
|
28
28
|
});
|
|
29
29
|
module.exports = __toCommonJS(DSTree_exports);
|
|
30
30
|
var React = __toESM(require("react"));
|
|
31
|
-
var import_Node = require("./Node");
|
|
31
|
+
var import_Node = require("./Node.js");
|
|
32
32
|
class DSTree {
|
|
33
33
|
constructor(item, options) {
|
|
34
34
|
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"],
|
|
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
5
|
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADCvB,kBAAqB;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AAK,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,iBAAK,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,QAAQ;AACxB,aAAO;AAAA,IACT,CAAC;AAAA,EACH;AAAA,EAEA,yBAAyB,MAAqB;AAE5C,QAAI,KAAK,MAAM,KAAK,OAAO;AACzB,YAAM,IAAI,MAAM,yBAAyB,KAAK,MAAM;AAAA,IACtD;AAEA,SAAK,MAAM,KAAK,QAAQ;AAAA,EAC1B;AAAA,EAEA,8BAA8B,MAAqB;AACjD,WAAO,KAAK,MAAM,KAAK;AAAA,EACzB;AAAA,EAEA,IAAI,OAAe;AACjB,WAAO,KAAK;AAAA,EACd;AAoFF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
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"],
|
|
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
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,2BAAkB,MAAY;AAC5B,eAAS,IAAI,GAAG,IAAI,KAAK,SAAS,QAAQ,KAAK;AAC7C,aAAK,SAAS,GAAG,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,aA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|
|
|
29
29
|
});
|
|
30
30
|
module.exports = __toCommonJS(src_exports);
|
|
31
31
|
var React = __toESM(require("react"));
|
|
32
|
-
var import_DSTree = require("./DSTree");
|
|
33
|
-
var import_useDSTree = require("./useDSTree");
|
|
32
|
+
var import_DSTree = require("./DSTree.js");
|
|
33
|
+
var import_useDSTree = require("./useDSTree.js");
|
|
34
34
|
//# 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"],
|
|
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
5
|
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;ACAA,YAAuB;ADCvB,oBAAuB;AACvB,uBAA0B;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
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"],
|
|
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
5
|
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;ACAA,YAAuB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/cjs/useDSTree.js
CHANGED
|
@@ -29,7 +29,7 @@ __export(useDSTree_exports, {
|
|
|
29
29
|
module.exports = __toCommonJS(useDSTree_exports);
|
|
30
30
|
var React = __toESM(require("react"));
|
|
31
31
|
var import_react = require("react");
|
|
32
|
-
var import_DSTree = require("./DSTree");
|
|
32
|
+
var import_DSTree = require("./DSTree.js");
|
|
33
33
|
const useDSTree = (rootItem, opts) => {
|
|
34
34
|
const tree = (0, import_react.useMemo)(() => new import_DSTree.DSTree(rootItem, opts), [opts, rootItem]);
|
|
35
35
|
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"],
|
|
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
5
|
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AA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K,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
6
|
"names": []
|
|
7
7
|
}
|
package/dist/esm/DSTree.js
CHANGED
package/dist/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"],
|
|
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
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;AAE9C,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,cA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C,SAAS;AACvB,WAAK,MAAM,KAAK,QAAQ;AACxB,aAAO;AAAA,IACT,CAAC;AAAA,EACH;AAAA,EAEA,yBAAyB,MAAqB;AAE5C,QAAI,KAAK,MAAM,KAAK,OAAO;AACzB,YAAM,IAAI,MAAM,yBAAyB,KAAK,MAAM;AAAA,IACtD;AAEA,SAAK,MAAM,KAAK,QAAQ;AAAA,EAC1B;AAAA,EAEA,8BAA8B,MAAqB;AACjD,WAAO,KAAK,MAAM,KAAK;AAAA,EACzB;AAAA,EAEA,IAAI,OAAe;AACjB,WAAO,KAAK;AAAA,EACd;AAoFF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
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"],
|
|
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
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,GAAG,aAAa;AAAA,MAChC;AACA,WAAK,SAAS;AAAA,IAChB;AAEA,wBAAe,CAAC,SAAe;AAC7B,YAAM,YAAY,KAAK,MAAM,CAAC,UAAU,CAAC;AACzC,aAAO,UAAU,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;AACnC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U;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,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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ACzD,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
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.10",
|
|
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"
|