✨ Innovative and open-source visualization application that transforms various data formats, such as JSON, YAML, XML and CSV into interactive graphs.
feat: support shift wheel horizontal pan (#595)
AlinAndrei3c9af69e23c6
diff --git a/packages/jsoncrack-react/src/JSONCrackComponent.tsx b/packages/jsoncrack-react/src/JSONCrackComponent.tsxindex 3cd01faa..718ce023 100644--- a/packages/jsoncrack-react/src/JSONCrackComponent.tsx+++ b/packages/jsoncrack-react/src/JSONCrackComponent.tsx@@ -25,6 +25,7 @@ import {buildEdgeTargetMap,fitGraphToViewPort,focusRootNode,+ getShiftWheelHorizontalPanDelta,parseJsonGraph,setCanvasDragging,setViewPortZoom,@@ -219,6 +220,30 @@ export const JSONCrack = forwardRef<JSONCrackRef, JSONCrackProps>(return () => observer.disconnect();}, [viewPort]);+ useEffect(() => {+ if (!viewPort) return;+ const space = containerRef.current?.querySelector<HTMLElement>(".jsoncrack-space");+ if (!space) return;++ const handleShiftWheel = (event: WheelEvent) => {+ const delta = getShiftWheelHorizontalPanDelta(+ event,+ space.clientWidth || window.innerWidth+ );+ if (delta === null) return;++ const camera = viewPort.camera;+ if (!camera) return;++ event.preventDefault();+ event.stopImmediatePropagation();+ camera.moveByInClientSpace(delta, 0, 0);+ };++ space.addEventListener("wheel", handleShiftWheel, { capture: true, passive: false });+ return () => space.removeEventListener("wheel", handleShiftWheel, { capture: true });+ }, [viewPort]);+const viewPortApi = useMemo(() => ({zoomIn: () => adjustViewPortZoom(viewPort, 0.1),diff --git a/packages/jsoncrack-react/src/__tests__/canvasHelpers.test.ts b/packages/jsoncrack-react/src/__tests__/canvasHelpers.test.tsindex 2fa6253d..0f5b95e0 100644--- a/packages/jsoncrack-react/src/__tests__/canvasHelpers.test.ts+++ b/packages/jsoncrack-react/src/__tests__/canvasHelpers.test.ts@@ -3,6 +3,7 @@ import {buildCanvasClassName,buildCanvasStyle,buildEdgeTargetMap,+ getShiftWheelHorizontalPanDelta,parseJsonGraph,toJsonText,} from "../canvasHelpers";@@ -143,3 +144,38 @@ describe("buildEdgeTargetMap", () => {expect(buildEdgeTargetMap([]).size).toBe(0);});});++describe("getShiftWheelHorizontalPanDelta", () => {+ it("ignores wheel events without the shift key", () => {+ expect(+ getShiftWheelHorizontalPanDelta({ shiftKey: false, deltaX: 0, deltaY: 80, deltaMode: 0 }, 900)+ ).toBeNull();+ });++ it("ignores shift wheel events without movement", () => {+ expect(+ getShiftWheelHorizontalPanDelta({ shiftKey: true, deltaX: 0, deltaY: 0, deltaMode: 0 }, 900)+ ).toBeNull();+ });++ it("maps vertical shift wheel movement to horizontal pan", () => {+ expect(+ getShiftWheelHorizontalPanDelta({ shiftKey: true, deltaX: 0, deltaY: 80, deltaMode: 0 }, 900)+ ).toBe(40);+ });++ it("falls back to horizontal wheel movement when no vertical delta is present", () => {+ expect(+ getShiftWheelHorizontalPanDelta({ shiftKey: true, deltaX: -60, deltaY: 0, deltaMode: 0 }, 900)+ ).toBe(-30);+ });++ it("scales line and page deltas before panning", () => {+ expect(+ getShiftWheelHorizontalPanDelta({ shiftKey: true, deltaX: 0, deltaY: 2, deltaMode: 1 }, 900)+ ).toBeCloseTo(7.15625);+ expect(+ getShiftWheelHorizontalPanDelta({ shiftKey: true, deltaX: 0, deltaY: 1, deltaMode: 2 }, 900)+ ).toBe(450);+ });+});diff --git a/packages/jsoncrack-react/src/canvasHelpers.ts b/packages/jsoncrack-react/src/canvasHelpers.tsindex 8cfd20f0..0ff0d9c5 100644--- a/packages/jsoncrack-react/src/canvasHelpers.ts+++ b/packages/jsoncrack-react/src/canvasHelpers.ts@@ -106,6 +106,31 @@ export const setCanvasDragging = (container: HTMLElement | null, dragging: boolecanvas.classList.toggle("dragging", dragging);};+const DOM_DELTA_LINE = 1;+const DOM_DELTA_PAGE = 2;++// Match react-zoomable-ui's wheel panning feel: line-mode deltas use its+// empirical pixel estimate, and panning moves at half the raw wheel delta.+const LINE_WHEEL_DELTA_SCALE = 7.15625;+const HORIZONTAL_WHEEL_PAN_SENSITIVITY = 0.5;++/** Convert Shift+wheel movement into a horizontal client-space pan delta. */+export const getShiftWheelHorizontalPanDelta = (+ event: Pick<WheelEvent, "shiftKey" | "deltaX" | "deltaY" | "deltaMode">,+ pageDeltaScale: number+): number | null => {+ if (!event.shiftKey) return null;++ const rawDelta = event.deltaY !== 0 ? event.deltaY : event.deltaX;+ if (rawDelta === 0) return null;++ let scale = 1;+ if (event.deltaMode === DOM_DELTA_LINE) scale = LINE_WHEEL_DELTA_SCALE;+ if (event.deltaMode === DOM_DELTA_PAGE) scale = pageDeltaScale;++ return rawDelta * scale * HORIZONTAL_WHEEL_PAN_SENSITIVITY;+};+/** Breathing room around the measured graph rect, expressed as a fraction of the rect's own dimensions. Using a fraction (instead of a fixed pixel value) is scale-invariant: whether we measure the graph at zoom 1 or at zoom 0.27, the padded rect expands by the same *proportion*, so translating through `translateClientRectToVirtualSpace` produces the same virtual rect regardless of current zoom. A fixed client-pixel padding would blow up in virtual space as zoom decreases and make manual Fit clicks land at a different zoom than the initial auto-fit. */const GRAPH_FIT_PADDING_RATIO = 0.02;