The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Handle multi-touch events to improve pinch-to-zoom functionality on mobile devices for visualizations (#71932)
Timofey Kachalov207548a86474
diff --git a/frontend/src/metabase/visualizations/visualizations/CartesianChart/use-brush.ts b/frontend/src/metabase/visualizations/visualizations/CartesianChart/use-brush.tsindex 41b3bfe8cf6..1a5e5b1b93b 100644--- a/frontend/src/metabase/visualizations/visualizations/CartesianChart/use-brush.ts+++ b/frontend/src/metabase/visualizations/visualizations/CartesianChart/use-brush.ts@@ -187,6 +187,22 @@ function useTouchBrush({};const onPointerDown = (event: PointerEvent) => {+ // A second finger means pinch-to-zoom, not a long press.+ // Cancel whether the timer is still pending or brush is already active.+ if (!event.isPrimary) {+ cancel();++ if (activeRef.current) {+ activeRef.current = false;+ // Defer so ECharts finishes processing the current pointer event+ // before we yank brush mode away. Without this, ECharts can get+ // stuck with brush enabled after both fingers are released.+ setTimeout(disableBrush, 0);+ }++ return;+ }+if (activeRef.current) {return;}diff --git a/frontend/src/metabase/visualizations/visualizations/CartesianChart/use-brush.unit.spec.ts b/frontend/src/metabase/visualizations/visualizations/CartesianChart/use-brush.unit.spec.tsindex 44116bded70..f4472a5409e 100644--- a/frontend/src/metabase/visualizations/visualizations/CartesianChart/use-brush.unit.spec.ts+++ b/frontend/src/metabase/visualizations/visualizations/CartesianChart/use-brush.unit.spec.ts@@ -12,11 +12,15 @@ import {class PointerEventPolyfill extends MouseEvent {readonly pointerType: string;+ readonly isPrimary: boolean;+ readonly pointerId: number;constructor(type: string, init: PointerEventInit = {}) {super(type, init);this.pointerType = init.pointerType ?? "";+ this.isPrimary = init.isPrimary ?? false;+ this.pointerId = init.pointerId ?? 0;}}@@ -50,12 +54,24 @@ const createMockContainerRef = () => {const firePointer = (element: HTMLElement,type: string,- { clientX = 0, clientY = 0 }: { clientX?: number; clientY?: number } = {},+ {+ clientX = 0,+ clientY = 0,+ isPrimary = true,+ pointerId = 1,+ }: {+ clientX?: number;+ clientY?: number;+ isPrimary?: boolean;+ pointerId?: number;+ } = {},) => {element.dispatchEvent(new PointerEvent(type, {clientX,clientY,+ isPrimary,+ pointerId,bubbles: true,cancelable: true,}),@@ -333,6 +349,106 @@ describe("use-brush", () => {expect(touchmove.defaultPrevented).toBe(true);});++ it("cancels long press when a second finger touches (pinch-to-zoom)", () => {+ const { el, dispatchAction } = setupTouchBrush();++ // First finger down+ firePointer(el, "pointerdown", {+ clientX: 100,+ clientY: 100,+ isPrimary: true,+ pointerId: 1,+ });++ // Second finger down before timer fires+ firePointer(el, "pointerdown", {+ clientX: 200,+ clientY: 100,+ isPrimary: false,+ pointerId: 2,+ });++ // Wait past the long press duration+ act(() => jest.advanceTimersByTime(500));++ expectBrushNotEnabled(dispatchAction);+ });++ it("deactivates brush when a second finger touches during active brush", () => {+ const { el, dispatchAction } = setupTouchBrush();++ // First finger down, wait for long press to activate brush+ firePointer(el, "pointerdown", {+ clientX: 100,+ clientY: 100,+ isPrimary: true,+ pointerId: 1,+ });+ act(() => jest.advanceTimersByTime(500));+ expectBrushEnabled(dispatchAction);+ dispatchAction.mockClear();++ // Second finger down while brush is active+ firePointer(el, "pointerdown", {+ clientX: 200,+ clientY: 100,+ isPrimary: false,+ pointerId: 2,+ });+ act(() => jest.runAllTimers());++ expectBrushDisabled(dispatchAction);+ });++ it("does not freeze brush mode after second finger tap during active brush", () => {+ const { el, dispatchAction } = setupTouchBrush();++ // Long press activates brush+ firePointer(el, "pointerdown", {+ clientX: 100,+ clientY: 100,+ isPrimary: true,+ pointerId: 1,+ });+ act(() => jest.advanceTimersByTime(500));+ expectBrushEnabled(dispatchAction);+ dispatchAction.mockClear();++ // Second finger tap while brush is active+ firePointer(el, "pointerdown", {+ clientX: 200,+ clientY: 100,+ isPrimary: false,+ pointerId: 2,+ });++ // disableBrush must be deferred — calling it synchronously during+ // the pointer event causes ECharts to get stuck with brush enabled.+ expect(dispatchAction).not.toHaveBeenCalled();++ act(() => jest.runAllTimers());+ expectBrushDisabled(dispatchAction);+ dispatchAction.mockClear();++ // Both fingers released+ firePointer(el, "pointerup", { isPrimary: false, pointerId: 2 });+ firePointer(el, "pointerup", { isPrimary: true, pointerId: 1 });+ act(() => jest.runAllTimers());++ // Brush should not be re-enabled — no stuck brush mode+ expectBrushNotEnabled(dispatchAction);++ // A new long press should work normally+ firePointer(el, "pointerdown", {+ clientX: 100,+ clientY: 100,+ isPrimary: true,+ pointerId: 1,+ });+ act(() => jest.advanceTimersByTime(500));+ expectBrushEnabled(dispatchAction);+ });});});});