-
-
Notifications
You must be signed in to change notification settings - Fork 751
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: attachment cells support drag and drop uploading (#1255)
- Loading branch information
Showing
6 changed files
with
122 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
packages/sdk/src/components/grid-enhancements/hooks/use-grid-file-event.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import type { DragEvent } from 'react'; | ||
import { useRef } from 'react'; | ||
import type { IGridRef } from '../../grid/Grid'; | ||
import { SelectionRegionType, type ICellItem } from '../../grid/interface'; | ||
import { CombinedSelection, emptySelection } from '../../grid/managers'; | ||
|
||
interface IUseGridFileEventProps { | ||
gridRef: React.RefObject<IGridRef>; | ||
onValidation: (cell: ICellItem) => boolean; | ||
onCellDrop: (cell: ICellItem, files: FileList) => Promise<void> | void; | ||
} | ||
|
||
export const useGridFileEvent = (props: IUseGridFileEventProps) => { | ||
const { gridRef, onValidation, onCellDrop } = props; | ||
const dropTargetRef = useRef<ICellItem | null>(null); | ||
|
||
const getDropCell = (event: DragEvent): ICellItem | null => { | ||
const rect = (event.currentTarget as HTMLElement).getBoundingClientRect(); | ||
const x = event.clientX - rect.left; | ||
const y = event.clientY - rect.top; | ||
return gridRef.current?.getCellIndicesAtPosition(x, y) ?? null; | ||
}; | ||
|
||
const onDragLeave = (event: DragEvent) => { | ||
event.preventDefault(); | ||
gridRef.current?.setSelection(emptySelection); | ||
}; | ||
|
||
const onDragOver = (event: DragEvent) => { | ||
event.preventDefault(); | ||
if (!onCellDrop) return; | ||
|
||
const cell = getDropCell(event); | ||
|
||
if (!cell || !onValidation(cell)) return; | ||
|
||
dropTargetRef.current = cell; | ||
|
||
const newSelection = new CombinedSelection(SelectionRegionType.Cells, [cell, cell]); | ||
gridRef.current?.setSelection(newSelection); | ||
}; | ||
|
||
const onDrop = (event: DragEvent) => { | ||
event.preventDefault(); | ||
gridRef.current?.setSelection(emptySelection); | ||
|
||
if (!onCellDrop || !dropTargetRef.current) return; | ||
|
||
const files = event.dataTransfer?.files; | ||
|
||
if (!files?.length) return; | ||
|
||
onCellDrop(dropTargetRef.current, files); | ||
dropTargetRef.current = null; | ||
}; | ||
|
||
return { | ||
onDragOver, | ||
onDragLeave, | ||
onDrop, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters