/
data-grid-overlay-editor.tsx
242 lines (217 loc) 路 8.77 KB
/
data-grid-overlay-editor.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import * as React from "react";
import { createPortal } from "react-dom";
import ClickOutsideContainer from "../click-outside-container/click-outside-container.js";
import { makeCSSStyle, type Theme, ThemeContext } from "../../common/styles.js";
import type { GetCellRendererCallback } from "../../cells/cell-types.js";
import {
type EditableGridCell,
type GridCell,
isEditableGridCell,
isInnerOnlyCell,
isObjectEditorCallbackResult,
type Item,
type ProvideEditorCallback,
type ProvideEditorCallbackResult,
type Rectangle,
type ValidatedGridCell,
} from "../data-grid/data-grid-types.js";
import { DataGridOverlayEditorStyle } from "./data-grid-overlay-editor-style.js";
import type { OverlayImageEditorProps } from "./private/image-overlay-editor.js";
import { useStayOnScreen } from "./use-stay-on-screen.js";
type ImageEditorType = React.ComponentType<OverlayImageEditorProps>;
interface DataGridOverlayEditorProps {
readonly target: Rectangle;
readonly cell: Item;
readonly content: GridCell;
readonly className?: string;
readonly id: string;
readonly initialValue?: string;
readonly bloom?: readonly [number, number];
readonly theme: Theme;
readonly onFinishEditing: (newCell: GridCell | undefined, movement: readonly [-1 | 0 | 1, -1 | 0 | 1]) => void;
readonly forceEditMode: boolean;
readonly highlight: boolean;
readonly imageEditorOverride?: ImageEditorType;
readonly getCellRenderer: GetCellRendererCallback;
readonly markdownDivCreateNode?: (content: string) => DocumentFragment;
readonly provideEditor?: ProvideEditorCallback<GridCell>;
readonly validateCell?: (
cell: Item,
newValue: EditableGridCell,
prevValue: GridCell
) => boolean | ValidatedGridCell;
readonly isOutsideClick?: (e: MouseEvent | TouchEvent) => boolean;
}
const DataGridOverlayEditor: React.FunctionComponent<DataGridOverlayEditorProps> = p => {
const {
target,
content,
onFinishEditing: onFinishEditingIn,
forceEditMode,
initialValue,
imageEditorOverride,
markdownDivCreateNode,
highlight,
className,
theme,
id,
cell,
bloom,
validateCell,
getCellRenderer,
provideEditor,
isOutsideClick,
} = p;
const [tempValue, setTempValueRaw] = React.useState<GridCell | undefined>(forceEditMode ? content : undefined);
const lastValueRef = React.useRef(tempValue ?? content);
lastValueRef.current = tempValue ?? content;
const [isValid, setIsValid] = React.useState(() => {
if (validateCell === undefined) return true;
return !(isEditableGridCell(content) && validateCell?.(cell, content, lastValueRef.current) === false);
});
const onFinishEditing = React.useCallback<typeof onFinishEditingIn>(
(newCell, movement) => {
onFinishEditingIn(isValid ? newCell : undefined, movement);
},
[isValid, onFinishEditingIn]
);
const setTempValue = React.useCallback(
(newVal: GridCell | undefined) => {
if (validateCell !== undefined && newVal !== undefined && isEditableGridCell(newVal)) {
const validResult = validateCell(cell, newVal, lastValueRef.current);
if (validResult === false) {
setIsValid(false);
} else if (typeof validResult === "object") {
newVal = validResult;
setIsValid(true);
} else {
setIsValid(true);
}
}
setTempValueRaw(newVal);
},
[cell, validateCell]
);
const finished = React.useRef(false);
const customMotion = React.useRef<[-1 | 0 | 1, -1 | 0 | 1] | undefined>(undefined);
const onClickOutside = React.useCallback(() => {
onFinishEditing(tempValue, [0, 0]);
finished.current = true;
}, [tempValue, onFinishEditing]);
const onEditorFinished = React.useCallback(
(newValue: GridCell | undefined, movement?: readonly [-1 | 0 | 1, -1 | 0 | 1]) => {
onFinishEditing(newValue, movement ?? customMotion.current ?? [0, 0]);
finished.current = true;
},
[onFinishEditing]
);
const onKeyDown = React.useCallback(
async (event: React.KeyboardEvent) => {
let save = false;
if (event.key === "Escape") {
event.stopPropagation();
event.preventDefault();
customMotion.current = [0, 0];
} else if (event.key === "Enter" && !event.shiftKey) {
event.stopPropagation();
event.preventDefault();
customMotion.current = [0, 1];
save = true;
} else if (event.key === "Tab") {
event.stopPropagation();
event.preventDefault();
customMotion.current = [event.shiftKey ? -1 : 1, 0];
save = true;
}
window.setTimeout(() => {
if (!finished.current && customMotion.current !== undefined) {
onFinishEditing(save ? tempValue : undefined, customMotion.current);
finished.current = true;
}
}, 0);
},
[onFinishEditing, tempValue]
);
const targetValue = tempValue ?? content;
const [editorProvider, useLabel] = React.useMemo((): [ProvideEditorCallbackResult<GridCell>, boolean] | [] => {
if (isInnerOnlyCell(content)) return [];
const external = provideEditor?.(content);
if (external !== undefined) return [external, false];
return [getCellRenderer(content)?.provideEditor?.(content), false];
}, [content, getCellRenderer, provideEditor]);
const { ref, style: stayOnScreenStyle } = useStayOnScreen();
let pad = true;
let editor: React.ReactNode;
let style = true;
let styleOverride: React.CSSProperties | undefined;
if (editorProvider !== undefined) {
pad = editorProvider.disablePadding !== true;
style = editorProvider.disableStyling !== true;
const isObjectEditor = isObjectEditorCallbackResult(editorProvider);
if (isObjectEditor) {
styleOverride = editorProvider.styleOverride;
}
const CustomEditor = isObjectEditor ? editorProvider.editor : editorProvider;
editor = (
<CustomEditor
isHighlighted={highlight}
onChange={setTempValue}
value={targetValue}
initialValue={initialValue}
onFinishedEditing={onEditorFinished}
validatedSelection={isEditableGridCell(targetValue) ? targetValue.selectionRange : undefined}
forceEditMode={forceEditMode}
target={target}
imageEditorOverride={imageEditorOverride}
markdownDivCreateNode={markdownDivCreateNode}
isValid={isValid}
theme={theme}
/>
);
}
styleOverride = { ...styleOverride, ...stayOnScreenStyle };
// Consider imperatively creating and adding the element to the dom?
const portalElement = document.getElementById("portal");
if (portalElement === null) {
// eslint-disable-next-line no-console
console.error(
'Cannot open Data Grid overlay editor, because portal not found. Please add `<div id="portal" />` as the last child of your `<body>`.'
);
return null;
}
let classWrap = style ? "gdg-style" : "gdg-unstyle";
if (!isValid) {
classWrap += " gdg-invalid";
}
if (pad) {
classWrap += " gdg-pad";
}
const bloomX = bloom?.[0] ?? 1;
const bloomY = bloom?.[1] ?? 1;
return createPortal(
<ThemeContext.Provider value={theme}>
<ClickOutsideContainer
style={makeCSSStyle(theme)}
className={className}
onClickOutside={onClickOutside}
isOutsideClick={isOutsideClick}>
<DataGridOverlayEditorStyle
ref={ref}
id={id}
className={classWrap}
style={styleOverride}
as={useLabel === true ? "label" : undefined}
targetX={target.x - bloomX}
targetY={target.y - bloomY}
targetWidth={target.width + bloomX * 2}
targetHeight={target.height + bloomY * 2}>
<div className="gdg-clip-region" onKeyDown={onKeyDown}>
{editor}
</div>
</DataGridOverlayEditorStyle>
</ClickOutsideContainer>
</ThemeContext.Provider>,
portalElement
);
};
export default DataGridOverlayEditor;