@pie-lib/plot 4.0.4-next.3 → 4.0.4-next.31

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.
Files changed (59) hide show
  1. package/dist/_virtual/_rolldown/runtime.js +20 -0
  2. package/dist/draggable.d.ts +13 -0
  3. package/dist/draggable.js +13 -0
  4. package/dist/graph-props.d.ts +22 -0
  5. package/dist/graph-props.js +29 -0
  6. package/dist/grid-draggable.d.ts +91 -0
  7. package/dist/grid-draggable.js +168 -0
  8. package/dist/index.d.ts +16 -0
  9. package/dist/index.js +8 -0
  10. package/dist/label.d.ts +30 -0
  11. package/dist/label.js +132 -0
  12. package/dist/node_modules/.bun/clsx@2.1.1/node_modules/clsx/dist/clsx.js +16 -0
  13. package/dist/node_modules/.bun/invariant@2.2.4/node_modules/invariant/browser.js +28 -0
  14. package/dist/node_modules/.bun/react-draggable@4.6.0_6dbf9a050bc9aadb/node_modules/react-draggable/build/cjs/chunk-D5BXCJ5G.js +503 -0
  15. package/dist/node_modules/.bun/react-draggable@4.6.0_6dbf9a050bc9aadb/node_modules/react-draggable/build/cjs/cjs.js +5 -0
  16. package/dist/root.d.ts +68 -0
  17. package/dist/root.js +302 -0
  18. package/dist/trig.d.ts +41 -0
  19. package/dist/trig.js +47 -0
  20. package/dist/types.d.ts +125 -0
  21. package/dist/types.js +46 -0
  22. package/dist/utils.d.ts +44 -0
  23. package/dist/utils.js +82 -0
  24. package/package.json +35 -25
  25. package/CHANGELOG.json +0 -17
  26. package/CHANGELOG.md +0 -838
  27. package/LICENSE.md +0 -5
  28. package/lib/draggable.js +0 -44
  29. package/lib/draggable.js.map +0 -1
  30. package/lib/graph-props.js +0 -46
  31. package/lib/graph-props.js.map +0 -1
  32. package/lib/grid-draggable.js +0 -361
  33. package/lib/grid-draggable.js.map +0 -1
  34. package/lib/index.js +0 -44
  35. package/lib/index.js.map +0 -1
  36. package/lib/label.js +0 -173
  37. package/lib/label.js.map +0 -1
  38. package/lib/root.js +0 -474
  39. package/lib/root.js.map +0 -1
  40. package/lib/trig.js +0 -149
  41. package/lib/trig.js.map +0 -1
  42. package/lib/types.js +0 -40
  43. package/lib/types.js.map +0 -1
  44. package/lib/utils.js +0 -165
  45. package/lib/utils.js.map +0 -1
  46. package/src/__tests__/draggable.test.jsx +0 -41
  47. package/src/__tests__/grid-draggable.test.jsx +0 -487
  48. package/src/__tests__/root.test.jsx +0 -277
  49. package/src/__tests__/trig.test.js +0 -163
  50. package/src/__tests__/utils.test.js +0 -229
  51. package/src/draggable.jsx +0 -11
  52. package/src/graph-props.js +0 -34
  53. package/src/grid-draggable.jsx +0 -332
  54. package/src/index.js +0 -9
  55. package/src/label.jsx +0 -199
  56. package/src/root.jsx +0 -485
  57. package/src/trig.js +0 -151
  58. package/src/types.js +0 -41
  59. package/src/utils.js +0 -167
@@ -1,487 +0,0 @@
1
- import { render } from '@testing-library/react';
2
- import React from 'react';
3
- import { gridDraggable } from '../grid-draggable';
4
- import { getDelta } from '../utils';
5
- import { pointer } from 'd3-selection';
6
-
7
- jest.mock('d3-selection', () => ({
8
- pointer: jest.fn().mockReturnValue([0, 0]),
9
- }));
10
-
11
- let mockDraggableCoreProps;
12
- jest.mock('../draggable', () => ({
13
- DraggableCore: (props) => {
14
- // Store the props so tests can call the handlers
15
- mockDraggableCoreProps = props;
16
- return props.children;
17
- },
18
- }));
19
-
20
- // Reusable mock start event with a target that supports addEventListener/removeEventListener
21
- const mockStartEvent = (overrides = {}) => ({
22
- clientX: 0,
23
- clientY: 0,
24
- target: { addEventListener: jest.fn(), removeEventListener: jest.fn() },
25
- ...overrides,
26
- });
27
-
28
- jest.mock('../utils', () => ({
29
- getDelta: jest.fn(),
30
- }));
31
-
32
- const xyFn = () => {
33
- const out = jest.fn((n) => n);
34
- out.invert = jest.fn((n) => n);
35
- return out;
36
- };
37
-
38
- const getGraphProps = () => ({
39
- scale: {
40
- x: xyFn(),
41
- y: xyFn(),
42
- },
43
- snap: {
44
- x: xyFn(),
45
- y: xyFn(),
46
- },
47
- domain: {
48
- min: 0,
49
- max: 1,
50
- step: 1,
51
- },
52
- range: {
53
- min: 0,
54
- max: 1,
55
- step: 1,
56
- },
57
- size: {
58
- width: 500,
59
- height: 500,
60
- },
61
- getRootNode: () => ({
62
- ownerSVGElement: null,
63
- getBoundingClientRect: () => ({
64
- left: 0,
65
- top: 0,
66
- right: 100,
67
- bottom: 100,
68
- }),
69
- }),
70
- });
71
-
72
- describe('gridDraggable', () => {
73
- let defaultOptions;
74
- let defaultProps;
75
-
76
- beforeEach(() => {
77
- mockDraggableCoreProps = null;
78
- defaultOptions = {
79
- anchorPoint: jest.fn().mockReturnValue({ x: 0, y: 0 }),
80
- bounds: jest.fn().mockReturnValue({ left: 0, top: 0, bottom: 0, right: 0 }),
81
- fromDelta: jest.fn(),
82
- };
83
-
84
- defaultProps = {
85
- graphProps: getGraphProps(),
86
- };
87
- });
88
-
89
- it('renders regular component', () => {
90
- const Comp = gridDraggable(defaultOptions)(() => <div>Test</div>);
91
- const { container } = render(<Comp {...defaultProps} />);
92
- expect(container.firstChild).toBeInTheDocument();
93
- });
94
-
95
- it('renders with decimal domain and range', () => {
96
- const props = {
97
- ...defaultProps,
98
- graphProps: {
99
- ...getGraphProps(),
100
- domain: {
101
- min: -1.5,
102
- max: 1.6,
103
- step: 0.3,
104
- },
105
- range: {
106
- min: -2,
107
- max: 3,
108
- step: 0.2,
109
- },
110
- },
111
- };
112
- const Comp = gridDraggable(defaultOptions)(() => <div>Test</div>);
113
- const { container } = render(<Comp {...props} />);
114
- expect(container.firstChild).toBeInTheDocument();
115
- });
116
-
117
- describe('logic', () => {
118
- describe('grid calculation', () => {
119
- it('passes correct grid to DraggableCore based on domain and range step', () => {
120
- const Comp = gridDraggable(defaultOptions)(() => <div>Test</div>);
121
- render(<Comp {...defaultProps} />);
122
-
123
- // Grid is calculated as: scale.x(domain.step) - scale.x(0)
124
- // With our mock xyFn that returns n, this is: step - 0 = step
125
- expect(mockDraggableCoreProps.grid).toEqual([1, 1]);
126
- });
127
-
128
- it('calculates grid with decimal steps', () => {
129
- const props = {
130
- ...defaultProps,
131
- graphProps: {
132
- ...getGraphProps(),
133
- domain: { min: -1.5, max: 1.6, step: 0.3 },
134
- range: { min: -2, max: 3, step: 0.2 },
135
- },
136
- };
137
- const Comp = gridDraggable(defaultOptions)(() => <div>Test</div>);
138
- render(<Comp {...props} />);
139
-
140
- expect(mockDraggableCoreProps.grid).toEqual([0.3, 0.2]);
141
- });
142
- });
143
-
144
- describe('onStart', () => {
145
- it('calls onDragStart handler when drag starts', () => {
146
- const onDragStart = jest.fn();
147
- const props = { ...defaultProps, onDragStart };
148
- const Comp = gridDraggable(defaultOptions)(() => <div>Test</div>);
149
- render(<Comp {...props} />);
150
-
151
- // Simulate drag start
152
- mockDraggableCoreProps.onStart(mockStartEvent({ clientX: 100, clientY: 100 }));
153
-
154
- expect(onDragStart).toHaveBeenCalled();
155
- });
156
- });
157
-
158
- describe('onDrag', () => {
159
- it('calls onDrag callback with result from fromDelta', () => {
160
- const onDrag = jest.fn();
161
- const fromDelta = jest.fn().mockReturnValue('delta-result');
162
- const bounds = jest.fn().mockReturnValue({ left: -100, top: -100, bottom: 100, right: 100 });
163
- const options = { ...defaultOptions, fromDelta, bounds };
164
- const props = { ...defaultProps, onDrag };
165
-
166
- const Comp = gridDraggable(options)(() => <div>Test</div>);
167
- render(<Comp {...props} />);
168
-
169
- // Set up drag start state
170
- mockDraggableCoreProps.onStart(mockStartEvent());
171
-
172
- // Simulate drag
173
- mockDraggableCoreProps.onDrag({}, { deltaX: 10, deltaY: 10 });
174
-
175
- expect(fromDelta).toHaveBeenCalled();
176
- expect(onDrag).toHaveBeenCalledWith('delta-result');
177
- });
178
-
179
- it('does not call onDrag when no onDrag handler is provided', () => {
180
- const fromDelta = jest.fn();
181
- const options = { ...defaultOptions, fromDelta };
182
- const Comp = gridDraggable(options)(() => <div>Test</div>);
183
- render(<Comp {...defaultProps} />);
184
-
185
- mockDraggableCoreProps.onStart(mockStartEvent());
186
- mockDraggableCoreProps.onDrag({}, { deltaX: 10, deltaY: 10 });
187
-
188
- expect(fromDelta).not.toHaveBeenCalled();
189
- });
190
-
191
- describe('bounds checking', () => {
192
- it('does not call onDrag when dragging left beyond left bound', () => {
193
- const onDrag = jest.fn();
194
- const bounds = jest.fn().mockReturnValue({ left: 0, top: 0, bottom: 0, right: 0 });
195
- const fromDelta = jest.fn().mockReturnValue('result');
196
- const options = { ...defaultOptions, bounds, fromDelta };
197
- const props = { ...defaultProps, onDrag };
198
-
199
- const Comp = gridDraggable(options)(() => <div>Test</div>);
200
- render(<Comp {...props} />);
201
-
202
- mockDraggableCoreProps.onStart(mockStartEvent());
203
- // deltaX < 0 and deltaX < scaled bounds.left (0), so -10 < 0 triggers early return
204
- mockDraggableCoreProps.onDrag({}, { deltaX: -10, deltaY: 0 });
205
-
206
- expect(onDrag).not.toHaveBeenCalled();
207
- });
208
-
209
- it('does not call onDrag when dragging right beyond right bound', () => {
210
- const onDrag = jest.fn();
211
- const bounds = jest.fn().mockReturnValue({ left: 0, top: 0, bottom: 0, right: 0 });
212
- const fromDelta = jest.fn().mockReturnValue('result');
213
- const options = { ...defaultOptions, bounds, fromDelta };
214
- const props = { ...defaultProps, onDrag };
215
-
216
- const Comp = gridDraggable(options)(() => <div>Test</div>);
217
- render(<Comp {...props} />);
218
-
219
- mockDraggableCoreProps.onStart(mockStartEvent());
220
- // deltaX > 0 and deltaX > scaled bounds.right (0), so 10 > 0 triggers early return
221
- mockDraggableCoreProps.onDrag({}, { deltaX: 10, deltaY: 0 });
222
-
223
- expect(onDrag).not.toHaveBeenCalled();
224
- });
225
-
226
- it('does not call onDrag when dragging up beyond top bound', () => {
227
- const onDrag = jest.fn();
228
- const bounds = jest.fn().mockReturnValue({ left: -100, top: 0, bottom: 0, right: 100 });
229
- const fromDelta = jest.fn().mockReturnValue('result');
230
- const options = { ...defaultOptions, bounds, fromDelta };
231
- const props = { ...defaultProps, onDrag };
232
-
233
- const Comp = gridDraggable(options)(() => <div>Test</div>);
234
- render(<Comp {...props} />);
235
-
236
- mockDraggableCoreProps.onStart(mockStartEvent());
237
- // deltaY < 0 and deltaY < scaled bounds.top (0), so -10 < 0 triggers early return
238
- mockDraggableCoreProps.onDrag({}, { deltaX: 0, deltaY: -10 });
239
-
240
- expect(onDrag).not.toHaveBeenCalled();
241
- });
242
-
243
- it('does not call onDrag when dragging down beyond bottom bound', () => {
244
- const onDrag = jest.fn();
245
- const bounds = jest.fn().mockReturnValue({ left: -100, top: -100, bottom: 0, right: 100 });
246
- const fromDelta = jest.fn().mockReturnValue('result');
247
- const options = { ...defaultOptions, bounds, fromDelta };
248
- const props = { ...defaultProps, onDrag };
249
-
250
- const Comp = gridDraggable(options)(() => <div>Test</div>);
251
- render(<Comp {...props} />);
252
-
253
- mockDraggableCoreProps.onStart(mockStartEvent());
254
- // deltaY > 0 and deltaY > scaled bounds.bottom (0), so 10 > 0 triggers early return
255
- mockDraggableCoreProps.onDrag({}, { deltaX: 0, deltaY: 10 });
256
-
257
- expect(onDrag).not.toHaveBeenCalled();
258
- });
259
-
260
- it('calls onDrag when dragging within bounds', () => {
261
- const onDrag = jest.fn();
262
- const bounds = jest.fn().mockReturnValue({ left: -100, top: -100, bottom: 100, right: 100 });
263
- const fromDelta = jest.fn().mockReturnValue('result');
264
- const options = { ...defaultOptions, bounds, fromDelta };
265
- const props = { ...defaultProps, onDrag };
266
-
267
- const Comp = gridDraggable(options)(() => <div>Test</div>);
268
- render(<Comp {...props} />);
269
-
270
- mockDraggableCoreProps.onStart(mockStartEvent());
271
- // All bound checks pass: deltaX (-10) is NOT < bounds.left (-100) and NOT > bounds.right (100)
272
- // Similarly for deltaY
273
- mockDraggableCoreProps.onDrag({}, { deltaX: -10, deltaY: 10 });
274
-
275
- expect(onDrag).toHaveBeenCalled();
276
- });
277
- });
278
-
279
- describe('skipDragOutsideOfBounds', () => {
280
- it('skips drag when moving left and x is below domain.min', () => {
281
- const onDrag = jest.fn();
282
- const graphProps = {
283
- ...getGraphProps(),
284
- domain: { min: -5, max: 5, step: 1 },
285
- range: { min: -5, max: 5, step: 1 },
286
- };
287
- graphProps.scale.x.invert = jest.fn().mockReturnValue(-6); // Below min
288
-
289
- const props = { ...defaultProps, graphProps, onDrag };
290
- const bounds = jest.fn().mockReturnValue({ left: 100, top: 100, bottom: 100, right: 100 });
291
- const options = { ...defaultOptions, bounds };
292
-
293
- const Comp = gridDraggable(options)(() => <div>Test</div>);
294
- render(<Comp {...props} />);
295
-
296
- mockDraggableCoreProps.onStart(mockStartEvent());
297
- mockDraggableCoreProps.onDrag({}, { deltaX: 1, deltaY: 0 });
298
-
299
- expect(onDrag).not.toHaveBeenCalled();
300
- });
301
-
302
- it('skips drag when moving right and x is above domain.max', () => {
303
- const onDrag = jest.fn();
304
- const graphProps = {
305
- ...getGraphProps(),
306
- domain: { min: -5, max: 5, step: 1 },
307
- range: { min: -5, max: 5, step: 1 },
308
- };
309
- graphProps.scale.x.invert = jest.fn().mockReturnValue(6); // Above max
310
-
311
- const props = { ...defaultProps, graphProps, onDrag };
312
- const bounds = jest.fn().mockReturnValue({ left: 100, top: 100, bottom: 100, right: 100 });
313
- const options = { ...defaultOptions, bounds };
314
-
315
- const Comp = gridDraggable(options)(() => <div>Test</div>);
316
- render(<Comp {...props} />);
317
-
318
- mockDraggableCoreProps.onStart(mockStartEvent());
319
- mockDraggableCoreProps.onDrag({}, { deltaX: -1, deltaY: 0 });
320
-
321
- expect(onDrag).not.toHaveBeenCalled();
322
- });
323
-
324
- it('skips drag when moving up and y is above range.max', () => {
325
- const onDrag = jest.fn();
326
- const graphProps = {
327
- ...getGraphProps(),
328
- domain: { min: -5, max: 5, step: 1 },
329
- range: { min: -5, max: 5, step: 1 },
330
- };
331
- graphProps.scale.y.invert = jest.fn().mockReturnValue(6); // Above max
332
-
333
- const props = { ...defaultProps, graphProps, onDrag };
334
- const bounds = jest.fn().mockReturnValue({ left: 100, top: 100, bottom: 100, right: 100 });
335
- const options = { ...defaultOptions, bounds };
336
-
337
- const Comp = gridDraggable(options)(() => <div>Test</div>);
338
- render(<Comp {...props} />);
339
-
340
- mockDraggableCoreProps.onStart(mockStartEvent());
341
- mockDraggableCoreProps.onDrag({}, { deltaX: 0, deltaY: 1 });
342
-
343
- expect(onDrag).not.toHaveBeenCalled();
344
- });
345
-
346
- it('skips drag when moving down and y is below range.min', () => {
347
- const onDrag = jest.fn();
348
- const graphProps = {
349
- ...getGraphProps(),
350
- domain: { min: -5, max: 5, step: 1 },
351
- range: { min: -5, max: 5, step: 1 },
352
- };
353
- graphProps.scale.y.invert = jest.fn().mockReturnValue(-6); // Below min
354
-
355
- const props = { ...defaultProps, graphProps, onDrag };
356
- const bounds = jest.fn().mockReturnValue({ left: 100, top: 100, bottom: 100, right: 100 });
357
- const options = { ...defaultOptions, bounds };
358
-
359
- const Comp = gridDraggable(options)(() => <div>Test</div>);
360
- render(<Comp {...props} />);
361
-
362
- mockDraggableCoreProps.onStart(mockStartEvent());
363
- mockDraggableCoreProps.onDrag({}, { deltaX: 0, deltaY: -1 });
364
-
365
- expect(onDrag).not.toHaveBeenCalled();
366
- });
367
-
368
- it('allows drag when within domain and range', () => {
369
- const onDrag = jest.fn();
370
- const graphProps = {
371
- ...getGraphProps(),
372
- domain: { min: -5, max: 5, step: 1 },
373
- range: { min: -5, max: 5, step: 1 },
374
- };
375
- graphProps.scale.x.invert = jest.fn().mockReturnValue(3); // Within bounds
376
- graphProps.scale.y.invert = jest.fn().mockReturnValue(2); // Within bounds
377
-
378
- const props = { ...defaultProps, graphProps, onDrag };
379
- const bounds = jest.fn().mockReturnValue({ left: -100, top: -100, bottom: 100, right: 100 });
380
- const options = { ...defaultOptions, bounds };
381
-
382
- const Comp = gridDraggable(options)(() => <div>Test</div>);
383
- render(<Comp {...props} />);
384
-
385
- mockDraggableCoreProps.onStart(mockStartEvent());
386
- mockDraggableCoreProps.onDrag({}, { deltaX: 1, deltaY: -1 });
387
-
388
- expect(onDrag).toHaveBeenCalled();
389
- });
390
- });
391
- });
392
-
393
- describe('getDelta and applyDelta', () => {
394
- it('calls utils.getDelta when processing drag', () => {
395
- getDelta.mockClear();
396
- const onDrag = jest.fn();
397
- const props = { ...defaultProps, onDrag };
398
- const bounds = jest.fn().mockReturnValue({ left: -100, top: -100, bottom: 100, right: 100 });
399
- const options = { ...defaultOptions, bounds };
400
-
401
- const Comp = gridDraggable(options)(() => <div>Test</div>);
402
- render(<Comp {...props} />);
403
-
404
- mockDraggableCoreProps.onStart(mockStartEvent());
405
- mockDraggableCoreProps.onDrag({}, { deltaX: 10, deltaY: 10 });
406
-
407
- expect(getDelta).toHaveBeenCalled();
408
- });
409
-
410
- it('calls fromDelta with result from getDelta', () => {
411
- const fromDelta = jest.fn();
412
- getDelta.mockReturnValue({ x: 5, y: 5 });
413
- const bounds = jest.fn().mockReturnValue({ left: -100, top: -100, bottom: 100, right: 100 });
414
- const options = { ...defaultOptions, fromDelta, bounds };
415
- const props = { ...defaultProps, onDrag: jest.fn() };
416
-
417
- const Comp = gridDraggable(options)(() => <div>Test</div>);
418
- render(<Comp {...props} />);
419
-
420
- mockDraggableCoreProps.onStart(mockStartEvent());
421
- mockDraggableCoreProps.onDrag({}, { deltaX: 10, deltaY: 10 });
422
-
423
- expect(fromDelta).toHaveBeenCalled();
424
- });
425
- });
426
-
427
- describe('onStop', () => {
428
- it('calls onDragStop when drag stops', () => {
429
- const onDragStop = jest.fn();
430
- const props = { ...defaultProps, onDragStop };
431
-
432
- const Comp = gridDraggable(defaultOptions)(() => <div>Test</div>);
433
- render(<Comp {...props} />);
434
-
435
- // Start to set up state
436
- mockDraggableCoreProps.onStart(mockStartEvent());
437
-
438
- // Stop with large movement (not tiny)
439
- mockDraggableCoreProps.onStop({ clientX: 100, clientY: 100, stopPropagation: jest.fn() }, {});
440
-
441
- expect(onDragStop).toHaveBeenCalled();
442
- });
443
-
444
- it('calls onClick instead of onDragStop when movement is tiny', () => {
445
- const onClick = jest.fn();
446
- const onDragStop = jest.fn();
447
- const props = { ...defaultProps, onClick, onDragStop };
448
-
449
- pointer.mockReturnValue([0, 0]);
450
-
451
- const Comp = gridDraggable(defaultOptions)(() => <div>Test</div>);
452
- render(<Comp {...props} />);
453
-
454
- // Start and stop at almost the same position (tiny movement)
455
- // Grid is 1x1, tiny threshold is grid/10 = 0.1
456
- mockDraggableCoreProps.onStart(mockStartEvent());
457
- mockDraggableCoreProps.onStop({ clientX: 0.05, clientY: 0.05, target: {}, stopPropagation: jest.fn() }, {});
458
-
459
- expect(onClick).toHaveBeenCalledWith({ x: 0, y: 0 });
460
- });
461
-
462
- it('calls onClick with snapped coordinates', () => {
463
- const onClick = jest.fn();
464
- const props = { ...defaultProps, onClick };
465
- const graphProps = getGraphProps();
466
- graphProps.scale.x.invert = jest.fn().mockReturnValue(1.7);
467
- graphProps.scale.y.invert = jest.fn().mockReturnValue(2.3);
468
- graphProps.snap.x = jest.fn().mockReturnValue(2);
469
- graphProps.snap.y = jest.fn().mockReturnValue(2);
470
-
471
- pointer.mockReturnValue([1.7, 2.3]);
472
-
473
- const propsWithGraphProps = { ...props, graphProps };
474
- const Comp = gridDraggable(defaultOptions)(() => <div>Test</div>);
475
- render(<Comp {...propsWithGraphProps} />);
476
-
477
- // Start and stop at almost the same position (tiny movement)
478
- mockDraggableCoreProps.onStart(mockStartEvent());
479
- mockDraggableCoreProps.onStop({ clientX: 0.05, clientY: 0.05, target: {}, stopPropagation: jest.fn() }, {});
480
-
481
- expect(graphProps.snap.x).toHaveBeenCalledWith(1.7);
482
- expect(graphProps.snap.y).toHaveBeenCalledWith(2.3);
483
- expect(onClick).toHaveBeenCalledWith({ x: 2, y: 2 });
484
- });
485
- });
486
- });
487
- });