@dxos/plugin-sheet 0.6.8-main.046e6cf
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +8 -0
- package/README.md +14 -0
- package/dist/lib/browser/SheetContainer-H22IDJ43.mjs +3740 -0
- package/dist/lib/browser/SheetContainer-H22IDJ43.mjs.map +7 -0
- package/dist/lib/browser/chunk-6VPEAUG6.mjs +82 -0
- package/dist/lib/browser/chunk-6VPEAUG6.mjs.map +7 -0
- package/dist/lib/browser/chunk-AT2FJXQX.mjs +861 -0
- package/dist/lib/browser/chunk-AT2FJXQX.mjs.map +7 -0
- package/dist/lib/browser/chunk-JRL5LGCE.mjs +18 -0
- package/dist/lib/browser/chunk-JRL5LGCE.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +213 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/browser/meta.mjs +9 -0
- package/dist/lib/browser/meta.mjs.map +7 -0
- package/dist/lib/browser/types.mjs +22 -0
- package/dist/lib/browser/types.mjs.map +7 -0
- package/dist/lib/node/SheetContainer-S32KTNZ6.cjs +3731 -0
- package/dist/lib/node/SheetContainer-S32KTNZ6.cjs.map +7 -0
- package/dist/lib/node/chunk-4CE6FK5Z.cjs +108 -0
- package/dist/lib/node/chunk-4CE6FK5Z.cjs.map +7 -0
- package/dist/lib/node/chunk-BJ6ZD7MN.cjs +51 -0
- package/dist/lib/node/chunk-BJ6ZD7MN.cjs.map +7 -0
- package/dist/lib/node/chunk-FCKJ4QRM.cjs +881 -0
- package/dist/lib/node/chunk-FCKJ4QRM.cjs.map +7 -0
- package/dist/lib/node/index.cjs +226 -0
- package/dist/lib/node/index.cjs.map +7 -0
- package/dist/lib/node/meta.cjs +30 -0
- package/dist/lib/node/meta.cjs.map +7 -0
- package/dist/lib/node/meta.json +1 -0
- package/dist/lib/node/types.cjs +44 -0
- package/dist/lib/node/types.cjs.map +7 -0
- package/dist/types/src/SheetPlugin.d.ts +4 -0
- package/dist/types/src/SheetPlugin.d.ts.map +1 -0
- package/dist/types/src/components/CellEditor/CellEditor.d.ts +14 -0
- package/dist/types/src/components/CellEditor/CellEditor.d.ts.map +1 -0
- package/dist/types/src/components/CellEditor/CellEditor.stories.d.ts +29 -0
- package/dist/types/src/components/CellEditor/CellEditor.stories.d.ts.map +1 -0
- package/dist/types/src/components/CellEditor/extension.d.ts +18 -0
- package/dist/types/src/components/CellEditor/extension.d.ts.map +1 -0
- package/dist/types/src/components/CellEditor/extension.test.d.ts +2 -0
- package/dist/types/src/components/CellEditor/extension.test.d.ts.map +1 -0
- package/dist/types/src/components/CellEditor/functions.d.ts +66 -0
- package/dist/types/src/components/CellEditor/functions.d.ts.map +1 -0
- package/dist/types/src/components/CellEditor/index.d.ts +3 -0
- package/dist/types/src/components/CellEditor/index.d.ts.map +1 -0
- package/dist/types/src/components/ComputeGraph/async-function.d.ts +52 -0
- package/dist/types/src/components/ComputeGraph/async-function.d.ts.map +1 -0
- package/dist/types/src/components/ComputeGraph/custom.d.ts +21 -0
- package/dist/types/src/components/ComputeGraph/custom.d.ts.map +1 -0
- package/dist/types/src/components/ComputeGraph/edge-function.d.ts +20 -0
- package/dist/types/src/components/ComputeGraph/edge-function.d.ts.map +1 -0
- package/dist/types/src/components/ComputeGraph/graph-context.d.ts +11 -0
- package/dist/types/src/components/ComputeGraph/graph-context.d.ts.map +1 -0
- package/dist/types/src/components/ComputeGraph/graph.browser.test.d.ts +2 -0
- package/dist/types/src/components/ComputeGraph/graph.browser.test.d.ts.map +1 -0
- package/dist/types/src/components/ComputeGraph/graph.d.ts +21 -0
- package/dist/types/src/components/ComputeGraph/graph.d.ts.map +1 -0
- package/dist/types/src/components/ComputeGraph/index.d.ts +4 -0
- package/dist/types/src/components/ComputeGraph/index.d.ts.map +1 -0
- package/dist/types/src/components/Sheet/Sheet.d.ts +55 -0
- package/dist/types/src/components/Sheet/Sheet.d.ts.map +1 -0
- package/dist/types/src/components/Sheet/Sheet.stories.d.ts +54 -0
- package/dist/types/src/components/Sheet/Sheet.stories.d.ts.map +1 -0
- package/dist/types/src/components/Sheet/formatting.d.ts +14 -0
- package/dist/types/src/components/Sheet/formatting.d.ts.map +1 -0
- package/dist/types/src/components/Sheet/grid.d.ts +52 -0
- package/dist/types/src/components/Sheet/grid.d.ts.map +1 -0
- package/dist/types/src/components/Sheet/index.d.ts +2 -0
- package/dist/types/src/components/Sheet/index.d.ts.map +1 -0
- package/dist/types/src/components/Sheet/nav.d.ts +29 -0
- package/dist/types/src/components/Sheet/nav.d.ts.map +1 -0
- package/dist/types/src/components/Sheet/sheet-context.d.ts +24 -0
- package/dist/types/src/components/Sheet/sheet-context.d.ts.map +1 -0
- package/dist/types/src/components/Sheet/util.d.ts +18 -0
- package/dist/types/src/components/Sheet/util.d.ts.map +1 -0
- package/dist/types/src/components/SheetContainer.d.ts +9 -0
- package/dist/types/src/components/SheetContainer.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar/Toolbar.d.ts +21 -0
- package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts +35 -0
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar/common.d.ts +20 -0
- package/dist/types/src/components/Toolbar/common.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar/index.d.ts +2 -0
- package/dist/types/src/components/Toolbar/index.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +7 -0
- package/dist/types/src/components/index.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +4 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/meta.d.ts +15 -0
- package/dist/types/src/meta.d.ts.map +1 -0
- package/dist/types/src/model/index.d.ts +3 -0
- package/dist/types/src/model/index.d.ts.map +1 -0
- package/dist/types/src/model/model.browser.test.d.ts +2 -0
- package/dist/types/src/model/model.browser.test.d.ts.map +1 -0
- package/dist/types/src/model/model.d.ts +142 -0
- package/dist/types/src/model/model.d.ts.map +1 -0
- package/dist/types/src/model/types.d.ts +17 -0
- package/dist/types/src/model/types.d.ts.map +1 -0
- package/dist/types/src/model/types.test.d.ts +2 -0
- package/dist/types/src/model/types.test.d.ts.map +1 -0
- package/dist/types/src/model/util.d.ts +15 -0
- package/dist/types/src/model/util.d.ts.map +1 -0
- package/dist/types/src/translations.d.ts +16 -0
- package/dist/types/src/translations.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +94 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/package.json +122 -0
- package/src/SheetPlugin.tsx +150 -0
- package/src/components/CellEditor/CellEditor.stories.tsx +88 -0
- package/src/components/CellEditor/CellEditor.tsx +113 -0
- package/src/components/CellEditor/extension.test.ts +42 -0
- package/src/components/CellEditor/extension.ts +286 -0
- package/src/components/CellEditor/functions.ts +2017 -0
- package/src/components/CellEditor/index.ts +6 -0
- package/src/components/ComputeGraph/async-function.ts +148 -0
- package/src/components/ComputeGraph/custom.ts +70 -0
- package/src/components/ComputeGraph/edge-function.ts +60 -0
- package/src/components/ComputeGraph/graph-context.tsx +37 -0
- package/src/components/ComputeGraph/graph.browser.test.ts +49 -0
- package/src/components/ComputeGraph/graph.ts +52 -0
- package/src/components/ComputeGraph/index.ts +7 -0
- package/src/components/Sheet/Sheet.stories.tsx +329 -0
- package/src/components/Sheet/Sheet.tsx +1164 -0
- package/src/components/Sheet/formatting.ts +106 -0
- package/src/components/Sheet/grid.ts +191 -0
- package/src/components/Sheet/index.ts +5 -0
- package/src/components/Sheet/nav.ts +157 -0
- package/src/components/Sheet/sheet-context.tsx +101 -0
- package/src/components/Sheet/util.ts +56 -0
- package/src/components/SheetContainer.tsx +30 -0
- package/src/components/Toolbar/Toolbar.stories.tsx +36 -0
- package/src/components/Toolbar/Toolbar.tsx +198 -0
- package/src/components/Toolbar/common.tsx +72 -0
- package/src/components/Toolbar/index.ts +5 -0
- package/src/components/index.ts +10 -0
- package/src/index.ts +9 -0
- package/src/meta.tsx +18 -0
- package/src/model/index.ts +6 -0
- package/src/model/model.browser.test.ts +100 -0
- package/src/model/model.ts +480 -0
- package/src/model/types.test.ts +92 -0
- package/src/model/types.ts +71 -0
- package/src/model/util.ts +36 -0
- package/src/translations.ts +22 -0
- package/src/types.ts +110 -0
|
@@ -0,0 +1,1164 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
type Active,
|
|
7
|
+
DndContext,
|
|
8
|
+
type DragEndEvent,
|
|
9
|
+
DragOverlay,
|
|
10
|
+
type DragStartEvent,
|
|
11
|
+
KeyboardSensor,
|
|
12
|
+
type Modifier,
|
|
13
|
+
MouseSensor,
|
|
14
|
+
type PointerActivationConstraint,
|
|
15
|
+
TouchSensor,
|
|
16
|
+
useDraggable,
|
|
17
|
+
useDroppable,
|
|
18
|
+
useSensor,
|
|
19
|
+
useSensors,
|
|
20
|
+
} from '@dnd-kit/core';
|
|
21
|
+
import { restrictToHorizontalAxis, restrictToVerticalAxis } from '@dnd-kit/modifiers';
|
|
22
|
+
import { getEventCoordinates, useCombinedRefs } from '@dnd-kit/utilities';
|
|
23
|
+
import { Function as FunctionIcon } from '@phosphor-icons/react';
|
|
24
|
+
import { Resizable, type ResizeCallback, type ResizeStartCallback } from 're-resizable';
|
|
25
|
+
import React, {
|
|
26
|
+
type CSSProperties,
|
|
27
|
+
type DOMAttributes,
|
|
28
|
+
type PropsWithChildren,
|
|
29
|
+
forwardRef,
|
|
30
|
+
useEffect,
|
|
31
|
+
useImperativeHandle,
|
|
32
|
+
useRef,
|
|
33
|
+
useState,
|
|
34
|
+
} from 'react';
|
|
35
|
+
import { createPortal } from 'react-dom';
|
|
36
|
+
import { useResizeDetector } from 'react-resize-detector';
|
|
37
|
+
|
|
38
|
+
import { debounce } from '@dxos/async';
|
|
39
|
+
import { fullyQualifiedId, createDocAccessor } from '@dxos/client/echo';
|
|
40
|
+
import { log } from '@dxos/log';
|
|
41
|
+
import { type ThemedClassName } from '@dxos/react-ui';
|
|
42
|
+
import { createAttendableAttributes } from '@dxos/react-ui-attention';
|
|
43
|
+
import { mx } from '@dxos/react-ui-theme';
|
|
44
|
+
|
|
45
|
+
import {
|
|
46
|
+
type GridLayoutProps,
|
|
47
|
+
type SizeMap,
|
|
48
|
+
CELL_DATA_KEY,
|
|
49
|
+
axisHeight,
|
|
50
|
+
axisWidth,
|
|
51
|
+
defaultHeight,
|
|
52
|
+
defaultWidth,
|
|
53
|
+
maxWidth,
|
|
54
|
+
maxHeight,
|
|
55
|
+
minWidth,
|
|
56
|
+
minHeight,
|
|
57
|
+
getCellElement,
|
|
58
|
+
useGridLayout,
|
|
59
|
+
} from './grid';
|
|
60
|
+
import { type GridSize, handleArrowNav, handleNav, useRangeSelect } from './nav';
|
|
61
|
+
import { type SheetContextProps, SheetContextProvider, useSheetContext } from './sheet-context';
|
|
62
|
+
import { getRectUnion, getRelativeClientRect, scrollIntoView } from './util';
|
|
63
|
+
import {
|
|
64
|
+
type CellIndex,
|
|
65
|
+
type CellAddress,
|
|
66
|
+
addressToA1Notation,
|
|
67
|
+
columnLetter,
|
|
68
|
+
posEquals,
|
|
69
|
+
rangeToA1Notation,
|
|
70
|
+
} from '../../model';
|
|
71
|
+
import {
|
|
72
|
+
CellEditor,
|
|
73
|
+
type CellRangeNotifier,
|
|
74
|
+
type EditorKeysProps,
|
|
75
|
+
editorKeys,
|
|
76
|
+
rangeExtension,
|
|
77
|
+
sheetExtension,
|
|
78
|
+
} from '../CellEditor';
|
|
79
|
+
|
|
80
|
+
// TODO(burdon): Virtualization bug.
|
|
81
|
+
// TODO(burdon): Toolbar styles and formatting.
|
|
82
|
+
// TODO(burdon): Insert/delete rows/columns (menu).
|
|
83
|
+
// TODO(burdon): Scroll to position if off screen.
|
|
84
|
+
// TODO(burdon): Don't render until sizes were updated (otherwise, flickers).
|
|
85
|
+
|
|
86
|
+
// TODO(burdon): Model multiple sheets (e.g., documents). And cross sheet references.
|
|
87
|
+
// TODO(burdon): Factor out react-ui-sheet.
|
|
88
|
+
// TODO(burdon): Comments (josiah).
|
|
89
|
+
// TODO(burdon): Realtime long text.
|
|
90
|
+
// TODO(burdon): Search.
|
|
91
|
+
|
|
92
|
+
// TODO(burdon): Virtualization:
|
|
93
|
+
// https://github.com/TanStack/virtual/blob/main/examples/react/dynamic/src/main.tsx#L171
|
|
94
|
+
// https://tanstack.com/virtual/v3/docs/framework/react/examples/variable
|
|
95
|
+
// https://canvas-grid-demo.vercel.app
|
|
96
|
+
// https://sheet.brianhung.me
|
|
97
|
+
// https://github.com/BrianHung
|
|
98
|
+
// https://daybrush.com/moveable
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Features:
|
|
102
|
+
* - Move rows/columns.
|
|
103
|
+
* - Insert/delete rows/columns.
|
|
104
|
+
* - Copy/paste.
|
|
105
|
+
* - Undo/redo.
|
|
106
|
+
* - Comments.
|
|
107
|
+
* - Real time collaborative editing of large text cells.
|
|
108
|
+
* - Select range.
|
|
109
|
+
* - Format cells.
|
|
110
|
+
* - Formulae.
|
|
111
|
+
* - Update formula ranges by selection.
|
|
112
|
+
*/
|
|
113
|
+
|
|
114
|
+
// TODO(burdon): Factor out fragments.
|
|
115
|
+
const fragments = {
|
|
116
|
+
axis: 'bg-neutral-50 text-neutral-800 dark:bg-neutral-800 dark:text-neutral-200 text-xs select-none',
|
|
117
|
+
axisSelected: 'bg-neutral-100 dark:bg-neutral-900 text-black dark:text-white',
|
|
118
|
+
cell: 'dark:bg-neutral-850',
|
|
119
|
+
cellSelected: 'bg-neutral-50 dark:bg-neutral-900 text-black dark:text-white border !border-primary-500',
|
|
120
|
+
border: 'border-neutral-200 dark:border-neutral-700',
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
//
|
|
124
|
+
// Root
|
|
125
|
+
//
|
|
126
|
+
|
|
127
|
+
type SheetRootProps = SheetContextProps;
|
|
128
|
+
|
|
129
|
+
const SheetRoot = ({ children, ...props }: PropsWithChildren<SheetContextProps>) => {
|
|
130
|
+
return <SheetContextProvider {...props}>{children}</SheetContextProvider>;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
//
|
|
134
|
+
// Main
|
|
135
|
+
//
|
|
136
|
+
|
|
137
|
+
type SheetMainProps = ThemedClassName<Partial<GridSize>>;
|
|
138
|
+
|
|
139
|
+
const SheetMain = forwardRef<HTMLDivElement, SheetMainProps>(({ classNames, numRows, numColumns }, forwardRef) => {
|
|
140
|
+
const { model, cursor, setCursor, setRange, setEditing } = useSheetContext();
|
|
141
|
+
|
|
142
|
+
// Scrolling.
|
|
143
|
+
const { rowsRef, columnsRef, contentRef } = useScrollHandlers();
|
|
144
|
+
|
|
145
|
+
//
|
|
146
|
+
// Order of Row/columns.
|
|
147
|
+
//
|
|
148
|
+
const [rows, setRows] = useState([...model.sheet.rows]);
|
|
149
|
+
const [columns, setColumns] = useState([...model.sheet.columns]);
|
|
150
|
+
useEffect(() => {
|
|
151
|
+
const rowsAccessor = createDocAccessor(model.sheet, ['rows']);
|
|
152
|
+
const columnsAccessor = createDocAccessor(model.sheet, ['columns']);
|
|
153
|
+
const handleUpdate = debounce(() => {
|
|
154
|
+
setRows([...model.sheet.rows]);
|
|
155
|
+
setColumns([...model.sheet.columns]);
|
|
156
|
+
}, 100);
|
|
157
|
+
|
|
158
|
+
rowsAccessor.handle.addListener('change', handleUpdate);
|
|
159
|
+
columnsAccessor.handle.addListener('change', handleUpdate);
|
|
160
|
+
handleUpdate();
|
|
161
|
+
return () => {
|
|
162
|
+
rowsAccessor.handle.removeListener('change', handleUpdate);
|
|
163
|
+
columnsAccessor.handle.removeListener('change', handleUpdate);
|
|
164
|
+
};
|
|
165
|
+
}, [model]);
|
|
166
|
+
|
|
167
|
+
// Refresh the model.
|
|
168
|
+
// TODO(burdon): Breaks undo.
|
|
169
|
+
useEffect(() => {
|
|
170
|
+
model.reset();
|
|
171
|
+
}, [rows, columns]);
|
|
172
|
+
|
|
173
|
+
const handleMoveRows: SheetRowsProps['onMove'] = (from, to, num = 1) => {
|
|
174
|
+
const cursorIdx = cursor ? model.addressToIndex(cursor) : undefined;
|
|
175
|
+
const [rows] = model.sheet.rows.splice(from, num);
|
|
176
|
+
model.sheet.rows.splice(to, 0, rows);
|
|
177
|
+
if (cursorIdx) {
|
|
178
|
+
setCursor(model.addressFromIndex(cursorIdx));
|
|
179
|
+
}
|
|
180
|
+
setRows([...model.sheet.rows]);
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const handleMoveColumns: SheetColumnsProps['onMove'] = (from, to, num = 1) => {
|
|
184
|
+
const cursorIdx = cursor ? model.addressToIndex(cursor) : undefined;
|
|
185
|
+
const columns = model.sheet.columns.splice(from, num);
|
|
186
|
+
model.sheet.columns.splice(to, 0, ...columns);
|
|
187
|
+
if (cursorIdx) {
|
|
188
|
+
setCursor(model.addressFromIndex(cursorIdx));
|
|
189
|
+
}
|
|
190
|
+
setColumns([...model.sheet.columns]);
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
//
|
|
194
|
+
// Row/column sizes.
|
|
195
|
+
//
|
|
196
|
+
const [rowSizes, setRowSizes] = useState<SizeMap>();
|
|
197
|
+
const [columnSizes, setColumnSizes] = useState<SizeMap>();
|
|
198
|
+
useEffect(() => {
|
|
199
|
+
const rowAccessor = createDocAccessor(model.sheet, ['rowMeta']);
|
|
200
|
+
const columnAccessor = createDocAccessor(model.sheet, ['columnMeta']);
|
|
201
|
+
const handleUpdate = debounce(() => {
|
|
202
|
+
const mapSizes = (values: [string, { size?: number | undefined }][]) =>
|
|
203
|
+
values.reduce<SizeMap>((map, [idx, meta]) => {
|
|
204
|
+
if (meta.size) {
|
|
205
|
+
map[idx] = meta.size;
|
|
206
|
+
}
|
|
207
|
+
return map;
|
|
208
|
+
}, {});
|
|
209
|
+
|
|
210
|
+
setRowSizes(mapSizes(Object.entries(model.sheet.rowMeta)));
|
|
211
|
+
setColumnSizes(mapSizes(Object.entries(model.sheet.columnMeta)));
|
|
212
|
+
}, 100);
|
|
213
|
+
|
|
214
|
+
rowAccessor.handle.addListener('change', handleUpdate);
|
|
215
|
+
columnAccessor.handle.addListener('change', handleUpdate);
|
|
216
|
+
handleUpdate();
|
|
217
|
+
return () => {
|
|
218
|
+
rowAccessor.handle.removeListener('change', handleUpdate);
|
|
219
|
+
columnAccessor.handle.removeListener('change', handleUpdate);
|
|
220
|
+
};
|
|
221
|
+
}, [model]);
|
|
222
|
+
|
|
223
|
+
const handleResizeRow: SheetRowsProps['onResize'] = (idx, size, save) => {
|
|
224
|
+
if (save) {
|
|
225
|
+
model.sheet.rowMeta[idx] ??= {};
|
|
226
|
+
model.sheet.rowMeta[idx].size = size;
|
|
227
|
+
} else {
|
|
228
|
+
setRowSizes((sizes) => ({ ...sizes, [idx]: size }));
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
const handleResizeColumn: SheetColumnsProps['onResize'] = (idx, size, save) => {
|
|
233
|
+
if (save) {
|
|
234
|
+
model.sheet.columnMeta[idx] ??= {};
|
|
235
|
+
model.sheet.columnMeta[idx].size = size;
|
|
236
|
+
} else {
|
|
237
|
+
setColumnSizes((sizes) => ({ ...sizes, [idx]: size }));
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
return (
|
|
242
|
+
<div
|
|
243
|
+
role='none'
|
|
244
|
+
className={mx(
|
|
245
|
+
'grid grid-cols-[calc(var(--rail-size)-2px)_1fr] grid-rows-[32px_1fr_32px] bs-full is-full overflow-hidden',
|
|
246
|
+
fragments.border,
|
|
247
|
+
classNames,
|
|
248
|
+
)}
|
|
249
|
+
>
|
|
250
|
+
<GridCorner
|
|
251
|
+
onClick={() => {
|
|
252
|
+
setCursor(undefined);
|
|
253
|
+
setRange(undefined);
|
|
254
|
+
setEditing(false);
|
|
255
|
+
}}
|
|
256
|
+
/>
|
|
257
|
+
<SheetColumns
|
|
258
|
+
ref={columnsRef}
|
|
259
|
+
columns={columns}
|
|
260
|
+
sizes={columnSizes}
|
|
261
|
+
selected={cursor?.column}
|
|
262
|
+
onSelect={(column) => setCursor(cursor?.column === column ? undefined : { row: -1, column })}
|
|
263
|
+
onResize={handleResizeColumn}
|
|
264
|
+
onMove={handleMoveColumns}
|
|
265
|
+
/>
|
|
266
|
+
|
|
267
|
+
<SheetRows
|
|
268
|
+
ref={rowsRef}
|
|
269
|
+
rows={rows}
|
|
270
|
+
sizes={rowSizes}
|
|
271
|
+
selected={cursor?.row}
|
|
272
|
+
onSelect={(row) => setCursor(cursor?.row === row ? undefined : { row, column: -1 })}
|
|
273
|
+
onResize={handleResizeRow}
|
|
274
|
+
onMove={handleMoveRows}
|
|
275
|
+
/>
|
|
276
|
+
<SheetGrid
|
|
277
|
+
ref={contentRef}
|
|
278
|
+
size={{ numRows: numRows ?? rows.length, numColumns: numColumns ?? columns.length }}
|
|
279
|
+
rows={rows}
|
|
280
|
+
columns={columns}
|
|
281
|
+
rowSizes={rowSizes}
|
|
282
|
+
columnSizes={columnSizes}
|
|
283
|
+
/>
|
|
284
|
+
|
|
285
|
+
<GridCorner />
|
|
286
|
+
<SheetStatusBar />
|
|
287
|
+
</div>
|
|
288
|
+
);
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Coordinate scrolling across components.
|
|
293
|
+
*/
|
|
294
|
+
const useScrollHandlers = () => {
|
|
295
|
+
const rowsRef = useRef<HTMLDivElement>(null);
|
|
296
|
+
const columnsRef = useRef<HTMLDivElement>(null);
|
|
297
|
+
const contentRef = useRef<HTMLDivElement>(null);
|
|
298
|
+
|
|
299
|
+
useEffect(() => {
|
|
300
|
+
const handleRowsScroll = (ev: Event) => {
|
|
301
|
+
const { scrollTop } = ev.target as HTMLDivElement;
|
|
302
|
+
if (!rowsRef.current!.dataset.locked) {
|
|
303
|
+
contentRef.current!.scrollTop = scrollTop;
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
const handleColumnsScroll = (ev: Event) => {
|
|
308
|
+
const { scrollLeft } = ev.target as HTMLDivElement;
|
|
309
|
+
if (!columnsRef.current!.dataset.locked) {
|
|
310
|
+
contentRef.current!.scrollLeft = scrollLeft;
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
const handleContentScroll = (ev: Event) => {
|
|
315
|
+
const { scrollTop, scrollLeft } = ev.target as HTMLDivElement;
|
|
316
|
+
rowsRef.current!.scrollTop = scrollTop;
|
|
317
|
+
columnsRef.current!.scrollLeft = scrollLeft;
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
const rows = rowsRef.current!;
|
|
321
|
+
const columns = columnsRef.current!;
|
|
322
|
+
const content = contentRef.current!;
|
|
323
|
+
|
|
324
|
+
rows.addEventListener('scroll', handleRowsScroll);
|
|
325
|
+
columns.addEventListener('scroll', handleColumnsScroll);
|
|
326
|
+
content.addEventListener('scroll', handleContentScroll);
|
|
327
|
+
return () => {
|
|
328
|
+
rows.removeEventListener('scroll', handleRowsScroll);
|
|
329
|
+
columns.removeEventListener('scroll', handleColumnsScroll);
|
|
330
|
+
content.removeEventListener('scroll', handleContentScroll);
|
|
331
|
+
};
|
|
332
|
+
}, []);
|
|
333
|
+
|
|
334
|
+
return { rowsRef, columnsRef, contentRef };
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
//
|
|
338
|
+
// Row/Column
|
|
339
|
+
//
|
|
340
|
+
|
|
341
|
+
const GridCorner = (props: { className?: string } & Pick<DOMAttributes<HTMLDivElement>, 'onClick'>) => {
|
|
342
|
+
return <div className={fragments.axis} {...props} />;
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
const MovingOverlay = ({ label }: { label: string }) => {
|
|
346
|
+
return (
|
|
347
|
+
<div className='flex w-full h-full justify-center items-center text-sm p-1 bg-primary-500/50 cursor-pointer'>
|
|
348
|
+
{label}
|
|
349
|
+
</div>
|
|
350
|
+
);
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
// https://docs.dndkit.com/api-documentation/sensors/pointer#activation-constraints
|
|
354
|
+
const mouseConstraints: PointerActivationConstraint = { distance: 10 };
|
|
355
|
+
const touchConstraints: PointerActivationConstraint = { delay: 250, tolerance: 5 };
|
|
356
|
+
|
|
357
|
+
type ResizeProps = {
|
|
358
|
+
sizes?: SizeMap;
|
|
359
|
+
onResize?: (idx: CellIndex, size: number, save?: boolean) => void;
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
type MoveProps = {
|
|
363
|
+
onMove?: (from: number, to: number) => void;
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
type RowColumnSelection = {
|
|
367
|
+
selected?: number;
|
|
368
|
+
onSelect?: (selected: number) => void;
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
type RowColumnProps = {
|
|
372
|
+
idx: CellIndex;
|
|
373
|
+
index: number;
|
|
374
|
+
label: string;
|
|
375
|
+
size: number;
|
|
376
|
+
resize: boolean;
|
|
377
|
+
selected: boolean;
|
|
378
|
+
} & Pick<ResizeProps, 'onResize'> &
|
|
379
|
+
Pick<RowColumnSelection, 'onSelect'>;
|
|
380
|
+
|
|
381
|
+
//
|
|
382
|
+
// Rows
|
|
383
|
+
//
|
|
384
|
+
|
|
385
|
+
type SheetRowsProps = { rows: CellIndex[] } & RowColumnSelection & ResizeProps & MoveProps;
|
|
386
|
+
|
|
387
|
+
const SheetRows = forwardRef<HTMLDivElement, SheetRowsProps>(
|
|
388
|
+
({ rows, sizes, selected, onSelect, onResize, onMove }, forwardRef) => {
|
|
389
|
+
const mouseSensor = useSensor(MouseSensor, { activationConstraint: mouseConstraints });
|
|
390
|
+
const touchSensor = useSensor(TouchSensor, { activationConstraint: touchConstraints });
|
|
391
|
+
const keyboardSensor = useSensor(KeyboardSensor, {});
|
|
392
|
+
const sensors = useSensors(mouseSensor, touchSensor, keyboardSensor);
|
|
393
|
+
|
|
394
|
+
const [active, setActive] = useState<Active | null>(null);
|
|
395
|
+
const handleDragStart = ({ active }: DragStartEvent) => {
|
|
396
|
+
setActive(active);
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
const handleDragEnd = ({ over, active }: DragEndEvent) => {
|
|
400
|
+
if (over && over.id !== active.id) {
|
|
401
|
+
setActive(null);
|
|
402
|
+
onMove?.(active.data.current!.index, over.data.current!.index);
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
const snapToCenter: Modifier = ({ activatorEvent, draggingNodeRect, transform }) => {
|
|
407
|
+
if (draggingNodeRect && activatorEvent) {
|
|
408
|
+
const activatorCoordinates = getEventCoordinates(activatorEvent);
|
|
409
|
+
if (!activatorCoordinates) {
|
|
410
|
+
return transform;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const offset = activatorCoordinates.y - draggingNodeRect.top;
|
|
414
|
+
return {
|
|
415
|
+
...transform,
|
|
416
|
+
y: transform.y + offset - draggingNodeRect.height / 2,
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
return transform;
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
return (
|
|
424
|
+
<div className='relative flex grow overflow-hidden'>
|
|
425
|
+
{/* Fixed border. */}
|
|
426
|
+
<div
|
|
427
|
+
className={mx('z-20 absolute inset-0 border-y pointer-events-none', fragments.border)}
|
|
428
|
+
style={{ width: axisWidth }}
|
|
429
|
+
/>
|
|
430
|
+
|
|
431
|
+
{/* Scrollbar. */}
|
|
432
|
+
<div ref={forwardRef} role='rowheader' className='grow overflow-y-auto scrollbar-none'>
|
|
433
|
+
<DndContext
|
|
434
|
+
sensors={sensors}
|
|
435
|
+
modifiers={[restrictToVerticalAxis, snapToCenter]}
|
|
436
|
+
onDragStart={handleDragStart}
|
|
437
|
+
onDragEnd={handleDragEnd}
|
|
438
|
+
>
|
|
439
|
+
<div className='flex flex-col' style={{ width: axisWidth }}>
|
|
440
|
+
{rows.map((idx, index) => (
|
|
441
|
+
<GridRowCell
|
|
442
|
+
key={idx}
|
|
443
|
+
idx={idx}
|
|
444
|
+
index={index}
|
|
445
|
+
label={String(index + 1)}
|
|
446
|
+
size={sizes?.[idx] ?? defaultHeight}
|
|
447
|
+
resize={index < rows.length - 1}
|
|
448
|
+
selected={selected === index}
|
|
449
|
+
onResize={onResize}
|
|
450
|
+
onSelect={onSelect}
|
|
451
|
+
/>
|
|
452
|
+
))}
|
|
453
|
+
</div>
|
|
454
|
+
|
|
455
|
+
{createPortal(
|
|
456
|
+
<DragOverlay>{active && <MovingOverlay label={String(active.data.current!.index + 1)} />}</DragOverlay>,
|
|
457
|
+
document.body,
|
|
458
|
+
)}
|
|
459
|
+
</DndContext>
|
|
460
|
+
</div>
|
|
461
|
+
</div>
|
|
462
|
+
);
|
|
463
|
+
},
|
|
464
|
+
);
|
|
465
|
+
|
|
466
|
+
const GridRowCell = ({ idx, index, label, size, resize, selected, onSelect, onResize }: RowColumnProps) => {
|
|
467
|
+
const { setNodeRef: setDroppableNodeRef } = useDroppable({ id: idx, data: { index } });
|
|
468
|
+
const {
|
|
469
|
+
setNodeRef: setDraggableNodeRef,
|
|
470
|
+
attributes,
|
|
471
|
+
listeners,
|
|
472
|
+
isDragging,
|
|
473
|
+
over,
|
|
474
|
+
} = useDraggable({ id: idx, data: { index } });
|
|
475
|
+
const setNodeRef = useCombinedRefs(setDroppableNodeRef, setDraggableNodeRef);
|
|
476
|
+
const [initialSize, setInitialSize] = useState(size);
|
|
477
|
+
const [resizing, setResizing] = useState(false);
|
|
478
|
+
|
|
479
|
+
// Lock scroll container while resizing (fixes scroll bug).
|
|
480
|
+
// https://github.com/bokuweb/re-resizable/issues/727
|
|
481
|
+
const scrollHandler = useRef<any>();
|
|
482
|
+
const handleResizeStart: ResizeStartCallback = (_ev, _dir, elementRef) => {
|
|
483
|
+
const scrollContainer = elementRef.closest<HTMLDivElement>('[role="rowheader"]')!;
|
|
484
|
+
const scrollTop = scrollContainer.scrollTop;
|
|
485
|
+
scrollHandler.current = (ev: Event) => ((ev.target as HTMLElement).scrollTop = scrollTop);
|
|
486
|
+
scrollContainer.addEventListener('scroll', scrollHandler.current);
|
|
487
|
+
scrollContainer.dataset.locked = 'true';
|
|
488
|
+
setResizing(true);
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
const handleResize: ResizeCallback = (_ev, _dir, _elementRef, { height }) => {
|
|
492
|
+
onResize?.(idx, initialSize + height);
|
|
493
|
+
};
|
|
494
|
+
|
|
495
|
+
const handleResizeStop: ResizeCallback = (_ev, _dir, elementRef, { height }) => {
|
|
496
|
+
const scrollContainer = elementRef.closest<HTMLDivElement>('[role="rowheader"]')!;
|
|
497
|
+
scrollContainer.removeEventListener('scroll', scrollHandler.current!);
|
|
498
|
+
delete scrollContainer.dataset.locked;
|
|
499
|
+
scrollHandler.current = undefined;
|
|
500
|
+
setInitialSize(initialSize + height);
|
|
501
|
+
onResize?.(idx, initialSize + height, true);
|
|
502
|
+
setResizing(false);
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
// Row.
|
|
506
|
+
return (
|
|
507
|
+
<Resizable
|
|
508
|
+
enable={{ bottom: resize }}
|
|
509
|
+
size={{ height: size - 1 }}
|
|
510
|
+
minHeight={minHeight - 1}
|
|
511
|
+
maxHeight={maxHeight}
|
|
512
|
+
onResizeStart={handleResizeStart}
|
|
513
|
+
onResize={handleResize}
|
|
514
|
+
onResizeStop={handleResizeStop}
|
|
515
|
+
>
|
|
516
|
+
<div
|
|
517
|
+
ref={setNodeRef}
|
|
518
|
+
{...attributes}
|
|
519
|
+
{...listeners}
|
|
520
|
+
className={mx(
|
|
521
|
+
'flex h-full items-center justify-center cursor-pointer',
|
|
522
|
+
'border-t focus-visible:outline-none',
|
|
523
|
+
fragments.border,
|
|
524
|
+
fragments.axis,
|
|
525
|
+
selected && fragments.axisSelected,
|
|
526
|
+
isDragging && fragments.axisSelected,
|
|
527
|
+
)}
|
|
528
|
+
onClick={() => onSelect?.(index)}
|
|
529
|
+
>
|
|
530
|
+
<span className='flex w-full justify-center'>{label}</span>
|
|
531
|
+
|
|
532
|
+
{/* Drop indicator. */}
|
|
533
|
+
{over?.id === idx && !isDragging && (
|
|
534
|
+
<div className='z-20 absolute top-0 w-full min-h-[4px] border-b-4 border-primary-500' />
|
|
535
|
+
)}
|
|
536
|
+
|
|
537
|
+
{/* Resize indicator. */}
|
|
538
|
+
{resizing && <div className='z-20 absolute bottom-0 w-full min-h-[4px] border-b-4 border-primary-500' />}
|
|
539
|
+
</div>
|
|
540
|
+
</Resizable>
|
|
541
|
+
);
|
|
542
|
+
};
|
|
543
|
+
|
|
544
|
+
//
|
|
545
|
+
// Columns
|
|
546
|
+
//
|
|
547
|
+
|
|
548
|
+
type SheetColumnsProps = { columns: CellIndex[] } & RowColumnSelection & ResizeProps & MoveProps;
|
|
549
|
+
|
|
550
|
+
const SheetColumns = forwardRef<HTMLDivElement, SheetColumnsProps>(
|
|
551
|
+
({ columns, sizes, selected, onSelect, onResize, onMove }, forwardRef) => {
|
|
552
|
+
const mouseSensor = useSensor(MouseSensor, { activationConstraint: mouseConstraints });
|
|
553
|
+
const touchSensor = useSensor(TouchSensor, { activationConstraint: touchConstraints });
|
|
554
|
+
const keyboardSensor = useSensor(KeyboardSensor, {});
|
|
555
|
+
const sensors = useSensors(mouseSensor, touchSensor, keyboardSensor);
|
|
556
|
+
|
|
557
|
+
const [active, setActive] = useState<Active | null>(null);
|
|
558
|
+
const handleDragStart = ({ active }: DragStartEvent) => {
|
|
559
|
+
setActive(active);
|
|
560
|
+
};
|
|
561
|
+
|
|
562
|
+
const handleDragEnd = ({ active, over }: DragEndEvent) => {
|
|
563
|
+
if (over && over.id !== active.id) {
|
|
564
|
+
setActive(null);
|
|
565
|
+
onMove?.(active.data.current!.index, over.data.current!.index);
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
|
|
569
|
+
const snapToCenter: Modifier = ({ activatorEvent, draggingNodeRect, transform }) => {
|
|
570
|
+
if (draggingNodeRect && activatorEvent) {
|
|
571
|
+
const activatorCoordinates = getEventCoordinates(activatorEvent);
|
|
572
|
+
if (!activatorCoordinates) {
|
|
573
|
+
return transform;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
const offset = activatorCoordinates.x - draggingNodeRect.left;
|
|
577
|
+
return {
|
|
578
|
+
...transform,
|
|
579
|
+
x: transform.x + offset - draggingNodeRect.width / 2,
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
return transform;
|
|
584
|
+
};
|
|
585
|
+
|
|
586
|
+
return (
|
|
587
|
+
<div className='relative flex grow overflow-hidden'>
|
|
588
|
+
{/* Fixed border. */}
|
|
589
|
+
<div
|
|
590
|
+
className={mx('z-20 absolute inset-0 border-x pointer-events-none', fragments.border)}
|
|
591
|
+
style={{ height: axisHeight }}
|
|
592
|
+
/>
|
|
593
|
+
|
|
594
|
+
{/* Scrollbar. */}
|
|
595
|
+
<div ref={forwardRef} role='columnheader' className='grow overflow-x-auto scrollbar-none'>
|
|
596
|
+
<DndContext
|
|
597
|
+
autoScroll={{ enabled: true }}
|
|
598
|
+
sensors={sensors}
|
|
599
|
+
modifiers={[restrictToHorizontalAxis, snapToCenter]}
|
|
600
|
+
onDragStart={handleDragStart}
|
|
601
|
+
onDragEnd={handleDragEnd}
|
|
602
|
+
>
|
|
603
|
+
<div className='flex h-full' style={{ height: axisHeight }}>
|
|
604
|
+
{columns.map((idx, index) => (
|
|
605
|
+
<GridColumnCell
|
|
606
|
+
key={idx}
|
|
607
|
+
idx={idx}
|
|
608
|
+
index={index}
|
|
609
|
+
label={columnLetter(index)}
|
|
610
|
+
size={sizes?.[idx] ?? defaultWidth}
|
|
611
|
+
resize={index < columns.length - 1}
|
|
612
|
+
selected={selected === index}
|
|
613
|
+
onResize={onResize}
|
|
614
|
+
onSelect={onSelect}
|
|
615
|
+
/>
|
|
616
|
+
))}
|
|
617
|
+
</div>
|
|
618
|
+
|
|
619
|
+
{createPortal(
|
|
620
|
+
<DragOverlay>{active && <MovingOverlay label={columnLetter(active.data.current!.index)} />}</DragOverlay>,
|
|
621
|
+
document.body,
|
|
622
|
+
)}
|
|
623
|
+
</DndContext>
|
|
624
|
+
</div>
|
|
625
|
+
</div>
|
|
626
|
+
);
|
|
627
|
+
},
|
|
628
|
+
);
|
|
629
|
+
|
|
630
|
+
const GridColumnCell = ({ idx, index, label, size, resize, selected, onSelect, onResize }: RowColumnProps) => {
|
|
631
|
+
const { setNodeRef: setDroppableNodeRef } = useDroppable({ id: idx, data: { index } });
|
|
632
|
+
const {
|
|
633
|
+
setNodeRef: setDraggableNodeRef,
|
|
634
|
+
attributes,
|
|
635
|
+
listeners,
|
|
636
|
+
over,
|
|
637
|
+
isDragging,
|
|
638
|
+
} = useDraggable({ id: idx, data: { index } });
|
|
639
|
+
const setNodeRef = useCombinedRefs(setDroppableNodeRef, setDraggableNodeRef);
|
|
640
|
+
const [initialSize, setInitialSize] = useState(size);
|
|
641
|
+
const [resizing, setResizing] = useState(false);
|
|
642
|
+
|
|
643
|
+
// Lock scroll container while resizing (fixes scroll bug).
|
|
644
|
+
// https://github.com/bokuweb/re-resizable/issues/727
|
|
645
|
+
const scrollHandler = useRef<any>();
|
|
646
|
+
const handleResizeStart: ResizeStartCallback = (_ev, _dir, elementRef) => {
|
|
647
|
+
const scrollContainer = elementRef.closest<HTMLDivElement>('[role="columnheader"]')!;
|
|
648
|
+
const scrollLeft = scrollContainer.scrollLeft;
|
|
649
|
+
scrollHandler.current = (ev: Event) => ((ev.target as HTMLElement).scrollLeft = scrollLeft);
|
|
650
|
+
scrollContainer.addEventListener('scroll', scrollHandler.current);
|
|
651
|
+
scrollContainer.dataset.locked = 'true';
|
|
652
|
+
setResizing(true);
|
|
653
|
+
};
|
|
654
|
+
|
|
655
|
+
const handleResize: ResizeCallback = (_ev, _dir, _elementRef, { width }) => {
|
|
656
|
+
onResize?.(idx, initialSize + width);
|
|
657
|
+
};
|
|
658
|
+
|
|
659
|
+
const handleResizeStop: ResizeCallback = (_ev, _dir, elementRef, { width }) => {
|
|
660
|
+
const scrollContainer = elementRef.closest<HTMLDivElement>('[role="columnheader"]')!;
|
|
661
|
+
scrollContainer.removeEventListener('scroll', scrollHandler.current!);
|
|
662
|
+
delete scrollContainer.dataset.locked;
|
|
663
|
+
scrollHandler.current = undefined;
|
|
664
|
+
setInitialSize(initialSize + width);
|
|
665
|
+
onResize?.(idx, initialSize + width, true);
|
|
666
|
+
setResizing(false);
|
|
667
|
+
};
|
|
668
|
+
|
|
669
|
+
// Column.
|
|
670
|
+
return (
|
|
671
|
+
<Resizable
|
|
672
|
+
enable={{ right: resize }}
|
|
673
|
+
size={{ width: size - 1 }}
|
|
674
|
+
minWidth={minWidth - 1}
|
|
675
|
+
maxWidth={maxWidth}
|
|
676
|
+
onResizeStart={handleResizeStart}
|
|
677
|
+
onResize={handleResize}
|
|
678
|
+
onResizeStop={handleResizeStop}
|
|
679
|
+
>
|
|
680
|
+
<div
|
|
681
|
+
ref={setNodeRef}
|
|
682
|
+
{...attributes}
|
|
683
|
+
{...listeners}
|
|
684
|
+
className={mx(
|
|
685
|
+
'flex h-full items-center justify-center cursor-pointer',
|
|
686
|
+
'border-l focus-visible:outline-none',
|
|
687
|
+
fragments.border,
|
|
688
|
+
fragments.axis,
|
|
689
|
+
selected && fragments.axisSelected,
|
|
690
|
+
isDragging && fragments.axisSelected,
|
|
691
|
+
)}
|
|
692
|
+
onClick={() => onSelect?.(index)}
|
|
693
|
+
>
|
|
694
|
+
<span className='flex w-full justify-center'>{label}</span>
|
|
695
|
+
|
|
696
|
+
{/* Drop indicator. */}
|
|
697
|
+
{over?.id === idx && !isDragging && (
|
|
698
|
+
<div className='z-20 absolute left-0 h-full min-w-[4px] border-l-4 border-primary-500' />
|
|
699
|
+
)}
|
|
700
|
+
|
|
701
|
+
{/* Resize indicator. */}
|
|
702
|
+
{resizing && <div className='z-20 absolute right-0 h-full min-h-[4px] border-l-4 border-primary-500' />}
|
|
703
|
+
</div>
|
|
704
|
+
</Resizable>
|
|
705
|
+
);
|
|
706
|
+
};
|
|
707
|
+
|
|
708
|
+
//
|
|
709
|
+
// Content
|
|
710
|
+
//
|
|
711
|
+
|
|
712
|
+
type SheetGridProps = GridLayoutProps & {
|
|
713
|
+
size: GridSize;
|
|
714
|
+
};
|
|
715
|
+
|
|
716
|
+
const SheetGrid = forwardRef<HTMLDivElement, SheetGridProps>(
|
|
717
|
+
({ size, rows, columns, rowSizes, columnSizes }, forwardRef) => {
|
|
718
|
+
const {
|
|
719
|
+
ref: containerRef,
|
|
720
|
+
width: containerWidth = 0,
|
|
721
|
+
height: containerHeight = 0,
|
|
722
|
+
} = useResizeDetector({ refreshRate: 200 });
|
|
723
|
+
const scrollerRef = useRef<HTMLDivElement>(null);
|
|
724
|
+
useImperativeHandle(forwardRef, () => scrollerRef.current!);
|
|
725
|
+
|
|
726
|
+
const { model, cursor, range, editing, setCursor, setRange, setEditing, onInfo } = useSheetContext();
|
|
727
|
+
const initialText = useRef<string>();
|
|
728
|
+
const quickEdit = useRef(false);
|
|
729
|
+
|
|
730
|
+
// Listen for async calculation updates.
|
|
731
|
+
const [, forceUpdate] = useState({});
|
|
732
|
+
useEffect(() => {
|
|
733
|
+
const unsubscribe = model.update.on(() => {
|
|
734
|
+
log('updated', { id: model.id });
|
|
735
|
+
forceUpdate({});
|
|
736
|
+
});
|
|
737
|
+
|
|
738
|
+
return () => {
|
|
739
|
+
unsubscribe();
|
|
740
|
+
};
|
|
741
|
+
}, [model]);
|
|
742
|
+
|
|
743
|
+
//
|
|
744
|
+
// Event handling.
|
|
745
|
+
//
|
|
746
|
+
|
|
747
|
+
const inputRef = useRef<HTMLInputElement>(null);
|
|
748
|
+
const handleKeyDown: DOMAttributes<HTMLInputElement>['onKeyDown'] = (ev) => {
|
|
749
|
+
// Cut-and-paste.
|
|
750
|
+
const isMacOS = /Mac|iPhone|iPod|iPad/.test(navigator.userAgent);
|
|
751
|
+
if (cursor && ((isMacOS && ev.metaKey) || ev.ctrlKey)) {
|
|
752
|
+
switch (ev.key) {
|
|
753
|
+
case 'x': {
|
|
754
|
+
model.cut(range ?? { from: cursor });
|
|
755
|
+
return;
|
|
756
|
+
}
|
|
757
|
+
case 'c': {
|
|
758
|
+
model.copy(range ?? { from: cursor });
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
761
|
+
case 'v': {
|
|
762
|
+
model.paste(cursor);
|
|
763
|
+
return;
|
|
764
|
+
}
|
|
765
|
+
case 'z': {
|
|
766
|
+
if (ev.shiftKey) {
|
|
767
|
+
model.redo();
|
|
768
|
+
} else {
|
|
769
|
+
model.undo();
|
|
770
|
+
}
|
|
771
|
+
return;
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
switch (ev.key) {
|
|
777
|
+
case 'ArrowUp':
|
|
778
|
+
case 'ArrowDown':
|
|
779
|
+
case 'ArrowLeft':
|
|
780
|
+
case 'ArrowRight':
|
|
781
|
+
case 'Home':
|
|
782
|
+
case 'End': {
|
|
783
|
+
const next = handleNav(ev, cursor, range, size);
|
|
784
|
+
setRange(next.range);
|
|
785
|
+
if (next.cursor) {
|
|
786
|
+
setCursor(next.cursor);
|
|
787
|
+
const element = getCellElement(scrollerRef.current!, next.cursor);
|
|
788
|
+
if (element) {
|
|
789
|
+
scrollIntoView(scrollerRef.current!, element);
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
break;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
case 'Backspace': {
|
|
796
|
+
if (cursor) {
|
|
797
|
+
if (range) {
|
|
798
|
+
model.clear(range);
|
|
799
|
+
} else {
|
|
800
|
+
model.setValue(cursor, null);
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
break;
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
case 'Escape': {
|
|
807
|
+
setRange(undefined);
|
|
808
|
+
break;
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
case 'Enter': {
|
|
812
|
+
ev.stopPropagation();
|
|
813
|
+
if (cursor) {
|
|
814
|
+
setEditing(true);
|
|
815
|
+
}
|
|
816
|
+
break;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
case '?': {
|
|
820
|
+
onInfo?.();
|
|
821
|
+
break;
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
default: {
|
|
825
|
+
if (ev.key.length === 1) {
|
|
826
|
+
initialText.current = ev.key;
|
|
827
|
+
quickEdit.current = true;
|
|
828
|
+
setEditing(true);
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
};
|
|
833
|
+
|
|
834
|
+
// Mouse handlers for selection.
|
|
835
|
+
const { handlers } = useRangeSelect((event, range) => {
|
|
836
|
+
switch (event) {
|
|
837
|
+
case 'start': {
|
|
838
|
+
// if (!editing) {
|
|
839
|
+
// setCursor(range?.from);
|
|
840
|
+
// }
|
|
841
|
+
setRange(undefined);
|
|
842
|
+
break;
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
default: {
|
|
846
|
+
setRange(range);
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
});
|
|
850
|
+
|
|
851
|
+
// Calculate visible grid.
|
|
852
|
+
const { width, height, rowRange, columnRange } = useGridLayout({
|
|
853
|
+
scroller: scrollerRef.current,
|
|
854
|
+
size: { width: containerWidth, height: containerHeight },
|
|
855
|
+
rows,
|
|
856
|
+
columns,
|
|
857
|
+
rowSizes,
|
|
858
|
+
columnSizes,
|
|
859
|
+
});
|
|
860
|
+
|
|
861
|
+
const qualifiedSubjectId = fullyQualifiedId(model.sheet);
|
|
862
|
+
const attendableAttrs = createAttendableAttributes(qualifiedSubjectId);
|
|
863
|
+
|
|
864
|
+
return (
|
|
865
|
+
<div ref={containerRef} role='grid' className='relative flex grow overflow-hidden'>
|
|
866
|
+
{/* Fixed border. */}
|
|
867
|
+
<div className={mx('z-20 absolute inset-0 border pointer-events-none', fragments.border)} />
|
|
868
|
+
|
|
869
|
+
{/* Grid scroll container. */}
|
|
870
|
+
<div ref={scrollerRef} className='grow overflow-auto scrollbar-thin'>
|
|
871
|
+
{/* Scroll content. */}
|
|
872
|
+
<div
|
|
873
|
+
className='relative select-none'
|
|
874
|
+
style={{ width, height }}
|
|
875
|
+
onClick={() => inputRef.current?.focus()}
|
|
876
|
+
{...handlers}
|
|
877
|
+
>
|
|
878
|
+
{/* Selection. */}
|
|
879
|
+
{scrollerRef.current && <SelectionOverlay root={scrollerRef.current} />}
|
|
880
|
+
|
|
881
|
+
{/* Grid cells. */}
|
|
882
|
+
{rowRange.map(({ row, top, height }) => {
|
|
883
|
+
return columnRange.map(({ column, left, width }) => {
|
|
884
|
+
const style: CSSProperties = { position: 'absolute', top, left, width, height };
|
|
885
|
+
const cell = { row, column };
|
|
886
|
+
const id = addressToA1Notation(cell);
|
|
887
|
+
const idx = model.addressToIndex(cell);
|
|
888
|
+
const active = posEquals(cursor, cell);
|
|
889
|
+
if (active && editing) {
|
|
890
|
+
const value = initialText.current ?? model.getCellText(cell) ?? '';
|
|
891
|
+
|
|
892
|
+
// TODO(burdon): Validate formula before closing: hf.validateFormula();
|
|
893
|
+
const handleClose: GridCellEditorProps['onClose'] = (value) => {
|
|
894
|
+
initialText.current = undefined;
|
|
895
|
+
quickEdit.current = false;
|
|
896
|
+
if (value !== undefined) {
|
|
897
|
+
model.setValue(cell, value);
|
|
898
|
+
// Auto-advance to next cell.
|
|
899
|
+
const next = handleArrowNav({ key: 'ArrowDown', metaKey: false }, cursor, size);
|
|
900
|
+
if (next) {
|
|
901
|
+
setCursor(next);
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
inputRef.current?.focus();
|
|
905
|
+
setEditing(false);
|
|
906
|
+
};
|
|
907
|
+
|
|
908
|
+
// Quick entry mode: i.e., typing to enter cell.
|
|
909
|
+
const handleNav: GridCellEditorProps['onNav'] = (value, { key }) => {
|
|
910
|
+
initialText.current = undefined;
|
|
911
|
+
model.setValue(cell, value ?? null);
|
|
912
|
+
const next = handleArrowNav({ key, metaKey: false }, cursor, size);
|
|
913
|
+
if (next) {
|
|
914
|
+
setCursor(next);
|
|
915
|
+
}
|
|
916
|
+
inputRef.current?.focus();
|
|
917
|
+
setEditing(false);
|
|
918
|
+
};
|
|
919
|
+
|
|
920
|
+
return (
|
|
921
|
+
<GridCellEditor
|
|
922
|
+
key={idx}
|
|
923
|
+
value={value}
|
|
924
|
+
style={style}
|
|
925
|
+
onNav={quickEdit.current ? handleNav : undefined}
|
|
926
|
+
onClose={handleClose}
|
|
927
|
+
/>
|
|
928
|
+
);
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
return (
|
|
932
|
+
<SheetCell
|
|
933
|
+
key={id}
|
|
934
|
+
id={id}
|
|
935
|
+
cell={cell}
|
|
936
|
+
active={active}
|
|
937
|
+
style={style}
|
|
938
|
+
onSelect={(cell, edit) => {
|
|
939
|
+
setEditing(edit);
|
|
940
|
+
setCursor(cell);
|
|
941
|
+
}}
|
|
942
|
+
/>
|
|
943
|
+
);
|
|
944
|
+
});
|
|
945
|
+
})}
|
|
946
|
+
</div>
|
|
947
|
+
</div>
|
|
948
|
+
|
|
949
|
+
{/* Hidden input for key navigation. */}
|
|
950
|
+
{createPortal(
|
|
951
|
+
<input
|
|
952
|
+
ref={inputRef}
|
|
953
|
+
autoFocus
|
|
954
|
+
className='absolute w-[1px] h-[1px] bg-transparent outline-none border-none caret-transparent'
|
|
955
|
+
onKeyDown={handleKeyDown}
|
|
956
|
+
{...attendableAttrs}
|
|
957
|
+
/>,
|
|
958
|
+
document.body,
|
|
959
|
+
)}
|
|
960
|
+
</div>
|
|
961
|
+
);
|
|
962
|
+
},
|
|
963
|
+
);
|
|
964
|
+
|
|
965
|
+
//
|
|
966
|
+
// Selection
|
|
967
|
+
//
|
|
968
|
+
|
|
969
|
+
const SelectionOverlay = ({ root }: { root: HTMLDivElement }) => {
|
|
970
|
+
const { range } = useSheetContext();
|
|
971
|
+
if (!range) {
|
|
972
|
+
return null;
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
const c1 = getCellElement(root, range.from);
|
|
976
|
+
const c2 = range.to ? getCellElement(root, range.to)! : c1;
|
|
977
|
+
if (!c1 || !c2) {
|
|
978
|
+
return null;
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
// TODO(burdon): Instead of measuring cells, get from grid layout?
|
|
982
|
+
const b1 = getRelativeClientRect(root, c1);
|
|
983
|
+
const b2 = getRelativeClientRect(root, c2);
|
|
984
|
+
const bounds = getRectUnion(b1, b2);
|
|
985
|
+
|
|
986
|
+
return (
|
|
987
|
+
<div
|
|
988
|
+
role='none'
|
|
989
|
+
style={bounds}
|
|
990
|
+
className='z-10 absolute pointer-events-none bg-primary-500/20 border border-primary-500/50'
|
|
991
|
+
/>
|
|
992
|
+
);
|
|
993
|
+
};
|
|
994
|
+
|
|
995
|
+
//
|
|
996
|
+
// Cell
|
|
997
|
+
//
|
|
998
|
+
|
|
999
|
+
type SheetCellProps = {
|
|
1000
|
+
id: string; // TODO(burdon): Should this be the index?
|
|
1001
|
+
cell: CellAddress;
|
|
1002
|
+
style: CSSProperties;
|
|
1003
|
+
active: boolean;
|
|
1004
|
+
onSelect?: (selected: CellAddress, edit: boolean) => void;
|
|
1005
|
+
};
|
|
1006
|
+
|
|
1007
|
+
const SheetCell = ({ id, cell, style, active, onSelect }: SheetCellProps) => {
|
|
1008
|
+
const { formatting, editing, setRange } = useSheetContext();
|
|
1009
|
+
const { value, classNames } = formatting.getFormatting(cell);
|
|
1010
|
+
|
|
1011
|
+
return (
|
|
1012
|
+
<div
|
|
1013
|
+
{...{ [`data-${CELL_DATA_KEY}`]: id }}
|
|
1014
|
+
role='cell'
|
|
1015
|
+
style={style}
|
|
1016
|
+
className={mx(
|
|
1017
|
+
'flex w-full h-full truncate items-center border cursor-pointer',
|
|
1018
|
+
'px-2 py-1',
|
|
1019
|
+
fragments.cell,
|
|
1020
|
+
fragments.border,
|
|
1021
|
+
active && ['z-20', fragments.cellSelected],
|
|
1022
|
+
classNames,
|
|
1023
|
+
)}
|
|
1024
|
+
onClick={() => {
|
|
1025
|
+
if (editing) {
|
|
1026
|
+
setRange?.({ from: cell });
|
|
1027
|
+
} else {
|
|
1028
|
+
onSelect?.(cell, false);
|
|
1029
|
+
}
|
|
1030
|
+
}}
|
|
1031
|
+
onDoubleClick={() => onSelect?.(cell, true)}
|
|
1032
|
+
>
|
|
1033
|
+
{value}
|
|
1034
|
+
</div>
|
|
1035
|
+
);
|
|
1036
|
+
};
|
|
1037
|
+
|
|
1038
|
+
type GridCellEditorProps = {
|
|
1039
|
+
style: CSSProperties;
|
|
1040
|
+
value: string;
|
|
1041
|
+
} & EditorKeysProps;
|
|
1042
|
+
|
|
1043
|
+
const GridCellEditor = ({ style, value, onNav, onClose }: GridCellEditorProps) => {
|
|
1044
|
+
const { model, range } = useSheetContext();
|
|
1045
|
+
const notifier = useRef<CellRangeNotifier>();
|
|
1046
|
+
useEffect(() => {
|
|
1047
|
+
if (range) {
|
|
1048
|
+
// Update range selection in formula.
|
|
1049
|
+
notifier.current?.(rangeToA1Notation(range));
|
|
1050
|
+
}
|
|
1051
|
+
}, [range]);
|
|
1052
|
+
const [extension] = useState(() => {
|
|
1053
|
+
return [
|
|
1054
|
+
editorKeys({ onNav, onClose }),
|
|
1055
|
+
sheetExtension({ functions: model.functions }),
|
|
1056
|
+
rangeExtension((fn) => (notifier.current = fn)),
|
|
1057
|
+
];
|
|
1058
|
+
});
|
|
1059
|
+
|
|
1060
|
+
return (
|
|
1061
|
+
<div
|
|
1062
|
+
role='cell'
|
|
1063
|
+
style={style}
|
|
1064
|
+
className={mx('z-20 flex', fragments.cellSelected)}
|
|
1065
|
+
onClick={(ev) => ev.stopPropagation()}
|
|
1066
|
+
>
|
|
1067
|
+
<CellEditor autoFocus value={value} extension={extension} />
|
|
1068
|
+
</div>
|
|
1069
|
+
);
|
|
1070
|
+
};
|
|
1071
|
+
|
|
1072
|
+
//
|
|
1073
|
+
// StatusBar
|
|
1074
|
+
//
|
|
1075
|
+
|
|
1076
|
+
const SheetStatusBar = () => {
|
|
1077
|
+
const { model, cursor, range } = useSheetContext();
|
|
1078
|
+
let value;
|
|
1079
|
+
let isFormula = false;
|
|
1080
|
+
if (cursor) {
|
|
1081
|
+
value = model.getCellValue(cursor);
|
|
1082
|
+
if (typeof value === 'string' && value.charAt(0) === '=') {
|
|
1083
|
+
value = model.mapFormulaIndicesToRefs(value);
|
|
1084
|
+
isFormula = true;
|
|
1085
|
+
} else if (value != null) {
|
|
1086
|
+
value = String(value);
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
return (
|
|
1091
|
+
<div className={mx('flex shrink-0 justify-between items-center px-4 py-1 text-sm border-x', fragments.border)}>
|
|
1092
|
+
<div className='flex gap-4 items-center'>
|
|
1093
|
+
<div className='flex w-16 items-center font-mono'>
|
|
1094
|
+
{(range && rangeToA1Notation(range)) || (cursor && addressToA1Notation(cursor))}
|
|
1095
|
+
</div>
|
|
1096
|
+
<div className='flex gap-2 items-center'>
|
|
1097
|
+
<FunctionIcon className={mx('text-green-500', isFormula ? 'visible' : 'invisible')} />
|
|
1098
|
+
<span className='font-mono'>{value}</span>
|
|
1099
|
+
</div>
|
|
1100
|
+
</div>
|
|
1101
|
+
</div>
|
|
1102
|
+
);
|
|
1103
|
+
};
|
|
1104
|
+
|
|
1105
|
+
//
|
|
1106
|
+
// Debug
|
|
1107
|
+
//
|
|
1108
|
+
|
|
1109
|
+
const SheetDebug = () => {
|
|
1110
|
+
const { model, cursor, range } = useSheetContext();
|
|
1111
|
+
const [, forceUpdate] = useState({});
|
|
1112
|
+
useEffect(() => {
|
|
1113
|
+
// TODO(burdon): This is called without registering a listener.
|
|
1114
|
+
const accessor = createDocAccessor(model.sheet, []);
|
|
1115
|
+
const handleUpdate = () => forceUpdate({});
|
|
1116
|
+
accessor.handle.addListener('change', handleUpdate);
|
|
1117
|
+
handleUpdate();
|
|
1118
|
+
return () => {
|
|
1119
|
+
accessor.handle.removeListener('change', handleUpdate);
|
|
1120
|
+
};
|
|
1121
|
+
}, [model]);
|
|
1122
|
+
|
|
1123
|
+
return (
|
|
1124
|
+
<div
|
|
1125
|
+
className={mx(
|
|
1126
|
+
'z-20 absolute right-0 top-20 bottom-20 w-[30rem] overflow-auto scrollbar-thin',
|
|
1127
|
+
'border text-xs bg-neutral-50 dark:bg-black text-cyan-500 font-mono p-1 opacity-80',
|
|
1128
|
+
fragments.border,
|
|
1129
|
+
)}
|
|
1130
|
+
>
|
|
1131
|
+
<pre className='whitespace-pre-wrap'>
|
|
1132
|
+
{JSON.stringify(
|
|
1133
|
+
{
|
|
1134
|
+
cursor,
|
|
1135
|
+
range,
|
|
1136
|
+
cells: model.sheet.cells,
|
|
1137
|
+
rowMeta: model.sheet.rowMeta,
|
|
1138
|
+
columnMeta: model.sheet.columnMeta,
|
|
1139
|
+
formatting: model.sheet.formatting,
|
|
1140
|
+
},
|
|
1141
|
+
undefined,
|
|
1142
|
+
2,
|
|
1143
|
+
)}
|
|
1144
|
+
</pre>
|
|
1145
|
+
</div>
|
|
1146
|
+
);
|
|
1147
|
+
};
|
|
1148
|
+
|
|
1149
|
+
//
|
|
1150
|
+
// Grid
|
|
1151
|
+
//
|
|
1152
|
+
|
|
1153
|
+
export const Sheet = {
|
|
1154
|
+
Root: SheetRoot,
|
|
1155
|
+
Main: SheetMain,
|
|
1156
|
+
Rows: SheetRows,
|
|
1157
|
+
Columns: SheetColumns,
|
|
1158
|
+
Grid: SheetGrid,
|
|
1159
|
+
Cell: SheetCell,
|
|
1160
|
+
StatusBar: SheetStatusBar,
|
|
1161
|
+
Debug: SheetDebug,
|
|
1162
|
+
};
|
|
1163
|
+
|
|
1164
|
+
export type { SheetRootProps, SheetMainProps, SheetRowsProps, SheetColumnsProps, SheetGridProps, SheetCellProps };
|