@neo4j-nvl/react 1.0.0-0e1dd35b-alpha → 1.0.0-0f60498a

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/CHANGELOG.md CHANGED
@@ -2,6 +2,31 @@
2
2
 
3
3
  All notable changes to NVL will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
4
4
 
5
+ ## [1.1.0] - 2026-02-16
6
+
7
+ This 1.1.0 release introduces new rendering capabilities with the addition of a new SVG export method, alongside a new Circular Layout algorithm and touchpad gestures for zooming. It also includes performance improvements for the renderer and physics engine, as well as various fixes for the React wrappers and Minimap.
8
+
9
+ ### Added
10
+
11
+ - new SVG export functionality.
12
+ - new circular layout option.
13
+ - two-finger pinch gesture to zoom with touchpads.
14
+ - onCanvasDoubleClick callback to the interactive NVL React wrapper.
15
+
16
+ ### Changed
17
+
18
+ - improved minimap handling for React components.
19
+ - rendering performance improvements.
20
+ - physics layout performance and gravity improvements.
21
+
22
+ ### Fixed
23
+
24
+ - seed radius calculation in D3 layout for large graphs.
25
+ - forceDirected layout unnecessarily reheating on switch without changes.
26
+ - ensure drag operations are always correctly ended on mouse up.
27
+ - minimap positioning issues.
28
+ - background color for the 'os' theme in api documentation.
29
+
5
30
  ## [1.0.0] - 2025-09-30
6
31
  This 1.0.0 release is our first major release, with NVL's release strategy fully adopting semantic versioning going forward. Updates in this release include React 19 support for the React wrappers, a UI overhaul of the [examples app](https://neo4j.com/docs/api/nvl/current/examples.html).
7
32
 
@@ -0,0 +1 @@
1
+ import '@testing-library/jest-dom';
@@ -0,0 +1,69 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import NVL, { HierarchicalLayoutType } from '@neo4j-nvl/base';
3
+ import '@testing-library/jest-dom';
4
+ import { render } from '@testing-library/react';
5
+ import React from 'react';
6
+ import { BasicNvlWrapper } from '../basic-wrapper/BasicNvlWrapper';
7
+ jest.mock('@neo4j-nvl/base');
8
+ jest.mock('@neo4j-nvl/layout-workers');
9
+ describe('BasicNvlWrapper', () => {
10
+ afterEach(() => {
11
+ jest.clearAllMocks();
12
+ });
13
+ test('initialises NVL expectedly with an empty graph object and no other properties', () => {
14
+ render(_jsx("div", { children: _jsx(BasicNvlWrapper, { nodes: [], rels: [] }) }));
15
+ expect(NVL).toHaveBeenCalledWith(expect.any(HTMLDivElement), [], [], {}, {});
16
+ });
17
+ test('initialises NVL expectedly with a graph object and properties', () => {
18
+ const mockLayoutDoneFunction = jest.fn();
19
+ render(_jsx("div", { children: _jsx(BasicNvlWrapper, { nodes: [{ id: '1' }, { id: '2' }], rels: [{ id: '12', from: '1', to: '2' }], layout: HierarchicalLayoutType, layoutOptions: { enableCytoscape: true }, nvlOptions: { renderer: 'canvas' }, nvlCallbacks: { onLayoutDone: mockLayoutDoneFunction } }) }));
20
+ expect(NVL).toHaveBeenCalledWith(expect.any(HTMLDivElement), [{ id: '1' }, { id: '2' }], [{ id: '12', from: '1', to: '2' }], { renderer: 'canvas', layout: HierarchicalLayoutType, layoutOptions: { enableCytoscape: true } }, {
21
+ onLayoutDone: mockLayoutDoneFunction
22
+ });
23
+ });
24
+ test('destroys NVL on unmount', () => {
25
+ const destroy = jest.fn();
26
+ jest.spyOn(NVL.prototype, 'destroy').mockImplementation(destroy);
27
+ const { unmount } = render(_jsx("div", { children: _jsx(BasicNvlWrapper, { nodes: [], rels: [] }) }));
28
+ unmount();
29
+ expect(NVL).toHaveBeenCalledTimes(1);
30
+ expect(destroy).toHaveBeenCalledTimes(1);
31
+ });
32
+ test('successfully re-initialises NVL when using React StrictMode', () => {
33
+ const destroy = jest.fn();
34
+ jest.spyOn(NVL.prototype, 'destroy').mockImplementation(destroy);
35
+ render(_jsx(React.StrictMode, { children: _jsx("div", { children: _jsx(BasicNvlWrapper, { nodes: [], rels: [] }) }) }));
36
+ expect(NVL).toHaveBeenCalledTimes(2);
37
+ expect(destroy).toHaveBeenCalledTimes(1);
38
+ });
39
+ test('calls setZoom when zoom property is provided', () => {
40
+ const setZoom = jest.fn();
41
+ jest.spyOn(NVL.prototype, 'setZoom').mockImplementation(setZoom);
42
+ render(_jsx("div", { children: _jsx(BasicNvlWrapper, { nodes: [], rels: [], zoom: 1.5 }) }));
43
+ expect(setZoom).toHaveBeenCalledWith(1.5);
44
+ });
45
+ test('calls setPan when pan property is provided', () => {
46
+ const setPan = jest.fn();
47
+ jest.spyOn(NVL.prototype, 'setPan').mockImplementation(setPan);
48
+ render(_jsx("div", { children: _jsx(BasicNvlWrapper, { nodes: [], rels: [], pan: { x: 10, y: 20 } }) }));
49
+ expect(setPan).toHaveBeenCalledWith(10, 20);
50
+ });
51
+ test('calls setZoomAndPan when both zoom and pan properties are provided', () => {
52
+ const setZoomAndPan = jest.fn();
53
+ jest.spyOn(NVL.prototype, 'setZoomAndPan').mockImplementation(setZoomAndPan);
54
+ render(_jsx("div", { children: _jsx(BasicNvlWrapper, { nodes: [], rels: [], zoom: 1.5, pan: { x: 10, y: 20 } }) }));
55
+ expect(setZoomAndPan).toHaveBeenCalledWith(1.5, 10, 20);
56
+ });
57
+ test('does not call zoom/pan methods when properties are undefined', () => {
58
+ const setZoom = jest.fn();
59
+ jest.spyOn(NVL.prototype, 'setZoom').mockImplementation(setZoom);
60
+ const setPan = jest.fn();
61
+ jest.spyOn(NVL.prototype, 'setPan').mockImplementation(setPan);
62
+ const setZoomAndPan = jest.fn();
63
+ jest.spyOn(NVL.prototype, 'setZoomAndPan').mockImplementation(setZoomAndPan);
64
+ render(_jsx("div", { children: _jsx(BasicNvlWrapper, { nodes: [], rels: [] }) }));
65
+ expect(setZoom).not.toHaveBeenCalled();
66
+ expect(setPan).not.toHaveBeenCalled();
67
+ expect(setZoomAndPan).not.toHaveBeenCalled();
68
+ });
69
+ });
@@ -0,0 +1 @@
1
+ import '@testing-library/jest-dom';
@@ -0,0 +1,331 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import NVL, { HierarchicalLayoutType } from '@neo4j-nvl/base';
3
+ import { BoxSelectInteraction, ClickInteraction, DragNodeInteraction, DrawInteraction, HoverInteraction, LassoInteraction, PanInteraction, ZoomInteraction } from '@neo4j-nvl/interaction-handlers';
4
+ import '@testing-library/jest-dom';
5
+ import { render } from '@testing-library/react';
6
+ import React, { act, createRef } from 'react';
7
+ import { InteractiveNvlWrapper } from '../interactive-nvl-wrapper/InteractiveNvlWrapper';
8
+ jest.mock('@neo4j-nvl/base');
9
+ jest.mock('@neo4j-nvl/layout-workers');
10
+ jest.mock('@neo4j-nvl/interaction-handlers');
11
+ const MockedNVL = NVL;
12
+ let mockOnInitialization;
13
+ HoverInteraction.prototype.callbackMap = new Map();
14
+ HoverInteraction.prototype.updateCallback = jest.fn();
15
+ HoverInteraction.prototype.removeCallback = jest.fn();
16
+ HoverInteraction.prototype.destroy = jest.fn();
17
+ PanInteraction.prototype.callbackMap = new Map();
18
+ PanInteraction.prototype.updateCallback = jest.fn();
19
+ PanInteraction.prototype.removeCallback = jest.fn();
20
+ PanInteraction.prototype.destroy = jest.fn();
21
+ ClickInteraction.prototype.callbackMap = new Map();
22
+ ClickInteraction.prototype.updateCallback = jest.fn();
23
+ ClickInteraction.prototype.removeCallback = jest.fn();
24
+ ClickInteraction.prototype.destroy = jest.fn();
25
+ ZoomInteraction.prototype.callbackMap = new Map();
26
+ ZoomInteraction.prototype.updateCallback = jest.fn();
27
+ ZoomInteraction.prototype.removeCallback = jest.fn();
28
+ ZoomInteraction.prototype.destroy = jest.fn();
29
+ DragNodeInteraction.prototype.callbackMap = new Map();
30
+ DragNodeInteraction.prototype.updateCallback = jest.fn();
31
+ DragNodeInteraction.prototype.removeCallback = jest.fn();
32
+ DragNodeInteraction.prototype.destroy = jest.fn();
33
+ DrawInteraction.prototype.callbackMap = new Map();
34
+ DrawInteraction.prototype.updateCallback = jest.fn();
35
+ DrawInteraction.prototype.removeCallback = jest.fn();
36
+ DrawInteraction.prototype.destroy = jest.fn();
37
+ BoxSelectInteraction.prototype.callbackMap = new Map();
38
+ BoxSelectInteraction.prototype.updateCallback = jest.fn();
39
+ BoxSelectInteraction.prototype.removeCallback = jest.fn();
40
+ BoxSelectInteraction.prototype.destroy = jest.fn();
41
+ LassoInteraction.prototype.callbackMap = new Map();
42
+ LassoInteraction.prototype.updateCallback = jest.fn();
43
+ LassoInteraction.prototype.removeCallback = jest.fn();
44
+ LassoInteraction.prototype.destroy = jest.fn();
45
+ let mockDestroy;
46
+ let destroyHoverInteraction;
47
+ let destroyPanInteraction;
48
+ describe('InteractiveNvlWrapper', () => {
49
+ beforeEach(() => {
50
+ jest.spyOn(NVL.prototype, 'getContainer').mockImplementation(() => document.createElement('div'));
51
+ MockedNVL.mockImplementation((_container, _nodes, _rels, _options, callbacks) => {
52
+ mockOnInitialization = callbacks.onInitialization;
53
+ return {
54
+ getContainer: () => document.createElement('div'),
55
+ destroy: mockDestroy,
56
+ setLayout: jest.fn(),
57
+ setLayoutOptions: jest.fn(),
58
+ setRenderer: jest.fn()
59
+ };
60
+ });
61
+ mockDestroy = jest.fn();
62
+ destroyHoverInteraction = jest.fn();
63
+ destroyPanInteraction = jest.fn();
64
+ jest.spyOn(NVL.prototype, 'destroy').mockImplementation(mockDestroy);
65
+ jest.spyOn(HoverInteraction.prototype, 'destroy').mockImplementation(destroyHoverInteraction);
66
+ jest.spyOn(PanInteraction.prototype, 'destroy').mockImplementation(destroyPanInteraction);
67
+ jest.spyOn(ClickInteraction.prototype, 'destroy').mockImplementation(jest.fn());
68
+ jest.spyOn(ZoomInteraction.prototype, 'destroy').mockImplementation(jest.fn());
69
+ jest.spyOn(DragNodeInteraction.prototype, 'destroy').mockImplementation(jest.fn());
70
+ jest.spyOn(DrawInteraction.prototype, 'destroy').mockImplementation(jest.fn());
71
+ jest.spyOn(BoxSelectInteraction.prototype, 'destroy').mockImplementation(jest.fn());
72
+ jest.spyOn(LassoInteraction.prototype, 'destroy').mockImplementation(jest.fn());
73
+ });
74
+ afterEach(() => {
75
+ jest.clearAllMocks();
76
+ });
77
+ test('initialises NVL expectedly with an empty graph object and no other properties', () => {
78
+ render(_jsx("div", { children: _jsx(InteractiveNvlWrapper, { nodes: [], rels: [] }) }));
79
+ expect(NVL).toHaveBeenCalledWith(expect.any(HTMLDivElement), [], [], {}, { onInitialization: mockOnInitialization });
80
+ });
81
+ test('initialises NVL expectedly with a graph object and properties', () => {
82
+ const mockLayoutDoneFunction = jest.fn();
83
+ render(_jsx(InteractiveNvlWrapper, { nodes: [{ id: '1' }, { id: '2' }], rels: [{ id: '12', from: '1', to: '2' }], layout: HierarchicalLayoutType, layoutOptions: { enableCytoscape: true }, nvlOptions: { renderer: 'canvas' }, nvlCallbacks: { onLayoutDone: mockLayoutDoneFunction } }));
84
+ act(() => {
85
+ mockOnInitialization?.();
86
+ });
87
+ expect(NVL).toHaveBeenCalledWith(expect.any(HTMLDivElement), [{ id: '1' }, { id: '2' }], [{ id: '12', from: '1', to: '2' }], { renderer: 'canvas', layout: HierarchicalLayoutType, layoutOptions: { enableCytoscape: true } }, {
88
+ onLayoutDone: mockLayoutDoneFunction,
89
+ onInitialization: mockOnInitialization
90
+ });
91
+ });
92
+ test('initialises expected interaction handlers with correct options', () => {
93
+ const myNvlRef = createRef();
94
+ render(_jsx(InteractiveNvlWrapper, { nodes: [], rels: [], ref: myNvlRef, mouseEventCallbacks: {
95
+ onHover: true,
96
+ onPan: true
97
+ }, interactionOptions: {
98
+ drawShadowOnHover: true
99
+ } }));
100
+ act(() => {
101
+ mockOnInitialization?.();
102
+ });
103
+ expect(HoverInteraction).toHaveBeenCalledWith(myNvlRef.current, { drawShadowOnHover: true });
104
+ expect(PanInteraction).toHaveBeenCalledWith(myNvlRef.current, { drawShadowOnHover: true });
105
+ expect(HoverInteraction).toHaveBeenCalledTimes(1);
106
+ expect(PanInteraction).toHaveBeenCalledTimes(1);
107
+ expect(ClickInteraction).not.toHaveBeenCalled();
108
+ expect(DragNodeInteraction).not.toHaveBeenCalled();
109
+ expect(DrawInteraction).not.toHaveBeenCalled();
110
+ expect(ZoomInteraction).not.toHaveBeenCalled();
111
+ expect(BoxSelectInteraction).not.toHaveBeenCalled();
112
+ expect(LassoInteraction).not.toHaveBeenCalled();
113
+ });
114
+ test.each([
115
+ {
116
+ name: 'HoverInteraction',
117
+ InteractionClass: HoverInteraction,
118
+ callback: { onHover: jest.fn() },
119
+ options: { drawShadowOnHover: true }
120
+ },
121
+ {
122
+ name: 'ClickInteraction with onCanvasClick',
123
+ InteractionClass: ClickInteraction,
124
+ callback: { onCanvasClick: jest.fn() },
125
+ options: { selectOnClick: false }
126
+ },
127
+ {
128
+ name: 'ClickInteraction with onCanvasDoubleClick',
129
+ InteractionClass: ClickInteraction,
130
+ callback: { onCanvasDoubleClick: jest.fn() },
131
+ options: { selectOnClick: false }
132
+ },
133
+ {
134
+ name: 'ClickInteraction with onCanvasRightClick',
135
+ InteractionClass: ClickInteraction,
136
+ callback: { onCanvasRightClick: jest.fn() },
137
+ options: { selectOnClick: false }
138
+ },
139
+ {
140
+ name: 'ClickInteraction with onNodeClick',
141
+ InteractionClass: ClickInteraction,
142
+ callback: { onNodeClick: jest.fn() },
143
+ options: { selectOnClick: true }
144
+ },
145
+ {
146
+ name: 'ClickInteraction with onNodeDoubleClick',
147
+ InteractionClass: ClickInteraction,
148
+ callback: { onNodeDoubleClick: jest.fn() },
149
+ options: { selectOnClick: false }
150
+ },
151
+ {
152
+ name: 'ClickInteraction with onNodeRightClick',
153
+ InteractionClass: ClickInteraction,
154
+ callback: { onNodeRightClick: jest.fn() },
155
+ options: { selectOnClick: false }
156
+ },
157
+ {
158
+ name: 'ClickInteraction with onRelationshipClick',
159
+ InteractionClass: ClickInteraction,
160
+ callback: { onRelationshipClick: jest.fn() },
161
+ options: { selectOnClick: false }
162
+ },
163
+ {
164
+ name: 'ClickInteraction with onRelationshipDoubleClick',
165
+ InteractionClass: ClickInteraction,
166
+ callback: { onRelationshipDoubleClick: jest.fn() },
167
+ options: { selectOnClick: false }
168
+ },
169
+ {
170
+ name: 'ClickInteraction with onRelationshipRightClick',
171
+ InteractionClass: ClickInteraction,
172
+ callback: { onRelationshipRightClick: jest.fn() },
173
+ options: { selectOnClick: false }
174
+ },
175
+ {
176
+ name: 'PanInteraction',
177
+ InteractionClass: PanInteraction,
178
+ callback: { onPan: jest.fn() },
179
+ options: {}
180
+ },
181
+ {
182
+ name: 'ZoomInteraction',
183
+ InteractionClass: ZoomInteraction,
184
+ callback: { onZoom: jest.fn() },
185
+ options: {}
186
+ },
187
+ {
188
+ name: 'DragNodeInteraction with onDrag',
189
+ InteractionClass: DragNodeInteraction,
190
+ callback: { onDrag: jest.fn() },
191
+ options: { selectOnRelease: false }
192
+ },
193
+ {
194
+ name: 'DragNodeInteraction with onDragStart',
195
+ InteractionClass: DragNodeInteraction,
196
+ callback: { onDragStart: jest.fn() },
197
+ options: {}
198
+ },
199
+ {
200
+ name: 'DragNodeInteraction with onDragEnd',
201
+ InteractionClass: DragNodeInteraction,
202
+ callback: { onDragEnd: jest.fn() },
203
+ options: {}
204
+ },
205
+ {
206
+ name: 'DrawInteraction with onHoverNodeMargin',
207
+ InteractionClass: DrawInteraction,
208
+ callback: { onHoverNodeMargin: jest.fn() },
209
+ options: { excludeNodeMargin: false }
210
+ },
211
+ {
212
+ name: 'DrawInteraction with onDrawStarted',
213
+ InteractionClass: DrawInteraction,
214
+ callback: { onDrawStarted: jest.fn() },
215
+ options: {}
216
+ },
217
+ {
218
+ name: 'DrawInteraction with onDrawEnded',
219
+ InteractionClass: DrawInteraction,
220
+ callback: { onDrawEnded: jest.fn() },
221
+ options: {}
222
+ },
223
+ {
224
+ name: 'BoxSelectInteraction with onBoxStarted',
225
+ InteractionClass: BoxSelectInteraction,
226
+ callback: { onBoxStarted: jest.fn() },
227
+ options: {}
228
+ },
229
+ {
230
+ name: 'BoxSelectInteraction with onBoxSelect',
231
+ InteractionClass: BoxSelectInteraction,
232
+ callback: { onBoxSelect: jest.fn() },
233
+ options: {}
234
+ },
235
+ {
236
+ name: 'LassoInteraction with onLassoStarted',
237
+ InteractionClass: LassoInteraction,
238
+ callback: { onLassoStarted: jest.fn() },
239
+ options: {}
240
+ },
241
+ {
242
+ name: 'LassoInteraction with onLassoSelect',
243
+ InteractionClass: LassoInteraction,
244
+ callback: { onLassoSelect: jest.fn() },
245
+ options: {}
246
+ }
247
+ ])('initialises $name when its callback is provided', ({ InteractionClass, callback, options }) => {
248
+ const myNvlRef = createRef();
249
+ render(_jsx(InteractiveNvlWrapper, { nodes: [], rels: [], ref: myNvlRef, mouseEventCallbacks: callback, interactionOptions: options }));
250
+ act(() => {
251
+ mockOnInitialization?.();
252
+ });
253
+ expect(InteractionClass).toHaveBeenCalledWith(myNvlRef.current, options);
254
+ expect(InteractionClass).toHaveBeenCalledTimes(1);
255
+ });
256
+ test('does not attempt to initialize interaction handlers if NVL container is not available', () => {
257
+ const myNvlRef = createRef();
258
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
259
+ // @ts-expect-error
260
+ jest.spyOn(NVL.prototype, 'getContainer').mockImplementation(() => null);
261
+ render(_jsx(InteractiveNvlWrapper, { nodes: [], rels: [], ref: myNvlRef, mouseEventCallbacks: {
262
+ onHover: true
263
+ }, interactionOptions: {
264
+ drawShadowOnHover: true
265
+ } }));
266
+ expect(HoverInteraction).not.toHaveBeenCalled();
267
+ expect(NVL).toHaveBeenCalledTimes(1);
268
+ });
269
+ test('destroys NVL and active interaction handlers on unmount', () => {
270
+ const { unmount } = render(_jsx(InteractiveNvlWrapper, { nodes: [], rels: [], mouseEventCallbacks: {
271
+ onHover: true
272
+ } }));
273
+ act(() => {
274
+ mockOnInitialization?.();
275
+ });
276
+ unmount();
277
+ expect(mockDestroy).toHaveBeenCalledTimes(1);
278
+ expect(destroyHoverInteraction).toHaveBeenCalledTimes(1);
279
+ expect(destroyPanInteraction).not.toHaveBeenCalled();
280
+ });
281
+ test('successfully re-initialises NVL and active interaction handlers when using React StrictMode', () => {
282
+ const myNvlRef = createRef();
283
+ render(_jsx(React.StrictMode, { children: _jsx(InteractiveNvlWrapper, { nodes: [], rels: [], ref: myNvlRef, mouseEventCallbacks: {
284
+ onHover: true
285
+ }, interactionOptions: {
286
+ drawShadowOnHover: true
287
+ } }) }));
288
+ act(() => {
289
+ mockOnInitialization?.();
290
+ });
291
+ expect(NVL).toHaveBeenCalledTimes(2);
292
+ expect(HoverInteraction).toHaveBeenCalledTimes(2);
293
+ expect(HoverInteraction).toHaveBeenCalledWith(myNvlRef.current, { drawShadowOnHover: true });
294
+ expect(mockDestroy).toHaveBeenCalledTimes(1);
295
+ expect(destroyHoverInteraction).toHaveBeenCalledTimes(1);
296
+ expect(destroyPanInteraction).not.toHaveBeenCalled();
297
+ expect(DragNodeInteraction).not.toHaveBeenCalled();
298
+ expect(DrawInteraction).not.toHaveBeenCalled();
299
+ expect(ZoomInteraction).not.toHaveBeenCalled();
300
+ expect(BoxSelectInteraction).not.toHaveBeenCalled();
301
+ expect(LassoInteraction).not.toHaveBeenCalled();
302
+ });
303
+ test('also calls custom callbacks on initialization', () => {
304
+ const customOnInitializationCallback = jest.fn();
305
+ render(_jsx(InteractiveNvlWrapper, { nodes: [], rels: [], nvlCallbacks: { onInitialization: customOnInitializationCallback } }));
306
+ act(() => {
307
+ mockOnInitialization?.();
308
+ });
309
+ expect(customOnInitializationCallback).toHaveBeenCalledTimes(1);
310
+ });
311
+ test('passes zoom and pan properties to BasicNvlWrapper', () => {
312
+ const setZoom = jest.fn();
313
+ const setPan = jest.fn();
314
+ const setZoomAndPan = jest.fn();
315
+ MockedNVL.mockImplementation(() => ({
316
+ getContainer: () => document.createElement('div'),
317
+ destroy: jest.fn(),
318
+ setLayout: jest.fn(),
319
+ setLayoutOptions: jest.fn(),
320
+ setNodePositions: jest.fn(),
321
+ setZoom,
322
+ setPan,
323
+ setZoomAndPan
324
+ }));
325
+ render(_jsx(InteractiveNvlWrapper, { nodes: [], rels: [], zoom: 1.5, pan: { x: 10, y: 20 } }));
326
+ act(() => {
327
+ mockOnInitialization?.();
328
+ });
329
+ expect(setZoomAndPan).toHaveBeenCalledWith(1.5, 10, 20);
330
+ });
331
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ import * as exportedModules from '../index';
2
+ jest.mock('@neo4j-nvl/layout-workers');
3
+ describe('index', () => {
4
+ test('provides the expected components', () => {
5
+ const modules = Object.keys(exportedModules);
6
+ expect(modules).toContain('BasicNvlWrapper');
7
+ });
8
+ });
@@ -0,0 +1,48 @@
1
+ import type { ExternalCallbacks, Layout, LayoutOptions, Node, NvlOptions, Relationship } from '@neo4j-nvl/base';
2
+ import NVL from '@neo4j-nvl/base';
3
+ import { type HTMLProps } from 'react';
4
+ /**
5
+ * The properties that can be passed to the {@link BasicNvlWrapper} component.
6
+ */
7
+ export interface BasicReactWrapperProps {
8
+ /** The nodes of the graph of type Node[] */
9
+ nodes: Node[];
10
+ /** The rels of the graph of type Relationship[] */
11
+ rels: Relationship[];
12
+ /** The layout, can be 'forceDirected' or 'hierarchical' */
13
+ layout?: Layout;
14
+ /** Options for the current layout */
15
+ layoutOptions?: LayoutOptions;
16
+ /** an Object containing functions for callbacks on certain actions */
17
+ nvlCallbacks?: ExternalCallbacks;
18
+ /** An object containing options for the Nvl instance */
19
+ nvlOptions?: NvlOptions;
20
+ /** Sets the positions of the nodes in the graph using the setNodePositions method. */
21
+ positions?: Node[];
22
+ /**
23
+ * Sets the zoom level of the viewport using the setZoom method.
24
+ * @remarks If both zoom and pan are provided, the setZoomAndPan method will be used.
25
+ */
26
+ zoom?: number;
27
+ /**
28
+ * Sets the pan coordinates of the viewport using the setPan method.
29
+ * @remarks If both zoom and pan are provided, the setZoomAndPan method will be used.
30
+ */
31
+ pan?: {
32
+ /** The x coordinate of the pan. */
33
+ x: number;
34
+ /** The y coordinate of the pan. */
35
+ y: number;
36
+ };
37
+ /** A callback to handle any errors that happen during NVL initialization */
38
+ onInitializationError?: (error: unknown) => void;
39
+ }
40
+ /**
41
+ *
42
+ * A basic React wrapper that wraps the NVL base library within a React component.
43
+ * It takes the class' arguments as properties, which are passed to the NVL constructor.
44
+ * Any changes in properties will be reflected in the NVL instance by calling the corresponding methods.
45
+ *
46
+ * For examples, head to the {@link https://neo4j.com/docs/nvl/current/react-wrappers/#_basic_react_wrapper Basic React wrapper documentation page}.
47
+ */
48
+ export declare const BasicNvlWrapper: import("react").MemoExoticComponent<import("react").ForwardRefExoticComponent<Omit<BasicReactWrapperProps & HTMLProps<HTMLDivElement>, "ref"> & import("react").RefAttributes<Partial<Pick<NVL, "restart" | "destroy" | "addAndUpdateElementsInGraph" | "getSelectedNodes" | "getSelectedRelationships" | "removeNodesWithIds" | "removeRelationshipsWithIds" | "getNodes" | "getRelationships" | "getNodeById" | "getRelationshipById" | "getPositionById" | "getCurrentOptions" | "deselectAll" | "fit" | "resetZoom" | "setRenderer" | "setDisableWebGL" | "pinNode" | "unPinNode" | "setLayout" | "setLayoutOptions" | "getNodesOnScreen" | "getNodePositions" | "setNodePositions" | "isLayoutMoving" | "saveToFile" | "saveToSvg" | "getImageDataUrl" | "saveFullGraphToLargeFile" | "setZoom" | "getScale" | "getPan" | "getHits" | "getContainer">>>>>;
@@ -0,0 +1,141 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import NVL from '@neo4j-nvl/base';
3
+ import { forwardRef, memo, useEffect, useImperativeHandle, useRef, useState } from 'react';
4
+ import { BASIC_WRAPPER_ID } from '../utils/constants';
5
+ import { getMapDifferences, getNodeAttributeDifferences } from '../utils/graph-comparison';
6
+ import { useDeepCompareEffect } from '../utils/hooks';
7
+ /**
8
+ *
9
+ * A basic React wrapper that wraps the NVL base library within a React component.
10
+ * It takes the class' arguments as properties, which are passed to the NVL constructor.
11
+ * Any changes in properties will be reflected in the NVL instance by calling the corresponding methods.
12
+ *
13
+ * For examples, head to the {@link https://neo4j.com/docs/nvl/current/react-wrappers/#_basic_react_wrapper Basic React wrapper documentation page}.
14
+ */
15
+ export const BasicNvlWrapper = memo(forwardRef(({ nodes, rels, layout, layoutOptions, nvlCallbacks = {}, nvlOptions = {}, positions = [], zoom, pan, onInitializationError, ...nvlEvents }, ref) => {
16
+ const nvlRef = useRef(null);
17
+ useImperativeHandle(ref, () => {
18
+ const nvlMethods = Object.getOwnPropertyNames(NVL.prototype);
19
+ return nvlMethods.reduce((current, method) => ({
20
+ ...current,
21
+ [method]: (...args) => {
22
+ if (nvlRef.current === null) {
23
+ return null;
24
+ }
25
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
26
+ return nvlRef.current[method](...args);
27
+ }
28
+ }), {});
29
+ });
30
+ const containerRef = useRef(null);
31
+ const [currentNodes, setCurrentNodes] = useState(nodes);
32
+ const [currentRels, setCurrentRels] = useState(rels);
33
+ useEffect(() => {
34
+ return () => {
35
+ nvlRef.current?.destroy();
36
+ nvlRef.current = null;
37
+ };
38
+ }, []);
39
+ useEffect(() => {
40
+ let newNvl = null;
41
+ const hasMinimap = 'minimapContainer' in nvlOptions;
42
+ const minimapContainerIsReady = hasMinimap ? nvlOptions.minimapContainer !== null : true;
43
+ const mainContainerIsReady = containerRef.current !== null;
44
+ if (mainContainerIsReady && minimapContainerIsReady) {
45
+ if (nvlRef.current === null) {
46
+ const combinedOptions = { ...nvlOptions, layoutOptions };
47
+ if (layout !== undefined) {
48
+ combinedOptions.layout = layout;
49
+ }
50
+ try {
51
+ newNvl = new NVL(containerRef.current, currentNodes, currentRels, combinedOptions, nvlCallbacks);
52
+ nvlRef.current = newNvl;
53
+ setCurrentRels(rels);
54
+ setCurrentNodes(nodes);
55
+ }
56
+ catch (e) {
57
+ if (typeof onInitializationError === 'function') {
58
+ onInitializationError(e);
59
+ }
60
+ else {
61
+ throw e;
62
+ }
63
+ }
64
+ }
65
+ }
66
+ // eslint-disable-next-line react-hooks/exhaustive-deps
67
+ }, [containerRef.current, nvlOptions.minimapContainer]);
68
+ useEffect(() => {
69
+ if (nvlRef.current === null) {
70
+ return;
71
+ }
72
+ const nodeChanges = getMapDifferences(currentNodes, nodes);
73
+ const nodeDiff = getNodeAttributeDifferences(currentNodes, nodes);
74
+ const relChanges = getMapDifferences(currentRels, rels);
75
+ const graphIsUnchanged = nodeChanges.added.length === 0 &&
76
+ nodeChanges.removed.length === 0 &&
77
+ nodeDiff.length === 0 &&
78
+ relChanges.added.length === 0 &&
79
+ relChanges.removed.length === 0 &&
80
+ relChanges.updated.length === 0;
81
+ if (graphIsUnchanged) {
82
+ return;
83
+ }
84
+ setCurrentRels(rels);
85
+ setCurrentNodes(nodes);
86
+ const nodesToAddAndUpdate = [...nodeChanges.added, ...nodeDiff];
87
+ const relsToAddAndUpdate = [...relChanges.added, ...relChanges.updated];
88
+ nvlRef.current.addAndUpdateElementsInGraph(nodesToAddAndUpdate, relsToAddAndUpdate);
89
+ const relationshipsToRemove = relChanges.removed.map((r) => r.id);
90
+ const nodesToRemove = nodeChanges.removed.map((n) => n.id);
91
+ nvlRef.current.removeRelationshipsWithIds(relationshipsToRemove);
92
+ nvlRef.current.removeNodesWithIds(nodesToRemove);
93
+ }, [currentNodes, currentRels, nodes, rels]);
94
+ useEffect(() => {
95
+ const updatedLayout = layout ?? nvlOptions.layout;
96
+ if (nvlRef.current === null || updatedLayout === undefined) {
97
+ return;
98
+ }
99
+ nvlRef.current.setLayout(updatedLayout);
100
+ }, [layout, nvlOptions.layout]);
101
+ useDeepCompareEffect(() => {
102
+ const updatedLayoutOptions = layoutOptions ?? nvlOptions?.layoutOptions;
103
+ if (nvlRef.current === null || updatedLayoutOptions === undefined) {
104
+ return;
105
+ }
106
+ nvlRef.current.setLayoutOptions(updatedLayoutOptions);
107
+ }, [layoutOptions, nvlOptions.layoutOptions]);
108
+ useEffect(() => {
109
+ if (nvlRef.current === null || nvlOptions.renderer === undefined) {
110
+ return;
111
+ }
112
+ nvlRef.current.setRenderer(nvlOptions.renderer);
113
+ }, [nvlOptions.renderer]);
114
+ useEffect(() => {
115
+ if (nvlRef.current === null || nvlOptions.disableWebGL === undefined) {
116
+ return;
117
+ }
118
+ nvlRef.current.setDisableWebGL(nvlOptions.disableWebGL);
119
+ }, [nvlOptions.disableWebGL]);
120
+ useEffect(() => {
121
+ if (nvlRef.current === null || positions.length === 0) {
122
+ return;
123
+ }
124
+ nvlRef.current.setNodePositions(positions);
125
+ }, [positions]);
126
+ useEffect(() => {
127
+ if (nvlRef.current === null) {
128
+ return;
129
+ }
130
+ if (zoom !== undefined && pan !== undefined) {
131
+ nvlRef.current.setZoomAndPan(zoom, pan.x, pan.y);
132
+ }
133
+ else if (zoom !== undefined) {
134
+ nvlRef.current.setZoom(zoom);
135
+ }
136
+ else if (pan !== undefined) {
137
+ nvlRef.current.setPan(pan.x, pan.y);
138
+ }
139
+ }, [zoom, pan]);
140
+ return _jsx("div", { id: BASIC_WRAPPER_ID, ref: containerRef, style: { height: '100%', outline: '0' }, ...nvlEvents });
141
+ }));
package/lib/index.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { BasicNvlWrapper } from './basic-wrapper/BasicNvlWrapper';
2
+ import type { BasicReactWrapperProps } from './basic-wrapper/BasicNvlWrapper';
3
+ import { InteractiveNvlWrapper } from './interactive-nvl-wrapper/InteractiveNvlWrapper';
4
+ import type { InteractionOptions, InteractiveNvlWrapperProps, MouseEvent, MouseEventCallbacks } from './interactive-nvl-wrapper/types';
5
+ import type { StaticPictureWrapperProps } from './static-picture-wrapper/StaticPictureWrapper';
6
+ import { StaticPictureWrapper } from './static-picture-wrapper/StaticPictureWrapper';
7
+ export { BasicNvlWrapper, InteractiveNvlWrapper, StaticPictureWrapper };
8
+ export type { MouseEventCallbacks, MouseEvent, BasicReactWrapperProps, InteractionOptions, InteractiveNvlWrapperProps, StaticPictureWrapperProps };
package/lib/index.js ADDED
@@ -0,0 +1,4 @@
1
+ import { BasicNvlWrapper } from './basic-wrapper/BasicNvlWrapper';
2
+ import { InteractiveNvlWrapper } from './interactive-nvl-wrapper/InteractiveNvlWrapper';
3
+ import { StaticPictureWrapper } from './static-picture-wrapper/StaticPictureWrapper';
4
+ export { BasicNvlWrapper, InteractiveNvlWrapper, StaticPictureWrapper };
@@ -0,0 +1,10 @@
1
+ import type NVL from '@neo4j-nvl/base';
2
+ import type { MutableRefObject } from 'react';
3
+ import type { InteractionOptions, MouseEventCallbacks } from './types';
4
+ interface InteractionHandlersProps {
5
+ nvlRef: MutableRefObject<NVL | null>;
6
+ mouseEventCallbacks: MouseEventCallbacks;
7
+ interactionOptions: InteractionOptions;
8
+ }
9
+ export declare const InteractionHandlers: React.FC<InteractionHandlersProps>;
10
+ export {};
@@ -0,0 +1,46 @@
1
+ import { BoxSelectInteraction, ClickInteraction, DragNodeInteraction, DrawInteraction, HoverInteraction, LassoInteraction, PanInteraction, ZoomInteraction } from '@neo4j-nvl/interaction-handlers';
2
+ import { useEffect, useRef } from 'react';
3
+ import { destroyInteraction, useInteraction } from './hooks';
4
+ export const InteractionHandlers = ({ nvlRef, mouseEventCallbacks, interactionOptions }) => {
5
+ const hoverInteraction = useRef(null);
6
+ const clickInteraction = useRef(null);
7
+ const panInteraction = useRef(null);
8
+ const zoomInteraction = useRef(null);
9
+ const dragNodeInteraction = useRef(null);
10
+ const drawInteraction = useRef(null);
11
+ const multiSelectInteraction = useRef(null);
12
+ const lassoInteraction = useRef(null);
13
+ useInteraction(HoverInteraction, hoverInteraction, mouseEventCallbacks.onHover, 'onHover', nvlRef, interactionOptions);
14
+ useInteraction(ClickInteraction, clickInteraction, mouseEventCallbacks.onNodeClick, 'onNodeClick', nvlRef, interactionOptions);
15
+ useInteraction(ClickInteraction, clickInteraction, mouseEventCallbacks.onNodeDoubleClick, 'onNodeDoubleClick', nvlRef, interactionOptions);
16
+ useInteraction(ClickInteraction, clickInteraction, mouseEventCallbacks.onNodeRightClick, 'onNodeRightClick', nvlRef, interactionOptions);
17
+ useInteraction(ClickInteraction, clickInteraction, mouseEventCallbacks.onRelationshipClick, 'onRelationshipClick', nvlRef, interactionOptions);
18
+ useInteraction(ClickInteraction, clickInteraction, mouseEventCallbacks.onRelationshipDoubleClick, 'onRelationshipDoubleClick', nvlRef, interactionOptions);
19
+ useInteraction(ClickInteraction, clickInteraction, mouseEventCallbacks.onRelationshipRightClick, 'onRelationshipRightClick', nvlRef, interactionOptions);
20
+ useInteraction(ClickInteraction, clickInteraction, mouseEventCallbacks.onCanvasClick, 'onCanvasClick', nvlRef, interactionOptions);
21
+ useInteraction(ClickInteraction, clickInteraction, mouseEventCallbacks.onCanvasDoubleClick, 'onCanvasDoubleClick', nvlRef, interactionOptions);
22
+ useInteraction(ClickInteraction, clickInteraction, mouseEventCallbacks.onCanvasRightClick, 'onCanvasRightClick', nvlRef, interactionOptions);
23
+ useInteraction(PanInteraction, panInteraction, mouseEventCallbacks.onPan, 'onPan', nvlRef, interactionOptions);
24
+ useInteraction(ZoomInteraction, zoomInteraction, mouseEventCallbacks.onZoom, 'onZoom', nvlRef, interactionOptions);
25
+ useInteraction(DragNodeInteraction, dragNodeInteraction, mouseEventCallbacks.onDrag, 'onDrag', nvlRef, interactionOptions);
26
+ useInteraction(DragNodeInteraction, dragNodeInteraction, mouseEventCallbacks.onDragStart, 'onDragStart', nvlRef, interactionOptions);
27
+ useInteraction(DragNodeInteraction, dragNodeInteraction, mouseEventCallbacks.onDragEnd, 'onDragEnd', nvlRef, interactionOptions);
28
+ useInteraction(DrawInteraction, drawInteraction, mouseEventCallbacks.onHoverNodeMargin, 'onHoverNodeMargin', nvlRef, interactionOptions);
29
+ useInteraction(DrawInteraction, drawInteraction, mouseEventCallbacks.onDrawStarted, 'onDrawStarted', nvlRef, interactionOptions);
30
+ useInteraction(DrawInteraction, drawInteraction, mouseEventCallbacks.onDrawEnded, 'onDrawEnded', nvlRef, interactionOptions);
31
+ useInteraction(BoxSelectInteraction, multiSelectInteraction, mouseEventCallbacks.onBoxStarted, 'onBoxStarted', nvlRef, interactionOptions);
32
+ useInteraction(BoxSelectInteraction, multiSelectInteraction, mouseEventCallbacks.onBoxSelect, 'onBoxSelect', nvlRef, interactionOptions);
33
+ useInteraction(LassoInteraction, lassoInteraction, mouseEventCallbacks.onLassoStarted, 'onLassoStarted', nvlRef, interactionOptions);
34
+ useInteraction(LassoInteraction, lassoInteraction, mouseEventCallbacks.onLassoSelect, 'onLassoSelect', nvlRef, interactionOptions);
35
+ useEffect(() => () => {
36
+ destroyInteraction(hoverInteraction);
37
+ destroyInteraction(clickInteraction);
38
+ destroyInteraction(panInteraction);
39
+ destroyInteraction(zoomInteraction);
40
+ destroyInteraction(dragNodeInteraction);
41
+ destroyInteraction(drawInteraction);
42
+ destroyInteraction(multiSelectInteraction);
43
+ destroyInteraction(lassoInteraction);
44
+ }, []);
45
+ return null;
46
+ };
@@ -0,0 +1,16 @@
1
+ import type NVL from '@neo4j-nvl/base';
2
+ import type { HTMLProps } from 'react';
3
+ import React from 'react';
4
+ import type { InteractiveNvlWrapperProps } from './types';
5
+ /**
6
+ * The interactive React wrapper component contains a collection of interaction handlers by default
7
+ * and provides a variety of common functionality and callbacks.
8
+ * It is an extension of the {@link BasicNvlWrapper} component and incorporates the
9
+ * @neo4j-nvl/interaction-handlers module's decorators to provide a set of interaction events.
10
+ *
11
+ * The mouseEventCallbacks property takes an object where various callbacks can be defined
12
+ * and behavior can be toggled on and off.
13
+ *
14
+ * For examples, head to the {@link https://neo4j.com/docs/nvl/current/react-wrappers/#_interactive_reactive_wrapperr Interactive React wrapper documentation page}.
15
+ */
16
+ export declare const InteractiveNvlWrapper: React.MemoExoticComponent<React.ForwardRefExoticComponent<Omit<InteractiveNvlWrapperProps & HTMLProps<HTMLDivElement>, "ref"> & React.RefAttributes<NVL>>>;
@@ -0,0 +1,46 @@
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { forwardRef, memo, useCallback, useRef, useState } from 'react';
3
+ import { BasicNvlWrapper } from '../basic-wrapper/BasicNvlWrapper';
4
+ import { INTERACTIVE_WRAPPER_ID } from '../utils/constants';
5
+ import { InteractionHandlers } from './InteractionHandlers';
6
+ const options = {
7
+ selectOnClick: false,
8
+ drawShadowOnHover: true,
9
+ selectOnRelease: false,
10
+ excludeNodeMargin: true
11
+ };
12
+ /**
13
+ * The interactive React wrapper component contains a collection of interaction handlers by default
14
+ * and provides a variety of common functionality and callbacks.
15
+ * It is an extension of the {@link BasicNvlWrapper} component and incorporates the
16
+ * @neo4j-nvl/interaction-handlers module's decorators to provide a set of interaction events.
17
+ *
18
+ * The mouseEventCallbacks property takes an object where various callbacks can be defined
19
+ * and behavior can be toggled on and off.
20
+ *
21
+ * For examples, head to the {@link https://neo4j.com/docs/nvl/current/react-wrappers/#_interactive_reactive_wrapperr Interactive React wrapper documentation page}.
22
+ */
23
+ export const InteractiveNvlWrapper = memo(forwardRef(({ nodes, rels, layout, layoutOptions, onInitializationError, mouseEventCallbacks = {}, nvlCallbacks = {}, nvlOptions = {}, interactionOptions = options, ...nvlEvents }, nvlRef) => {
24
+ const newNvlRef = useRef(null);
25
+ const myNvlRef = nvlRef ?? newNvlRef;
26
+ const [isNvlInitialized, setIsNvlInitialized] = useState(false);
27
+ const handleInitialization = useCallback(() => {
28
+ setIsNvlInitialized(true);
29
+ }, []);
30
+ const handleInitializationError = useCallback((error) => {
31
+ setIsNvlInitialized(false);
32
+ if (onInitializationError) {
33
+ onInitializationError(error);
34
+ }
35
+ }, [onInitializationError]);
36
+ const setupInteractions = isNvlInitialized && myNvlRef.current !== null;
37
+ return (_jsxs(_Fragment, { children: [_jsx(BasicNvlWrapper, { ref: myNvlRef, nodes: nodes, id: INTERACTIVE_WRAPPER_ID, rels: rels, nvlOptions: nvlOptions, nvlCallbacks: {
38
+ ...nvlCallbacks,
39
+ onInitialization: () => {
40
+ if (nvlCallbacks.onInitialization !== undefined) {
41
+ nvlCallbacks.onInitialization();
42
+ }
43
+ handleInitialization();
44
+ }
45
+ }, layout: layout, layoutOptions: layoutOptions, onInitializationError: handleInitializationError, ...nvlEvents }), setupInteractions && (_jsx(InteractionHandlers, { nvlRef: myNvlRef, mouseEventCallbacks: mouseEventCallbacks, interactionOptions: interactionOptions }))] }));
46
+ }));
@@ -0,0 +1,5 @@
1
+ import type NVL from '@neo4j-nvl/base';
2
+ import type { MutableRefObject } from 'react';
3
+ import type { InteractionOptions, MouseEvent, MouseInteraction, MouseInteractionModule } from './types';
4
+ export declare const destroyInteraction: (interactionRef: MutableRefObject<MouseInteraction | null>) => void;
5
+ export declare const useInteraction: (Interaction: MouseInteractionModule, interactionRef: MutableRefObject<MouseInteraction | null>, callback: ((...args: unknown[]) => void) | boolean | undefined, eventName: MouseEvent, nvlRef: MutableRefObject<NVL | null>, interactionOptions: InteractionOptions) => void;
@@ -0,0 +1,29 @@
1
+ import { isNil } from 'lodash';
2
+ import { useEffect } from 'react';
3
+ export const destroyInteraction = (interactionRef) => {
4
+ interactionRef.current?.destroy();
5
+ interactionRef.current = null;
6
+ };
7
+ export const useInteraction = (Interaction, interactionRef, callback, eventName, nvlRef, interactionOptions) => {
8
+ useEffect(() => {
9
+ const nvl = nvlRef.current;
10
+ // Make sure we are not mounting interaction handlers when there is NVL setup
11
+ if (isNil(nvl) || isNil(nvl.getContainer())) {
12
+ return;
13
+ }
14
+ if (callback === true || typeof callback === 'function') {
15
+ if (isNil(interactionRef.current)) {
16
+ interactionRef.current = new Interaction(nvl, interactionOptions);
17
+ }
18
+ if (typeof callback === 'function') {
19
+ interactionRef.current.updateCallback(eventName, callback);
20
+ }
21
+ else if (!isNil(interactionRef.current.callbackMap[eventName])) {
22
+ interactionRef.current.removeCallback(eventName);
23
+ }
24
+ }
25
+ else if (callback === false) {
26
+ destroyInteraction(interactionRef);
27
+ }
28
+ }, [Interaction, callback, eventName, interactionOptions, interactionRef, nvlRef]);
29
+ };
@@ -0,0 +1,28 @@
1
+ import type { BoxSelectInteraction, BoxSelectInteractionCallbacks, BoxSelectInteractionOptions, ClickInteraction, ClickInteractionCallbacks, ClickInteractionOptions, DragNodeInteraction, DragNodeInteractionCallbacks, DrawInteraction, DrawInteractionCallbacks, DrawInteractionOptions, HoverInteraction, HoverInteractionCallbacks, HoverInteractionOptions, LassoInteraction, LassoInteractionCallbacks, LassoInteractionOptions, PanInteraction, PanInteractionCallbacks, PanInteractionOptions, ZoomInteraction, ZoomInteractionCallbacks } from '@neo4j-nvl/interaction-handlers';
2
+ import type { BasicReactWrapperProps } from '../basic-wrapper/BasicNvlWrapper';
3
+ export type MouseInteractionModule = typeof HoverInteraction | typeof ClickInteraction | typeof PanInteraction | typeof ZoomInteraction | typeof DragNodeInteraction | typeof DrawInteraction | typeof BoxSelectInteraction | typeof LassoInteraction;
4
+ export type MouseInteraction = HoverInteraction | ClickInteraction | PanInteraction | ZoomInteraction | DragNodeInteraction | DrawInteraction | BoxSelectInteraction | LassoInteraction;
5
+ /**
6
+ * Collection of mouse event callbacks that can be used with
7
+ * the {@link InteractiveNvlWrapper} component.
8
+ */
9
+ export type MouseEventCallbacks = ClickInteractionCallbacks & DragNodeInteractionCallbacks & HoverInteractionCallbacks & PanInteractionCallbacks & ZoomInteractionCallbacks & BoxSelectInteractionCallbacks & DrawInteractionCallbacks & LassoInteractionCallbacks;
10
+ /**
11
+ * Collection of interaction options that can be used with
12
+ * the {@link InteractiveNvlWrapper} component.
13
+ */
14
+ export type InteractionOptions = ClickInteractionOptions & BoxSelectInteractionOptions & HoverInteractionOptions & PanInteractionOptions & LassoInteractionOptions & DrawInteractionOptions;
15
+ /**
16
+ * The events that can be passed to the {@link MouseEventCallbacks} object
17
+ * to turn on/off certain events for the {@link InteractiveNvlWrapper} component.
18
+ */
19
+ export type MouseEvent = keyof MouseEventCallbacks;
20
+ /**
21
+ * The properties that can be passed to the {@link InteractiveNvlWrapper} component.
22
+ */
23
+ export interface InteractiveNvlWrapperProps extends BasicReactWrapperProps {
24
+ /** {@link MouseEventCallbacks} containing functions for callbacks on certain actions */
25
+ mouseEventCallbacks?: MouseEventCallbacks;
26
+ /** {@link InteractionOptions} for the underlying interaction handlers */
27
+ interactionOptions?: InteractionOptions;
28
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,22 @@
1
+ import type { Node, NvlOptions, Relationship } from '@neo4j-nvl/base';
2
+ /**
3
+ * The properties that can be passed to the StaticPictureWrapper component.
4
+ */
5
+ export type StaticPictureWrapperProps = {
6
+ /** The nodes to be displayed in the graph. */
7
+ nodes: Node[];
8
+ /** The relationships to be displayed in the graph. */
9
+ rels: Relationship[];
10
+ /** Options to customize the NVL instance. */
11
+ nvlOptions?: NvlOptions;
12
+ /** The width of the static picture. */
13
+ width?: number;
14
+ /** The height of the static picture. */
15
+ height?: number;
16
+ };
17
+ /**
18
+ * A React component that creates a static picture of a graph using NVL.
19
+ * This component is useful for generating static visualizations of graphs without requiring user interaction.
20
+ * NVL is destroyed after capturing the image to free up resources.
21
+ */
22
+ export declare const StaticPictureWrapper: ({ nodes, rels, nvlOptions, width, height }: StaticPictureWrapperProps) => import("react/jsx-runtime").JSX.Element | null;
@@ -0,0 +1,32 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import NVL from '@neo4j-nvl/base';
3
+ import { useEffect, useState } from 'react';
4
+ /**
5
+ * A React component that creates a static picture of a graph using NVL.
6
+ * This component is useful for generating static visualizations of graphs without requiring user interaction.
7
+ * NVL is destroyed after capturing the image to free up resources.
8
+ */
9
+ export const StaticPictureWrapper = ({ nodes, rels, nvlOptions = {}, width = 500, height = 500 }) => {
10
+ const [imgSrc, setImgSrc] = useState();
11
+ useEffect(() => {
12
+ const div = document.createElement('div');
13
+ div.style.width = `${width / window.devicePixelRatio}px`;
14
+ div.style.height = `${height / window.devicePixelRatio}px`;
15
+ const myNvl = new NVL(div, nodes, rels, nvlOptions, {
16
+ onLayoutDone: () => {
17
+ myNvl.fit(myNvl.getNodes().map((n) => n.id));
18
+ },
19
+ onZoomTransitionDone: () => {
20
+ setImgSrc(myNvl.getImageDataUrl());
21
+ setTimeout(() => {
22
+ myNvl.destroy();
23
+ });
24
+ }
25
+ });
26
+ return () => {
27
+ myNvl?.destroy();
28
+ };
29
+ // eslint-disable-next-line react-hooks/exhaustive-deps
30
+ }, [nodes, rels, width, height]);
31
+ return imgSrc !== undefined ? _jsx("img", { src: imgSrc, width: width, height: height, alt: "Graph" }) : null;
32
+ };
@@ -0,0 +1,2 @@
1
+ export const BASIC_WRAPPER_ID: "NVL_basic-wrapper";
2
+ export const INTERACTIVE_WRAPPER_ID: "NVL_interactive-wrapper";
@@ -0,0 +1,2 @@
1
+ export const BASIC_WRAPPER_ID = 'NVL_basic-wrapper';
2
+ export const INTERACTIVE_WRAPPER_ID = 'NVL_interactive-wrapper';
@@ -0,0 +1,8 @@
1
+ import type { Node, Relationship } from '@neo4j-nvl/base';
2
+ declare const getMapDifferences: <T extends Node | Relationship>(prevGraphElements: T[], newGraphElements: T[]) => {
3
+ added: T[];
4
+ removed: T[];
5
+ updated: T[];
6
+ };
7
+ declare const getNodeAttributeDifferences: (prevNodes: Node[], newNodes: Node[]) => Node[];
8
+ export { getNodeAttributeDifferences, getMapDifferences };
@@ -0,0 +1,72 @@
1
+ import { isEqual, isNil, keyBy, keys, sortBy, transform } from 'lodash';
2
+ const getMapDifferences = (prevGraphElements, newGraphElements) => {
3
+ const prevMap = keyBy(prevGraphElements, 'id');
4
+ const currentMap = keyBy(newGraphElements, 'id');
5
+ const prevIds = sortBy(keys(prevMap));
6
+ const currentIds = sortBy(keys(currentMap));
7
+ const added = [];
8
+ const removed = [];
9
+ const updated = [];
10
+ let i = 0;
11
+ let j = 0;
12
+ while (i < prevIds.length && j < currentIds.length) {
13
+ const prevId = prevIds[i];
14
+ const currId = currentIds[j];
15
+ if (prevId === undefined || currId === undefined) {
16
+ continue;
17
+ }
18
+ if (prevId === currId) {
19
+ if (!isEqual(prevMap[prevId], currentMap[currId])) {
20
+ updated.push(currId);
21
+ }
22
+ i += 1;
23
+ j += 1;
24
+ }
25
+ else if (prevId < currId) {
26
+ removed.push(prevId);
27
+ i += 1;
28
+ }
29
+ else {
30
+ added.push(currId);
31
+ j += 1;
32
+ }
33
+ }
34
+ while (i < prevIds.length) {
35
+ const prevId = prevIds[i];
36
+ if (prevId === undefined) {
37
+ continue;
38
+ }
39
+ removed.push(prevId);
40
+ i += 1;
41
+ }
42
+ while (j < currentIds.length) {
43
+ const currId = currentIds[j];
44
+ if (currId === undefined) {
45
+ continue;
46
+ }
47
+ added.push(currId);
48
+ j += 1;
49
+ }
50
+ return {
51
+ added: added.map((id) => currentMap[id]).filter((n) => !isNil(n)),
52
+ removed: removed.map((id) => prevMap[id]).filter((n) => !isNil(n)),
53
+ updated: updated.map((id) => currentMap[id]).filter((n) => !isNil(n))
54
+ };
55
+ };
56
+ const getNodeAttributeDifferences = (prevNodes, newNodes) => {
57
+ const prevNodeMap = keyBy(prevNodes, 'id');
58
+ return newNodes
59
+ .map((nodeToUpdate) => {
60
+ const previousNode = prevNodeMap[nodeToUpdate.id];
61
+ if (previousNode === undefined) {
62
+ return null;
63
+ }
64
+ return transform(nodeToUpdate, (result, value, key) => {
65
+ if (key === 'id' || value !== previousNode[key]) {
66
+ Object.assign(result, { [key]: value });
67
+ }
68
+ });
69
+ })
70
+ .filter((n) => n !== null && Object.keys(n).length > 1);
71
+ };
72
+ export { getNodeAttributeDifferences, getMapDifferences };
@@ -0,0 +1,2 @@
1
+ import type { EffectCallback } from 'react';
2
+ export declare const useDeepCompareEffect: (callback: EffectCallback, dependencies: unknown[]) => void;
@@ -0,0 +1,16 @@
1
+ import { isEqual } from 'lodash';
2
+ import { useEffect, useRef } from 'react';
3
+ const deepCompareEquals = (a, b) => {
4
+ return isEqual(a, b);
5
+ };
6
+ const useDeepCompareMemoize = (value) => {
7
+ const ref = useRef();
8
+ if (!deepCompareEquals(value, ref.current)) {
9
+ ref.current = value;
10
+ }
11
+ return ref.current;
12
+ };
13
+ export const useDeepCompareEffect = (callback, dependencies) => {
14
+ // eslint-disable-next-line react-hooks/exhaustive-deps
15
+ useEffect(callback, dependencies.map(useDeepCompareMemoize));
16
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neo4j-nvl/react",
3
- "version": "1.0.0-0e1dd35b-alpha",
3
+ "version": "1.0.0-0f60498a",
4
4
  "main": "lib/index.js",
5
5
  "homepage": "https://neo4j.com/docs/nvl/current/",
6
6
  "license": "SEE LICENSE IN 'LICENSE.txt'",
@@ -27,22 +27,22 @@
27
27
  "lib"
28
28
  ],
29
29
  "devDependencies": {
30
- "@testing-library/dom": "^10.4.0",
31
- "@testing-library/jest-dom": "^5.16.5",
32
- "@testing-library/react": "^16.3.0",
33
- "@types/lodash": "4.14.202",
34
- "@types/react": "^18.2.58",
35
- "react": "^19.1.1",
36
- "react-dom": "^19.1.1"
30
+ "@testing-library/dom": "10.4.1",
31
+ "@testing-library/jest-dom": "5.17.0",
32
+ "@testing-library/react": "16.3.0",
33
+ "@types/lodash": "4.17.21",
34
+ "@types/react": "18.3.27",
35
+ "react": "19.2.1",
36
+ "react-dom": "19.2.1"
37
37
  },
38
38
  "dependencies": {
39
- "@neo4j-nvl/base": "1.0.0-0e1dd35b-alpha",
40
- "@neo4j-nvl/interaction-handlers": "1.0.0-0e1dd35b-alpha",
41
- "lodash": "4.17.21"
39
+ "@neo4j-nvl/base": "1.0.0-0f60498a",
40
+ "@neo4j-nvl/interaction-handlers": "1.0.0-0f60498a",
41
+ "lodash": "4.17.23"
42
42
  },
43
43
  "peerDependencies": {
44
- "react": "^18.0.0 || ^19.0.0",
45
- "react-dom": "^18.0.0 || ^19.0.0"
44
+ "react": "18.0.0 || ^19.0.0",
45
+ "react-dom": "18.0.0 || ^19.0.0"
46
46
  },
47
47
  "stableVersion": "1.0.0"
48
48
  }