@neo4j-nvl/interaction-handlers 1.0.0 → 1.1.0-17f2ef68
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 +25 -0
- package/lib/__tests__/pan-interaction.test.js +105 -0
- package/lib/__tests__/zoom-interaction.test.js +171 -64
- package/lib/index.d.ts +2 -2
- package/lib/interaction-handlers/base.d.ts +1 -1
- package/lib/interaction-handlers/drag-node-interaction.js +3 -8
- package/lib/interaction-handlers/pan-interaction.d.ts +11 -2
- package/lib/interaction-handlers/pan-interaction.js +20 -9
- package/lib/interaction-handlers/zoom-interaction.d.ts +33 -9
- package/lib/interaction-handlers/zoom-interaction.js +31 -19
- package/package.json +8 -7
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
|
|
|
@@ -112,4 +112,109 @@ describe('PanInteraction', () => {
|
|
|
112
112
|
document.body.dispatchEvent(bodyMouseUpEvent);
|
|
113
113
|
expect(document.body.style.getPropertyValue('user-select')).toBe('');
|
|
114
114
|
});
|
|
115
|
+
test('when controlledPan is true, should not update NVL instance', () => {
|
|
116
|
+
panInteraction.destroy();
|
|
117
|
+
myNVL.destroy();
|
|
118
|
+
myNVL = new NVL(document.createElement('div'), [
|
|
119
|
+
{ id: '0', x: 10, y: 10 },
|
|
120
|
+
{ id: '1', x: 200, y: 200 }
|
|
121
|
+
], [{ id: '10', from: '0', to: '1' }], { disableWebGL: true, initialZoom: 1, layout: 'free' });
|
|
122
|
+
const panInteractionNoUpdate = new PanInteraction(myNVL, { controlledPan: true });
|
|
123
|
+
panInteractionNoUpdate.updateCallback('onPan', panCallbackMock);
|
|
124
|
+
const initialPan = myNVL.getPan();
|
|
125
|
+
const mouseDownEvent = new MouseEvent('mousedown', {
|
|
126
|
+
clientX: 100,
|
|
127
|
+
clientY: 100
|
|
128
|
+
});
|
|
129
|
+
const mouseMoveEvent = new MouseEvent('mousemove', {
|
|
130
|
+
buttons: 1,
|
|
131
|
+
clientX: 120,
|
|
132
|
+
clientY: 120
|
|
133
|
+
});
|
|
134
|
+
const mouseUpEvent = new MouseEvent('mouseup', {
|
|
135
|
+
clientX: 120,
|
|
136
|
+
clientY: 120
|
|
137
|
+
});
|
|
138
|
+
const container = myNVL.getContainer();
|
|
139
|
+
container.dispatchEvent(mouseDownEvent);
|
|
140
|
+
container.dispatchEvent(mouseMoveEvent);
|
|
141
|
+
container.dispatchEvent(mouseUpEvent);
|
|
142
|
+
return new Promise((resolve) => {
|
|
143
|
+
expect(myNVL.getPan()).toEqual(initialPan);
|
|
144
|
+
expect(panCallbackMock).toHaveBeenCalledTimes(1);
|
|
145
|
+
expect(panCallbackMock).toHaveBeenCalledWith({
|
|
146
|
+
x: initialPan.x - 20,
|
|
147
|
+
y: initialPan.y - 20
|
|
148
|
+
}, mouseMoveEvent);
|
|
149
|
+
panInteractionNoUpdate.destroy();
|
|
150
|
+
resolve();
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
test('when controlledPan is false, should update NVL instance normally', () => {
|
|
154
|
+
panInteraction.destroy();
|
|
155
|
+
myNVL.destroy();
|
|
156
|
+
myNVL = new NVL(document.createElement('div'), [
|
|
157
|
+
{ id: '0', x: 10, y: 10 },
|
|
158
|
+
{ id: '1', x: 200, y: 200 }
|
|
159
|
+
], [{ id: '10', from: '0', to: '1' }], { disableWebGL: true, initialZoom: 1, layout: 'free' });
|
|
160
|
+
const panInteractionWithUpdate = new PanInteraction(myNVL, { controlledPan: false });
|
|
161
|
+
panInteractionWithUpdate.updateCallback('onPan', panCallbackMock);
|
|
162
|
+
const initialPan = myNVL.getPan();
|
|
163
|
+
const mouseDownEvent = new MouseEvent('mousedown', {
|
|
164
|
+
clientX: 100,
|
|
165
|
+
clientY: 100
|
|
166
|
+
});
|
|
167
|
+
const mouseMoveEvent = new MouseEvent('mousemove', {
|
|
168
|
+
buttons: 1,
|
|
169
|
+
clientX: 120,
|
|
170
|
+
clientY: 120
|
|
171
|
+
});
|
|
172
|
+
const mouseUpEvent = new MouseEvent('mouseup');
|
|
173
|
+
const container = myNVL.getContainer();
|
|
174
|
+
container.dispatchEvent(mouseDownEvent);
|
|
175
|
+
container.dispatchEvent(mouseMoveEvent);
|
|
176
|
+
container.dispatchEvent(mouseUpEvent);
|
|
177
|
+
return new Promise((resolve) => {
|
|
178
|
+
expect(myNVL.getPan()).not.toEqual(initialPan);
|
|
179
|
+
expect(myNVL.getPan()).toEqual({
|
|
180
|
+
x: initialPan.x - 20,
|
|
181
|
+
y: initialPan.y - 20
|
|
182
|
+
});
|
|
183
|
+
expect(panCallbackMock).toHaveBeenCalledTimes(1);
|
|
184
|
+
panInteractionWithUpdate.destroy();
|
|
185
|
+
resolve();
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
test('when controlledPan is true, should provide correct callback values for multiple operations', () => {
|
|
189
|
+
panInteraction.destroy();
|
|
190
|
+
myNVL.destroy();
|
|
191
|
+
myNVL = new NVL(document.createElement('div'), [
|
|
192
|
+
{ id: '0', x: 10, y: 10 },
|
|
193
|
+
{ id: '1', x: 200, y: 200 }
|
|
194
|
+
], [{ id: '10', from: '0', to: '1' }], { disableWebGL: true, initialZoom: 1, layout: 'free' });
|
|
195
|
+
const panInteractionNoUpdate = new PanInteraction(myNVL, { controlledPan: true });
|
|
196
|
+
panInteractionNoUpdate.updateCallback('onPan', panCallbackMock);
|
|
197
|
+
const initialPan = myNVL.getPan();
|
|
198
|
+
const container = myNVL.getContainer();
|
|
199
|
+
container.dispatchEvent(new MouseEvent('mousedown', { clientX: 100, clientY: 100 }));
|
|
200
|
+
container.dispatchEvent(new MouseEvent('mousemove', { buttons: 1, clientX: 120, clientY: 120 }));
|
|
201
|
+
container.dispatchEvent(new MouseEvent('mouseup'));
|
|
202
|
+
container.dispatchEvent(new MouseEvent('mousedown', { clientX: 120, clientY: 120 }));
|
|
203
|
+
container.dispatchEvent(new MouseEvent('mousemove', { buttons: 1, clientX: 140, clientY: 140 }));
|
|
204
|
+
container.dispatchEvent(new MouseEvent('mouseup'));
|
|
205
|
+
return new Promise((resolve) => {
|
|
206
|
+
expect(panCallbackMock).toHaveBeenCalledTimes(2);
|
|
207
|
+
expect(panCallbackMock).toHaveBeenNthCalledWith(1, {
|
|
208
|
+
x: initialPan.x - 20,
|
|
209
|
+
y: initialPan.y - 20
|
|
210
|
+
}, expect.anything());
|
|
211
|
+
expect(panCallbackMock).toHaveBeenNthCalledWith(2, {
|
|
212
|
+
x: initialPan.x - 20,
|
|
213
|
+
y: initialPan.y - 20
|
|
214
|
+
}, expect.anything());
|
|
215
|
+
expect(myNVL.getPan()).toEqual(initialPan);
|
|
216
|
+
panInteractionNoUpdate.destroy();
|
|
217
|
+
resolve();
|
|
218
|
+
});
|
|
219
|
+
});
|
|
115
220
|
});
|
|
@@ -7,6 +7,7 @@ describe('ZoomInteraction', () => {
|
|
|
7
7
|
let myNVL;
|
|
8
8
|
let myNvlContainer;
|
|
9
9
|
const zoomCallbackMock = jest.fn();
|
|
10
|
+
const zoomAndPanCallbackMock = jest.fn();
|
|
10
11
|
beforeEach(() => {
|
|
11
12
|
myNVL = new NVL(document.createElement('div'), [], [], { disableWebGL: true, initialZoom: 1, layout: 'free' });
|
|
12
13
|
myNvlContainer = myNVL.getContainer();
|
|
@@ -16,6 +17,7 @@ describe('ZoomInteraction', () => {
|
|
|
16
17
|
zoomInteraction.destroy();
|
|
17
18
|
myNVL.destroy();
|
|
18
19
|
zoomCallbackMock.mockReset();
|
|
20
|
+
zoomAndPanCallbackMock.mockReset();
|
|
19
21
|
});
|
|
20
22
|
const simulateWheelEvent = async (container, deltaY, delay = 50, eventOptions = {}) => {
|
|
21
23
|
const wheelEvent = new WheelEvent('wheel', {
|
|
@@ -34,71 +36,176 @@ describe('ZoomInteraction', () => {
|
|
|
34
36
|
await simulateWheelEvent(container, event.deltaY, event.delay);
|
|
35
37
|
}
|
|
36
38
|
};
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
39
|
+
describe('Scrolling', () => {
|
|
40
|
+
test('zooming in should increase the scale', async () => {
|
|
41
|
+
zoomInteraction.updateCallback('onZoom', zoomCallbackMock);
|
|
42
|
+
const initialScale = myNVL.getScale();
|
|
43
|
+
const container = myNVL.getContainer();
|
|
44
|
+
await simulateWheelEvent(container, -100);
|
|
45
|
+
expect(zoomCallbackMock).toHaveBeenCalledTimes(1);
|
|
46
|
+
const newScale = myNVL.getScale();
|
|
47
|
+
expect(newScale).toBeGreaterThan(initialScale);
|
|
48
|
+
expect(newScale).toBe(1.2);
|
|
49
|
+
});
|
|
50
|
+
test('zooming out should decrease the scale', async () => {
|
|
51
|
+
zoomInteraction.updateCallback('onZoom', zoomCallbackMock);
|
|
52
|
+
const initialScale = myNVL.getScale();
|
|
53
|
+
await simulateWheelEvent(myNvlContainer, 100);
|
|
54
|
+
expect(zoomCallbackMock).toHaveBeenCalledTimes(1);
|
|
55
|
+
const newScale = myNVL.getScale();
|
|
56
|
+
expect(newScale).toBeLessThan(initialScale);
|
|
57
|
+
expect(newScale).toBe(0.8);
|
|
58
|
+
});
|
|
59
|
+
test('zoom callback should be called with the correct zoom level', async () => {
|
|
60
|
+
zoomInteraction.updateCallback('onZoom', zoomCallbackMock);
|
|
61
|
+
const wheelEvent = await simulateWheelEvent(myNvlContainer, -100);
|
|
62
|
+
expect(zoomCallbackMock).toHaveBeenCalledTimes(1);
|
|
63
|
+
const zoomCallbackMockArguments = zoomCallbackMock.mock.calls[0] ?? [];
|
|
64
|
+
expect(zoomCallbackMockArguments[0]).toBe(1.2);
|
|
65
|
+
expect(zoomCallbackMockArguments[1]).toBe(wheelEvent);
|
|
66
|
+
});
|
|
67
|
+
test('zooming should adjust pan to mouse position', async () => {
|
|
68
|
+
const initialPan = myNVL.getPan();
|
|
69
|
+
const initialScale = myNVL.getScale();
|
|
70
|
+
await simulateWheelEvent(myNvlContainer, -100);
|
|
71
|
+
const newPan = myNVL.getPan();
|
|
72
|
+
const newScale = myNVL.getScale();
|
|
73
|
+
expect(newPan).not.toEqual(initialPan);
|
|
74
|
+
expect(newPan.x).toBe(25);
|
|
75
|
+
expect(newPan.y).toBe(25);
|
|
76
|
+
expect(newScale).toBeGreaterThan(initialScale);
|
|
77
|
+
expect(newScale).toBe(1.2);
|
|
78
|
+
});
|
|
79
|
+
test('zooming should be progressively faster at higher zoom levels', async () => {
|
|
80
|
+
const sequentialWheelEvents = Array(8).fill({ deltaY: -100 });
|
|
81
|
+
await simulateSequentialWheelEvents(myNvlContainer, sequentialWheelEvents);
|
|
82
|
+
const newScale = myNVL.getScale();
|
|
83
|
+
expect(newScale).toBeGreaterThan(3.0);
|
|
84
|
+
expect(newScale).toBeCloseTo(4.299);
|
|
85
|
+
});
|
|
86
|
+
test('handles touchpad-like small incremental scrolling', async () => {
|
|
87
|
+
const initialScale = myNVL.getScale();
|
|
88
|
+
const touchpadEvents = Array(25).fill({ deltaY: -20, delay: 25 });
|
|
89
|
+
await simulateSequentialWheelEvents(myNvlContainer, touchpadEvents);
|
|
90
|
+
const newScale = myNVL.getScale();
|
|
91
|
+
expect(newScale).toBeGreaterThan(initialScale);
|
|
92
|
+
expect(newScale).toBeGreaterThan(2.5);
|
|
93
|
+
expect(newScale).toBeLessThan(2.7);
|
|
94
|
+
});
|
|
95
|
+
test('onZoomAndPan callback should be called with zoom level and pan values', async () => {
|
|
96
|
+
zoomInteraction.updateCallback('onZoomAndPan', zoomAndPanCallbackMock);
|
|
97
|
+
const wheelEvent = await simulateWheelEvent(myNvlContainer, -100);
|
|
98
|
+
expect(zoomAndPanCallbackMock).toHaveBeenCalledTimes(1);
|
|
99
|
+
const zoomAndPanCallbackMockArguments = zoomAndPanCallbackMock.mock.calls[0] ?? [];
|
|
100
|
+
expect(zoomAndPanCallbackMockArguments[0]).toBe(1.2);
|
|
101
|
+
expect(zoomAndPanCallbackMockArguments[1]).toBe(25);
|
|
102
|
+
expect(zoomAndPanCallbackMockArguments[2]).toBe(25);
|
|
103
|
+
expect(zoomAndPanCallbackMockArguments[3]).toBe(wheelEvent);
|
|
104
|
+
expect(zoomCallbackMock).toHaveBeenCalledTimes(0);
|
|
105
|
+
});
|
|
106
|
+
test('both callbacks should be called when both are registered', async () => {
|
|
107
|
+
zoomInteraction.updateCallback('onZoom', zoomCallbackMock);
|
|
108
|
+
zoomInteraction.updateCallback('onZoomAndPan', zoomAndPanCallbackMock);
|
|
109
|
+
const wheelEvent = await simulateWheelEvent(myNvlContainer, -100);
|
|
110
|
+
expect(zoomCallbackMock).toHaveBeenCalledTimes(1);
|
|
111
|
+
expect(zoomAndPanCallbackMock).toHaveBeenCalledTimes(1);
|
|
112
|
+
const zoomCallbackMockArguments = zoomCallbackMock.mock.calls[0] ?? [];
|
|
113
|
+
expect(zoomCallbackMockArguments[0]).toBe(1.2);
|
|
114
|
+
expect(zoomCallbackMockArguments[1]).toBe(wheelEvent);
|
|
115
|
+
const zoomAndPanCallbackMockArguments = zoomAndPanCallbackMock.mock.calls[0] ?? [];
|
|
116
|
+
expect(zoomAndPanCallbackMockArguments[0]).toBe(1.2);
|
|
117
|
+
expect(zoomAndPanCallbackMockArguments[1]).toBe(25);
|
|
118
|
+
expect(zoomAndPanCallbackMockArguments[2]).toBe(25);
|
|
119
|
+
expect(zoomAndPanCallbackMockArguments[3]).toBe(wheelEvent);
|
|
120
|
+
});
|
|
91
121
|
});
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
122
|
+
describe('Trackpad pinch gestures', () => {
|
|
123
|
+
test('trackpad pinch out should zoom in', async () => {
|
|
124
|
+
zoomInteraction.updateCallback('onZoom', zoomCallbackMock);
|
|
125
|
+
await simulateWheelEvent(myNvlContainer, -75, 50, { ctrlKey: true });
|
|
126
|
+
expect(zoomCallbackMock).toHaveBeenCalledTimes(1);
|
|
127
|
+
const newScale = myNVL.getScale();
|
|
128
|
+
expect(newScale).toBe(2);
|
|
129
|
+
});
|
|
130
|
+
test('trackpad pinch in should zoom out', async () => {
|
|
131
|
+
zoomInteraction.updateCallback('onZoom', zoomCallbackMock);
|
|
132
|
+
await simulateWheelEvent(myNvlContainer, 100, 50, { ctrlKey: true });
|
|
133
|
+
expect(zoomCallbackMock).toHaveBeenCalledTimes(1);
|
|
134
|
+
const newScale = myNVL.getScale();
|
|
135
|
+
expect(newScale).toBe(0.075);
|
|
136
|
+
});
|
|
137
|
+
test('trackpad pinch should work with meta key (macOS)', async () => {
|
|
138
|
+
zoomInteraction.updateCallback('onZoom', zoomCallbackMock);
|
|
139
|
+
const initialScale = myNVL.getScale();
|
|
140
|
+
await simulateWheelEvent(myNvlContainer, -50, 50, { metaKey: true });
|
|
141
|
+
expect(zoomCallbackMock).toHaveBeenCalledTimes(1);
|
|
142
|
+
const newScale = myNVL.getScale();
|
|
143
|
+
expect(newScale).toBeGreaterThan(initialScale);
|
|
144
|
+
});
|
|
145
|
+
test('trackpad pinch should adjust pan to cursor position', async () => {
|
|
146
|
+
const initialPan = myNVL.getPan();
|
|
147
|
+
const initialScale = myNVL.getScale();
|
|
148
|
+
await simulateWheelEvent(myNvlContainer, -50, 50, {
|
|
149
|
+
ctrlKey: true,
|
|
150
|
+
clientX: 200,
|
|
151
|
+
clientY: 100
|
|
152
|
+
});
|
|
153
|
+
const newPan = myNVL.getPan();
|
|
154
|
+
const newScale = myNVL.getScale();
|
|
155
|
+
expect(newPan).not.toEqual(initialPan);
|
|
156
|
+
expect(newScale).toBeGreaterThan(initialScale);
|
|
157
|
+
});
|
|
97
158
|
});
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
159
|
+
describe('Controlled Zoom', () => {
|
|
160
|
+
test('when controlledZoom is true, should not update NVL instance', async () => {
|
|
161
|
+
zoomInteraction.destroy();
|
|
162
|
+
myNVL.destroy();
|
|
163
|
+
myNVL = new NVL(document.createElement('div'), [], [], { disableWebGL: true, initialZoom: 1, layout: 'free' });
|
|
164
|
+
myNvlContainer = myNVL.getContainer();
|
|
165
|
+
const zoomInteractionNoUpdate = new ZoomInteraction(myNVL, { controlledZoom: true });
|
|
166
|
+
zoomInteractionNoUpdate.updateCallback('onZoomAndPan', zoomAndPanCallbackMock);
|
|
167
|
+
const initialScale = myNVL.getScale();
|
|
168
|
+
const initialPan = myNVL.getPan();
|
|
169
|
+
await simulateWheelEvent(myNvlContainer, -100);
|
|
170
|
+
expect(myNVL.getScale()).toBe(initialScale);
|
|
171
|
+
expect(myNVL.getPan()).toEqual(initialPan);
|
|
172
|
+
expect(zoomAndPanCallbackMock).toHaveBeenCalledTimes(1);
|
|
173
|
+
const [newZoom, newPanX, newPanY, event] = zoomAndPanCallbackMock.mock.calls[0] ?? [];
|
|
174
|
+
expect(newZoom).toBe(1.2);
|
|
175
|
+
expect(newPanX).toBe(25);
|
|
176
|
+
expect(newPanY).toBe(25);
|
|
177
|
+
expect(event).toBeInstanceOf(WheelEvent);
|
|
178
|
+
zoomInteractionNoUpdate.destroy();
|
|
179
|
+
});
|
|
180
|
+
test('when controlledZoom is false, should update NVL instance normally', async () => {
|
|
181
|
+
zoomInteraction.destroy();
|
|
182
|
+
myNVL.destroy();
|
|
183
|
+
myNVL = new NVL(document.createElement('div'), [], [], { disableWebGL: true, initialZoom: 1, layout: 'free' });
|
|
184
|
+
myNvlContainer = myNVL.getContainer();
|
|
185
|
+
const zoomInteractionWithUpdate = new ZoomInteraction(myNVL, { controlledZoom: false });
|
|
186
|
+
zoomInteractionWithUpdate.updateCallback('onZoom', zoomCallbackMock);
|
|
187
|
+
const initialScale = myNVL.getScale();
|
|
188
|
+
await simulateWheelEvent(myNvlContainer, -100);
|
|
189
|
+
expect(myNVL.getScale()).not.toBe(initialScale);
|
|
190
|
+
expect(myNVL.getScale()).toBe(1.2);
|
|
191
|
+
expect(zoomCallbackMock).toHaveBeenCalledTimes(1);
|
|
192
|
+
zoomInteractionWithUpdate.destroy();
|
|
193
|
+
});
|
|
194
|
+
test('when controlledZoom is true, should provide correct callback values for multiple operations', async () => {
|
|
195
|
+
zoomInteraction.destroy();
|
|
196
|
+
myNVL.destroy();
|
|
197
|
+
myNVL = new NVL(document.createElement('div'), [], [], { disableWebGL: true, initialZoom: 1, layout: 'free' });
|
|
198
|
+
myNvlContainer = myNVL.getContainer();
|
|
199
|
+
const zoomInteractionNoUpdate = new ZoomInteraction(myNVL, { controlledZoom: true });
|
|
200
|
+
zoomInteractionNoUpdate.updateCallback('onZoomAndPan', zoomAndPanCallbackMock);
|
|
201
|
+
await simulateWheelEvent(myNvlContainer, -100);
|
|
202
|
+
await simulateWheelEvent(myNvlContainer, 100);
|
|
203
|
+
expect(zoomAndPanCallbackMock).toHaveBeenCalledTimes(2);
|
|
204
|
+
const [zoom1] = zoomAndPanCallbackMock.mock.calls[0] ?? [];
|
|
205
|
+
expect(zoom1).toBe(1.2);
|
|
206
|
+
const [zoom2] = zoomAndPanCallbackMock.mock.calls[1] ?? [];
|
|
207
|
+
expect(zoom2).toBe(0.8);
|
|
208
|
+
zoomInteractionNoUpdate.destroy();
|
|
209
|
+
});
|
|
103
210
|
});
|
|
104
211
|
});
|
package/lib/index.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ import { LassoInteraction } from './interaction-handlers/lasso-interaction';
|
|
|
12
12
|
import type { LassoInteractionCallbacks, LassoInteractionOptions } from './interaction-handlers/lasso-interaction';
|
|
13
13
|
import type { PanInteractionCallbacks, PanInteractionOptions } from './interaction-handlers/pan-interaction';
|
|
14
14
|
import { PanInteraction } from './interaction-handlers/pan-interaction';
|
|
15
|
-
import type { ZoomInteractionCallbacks } from './interaction-handlers/zoom-interaction';
|
|
15
|
+
import type { ZoomInteractionCallbacks, ZoomInteractionOptions } from './interaction-handlers/zoom-interaction';
|
|
16
16
|
import { ZoomInteraction } from './interaction-handlers/zoom-interaction';
|
|
17
|
-
export type { BoxSelectInteractionOptions, BoxSelectInteractionCallbacks, ClickInteractionOptions, HoverInteractionOptions, HoverInteractionCallbacks, DragNodeInteractionCallbacks, ClickInteractionCallbacks, PanInteractionCallbacks, DrawInteractionCallbacks, PanInteractionOptions, ZoomInteractionCallbacks, LassoInteractionOptions, LassoInteractionCallbacks, DrawInteractionOptions };
|
|
17
|
+
export type { BoxSelectInteractionOptions, BoxSelectInteractionCallbacks, ClickInteractionOptions, HoverInteractionOptions, HoverInteractionCallbacks, DragNodeInteractionCallbacks, ClickInteractionCallbacks, PanInteractionCallbacks, DrawInteractionCallbacks, PanInteractionOptions, ZoomInteractionCallbacks, ZoomInteractionOptions, LassoInteractionOptions, LassoInteractionCallbacks, DrawInteractionOptions };
|
|
18
18
|
export { ZoomInteraction, PanInteraction, BoxSelectInteraction, ClickInteraction, HoverInteraction, DragNodeInteraction, DrawInteraction, LassoInteraction };
|
|
@@ -65,7 +65,7 @@ declare abstract class BaseInteraction<T extends Record<string, ((...args: unkno
|
|
|
65
65
|
* @param enable - Whether to enable or disable global text selection.
|
|
66
66
|
* @param eventFunction - The event function to be added/removed when text selection is disabled/enabled.
|
|
67
67
|
*/
|
|
68
|
-
toggleGlobalTextSelection: (enable: boolean, eventFunction?: (
|
|
68
|
+
toggleGlobalTextSelection: (enable: boolean, eventFunction?: (this: HTMLElement, ev: MouseEvent) => void) => void;
|
|
69
69
|
/**
|
|
70
70
|
* @internal
|
|
71
71
|
* @hidden
|
|
@@ -26,19 +26,17 @@ export class DragNodeInteraction extends BaseInteraction {
|
|
|
26
26
|
}
|
|
27
27
|
handleMouseDown = (event) => {
|
|
28
28
|
this.mousePosition = { x: event.clientX, y: event.clientY };
|
|
29
|
+
this.mouseDownNode = null;
|
|
29
30
|
const hits = this.nvlInstance.getHits(event, ['node'], { hitNodeMarginWidth: NODE_EDGE_WIDTH });
|
|
30
31
|
const hitNodes = hits.nvlTargets.nodes.filter((node) => node.insideNode);
|
|
31
32
|
const hitNodeEdges = hits.nvlTargets.nodes.filter((node) => !node.insideNode);
|
|
32
33
|
if (hitNodeEdges.length > 0) {
|
|
33
34
|
this.isDrawing = true;
|
|
34
|
-
this.mouseDownNode = null;
|
|
35
35
|
this.addEventListener('mouseup', this.resetState, { once: true });
|
|
36
36
|
}
|
|
37
37
|
else if (hitNodes.length > 0) {
|
|
38
38
|
this.mouseDownNode = hits.nvlTargets.nodes[0] ?? null;
|
|
39
|
-
|
|
40
|
-
else {
|
|
41
|
-
this.mouseDownNode = null;
|
|
39
|
+
this.toggleGlobalTextSelection(false, this.handleBodyMouseUp);
|
|
42
40
|
}
|
|
43
41
|
this.selectedNodes = this.nvlInstance.getSelectedNodes();
|
|
44
42
|
if (this.mouseDownNode !== null && this.selectedNodes.map((node) => node.id).includes(this.mouseDownNode.data.id)) {
|
|
@@ -57,7 +55,6 @@ export class DragNodeInteraction extends BaseInteraction {
|
|
|
57
55
|
return;
|
|
58
56
|
}
|
|
59
57
|
if (!this.isDragging) {
|
|
60
|
-
this.toggleGlobalTextSelection(false, this.handleBodyMouseUp);
|
|
61
58
|
if (this.moveSelectedNodes) {
|
|
62
59
|
this.callCallbackIfRegistered('onDragStart', this.selectedNodes, evt);
|
|
63
60
|
}
|
|
@@ -95,9 +92,7 @@ export class DragNodeInteraction extends BaseInteraction {
|
|
|
95
92
|
this.callCallbackIfRegistered('onDragEnd', [this.mouseDownNode.data], evt);
|
|
96
93
|
}
|
|
97
94
|
}
|
|
98
|
-
|
|
99
|
-
this.resetState();
|
|
100
|
-
}
|
|
95
|
+
this.resetState();
|
|
101
96
|
};
|
|
102
97
|
resetState = () => {
|
|
103
98
|
this.isDragging = false;
|
|
@@ -9,6 +9,13 @@ export type PanInteractionOptions = {
|
|
|
9
9
|
* @defaultValue false
|
|
10
10
|
*/
|
|
11
11
|
excludeNodeMargin?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* If true, the interaction handler will not update the NVL instance directly.
|
|
14
|
+
* Instead, it will only call the onPan callback with the calculated values,
|
|
15
|
+
* allowing the parent application to control when and how to apply the pan update.
|
|
16
|
+
* @defaultValue false
|
|
17
|
+
*/
|
|
18
|
+
controlledPan?: boolean;
|
|
12
19
|
};
|
|
13
20
|
/**
|
|
14
21
|
* Callbacks for the pan interaction handler.
|
|
@@ -30,7 +37,8 @@ export type PanInteractionCallbacks = {
|
|
|
30
37
|
* For examples, head to the {@link https://neo4j.com/docs/nvl/current/interaction-handlers/#_paninteraction Pan Interaction documentation page}.
|
|
31
38
|
*/
|
|
32
39
|
export declare class PanInteraction extends BaseInteraction<PanInteractionCallbacks, PanInteractionOptions> {
|
|
33
|
-
private
|
|
40
|
+
private initialMousePosition;
|
|
41
|
+
private initialPan;
|
|
34
42
|
private targets;
|
|
35
43
|
private shouldPan;
|
|
36
44
|
private isPanning;
|
|
@@ -57,10 +65,11 @@ export declare class PanInteraction extends BaseInteraction<PanInteractionCallba
|
|
|
57
65
|
* panInteraction.updateTargets([], true)
|
|
58
66
|
* ```
|
|
59
67
|
*/
|
|
60
|
-
updateTargets: (targets: (
|
|
68
|
+
updateTargets: (targets: ("node" | "relationship")[], excludeNodeMargin: boolean) => void;
|
|
61
69
|
private handleMouseDown;
|
|
62
70
|
private handleMouseMove;
|
|
63
71
|
private handleMouseUp;
|
|
72
|
+
private resetPanState;
|
|
64
73
|
/**
|
|
65
74
|
* Removes the related event listeners from the canvas.
|
|
66
75
|
*/
|
|
@@ -7,7 +7,8 @@ import { BaseInteraction } from './base';
|
|
|
7
7
|
* For examples, head to the {@link https://neo4j.com/docs/nvl/current/interaction-handlers/#_paninteraction Pan Interaction documentation page}.
|
|
8
8
|
*/
|
|
9
9
|
export class PanInteraction extends BaseInteraction {
|
|
10
|
-
|
|
10
|
+
initialMousePosition = { x: 0, y: 0 };
|
|
11
|
+
initialPan = { x: 0, y: 0 };
|
|
11
12
|
targets = [];
|
|
12
13
|
shouldPan = false;
|
|
13
14
|
isPanning = false;
|
|
@@ -51,7 +52,8 @@ export class PanInteraction extends BaseInteraction {
|
|
|
51
52
|
this.shouldPan = false;
|
|
52
53
|
}
|
|
53
54
|
else {
|
|
54
|
-
this.
|
|
55
|
+
this.initialMousePosition = { x: event.clientX, y: event.clientY };
|
|
56
|
+
this.initialPan = this.nvlInstance.getPan();
|
|
55
57
|
this.shouldPan = true;
|
|
56
58
|
}
|
|
57
59
|
};
|
|
@@ -64,19 +66,28 @@ export class PanInteraction extends BaseInteraction {
|
|
|
64
66
|
this.isPanning = true;
|
|
65
67
|
}
|
|
66
68
|
const zoom = this.nvlInstance.getScale();
|
|
67
|
-
const { x, y } = this.
|
|
68
|
-
const dx = ((evt.clientX - this.
|
|
69
|
-
const dy = ((evt.clientY - this.
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
this.
|
|
69
|
+
const { x, y } = this.initialPan;
|
|
70
|
+
const dx = ((evt.clientX - this.initialMousePosition.x) / zoom) * window.devicePixelRatio;
|
|
71
|
+
const dy = ((evt.clientY - this.initialMousePosition.y) / zoom) * window.devicePixelRatio;
|
|
72
|
+
const newPanX = x - dx;
|
|
73
|
+
const newPanY = y - dy;
|
|
74
|
+
if (this.currentOptions.controlledPan !== true) {
|
|
75
|
+
this.nvlInstance.setPan(newPanX, newPanY);
|
|
76
|
+
}
|
|
77
|
+
this.callCallbackIfRegistered('onPan', { x: newPanX, y: newPanY }, evt);
|
|
73
78
|
};
|
|
74
79
|
handleMouseUp = () => {
|
|
75
80
|
if (this.isPanning) {
|
|
76
81
|
this.toggleGlobalTextSelection(true, this.handleMouseUp);
|
|
77
|
-
this.isPanning = false;
|
|
78
82
|
}
|
|
83
|
+
this.resetPanState();
|
|
84
|
+
};
|
|
85
|
+
resetPanState = () => {
|
|
86
|
+
this.isPanning = false;
|
|
79
87
|
this.shouldPan = false;
|
|
88
|
+
this.initialMousePosition = { x: 0, y: 0 };
|
|
89
|
+
this.initialPan = { x: 0, y: 0 };
|
|
90
|
+
this.targets = [];
|
|
80
91
|
};
|
|
81
92
|
/**
|
|
82
93
|
* Removes the related event listeners from the canvas.
|
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
import type { NVL } from '@neo4j-nvl/base';
|
|
2
2
|
import { BaseInteraction } from './base';
|
|
3
|
+
/**
|
|
4
|
+
* Options for the zoom interaction handler to customize its behavior.
|
|
5
|
+
*/
|
|
6
|
+
export type ZoomInteractionOptions = {
|
|
7
|
+
/**
|
|
8
|
+
* If true, the interaction handler will not update the NVL instance directly.
|
|
9
|
+
* Instead, it will only call the onZoom callback with the calculated values,
|
|
10
|
+
* allowing the parent application to control when and how to apply the zoom update.
|
|
11
|
+
* @defaultValue false
|
|
12
|
+
*/
|
|
13
|
+
controlledZoom?: boolean;
|
|
14
|
+
};
|
|
3
15
|
/**
|
|
4
16
|
* Callbacks for the zoom interaction handler.
|
|
5
17
|
*/
|
|
@@ -8,20 +20,38 @@ export type ZoomInteractionCallbacks = {
|
|
|
8
20
|
* Called when the canvas is zoomed.
|
|
9
21
|
* @param zoomLevel - The zoom level.
|
|
10
22
|
* @param event - The original mouse wheel event.
|
|
23
|
+
* @deprecated Use {@link onZoomAndPan} instead.
|
|
11
24
|
*/
|
|
12
25
|
onZoom?: ((zoomLevel: number, event: WheelEvent) => void) | boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Called when the canvas is zoomed and panned.
|
|
28
|
+
* @param zoomLevel - The zoom level.
|
|
29
|
+
* @param panX - The new pan X coordinate.
|
|
30
|
+
* @param panY - The new pan Y coordinate.
|
|
31
|
+
* @param event - The original mouse wheel event.
|
|
32
|
+
*/
|
|
33
|
+
onZoomAndPan?: ((zoomLevel: number, panX: number, panY: number, event: WheelEvent) => void) | boolean;
|
|
13
34
|
};
|
|
14
35
|
/**
|
|
15
|
-
* Interaction handler for zooming the canvas, which is achieved by
|
|
36
|
+
* Interaction handler for zooming the canvas, which is achieved by:
|
|
37
|
+
* - Scrolling the mouse wheel on the canvas
|
|
38
|
+
* - Using trackpad pinch gestures (equivalent to Ctrl/Cmd + scroll)
|
|
16
39
|
*
|
|
17
40
|
* For examples, head to the {@link https://neo4j.com/docs/nvl/current/interaction-handlers/#_zoominteraction Zoom Interaction documentation page}.
|
|
18
41
|
*/
|
|
19
|
-
export declare class ZoomInteraction extends BaseInteraction<ZoomInteractionCallbacks,
|
|
42
|
+
export declare class ZoomInteraction extends BaseInteraction<ZoomInteractionCallbacks, ZoomInteractionOptions> {
|
|
43
|
+
private zoomLimits;
|
|
20
44
|
/**
|
|
21
45
|
* Creates a new instance of the zoom interaction handler.
|
|
22
46
|
* @param nvl - The NVL instance to attach the interaction handler to.
|
|
47
|
+
* @param options - Options to customize the zoom interaction behavior.
|
|
23
48
|
*/
|
|
24
|
-
constructor(nvl: NVL, options?:
|
|
49
|
+
constructor(nvl: NVL, options?: ZoomInteractionOptions);
|
|
50
|
+
/**
|
|
51
|
+
* The function to be called on mouse wheel event on the canvas.
|
|
52
|
+
* @param evt - The mouse wheel event.
|
|
53
|
+
*/
|
|
54
|
+
private handleWheel;
|
|
25
55
|
/**
|
|
26
56
|
* Throttled zoom function to avoid events happening too fast.
|
|
27
57
|
* @param event - The original mouse wheel event.
|
|
@@ -36,12 +66,6 @@ export declare class ZoomInteraction extends BaseInteraction<ZoomInteractionCall
|
|
|
36
66
|
* The throttle is set to 25ms.
|
|
37
67
|
*/
|
|
38
68
|
private throttledZoom;
|
|
39
|
-
/**
|
|
40
|
-
* The function to be called on mouse wheel event on the canvas.
|
|
41
|
-
* @param evt - The mouse wheel event.
|
|
42
|
-
* @remarks If the ctrl key or meta key is pressed, it does not zoom to avoid conflicts with default browser behavior.
|
|
43
|
-
*/
|
|
44
|
-
private handleWheel;
|
|
45
69
|
/**
|
|
46
70
|
* Removes the relevant event listeners from the canvas.
|
|
47
71
|
*/
|
|
@@ -2,19 +2,32 @@ import { throttle } from 'lodash';
|
|
|
2
2
|
import { BaseInteraction } from './base';
|
|
3
3
|
import { getCanvasCenterOffset } from './utils';
|
|
4
4
|
/**
|
|
5
|
-
* Interaction handler for zooming the canvas, which is achieved by
|
|
5
|
+
* Interaction handler for zooming the canvas, which is achieved by:
|
|
6
|
+
* - Scrolling the mouse wheel on the canvas
|
|
7
|
+
* - Using trackpad pinch gestures (equivalent to Ctrl/Cmd + scroll)
|
|
6
8
|
*
|
|
7
9
|
* For examples, head to the {@link https://neo4j.com/docs/nvl/current/interaction-handlers/#_zoominteraction Zoom Interaction documentation page}.
|
|
8
10
|
*/
|
|
9
11
|
export class ZoomInteraction extends BaseInteraction {
|
|
12
|
+
zoomLimits;
|
|
10
13
|
/**
|
|
11
14
|
* Creates a new instance of the zoom interaction handler.
|
|
12
15
|
* @param nvl - The NVL instance to attach the interaction handler to.
|
|
16
|
+
* @param options - Options to customize the zoom interaction behavior.
|
|
13
17
|
*/
|
|
14
18
|
constructor(nvl, options = {}) {
|
|
15
19
|
super(nvl, options);
|
|
20
|
+
this.zoomLimits = nvl.getZoomLimits();
|
|
16
21
|
this.addEventListener('wheel', this.handleWheel);
|
|
17
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* The function to be called on mouse wheel event on the canvas.
|
|
25
|
+
* @param evt - The mouse wheel event.
|
|
26
|
+
*/
|
|
27
|
+
handleWheel = (evt) => {
|
|
28
|
+
evt.preventDefault();
|
|
29
|
+
this.throttledZoom(evt);
|
|
30
|
+
};
|
|
18
31
|
/**
|
|
19
32
|
* Throttled zoom function to avoid events happening too fast.
|
|
20
33
|
* @param event - The original mouse wheel event.
|
|
@@ -31,27 +44,26 @@ export class ZoomInteraction extends BaseInteraction {
|
|
|
31
44
|
throttledZoom = throttle((event) => {
|
|
32
45
|
const zoom = this.nvlInstance.getScale();
|
|
33
46
|
const { x, y } = this.nvlInstance.getPan();
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
const
|
|
47
|
+
this.zoomLimits = this.nvlInstance.getZoomLimits();
|
|
48
|
+
const isPincehZoom = event.ctrlKey || event.metaKey;
|
|
49
|
+
const speedFactor = isPincehZoom ? 75 : 500;
|
|
50
|
+
const baseSpeed = event.deltaY / speedFactor;
|
|
51
|
+
const scaleFactor = zoom >= 1 ? baseSpeed * zoom : baseSpeed;
|
|
52
|
+
const newZoomTarget = zoom - scaleFactor * Math.min(1, zoom);
|
|
53
|
+
const zoomIsAtLimit = newZoomTarget > this.zoomLimits.maxZoom || newZoomTarget < this.zoomLimits.minZoom;
|
|
37
54
|
const offs = getCanvasCenterOffset(this.containerInstance, event);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
55
|
+
let newPanX = x;
|
|
56
|
+
let newPanY = y;
|
|
57
|
+
if (!zoomIsAtLimit) {
|
|
58
|
+
newPanX = x + (offs.x / zoom - offs.x / newZoomTarget);
|
|
59
|
+
newPanY = y + (offs.y / zoom - offs.y / newZoomTarget);
|
|
60
|
+
}
|
|
61
|
+
if (this.currentOptions.controlledZoom !== true) {
|
|
62
|
+
this.nvlInstance.setZoomAndPan(newZoomTarget, newPanX, newPanY);
|
|
63
|
+
}
|
|
41
64
|
this.callCallbackIfRegistered('onZoom', newZoomTarget, event);
|
|
65
|
+
this.callCallbackIfRegistered('onZoomAndPan', newZoomTarget, newPanX, newPanY, event);
|
|
42
66
|
}, 25, { leading: true });
|
|
43
|
-
/**
|
|
44
|
-
* The function to be called on mouse wheel event on the canvas.
|
|
45
|
-
* @param evt - The mouse wheel event.
|
|
46
|
-
* @remarks If the ctrl key or meta key is pressed, it does not zoom to avoid conflicts with default browser behavior.
|
|
47
|
-
*/
|
|
48
|
-
handleWheel = (evt) => {
|
|
49
|
-
if (evt.ctrlKey || evt.metaKey) {
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
evt.preventDefault();
|
|
53
|
-
this.throttledZoom(evt);
|
|
54
|
-
};
|
|
55
67
|
/**
|
|
56
68
|
* Removes the relevant event listeners from the canvas.
|
|
57
69
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@neo4j-nvl/interaction-handlers",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0-17f2ef68",
|
|
4
4
|
"license": "SEE LICENSE IN 'LICENSE.txt'",
|
|
5
5
|
"homepage": "https://neo4j.com/docs/nvl/current/",
|
|
6
6
|
"description": "Interaction handlers for the Neo4j Visualization Library",
|
|
@@ -27,13 +27,14 @@
|
|
|
27
27
|
"eslint": "yarn global:eslint ./src/"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@neo4j-nvl/base": "1.
|
|
31
|
-
"concaveman": "
|
|
32
|
-
"lodash": "4.
|
|
30
|
+
"@neo4j-nvl/base": "1.1.0-17f2ef68",
|
|
31
|
+
"concaveman": "1.2.1",
|
|
32
|
+
"lodash": "4.18.1"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@testing-library/jest-dom": "
|
|
35
|
+
"@testing-library/jest-dom": "5.17.0",
|
|
36
36
|
"@types/concaveman": "1.1.6",
|
|
37
|
-
"@types/lodash": "4.
|
|
38
|
-
}
|
|
37
|
+
"@types/lodash": "4.17.21"
|
|
38
|
+
},
|
|
39
|
+
"stableVersion": "1.1.0"
|
|
39
40
|
}
|