@20baht/spreadsheet 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/LICENSE +21 -0
- package/README.md +83 -2
- package/dist/book.d.ts +97 -0
- package/dist/book.js +85 -0
- package/dist/compute.d.ts +22 -0
- package/dist/compute.js +114 -0
- package/dist/custom-elements.json +906 -0
- package/dist/dates.d.ts +22 -0
- package/dist/dates.js +53 -0
- package/dist/define.d.ts +5 -0
- package/dist/define.js +6 -0
- package/dist/element.d.ts +3 -0
- package/dist/element.js +1 -0
- package/dist/elements/baht-spreadsheet.d.ts +158 -0
- package/dist/elements/baht-spreadsheet.js +902 -0
- package/dist/formula.d.ts +64 -0
- package/dist/formula.js +556 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +9 -0
- package/dist/parse.d.ts +15 -0
- package/dist/parse.js +245 -0
- package/dist/refs.d.ts +43 -0
- package/dist/refs.js +120 -0
- package/dist/session.d.ts +157 -0
- package/dist/session.js +550 -0
- package/dist/spreadsheet.css +382 -0
- package/dist/tsv.d.ts +3 -0
- package/dist/tsv.js +23 -0
- package/dist/workbook.d.ts +38 -0
- package/dist/workbook.js +42 -0
- package/package.json +75 -4
package/dist/session.js
ADDED
|
@@ -0,0 +1,550 @@
|
|
|
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
|
+
import { cellInput, inputText } from './compute.js';
|
|
3
|
+
import { dateSerial } from './dates.js';
|
|
4
|
+
import { movedIndex, movedRefs, movedSpan, renamedRefs, shiftedRefs } from './refs.js';
|
|
5
|
+
const HISTORY = 100;
|
|
6
|
+
const ORIGIN = { row: 0, column: 0 };
|
|
7
|
+
const HEADING = { bold: true, fill: '#e8eef7' };
|
|
8
|
+
export function openBook(book, sheet) {
|
|
9
|
+
const shown = (sheet === undefined ? undefined : sheetOf(book, sheet)) ?? book.sheets[0];
|
|
10
|
+
return {
|
|
11
|
+
book,
|
|
12
|
+
sheet: shown?.key ?? '',
|
|
13
|
+
active: ORIGIN,
|
|
14
|
+
end: ORIGIN,
|
|
15
|
+
editing: null,
|
|
16
|
+
undo: [],
|
|
17
|
+
redo: [],
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export function shownSheet(state) {
|
|
21
|
+
return sheetOf(state.book, state.sheet);
|
|
22
|
+
}
|
|
23
|
+
export function selectionOf(state) {
|
|
24
|
+
const sheet = shownSheet(state);
|
|
25
|
+
let range = rangeOf(state.active, state.end);
|
|
26
|
+
if (!sheet)
|
|
27
|
+
return range;
|
|
28
|
+
const merges = mergesOf(sheet);
|
|
29
|
+
for (let grown = true; grown;) {
|
|
30
|
+
grown = false;
|
|
31
|
+
for (const merge of merges) {
|
|
32
|
+
if (!overlaps(range, merge))
|
|
33
|
+
continue;
|
|
34
|
+
const wider = {
|
|
35
|
+
top: Math.min(range.top, merge.top),
|
|
36
|
+
left: Math.min(range.left, merge.left),
|
|
37
|
+
bottom: Math.max(range.bottom, merge.bottom),
|
|
38
|
+
right: Math.max(range.right, merge.right),
|
|
39
|
+
};
|
|
40
|
+
if (rangeName(wider) !== rangeName(range)) {
|
|
41
|
+
range = wider;
|
|
42
|
+
grown = true;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return range;
|
|
47
|
+
}
|
|
48
|
+
export function savedBook(book) {
|
|
49
|
+
return {
|
|
50
|
+
...book,
|
|
51
|
+
sheets: book.sheets.map((sheet) => (sheet.source ? { ...sheet, cells: {} } : sheet)),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
const cellOf = (sheet, address) => Object.hasOwn(sheet.cells, address) ? sheet.cells[address] : undefined;
|
|
55
|
+
function changedCell(before, change) {
|
|
56
|
+
const after = change(before ?? {});
|
|
57
|
+
if (after.style && Object.keys(after.style).length === 0)
|
|
58
|
+
delete after.style;
|
|
59
|
+
return Object.keys(after).length === 0 ? null : after;
|
|
60
|
+
}
|
|
61
|
+
function changedCells(sheet, range, change) {
|
|
62
|
+
const changed = new Map();
|
|
63
|
+
for (let row = range.top; row <= range.bottom; row++) {
|
|
64
|
+
for (let column = range.left; column <= range.right; column++) {
|
|
65
|
+
const address = addressOf({ row, column });
|
|
66
|
+
changed.set(address, changedCell(cellOf(sheet, address), (cell) => change(cell, { row, column })));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const cells = {};
|
|
70
|
+
for (const [address, cell] of Object.entries(sheet.cells))
|
|
71
|
+
if (!changed.has(address))
|
|
72
|
+
cells[address] = cell;
|
|
73
|
+
for (const [address, cell] of changed)
|
|
74
|
+
if (cell)
|
|
75
|
+
cells[address] = cell;
|
|
76
|
+
return { ...sheet, cells };
|
|
77
|
+
}
|
|
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
|
+
}
|
|
86
|
+
const within = (cell) => ({
|
|
87
|
+
row: Math.min(MAX_ROWS - 1, Math.max(0, cell.row)),
|
|
88
|
+
column: Math.min(MAX_COLUMNS - 1, Math.max(0, cell.column)),
|
|
89
|
+
});
|
|
90
|
+
function anchorOf(sheet, cell) {
|
|
91
|
+
const merge = mergeAt(sheet, cell);
|
|
92
|
+
return merge ? { row: merge.top, column: merge.left } : cell;
|
|
93
|
+
}
|
|
94
|
+
function stepped(sheet, from, direction) {
|
|
95
|
+
const range = mergeAt(sheet, from) ?? rangeOf(from);
|
|
96
|
+
const to = {
|
|
97
|
+
up: { row: range.top - 1, column: from.column },
|
|
98
|
+
down: { row: range.bottom + 1, column: from.column },
|
|
99
|
+
left: { row: from.row, column: range.left - 1 },
|
|
100
|
+
right: { row: from.row, column: range.right + 1 },
|
|
101
|
+
};
|
|
102
|
+
return within(to[direction]);
|
|
103
|
+
}
|
|
104
|
+
const same = (state) => ({ state, changed: false });
|
|
105
|
+
function written(state, sheet, more = {}) {
|
|
106
|
+
const book = {
|
|
107
|
+
...state.book,
|
|
108
|
+
sheets: state.book.sheets.map((other) => (other.key === sheet.key ? sheet : other)),
|
|
109
|
+
};
|
|
110
|
+
return rewritten(state, book, more);
|
|
111
|
+
}
|
|
112
|
+
function rewritten(state, book, more = {}) {
|
|
113
|
+
return {
|
|
114
|
+
state: {
|
|
115
|
+
...state,
|
|
116
|
+
...more,
|
|
117
|
+
book,
|
|
118
|
+
undo: [...state.undo, state.book].slice(-HISTORY),
|
|
119
|
+
redo: [],
|
|
120
|
+
},
|
|
121
|
+
changed: true,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
function freeName(book, base, except) {
|
|
125
|
+
const taken = new Set(book.sheets.filter((sheet) => sheet.key !== except).map((sheet) => sheet.name.toLowerCase()));
|
|
126
|
+
let name = base;
|
|
127
|
+
for (let n = 2; taken.has(name.toLowerCase()); n++)
|
|
128
|
+
name = `${base} ${String(n)}`;
|
|
129
|
+
return name;
|
|
130
|
+
}
|
|
131
|
+
const cleanName = (name) => name
|
|
132
|
+
.replace(/[\\/?*[\]:!']/g, ' ')
|
|
133
|
+
.trim()
|
|
134
|
+
.slice(0, 31);
|
|
135
|
+
function addSheet(state, msg) {
|
|
136
|
+
if (state.book.sheets.length >= MAX_SHEETS)
|
|
137
|
+
return same(state);
|
|
138
|
+
const keys = new Set(state.book.sheets.map((sheet) => sheet.key));
|
|
139
|
+
let n = state.book.sheets.length + 1;
|
|
140
|
+
while (keys.has(`sheet_${String(n)}`))
|
|
141
|
+
n++;
|
|
142
|
+
const base = cleanName(msg.name ?? '') || (msg.source ? 'Data' : `Sheet${String(n)}`);
|
|
143
|
+
const sheet = {
|
|
144
|
+
key: `sheet_${String(n)}`,
|
|
145
|
+
name: freeName(state.book, base),
|
|
146
|
+
cells: {},
|
|
147
|
+
...(msg.source ? { source: msg.source } : {}),
|
|
148
|
+
};
|
|
149
|
+
const book = { ...state.book, sheets: [...state.book.sheets, sheet] };
|
|
150
|
+
return rewritten(state, book, { sheet: sheet.key, active: ORIGIN, end: ORIGIN, editing: null });
|
|
151
|
+
}
|
|
152
|
+
function removeSheet(state, key) {
|
|
153
|
+
const at = state.book.sheets.findIndex((sheet) => sheet.key === key);
|
|
154
|
+
if (at < 0 || state.book.sheets.length < 2)
|
|
155
|
+
return same(state);
|
|
156
|
+
const sheets = state.book.sheets.filter((sheet) => sheet.key !== key);
|
|
157
|
+
const shown = state.sheet === key ? (sheets[Math.min(at, sheets.length - 1)]?.key ?? '') : state.sheet;
|
|
158
|
+
const moved = state.sheet === key ? { active: ORIGIN, end: ORIGIN, editing: null } : {};
|
|
159
|
+
return rewritten(state, { ...state.book, sheets }, { sheet: shown, ...moved });
|
|
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
|
+
}
|
|
171
|
+
function fill(state, key, data) {
|
|
172
|
+
const sheet = sheetOf(state.book, key);
|
|
173
|
+
if (!sheet?.source)
|
|
174
|
+
return same(state);
|
|
175
|
+
const cells = {};
|
|
176
|
+
const columns = data.columns.slice(0, MAX_COLUMNS);
|
|
177
|
+
columns.forEach((label, column) => {
|
|
178
|
+
cells[addressOf({ row: 0, column })] = { value: label, style: HEADING };
|
|
179
|
+
});
|
|
180
|
+
data.rows.slice(0, MAX_ROWS - 1).forEach((values, r) => {
|
|
181
|
+
columns.forEach((_, column) => {
|
|
182
|
+
const cell = sourced(values[column]);
|
|
183
|
+
if (cell)
|
|
184
|
+
cells[addressOf({ row: r + 1, column })] = cell;
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
const book = {
|
|
188
|
+
...state.book,
|
|
189
|
+
sheets: state.book.sheets.map((other) => (other.key === key ? { ...sheet, cells } : other)),
|
|
190
|
+
};
|
|
191
|
+
return { state: { ...state, book }, changed: false };
|
|
192
|
+
}
|
|
193
|
+
function merged(state, sheet) {
|
|
194
|
+
const range = selectionOf(state);
|
|
195
|
+
if (range.top === range.bottom && range.left === range.right)
|
|
196
|
+
return same(state);
|
|
197
|
+
const kept = mergesOf(sheet).filter((merge) => !overlaps(merge, range));
|
|
198
|
+
const anchor = { row: range.top, column: range.left };
|
|
199
|
+
const emptied = changedCells(sheet, range, (cell, at) => at.row === anchor.row && at.column === anchor.column
|
|
200
|
+
? cell
|
|
201
|
+
: { ...(cell.style ? { style: cell.style } : {}) });
|
|
202
|
+
return written(state, { ...emptied, merges: [...kept.map(rangeName), rangeName(range)] }, { active: anchor, end: anchor });
|
|
203
|
+
}
|
|
204
|
+
function unmerged(state, sheet) {
|
|
205
|
+
const range = selectionOf(state);
|
|
206
|
+
const kept = mergesOf(sheet).filter((merge) => !overlaps(merge, range));
|
|
207
|
+
if (kept.length === (sheet.merges ?? []).length)
|
|
208
|
+
return same(state);
|
|
209
|
+
const { merges: _merges, ...rest } = sheet;
|
|
210
|
+
return written(state, kept.length ? { ...rest, merges: kept.map(rangeName) } : rest);
|
|
211
|
+
}
|
|
212
|
+
function styled(cell, patch) {
|
|
213
|
+
const off = (value) => value === null || value === false;
|
|
214
|
+
const style = Object.fromEntries(Object.entries({ ...cell.style, ...patch }).filter(([, value]) => !off(value)));
|
|
215
|
+
return { ...cell, style };
|
|
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
|
+
}
|
|
236
|
+
function pasted(state, sheet, rows) {
|
|
237
|
+
const from = selectionOf(state);
|
|
238
|
+
const height = Math.min(rows.length, MAX_ROWS - from.top);
|
|
239
|
+
const width = Math.min(Math.max(0, ...rows.map((row) => row.length)), MAX_COLUMNS - from.left);
|
|
240
|
+
if (height === 0 || width === 0)
|
|
241
|
+
return same(state);
|
|
242
|
+
const range = {
|
|
243
|
+
top: from.top,
|
|
244
|
+
left: from.left,
|
|
245
|
+
bottom: from.top + height - 1,
|
|
246
|
+
right: from.left + width - 1,
|
|
247
|
+
};
|
|
248
|
+
const after = changedCells(sheet, range, (cell, at) => typed(cell, rows[at.row - from.top]?.[at.column - from.left] ?? ''));
|
|
249
|
+
return written(state, after, {
|
|
250
|
+
active: anchorOf(after, { row: range.top, column: range.left }),
|
|
251
|
+
end: { row: range.bottom, column: range.right },
|
|
252
|
+
});
|
|
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
|
+
}
|
|
379
|
+
const READS = new Set(['select', 'move', 'cancel', 'width']);
|
|
380
|
+
function onSheet(state, sheet, msg) {
|
|
381
|
+
if (sheet.source && !READS.has(msg.type))
|
|
382
|
+
return same(state);
|
|
383
|
+
switch (msg.type) {
|
|
384
|
+
case 'select': {
|
|
385
|
+
const cell = within(msg.cell);
|
|
386
|
+
if (msg.extend === true)
|
|
387
|
+
return same({ ...state, end: cell, editing: null });
|
|
388
|
+
const active = anchorOf(sheet, cell);
|
|
389
|
+
return same({ ...state, active, end: active, editing: null });
|
|
390
|
+
}
|
|
391
|
+
case 'move': {
|
|
392
|
+
if (msg.extend === true)
|
|
393
|
+
return same({ ...state, end: stepped(sheet, state.end, msg.direction) });
|
|
394
|
+
const active = anchorOf(sheet, stepped(sheet, state.active, msg.direction));
|
|
395
|
+
return same({ ...state, active, end: active, editing: null });
|
|
396
|
+
}
|
|
397
|
+
case 'edit':
|
|
398
|
+
return same({
|
|
399
|
+
...state,
|
|
400
|
+
end: state.active,
|
|
401
|
+
editing: msg.text ?? inputText(cellOf(sheet, addressOf(state.active))),
|
|
402
|
+
});
|
|
403
|
+
case 'type':
|
|
404
|
+
return state.editing === null ? same(state) : same({ ...state, editing: msg.text });
|
|
405
|
+
case 'cancel':
|
|
406
|
+
return same({ ...state, editing: null });
|
|
407
|
+
case 'commit':
|
|
408
|
+
case 'set': {
|
|
409
|
+
const text = msg.type === 'set' ? msg.text : state.editing;
|
|
410
|
+
if (text === null)
|
|
411
|
+
return same(state);
|
|
412
|
+
const address = addressOf(state.active);
|
|
413
|
+
if (text === inputText(cellOf(sheet, address)))
|
|
414
|
+
return same({ ...state, editing: null });
|
|
415
|
+
return written(state, changedCells(sheet, rangeOf(state.active), (cell) => typed(cell, text)), { editing: null });
|
|
416
|
+
}
|
|
417
|
+
case 'clear':
|
|
418
|
+
return written(state, changedCells(sheet, selectionOf(state), (cell) => typed(cell, '')));
|
|
419
|
+
case 'paste':
|
|
420
|
+
return pasted(state, sheet, msg.cells);
|
|
421
|
+
case 'style':
|
|
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));
|
|
425
|
+
case 'merge':
|
|
426
|
+
return merged(state, sheet);
|
|
427
|
+
case 'unmerge':
|
|
428
|
+
return unmerged(state, sheet);
|
|
429
|
+
case 'width': {
|
|
430
|
+
const letter = addressOf({ row: 0, column: msg.column }).replace(/\d+$/, '');
|
|
431
|
+
const width = Math.round(Math.min(600, Math.max(24, msg.width)));
|
|
432
|
+
return written(state, { ...sheet, widths: { ...sheet.widths, [letter]: width } });
|
|
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');
|
|
439
|
+
case 'show':
|
|
440
|
+
case 'add-sheet':
|
|
441
|
+
case 'rename-sheet':
|
|
442
|
+
case 'remove-sheet':
|
|
443
|
+
case 'fill':
|
|
444
|
+
case 'undo':
|
|
445
|
+
case 'redo':
|
|
446
|
+
return same(state);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
function renameSheet(state, key, wanted) {
|
|
450
|
+
const sheet = sheetOf(state.book, key);
|
|
451
|
+
const base = cleanName(wanted);
|
|
452
|
+
if (!sheet || base === '')
|
|
453
|
+
return same(state);
|
|
454
|
+
const name = freeName(state.book, base, sheet.key);
|
|
455
|
+
if (name === sheet.name)
|
|
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
|
+
});
|
|
466
|
+
return rewritten(state, {
|
|
467
|
+
...state.book,
|
|
468
|
+
sheets: state.book.sheets.map((other) => other === sheet ? { ...called(sheet), name } : other.source ? other : called(other)),
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
function stepBack(state, from) {
|
|
472
|
+
const to = from === 'undo' ? 'redo' : 'undo';
|
|
473
|
+
const book = state[from].at(-1);
|
|
474
|
+
if (!book)
|
|
475
|
+
return same(state);
|
|
476
|
+
const kept = {
|
|
477
|
+
...book,
|
|
478
|
+
sheets: book.sheets.map((sheet) => {
|
|
479
|
+
const now = sheetOf(state.book, sheet.key);
|
|
480
|
+
return sheet.source && now?.source ? { ...sheet, cells: now.cells } : sheet;
|
|
481
|
+
}),
|
|
482
|
+
};
|
|
483
|
+
const shown = sheetOf(kept, state.sheet) ? state.sheet : (kept.sheets[0]?.key ?? '');
|
|
484
|
+
const moved = shown === state.sheet ? {} : { active: ORIGIN, end: ORIGIN };
|
|
485
|
+
return {
|
|
486
|
+
state: {
|
|
487
|
+
...state,
|
|
488
|
+
...moved,
|
|
489
|
+
book: kept,
|
|
490
|
+
sheet: shown,
|
|
491
|
+
editing: null,
|
|
492
|
+
[from]: state[from].slice(0, -1),
|
|
493
|
+
[to]: [...state[to], state.book].slice(-HISTORY),
|
|
494
|
+
},
|
|
495
|
+
changed: true,
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
export function updateBook(state, msg) {
|
|
499
|
+
switch (msg.type) {
|
|
500
|
+
case 'show':
|
|
501
|
+
return sheetOf(state.book, msg.sheet) && msg.sheet !== state.sheet
|
|
502
|
+
? same({ ...state, sheet: msg.sheet, active: ORIGIN, end: ORIGIN, editing: null })
|
|
503
|
+
: same(state);
|
|
504
|
+
case 'add-sheet':
|
|
505
|
+
return addSheet(state, msg);
|
|
506
|
+
case 'remove-sheet':
|
|
507
|
+
return removeSheet(state, msg.sheet);
|
|
508
|
+
case 'rename-sheet':
|
|
509
|
+
return renameSheet(state, msg.sheet, msg.name);
|
|
510
|
+
case 'fill':
|
|
511
|
+
return fill(state, msg.sheet, msg.data);
|
|
512
|
+
case 'undo':
|
|
513
|
+
case 'redo':
|
|
514
|
+
return stepBack(state, msg.type);
|
|
515
|
+
case 'select':
|
|
516
|
+
case 'move':
|
|
517
|
+
case 'edit':
|
|
518
|
+
case 'type':
|
|
519
|
+
case 'commit':
|
|
520
|
+
case 'cancel':
|
|
521
|
+
case 'set':
|
|
522
|
+
case 'clear':
|
|
523
|
+
case 'paste':
|
|
524
|
+
case 'style':
|
|
525
|
+
case 'border':
|
|
526
|
+
case 'merge':
|
|
527
|
+
case 'unmerge':
|
|
528
|
+
case 'width':
|
|
529
|
+
case 'spread':
|
|
530
|
+
case 'insert':
|
|
531
|
+
case 'remove': {
|
|
532
|
+
const sheet = shownSheet(state);
|
|
533
|
+
return sheet ? onSheet(state, sheet, msg) : same(state);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
export function copied(state, text) {
|
|
538
|
+
const sheet = shownSheet(state);
|
|
539
|
+
if (!sheet)
|
|
540
|
+
return [];
|
|
541
|
+
const range = selectionOf(state);
|
|
542
|
+
const rows = [];
|
|
543
|
+
for (let row = range.top; row <= range.bottom; row++) {
|
|
544
|
+
const cells = [];
|
|
545
|
+
for (let column = range.left; column <= range.right; column++)
|
|
546
|
+
cells.push(text(cellOf(sheet, addressOf({ row, column })), { row, column }));
|
|
547
|
+
rows.push(cells);
|
|
548
|
+
}
|
|
549
|
+
return rows;
|
|
550
|
+
}
|