svelteplot 0.6.0-pr-263.1 → 0.6.0-pr-262.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -33,14 +33,17 @@
33
33
  onbrushend?: (evt: BrushEvent) => void;
34
34
  onbrush?: (evt: BrushEvent) => void;
35
35
  }
36
- import { getContext } from 'svelte';
36
+ import { getContext, untrack } from 'svelte';
37
37
  import Rect from './Rect.svelte';
38
38
  import type { BaseMarkProps, DataRecord, PlotContext } from '../types/index.js';
39
39
  import { clientToLayerCoordinates } from './helpers/events.js';
40
40
  import Frame from './Frame.svelte';
41
41
  import { getPlotDefaults } from '../hooks/plotDefaults.js';
42
42
 
43
- let { brush = $bindable({ enabled: false }), ...markProps }: BrushMarkProps = $props();
43
+ let { brush: brushExternal = $bindable({ enabled: false }), ...markProps }: BrushMarkProps =
44
+ $props();
45
+
46
+ let brush = $state<Brush>(brushExternal);
44
47
 
45
48
  const DEFAULTS = {
46
49
  stroke: 'currentColor',
@@ -179,6 +182,7 @@
179
182
  );
180
183
 
181
184
  $effect(() => {
185
+ // update brush prop when internal state changes
182
186
  brush.x1 =
183
187
  !brush.enabled || limitDimension === 'y'
184
188
  ? undefined
@@ -197,6 +201,39 @@
197
201
  : constrain(y1 > y2 ? y1 : y2, yDomain);
198
202
  });
199
203
 
204
+ // update internal state when external brush prop changes
205
+ $effect(() => {
206
+ const brushInt = untrack(() => brush);
207
+ if (!brushIdentical(brushInt, brushExternal)) {
208
+ brush = brushExternal;
209
+ // also keep internal x1,x2,y1,y2 in sync
210
+ x1 = brush.x1 as Date | number;
211
+ x2 = brush.x2 as Date | number;
212
+ y1 = brush.y1 as Date | number;
213
+ y2 = brush.y2 as Date | number;
214
+ onbrush?.({ brush } as BrushEvent);
215
+ }
216
+ });
217
+
218
+ // update external brush when internal state changes
219
+ $effect(() => {
220
+ const brushExt = untrack(() => brushExternal);
221
+ if (!brushIdentical(brush, brushExt)) {
222
+ // avoid cycles
223
+ brushExternal = brush;
224
+ }
225
+ });
226
+
227
+ function brushIdentical(b1: Brush, b2: Brush) {
228
+ return (
229
+ b1.enabled === b2.enabled &&
230
+ b1.x1 === b2.x1 &&
231
+ b1.x2 === b2.x2 &&
232
+ b1.y1 === b2.y1 &&
233
+ b1.y2 === b2.y2
234
+ );
235
+ }
236
+
200
237
  function constrain<T extends number | Date>(x: T, extent: [typeof x, typeof x]) {
201
238
  const minE = extent[0] < extent[1] ? extent[0] : extent[1];
202
239
  const maxE = extent[0] > extent[1] ? extent[0] : extent[1];
@@ -242,8 +279,12 @@
242
279
  } else {
243
280
  // draw new brush selection
244
281
  action = 'draw';
245
- x1 = x2 = xScaleFn.invert(dragStart[0]);
246
- y1 = y2 = yScaleFn.invert(dragStart[1]);
282
+ if (typeof xScaleFn.invert === 'function' && limitDimension !== 'y') {
283
+ x1 = x2 = xScaleFn.invert(dragStart[0]);
284
+ }
285
+ if (typeof yScaleFn.invert === 'function' && limitDimension !== 'x') {
286
+ y1 = y2 = yScaleFn.invert(dragStart[1]);
287
+ }
247
288
  }
248
289
  onbrushstart?.({ ...e, brush });
249
290
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelteplot",
3
- "version": "0.6.0-pr-263.1",
3
+ "version": "0.6.0-pr-262.2",
4
4
  "license": "ISC",
5
5
  "author": {
6
6
  "name": "Gregor Aisch",