@ruckc/truss-chart 1.26.0 → 1.27.0

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.
@@ -1,5 +1,6 @@
1
1
  <script lang="ts">
2
2
  import type { Snippet } from 'svelte';
3
+ import { startDrag } from '@ruckc/truss-ui';
3
4
  import ChartFrame from './ChartFrame.svelte';
4
5
  import { watchFill } from './fill.js';
5
6
  import ColorScale from './ColorScale.svelte';
@@ -304,26 +305,33 @@
304
305
  // ── pointers: drag pans, two pointers pinch
305
306
  const ptrs = new Map<number, { x: number; y: number }>();
306
307
  const spread = () => { const [a, b] = [...ptrs.values()]; return Math.hypot(a.x - b.x, a.y - b.y); };
308
+ const stops = new Map<number, () => void>(); // the running drags, abandoned if the chart unmounts mid-drag
309
+ $effect(() => () => { for (const stop of stops.values()) stop(); stops.clear(); });
307
310
  function down(e: PointerEvent) {
308
- ptrs.set(e.pointerId, { x: e.clientX, y: e.clientY });
311
+ const id = e.pointerId;
312
+ const stop = startDrag(e, {
313
+ cursor: 'grabbing',
314
+ move: (ev) => {
315
+ const prev = ptrs.get(id)!;
316
+ if (ptrs.size === 1) {
317
+ pan(-(ev.clientX - prev.x) / pw, (ev.clientY - prev.y) / ph);
318
+ ptrs.set(id, { x: ev.clientX, y: ev.clientY });
319
+ } else {
320
+ const before = spread();
321
+ ptrs.set(id, { x: ev.clientX, y: ev.clientY });
322
+ const [a, b] = [...ptrs.values()];
323
+ const r = box!.getBoundingClientRect();
324
+ zoomAt((a.x + b.x) / 2 - r.left, (a.y + b.y) / 2 - r.top, spread() / (before || 1));
325
+ }
326
+ },
327
+ end: () => { ptrs.delete(id); stops.delete(id); }
328
+ });
329
+ if (!stop) return;
330
+ stops.set(id, stop);
331
+ ptrs.set(id, { x: e.clientX, y: e.clientY });
309
332
  hover = null;
310
- try { (e.currentTarget as HTMLElement).setPointerCapture(e.pointerId); } catch { /* synthetic pointer */ }
311
- }
312
- function move(e: PointerEvent) {
313
- const prev = ptrs.get(e.pointerId);
314
- if (!prev) return hit(e);
315
- if (ptrs.size === 1) {
316
- pan(-(e.clientX - prev.x) / pw, (e.clientY - prev.y) / ph);
317
- ptrs.set(e.pointerId, { x: e.clientX, y: e.clientY });
318
- } else {
319
- const before = spread();
320
- ptrs.set(e.pointerId, { x: e.clientX, y: e.clientY });
321
- const [a, b] = [...ptrs.values()];
322
- const r = box!.getBoundingClientRect();
323
- zoomAt((a.x + b.x) / 2 - r.left, (a.y + b.y) / 2 - r.top, spread() / (before || 1));
324
- }
325
333
  }
326
- const up = (e: PointerEvent) => { ptrs.delete(e.pointerId); };
334
+ const move = (e: PointerEvent) => { if (!ptrs.has(e.pointerId)) hit(e); };
327
335
 
328
336
  // ── hit testing: nearest marker when zoomed in, bin count when zoomed out
329
337
  function hit(e: PointerEvent) {
@@ -412,7 +420,7 @@
412
420
  <div
413
421
  class="dc" role="group" tabindex="0" bind:this={box} bind:clientWidth={cw}
414
422
  aria-label="{title ?? 'Density chart'}, {fmtValue(total)} points. Drag or arrow keys pan, wheel or plus and minus zoom, 0 resets the view."
415
- onpointerdown={down} onpointermove={move} onpointerup={up} onpointercancel={up}
423
+ onpointerdown={down} onpointermove={move}
416
424
  onpointerleave={() => (hover = null)} onblur={() => (hover = null)} onkeydown={key}
417
425
  ondblclick={(e) => { const r = box!.getBoundingClientRect(); zoomAt(e.clientX - r.left, e.clientY - r.top, 2); }}
418
426
  >
@@ -1,4 +1,5 @@
1
1
  <script lang="ts">
2
+ import { startDrag } from '@ruckc/truss-ui';
2
3
  import ChartFrame from './ChartFrame.svelte';
3
4
  import { watchFill } from './fill.js';
4
5
  import Tooltip from './Tooltip.svelte';
@@ -37,6 +38,8 @@
37
38
 
38
39
  let hover = $state<number | null>(null);
39
40
  let drag = $state<{ a: number; b: number } | null>(null);
41
+ let stop: (() => void) | undefined; // the running brush, abandoned if the chart unmounts mid-drag
42
+ $effect(() => () => stop?.());
40
43
 
41
44
  const clean = $derived(values.filter((v) => Number.isFinite(v)));
42
45
  const want = $derived(bins === 'auto' ? Math.min(60, Math.max(5, Math.ceil(Math.log2(Math.max(clean.length, 1)) + 1))) : bins);
@@ -130,18 +133,25 @@
130
133
  onpointermove={(e) => {
131
134
  const px = e.clientX - e.currentTarget.getBoundingClientRect().left + m.left;
132
135
  hover = Math.min(nb - 1, Math.max(0, Math.floor((toVal(px) - grid.min) / step)));
133
- if (drag) drag = { ...drag, b: Math.min(grid.max, Math.max(grid.min, toVal(px))) };
134
136
  }}
135
137
  onpointerdown={(e) => {
136
- e.currentTarget.setPointerCapture(e.pointerId);
137
- const v = toVal(e.clientX - e.currentTarget.getBoundingClientRect().left + m.left);
138
- drag = { a: v, b: v };
139
- }}
140
- onpointerup={() => {
141
- if (!drag) return;
142
- const lo = Math.min(drag.a, drag.b), hi = Math.max(drag.a, drag.b);
143
- set(Math.abs(X(hi) - X(lo)) < 4 ? null : [snapLo(lo), snapHi(hi)]);
144
- drag = null;
138
+ if (stop) return; // a second finger does not start a second brush
139
+ const rect = e.currentTarget;
140
+ const at = (ev: PointerEvent) => toVal(ev.clientX - rect.getBoundingClientRect().left + m.left);
141
+ const v = at(e);
142
+ stop = startDrag(e, {
143
+ cursor: 'col-resize',
144
+ move: (ev) => { drag = { a: v, b: Math.min(grid.max, Math.max(grid.min, at(ev))) }; },
145
+ end: (_, cancelled) => {
146
+ stop = undefined;
147
+ const d = drag;
148
+ drag = null;
149
+ if (!d || cancelled) return;
150
+ const lo = Math.min(d.a, d.b), hi = Math.max(d.a, d.b);
151
+ set(Math.abs(X(hi) - X(lo)) < 4 ? null : [snapLo(lo), snapHi(hi)]);
152
+ }
153
+ });
154
+ if (stop) drag = { a: v, b: v };
145
155
  }}
146
156
  />
147
157
  </svg>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruckc/truss-chart",
3
- "version": "1.26.0",
3
+ "version": "1.27.0",
4
4
  "description": "Zero-dependency SVG charts for Truss UI",
5
5
  "type": "module",
6
6
  "license": "MIT",