import { BODY_CELL, BODY_CELL_HANDLE, COLUMN_RESIZER, HEADER_CELL, ROLE, } from '@/lib/constants/dom-role'; import { getElementByRole } from '@/lib/utils/dom'; import { selecting } from '@/lib/services/mouse-dwon/selection'; import { columnResize } from '@/lib/services/mouse-dwon/column-resize'; import { dragCalculate } from '@/lib/services/mouse-dwon/drag-calculate'; import { Store } from '@/lib/store'; import { Operator } from '@/lib/operator'; import {log} from "@/lib/utils/log"; export const useMouseDown = (store: Store, operators: Record) => () => (event: PointerEvent) => { log('[event]onMouseDown'); if (event.button !== 0) return; // 列宽改动 if (COLUMN_RESIZER === (event.target as Element).getAttribute(ROLE)) { const headerCol = getElementByRole(event.target as Element, HEADER_CELL); event.stopPropagation(); columnResize(store)(headerCol.id, event.clientX); return; } // 单元格拖拽 自动计算 if (BODY_CELL_HANDLE === (event.target as Element).getAttribute(ROLE)) { const cell = getElementByRole(event.target as Element, BODY_CELL); const rowId = cell.getAttribute('row-id'); const columnId = cell.getAttribute('column-id'); event.stopPropagation(); dragCalculate(operators[Operator.calculating])(columnId, rowId); return; } // 选择 编辑时 不允许进行计算 if (getElementByRole(event.target as Element, BODY_CELL)) { const cell = getElementByRole(event.target as Element, BODY_CELL); const rowId = cell.getAttribute('row-id'); const columnId = cell.getAttribute('column-id'); const pin = cell.getAttribute('pin'); const currentDBCell = operators[Operator.bodyClick].getDbClickedBodyCell(); if ( rowId && columnId && !( rowId === currentDBCell?.rowId && currentDBCell?.columnId === columnId && pin === currentDBCell?.pin ) ) { event.stopPropagation(); selecting(operators)(rowId, columnId); } return; } };