@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,106 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type ClassNameValue } from '@dxos/react-ui-types';
|
|
6
|
+
|
|
7
|
+
import { type SheetModel, type CellAddress, inRange } from '../../model';
|
|
8
|
+
import { ValueTypeEnum } from '../../types';
|
|
9
|
+
|
|
10
|
+
export class FormattingModel {
|
|
11
|
+
constructor(private readonly model: SheetModel) {}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Get formatted string value and className for cell.
|
|
15
|
+
*/
|
|
16
|
+
getFormatting(cell: CellAddress): { value?: string; classNames?: ClassNameValue } {
|
|
17
|
+
const value = this.model.getValue(cell);
|
|
18
|
+
if (value === undefined || value === null) {
|
|
19
|
+
return {};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// TODO(burdon): Locale.
|
|
23
|
+
const locales = undefined;
|
|
24
|
+
|
|
25
|
+
// Cell-specific formatting.
|
|
26
|
+
const idx = this.model.addressToIndex(cell);
|
|
27
|
+
let formatting = this.model.sheet.formatting?.[idx] ?? {};
|
|
28
|
+
const classNames = [...(formatting?.classNames ?? [])];
|
|
29
|
+
|
|
30
|
+
// Range formatting.
|
|
31
|
+
// TODO(burdon): NOTE: D0 means the D column.
|
|
32
|
+
// TODO(burdon): Cache model formatting (e.g., for ranges). Create class out of this function.
|
|
33
|
+
for (const [idx, _formatting] of Object.entries(this.model.sheet.formatting)) {
|
|
34
|
+
const range = this.model.rangeFromIndex(idx);
|
|
35
|
+
if (inRange(range, cell)) {
|
|
36
|
+
if (_formatting.classNames) {
|
|
37
|
+
classNames.push(..._formatting.classNames);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// TODO(burdon): Last wins.
|
|
41
|
+
if (_formatting.type) {
|
|
42
|
+
formatting = _formatting;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const defaultNumber = 'justify-end font-mono';
|
|
48
|
+
|
|
49
|
+
const type = formatting?.type ?? this.model.getValueType(cell);
|
|
50
|
+
switch (type) {
|
|
51
|
+
case ValueTypeEnum.Boolean: {
|
|
52
|
+
return {
|
|
53
|
+
value: (value as boolean).toLocaleString().toUpperCase(),
|
|
54
|
+
classNames: [...classNames, value ? '!text-green-500' : '!text-orange-500'],
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
//
|
|
59
|
+
// Numbers.
|
|
60
|
+
//
|
|
61
|
+
|
|
62
|
+
case ValueTypeEnum.Number: {
|
|
63
|
+
return { value: value.toLocaleString(locales), classNames: [...classNames, defaultNumber] };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
case ValueTypeEnum.Percent: {
|
|
67
|
+
return { value: (value as number) * 100 + '%', classNames: [...classNames, defaultNumber] };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
case ValueTypeEnum.Currency: {
|
|
71
|
+
return {
|
|
72
|
+
value: (value as number).toLocaleString(locales, {
|
|
73
|
+
style: 'currency',
|
|
74
|
+
currency: 'USD',
|
|
75
|
+
minimumFractionDigits: 2,
|
|
76
|
+
maximumFractionDigits: 2,
|
|
77
|
+
}),
|
|
78
|
+
classNames: [...classNames, defaultNumber],
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
//
|
|
83
|
+
// Dates.
|
|
84
|
+
//
|
|
85
|
+
|
|
86
|
+
case ValueTypeEnum.DateTime: {
|
|
87
|
+
const date = this.model.toLocalDate(value as number);
|
|
88
|
+
return { value: date.toLocaleString(locales), classNames };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
case ValueTypeEnum.Date: {
|
|
92
|
+
const date = this.model.toLocalDate(value as number);
|
|
93
|
+
return { value: date.toLocaleDateString(locales), classNames };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
case ValueTypeEnum.Time: {
|
|
97
|
+
const date = this.model.toLocalDate(value as number);
|
|
98
|
+
return { value: date.toLocaleTimeString(locales), classNames };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
default: {
|
|
102
|
+
return { value: String(value), classNames };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type MouseEvent, useEffect, useState } from 'react';
|
|
6
|
+
|
|
7
|
+
import { type CellAddress, type CellIndex, addressFromA1Notation, addressToA1Notation } from '../../model';
|
|
8
|
+
|
|
9
|
+
// export type Bounds = Pick<DOMRect, 'left' | 'top' | 'width' | 'height'>;
|
|
10
|
+
// export type Dimension = Pick<DOMRect, 'width' | 'height'>;
|
|
11
|
+
|
|
12
|
+
export type SizeMap = Record<string, number>;
|
|
13
|
+
|
|
14
|
+
export type RowPosition = { row: number } & Pick<DOMRect, 'top' | 'height'>;
|
|
15
|
+
export type ColumnPosition = { column: number } & Pick<DOMRect, 'left' | 'width'>;
|
|
16
|
+
|
|
17
|
+
export const axisWidth = 'calc(var(--rail-size)-2px)';
|
|
18
|
+
export const axisHeight = 34;
|
|
19
|
+
|
|
20
|
+
export const minWidth = 40;
|
|
21
|
+
export const maxWidth = 800;
|
|
22
|
+
|
|
23
|
+
export const minHeight = axisHeight;
|
|
24
|
+
export const maxHeight = 400;
|
|
25
|
+
|
|
26
|
+
export const defaultWidth = 200;
|
|
27
|
+
export const defaultHeight = minHeight;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Cell nodes are identified by their A1 notation.
|
|
31
|
+
*/
|
|
32
|
+
export const CELL_DATA_KEY = 'cell';
|
|
33
|
+
|
|
34
|
+
export type GridLayoutProps = {
|
|
35
|
+
rows: CellIndex[];
|
|
36
|
+
columns: CellIndex[];
|
|
37
|
+
rowSizes?: SizeMap;
|
|
38
|
+
columnSizes?: SizeMap;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export type GridLayout = {
|
|
42
|
+
width: number;
|
|
43
|
+
height: number;
|
|
44
|
+
rowRange: RowPosition[];
|
|
45
|
+
columnRange: ColumnPosition[];
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Calculates the grid geometry for the current viewport.
|
|
50
|
+
*/
|
|
51
|
+
export const useGridLayout = ({
|
|
52
|
+
scroller,
|
|
53
|
+
size,
|
|
54
|
+
rows,
|
|
55
|
+
columns,
|
|
56
|
+
rowSizes,
|
|
57
|
+
columnSizes,
|
|
58
|
+
}: GridLayoutProps & {
|
|
59
|
+
scroller: HTMLDivElement | null;
|
|
60
|
+
size: { width: number; height: number };
|
|
61
|
+
}): GridLayout => {
|
|
62
|
+
const [rowPositions, setRowPositions] = useState<RowPosition[]>([]);
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
if (!rowSizes) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
let y = 0;
|
|
69
|
+
setRowPositions(
|
|
70
|
+
rows.map((idx, i) => {
|
|
71
|
+
const height = rowSizes?.[idx] ?? defaultHeight;
|
|
72
|
+
const top = y;
|
|
73
|
+
y += height - 1;
|
|
74
|
+
return { row: i, top, height };
|
|
75
|
+
}),
|
|
76
|
+
);
|
|
77
|
+
}, [rows, rowSizes]);
|
|
78
|
+
|
|
79
|
+
const [columnPositions, setColumnPositions] = useState<ColumnPosition[]>([]);
|
|
80
|
+
useEffect(() => {
|
|
81
|
+
if (!columns) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
let x = 0;
|
|
86
|
+
setColumnPositions(
|
|
87
|
+
columns.map((idx, i) => {
|
|
88
|
+
const width = columnSizes?.[idx] ?? defaultWidth;
|
|
89
|
+
const left = x;
|
|
90
|
+
x += width - 1;
|
|
91
|
+
return { column: i, left, width };
|
|
92
|
+
}),
|
|
93
|
+
);
|
|
94
|
+
}, [columns, columnSizes]);
|
|
95
|
+
|
|
96
|
+
const height = rowPositions.length
|
|
97
|
+
? rowPositions[rowPositions.length - 1].top + rowPositions[rowPositions.length - 1].height
|
|
98
|
+
: 0;
|
|
99
|
+
|
|
100
|
+
const width = columnPositions.length
|
|
101
|
+
? columnPositions[columnPositions.length - 1].left + columnPositions[columnPositions.length - 1].width
|
|
102
|
+
: 0;
|
|
103
|
+
|
|
104
|
+
//
|
|
105
|
+
// Virtual window.
|
|
106
|
+
// TODO(burdon): Preserve edit state, selection.
|
|
107
|
+
// TODO(burdon): BUG: Doesn't scroll to cursor if jump to end.
|
|
108
|
+
//
|
|
109
|
+
|
|
110
|
+
const [{ rowRange, columnRange }, setWindow] = useState<{
|
|
111
|
+
rowRange: RowPosition[];
|
|
112
|
+
columnRange: ColumnPosition[];
|
|
113
|
+
}>({ rowRange: [], columnRange: [] });
|
|
114
|
+
useEffect(() => {
|
|
115
|
+
const handleScroll = () => {
|
|
116
|
+
if (!scroller) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const { scrollLeft: left, scrollTop: top, clientWidth: width, clientHeight: height } = scroller;
|
|
121
|
+
|
|
122
|
+
let rowStart = 0;
|
|
123
|
+
let rowEnd = 0;
|
|
124
|
+
for (let i = 0; i < rowPositions.length; i++) {
|
|
125
|
+
const row = rowPositions[i];
|
|
126
|
+
if (row.top <= top) {
|
|
127
|
+
rowStart = i;
|
|
128
|
+
}
|
|
129
|
+
if (row.top + row.height >= top + height) {
|
|
130
|
+
rowEnd = i;
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
let columnStart = 0;
|
|
136
|
+
let columnEnd = 0;
|
|
137
|
+
for (let i = 0; i < columnPositions.length; i++) {
|
|
138
|
+
const column = columnPositions[i];
|
|
139
|
+
if (column.left <= left) {
|
|
140
|
+
columnStart = i;
|
|
141
|
+
}
|
|
142
|
+
if (column.left + column.width >= left + width) {
|
|
143
|
+
columnEnd = i;
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const overscan = 5;
|
|
149
|
+
setWindow({
|
|
150
|
+
rowRange: rowPositions.slice(
|
|
151
|
+
Math.max(0, rowStart - overscan),
|
|
152
|
+
Math.min(rowPositions.length, rowEnd + overscan),
|
|
153
|
+
),
|
|
154
|
+
columnRange: columnPositions.slice(
|
|
155
|
+
Math.max(0, columnStart - overscan),
|
|
156
|
+
Math.min(columnPositions.length, columnEnd + overscan),
|
|
157
|
+
),
|
|
158
|
+
});
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
scroller?.addEventListener('scroll', handleScroll);
|
|
162
|
+
handleScroll();
|
|
163
|
+
return () => {
|
|
164
|
+
scroller?.removeEventListener('scroll', handleScroll);
|
|
165
|
+
};
|
|
166
|
+
}, [size.width, size.height, rowPositions, columnPositions]);
|
|
167
|
+
|
|
168
|
+
return { width, height, rowRange, columnRange };
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Find child node at mouse pointer.
|
|
173
|
+
*/
|
|
174
|
+
export const getCellAtPointer = (event: MouseEvent): CellAddress | undefined => {
|
|
175
|
+
const element = document.elementFromPoint(event.clientX, event.clientY);
|
|
176
|
+
const root = element?.closest<HTMLDivElement>(`[data-${CELL_DATA_KEY}]`);
|
|
177
|
+
if (root) {
|
|
178
|
+
const value = root.dataset[CELL_DATA_KEY];
|
|
179
|
+
if (value) {
|
|
180
|
+
return addressFromA1Notation(value);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Get element.
|
|
187
|
+
*/
|
|
188
|
+
export const getCellElement = (root: HTMLElement, cell: CellAddress): HTMLElement | null => {
|
|
189
|
+
const pos = addressToA1Notation(cell);
|
|
190
|
+
return root.querySelector(`[data-${CELL_DATA_KEY}="${pos}"]`);
|
|
191
|
+
};
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type KeyboardEvent, type MouseEventHandler, useState } from 'react';
|
|
6
|
+
|
|
7
|
+
import { getCellAtPointer } from './grid';
|
|
8
|
+
import { type CellAddress, type CellRange, posEquals } from '../../model';
|
|
9
|
+
|
|
10
|
+
export type GridSize = {
|
|
11
|
+
numRows: number;
|
|
12
|
+
numColumns: number;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Calculate next range based on arrow keys.
|
|
17
|
+
*/
|
|
18
|
+
export const handleNav = (
|
|
19
|
+
ev: KeyboardEvent<HTMLInputElement>,
|
|
20
|
+
cursor: CellAddress | undefined,
|
|
21
|
+
range: CellRange | undefined,
|
|
22
|
+
size: GridSize,
|
|
23
|
+
): { cursor?: CellAddress; range?: CellRange } => {
|
|
24
|
+
if (cursor && ev.shiftKey) {
|
|
25
|
+
// Navigate from the furthest point.
|
|
26
|
+
const opposite = range?.to ?? { ...cursor };
|
|
27
|
+
switch (ev.key) {
|
|
28
|
+
case 'ArrowUp': {
|
|
29
|
+
if (opposite.row > 0) {
|
|
30
|
+
opposite.row -= 1;
|
|
31
|
+
}
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
case 'ArrowDown': {
|
|
35
|
+
if (opposite.row < size.numRows - 1) {
|
|
36
|
+
opposite.row += 1;
|
|
37
|
+
}
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
case 'ArrowLeft': {
|
|
41
|
+
if (opposite.column > 0) {
|
|
42
|
+
opposite.column -= 1;
|
|
43
|
+
}
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
case 'ArrowRight': {
|
|
47
|
+
if (opposite.column < size.numColumns - 1) {
|
|
48
|
+
opposite.column += 1;
|
|
49
|
+
}
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return { cursor, range: { from: cursor, to: opposite } };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const next = handleArrowNav(ev, cursor, size);
|
|
58
|
+
return { cursor: next };
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Calculate next cell based on arrow keys.
|
|
63
|
+
*/
|
|
64
|
+
export const handleArrowNav = (
|
|
65
|
+
ev: Pick<KeyboardEvent<HTMLInputElement>, 'key' | 'metaKey'>,
|
|
66
|
+
cursor: CellAddress | undefined,
|
|
67
|
+
{ numRows, numColumns }: GridSize,
|
|
68
|
+
): CellAddress | undefined => {
|
|
69
|
+
switch (ev.key) {
|
|
70
|
+
case 'ArrowUp':
|
|
71
|
+
if (cursor === undefined) {
|
|
72
|
+
return { row: 0, column: 0 };
|
|
73
|
+
} else if (cursor.row > 0) {
|
|
74
|
+
return { row: ev.metaKey ? 0 : cursor.row - 1, column: cursor.column };
|
|
75
|
+
}
|
|
76
|
+
break;
|
|
77
|
+
case 'ArrowDown':
|
|
78
|
+
if (cursor === undefined) {
|
|
79
|
+
return { row: 0, column: 0 };
|
|
80
|
+
} else if (cursor.row < numRows - 1) {
|
|
81
|
+
return { row: ev.metaKey ? numRows - 1 : cursor.row + 1, column: cursor.column };
|
|
82
|
+
}
|
|
83
|
+
break;
|
|
84
|
+
case 'ArrowLeft':
|
|
85
|
+
if (cursor === undefined) {
|
|
86
|
+
return { row: 0, column: 0 };
|
|
87
|
+
} else if (cursor.column > 0) {
|
|
88
|
+
return { row: cursor.row, column: ev.metaKey ? 0 : cursor.column - 1 };
|
|
89
|
+
}
|
|
90
|
+
break;
|
|
91
|
+
case 'ArrowRight':
|
|
92
|
+
if (cursor === undefined) {
|
|
93
|
+
return { row: 0, column: 0 };
|
|
94
|
+
} else if (cursor.column < numColumns - 1) {
|
|
95
|
+
return { row: cursor.row, column: ev.metaKey ? numColumns - 1 : cursor.column + 1 };
|
|
96
|
+
}
|
|
97
|
+
break;
|
|
98
|
+
case 'Home':
|
|
99
|
+
return { row: 0, column: 0 };
|
|
100
|
+
case 'End':
|
|
101
|
+
return { row: numRows - 1, column: numColumns - 1 };
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Hook to manage range drag handlers.
|
|
107
|
+
*/
|
|
108
|
+
// TODO(burdon): Memoize callbacks?
|
|
109
|
+
export const useRangeSelect = (
|
|
110
|
+
cb: (event: 'start' | 'move' | 'end', range: CellRange | undefined) => void,
|
|
111
|
+
): {
|
|
112
|
+
range: CellRange | undefined;
|
|
113
|
+
handlers: {
|
|
114
|
+
onMouseDown: MouseEventHandler<HTMLDivElement>;
|
|
115
|
+
onMouseMove: MouseEventHandler<HTMLDivElement>;
|
|
116
|
+
onMouseUp: MouseEventHandler<HTMLDivElement>;
|
|
117
|
+
};
|
|
118
|
+
} => {
|
|
119
|
+
const [from, setFrom] = useState<CellAddress | undefined>();
|
|
120
|
+
const [to, setTo] = useState<CellAddress | undefined>();
|
|
121
|
+
|
|
122
|
+
const onMouseDown: MouseEventHandler<HTMLDivElement> = (ev) => {
|
|
123
|
+
const current = getCellAtPointer(ev);
|
|
124
|
+
setFrom(current);
|
|
125
|
+
if (current) {
|
|
126
|
+
setTimeout(() => cb('start', { from: current }));
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const onMouseMove: MouseEventHandler<HTMLDivElement> = (ev) => {
|
|
131
|
+
if (from) {
|
|
132
|
+
let current = getCellAtPointer(ev);
|
|
133
|
+
if (posEquals(current, from)) {
|
|
134
|
+
current = undefined;
|
|
135
|
+
}
|
|
136
|
+
setTo(current);
|
|
137
|
+
setTimeout(() => cb('move', { from, to: current }));
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const onMouseUp: MouseEventHandler<HTMLDivElement> = (ev) => {
|
|
142
|
+
if (from) {
|
|
143
|
+
let current = getCellAtPointer(ev);
|
|
144
|
+
if (posEquals(current, from)) {
|
|
145
|
+
current = undefined;
|
|
146
|
+
}
|
|
147
|
+
setFrom(undefined);
|
|
148
|
+
setTo(undefined);
|
|
149
|
+
setTimeout(() => cb('end', current ? { from, to: current } : undefined));
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
return {
|
|
154
|
+
range: from ? { from, to } : undefined,
|
|
155
|
+
handlers: { onMouseDown, onMouseMove, onMouseUp },
|
|
156
|
+
};
|
|
157
|
+
};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import React, { type PropsWithChildren, createContext, useContext, useState, useEffect } from 'react';
|
|
6
|
+
|
|
7
|
+
import { invariant } from '@dxos/invariant';
|
|
8
|
+
import { type Space } from '@dxos/react-client/echo';
|
|
9
|
+
|
|
10
|
+
import { FormattingModel } from './formatting';
|
|
11
|
+
import { type CellAddress, type CellRange, SheetModel } from '../../model';
|
|
12
|
+
import { type SheetType } from '../../types';
|
|
13
|
+
import { useComputeGraph } from '../ComputeGraph';
|
|
14
|
+
|
|
15
|
+
export type SheetContextType = {
|
|
16
|
+
model: SheetModel;
|
|
17
|
+
formatting: FormattingModel;
|
|
18
|
+
|
|
19
|
+
// Cursor state.
|
|
20
|
+
// TODO(burdon): Cursor and range should use indices.
|
|
21
|
+
cursor?: CellAddress;
|
|
22
|
+
setCursor: (cell: CellAddress | undefined) => void;
|
|
23
|
+
range?: CellRange;
|
|
24
|
+
setRange: (range: CellRange | undefined) => void;
|
|
25
|
+
|
|
26
|
+
// Editing state (undefined if not editing).
|
|
27
|
+
editing: boolean;
|
|
28
|
+
setEditing: (editing: boolean) => void;
|
|
29
|
+
|
|
30
|
+
// Events.
|
|
31
|
+
// TODO(burdon): Generalize.
|
|
32
|
+
onInfo?: () => void;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const SheetContext = createContext<SheetContextType | null>(null);
|
|
36
|
+
|
|
37
|
+
export const useSheetContext = (): SheetContextType => {
|
|
38
|
+
const context = useContext(SheetContext);
|
|
39
|
+
invariant(context);
|
|
40
|
+
return context;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export type SheetContextProps = {
|
|
44
|
+
sheet: SheetType;
|
|
45
|
+
space: Space;
|
|
46
|
+
readonly?: boolean;
|
|
47
|
+
} & Pick<SheetContextType, 'onInfo'>;
|
|
48
|
+
|
|
49
|
+
export const SheetContextProvider = ({
|
|
50
|
+
children,
|
|
51
|
+
sheet,
|
|
52
|
+
space,
|
|
53
|
+
readonly,
|
|
54
|
+
onInfo,
|
|
55
|
+
}: PropsWithChildren<SheetContextProps>) => {
|
|
56
|
+
const graph = useComputeGraph(space);
|
|
57
|
+
|
|
58
|
+
const [cursor, setCursor] = useState<CellAddress>();
|
|
59
|
+
const [range, setRange] = useState<CellRange>();
|
|
60
|
+
const [editing, setEditing] = useState<boolean>(false);
|
|
61
|
+
|
|
62
|
+
const [[model, formatting] = [], setModels] = useState<[SheetModel, FormattingModel] | undefined>(undefined);
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
let model: SheetModel | undefined;
|
|
65
|
+
let formatting;
|
|
66
|
+
const t = setTimeout(async () => {
|
|
67
|
+
model = new SheetModel(graph, sheet, { readonly });
|
|
68
|
+
await model.initialize();
|
|
69
|
+
formatting = new FormattingModel(model);
|
|
70
|
+
setModels([model, formatting]);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
return () => {
|
|
74
|
+
clearTimeout(t);
|
|
75
|
+
void model?.destroy();
|
|
76
|
+
};
|
|
77
|
+
}, [graph, readonly]);
|
|
78
|
+
|
|
79
|
+
if (!model || !formatting) {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return (
|
|
84
|
+
<SheetContext.Provider
|
|
85
|
+
value={{
|
|
86
|
+
model,
|
|
87
|
+
formatting,
|
|
88
|
+
cursor,
|
|
89
|
+
setCursor,
|
|
90
|
+
range,
|
|
91
|
+
setRange,
|
|
92
|
+
editing,
|
|
93
|
+
setEditing,
|
|
94
|
+
// TODO(burdon): Change to event.
|
|
95
|
+
onInfo,
|
|
96
|
+
}}
|
|
97
|
+
>
|
|
98
|
+
{children}
|
|
99
|
+
</SheetContext.Provider>
|
|
100
|
+
);
|
|
101
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Gets the relative client rect of an element within a parent container.
|
|
7
|
+
* NOTE: This is stable even when the parent is scrolling.
|
|
8
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect
|
|
9
|
+
* @param root Parent container (e.g., scrollable container).
|
|
10
|
+
* @param element
|
|
11
|
+
*/
|
|
12
|
+
export const getRelativeClientRect = (root: HTMLElement, element: HTMLElement): DOMRect => {
|
|
13
|
+
const rootRect = root.getBoundingClientRect();
|
|
14
|
+
const elementRect = element.getBoundingClientRect();
|
|
15
|
+
return new DOMRect(
|
|
16
|
+
elementRect.left - rootRect.left + root.scrollLeft,
|
|
17
|
+
elementRect.top - rootRect.top + root.scrollTop,
|
|
18
|
+
elementRect.width,
|
|
19
|
+
elementRect.height,
|
|
20
|
+
);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Union of two rectangles.
|
|
25
|
+
*/
|
|
26
|
+
export const getRectUnion = (b1: DOMRect, b2: DOMRect): Pick<DOMRect, 'left' | 'top' | 'width' | 'height'> => {
|
|
27
|
+
return {
|
|
28
|
+
left: Math.min(b1.left, b2.left),
|
|
29
|
+
top: Math.min(b1.top, b2.top),
|
|
30
|
+
width: Math.abs(b1.left - b2.left) + (b1.left > b2.left ? b1.width : b2.width),
|
|
31
|
+
height: Math.abs(b1.top - b2.top) + (b1.height > b2.height ? b1.height : b2.height),
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Scroll to cell.
|
|
37
|
+
* We need to correct for the DOM `scrollIntoView` function which doesn't show the border.
|
|
38
|
+
*/
|
|
39
|
+
export const scrollIntoView = (scrollContainer: HTMLElement, el: HTMLElement) => {
|
|
40
|
+
el.scrollIntoView({ block: 'nearest', inline: 'nearest' });
|
|
41
|
+
|
|
42
|
+
const cellBounds = el.getBoundingClientRect();
|
|
43
|
+
const scrollerBounds = scrollContainer.getBoundingClientRect();
|
|
44
|
+
|
|
45
|
+
if (cellBounds.top < scrollerBounds.top) {
|
|
46
|
+
scrollContainer.scrollTop -= scrollerBounds.top - cellBounds.top;
|
|
47
|
+
} else if (cellBounds.bottom >= scrollerBounds.bottom - 1) {
|
|
48
|
+
scrollContainer.scrollTop += 2 + scrollerBounds.bottom - cellBounds.bottom;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (cellBounds.left < scrollerBounds.left) {
|
|
52
|
+
scrollContainer.scrollLeft -= scrollerBounds.left - cellBounds.left;
|
|
53
|
+
} else if (cellBounds.right >= scrollerBounds.right) {
|
|
54
|
+
scrollContainer.scrollLeft += 2 + scrollerBounds.right - cellBounds.right;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import React from 'react';
|
|
6
|
+
|
|
7
|
+
import { type LayoutCoordinate } from '@dxos/app-framework';
|
|
8
|
+
import { mx } from '@dxos/react-ui-theme';
|
|
9
|
+
|
|
10
|
+
import { Sheet, type SheetRootProps } from './Sheet';
|
|
11
|
+
|
|
12
|
+
const SheetContainer = ({
|
|
13
|
+
sheet,
|
|
14
|
+
space,
|
|
15
|
+
role,
|
|
16
|
+
coordinate = { part: 'main', entryId: '' },
|
|
17
|
+
}: SheetRootProps & { role?: string; coordinate?: LayoutCoordinate }) => {
|
|
18
|
+
return (
|
|
19
|
+
<div role='none' className={mx(role === 'section' && 'aspect-square', role === 'article' && 'row-span-2')}>
|
|
20
|
+
<Sheet.Root sheet={sheet} space={space}>
|
|
21
|
+
<Sheet.Main
|
|
22
|
+
// TODO(burdon): Standardize for other components (e.g., table).
|
|
23
|
+
classNames={[coordinate.part !== 'solo' && 'border-is', role === 'section' && 'border-y border-is']}
|
|
24
|
+
/>
|
|
25
|
+
</Sheet.Root>
|
|
26
|
+
</div>
|
|
27
|
+
);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export default SheetContainer;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import '@dxosTheme';
|
|
6
|
+
|
|
7
|
+
import React from 'react';
|
|
8
|
+
|
|
9
|
+
import { Tooltip } from '@dxos/react-ui';
|
|
10
|
+
import { textBlockWidth } from '@dxos/react-ui-theme';
|
|
11
|
+
import { withTheme } from '@dxos/storybook-utils';
|
|
12
|
+
|
|
13
|
+
import { Toolbar } from './Toolbar';
|
|
14
|
+
import translations from '../../translations';
|
|
15
|
+
|
|
16
|
+
const Story = () => {
|
|
17
|
+
return (
|
|
18
|
+
<Tooltip.Provider>
|
|
19
|
+
<div role='none' className='fixed inset-0 flex flex-col'>
|
|
20
|
+
<Toolbar.Root classNames={textBlockWidth}>
|
|
21
|
+
<Toolbar.Alignment />
|
|
22
|
+
</Toolbar.Root>
|
|
23
|
+
</div>
|
|
24
|
+
</Tooltip.Provider>
|
|
25
|
+
);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export default {
|
|
29
|
+
title: 'plugin-sheet/Toolbar',
|
|
30
|
+
component: Toolbar,
|
|
31
|
+
decorators: [withTheme],
|
|
32
|
+
parameters: { translations, layout: 'fullscreen' },
|
|
33
|
+
render: (args: any) => <Story {...args} />,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const Default = {};
|