Raw
1 import { RefObject, useCallback } from "react"
2 import { FieldValues, Path, PathValue } from "react-hook-form"
3
4 import { DataGridBulkUpdateCommand, DataGridMatrix } from "../models"
5 import { DataGridCoordinates } from "../types"
6
7 type UseDataGridClipboardEventsOptions<
8 TData,
9 TFieldValues extends FieldValues
10 > = {
11 containerRef: RefObject<HTMLDivElement>
12 matrix: DataGridMatrix<TData, TFieldValues>
13 isEditing: boolean
14 anchor: DataGridCoordinates | null
15 rangeEnd: DataGridCoordinates | null
16 getSelectionValues: (
17 fields: string[]
18 ) => PathValue<TFieldValues, Path<TFieldValues>>[]
19 setSelectionValues: (
20 fields: string[],
21 values: PathValue<TFieldValues, Path<TFieldValues>>[]
22 ) => void
23 execute: (command: DataGridBulkUpdateCommand) => void
24 }
25
26 export const useDataGridClipboardEvents = <
27 TData,
28 TFieldValues extends FieldValues
29 >({
30 containerRef,
31 matrix,
32 anchor,
33 rangeEnd,
34 isEditing,
35 getSelectionValues,
36 setSelectionValues,
37 execute,
38 }: UseDataGridClipboardEventsOptions<TData, TFieldValues>) => {
39 const handleCopyEvent = useCallback(
40 (e: ClipboardEvent) => {
41 if (isEditing || !anchor || !rangeEnd) {
42 return
43 }
44
45 const container = containerRef.current
46 if (e.defaultPrevented || !container) {
47 return
48 }
49
50 const activeElement = document.activeElement
51 if (activeElement && !container.contains(activeElement)) {
52 return
53 }
54
55 const selection = window.getSelection()
56 if (selection && !selection.isCollapsed) {
57 const selectionInsideGrid =
58 !!selection.anchorNode &&
59 !!selection.focusNode &&
60 container.contains(selection.anchorNode) &&
61 container.contains(selection.focusNode)
62
63 if (!selectionInsideGrid) {
64 return
65 }
66 }
67
68 e.preventDefault()
69
70 const fields = matrix.getFieldsInSelection(anchor, rangeEnd)
71 const values = getSelectionValues(fields)
72
73 const text = values
74 .map((value) => {
75 if (typeof value === "object" && value !== null) {
76 return JSON.stringify(value)
77 }
78 return value == null ? "" : `${value}`
79 })
80 .join("\t")
81
82 e.clipboardData?.setData("text/plain", text)
83 },
84 [isEditing, anchor, rangeEnd, containerRef, matrix, getSelectionValues]
85 )
86
87 const handlePasteEvent = useCallback(
88 (e: ClipboardEvent) => {
89 if (isEditing || !anchor || !rangeEnd) {
90 return
91 }
92
93 e.preventDefault()
94
95 const text = e.clipboardData?.getData("text/plain")
96
97 if (!text) {
98 return
99 }
100
101 const next = text.split("\t")
102
103 const fields = matrix.getFieldsInSelection(anchor, rangeEnd)
104 const prev = getSelectionValues(fields)
105
106 const command = new DataGridBulkUpdateCommand({
107 fields,
108 next,
109 prev,
110 setter: setSelectionValues,
111 })
112
113 execute(command)
114 },
115 [
116 isEditing,
117 anchor,
118 rangeEnd,
119 matrix,
120 getSelectionValues,
121 setSelectionValues,
122 execute,
123 ]
124 )
125
126 return {
127 handleCopyEvent,
128 handlePasteEvent,
129 }
130 }