@20baht/spreadsheet 0.1.0 → 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/README.md +9 -5
- package/dist/book.d.ts +10 -2
- package/dist/book.js +2 -1
- package/dist/compute.d.ts +8 -3
- package/dist/compute.js +11 -0
- package/dist/custom-elements.json +153 -1
- package/dist/dates.d.ts +22 -0
- package/dist/dates.js +53 -0
- package/dist/elements/baht-spreadsheet.d.ts +33 -0
- package/dist/elements/baht-spreadsheet.js +196 -6
- package/dist/formula.d.ts +1 -1
- package/dist/formula.js +82 -3
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/parse.js +13 -1
- package/dist/refs.d.ts +43 -0
- package/dist/refs.js +120 -0
- package/dist/session.d.ts +26 -1
- package/dist/session.js +190 -10
- package/dist/spreadsheet.css +57 -2
- package/dist/workbook.d.ts +3 -1
- package/dist/workbook.js +2 -0
- package/package.json +1 -1
package/dist/refs.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { columnIndex, columnName, MAX_COLUMNS, MAX_ROWS } from './book.js';
|
|
2
|
+
const NAME = String.raw `'(?:[^']|'')*'|[\p{L}_][\p{L}\p{N}_.]*`;
|
|
3
|
+
const CELL = String.raw `\$?[A-Za-z]{1,3}\$?[1-9]\d{0,6}`;
|
|
4
|
+
const COLUMN = String.raw `\$?[A-Za-z]{1,3}`;
|
|
5
|
+
const PLACES = new RegExp(String.raw `"(?:[^"]|"")*"|(?<![\p{L}\p{N}_.$'])(?:(${NAME})!)?(${CELL}(?::${CELL})?|${COLUMN}:${COLUMN})(?![\p{L}\p{N}_.(!$])`, 'gu');
|
|
6
|
+
const END = /^(\$?)([A-Za-z]{1,3})(\$?)(\d*)$/;
|
|
7
|
+
const PLAIN = /^[\p{L}_][\p{L}\p{N}_.]*$/u;
|
|
8
|
+
const LIKE_A_PLACE = /^(?:[A-Za-z]{1,3}\d*|TRUE|FALSE)$/i;
|
|
9
|
+
function endOf(text) {
|
|
10
|
+
const [, columnFixed, letters = '', rowFixed, digits] = END.exec(text) ?? [];
|
|
11
|
+
return {
|
|
12
|
+
column: columnIndex(letters),
|
|
13
|
+
...(digits ? { row: Number(digits) - 1 } : {}),
|
|
14
|
+
columnFixed: columnFixed === '$',
|
|
15
|
+
rowFixed: rowFixed === '$',
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function endText(end) {
|
|
19
|
+
const column = `${end.columnFixed ? '$' : ''}${columnName(end.column)}`;
|
|
20
|
+
return end.row === undefined
|
|
21
|
+
? column
|
|
22
|
+
: `${column}${end.rowFixed ? '$' : ''}${String(end.row + 1)}`;
|
|
23
|
+
}
|
|
24
|
+
export function sheetRef(name) {
|
|
25
|
+
return PLAIN.test(name) && !LIKE_A_PLACE.test(name) ? name : `'${name.replaceAll("'", "''")}'`;
|
|
26
|
+
}
|
|
27
|
+
function refText(ref) {
|
|
28
|
+
const sheet = ref.sheet === undefined ? '' : `${sheetRef(ref.sheet)}!`;
|
|
29
|
+
return `${sheet}${endText(ref.from)}${ref.to ? `:${endText(ref.to)}` : ''}`;
|
|
30
|
+
}
|
|
31
|
+
export function rewriteRefs(formula, change) {
|
|
32
|
+
return formula.replace(PLACES, (found, sheet, place) => {
|
|
33
|
+
if (place === undefined)
|
|
34
|
+
return found;
|
|
35
|
+
const [from = '', to] = place.split(':');
|
|
36
|
+
const changed = change({
|
|
37
|
+
...(sheet === undefined
|
|
38
|
+
? {}
|
|
39
|
+
: { sheet: sheet.startsWith("'") ? sheet.slice(1, -1).replaceAll("''", "'") : sheet }),
|
|
40
|
+
from: endOf(from),
|
|
41
|
+
...(to === undefined ? {} : { to: endOf(to) }),
|
|
42
|
+
});
|
|
43
|
+
if (changed === undefined)
|
|
44
|
+
return found;
|
|
45
|
+
return changed === null ? '#REF!' : refText(changed);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
const limitOf = (axis) => (axis === 'row' ? MAX_ROWS : MAX_COLUMNS);
|
|
49
|
+
export function movedIndex(index, move) {
|
|
50
|
+
if (index < move.at)
|
|
51
|
+
return index;
|
|
52
|
+
if (!move.remove)
|
|
53
|
+
return index + move.count < limitOf(move.axis) ? index + move.count : null;
|
|
54
|
+
return index < move.at + move.count ? null : index - move.count;
|
|
55
|
+
}
|
|
56
|
+
export function movedSpan(low, high, move) {
|
|
57
|
+
if (!move.remove) {
|
|
58
|
+
const to = movedIndex(high, move);
|
|
59
|
+
return to === null ? null : [movedIndex(low, move) ?? low, to];
|
|
60
|
+
}
|
|
61
|
+
const end = move.at + move.count;
|
|
62
|
+
if (low >= move.at && high < end)
|
|
63
|
+
return null;
|
|
64
|
+
return [
|
|
65
|
+
low >= end ? low - move.count : Math.min(low, move.at),
|
|
66
|
+
high >= end ? high - move.count : high >= move.at ? move.at - 1 : high,
|
|
67
|
+
];
|
|
68
|
+
}
|
|
69
|
+
const key = (axis) => axis;
|
|
70
|
+
function movedRef(ref, move) {
|
|
71
|
+
const axis = key(move.axis);
|
|
72
|
+
const a = ref.from[axis];
|
|
73
|
+
if (a === undefined)
|
|
74
|
+
return undefined;
|
|
75
|
+
if (!ref.to) {
|
|
76
|
+
const to = movedIndex(a, move);
|
|
77
|
+
return to === null ? null : { ...ref, from: { ...ref.from, [axis]: to } };
|
|
78
|
+
}
|
|
79
|
+
const b = ref.to[axis] ?? a;
|
|
80
|
+
const span = movedSpan(Math.min(a, b), Math.max(a, b), move);
|
|
81
|
+
if (!span)
|
|
82
|
+
return null;
|
|
83
|
+
const [low, high] = span;
|
|
84
|
+
return {
|
|
85
|
+
...ref,
|
|
86
|
+
from: { ...ref.from, [axis]: a <= b ? low : high },
|
|
87
|
+
to: { ...ref.to, [axis]: a <= b ? high : low },
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
export function movedRefs(formula, move, sheet, own) {
|
|
91
|
+
const name = sheet.toLowerCase();
|
|
92
|
+
return rewriteRefs(formula, (ref) => {
|
|
93
|
+
const there = ref.sheet === undefined ? own : ref.sheet.toLowerCase() === name;
|
|
94
|
+
return there ? movedRef(ref, move) : undefined;
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
function shiftedEnd(end, rows, columns) {
|
|
98
|
+
const column = end.columnFixed ? end.column : end.column + columns;
|
|
99
|
+
const row = end.row === undefined || end.rowFixed ? end.row : end.row + rows;
|
|
100
|
+
if (column < 0 || column >= MAX_COLUMNS)
|
|
101
|
+
return null;
|
|
102
|
+
if (row !== undefined && (row < 0 || row >= MAX_ROWS))
|
|
103
|
+
return null;
|
|
104
|
+
return { ...end, column, ...(row === undefined ? {} : { row }) };
|
|
105
|
+
}
|
|
106
|
+
export function shiftedRefs(formula, rows, columns) {
|
|
107
|
+
if (rows === 0 && columns === 0)
|
|
108
|
+
return formula;
|
|
109
|
+
return rewriteRefs(formula, (ref) => {
|
|
110
|
+
const from = shiftedEnd(ref.from, rows, columns);
|
|
111
|
+
const to = ref.to ? shiftedEnd(ref.to, rows, columns) : undefined;
|
|
112
|
+
if (!from || to === null)
|
|
113
|
+
return null;
|
|
114
|
+
return { ...ref, from, ...(to ? { to } : {}) };
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
export function renamedRefs(formula, from, to) {
|
|
118
|
+
const name = from.toLowerCase();
|
|
119
|
+
return rewriteRefs(formula, (ref) => ref.sheet?.toLowerCase() === name ? { ...ref, sheet: to } : undefined);
|
|
120
|
+
}
|
package/dist/session.d.ts
CHANGED
|
@@ -20,7 +20,8 @@ export type StylePatch = {
|
|
|
20
20
|
/** What a source answered: the columns' labels, then a row of values for each record. */
|
|
21
21
|
export interface SourceRows {
|
|
22
22
|
columns: readonly string[];
|
|
23
|
-
|
|
23
|
+
/** A `Date` becomes a date cell: its wall clock where this runs, shown with its time unless it is midnight. */
|
|
24
|
+
rows: readonly (readonly (CellValue | Date | null)[])[];
|
|
24
25
|
}
|
|
25
26
|
export type BookMsg =
|
|
26
27
|
/** Another sheet. */
|
|
@@ -77,6 +78,11 @@ export type BookMsg =
|
|
|
77
78
|
type: 'style';
|
|
78
79
|
style: StylePatch;
|
|
79
80
|
}
|
|
81
|
+
/** Lines on the selected cells: every side of each (`all`), the selection's edge (`outer`), or none. */
|
|
82
|
+
| {
|
|
83
|
+
type: 'border';
|
|
84
|
+
sides: 'all' | 'outer' | 'none';
|
|
85
|
+
}
|
|
80
86
|
/** Show the selection as one cell: its top left cell keeps what it holds, the others are emptied. */
|
|
81
87
|
| {
|
|
82
88
|
type: 'merge';
|
|
@@ -91,6 +97,25 @@ export type BookMsg =
|
|
|
91
97
|
column: number;
|
|
92
98
|
width: number;
|
|
93
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* The cells of `from` copied over the rest of `to` (the fill handle, Ctrl+D): a formula reads
|
|
102
|
+
* from as far away as it did, numbers in step go on in step, anything else repeats.
|
|
103
|
+
*/
|
|
104
|
+
| {
|
|
105
|
+
type: 'spread';
|
|
106
|
+
from: CellRange;
|
|
107
|
+
to: CellRange;
|
|
108
|
+
}
|
|
109
|
+
/** As many empty rows (or columns) as the selection has, before it. Every formula follows. */
|
|
110
|
+
| {
|
|
111
|
+
type: 'insert';
|
|
112
|
+
axis: 'row' | 'column';
|
|
113
|
+
}
|
|
114
|
+
/** Take the selection's rows (or columns) out. A formula that named one of them reads `#REF!`. */
|
|
115
|
+
| {
|
|
116
|
+
type: 'remove';
|
|
117
|
+
axis: 'row' | 'column';
|
|
118
|
+
}
|
|
94
119
|
/** A new sheet, shown; with `source`, a data sheet. */
|
|
95
120
|
| {
|
|
96
121
|
type: 'add-sheet';
|
package/dist/session.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { addressOf, MAX_COLUMNS, MAX_ROWS, MAX_SHEETS, mergeAt, mergesOf, overlaps, rangeName, rangeOf, sheetOf, } from './book.js';
|
|
1
|
+
import { addressOf, BORDER_SIDES, columnIndex, columnName, extentOf, MAX_COLUMNS, MAX_ROWS, MAX_SHEETS, mergeAt, mergesOf, overlaps, parseAddress, rangeName, rangeOf, sheetOf, } from './book.js';
|
|
2
2
|
import { cellInput, inputText } from './compute.js';
|
|
3
|
+
import { dateSerial } from './dates.js';
|
|
4
|
+
import { movedIndex, movedRefs, movedSpan, renamedRefs, shiftedRefs } from './refs.js';
|
|
3
5
|
const HISTORY = 100;
|
|
4
6
|
const ORIGIN = { row: 0, column: 0 };
|
|
5
7
|
const HEADING = { bold: true, fill: '#e8eef7' };
|
|
@@ -73,10 +75,14 @@ function changedCells(sheet, range, change) {
|
|
|
73
75
|
cells[address] = cell;
|
|
74
76
|
return { ...sheet, cells };
|
|
75
77
|
}
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
...cellInput(text)
|
|
79
|
-
|
|
78
|
+
const NOT_DATES = new Set(['number', 'integer', 'percent']);
|
|
79
|
+
function typed(cell, text) {
|
|
80
|
+
const { format, ...held } = cellInput(text);
|
|
81
|
+
const style = format === undefined || NOT_DATES.has(cell.style?.format)
|
|
82
|
+
? cell.style
|
|
83
|
+
: { ...cell.style, format };
|
|
84
|
+
return { ...(style ? { style } : {}), ...held };
|
|
85
|
+
}
|
|
80
86
|
const within = (cell) => ({
|
|
81
87
|
row: Math.min(MAX_ROWS - 1, Math.max(0, cell.row)),
|
|
82
88
|
column: Math.min(MAX_COLUMNS - 1, Math.max(0, cell.column)),
|
|
@@ -152,6 +158,16 @@ function removeSheet(state, key) {
|
|
|
152
158
|
const moved = state.sheet === key ? { active: ORIGIN, end: ORIGIN, editing: null } : {};
|
|
153
159
|
return rewritten(state, { ...state.book, sheets }, { sheet: shown, ...moved });
|
|
154
160
|
}
|
|
161
|
+
function sourced(value) {
|
|
162
|
+
if (value === null || value === undefined || value === '')
|
|
163
|
+
return null;
|
|
164
|
+
if (!(value instanceof Date))
|
|
165
|
+
return { value };
|
|
166
|
+
const serial = dateSerial(value);
|
|
167
|
+
if (serial === null)
|
|
168
|
+
return null;
|
|
169
|
+
return { value: serial, style: { format: Number.isInteger(serial) ? 'date' : 'datetime' } };
|
|
170
|
+
}
|
|
155
171
|
function fill(state, key, data) {
|
|
156
172
|
const sheet = sheetOf(state.book, key);
|
|
157
173
|
if (!sheet?.source)
|
|
@@ -163,9 +179,9 @@ function fill(state, key, data) {
|
|
|
163
179
|
});
|
|
164
180
|
data.rows.slice(0, MAX_ROWS - 1).forEach((values, r) => {
|
|
165
181
|
columns.forEach((_, column) => {
|
|
166
|
-
const
|
|
167
|
-
if (
|
|
168
|
-
cells[addressOf({ row: r + 1, column })] =
|
|
182
|
+
const cell = sourced(values[column]);
|
|
183
|
+
if (cell)
|
|
184
|
+
cells[addressOf({ row: r + 1, column })] = cell;
|
|
169
185
|
});
|
|
170
186
|
});
|
|
171
187
|
const book = {
|
|
@@ -198,6 +214,25 @@ function styled(cell, patch) {
|
|
|
198
214
|
const style = Object.fromEntries(Object.entries({ ...cell.style, ...patch }).filter(([, value]) => !off(value)));
|
|
199
215
|
return { ...cell, style };
|
|
200
216
|
}
|
|
217
|
+
const edgesOf = (range, at) => BORDER_SIDES.filter((side) => (side === 'top' && at.row === range.top) ||
|
|
218
|
+
(side === 'right' && at.column === range.right) ||
|
|
219
|
+
(side === 'bottom' && at.row === range.bottom) ||
|
|
220
|
+
(side === 'left' && at.column === range.left));
|
|
221
|
+
function bordered(sheet, range, sides) {
|
|
222
|
+
return changedCells(sheet, range, (cell, at) => {
|
|
223
|
+
const { border: had = [], ...style } = cell.style ?? {};
|
|
224
|
+
const merge = mergeAt(sheet, at);
|
|
225
|
+
const anchor = merge?.top === at.row && merge.left === at.column;
|
|
226
|
+
const far = merge && anchor ? { row: merge.bottom, column: merge.right } : at;
|
|
227
|
+
const edges = new Set([...had, ...edgesOf(range, at), ...edgesOf(range, far)]);
|
|
228
|
+
const border = sides === 'all'
|
|
229
|
+
? [...BORDER_SIDES]
|
|
230
|
+
: sides === 'none'
|
|
231
|
+
? []
|
|
232
|
+
: BORDER_SIDES.filter((side) => edges.has(side));
|
|
233
|
+
return { ...cell, style: { ...style, ...(border.length ? { border } : {}) } };
|
|
234
|
+
});
|
|
235
|
+
}
|
|
201
236
|
function pasted(state, sheet, rows) {
|
|
202
237
|
const from = selectionOf(state);
|
|
203
238
|
const height = Math.min(rows.length, MAX_ROWS - from.top);
|
|
@@ -216,6 +251,131 @@ function pasted(state, sheet, rows) {
|
|
|
216
251
|
end: { row: range.bottom, column: range.right },
|
|
217
252
|
});
|
|
218
253
|
}
|
|
254
|
+
const modulo = (n, by) => ((n % by) + by) % by;
|
|
255
|
+
function stepOf(cells) {
|
|
256
|
+
const numbers = cells.map((cell) => cell?.formula === undefined && typeof cell?.value === 'number' ? cell.value : null);
|
|
257
|
+
const [first, second] = numbers;
|
|
258
|
+
if (typeof first !== 'number' || typeof second !== 'number')
|
|
259
|
+
return null;
|
|
260
|
+
const step = second - first;
|
|
261
|
+
const even = numbers.every((n, i) => typeof n === 'number' && Math.abs(n - (first + step * i)) < 1e-9);
|
|
262
|
+
return even ? step : null;
|
|
263
|
+
}
|
|
264
|
+
function spread(state, sheet, from, to) {
|
|
265
|
+
const inside = to.top <= from.top &&
|
|
266
|
+
to.left <= from.left &&
|
|
267
|
+
to.bottom >= from.bottom &&
|
|
268
|
+
to.right >= from.right;
|
|
269
|
+
if (!inside || rangeName(from) === rangeName(to))
|
|
270
|
+
return same(state);
|
|
271
|
+
if (to.bottom >= MAX_ROWS || to.right >= MAX_COLUMNS || to.top < 0 || to.left < 0)
|
|
272
|
+
return same(state);
|
|
273
|
+
if (mergesOf(sheet).some((merge) => overlaps(merge, to)))
|
|
274
|
+
return same(state);
|
|
275
|
+
const height = from.bottom - from.top + 1;
|
|
276
|
+
const width = from.right - from.left + 1;
|
|
277
|
+
const down = to.left === from.left && to.right === from.right;
|
|
278
|
+
const across = to.top === from.top && to.bottom === from.bottom;
|
|
279
|
+
const source = (row, column) => cellOf(sheet, addressOf({ row, column }));
|
|
280
|
+
const steps = new Map();
|
|
281
|
+
const stepAt = (at) => {
|
|
282
|
+
if (down === across)
|
|
283
|
+
return null;
|
|
284
|
+
const line = down ? at.column : at.row;
|
|
285
|
+
if (!steps.has(line))
|
|
286
|
+
steps.set(line, stepOf(Array.from({ length: down ? height : width }, (_, i) => down ? source(from.top + i, line) : source(line, from.left + i))));
|
|
287
|
+
return steps.get(line) ?? null;
|
|
288
|
+
};
|
|
289
|
+
const after = changedCells(sheet, to, (cell, at) => {
|
|
290
|
+
const row = from.top + modulo(at.row - from.top, height);
|
|
291
|
+
const column = from.left + modulo(at.column - from.left, width);
|
|
292
|
+
if (row === at.row && column === at.column)
|
|
293
|
+
return cell;
|
|
294
|
+
const copied = source(row, column);
|
|
295
|
+
if (!copied)
|
|
296
|
+
return {};
|
|
297
|
+
if (copied.formula !== undefined)
|
|
298
|
+
return { ...copied, formula: shiftedRefs(copied.formula, at.row - row, at.column - column) };
|
|
299
|
+
const step = stepAt(at);
|
|
300
|
+
if (step === null)
|
|
301
|
+
return copied;
|
|
302
|
+
const first = down ? source(from.top, at.column) : source(at.row, from.left);
|
|
303
|
+
const index = down ? at.row - from.top : at.column - from.left;
|
|
304
|
+
return { ...copied, value: Number((first?.value + step * index).toPrecision(12)) };
|
|
305
|
+
});
|
|
306
|
+
return written(state, after, {
|
|
307
|
+
active: { row: to.top, column: to.left },
|
|
308
|
+
end: { row: to.bottom, column: to.right },
|
|
309
|
+
editing: null,
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
function movedRange(range, move) {
|
|
313
|
+
const span = move.axis === 'row'
|
|
314
|
+
? movedSpan(range.top, range.bottom, move)
|
|
315
|
+
: movedSpan(range.left, range.right, move);
|
|
316
|
+
if (!span)
|
|
317
|
+
return null;
|
|
318
|
+
return move.axis === 'row'
|
|
319
|
+
? { ...range, top: span[0], bottom: span[1] }
|
|
320
|
+
: { ...range, left: span[0], right: span[1] };
|
|
321
|
+
}
|
|
322
|
+
function movedSheet(sheet, move) {
|
|
323
|
+
const cells = {};
|
|
324
|
+
for (const [address, cell] of Object.entries(sheet.cells)) {
|
|
325
|
+
const at = parseAddress(address);
|
|
326
|
+
const to = at && movedIndex(at[move.axis], move);
|
|
327
|
+
if (at && to !== null)
|
|
328
|
+
cells[addressOf({ ...at, [move.axis]: to })] = cell;
|
|
329
|
+
}
|
|
330
|
+
const merges = mergesOf(sheet)
|
|
331
|
+
.flatMap((range) => movedRange(range, move) ?? [])
|
|
332
|
+
.filter((range) => range.top !== range.bottom || range.left !== range.right)
|
|
333
|
+
.map(rangeName);
|
|
334
|
+
const widths = {};
|
|
335
|
+
for (const [letter, width] of Object.entries(sheet.widths ?? {})) {
|
|
336
|
+
const to = move.axis === 'column' ? movedIndex(columnIndex(letter), move) : columnIndex(letter);
|
|
337
|
+
if (to !== null)
|
|
338
|
+
widths[columnName(to)] = width;
|
|
339
|
+
}
|
|
340
|
+
const { merges: _merges, widths: _widths, ...rest } = sheet;
|
|
341
|
+
return {
|
|
342
|
+
...rest,
|
|
343
|
+
cells,
|
|
344
|
+
...(merges.length ? { merges } : {}),
|
|
345
|
+
...(Object.keys(widths).length ? { widths } : {}),
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
function followed(sheet, move, changed) {
|
|
349
|
+
if (sheet.source)
|
|
350
|
+
return sheet;
|
|
351
|
+
let moved = false;
|
|
352
|
+
const cells = {};
|
|
353
|
+
for (const [address, cell] of Object.entries(sheet.cells)) {
|
|
354
|
+
const formula = cell.formula === undefined
|
|
355
|
+
? undefined
|
|
356
|
+
: movedRefs(cell.formula, move, changed.name, sheet.key === changed.key);
|
|
357
|
+
moved ||= formula !== cell.formula;
|
|
358
|
+
cells[address] = formula === undefined ? cell : { ...cell, formula };
|
|
359
|
+
}
|
|
360
|
+
return moved ? { ...sheet, cells } : sheet;
|
|
361
|
+
}
|
|
362
|
+
function restructured(state, sheet, axis, remove) {
|
|
363
|
+
const selection = selectionOf(state);
|
|
364
|
+
const [at, last] = axis === 'row' ? [selection.top, selection.bottom] : [selection.left, selection.right];
|
|
365
|
+
const move = { axis, at, count: last - at + 1, remove };
|
|
366
|
+
const extent = extentOf(sheet);
|
|
367
|
+
const used = axis === 'row' ? extent.rows : extent.columns;
|
|
368
|
+
const limit = axis === 'row' ? MAX_ROWS : MAX_COLUMNS;
|
|
369
|
+
if (!remove && Math.max(used, at) + move.count > limit)
|
|
370
|
+
return same(state);
|
|
371
|
+
const moved = movedSheet(sheet, move);
|
|
372
|
+
const book = {
|
|
373
|
+
...state.book,
|
|
374
|
+
sheets: state.book.sheets.map((other) => followed(other.key === sheet.key ? moved : other, move, sheet)),
|
|
375
|
+
};
|
|
376
|
+
const active = anchorOf(moved, state.active);
|
|
377
|
+
return rewritten(state, book, { active, end: active, editing: null });
|
|
378
|
+
}
|
|
219
379
|
const READS = new Set(['select', 'move', 'cancel', 'width']);
|
|
220
380
|
function onSheet(state, sheet, msg) {
|
|
221
381
|
if (sheet.source && !READS.has(msg.type))
|
|
@@ -260,6 +420,8 @@ function onSheet(state, sheet, msg) {
|
|
|
260
420
|
return pasted(state, sheet, msg.cells);
|
|
261
421
|
case 'style':
|
|
262
422
|
return written(state, changedCells(sheet, selectionOf(state), (cell) => styled(cell, msg.style)));
|
|
423
|
+
case 'border':
|
|
424
|
+
return written(state, bordered(sheet, selectionOf(state), msg.sides));
|
|
263
425
|
case 'merge':
|
|
264
426
|
return merged(state, sheet);
|
|
265
427
|
case 'unmerge':
|
|
@@ -269,6 +431,11 @@ function onSheet(state, sheet, msg) {
|
|
|
269
431
|
const width = Math.round(Math.min(600, Math.max(24, msg.width)));
|
|
270
432
|
return written(state, { ...sheet, widths: { ...sheet.widths, [letter]: width } });
|
|
271
433
|
}
|
|
434
|
+
case 'spread':
|
|
435
|
+
return spread(state, sheet, msg.from, msg.to);
|
|
436
|
+
case 'insert':
|
|
437
|
+
case 'remove':
|
|
438
|
+
return restructured(state, sheet, msg.axis, msg.type === 'remove');
|
|
272
439
|
case 'show':
|
|
273
440
|
case 'add-sheet':
|
|
274
441
|
case 'rename-sheet':
|
|
@@ -287,9 +454,18 @@ function renameSheet(state, key, wanted) {
|
|
|
287
454
|
const name = freeName(state.book, base, sheet.key);
|
|
288
455
|
if (name === sheet.name)
|
|
289
456
|
return same(state);
|
|
457
|
+
const called = (other) => ({
|
|
458
|
+
...other,
|
|
459
|
+
cells: Object.fromEntries(Object.entries(other.cells).map(([address, cell]) => [
|
|
460
|
+
address,
|
|
461
|
+
cell.formula === undefined
|
|
462
|
+
? cell
|
|
463
|
+
: { ...cell, formula: renamedRefs(cell.formula, sheet.name, name) },
|
|
464
|
+
])),
|
|
465
|
+
});
|
|
290
466
|
return rewritten(state, {
|
|
291
467
|
...state.book,
|
|
292
|
-
sheets: state.book.sheets.map((other) =>
|
|
468
|
+
sheets: state.book.sheets.map((other) => other === sheet ? { ...called(sheet), name } : other.source ? other : called(other)),
|
|
293
469
|
});
|
|
294
470
|
}
|
|
295
471
|
function stepBack(state, from) {
|
|
@@ -346,9 +522,13 @@ export function updateBook(state, msg) {
|
|
|
346
522
|
case 'clear':
|
|
347
523
|
case 'paste':
|
|
348
524
|
case 'style':
|
|
525
|
+
case 'border':
|
|
349
526
|
case 'merge':
|
|
350
527
|
case 'unmerge':
|
|
351
|
-
case 'width':
|
|
528
|
+
case 'width':
|
|
529
|
+
case 'spread':
|
|
530
|
+
case 'insert':
|
|
531
|
+
case 'remove': {
|
|
352
532
|
const sheet = shownSheet(state);
|
|
353
533
|
return sheet ? onSheet(state, sheet, msg) : same(state);
|
|
354
534
|
}
|
package/dist/spreadsheet.css
CHANGED
|
@@ -17,6 +17,7 @@ baht-spreadsheet {
|
|
|
17
17
|
font-size: var(--baht-text-sm, 14px);
|
|
18
18
|
color: var(--baht-color-text, #1a2233);
|
|
19
19
|
overflow: hidden;
|
|
20
|
+
position: relative;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
/* ---- tools ---- */
|
|
@@ -235,16 +236,38 @@ baht-spreadsheet {
|
|
|
235
236
|
color: var(--baht-color-primary, #0755d9);
|
|
236
237
|
}
|
|
237
238
|
.baht-spreadsheet-grid td {
|
|
239
|
+
/* the selection's mark over the cell's own lines: its top and left are drawn inside it */
|
|
240
|
+
--baht-spreadsheet-mark: 0 0 transparent;
|
|
241
|
+
--baht-spreadsheet-line: var(--baht-color-text, #1a2233);
|
|
238
242
|
position: relative;
|
|
239
243
|
background: var(--baht-color-surface, #fff);
|
|
244
|
+
box-shadow:
|
|
245
|
+
var(--baht-spreadsheet-mark),
|
|
246
|
+
inset 0 var(--baht-spreadsheet-top, 0) 0 0 var(--baht-spreadsheet-line),
|
|
247
|
+
inset var(--baht-spreadsheet-left, 0) 0 0 0 var(--baht-spreadsheet-line);
|
|
240
248
|
cursor: cell;
|
|
241
249
|
}
|
|
242
250
|
.baht-spreadsheet-grid td[data-selected] {
|
|
243
|
-
|
|
251
|
+
--baht-spreadsheet-mark: inset 0 0 0 999px
|
|
244
252
|
color-mix(in srgb, var(--baht-color-primary, #0755d9) 12%, transparent);
|
|
245
253
|
}
|
|
254
|
+
.baht-spreadsheet-grid td[data-filling] {
|
|
255
|
+
--baht-spreadsheet-mark: inset 0 0 0 1px var(--baht-color-primary, #0755d9);
|
|
256
|
+
}
|
|
246
257
|
.baht-spreadsheet-grid td[data-active] {
|
|
247
|
-
|
|
258
|
+
--baht-spreadsheet-mark: inset 0 0 0 2px var(--baht-color-primary, #0755d9);
|
|
259
|
+
}
|
|
260
|
+
.baht-spreadsheet-handle {
|
|
261
|
+
position: absolute;
|
|
262
|
+
right: -4px;
|
|
263
|
+
bottom: -4px;
|
|
264
|
+
z-index: 2;
|
|
265
|
+
width: 8px;
|
|
266
|
+
height: 8px;
|
|
267
|
+
border: 1px solid var(--baht-color-surface, #fff);
|
|
268
|
+
background: var(--baht-color-primary, #0755d9);
|
|
269
|
+
cursor: crosshair;
|
|
270
|
+
touch-action: none;
|
|
248
271
|
}
|
|
249
272
|
.baht-spreadsheet-grid td[data-error] {
|
|
250
273
|
color: var(--baht-color-danger, #b42318);
|
|
@@ -325,3 +348,35 @@ baht-spreadsheet {
|
|
|
325
348
|
border-radius: var(--baht-radius-sm, 6px);
|
|
326
349
|
font: inherit;
|
|
327
350
|
}
|
|
351
|
+
|
|
352
|
+
/* ---- the grid's menu ---- */
|
|
353
|
+
.baht-spreadsheet-menu {
|
|
354
|
+
position: absolute;
|
|
355
|
+
z-index: 5;
|
|
356
|
+
display: grid;
|
|
357
|
+
min-width: 190px;
|
|
358
|
+
padding: var(--baht-space-1, 4px);
|
|
359
|
+
border: 1px solid var(--baht-color-border, #d6dce8);
|
|
360
|
+
border-radius: var(--baht-radius-md, 8px);
|
|
361
|
+
background: var(--baht-color-surface, #fff);
|
|
362
|
+
box-shadow: 0 8px 24px rgb(0 0 0 / 18%);
|
|
363
|
+
}
|
|
364
|
+
.baht-spreadsheet-menu button {
|
|
365
|
+
appearance: none;
|
|
366
|
+
display: flex;
|
|
367
|
+
align-items: center;
|
|
368
|
+
gap: var(--baht-space-2, 8px);
|
|
369
|
+
padding: 6px var(--baht-space-2, 8px);
|
|
370
|
+
border: 0;
|
|
371
|
+
border-radius: var(--baht-radius-sm, 6px);
|
|
372
|
+
background: none;
|
|
373
|
+
font: inherit;
|
|
374
|
+
color: inherit;
|
|
375
|
+
text-align: left;
|
|
376
|
+
cursor: pointer;
|
|
377
|
+
}
|
|
378
|
+
.baht-spreadsheet-menu button:hover,
|
|
379
|
+
.baht-spreadsheet-menu button:focus-visible {
|
|
380
|
+
background: var(--baht-color-primary-wash, #eef4ff);
|
|
381
|
+
outline: none;
|
|
382
|
+
}
|
package/dist/workbook.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Book, type CellAlign } from './book.js';
|
|
1
|
+
import { type Book, type BorderSide, type CellAlign } from './book.js';
|
|
2
2
|
export interface WorkbookCell {
|
|
3
3
|
/** What the cell holds; for a formula, what it works out to here (a file's reader works it out again). */
|
|
4
4
|
value: string | number | boolean | null;
|
|
@@ -11,6 +11,8 @@ export interface WorkbookCell {
|
|
|
11
11
|
color?: string;
|
|
12
12
|
fill?: string;
|
|
13
13
|
align?: CellAlign;
|
|
14
|
+
/** The sides a thin line is drawn on. */
|
|
15
|
+
border?: BorderSide[];
|
|
14
16
|
/** An Excel number format. */
|
|
15
17
|
format?: string;
|
|
16
18
|
}
|
package/dist/workbook.js
CHANGED