Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(md-tables): require a long press before touch cell selection (#5930)
Wolf Mermelstein7dea5a2a4c77
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tablePlugin.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tablePlugin.tsindex 28c36c96fd..7c905b8c29 100644--- a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tablePlugin.ts+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tablePlugin.ts@@ -10,6 +10,7 @@ import type { LexicalEditor } from 'lexical';import { registerTableListTab } from './tableListTab';import { registerTableSelectAll } from './tableSelectAll';import { registerTableTabInsertRow } from './tableTabInsertRow';+import { registerTableTouchDragGuard } from './tableTouchDragGuard';interface TablePluginProps {// When `false` (default `true`), merged cell support (colspan and rowspan) will be disabled and all@@ -33,6 +34,10 @@ function _registerTablePlugin(editor: LexicalEditor, props: TablePluginProps) {// Register the table selection observerregisterTableSelectionObserver(editor, props.hasTabHandler ?? true),+ // Keep the observer's pointer drag from turning a scroll into a cell+ // selection on touch (see tableTouchSelection for the touch gesture)+ registerTableTouchDragGuard(editor),+// Let list items claim Tab for indentation before cell navigation(props.hasTabHandler ?? true) ? registerTableListTab(editor) : () => {},diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tableTouchDragGuard.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tableTouchDragGuard.tsnew file mode 100644index 0000000000..74dfc34995--- /dev/null+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tableTouchDragGuard.ts@@ -0,0 +1,67 @@+/**+ * @file A touch drag across cells must never select them. @lexical/table+ * starts a drag selection from the first pointermove after any pointerdown on+ * a cell, and a finger in contact with the screen reports the primary button+ * as pressed, so its window-level handler treats a plain scroll over a table+ * as a drag and lights up every cell the finger passes. Touch selection is+ * instead owned by the long-press gesture in `tableTouchSelection`, so this+ * guard keeps touch pointermove from reaching that window handler for the+ * whole gesture. Mouse and pen are untouched: dragging still selects.+ *+ * Stopping propagation on the document leaves other document-level listeners+ * (the cell resizer's, the long-press gesture's) intact — only the window+ * listeners further up the path are cut off.+ */+import { getDOMCellFromTarget } from '@lexical/table';+import type { LexicalEditor } from 'lexical';++export function registerTableTouchDragGuard(editor: LexicalEditor): () => void {+ let endGesture: (() => void) | null = null;++ const attach = (root: HTMLElement): (() => void) => {+ const onPointerDown = (down: PointerEvent) => {+ if (down.pointerType !== 'touch' || !down.isPrimary) return;+ endGesture?.();++ const onCell =+ down.target instanceof Node && getDOMCellFromTarget(down.target);+ if (!onCell) return;++ const { pointerId } = down;++ const swallowMove = (move: PointerEvent) => {+ if (move.pointerId === pointerId) move.stopPropagation();+ };++ const end = () => {+ document.removeEventListener('pointermove', swallowMove);+ document.removeEventListener('pointerup', end);+ document.removeEventListener('pointercancel', end);+ endGesture = null;+ };++ document.addEventListener('pointermove', swallowMove);+ document.addEventListener('pointerup', end);+ document.addEventListener('pointercancel', end);+ endGesture = end;+ };++ root.addEventListener('pointerdown', onPointerDown);+ return () => {+ root.removeEventListener('pointerdown', onPointerDown);+ endGesture?.();+ };+ };++ let cleanupRoot: (() => void) | null = null;+ const removeRootListener = editor.registerRootListener((root) => {+ cleanupRoot?.();+ cleanupRoot = root ? attach(root) : null;+ });++ return () => {+ removeRootListener();+ cleanupRoot?.();+ cleanupRoot = null;+ };+}diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tableTouchSelection.test.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tableTouchSelection.test.tsindex 7913702177..4fc3776ae4 100644--- a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tableTouchSelection.test.ts+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tableTouchSelection.test.ts@@ -74,6 +74,27 @@ function touchEvent(isPrimary: true,pointerId: 1,pointerType: 'touch',+ // A finger in contact with the screen reports the primary button as+ // pressed, which is how @lexical/table's window-level drag handler+ // recognizes a move as part of a drag.+ ...(type === 'pointermove' ? { buttons: 1 } : {}),+ }) as unknown as PointerEvent;+}++function mouseEvent(+ type: string,+ init: { clientX?: number; clientY?: number } = {}+): PointerEvent {+ return new PolyfillPointerEvent(type, {+ bubbles: true,+ button: 0,+ buttons: 1,+ cancelable: true,+ clientX: init.clientX ?? 0,+ clientY: init.clientY ?? 0,+ isPrimary: true,+ pointerId: 1,+ pointerType: 'mouse',}) as unknown as PointerEvent;}@@ -168,11 +189,15 @@ describe('table touch selection', () => {it('does nothing when the finger drifts before the long press fires', async () => {const editor = createTestEditor();await buildTable(editor, coordGrid(2, 2));+ const startCell = getCellElement(editor, 0, 0);- getCellElement(editor, 0, 0).dispatchEvent(+ startCell.dispatchEvent(touchEvent('pointerdown', { clientX: 0, clientY: 0 }));- document.dispatchEvent(+ // Touch pointermove keeps targeting the element the finger went down on,+ // so the drift move comes from the pressed cell rather than the document.+ elementsUnderPointer = [getCellElement(editor, 1, 0)];+ startCell.dispatchEvent(touchEvent('pointermove', { clientX: 0, clientY: 30 }));vi.advanceTimersByTime(500);@@ -181,6 +206,72 @@ describe('table touch selection', () => {expect(readTableSelection(editor)).toBeNull();});+ it('scrolls instead of selecting when the drag starts before the long press', async () => {+ const editor = createTestEditor();+ await buildTable(editor, coordGrid(3, 3));+ const startCell = getCellElement(editor, 0, 0);++ startCell.dispatchEvent(+ touchEvent('pointerdown', { clientX: 0, clientY: 0 })+ );+ // A finger that pans down the page crosses cell after cell; none of them+ // may become a selection, because the press never stood still.+ for (const [index, row] of [1, 2].entries()) {+ elementsUnderPointer = [getCellElement(editor, row, 0)];+ startCell.dispatchEvent(+ touchEvent('pointermove', { clientX: 0, clientY: 30 * (index + 1) })+ );+ await flush();+ expect(readTableSelection(editor)).toBeNull();+ }++ startCell.dispatchEvent(+ touchEvent('pointerup', { clientX: 0, clientY: 60 })+ );+ await flush();++ expect(readTableSelection(editor)).toBeNull();+ });++ it('leaves the caret alone when a table is only scrolled past', async () => {+ // Editors that never register the touch-selection plugin (channel input,+ // comments, AI chat) must not turn a scroll into a cell selection either.+ const editor = createTableTestEditor();+ await buildTable(editor, coordGrid(3, 3));+ const startCell = getCellElement(editor, 0, 0);++ startCell.dispatchEvent(+ touchEvent('pointerdown', { clientX: 0, clientY: 0 })+ );+ elementsUnderPointer = [getCellElement(editor, 2, 0)];+ startCell.dispatchEvent(+ touchEvent('pointermove', { clientX: 0, clientY: 60 })+ );+ vi.advanceTimersByTime(500);+ await flush();++ expect(readTableSelection(editor)).toBeNull();+ });++ it('still lets a mouse drag select cells', async () => {+ const editor = createTestEditor();+ await buildTable(editor, coordGrid(3, 3));+ const startCell = getCellElement(editor, 0, 0);++ startCell.dispatchEvent(+ mouseEvent('pointerdown', { clientX: 0, clientY: 0 })+ );+ elementsUnderPointer = [getCellElement(editor, 2, 1)];+ startCell.dispatchEvent(+ mouseEvent('pointermove', { clientX: 50, clientY: 80 })+ );+ await flush();++ const selection = readTableSelection(editor);+ expect(selection?.anchor.getKey()).toBe(getCellNode(editor, 0, 0).getKey());+ expect(selection?.focus.getKey()).toBe(getCellNode(editor, 2, 1).getKey());+ });+it('does nothing on a quick tap', async () => {const editor = createTestEditor();await buildTable(editor, coordGrid(2, 2));diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tableTouchSelection.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tableTouchSelection.tsindex 42dc784c45..933eccf200 100644--- a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tableTouchSelection.ts+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tableTouchSelection.ts@@ -1,12 +1,13 @@/*** @file Touch cell selection for tables: long-press a cell to select it, then* swipe in any direction to extend the selection cell-by-cell. Mouse/pen- * drags already do this via @lexical/table's own pointer handlers; on touch- * the browser claims the drag for scrolling before those handlers see any- * movement. Long-press is the disambiguator: a finger that hasn't moved by- * then isn't scrolling, so from that point the gesture is ours to keep (a- * non-passive touchmove listener blocks the pan) and the selection is driven- * through the same TableObserver the desktop drag uses.+ * drags already do this via @lexical/table's own pointer handlers, but a+ * finger that starts moving right away is scrolling, so those handlers are+ * kept away from touch entirely by `tableTouchDragGuard`. Long-press is the+ * disambiguator: a finger that hasn't moved by then isn't scrolling, so from+ * that point the gesture is ours to keep (a non-passive touchmove listener+ * blocks the pan) and the selection is driven through the same TableObserver+ * the desktop drag uses.*/import {getDOMCellFromTarget,@@ -86,9 +87,6 @@ function registerTableTouchSelection(editor: LexicalEditor): () => void {const onSelectMove = (move: PointerEvent) => {if (move.pointerId !== pointerId) return;- // Keep @lexical/table's window-level drag handler (attached on the- // same pointerdown) from fighting over the selection.- move.stopPropagation();let focusCell: TableDOMCell | null = null;for (const el of document.elementsFromPoint(move.clientX,