@molecule/app-spreadsheet-grid-svelte 0.0.0-stage → 1.1.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/LICENSE +202 -0
- package/README.md +334 -2
- package/dist/SpreadsheetGrid.svelte +387 -0
- package/dist/SpreadsheetGrid.svelte.d.ts +5 -0
- package/dist/SpreadsheetGrid.svelte.d.ts.map +1 -0
- package/dist/components.d.ts +8 -0
- package/dist/components.d.ts.map +1 -0
- package/dist/components.js +7 -0
- package/dist/index.d.ts +70 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +69 -0
- package/dist/layout.d.ts +167 -0
- package/dist/layout.d.ts.map +1 -0
- package/dist/layout.js +182 -0
- package/dist/types.d.ts +76 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +7 -0
- package/dist/utilities.d.ts +74 -0
- package/dist/utilities.d.ts.map +1 -0
- package/dist/utilities.js +121 -0
- package/dist/value.d.ts +33 -0
- package/dist/value.d.ts.map +1 -0
- package/dist/value.js +41 -0
- package/package.json +77 -4
package/dist/layout.d.ts
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Geometry, ClassMap classes and inline-style strings for `<SpreadsheetGrid>`.
|
|
3
|
+
*
|
|
4
|
+
* The plan is framework-neutral: every framework's port computes the same one
|
|
5
|
+
* from the same props and state, so the rendered DOM (positions, classes,
|
|
6
|
+
* indices, text) cannot drift between frameworks.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import type { UIClassMap } from '@molecule/app-ui';
|
|
11
|
+
import type { CellMap, CellRef, CellValue, SpreadsheetSelection } from './types.js';
|
|
12
|
+
/** The open in-cell editor. */
|
|
13
|
+
export interface GridEditing {
|
|
14
|
+
/** 0-indexed row of the edited cell. */
|
|
15
|
+
row: number;
|
|
16
|
+
/** 0-indexed column of the edited cell. */
|
|
17
|
+
col: number;
|
|
18
|
+
/** The draft text the editor shows. */
|
|
19
|
+
draft: string;
|
|
20
|
+
}
|
|
21
|
+
/** Everything the plan is computed from. */
|
|
22
|
+
export interface GridGeometry {
|
|
23
|
+
/** Total number of rows. */
|
|
24
|
+
rows: number;
|
|
25
|
+
/** Total number of columns. */
|
|
26
|
+
columns: number;
|
|
27
|
+
/** Sparse cell data. */
|
|
28
|
+
cells: CellMap;
|
|
29
|
+
/** Current selection. */
|
|
30
|
+
selection: SpreadsheetSelection;
|
|
31
|
+
/** Rows frozen at the top. */
|
|
32
|
+
frozenRows: number;
|
|
33
|
+
/** Columns frozen on the left. */
|
|
34
|
+
frozenCols: number;
|
|
35
|
+
/** Per-cell width in pixels. */
|
|
36
|
+
cellWidth: number;
|
|
37
|
+
/** Per-cell height in pixels. */
|
|
38
|
+
cellHeight: number;
|
|
39
|
+
/** Width of the row-number gutter. */
|
|
40
|
+
gutterWidth: number;
|
|
41
|
+
/** Height of the column-letter header. */
|
|
42
|
+
headerHeight: number;
|
|
43
|
+
/** Visible viewport width. */
|
|
44
|
+
viewportWidth: number;
|
|
45
|
+
/** Visible viewport height. */
|
|
46
|
+
viewportHeight: number;
|
|
47
|
+
/** Horizontal scroll offset of the scrollable region. */
|
|
48
|
+
scrollLeft: number;
|
|
49
|
+
/** Vertical scroll offset of the scrollable region. */
|
|
50
|
+
scrollTop: number;
|
|
51
|
+
/** The open editor, or `null`. */
|
|
52
|
+
editing: GridEditing | null;
|
|
53
|
+
}
|
|
54
|
+
/** One column-letter or row-number label. */
|
|
55
|
+
export interface GridHeaderCell {
|
|
56
|
+
/** A stable key for the label. */
|
|
57
|
+
key: string;
|
|
58
|
+
/** The 0-indexed column (headers) or row (gutter). */
|
|
59
|
+
index: number;
|
|
60
|
+
/** The label text (`A`, `12`). */
|
|
61
|
+
text: string;
|
|
62
|
+
/** Its inline style. */
|
|
63
|
+
style: string;
|
|
64
|
+
/** Its ClassMap classes. */
|
|
65
|
+
classes: string;
|
|
66
|
+
}
|
|
67
|
+
/** One rendered cell, positioned for the region it renders in. */
|
|
68
|
+
export interface GridCell {
|
|
69
|
+
/** 0-indexed row. */
|
|
70
|
+
row: number;
|
|
71
|
+
/** 0-indexed column. */
|
|
72
|
+
col: number;
|
|
73
|
+
/** A1 reference. */
|
|
74
|
+
ref: CellRef;
|
|
75
|
+
/** The raw cell value. */
|
|
76
|
+
value: CellValue | undefined;
|
|
77
|
+
/** The value's display string (React's `formatCellValue`). */
|
|
78
|
+
text: string;
|
|
79
|
+
/** Its inline style for its region. */
|
|
80
|
+
style: string;
|
|
81
|
+
/** Its ClassMap classes. */
|
|
82
|
+
classes: string;
|
|
83
|
+
/** Whether the cell lies inside the selection. */
|
|
84
|
+
selected: boolean;
|
|
85
|
+
/** Whether it is the selection anchor (the cell in the tab order). */
|
|
86
|
+
anchor: boolean;
|
|
87
|
+
}
|
|
88
|
+
/** The plan one render of the grid is built from. */
|
|
89
|
+
export interface GridLayout {
|
|
90
|
+
/** The root `<div role="grid">`. */
|
|
91
|
+
rootClasses: string;
|
|
92
|
+
/** Its inline style. */
|
|
93
|
+
rootStyle: string;
|
|
94
|
+
/** The top-left corner. */
|
|
95
|
+
cornerClasses: string;
|
|
96
|
+
/** Its inline style. */
|
|
97
|
+
cornerStyle: string;
|
|
98
|
+
/** The column-letter header lane. */
|
|
99
|
+
headerLaneClasses: string;
|
|
100
|
+
/** Its inline style. */
|
|
101
|
+
headerLaneStyle: string;
|
|
102
|
+
/** The scrolling column-letter container's inline style. */
|
|
103
|
+
headerScrollerStyle: string;
|
|
104
|
+
/** The letters of the frozen columns. */
|
|
105
|
+
frozenHeaderCells: GridHeaderCell[];
|
|
106
|
+
/** The letters of the scrolling columns. */
|
|
107
|
+
scrollingHeaderCells: GridHeaderCell[];
|
|
108
|
+
/** The row-number gutter lane. */
|
|
109
|
+
gutterLaneClasses: string;
|
|
110
|
+
/** Its inline style. */
|
|
111
|
+
gutterLaneStyle: string;
|
|
112
|
+
/** The scrolling row-number container's inline style. */
|
|
113
|
+
gutterScrollerStyle: string;
|
|
114
|
+
/** The numbers of the frozen rows. */
|
|
115
|
+
frozenGutterCells: GridHeaderCell[];
|
|
116
|
+
/** The numbers of the scrolling rows. */
|
|
117
|
+
scrollingGutterCells: GridHeaderCell[];
|
|
118
|
+
/** Whether the frozen-rows region renders. */
|
|
119
|
+
showFrozenRows: boolean;
|
|
120
|
+
/** Whether the frozen-cols region renders. */
|
|
121
|
+
showFrozenCols: boolean;
|
|
122
|
+
/** Whether the frozen-rows × frozen-cols corner renders. */
|
|
123
|
+
showFrozenCorner: boolean;
|
|
124
|
+
/** The frozen-rows region's inline style. */
|
|
125
|
+
frozenRowsRegionStyle: string;
|
|
126
|
+
/** Its inner (scrolling) container's inline style. */
|
|
127
|
+
frozenRowsInnerStyle: string;
|
|
128
|
+
/** The frozen-cols region's inline style. */
|
|
129
|
+
frozenColsRegionStyle: string;
|
|
130
|
+
/** Its inner (scrolling) container's inline style. */
|
|
131
|
+
frozenColsInnerStyle: string;
|
|
132
|
+
/** The frozen corner's inline style. */
|
|
133
|
+
frozenCornerStyle: string;
|
|
134
|
+
/** Cells of the frozen-rows × scrolling-cols region. */
|
|
135
|
+
frozenRowsCells: GridCell[];
|
|
136
|
+
/** Cells of the frozen-cols × scrolling-rows region. */
|
|
137
|
+
frozenColsCells: GridCell[];
|
|
138
|
+
/** Cells of the frozen-rows × frozen-cols corner. */
|
|
139
|
+
frozenCornerCells: GridCell[];
|
|
140
|
+
/** Cells of the main scrollable region. */
|
|
141
|
+
mainCells: GridCell[];
|
|
142
|
+
/** Every rendered cell, by A1 reference (a region's cells are disjoint from the others'). */
|
|
143
|
+
byRef: Map<string, GridCell>;
|
|
144
|
+
/** The scroll region's inline style. */
|
|
145
|
+
scrollRegionStyle: string;
|
|
146
|
+
/** The scroll spacer's inline style. */
|
|
147
|
+
scrollSpacerStyle: string;
|
|
148
|
+
/** The hidden total-size hint's classes. */
|
|
149
|
+
totalClasses: string;
|
|
150
|
+
/** Its inline style. */
|
|
151
|
+
totalStyle: string;
|
|
152
|
+
/** Its text (`720×360`). */
|
|
153
|
+
totalText: string;
|
|
154
|
+
/** The in-cell editor input's classes. */
|
|
155
|
+
editorClasses: string;
|
|
156
|
+
/** The cell content wrapper's classes. */
|
|
157
|
+
contentClasses: string;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Computes one render's plan from the grid's geometry and state.
|
|
161
|
+
*
|
|
162
|
+
* @param geometry - The grid geometry.
|
|
163
|
+
* @param cm - The bonded ClassMap.
|
|
164
|
+
* @returns The layout plan.
|
|
165
|
+
*/
|
|
166
|
+
export declare function computeLayout(geometry: GridGeometry, cm: UIClassMap): GridLayout;
|
|
167
|
+
//# sourceMappingURL=layout.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"layout.d.ts","sourceRoot":"","sources":["../src/layout.ts"],"names":[],"mappings":"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"}
|
package/dist/layout.js
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Geometry, ClassMap classes and inline-style strings for `<SpreadsheetGrid>`.
|
|
3
|
+
*
|
|
4
|
+
* The plan is framework-neutral: every framework's port computes the same one
|
|
5
|
+
* from the same props and state, so the rendered DOM (positions, classes,
|
|
6
|
+
* indices, text) cannot drift between frameworks.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import { cellRef, columnLetter, computeVisibleRange, formatCellValue, isInSelection, } from './utilities.js';
|
|
11
|
+
/**
|
|
12
|
+
* A pixel length, as React writes one.
|
|
13
|
+
*
|
|
14
|
+
* @param value - A length in pixels.
|
|
15
|
+
* @returns The CSS length (`28px`).
|
|
16
|
+
*/
|
|
17
|
+
const px = (value) => `${value}px`;
|
|
18
|
+
/**
|
|
19
|
+
* Whether a cell is the selection anchor.
|
|
20
|
+
*
|
|
21
|
+
* @param selection - The selection.
|
|
22
|
+
* @param row - The cell's 0-indexed row.
|
|
23
|
+
* @param col - The cell's 0-indexed column.
|
|
24
|
+
* @returns `true` when the cell is the anchor.
|
|
25
|
+
*/
|
|
26
|
+
const isAnchor = (selection, row, col) => selection.r1 === row && selection.c1 === col;
|
|
27
|
+
/**
|
|
28
|
+
* Builds the cell box style of one region.
|
|
29
|
+
*
|
|
30
|
+
* @param geometry - The grid geometry.
|
|
31
|
+
* @param left - The cell's left offset in pixels.
|
|
32
|
+
* @param top - The cell's top offset in pixels.
|
|
33
|
+
* @returns The inline style.
|
|
34
|
+
*/
|
|
35
|
+
const cellBoxStyle = (geometry, left, top) => `position: absolute; left: ${px(left)}; top: ${px(top)}; width: ${px(geometry.cellWidth)}; height: ${px(geometry.cellHeight)}; box-sizing: border-box;`;
|
|
36
|
+
/**
|
|
37
|
+
* Describes one rendered cell.
|
|
38
|
+
*
|
|
39
|
+
* @param geometry - The grid geometry.
|
|
40
|
+
* @param cm - The ClassMap.
|
|
41
|
+
* @param row - The cell's 0-indexed row.
|
|
42
|
+
* @param col - The cell's 0-indexed column.
|
|
43
|
+
* @param left - Its left offset in pixels.
|
|
44
|
+
* @param top - Its top offset in pixels.
|
|
45
|
+
* @returns The cell.
|
|
46
|
+
*/
|
|
47
|
+
const cellAt = (geometry, cm, row, col, left, top) => {
|
|
48
|
+
const ref = cellRef(row, col);
|
|
49
|
+
const selected = isInSelection(geometry.selection, row, col);
|
|
50
|
+
const anchor = isAnchor(geometry.selection, row, col);
|
|
51
|
+
const value = geometry.cells.get(ref);
|
|
52
|
+
return {
|
|
53
|
+
row,
|
|
54
|
+
col,
|
|
55
|
+
ref,
|
|
56
|
+
value,
|
|
57
|
+
text: formatCellValue(value),
|
|
58
|
+
style: cellBoxStyle(geometry, left, top),
|
|
59
|
+
classes: cm.cn(cm.spreadsheetGridCellBorder, selected ? cm.spreadsheetGridCellSelected : cm.surface, anchor ? cm.spreadsheetGridCellAnchor : ''),
|
|
60
|
+
selected,
|
|
61
|
+
anchor,
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Computes one render's plan from the grid's geometry and state.
|
|
66
|
+
*
|
|
67
|
+
* @param geometry - The grid geometry.
|
|
68
|
+
* @param cm - The bonded ClassMap.
|
|
69
|
+
* @returns The layout plan.
|
|
70
|
+
*/
|
|
71
|
+
export function computeLayout(geometry, cm) {
|
|
72
|
+
const { cellHeight, cellWidth, columns, frozenCols, frozenRows, gutterWidth, headerHeight } = geometry;
|
|
73
|
+
const frozenRowsHeight = frozenRows * cellHeight;
|
|
74
|
+
const frozenColsWidth = frozenCols * cellWidth;
|
|
75
|
+
const scrollableWidth = Math.max(0, geometry.viewportWidth - gutterWidth - frozenColsWidth);
|
|
76
|
+
const scrollableHeight = Math.max(0, geometry.viewportHeight - headerHeight - frozenRowsHeight);
|
|
77
|
+
const [colStart, colEnd] = computeVisibleRange(geometry.scrollLeft, scrollableWidth, cellWidth, Math.max(0, columns - frozenCols));
|
|
78
|
+
const [rowStart, rowEnd] = computeVisibleRange(geometry.scrollTop, scrollableHeight, cellHeight, Math.max(0, geometry.rows - frozenRows));
|
|
79
|
+
const visibleCols = [];
|
|
80
|
+
for (let c = frozenCols + colStart; c <= frozenCols + colEnd; c += 1) {
|
|
81
|
+
if (c >= columns)
|
|
82
|
+
break;
|
|
83
|
+
visibleCols.push(c);
|
|
84
|
+
}
|
|
85
|
+
const visibleRows = [];
|
|
86
|
+
for (let r = frozenRows + rowStart; r <= frozenRows + rowEnd; r += 1) {
|
|
87
|
+
if (r >= geometry.rows)
|
|
88
|
+
break;
|
|
89
|
+
visibleRows.push(r);
|
|
90
|
+
}
|
|
91
|
+
const frozenColumns = Array.from({ length: Math.min(frozenCols, columns) }, (_v, i) => i);
|
|
92
|
+
const frozenRowIndices = Array.from({ length: Math.min(frozenRows, geometry.rows) }, (_v, i) => i);
|
|
93
|
+
const totalScrollWidth = Math.max(0, columns - frozenCols) * cellWidth;
|
|
94
|
+
const totalScrollHeight = Math.max(0, geometry.rows - frozenRows) * cellHeight;
|
|
95
|
+
const totalGridWidth = gutterWidth + columns * cellWidth;
|
|
96
|
+
const totalGridHeight = headerHeight + geometry.rows * cellHeight;
|
|
97
|
+
const headerCellClasses = cm.cn(cm.spreadsheetGridHeaderCell, cm.textCenter, cm.textSize('xs'), cm.fontWeight('semibold'));
|
|
98
|
+
const headerCellStyle = (left) => `position: absolute; left: ${px(left)}; top: ${px(0)}; width: ${px(cellWidth)}; height: ${px(headerHeight)}; line-height: ${px(headerHeight)};`;
|
|
99
|
+
const gutterCellStyle = (top, width, height) => `position: absolute; left: ${px(0)}; top: ${px(top)}; width: ${px(width)}; height: ${px(height)}; line-height: ${px(height)};`;
|
|
100
|
+
const frozenRowsCells = [];
|
|
101
|
+
const frozenColsCells = [];
|
|
102
|
+
const frozenCornerCells = [];
|
|
103
|
+
const mainCells = [];
|
|
104
|
+
if (frozenRows > 0)
|
|
105
|
+
for (const row of frozenRowIndices)
|
|
106
|
+
for (const col of visibleCols)
|
|
107
|
+
frozenRowsCells.push(cellAt(geometry, cm, row, col, (col - frozenCols) * cellWidth, row * cellHeight));
|
|
108
|
+
if (frozenCols > 0)
|
|
109
|
+
for (const col of frozenColumns)
|
|
110
|
+
for (const row of visibleRows)
|
|
111
|
+
frozenColsCells.push(cellAt(geometry, cm, row, col, col * cellWidth, (row - frozenRows) * cellHeight));
|
|
112
|
+
if (frozenRows > 0 && frozenCols > 0)
|
|
113
|
+
for (const row of frozenRowIndices)
|
|
114
|
+
for (const col of frozenColumns)
|
|
115
|
+
frozenCornerCells.push(cellAt(geometry, cm, row, col, col * cellWidth, row * cellHeight));
|
|
116
|
+
for (const row of visibleRows)
|
|
117
|
+
for (const col of visibleCols)
|
|
118
|
+
mainCells.push(cellAt(geometry, cm, row, col, (col - frozenCols) * cellWidth, (row - frozenRows) * cellHeight));
|
|
119
|
+
return {
|
|
120
|
+
rootClasses: cm.cn(cm.spreadsheetGridRoot, cm.spreadsheetGridRootBorder),
|
|
121
|
+
rootStyle: `width: ${px(geometry.viewportWidth)}; height: ${px(geometry.viewportHeight)}; position: relative; overflow: hidden; outline: none;`,
|
|
122
|
+
cornerClasses: cm.spreadsheetGridCorner,
|
|
123
|
+
cornerStyle: `position: absolute; left: ${px(0)}; top: ${px(0)}; width: ${px(gutterWidth)}; height: ${px(headerHeight)};`,
|
|
124
|
+
headerLaneClasses: cm.spreadsheetGridLane,
|
|
125
|
+
headerLaneStyle: `position: absolute; left: ${px(gutterWidth)}; top: ${px(0)}; width: ${px(geometry.viewportWidth - gutterWidth)}; height: ${px(headerHeight)}; overflow: hidden;`,
|
|
126
|
+
headerScrollerStyle: `position: absolute; left: ${px(frozenColsWidth - geometry.scrollLeft)}; top: ${px(0)}; width: ${px(totalScrollWidth)}; height: ${px(headerHeight)};`,
|
|
127
|
+
frozenHeaderCells: frozenColumns.map((col) => ({
|
|
128
|
+
key: `hdr-frozen-${col}`,
|
|
129
|
+
index: col,
|
|
130
|
+
text: columnLetter(col),
|
|
131
|
+
style: headerCellStyle(col * cellWidth),
|
|
132
|
+
classes: headerCellClasses,
|
|
133
|
+
})),
|
|
134
|
+
scrollingHeaderCells: visibleCols.map((col) => ({
|
|
135
|
+
key: `hdr-${col}`,
|
|
136
|
+
index: col,
|
|
137
|
+
text: columnLetter(col),
|
|
138
|
+
style: headerCellStyle((col - frozenCols) * cellWidth),
|
|
139
|
+
classes: headerCellClasses,
|
|
140
|
+
})),
|
|
141
|
+
gutterLaneClasses: cm.spreadsheetGridLane,
|
|
142
|
+
gutterLaneStyle: `position: absolute; left: ${px(0)}; top: ${px(headerHeight)}; width: ${px(gutterWidth)}; height: ${px(geometry.viewportHeight - headerHeight)}; overflow: hidden;`,
|
|
143
|
+
gutterScrollerStyle: `position: absolute; left: ${px(0)}; top: ${px(frozenRowsHeight - geometry.scrollTop)}; width: ${px(gutterWidth)}; height: ${px(totalScrollHeight)};`,
|
|
144
|
+
frozenGutterCells: frozenRowIndices.map((row) => ({
|
|
145
|
+
key: `gutter-frozen-${row}`,
|
|
146
|
+
index: row,
|
|
147
|
+
text: String(row + 1),
|
|
148
|
+
style: gutterCellStyle(row * cellHeight, gutterWidth, cellHeight),
|
|
149
|
+
classes: headerCellClasses,
|
|
150
|
+
})),
|
|
151
|
+
scrollingGutterCells: visibleRows.map((row) => ({
|
|
152
|
+
key: `gutter-${row}`,
|
|
153
|
+
index: row,
|
|
154
|
+
text: String(row + 1),
|
|
155
|
+
style: gutterCellStyle((row - frozenRows) * cellHeight, gutterWidth, cellHeight),
|
|
156
|
+
classes: headerCellClasses,
|
|
157
|
+
})),
|
|
158
|
+
showFrozenRows: frozenRows > 0,
|
|
159
|
+
showFrozenCols: frozenCols > 0,
|
|
160
|
+
showFrozenCorner: frozenRows > 0 && frozenCols > 0,
|
|
161
|
+
frozenRowsRegionStyle: `position: absolute; left: ${px(gutterWidth + frozenColsWidth)}; top: ${px(headerHeight)}; width: ${px(scrollableWidth)}; height: ${px(frozenRowsHeight)}; overflow: hidden;`,
|
|
162
|
+
frozenRowsInnerStyle: `position: absolute; left: ${px(-geometry.scrollLeft)}; top: ${px(0)}; width: ${px(totalScrollWidth)}; height: ${px(frozenRowsHeight)};`,
|
|
163
|
+
frozenColsRegionStyle: `position: absolute; left: ${px(gutterWidth)}; top: ${px(headerHeight + frozenRowsHeight)}; width: ${px(frozenColsWidth)}; height: ${px(scrollableHeight)}; overflow: hidden;`,
|
|
164
|
+
frozenColsInnerStyle: `position: absolute; left: ${px(0)}; top: ${px(-geometry.scrollTop)}; width: ${px(frozenColsWidth)}; height: ${px(totalScrollHeight)};`,
|
|
165
|
+
frozenCornerStyle: `position: absolute; left: ${px(gutterWidth)}; top: ${px(headerHeight)}; width: ${px(frozenColsWidth)}; height: ${px(frozenRowsHeight)}; overflow: hidden;`,
|
|
166
|
+
frozenRowsCells,
|
|
167
|
+
frozenColsCells,
|
|
168
|
+
frozenCornerCells,
|
|
169
|
+
mainCells,
|
|
170
|
+
byRef: new Map([...frozenRowsCells, ...frozenColsCells, ...frozenCornerCells, ...mainCells].map((cell) => [
|
|
171
|
+
cell.ref,
|
|
172
|
+
cell,
|
|
173
|
+
])),
|
|
174
|
+
scrollRegionStyle: `position: absolute; left: ${px(gutterWidth + frozenColsWidth)}; top: ${px(headerHeight + frozenRowsHeight)}; width: ${px(scrollableWidth)}; height: ${px(scrollableHeight)}; overflow: auto;`,
|
|
175
|
+
scrollSpacerStyle: `width: ${px(totalScrollWidth)}; height: ${px(totalScrollHeight)}; position: relative;`,
|
|
176
|
+
totalClasses: cm.cn(cm.displayBlock),
|
|
177
|
+
totalStyle: `position: absolute; left: ${px(-10000)}; top: ${px(-10000)};`,
|
|
178
|
+
totalText: `${totalGridWidth}×${totalGridHeight}`,
|
|
179
|
+
editorClasses: cm.cn(cm.w('full'), cm.h('full'), cm.spreadsheetGridCellEditor),
|
|
180
|
+
contentClasses: cm.cn(cm.w('full'), cm.h('full'), cm.spreadsheetGridCellContent, cm.textSize('sm')),
|
|
181
|
+
};
|
|
182
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types for the Svelte spreadsheet grid, the same props
|
|
3
|
+
* `@molecule/app-spreadsheet-grid-react` accepts.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import type { Snippet } from 'svelte';
|
|
8
|
+
/**
|
|
9
|
+
* Cell reference in A1 notation (e.g. `'A1'`, `'AB17'`). Column letters are
|
|
10
|
+
* uppercase; row numbers are 1-indexed.
|
|
11
|
+
*/
|
|
12
|
+
export type CellRef = string;
|
|
13
|
+
/**
|
|
14
|
+
* Primitive value stored in a cell. Formula strings start with `'='` —
|
|
15
|
+
* evaluation is the host application's responsibility (typically wired
|
|
16
|
+
* through `@molecule/api-formula-engine`).
|
|
17
|
+
*/
|
|
18
|
+
export type CellValue = string | number | boolean | null;
|
|
19
|
+
/**
|
|
20
|
+
* Sparse map from `CellRef` to value. Cells not present in the map render
|
|
21
|
+
* as empty. Using a `Map` keeps update cost O(1) regardless of grid size.
|
|
22
|
+
*/
|
|
23
|
+
export type CellMap = Map<CellRef, CellValue>;
|
|
24
|
+
/**
|
|
25
|
+
* Inclusive rectangular selection — `r1`/`c1` is the anchor (where the
|
|
26
|
+
* drag started) and `r2`/`c2` is the focus (where it ended). Both are
|
|
27
|
+
* 0-indexed. A single-cell selection has `r1 === r2 && c1 === c2`.
|
|
28
|
+
*/
|
|
29
|
+
export interface SpreadsheetSelection {
|
|
30
|
+
r1: number;
|
|
31
|
+
c1: number;
|
|
32
|
+
r2: number;
|
|
33
|
+
c2: number;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Props accepted by the {@link SpreadsheetGrid} component.
|
|
37
|
+
*/
|
|
38
|
+
export interface SpreadsheetGridProps {
|
|
39
|
+
/** Total number of rows in the grid. */
|
|
40
|
+
rows: number;
|
|
41
|
+
/** Total number of columns in the grid. */
|
|
42
|
+
columns: number;
|
|
43
|
+
/** Sparse cell-data map. Cells not present render as empty. */
|
|
44
|
+
cells: CellMap;
|
|
45
|
+
/** Called when the user commits an edit (Enter, Tab, or focus loss). */
|
|
46
|
+
onCellChange: (ref: CellRef, value: CellValue) => void;
|
|
47
|
+
/** Current selection. */
|
|
48
|
+
selection: SpreadsheetSelection;
|
|
49
|
+
/** Called when the user changes the selection (click, drag, Shift+click). */
|
|
50
|
+
onSelectionChange: (selection: SpreadsheetSelection) => void;
|
|
51
|
+
/** Number of rows frozen at the top (default `0`). */
|
|
52
|
+
frozenRows?: number;
|
|
53
|
+
/** Number of columns frozen on the left (default `0`). */
|
|
54
|
+
frozenCols?: number;
|
|
55
|
+
/** Per-cell width in pixels (default `96`). */
|
|
56
|
+
cellWidth?: number;
|
|
57
|
+
/** Per-cell height in pixels (default `28`). */
|
|
58
|
+
cellHeight?: number;
|
|
59
|
+
/** Width of the row-number gutter on the left (default `48`). */
|
|
60
|
+
gutterWidth?: number;
|
|
61
|
+
/** Height of the column-letter header (default `28`). */
|
|
62
|
+
headerHeight?: number;
|
|
63
|
+
/** Visible viewport width in pixels (default `720`). */
|
|
64
|
+
viewportWidth?: number;
|
|
65
|
+
/** Visible viewport height in pixels (default `360`). */
|
|
66
|
+
viewportHeight?: number;
|
|
67
|
+
/** `data-mol-id` for AI-agent selectors. */
|
|
68
|
+
dataMolId?: string;
|
|
69
|
+
/**
|
|
70
|
+
* Optional cell renderer. Receives the cached display string plus the
|
|
71
|
+
* raw value. Defaults to plain text. Use to render formula results
|
|
72
|
+
* (`'=A1+B1'` → evaluated number) or custom formatting.
|
|
73
|
+
*/
|
|
74
|
+
renderCell?: Snippet<[CellValue | undefined, CellRef]>;
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAA;AAErC;;;GAGG;AACH,MAAM,MAAM,OAAO,GAAG,MAAM,CAAA;AAE5B;;;;GAIG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAA;AAExD;;;GAGG;AACH,MAAM,MAAM,OAAO,GAAG,GAAG,CAAC,OAAO,EAAE,SAAS,CAAC,CAAA;AAE7C;;;;GAIG;AACH,MAAM,WAAW,oBAAoB;IACnC,EAAE,EAAE,MAAM,CAAA;IACV,EAAE,EAAE,MAAM,CAAA;IACV,EAAE,EAAE,MAAM,CAAA;IACV,EAAE,EAAE,MAAM,CAAA;CACX;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC,wCAAwC;IACxC,IAAI,EAAE,MAAM,CAAA;IACZ,2CAA2C;IAC3C,OAAO,EAAE,MAAM,CAAA;IACf,+DAA+D;IAC/D,KAAK,EAAE,OAAO,CAAA;IACd,wEAAwE;IACxE,YAAY,EAAE,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,KAAK,IAAI,CAAA;IACtD,yBAAyB;IACzB,SAAS,EAAE,oBAAoB,CAAA;IAC/B,6EAA6E;IAC7E,iBAAiB,EAAE,CAAC,SAAS,EAAE,oBAAoB,KAAK,IAAI,CAAA;IAC5D,sDAAsD;IACtD,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,0DAA0D;IAC1D,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,+CAA+C;IAC/C,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,gDAAgD;IAChD,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,iEAAiE;IACjE,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,yDAAyD;IACzD,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,wDAAwD;IACxD,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,yDAAyD;IACzD,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,4CAA4C;IAC5C,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB;;;;OAIG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC,CAAC,SAAS,GAAG,SAAS,EAAE,OAAO,CAAC,CAAC,CAAA;CACvD"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { CellMap, CellRef, CellValue, SpreadsheetSelection } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Convert a 0-indexed column number to its A1 letter (`0` → `'A'`, `25` →
|
|
4
|
+
* `'Z'`, `26` → `'AA'`, `701` → `'ZZ'`, `702` → `'AAA'`).
|
|
5
|
+
*
|
|
6
|
+
* @param col - 0-indexed column number.
|
|
7
|
+
* @returns Uppercase A1 column letter.
|
|
8
|
+
*/
|
|
9
|
+
export declare function columnLetter(col: number): string;
|
|
10
|
+
/**
|
|
11
|
+
* Build an A1 reference from 0-indexed row and column numbers.
|
|
12
|
+
*
|
|
13
|
+
* @param row - 0-indexed row number.
|
|
14
|
+
* @param col - 0-indexed column number.
|
|
15
|
+
* @returns A1-style cell reference (e.g. `'B3'`).
|
|
16
|
+
*/
|
|
17
|
+
export declare function cellRef(row: number, col: number): CellRef;
|
|
18
|
+
/**
|
|
19
|
+
* Normalize a possibly-inverted selection so `r1 <= r2 && c1 <= c2`.
|
|
20
|
+
*
|
|
21
|
+
* @param sel - Raw selection (anchor and focus).
|
|
22
|
+
* @returns Selection with `r1`/`c1` as top-left and `r2`/`c2` as bottom-right.
|
|
23
|
+
*/
|
|
24
|
+
export declare function normalizeSelection(sel: SpreadsheetSelection): SpreadsheetSelection;
|
|
25
|
+
/**
|
|
26
|
+
* Test whether `(row, col)` is inside the selection.
|
|
27
|
+
*
|
|
28
|
+
* @param sel - Selection to test against.
|
|
29
|
+
* @param row - 0-indexed row number.
|
|
30
|
+
* @param col - 0-indexed column number.
|
|
31
|
+
* @returns `true` when the cell is in the selection.
|
|
32
|
+
*/
|
|
33
|
+
export declare function isInSelection(sel: SpreadsheetSelection, row: number, col: number): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Format a cell value for display. `null`/`undefined` render as the empty
|
|
36
|
+
* string; booleans become `TRUE`/`FALSE` (matching common spreadsheet
|
|
37
|
+
* tools); numbers and strings stringify as-is.
|
|
38
|
+
*
|
|
39
|
+
* @param value - The raw cell value.
|
|
40
|
+
* @returns Display string.
|
|
41
|
+
*/
|
|
42
|
+
export declare function formatCellValue(value: CellValue | undefined): string;
|
|
43
|
+
/**
|
|
44
|
+
* Parse a TSV (tab-separated values) string into a 2D array of strings.
|
|
45
|
+
* Empty trailing lines are ignored. Used to ingest clipboard paste data.
|
|
46
|
+
*
|
|
47
|
+
* @param text - Clipboard text (typically TSV).
|
|
48
|
+
* @returns 2D array of cell strings — `rows[r][c]`.
|
|
49
|
+
*/
|
|
50
|
+
export declare function parseClipboardTsv(text: string): string[][];
|
|
51
|
+
/**
|
|
52
|
+
* Serialize a rectangular selection to TSV (tab-separated values) so it
|
|
53
|
+
* can round-trip with Excel/Google Sheets via the system clipboard.
|
|
54
|
+
*
|
|
55
|
+
* @param cells - Source cell map.
|
|
56
|
+
* @param sel - Selection to serialize.
|
|
57
|
+
* @returns TSV-formatted string with `\n` row separators.
|
|
58
|
+
*/
|
|
59
|
+
export declare function serializeSelectionTsv(cells: CellMap, sel: SpreadsheetSelection): string;
|
|
60
|
+
/**
|
|
61
|
+
* Compute the inclusive `[start, end]` window of indices to render given
|
|
62
|
+
* the scroll offset, viewport size, item size, and total count. Adds an
|
|
63
|
+
* `overscan` margin on each side so newly-visible cells are pre-rendered
|
|
64
|
+
* during fast scrolls.
|
|
65
|
+
*
|
|
66
|
+
* @param scroll - Current scroll position (px).
|
|
67
|
+
* @param viewport - Viewport size (px) along the scroll axis.
|
|
68
|
+
* @param itemSize - Per-item size (px) along the scroll axis.
|
|
69
|
+
* @param total - Total number of items.
|
|
70
|
+
* @param overscan - Extra items to render on each side (default `2`).
|
|
71
|
+
* @returns `[startIndex, endIndex]` (both inclusive, clamped to `[0, total)`).
|
|
72
|
+
*/
|
|
73
|
+
export declare function computeVisibleRange(scroll: number, viewport: number, itemSize: number, total: number, overscan?: number): [number, number];
|
|
74
|
+
//# sourceMappingURL=utilities.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utilities.d.ts","sourceRoot":"","sources":["../src/utilities.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAA;AAEnF;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAQhD;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAEzD;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,oBAAoB,GAAG,oBAAoB,CAOlF;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,oBAAoB,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAG1F;AAED;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,SAAS,GAAG,SAAS,GAAG,MAAM,CAIpE;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,EAAE,CAM1D;AAED;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,oBAAoB,GAAG,MAAM,CAWvF;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,EACb,QAAQ,SAAI,GACX,CAAC,MAAM,EAAE,MAAM,CAAC,CAKlB"}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a 0-indexed column number to its A1 letter (`0` → `'A'`, `25` →
|
|
3
|
+
* `'Z'`, `26` → `'AA'`, `701` → `'ZZ'`, `702` → `'AAA'`).
|
|
4
|
+
*
|
|
5
|
+
* @param col - 0-indexed column number.
|
|
6
|
+
* @returns Uppercase A1 column letter.
|
|
7
|
+
*/
|
|
8
|
+
export function columnLetter(col) {
|
|
9
|
+
let n = col;
|
|
10
|
+
let result = '';
|
|
11
|
+
while (n >= 0) {
|
|
12
|
+
result = String.fromCharCode(65 + (n % 26)) + result;
|
|
13
|
+
n = Math.floor(n / 26) - 1;
|
|
14
|
+
}
|
|
15
|
+
return result;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Build an A1 reference from 0-indexed row and column numbers.
|
|
19
|
+
*
|
|
20
|
+
* @param row - 0-indexed row number.
|
|
21
|
+
* @param col - 0-indexed column number.
|
|
22
|
+
* @returns A1-style cell reference (e.g. `'B3'`).
|
|
23
|
+
*/
|
|
24
|
+
export function cellRef(row, col) {
|
|
25
|
+
return `${columnLetter(col)}${row + 1}`;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Normalize a possibly-inverted selection so `r1 <= r2 && c1 <= c2`.
|
|
29
|
+
*
|
|
30
|
+
* @param sel - Raw selection (anchor and focus).
|
|
31
|
+
* @returns Selection with `r1`/`c1` as top-left and `r2`/`c2` as bottom-right.
|
|
32
|
+
*/
|
|
33
|
+
export function normalizeSelection(sel) {
|
|
34
|
+
return {
|
|
35
|
+
r1: Math.min(sel.r1, sel.r2),
|
|
36
|
+
c1: Math.min(sel.c1, sel.c2),
|
|
37
|
+
r2: Math.max(sel.r1, sel.r2),
|
|
38
|
+
c2: Math.max(sel.c1, sel.c2),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Test whether `(row, col)` is inside the selection.
|
|
43
|
+
*
|
|
44
|
+
* @param sel - Selection to test against.
|
|
45
|
+
* @param row - 0-indexed row number.
|
|
46
|
+
* @param col - 0-indexed column number.
|
|
47
|
+
* @returns `true` when the cell is in the selection.
|
|
48
|
+
*/
|
|
49
|
+
export function isInSelection(sel, row, col) {
|
|
50
|
+
const n = normalizeSelection(sel);
|
|
51
|
+
return row >= n.r1 && row <= n.r2 && col >= n.c1 && col <= n.c2;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Format a cell value for display. `null`/`undefined` render as the empty
|
|
55
|
+
* string; booleans become `TRUE`/`FALSE` (matching common spreadsheet
|
|
56
|
+
* tools); numbers and strings stringify as-is.
|
|
57
|
+
*
|
|
58
|
+
* @param value - The raw cell value.
|
|
59
|
+
* @returns Display string.
|
|
60
|
+
*/
|
|
61
|
+
export function formatCellValue(value) {
|
|
62
|
+
if (value === null || value === undefined)
|
|
63
|
+
return '';
|
|
64
|
+
if (typeof value === 'boolean')
|
|
65
|
+
return value ? 'TRUE' : 'FALSE';
|
|
66
|
+
return String(value);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Parse a TSV (tab-separated values) string into a 2D array of strings.
|
|
70
|
+
* Empty trailing lines are ignored. Used to ingest clipboard paste data.
|
|
71
|
+
*
|
|
72
|
+
* @param text - Clipboard text (typically TSV).
|
|
73
|
+
* @returns 2D array of cell strings — `rows[r][c]`.
|
|
74
|
+
*/
|
|
75
|
+
export function parseClipboardTsv(text) {
|
|
76
|
+
const lines = text.replace(/\r\n/g, '\n').split('\n');
|
|
77
|
+
while (lines.length > 0 && lines[lines.length - 1] === '') {
|
|
78
|
+
lines.pop();
|
|
79
|
+
}
|
|
80
|
+
return lines.map((line) => line.split('\t'));
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Serialize a rectangular selection to TSV (tab-separated values) so it
|
|
84
|
+
* can round-trip with Excel/Google Sheets via the system clipboard.
|
|
85
|
+
*
|
|
86
|
+
* @param cells - Source cell map.
|
|
87
|
+
* @param sel - Selection to serialize.
|
|
88
|
+
* @returns TSV-formatted string with `\n` row separators.
|
|
89
|
+
*/
|
|
90
|
+
export function serializeSelectionTsv(cells, sel) {
|
|
91
|
+
const n = normalizeSelection(sel);
|
|
92
|
+
const rows = [];
|
|
93
|
+
for (let r = n.r1; r <= n.r2; r += 1) {
|
|
94
|
+
const cols = [];
|
|
95
|
+
for (let c = n.c1; c <= n.c2; c += 1) {
|
|
96
|
+
cols.push(formatCellValue(cells.get(cellRef(r, c))));
|
|
97
|
+
}
|
|
98
|
+
rows.push(cols.join('\t'));
|
|
99
|
+
}
|
|
100
|
+
return rows.join('\n');
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Compute the inclusive `[start, end]` window of indices to render given
|
|
104
|
+
* the scroll offset, viewport size, item size, and total count. Adds an
|
|
105
|
+
* `overscan` margin on each side so newly-visible cells are pre-rendered
|
|
106
|
+
* during fast scrolls.
|
|
107
|
+
*
|
|
108
|
+
* @param scroll - Current scroll position (px).
|
|
109
|
+
* @param viewport - Viewport size (px) along the scroll axis.
|
|
110
|
+
* @param itemSize - Per-item size (px) along the scroll axis.
|
|
111
|
+
* @param total - Total number of items.
|
|
112
|
+
* @param overscan - Extra items to render on each side (default `2`).
|
|
113
|
+
* @returns `[startIndex, endIndex]` (both inclusive, clamped to `[0, total)`).
|
|
114
|
+
*/
|
|
115
|
+
export function computeVisibleRange(scroll, viewport, itemSize, total, overscan = 2) {
|
|
116
|
+
if (total <= 0 || itemSize <= 0)
|
|
117
|
+
return [0, -1];
|
|
118
|
+
const start = Math.max(0, Math.floor(scroll / itemSize) - overscan);
|
|
119
|
+
const end = Math.min(total - 1, Math.ceil((scroll + viewport) / itemSize) + overscan);
|
|
120
|
+
return [start, end];
|
|
121
|
+
}
|