@ebay/nice-dag-react 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2021 eBay Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # nice-dag-react
2
+
3
+ ## What is nice-dag-react?
4
+
5
+ `nice-dag-react` is a wrapper of DOM operations given by `nice-dag-core`. In terms of React applications, you can simply use this customized react hook which is given by `nice-dag-react` module.
6
+
7
+ For more details, please you can visit [Doc Portal](https://opensource.ebay.com/nice-dag/docs/api-ref/useNiceDag).
@@ -0,0 +1,60 @@
1
+ import React, { MutableRefObject } from 'react';
2
+ import NiceDagTypes, { IEdge, MinimapConfig } from '@ebay/nice-dag-core/lib/esm/types';
3
+ export declare type UseNiceDagArgs = Omit<NiceDagTypes.NiceDagInitArgs, 'container'> & {
4
+ renderNode: (props: NiceDagNodeProps) => React.Component<any, any, any>;
5
+ renderEdge?: (props: NiceDagEdgeProps) => React.Component<any, any, any>;
6
+ scrollPosition?: NiceDagTypes.Point;
7
+ initNodes?: NiceDagTypes.Node[];
8
+ onMount?: () => void;
9
+ editable?: boolean;
10
+ minimapConfig?: MinimapConfig;
11
+ };
12
+ declare type RenderNodeType = UseNiceDagArgs['renderNode'];
13
+ declare type RenderEdgeType = UseNiceDagArgs['renderEdge'];
14
+ export declare type UseNiceDagType = {
15
+ niceDagEl: MutableRefObject<any>;
16
+ minimapEl: MutableRefObject<any>;
17
+ niceDag: NiceDagTypes.NiceDag;
18
+ render: () => JSX.Element[];
19
+ reset: () => void;
20
+ };
21
+ export interface NiceDagNodeProps {
22
+ node: NiceDagTypes.IViewNode;
23
+ render: RenderNodeType;
24
+ niceDag: NiceDagTypes.NiceDag;
25
+ patchVersion: number;
26
+ }
27
+ export interface NiceDagEdgeProps {
28
+ niceDag: NiceDagTypes.NiceDag;
29
+ edge: NiceDagTypes.IEdge;
30
+ render: RenderEdgeType;
31
+ patchVersion: number;
32
+ }
33
+ export declare class NiceDagNode extends React.Component<NiceDagNodeProps, any> {
34
+ render(): React.ReactPortal;
35
+ }
36
+ export declare class NiceDagEdge extends React.Component<NiceDagEdgeProps, any> {
37
+ render(): React.ReactPortal;
38
+ }
39
+ export declare type DagViewProps = Omit<NiceDagTypes.NiceDagInitArgs, 'container'> & {
40
+ initNodes?: NiceDagTypes.Node[];
41
+ renderHeader: () => React.Component<any, any>;
42
+ renderFooter: () => React.Component<any, any>;
43
+ renderNode: (props: NiceDagNodeProps) => React.Component<any, any>;
44
+ onMount?: () => void;
45
+ editable?: boolean;
46
+ };
47
+ export declare function useNiceDagEdge(edge: IEdge): {
48
+ onEdgeRemove: () => void;
49
+ onNodeInsert: (nodes: NiceDagTypes.Node[], offset: number) => void;
50
+ };
51
+ export declare function useNiceDagNode({ node, niceDag }: {
52
+ node: NiceDagTypes.IViewNode;
53
+ niceDag: NiceDagTypes.IWritableNiceDag;
54
+ }): {
55
+ onNodeRemove: () => void;
56
+ startEdgeDragging: (e: MouseEvent) => void;
57
+ startNodeDragging: (e: MouseEvent) => void;
58
+ };
59
+ export declare function useNiceDag(args: UseNiceDagArgs): UseNiceDagType;
60
+ export {};
@@ -0,0 +1,218 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
28
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
29
+ if (k2 === undefined) k2 = k;
30
+ var desc = Object.getOwnPropertyDescriptor(m, k);
31
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
32
+ desc = { enumerable: true, get: function() { return m[k]; } };
33
+ }
34
+ Object.defineProperty(o, k2, desc);
35
+ }) : (function(o, m, k, k2) {
36
+ if (k2 === undefined) k2 = k;
37
+ o[k2] = m[k];
38
+ }));
39
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
40
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
41
+ }) : function(o, v) {
42
+ o["default"] = v;
43
+ });
44
+ var __importStar = (this && this.__importStar) || function (mod) {
45
+ if (mod && mod.__esModule) return mod;
46
+ var result = {};
47
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
48
+ __setModuleDefault(result, mod);
49
+ return result;
50
+ };
51
+ var __rest = (this && this.__rest) || function (s, e) {
52
+ var t = {};
53
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
54
+ t[p] = s[p];
55
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
56
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
57
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
58
+ t[p[i]] = s[p[i]];
59
+ }
60
+ return t;
61
+ };
62
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
63
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
64
+ if (ar || !(i in from)) {
65
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
66
+ ar[i] = from[i];
67
+ }
68
+ }
69
+ return to.concat(ar || Array.prototype.slice.call(from));
70
+ };
71
+ var __importDefault = (this && this.__importDefault) || function (mod) {
72
+ return (mod && mod.__esModule) ? mod : { "default": mod };
73
+ };
74
+ Object.defineProperty(exports, "__esModule", { value: true });
75
+ exports.useNiceDag = exports.useNiceDagNode = exports.useNiceDagEdge = exports.NiceDagEdge = exports.NiceDagNode = void 0;
76
+ var react_1 = __importStar(require("react"));
77
+ var react_dom_1 = __importDefault(require("react-dom"));
78
+ var nice_dag_core_1 = __importDefault(require("@ebay/nice-dag-core"));
79
+ var NiceDagNode = /** @class */ (function (_super) {
80
+ __extends(NiceDagNode, _super);
81
+ function NiceDagNode() {
82
+ return _super !== null && _super.apply(this, arguments) || this;
83
+ }
84
+ NiceDagNode.prototype.render = function () {
85
+ if (this.props.niceDag && this.props.node.ref) {
86
+ var domRef = this.props.niceDag.getElementByNodeId(this.props.node.id);
87
+ if (domRef) {
88
+ return react_dom_1.default.createPortal(react_1.default.createElement(react_1.default.Fragment, null, this.props.render(this.props)), domRef);
89
+ }
90
+ }
91
+ return null;
92
+ };
93
+ return NiceDagNode;
94
+ }(react_1.default.Component));
95
+ exports.NiceDagNode = NiceDagNode;
96
+ ;
97
+ var NiceDagEdge = /** @class */ (function (_super) {
98
+ __extends(NiceDagEdge, _super);
99
+ function NiceDagEdge() {
100
+ return _super !== null && _super.apply(this, arguments) || this;
101
+ }
102
+ NiceDagEdge.prototype.render = function () {
103
+ if (this.props.niceDag && this.props.edge.ref) {
104
+ return react_dom_1.default.createPortal(react_1.default.createElement(react_1.default.Fragment, null, this.props.render(this.props)), this.props.edge.ref);
105
+ }
106
+ return null;
107
+ };
108
+ return NiceDagEdge;
109
+ }(react_1.default.Component));
110
+ exports.NiceDagEdge = NiceDagEdge;
111
+ ;
112
+ function useNiceDagEdge(edge) {
113
+ var onEdgeRemove = (0, react_1.useCallback)(function () {
114
+ edge.remove();
115
+ }, [edge]);
116
+ var onNodeInsert = (0, react_1.useCallback)(function (nodes, offset) {
117
+ if (offset === void 0) { offset = 40; }
118
+ edge.insertNodes(nodes, offset);
119
+ }, [edge]);
120
+ return { onEdgeRemove: onEdgeRemove, onNodeInsert: onNodeInsert };
121
+ }
122
+ exports.useNiceDagEdge = useNiceDagEdge;
123
+ function useNiceDagNode(_a) {
124
+ var node = _a.node, niceDag = _a.niceDag;
125
+ var onNodeRemove = (0, react_1.useCallback)(function () {
126
+ node.remove();
127
+ }, [node]);
128
+ var startEdgeDragging = (0, react_1.useCallback)(function (e) {
129
+ niceDag.startEdgeDragging(node, e);
130
+ }, [niceDag, node]);
131
+ var startNodeDragging = (0, react_1.useCallback)(function (e) {
132
+ niceDag.startNodeDragging(node, e);
133
+ }, [niceDag, node]);
134
+ return { onNodeRemove: onNodeRemove, startEdgeDragging: startEdgeDragging, startNodeDragging: startNodeDragging };
135
+ }
136
+ exports.useNiceDagNode = useNiceDagNode;
137
+ function validateProps(args) {
138
+ var initNodes = args.initNodes;
139
+ if (!initNodes) {
140
+ throw new Error("initNodes can't be undefined!");
141
+ }
142
+ }
143
+ function useNiceDag(args) {
144
+ validateProps(args);
145
+ var onMount = args.onMount, renderNode = args.renderNode, renderEdge = args.renderEdge, scrollPosition = args.scrollPosition, initNodes = args.initNodes, minimapConfig = args.minimapConfig, niceDagConfig = __rest(args, ["onMount", "renderNode", "renderEdge", "scrollPosition", "initNodes", "minimapConfig"]);
146
+ var niceDagEl = (0, react_1.useRef)(null);
147
+ var minimapEl = (0, react_1.useRef)(null);
148
+ var niceDagRef = (0, react_1.useRef)(null);
149
+ var prevInitNodesRef = (0, react_1.useRef)();
150
+ var _a = (0, react_1.useState)(0), patchVersion = _a[0], setPatchVersion = _a[1];
151
+ var reset = (0, react_1.useCallback)(function () {
152
+ if (niceDagRef.current) {
153
+ niceDagRef.current.destory();
154
+ }
155
+ }, [setPatchVersion]);
156
+ var onNiceDagChange = (0, react_1.useMemo)(function () {
157
+ return {
158
+ onChange: function () {
159
+ setPatchVersion(patchVersion + 1);
160
+ }
161
+ };
162
+ }, [setPatchVersion, patchVersion]);
163
+ (0, react_1.useEffect)(function () {
164
+ if (prevInitNodesRef.current !== initNodes) {
165
+ if (!niceDagRef.current) {
166
+ var niceDag = nice_dag_core_1.default.init(__assign({ container: niceDagEl.current, minimapContainer: minimapEl.current, minimapConfig: minimapConfig }, niceDagConfig), args.editable);
167
+ niceDagRef.current = niceDag;
168
+ }
169
+ niceDagRef.current.withNodes(initNodes).render();
170
+ setPatchVersion(patchVersion + 1);
171
+ prevInitNodesRef.current = initNodes;
172
+ onMount && onMount();
173
+ }
174
+ /**
175
+ * Add listener here is to pass onChange closure variables "patchVersion"
176
+ */
177
+ niceDagRef.current.addNiceDagChangeListener(onNiceDagChange);
178
+ return function () {
179
+ niceDagRef.current.removeNiceDagChangeListener(onNiceDagChange);
180
+ };
181
+ }, [initNodes, setPatchVersion, patchVersion, onMount]);
182
+ /**
183
+ * Just clean when the component is removed
184
+ */
185
+ (0, react_1.useEffect)(function () {
186
+ if (niceDagRef.current && niceDagRef.current.isDestoried) {
187
+ niceDagRef.current.withNodes(initNodes).render();
188
+ setPatchVersion(patchVersion + 1);
189
+ }
190
+ return function () {
191
+ if (niceDagRef.current) {
192
+ niceDagRef.current.destory();
193
+ }
194
+ };
195
+ }, []);
196
+ return {
197
+ niceDag: niceDagRef.current,
198
+ niceDagEl: niceDagEl,
199
+ minimapEl: minimapEl,
200
+ render: function () {
201
+ if (niceDagRef.current) {
202
+ var allNodes = niceDagRef.current.getAllNodes();
203
+ var elements = (allNodes === null || allNodes === void 0 ? void 0 : allNodes.length) > 0 ? allNodes.map(function (node) { return react_1.default.createElement(NiceDagNode, { render: renderNode, node: node, key: node.id, niceDag: niceDagRef.current, patchVersion: patchVersion }); }) : null;
204
+ if (renderEdge) {
205
+ var allEdges = niceDagRef.current.getAllEdges();
206
+ var edgeElements = (allEdges === null || allEdges === void 0 ? void 0 : allEdges.length) > 0 ? allEdges.map(function (edge) {
207
+ return react_1.default.createElement(NiceDagEdge, { render: renderEdge, edge: edge, patchVersion: patchVersion, key: "edge-".concat(edge.source.id, "-").concat(edge.target.id), niceDag: niceDagRef.current });
208
+ }) : [];
209
+ elements = __spreadArray(__spreadArray([], elements, true), edgeElements, true);
210
+ }
211
+ return elements;
212
+ }
213
+ return null;
214
+ },
215
+ reset: reset
216
+ };
217
+ }
218
+ exports.useNiceDag = useNiceDag;
@@ -0,0 +1,60 @@
1
+ import React, { MutableRefObject } from 'react';
2
+ import NiceDagTypes, { IEdge, MinimapConfig } from '@ebay/nice-dag-core/lib/esm/types';
3
+ export declare type UseNiceDagArgs = Omit<NiceDagTypes.NiceDagInitArgs, 'container'> & {
4
+ renderNode: (props: NiceDagNodeProps) => React.Component<any, any, any>;
5
+ renderEdge?: (props: NiceDagEdgeProps) => React.Component<any, any, any>;
6
+ scrollPosition?: NiceDagTypes.Point;
7
+ initNodes?: NiceDagTypes.Node[];
8
+ onMount?: () => void;
9
+ editable?: boolean;
10
+ minimapConfig?: MinimapConfig;
11
+ };
12
+ declare type RenderNodeType = UseNiceDagArgs['renderNode'];
13
+ declare type RenderEdgeType = UseNiceDagArgs['renderEdge'];
14
+ export declare type UseNiceDagType = {
15
+ niceDagEl: MutableRefObject<any>;
16
+ minimapEl: MutableRefObject<any>;
17
+ niceDag: NiceDagTypes.NiceDag;
18
+ render: () => JSX.Element[];
19
+ reset: () => void;
20
+ };
21
+ export interface NiceDagNodeProps {
22
+ node: NiceDagTypes.IViewNode;
23
+ render: RenderNodeType;
24
+ niceDag: NiceDagTypes.NiceDag;
25
+ patchVersion: number;
26
+ }
27
+ export interface NiceDagEdgeProps {
28
+ niceDag: NiceDagTypes.NiceDag;
29
+ edge: NiceDagTypes.IEdge;
30
+ render: RenderEdgeType;
31
+ patchVersion: number;
32
+ }
33
+ export declare class NiceDagNode extends React.Component<NiceDagNodeProps, any> {
34
+ render(): React.ReactPortal;
35
+ }
36
+ export declare class NiceDagEdge extends React.Component<NiceDagEdgeProps, any> {
37
+ render(): React.ReactPortal;
38
+ }
39
+ export declare type DagViewProps = Omit<NiceDagTypes.NiceDagInitArgs, 'container'> & {
40
+ initNodes?: NiceDagTypes.Node[];
41
+ renderHeader: () => React.Component<any, any>;
42
+ renderFooter: () => React.Component<any, any>;
43
+ renderNode: (props: NiceDagNodeProps) => React.Component<any, any>;
44
+ onMount?: () => void;
45
+ editable?: boolean;
46
+ };
47
+ export declare function useNiceDagEdge(edge: IEdge): {
48
+ onEdgeRemove: () => void;
49
+ onNodeInsert: (nodes: NiceDagTypes.Node[], offset: number) => void;
50
+ };
51
+ export declare function useNiceDagNode({ node, niceDag }: {
52
+ node: NiceDagTypes.IViewNode;
53
+ niceDag: NiceDagTypes.IWritableNiceDag;
54
+ }): {
55
+ onNodeRemove: () => void;
56
+ startEdgeDragging: (e: MouseEvent) => void;
57
+ startNodeDragging: (e: MouseEvent) => void;
58
+ };
59
+ export declare function useNiceDag(args: UseNiceDagArgs): UseNiceDagType;
60
+ export {};
@@ -0,0 +1,186 @@
1
+ var __extends = (this && this.__extends) || (function () {
2
+ var extendStatics = function (d, b) {
3
+ extendStatics = Object.setPrototypeOf ||
4
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
6
+ return extendStatics(d, b);
7
+ };
8
+ return function (d, b) {
9
+ if (typeof b !== "function" && b !== null)
10
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
11
+ extendStatics(d, b);
12
+ function __() { this.constructor = d; }
13
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
14
+ };
15
+ })();
16
+ var __assign = (this && this.__assign) || function () {
17
+ __assign = Object.assign || function(t) {
18
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
19
+ s = arguments[i];
20
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
21
+ t[p] = s[p];
22
+ }
23
+ return t;
24
+ };
25
+ return __assign.apply(this, arguments);
26
+ };
27
+ var __rest = (this && this.__rest) || function (s, e) {
28
+ var t = {};
29
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
30
+ t[p] = s[p];
31
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
32
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
33
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
34
+ t[p[i]] = s[p[i]];
35
+ }
36
+ return t;
37
+ };
38
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
39
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
40
+ if (ar || !(i in from)) {
41
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
42
+ ar[i] = from[i];
43
+ }
44
+ }
45
+ return to.concat(ar || Array.prototype.slice.call(from));
46
+ };
47
+ import React, { useEffect, useState, useRef, useCallback, useMemo } from 'react';
48
+ import ReactDOM from 'react-dom';
49
+ import NiceDag from '@ebay/nice-dag-core';
50
+ var NiceDagNode = /** @class */ (function (_super) {
51
+ __extends(NiceDagNode, _super);
52
+ function NiceDagNode() {
53
+ return _super !== null && _super.apply(this, arguments) || this;
54
+ }
55
+ NiceDagNode.prototype.render = function () {
56
+ if (this.props.niceDag && this.props.node.ref) {
57
+ var domRef = this.props.niceDag.getElementByNodeId(this.props.node.id);
58
+ if (domRef) {
59
+ return ReactDOM.createPortal(React.createElement(React.Fragment, null, this.props.render(this.props)), domRef);
60
+ }
61
+ }
62
+ return null;
63
+ };
64
+ return NiceDagNode;
65
+ }(React.Component));
66
+ export { NiceDagNode };
67
+ ;
68
+ var NiceDagEdge = /** @class */ (function (_super) {
69
+ __extends(NiceDagEdge, _super);
70
+ function NiceDagEdge() {
71
+ return _super !== null && _super.apply(this, arguments) || this;
72
+ }
73
+ NiceDagEdge.prototype.render = function () {
74
+ if (this.props.niceDag && this.props.edge.ref) {
75
+ return ReactDOM.createPortal(React.createElement(React.Fragment, null, this.props.render(this.props)), this.props.edge.ref);
76
+ }
77
+ return null;
78
+ };
79
+ return NiceDagEdge;
80
+ }(React.Component));
81
+ export { NiceDagEdge };
82
+ ;
83
+ export function useNiceDagEdge(edge) {
84
+ var onEdgeRemove = useCallback(function () {
85
+ edge.remove();
86
+ }, [edge]);
87
+ var onNodeInsert = useCallback(function (nodes, offset) {
88
+ if (offset === void 0) { offset = 40; }
89
+ edge.insertNodes(nodes, offset);
90
+ }, [edge]);
91
+ return { onEdgeRemove: onEdgeRemove, onNodeInsert: onNodeInsert };
92
+ }
93
+ export function useNiceDagNode(_a) {
94
+ var node = _a.node, niceDag = _a.niceDag;
95
+ var onNodeRemove = useCallback(function () {
96
+ node.remove();
97
+ }, [node]);
98
+ var startEdgeDragging = useCallback(function (e) {
99
+ niceDag.startEdgeDragging(node, e);
100
+ }, [niceDag, node]);
101
+ var startNodeDragging = useCallback(function (e) {
102
+ niceDag.startNodeDragging(node, e);
103
+ }, [niceDag, node]);
104
+ return { onNodeRemove: onNodeRemove, startEdgeDragging: startEdgeDragging, startNodeDragging: startNodeDragging };
105
+ }
106
+ function validateProps(args) {
107
+ var initNodes = args.initNodes;
108
+ if (!initNodes) {
109
+ throw new Error("initNodes can't be undefined!");
110
+ }
111
+ }
112
+ export function useNiceDag(args) {
113
+ validateProps(args);
114
+ var onMount = args.onMount, renderNode = args.renderNode, renderEdge = args.renderEdge, scrollPosition = args.scrollPosition, initNodes = args.initNodes, minimapConfig = args.minimapConfig, niceDagConfig = __rest(args, ["onMount", "renderNode", "renderEdge", "scrollPosition", "initNodes", "minimapConfig"]);
115
+ var niceDagEl = useRef(null);
116
+ var minimapEl = useRef(null);
117
+ var niceDagRef = useRef(null);
118
+ var prevInitNodesRef = useRef();
119
+ var _a = useState(0), patchVersion = _a[0], setPatchVersion = _a[1];
120
+ var reset = useCallback(function () {
121
+ if (niceDagRef.current) {
122
+ niceDagRef.current.destory();
123
+ }
124
+ }, [setPatchVersion]);
125
+ var onNiceDagChange = useMemo(function () {
126
+ return {
127
+ onChange: function () {
128
+ setPatchVersion(patchVersion + 1);
129
+ }
130
+ };
131
+ }, [setPatchVersion, patchVersion]);
132
+ useEffect(function () {
133
+ if (prevInitNodesRef.current !== initNodes) {
134
+ if (!niceDagRef.current) {
135
+ var niceDag = NiceDag.init(__assign({ container: niceDagEl.current, minimapContainer: minimapEl.current, minimapConfig: minimapConfig }, niceDagConfig), args.editable);
136
+ niceDagRef.current = niceDag;
137
+ }
138
+ niceDagRef.current.withNodes(initNodes).render();
139
+ setPatchVersion(patchVersion + 1);
140
+ prevInitNodesRef.current = initNodes;
141
+ onMount && onMount();
142
+ }
143
+ /**
144
+ * Add listener here is to pass onChange closure variables "patchVersion"
145
+ */
146
+ niceDagRef.current.addNiceDagChangeListener(onNiceDagChange);
147
+ return function () {
148
+ niceDagRef.current.removeNiceDagChangeListener(onNiceDagChange);
149
+ };
150
+ }, [initNodes, setPatchVersion, patchVersion, onMount]);
151
+ /**
152
+ * Just clean when the component is removed
153
+ */
154
+ useEffect(function () {
155
+ if (niceDagRef.current && niceDagRef.current.isDestoried) {
156
+ niceDagRef.current.withNodes(initNodes).render();
157
+ setPatchVersion(patchVersion + 1);
158
+ }
159
+ return function () {
160
+ if (niceDagRef.current) {
161
+ niceDagRef.current.destory();
162
+ }
163
+ };
164
+ }, []);
165
+ return {
166
+ niceDag: niceDagRef.current,
167
+ niceDagEl: niceDagEl,
168
+ minimapEl: minimapEl,
169
+ render: function () {
170
+ if (niceDagRef.current) {
171
+ var allNodes = niceDagRef.current.getAllNodes();
172
+ var elements = (allNodes === null || allNodes === void 0 ? void 0 : allNodes.length) > 0 ? allNodes.map(function (node) { return React.createElement(NiceDagNode, { render: renderNode, node: node, key: node.id, niceDag: niceDagRef.current, patchVersion: patchVersion }); }) : null;
173
+ if (renderEdge) {
174
+ var allEdges = niceDagRef.current.getAllEdges();
175
+ var edgeElements = (allEdges === null || allEdges === void 0 ? void 0 : allEdges.length) > 0 ? allEdges.map(function (edge) {
176
+ return React.createElement(NiceDagEdge, { render: renderEdge, edge: edge, patchVersion: patchVersion, key: "edge-".concat(edge.source.id, "-").concat(edge.target.id), niceDag: niceDagRef.current });
177
+ }) : [];
178
+ elements = __spreadArray(__spreadArray([], elements, true), edgeElements, true);
179
+ }
180
+ return elements;
181
+ }
182
+ return null;
183
+ },
184
+ reset: reset
185
+ };
186
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ebay/nice-dag-react",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "license": "MIT",
5
5
  "main": "./lib/cjs/index.js",
6
6
  "module": "./lib/esm/index.js",
@@ -10,16 +10,6 @@
10
10
  "access": "public",
11
11
  "registry": "https://registry.npmjs.org/"
12
12
  },
13
- "scripts": {
14
- "build": "rm -rf lib && pnpm build:esm && pnpm build:cjs",
15
- "build:esm": "tsc",
16
- "build:cjs": "tsc --module commonjs --outDir lib/cjs",
17
- "dev": "tsc -w",
18
- "codecov": "codecov",
19
- "gendoc": "typedoc",
20
- "test": "jest",
21
- "release": "pnpm build && pnpm version patch && yarn publish"
22
- },
23
13
  "files": [
24
14
  "/lib"
25
15
  ],
@@ -91,10 +81,21 @@
91
81
  "glob-parent": "6.0.1"
92
82
  },
93
83
  "dependencies": {
94
- "@ebay/nice-dag-core": "workspace:^"
84
+ "@ebay/nice-dag-core": "^1.0.2"
95
85
  },
96
86
  "peerDependencies": {
97
87
  "react": ">16.8.0",
98
88
  "react-dom": ">16.8.0"
99
- }
100
- }
89
+ },
90
+ "scripts": {
91
+ "build": "rm -rf lib && pnpm build:esm && pnpm build:cjs",
92
+ "build:esm": "tsc",
93
+ "build:cjs": "tsc --module commonjs --outDir lib/cjs",
94
+ "dev": "tsc -w",
95
+ "codecov": "codecov",
96
+ "gendoc": "typedoc",
97
+ "test": "jest",
98
+ "release": "pnpm build && pnpm version patch && yarn publish"
99
+ },
100
+ "readme": "# nice-dag-react\n\n## What is nice-dag-react?\n\n`nice-dag-react` is a wrapper of DOM operations given by `nice-dag-core`. In terms of React applications, you can simply use this customized react hook which is given by `nice-dag-react` module. \n\nFor more details, please you can visit [Doc Portal](https://opensource.ebay.com/nice-dag/docs/api-ref/useNiceDag)."
101
+ }