@aseity/igrid-react 0.0.0-stage → 0.2.0
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/CHANGELOG.md +34 -0
- package/LICENSE +21 -0
- package/README.md +68 -2
- package/dist/cells.d.ts +7 -0
- package/dist/cells.js +98 -0
- package/dist/charts.d.ts +14 -0
- package/dist/charts.js +51 -0
- package/dist/column-resize.d.ts +15 -0
- package/dist/column-resize.js +75 -0
- package/dist/echarts-adapter.d.ts +2 -0
- package/dist/echarts-adapter.js +128 -0
- package/dist/formula-value.d.ts +5 -0
- package/dist/formula-value.js +21 -0
- package/dist/grid.d.ts +6 -0
- package/dist/grid.js +192 -0
- package/dist/images.d.ts +13 -0
- package/dist/images.js +56 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +77 -0
- package/dist/paint-grid.d.ts +19 -0
- package/dist/paint-grid.js +75 -0
- package/dist/presentation.d.ts +39 -0
- package/dist/presentation.js +23 -0
- package/dist/selection.d.ts +48 -0
- package/dist/selection.js +149 -0
- package/dist/style.css +150 -0
- package/dist/text.d.ts +2 -0
- package/dist/text.js +202 -0
- package/package.json +33 -4
package/dist/grid.js
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useMemo, useRef, useState } from "react";
|
|
3
|
+
import { Charts } from "./charts.js";
|
|
4
|
+
import { ColumnResize } from "./column-resize.js";
|
|
5
|
+
import { FormulaValue } from "./formula-value.js";
|
|
6
|
+
import { Images } from "./images.js";
|
|
7
|
+
import { paintGrid } from "./paint-grid.js";
|
|
8
|
+
import { useGridPresentation } from "./presentation.js";
|
|
9
|
+
import { SelectionOutline, useSelection } from "./selection.js";
|
|
10
|
+
export function CanvasGrid({ workbook, sheet, zoom, }) {
|
|
11
|
+
const { labels } = useGridPresentation();
|
|
12
|
+
const canvas = useRef(null);
|
|
13
|
+
const scroll = useRef(null);
|
|
14
|
+
const previousZoom = useRef(zoom);
|
|
15
|
+
const positioned = useRef(false);
|
|
16
|
+
const initialLayout = useMemo(() => readLayout(workbook, sheet), [workbook, sheet]);
|
|
17
|
+
const [resizedLayout, setResizedLayout] = useState();
|
|
18
|
+
const layout = resizedLayout?.workbook === workbook && resizedLayout.sheet === sheet
|
|
19
|
+
? resizedLayout
|
|
20
|
+
: initialLayout;
|
|
21
|
+
const saved = layout.view;
|
|
22
|
+
const [view, setView] = useState({
|
|
23
|
+
x: saved.scrollX,
|
|
24
|
+
y: saved.scrollY,
|
|
25
|
+
width: 0,
|
|
26
|
+
height: 0,
|
|
27
|
+
dpr: 1,
|
|
28
|
+
});
|
|
29
|
+
const geometry = workbook.geometry(sheet);
|
|
30
|
+
const [expanded, setExpanded] = useState({ width: 0, height: 0 });
|
|
31
|
+
const headerWidth = saved.showHeaders ? 48 : 0;
|
|
32
|
+
const headerHeight = saved.showHeaders ? 24 : 0;
|
|
33
|
+
const width = Math.max(0, view.width / zoom - headerWidth);
|
|
34
|
+
const height = Math.max(0, view.height / zoom - headerHeight);
|
|
35
|
+
const frozenWidth = Math.min(saved.frozenWidth, width);
|
|
36
|
+
const frozenHeight = Math.min(saved.frozenHeight, height);
|
|
37
|
+
const panes = useMemo(() => [
|
|
38
|
+
{
|
|
39
|
+
left: frozenWidth,
|
|
40
|
+
top: frozenHeight,
|
|
41
|
+
x: saved.frozenWidth + view.x,
|
|
42
|
+
y: saved.frozenHeight + view.y,
|
|
43
|
+
width: width - frozenWidth,
|
|
44
|
+
height: height - frozenHeight,
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
left: frozenWidth,
|
|
48
|
+
top: 0,
|
|
49
|
+
x: saved.frozenWidth + view.x,
|
|
50
|
+
y: 0,
|
|
51
|
+
width: width - frozenWidth,
|
|
52
|
+
height: frozenHeight,
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
left: 0,
|
|
56
|
+
top: frozenHeight,
|
|
57
|
+
x: 0,
|
|
58
|
+
y: saved.frozenHeight + view.y,
|
|
59
|
+
width: frozenWidth,
|
|
60
|
+
height: height - frozenHeight,
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
left: 0,
|
|
64
|
+
top: 0,
|
|
65
|
+
x: 0,
|
|
66
|
+
y: 0,
|
|
67
|
+
width: frozenWidth,
|
|
68
|
+
height: frozenHeight,
|
|
69
|
+
},
|
|
70
|
+
].filter((pane) => pane.width > 0 && pane.height > 0), [width, height, frozenWidth, frozenHeight, saved, view.x, view.y]);
|
|
71
|
+
const interaction = useSelection(workbook, sheet, panes, zoom, headerWidth, headerHeight);
|
|
72
|
+
const lists = useMemo(() => workbook.visibleViewports(sheet, panes), [workbook, sheet, panes]);
|
|
73
|
+
const pageWidth = view.width / zoom;
|
|
74
|
+
const pageHeight = view.height / zoom;
|
|
75
|
+
const limitWidth = Math.max(geometry.width + headerWidth, interaction.extent.width + headerWidth, saved.scrollX + pageWidth);
|
|
76
|
+
const limitHeight = Math.max(geometry.height + headerHeight, interaction.extent.height + headerHeight, saved.scrollY + pageHeight);
|
|
77
|
+
const scrollWidth = Math.min(limitWidth, Math.max(expanded.width, geometry.usedWidth + headerWidth + pageWidth / 2, pageWidth * 1.5, interaction.extent.width + headerWidth, saved.scrollX + pageWidth));
|
|
78
|
+
const scrollHeight = Math.min(limitHeight, Math.max(expanded.height, geometry.usedHeight + headerHeight + pageHeight / 2, pageHeight * 1.5, interaction.extent.height + headerHeight, saved.scrollY + pageHeight));
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
const element = scroll.current;
|
|
81
|
+
if (!element || view.width === 0 || view.height === 0)
|
|
82
|
+
return;
|
|
83
|
+
const x = positioned.current
|
|
84
|
+
? element.scrollLeft / previousZoom.current
|
|
85
|
+
: saved.scrollX;
|
|
86
|
+
const y = positioned.current
|
|
87
|
+
? element.scrollTop / previousZoom.current
|
|
88
|
+
: saved.scrollY;
|
|
89
|
+
element.scrollLeft = x * zoom;
|
|
90
|
+
element.scrollTop = y * zoom;
|
|
91
|
+
previousZoom.current = zoom;
|
|
92
|
+
positioned.current = true;
|
|
93
|
+
}, [saved, zoom, view.width, view.height]);
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
const element = scroll.current;
|
|
96
|
+
if (!element)
|
|
97
|
+
return;
|
|
98
|
+
const resize = () => setView((previous) => ({
|
|
99
|
+
...previous,
|
|
100
|
+
width: element.clientWidth,
|
|
101
|
+
height: element.clientHeight,
|
|
102
|
+
dpr: window.devicePixelRatio || 1,
|
|
103
|
+
}));
|
|
104
|
+
const observer = new ResizeObserver(resize);
|
|
105
|
+
observer.observe(element);
|
|
106
|
+
let resolution = window.matchMedia(`(resolution: ${window.devicePixelRatio}dppx)`);
|
|
107
|
+
const resolutionChanged = () => {
|
|
108
|
+
resize();
|
|
109
|
+
resolution.removeEventListener("change", resolutionChanged);
|
|
110
|
+
resolution = window.matchMedia(`(resolution: ${window.devicePixelRatio}dppx)`);
|
|
111
|
+
resolution.addEventListener("change", resolutionChanged);
|
|
112
|
+
};
|
|
113
|
+
resolution.addEventListener("change", resolutionChanged);
|
|
114
|
+
window.addEventListener("resize", resize);
|
|
115
|
+
resize();
|
|
116
|
+
return () => {
|
|
117
|
+
observer.disconnect();
|
|
118
|
+
window.removeEventListener("resize", resize);
|
|
119
|
+
resolution.removeEventListener("change", resolutionChanged);
|
|
120
|
+
};
|
|
121
|
+
}, []);
|
|
122
|
+
useEffect(() => {
|
|
123
|
+
if (canvas.current)
|
|
124
|
+
paintGrid(canvas.current, lists, panes, {
|
|
125
|
+
...view,
|
|
126
|
+
zoom,
|
|
127
|
+
headerWidth,
|
|
128
|
+
headerHeight,
|
|
129
|
+
});
|
|
130
|
+
}, [lists, panes, view, zoom, headerWidth, headerHeight]);
|
|
131
|
+
return (_jsxs(_Fragment, { children: [_jsxs("div", { className: "igrid-formula", children: [_jsx("input", { "aria-label": labels.nameBox, readOnly: true, value: interaction.selection.address }), _jsx("span", { className: "igrid-formula-symbol", "aria-hidden": "true", children: _jsx("svg", { width: "18", height: "18", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", focusable: "false", children: _jsx("path", { d: "M12 4h-1.5C9 4 8.4 5 8.1 6.5L6.4 17.5C6.2 19 5.5 20 4 20H3M5.5 9H12M14 10l6 8m0-8-6 8" }) }) }), _jsx(FormulaValue, { label: labels.formulaBar, value: interaction.formulaText, isFormula: interaction.isFormula })] }), _jsxs("div", { className: "igrid-viewport", children: [_jsx("section", { ref: scroll, tabIndex: 0, ...interaction.pointer, onKeyDown: interaction.onKeyDown, onCopy: interaction.onCopy, "aria-label": labels.viewport, style: {
|
|
132
|
+
position: "absolute",
|
|
133
|
+
inset: 0,
|
|
134
|
+
overflow: "auto",
|
|
135
|
+
zIndex: 1,
|
|
136
|
+
}, onScroll: (event) => {
|
|
137
|
+
const element = event.currentTarget;
|
|
138
|
+
const x = element.scrollLeft / zoom;
|
|
139
|
+
const y = element.scrollTop / zoom;
|
|
140
|
+
const nextWidth = x > view.x
|
|
141
|
+
? expandRange(scrollWidth, x, pageWidth, limitWidth)
|
|
142
|
+
: scrollWidth;
|
|
143
|
+
const nextHeight = y > view.y
|
|
144
|
+
? expandRange(scrollHeight, y, pageHeight, limitHeight)
|
|
145
|
+
: scrollHeight;
|
|
146
|
+
if (nextWidth > scrollWidth || nextHeight > scrollHeight)
|
|
147
|
+
setExpanded({ width: nextWidth, height: nextHeight });
|
|
148
|
+
setView((previous) => ({
|
|
149
|
+
...previous,
|
|
150
|
+
x,
|
|
151
|
+
y,
|
|
152
|
+
}));
|
|
153
|
+
}, children: _jsx("div", { style: {
|
|
154
|
+
width: scrollWidth * zoom,
|
|
155
|
+
height: scrollHeight * zoom,
|
|
156
|
+
} }) }), saved.showHeaders && (_jsx(ColumnResize, { lists: lists, panes: panes, zoom: zoom, headerWidth: headerWidth, headerHeight: headerHeight, height: view.height, onResize: (column, width) => {
|
|
157
|
+
workbook.resizeColumn(sheet, column, width);
|
|
158
|
+
setResizedLayout(readLayout(workbook, sheet));
|
|
159
|
+
} })), _jsx("canvas", { ref: canvas, style: {
|
|
160
|
+
position: "absolute",
|
|
161
|
+
left: 0,
|
|
162
|
+
top: 0,
|
|
163
|
+
width: view.width,
|
|
164
|
+
height: view.height,
|
|
165
|
+
pointerEvents: "none",
|
|
166
|
+
border: 0,
|
|
167
|
+
}, role: "img", "aria-label": `${labels.worksheet} ${workbook.sheets[sheet].name}`, children: lists.flatMap((list) => list.strings).join("\n") }), panes.map((pane) => (_jsxs("div", { style: {
|
|
168
|
+
position: "absolute",
|
|
169
|
+
left: (headerWidth + pane.left) * zoom,
|
|
170
|
+
top: (headerHeight + pane.top) * zoom,
|
|
171
|
+
width: pane.width,
|
|
172
|
+
height: pane.height,
|
|
173
|
+
transform: `scale(${zoom})`,
|
|
174
|
+
transformOrigin: "top left",
|
|
175
|
+
overflow: "hidden",
|
|
176
|
+
pointerEvents: "none",
|
|
177
|
+
}, children: [_jsx(SelectionOutline, { rectangle: interaction.selection.rectangle, pane: pane, multiple: interaction.multiple }), _jsx(Images, { images: layout.images, view: { ...pane, dpr: view.dpr * zoom }, headerWidth: 0, headerHeight: 0 }), _jsx(Charts, { pixelRatio: view.dpr * zoom, workbook: workbook, charts: layout.charts, view: pane, headerWidth: 0, headerHeight: 0 })] }, `${pane.left}-${pane.top}`)))] })] }));
|
|
178
|
+
}
|
|
179
|
+
function expandRange(current, position, page, limit) {
|
|
180
|
+
return position + page * 1.5 >= current
|
|
181
|
+
? Math.min(limit, current + page)
|
|
182
|
+
: current;
|
|
183
|
+
}
|
|
184
|
+
function readLayout(workbook, sheet) {
|
|
185
|
+
return {
|
|
186
|
+
workbook,
|
|
187
|
+
sheet,
|
|
188
|
+
view: workbook.view(sheet),
|
|
189
|
+
charts: workbook.charts(sheet),
|
|
190
|
+
images: workbook.images(sheet),
|
|
191
|
+
};
|
|
192
|
+
}
|
package/dist/images.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Image as WorkbookImage } from "@aseity/igrid-engine";
|
|
2
|
+
export declare function Images({ images, view, headerWidth, headerHeight, }: {
|
|
3
|
+
images: WorkbookImage[];
|
|
4
|
+
view: {
|
|
5
|
+
x: number;
|
|
6
|
+
y: number;
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
dpr: number;
|
|
10
|
+
};
|
|
11
|
+
headerWidth: number;
|
|
12
|
+
headerHeight: number;
|
|
13
|
+
}): import("react/jsx-runtime").JSX.Element;
|
package/dist/images.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useRef, useState } from "react";
|
|
3
|
+
import { GridError, useGridPresentation } from "./presentation.js";
|
|
4
|
+
export function Images({ images, view, headerWidth, headerHeight, }) {
|
|
5
|
+
return (_jsx("div", { style: {
|
|
6
|
+
position: "absolute",
|
|
7
|
+
left: headerWidth,
|
|
8
|
+
top: headerHeight,
|
|
9
|
+
width: Math.max(0, view.width - headerWidth),
|
|
10
|
+
height: Math.max(0, view.height - headerHeight),
|
|
11
|
+
overflow: "hidden",
|
|
12
|
+
pointerEvents: "none",
|
|
13
|
+
}, children: images.map((image, index) => (_jsx(Picture, { image: image, view: view }, `${index}-${image.name}`))) }));
|
|
14
|
+
}
|
|
15
|
+
function Picture({ image, view, }) {
|
|
16
|
+
const { labels } = useGridPresentation();
|
|
17
|
+
const canvas = useRef(null);
|
|
18
|
+
const [error, setError] = useState("");
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
const element = canvas.current;
|
|
21
|
+
const context = element?.getContext("2d");
|
|
22
|
+
if (!element || !context)
|
|
23
|
+
return;
|
|
24
|
+
let cancelled = false;
|
|
25
|
+
const url = URL.createObjectURL(new Blob([new Uint8Array(image.bytes)], { type: image.mime_type }));
|
|
26
|
+
const bitmap = new Image();
|
|
27
|
+
bitmap.src = url;
|
|
28
|
+
bitmap
|
|
29
|
+
.decode()
|
|
30
|
+
.then(() => {
|
|
31
|
+
if (cancelled)
|
|
32
|
+
return;
|
|
33
|
+
element.width = Math.round(image.rectangle.width * view.dpr);
|
|
34
|
+
element.height = Math.round(image.rectangle.height * view.dpr);
|
|
35
|
+
context.drawImage(bitmap, 0, 0, element.width, element.height);
|
|
36
|
+
})
|
|
37
|
+
.catch((cause) => {
|
|
38
|
+
if (!cancelled)
|
|
39
|
+
setError(cause instanceof Error ? cause.message : String(cause));
|
|
40
|
+
});
|
|
41
|
+
return () => {
|
|
42
|
+
cancelled = true;
|
|
43
|
+
URL.revokeObjectURL(url);
|
|
44
|
+
};
|
|
45
|
+
}, [image, view.dpr]);
|
|
46
|
+
if (error)
|
|
47
|
+
return _jsx(GridError, { error: error, kind: "image" });
|
|
48
|
+
return (_jsx("canvas", { ref: canvas, role: "img", "aria-label": `${labels.picture} ${image.name}`, style: {
|
|
49
|
+
position: "absolute",
|
|
50
|
+
left: image.rectangle.x - view.x,
|
|
51
|
+
top: image.rectangle.y - view.y,
|
|
52
|
+
width: image.rectangle.width,
|
|
53
|
+
height: image.rectangle.height,
|
|
54
|
+
border: 0,
|
|
55
|
+
} }));
|
|
56
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type RenderingLoss } from "@aseity/igrid-engine";
|
|
2
|
+
import { type IGridPresentation } from "./presentation.js";
|
|
3
|
+
import "./style.css";
|
|
4
|
+
export type WorkbookFormat = "xlsx" | "csv";
|
|
5
|
+
export type IGridProps = IGridPresentation & {
|
|
6
|
+
bytes: Uint8Array;
|
|
7
|
+
format?: WorkbookFormat;
|
|
8
|
+
zoom?: number;
|
|
9
|
+
onZoomChange?: (zoom: number) => void;
|
|
10
|
+
onLosses?: (losses: RenderingLoss[]) => void;
|
|
11
|
+
};
|
|
12
|
+
export declare function IGrid({ labels, loadingFallback, renderError, ...props }: IGridProps): import("react/jsx-runtime").JSX.Element;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { load, loadCsv, } from "@aseity/igrid-engine";
|
|
3
|
+
import { useEffect, useState } from "react";
|
|
4
|
+
import { CanvasGrid } from "./grid.js";
|
|
5
|
+
import { GridError, PresentationContext, useGridPresentation, } from "./presentation.js";
|
|
6
|
+
import "./style.css";
|
|
7
|
+
export function IGrid({ labels, loadingFallback, renderError, ...props }) {
|
|
8
|
+
return (_jsx(PresentationContext.Provider, { value: { labels, loadingFallback, renderError }, children: _jsx(WorkbookView, { ...props }) }));
|
|
9
|
+
}
|
|
10
|
+
function WorkbookView({ bytes, format = "xlsx", onLosses, zoom, onZoomChange, }) {
|
|
11
|
+
const { labels, loadingFallback } = useGridPresentation();
|
|
12
|
+
const [workbook, setWorkbook] = useState();
|
|
13
|
+
const [workbookVersion, setWorkbookVersion] = useState(0);
|
|
14
|
+
const [error, setError] = useState("");
|
|
15
|
+
const [sheetZoom, setSheetZoom] = useState({});
|
|
16
|
+
const [sheet, setSheet] = useState(0);
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
let cancelled = false;
|
|
19
|
+
let opened;
|
|
20
|
+
setWorkbook(undefined);
|
|
21
|
+
setError("");
|
|
22
|
+
setSheet(0);
|
|
23
|
+
setSheetZoom({});
|
|
24
|
+
(format === "csv" ? loadCsv(bytes) : load(bytes))
|
|
25
|
+
.then((result) => {
|
|
26
|
+
if (cancelled) {
|
|
27
|
+
result.dispose();
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
opened = result;
|
|
31
|
+
const active = result.sheets[result.activeTab]?.visibility === "visible"
|
|
32
|
+
? result.activeTab
|
|
33
|
+
: result.sheets.findIndex((info) => info.visibility === "visible");
|
|
34
|
+
result.view(Math.max(0, active));
|
|
35
|
+
result.charts(Math.max(0, active));
|
|
36
|
+
setSheet(Math.max(0, active));
|
|
37
|
+
setWorkbookVersion((version) => version + 1);
|
|
38
|
+
setWorkbook(result);
|
|
39
|
+
})
|
|
40
|
+
.catch((cause) => {
|
|
41
|
+
if (!cancelled)
|
|
42
|
+
setError(cause instanceof Error ? cause.message : String(cause));
|
|
43
|
+
});
|
|
44
|
+
return () => {
|
|
45
|
+
cancelled = true;
|
|
46
|
+
opened?.dispose();
|
|
47
|
+
};
|
|
48
|
+
}, [bytes, format]);
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
onLosses?.(workbook?.losses ?? []);
|
|
51
|
+
}, [workbook, onLosses]);
|
|
52
|
+
if (error)
|
|
53
|
+
return _jsx(GridError, { error: error, kind: "workbook" });
|
|
54
|
+
if (!workbook)
|
|
55
|
+
return loadingFallback ?? _jsx("output", { children: "Loading workbook\u2026" });
|
|
56
|
+
const scale = zoom ?? sheetZoom[sheet] ?? workbook.view(sheet).zoom;
|
|
57
|
+
const changeSheet = (index) => {
|
|
58
|
+
try {
|
|
59
|
+
workbook.view(index);
|
|
60
|
+
workbook.charts(index);
|
|
61
|
+
setSheet(index);
|
|
62
|
+
onLosses?.(workbook.losses);
|
|
63
|
+
}
|
|
64
|
+
catch (cause) {
|
|
65
|
+
setError(cause instanceof Error ? cause.message : String(cause));
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const changeZoom = (value) => {
|
|
69
|
+
setSheetZoom((previous) => ({ ...previous, [sheet]: value }));
|
|
70
|
+
onZoomChange?.(value);
|
|
71
|
+
};
|
|
72
|
+
return (_jsxs("div", { className: "igrid", children: [_jsx(CanvasGrid, { workbook: workbook, sheet: sheet, zoom: Math.min(4, Math.max(0.1, scale)) }, `${workbookVersion}:${sheet}`), _jsx("nav", { "aria-label": labels.worksheets, children: workbook.sheets.map((info, index) => info.visibility === "visible" && (_jsx("button", { type: "button", "aria-pressed": index === sheet, onClick: () => changeSheet(index), children: info.name }, info.name))) }), _jsxs("label", { className: "igrid-zoom", children: [labels.zoom, " ", _jsx("input", { "aria-label": labels.zoom, type: "number", min: "10", max: "400", step: "10", value: Math.round(scale * 100), onChange: (event) => {
|
|
73
|
+
const value = event.currentTarget.valueAsNumber;
|
|
74
|
+
if (Number.isFinite(value) && value >= 10 && value <= 400)
|
|
75
|
+
changeZoom(value / 100);
|
|
76
|
+
} }), "%"] })] }));
|
|
77
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { DrawList } from "@aseity/igrid-engine";
|
|
2
|
+
type Pane = {
|
|
3
|
+
x: number;
|
|
4
|
+
y: number;
|
|
5
|
+
left: number;
|
|
6
|
+
top: number;
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
};
|
|
10
|
+
export declare function paintGrid(canvas: HTMLCanvasElement, lists: DrawList[], panes: Pane[], view: {
|
|
11
|
+
width: number;
|
|
12
|
+
height: number;
|
|
13
|
+
dpr: number;
|
|
14
|
+
zoom: number;
|
|
15
|
+
headerWidth: number;
|
|
16
|
+
headerHeight: number;
|
|
17
|
+
}): void;
|
|
18
|
+
export declare function columnName(index: number): string;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { drawCells } from "./cells.js";
|
|
2
|
+
export function paintGrid(canvas, lists, panes, view) {
|
|
3
|
+
const context = canvas.getContext("2d");
|
|
4
|
+
if (!context)
|
|
5
|
+
return;
|
|
6
|
+
canvas.width = Math.round(view.width * view.dpr);
|
|
7
|
+
canvas.height = Math.round(view.height * view.dpr);
|
|
8
|
+
context.scale(view.dpr * view.zoom, view.dpr * view.zoom);
|
|
9
|
+
const width = view.width / view.zoom;
|
|
10
|
+
const height = view.height / view.zoom;
|
|
11
|
+
const style = getComputedStyle(canvas);
|
|
12
|
+
const color = (name) => style.getPropertyValue(`--igrid-${name}`).trim();
|
|
13
|
+
context.fillStyle = color("background");
|
|
14
|
+
context.fillRect(0, 0, width, height);
|
|
15
|
+
context.font = "13px Calibri, Carlito, Arial, sans-serif";
|
|
16
|
+
context.textBaseline = "middle";
|
|
17
|
+
for (const [index, pane] of panes.entries()) {
|
|
18
|
+
context.save();
|
|
19
|
+
context.beginPath();
|
|
20
|
+
context.rect(view.headerWidth + pane.left, view.headerHeight + pane.top, pane.width, pane.height);
|
|
21
|
+
context.clip();
|
|
22
|
+
context.translate(view.headerWidth + pane.left - pane.x, view.headerHeight + pane.top - pane.y);
|
|
23
|
+
drawCells(context, lists[index], pane, color("grid-line"));
|
|
24
|
+
context.restore();
|
|
25
|
+
}
|
|
26
|
+
if (!view.headerWidth)
|
|
27
|
+
return;
|
|
28
|
+
context.fillStyle = color("header-background");
|
|
29
|
+
context.fillRect(0, 0, width, view.headerHeight);
|
|
30
|
+
context.fillRect(0, 0, view.headerWidth, height);
|
|
31
|
+
context.strokeStyle = color("grid-line");
|
|
32
|
+
context.fillStyle = color("header-text");
|
|
33
|
+
context.textAlign = "center";
|
|
34
|
+
for (const [index, pane] of panes.entries()) {
|
|
35
|
+
context.save();
|
|
36
|
+
context.beginPath();
|
|
37
|
+
context.rect(view.headerWidth + pane.left, 0, pane.width, view.headerHeight);
|
|
38
|
+
context.clip();
|
|
39
|
+
const list = lists[index];
|
|
40
|
+
for (let i = 0; pane.top === 0 && i < list.columns.length; i += 3) {
|
|
41
|
+
const [column, position, size] = list.columns.subarray(i, i + 3);
|
|
42
|
+
const x = position - pane.x + pane.left + view.headerWidth;
|
|
43
|
+
context.beginPath();
|
|
44
|
+
context.moveTo(x + 0.5, 0);
|
|
45
|
+
context.lineTo(x + 0.5, view.headerHeight + 0.5);
|
|
46
|
+
context.lineTo(x + size + 0.5, view.headerHeight + 0.5);
|
|
47
|
+
context.lineTo(x + size + 0.5, 0);
|
|
48
|
+
context.stroke();
|
|
49
|
+
context.fillText(columnName(column), x + size / 2, view.headerHeight / 2);
|
|
50
|
+
}
|
|
51
|
+
context.restore();
|
|
52
|
+
context.save();
|
|
53
|
+
context.beginPath();
|
|
54
|
+
context.rect(0, view.headerHeight + pane.top, view.headerWidth, pane.height);
|
|
55
|
+
context.clip();
|
|
56
|
+
for (let i = 0; pane.left === 0 && i < list.rows.length; i += 3) {
|
|
57
|
+
const [row, position, size] = list.rows.subarray(i, i + 3);
|
|
58
|
+
const y = position - pane.y + pane.top + view.headerHeight;
|
|
59
|
+
context.beginPath();
|
|
60
|
+
context.moveTo(0, y + 0.5);
|
|
61
|
+
context.lineTo(view.headerWidth + 0.5, y + 0.5);
|
|
62
|
+
context.lineTo(view.headerWidth + 0.5, y + size + 0.5);
|
|
63
|
+
context.lineTo(0, y + size + 0.5);
|
|
64
|
+
context.stroke();
|
|
65
|
+
context.fillText(String(row + 1), view.headerWidth / 2, y + size / 2);
|
|
66
|
+
}
|
|
67
|
+
context.restore();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
export function columnName(index) {
|
|
71
|
+
let name = "";
|
|
72
|
+
for (let column = index + 1; column > 0; column = Math.floor((column - 1) / 26))
|
|
73
|
+
name = String.fromCharCode(65 + ((column - 1) % 26)) + name;
|
|
74
|
+
return name;
|
|
75
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
export declare const defaultLabels: {
|
|
3
|
+
worksheets: string;
|
|
4
|
+
zoom: string;
|
|
5
|
+
nameBox: string;
|
|
6
|
+
formulaBar: string;
|
|
7
|
+
viewport: string;
|
|
8
|
+
worksheet: string;
|
|
9
|
+
chart: string;
|
|
10
|
+
picture: string;
|
|
11
|
+
resizeColumn: string;
|
|
12
|
+
unsupportedChart: string;
|
|
13
|
+
};
|
|
14
|
+
export type IGridPresentation = {
|
|
15
|
+
labels?: Partial<typeof defaultLabels>;
|
|
16
|
+
loadingFallback?: ReactNode;
|
|
17
|
+
renderError?: (error: unknown, kind: "workbook" | "chart" | "image") => ReactNode;
|
|
18
|
+
};
|
|
19
|
+
export declare const PresentationContext: import("react").Context<IGridPresentation>;
|
|
20
|
+
export declare function useGridPresentation(): {
|
|
21
|
+
labels: {
|
|
22
|
+
worksheets: string;
|
|
23
|
+
zoom: string;
|
|
24
|
+
nameBox: string;
|
|
25
|
+
formulaBar: string;
|
|
26
|
+
viewport: string;
|
|
27
|
+
worksheet: string;
|
|
28
|
+
chart: string;
|
|
29
|
+
picture: string;
|
|
30
|
+
resizeColumn: string;
|
|
31
|
+
unsupportedChart: string;
|
|
32
|
+
};
|
|
33
|
+
loadingFallback?: ReactNode;
|
|
34
|
+
renderError?: (error: unknown, kind: "workbook" | "chart" | "image") => ReactNode;
|
|
35
|
+
};
|
|
36
|
+
export declare function GridError({ error, kind, }: {
|
|
37
|
+
error: unknown;
|
|
38
|
+
kind: "workbook" | "chart" | "image";
|
|
39
|
+
}): string | number | bigint | boolean | Iterable<ReactNode> | Promise<string | number | bigint | boolean | import("react").ReactPortal | import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>> | Iterable<ReactNode> | null | undefined> | import("react/jsx-runtime").JSX.Element | null | undefined;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useContext } from "react";
|
|
3
|
+
export const defaultLabels = {
|
|
4
|
+
worksheets: "Worksheets",
|
|
5
|
+
zoom: "Zoom",
|
|
6
|
+
nameBox: "Name box",
|
|
7
|
+
formulaBar: "Formula bar",
|
|
8
|
+
viewport: "Worksheet viewport",
|
|
9
|
+
worksheet: "Worksheet",
|
|
10
|
+
chart: "Chart:",
|
|
11
|
+
picture: "Picture:",
|
|
12
|
+
resizeColumn: "Resize column",
|
|
13
|
+
unsupportedChart: "Unsupported chart",
|
|
14
|
+
};
|
|
15
|
+
export const PresentationContext = createContext({});
|
|
16
|
+
export function useGridPresentation() {
|
|
17
|
+
const value = useContext(PresentationContext);
|
|
18
|
+
return { ...value, labels: { ...defaultLabels, ...value.labels } };
|
|
19
|
+
}
|
|
20
|
+
export function GridError({ error, kind, }) {
|
|
21
|
+
const { renderError } = useGridPresentation();
|
|
22
|
+
return renderError ? (renderError(error, kind)) : (_jsx("div", { role: "alert", children: error instanceof Error ? error.message : String(error) }));
|
|
23
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Workbook } from "@aseity/igrid-engine";
|
|
2
|
+
import { type ClipboardEvent, type KeyboardEvent, type PointerEvent } from "react";
|
|
3
|
+
type Pane = {
|
|
4
|
+
left: number;
|
|
5
|
+
top: number;
|
|
6
|
+
x: number;
|
|
7
|
+
y: number;
|
|
8
|
+
width: number;
|
|
9
|
+
height: number;
|
|
10
|
+
};
|
|
11
|
+
export declare function useSelection(workbook: Workbook, sheet: number, panes: Pane[], zoom: number, headerWidth: number, headerHeight: number): {
|
|
12
|
+
selection: {
|
|
13
|
+
address: string;
|
|
14
|
+
activeCell: string;
|
|
15
|
+
rectangle: {
|
|
16
|
+
x: number;
|
|
17
|
+
y: number;
|
|
18
|
+
width: number;
|
|
19
|
+
height: number;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
multiple: boolean;
|
|
23
|
+
extent: {
|
|
24
|
+
width: number;
|
|
25
|
+
height: number;
|
|
26
|
+
};
|
|
27
|
+
onCopy(event: ClipboardEvent<HTMLElement>): void;
|
|
28
|
+
isFormula: boolean;
|
|
29
|
+
formulaText: string;
|
|
30
|
+
onKeyDown(event: KeyboardEvent<HTMLElement>): void;
|
|
31
|
+
pointer: {
|
|
32
|
+
onPointerDown(event: PointerEvent<HTMLElement>): void;
|
|
33
|
+
onPointerMove(event: PointerEvent<HTMLElement>): void;
|
|
34
|
+
onPointerUp(): void;
|
|
35
|
+
onLostPointerCapture(): void;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
export declare function SelectionOutline({ rectangle, pane, multiple, }: {
|
|
39
|
+
rectangle: {
|
|
40
|
+
x: number;
|
|
41
|
+
y: number;
|
|
42
|
+
width: number;
|
|
43
|
+
height: number;
|
|
44
|
+
};
|
|
45
|
+
pane: Pane;
|
|
46
|
+
multiple: boolean;
|
|
47
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
48
|
+
export {};
|