@20baht/spreadsheet 0.0.0-stage → 0.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 +21 -0
- package/README.md +79 -2
- package/dist/book.d.ts +89 -0
- package/dist/book.js +84 -0
- package/dist/compute.d.ts +17 -0
- package/dist/compute.js +103 -0
- package/dist/custom-elements.json +754 -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 +125 -0
- package/dist/elements/baht-spreadsheet.js +712 -0
- package/dist/formula.d.ts +64 -0
- package/dist/formula.js +477 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +7 -0
- package/dist/parse.d.ts +15 -0
- package/dist/parse.js +233 -0
- package/dist/session.d.ts +132 -0
- package/dist/session.js +370 -0
- package/dist/spreadsheet.css +327 -0
- package/dist/tsv.d.ts +3 -0
- package/dist/tsv.js +23 -0
- package/dist/workbook.d.ts +36 -0
- package/dist/workbook.js +40 -0
- package/package.json +75 -4
package/dist/parse.js
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { ALIGNS, MAX_COLUMNS, MAX_ROWS, MAX_SHEETS, NUMBER_FORMATS, overlaps, parseAddress, parseRange, } from './book.js';
|
|
2
|
+
const COLOR = /^#[0-9a-fA-F]{6}$/;
|
|
3
|
+
const KEY = /^[A-Za-z0-9_-]{1,64}$/;
|
|
4
|
+
const UNSAFE = new Set(['__proto__', 'constructor', 'prototype']);
|
|
5
|
+
const MAX_TEXT = 32_767;
|
|
6
|
+
const MAX_FORMULA = 2000;
|
|
7
|
+
const NOT_OBJECT = 'must be an object';
|
|
8
|
+
const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
9
|
+
class Gate {
|
|
10
|
+
constructor() {
|
|
11
|
+
this.issues = [];
|
|
12
|
+
}
|
|
13
|
+
fail(path, message) {
|
|
14
|
+
this.issues.push({ path, message });
|
|
15
|
+
}
|
|
16
|
+
#oneOf(value, known, path) {
|
|
17
|
+
if (value === undefined)
|
|
18
|
+
return undefined;
|
|
19
|
+
const found = known.find((candidate) => candidate === value);
|
|
20
|
+
if (found === undefined)
|
|
21
|
+
this.fail(path, `must be one of ${known.join(', ')}`);
|
|
22
|
+
return found;
|
|
23
|
+
}
|
|
24
|
+
#colour(value, path) {
|
|
25
|
+
if (value === undefined)
|
|
26
|
+
return undefined;
|
|
27
|
+
if (typeof value !== 'string' || !COLOR.test(value)) {
|
|
28
|
+
this.fail(path, 'must be a colour like #1a2b3c');
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
return value.toLowerCase();
|
|
32
|
+
}
|
|
33
|
+
#flag(value, path) {
|
|
34
|
+
if (value !== undefined && typeof value !== 'boolean')
|
|
35
|
+
this.fail(path, 'must be true or false');
|
|
36
|
+
return value === true;
|
|
37
|
+
}
|
|
38
|
+
style(value, path) {
|
|
39
|
+
if (value === undefined)
|
|
40
|
+
return undefined;
|
|
41
|
+
if (!isRecord(value)) {
|
|
42
|
+
this.fail(path, NOT_OBJECT);
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
const bold = this.#flag(value.bold, `${path}.bold`);
|
|
46
|
+
const italic = this.#flag(value.italic, `${path}.italic`);
|
|
47
|
+
const underline = this.#flag(value.underline, `${path}.underline`);
|
|
48
|
+
const color = this.#colour(value.color, `${path}.color`);
|
|
49
|
+
const fill = this.#colour(value.fill, `${path}.fill`);
|
|
50
|
+
const align = this.#oneOf(value.align, ALIGNS, `${path}.align`);
|
|
51
|
+
const format = this.#oneOf(value.format, NUMBER_FORMATS, `${path}.format`);
|
|
52
|
+
const style = {
|
|
53
|
+
...(bold ? { bold } : {}),
|
|
54
|
+
...(italic ? { italic } : {}),
|
|
55
|
+
...(underline ? { underline } : {}),
|
|
56
|
+
...(color === undefined ? {} : { color }),
|
|
57
|
+
...(fill === undefined ? {} : { fill }),
|
|
58
|
+
...(align === undefined ? {} : { align }),
|
|
59
|
+
...(format === undefined ? {} : { format }),
|
|
60
|
+
};
|
|
61
|
+
return Object.keys(style).length ? style : undefined;
|
|
62
|
+
}
|
|
63
|
+
cell(value, path) {
|
|
64
|
+
if (!isRecord(value)) {
|
|
65
|
+
this.fail(path, NOT_OBJECT);
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
const cell = {};
|
|
69
|
+
const held = value.value;
|
|
70
|
+
if (typeof held === 'string' && held.length > MAX_TEXT)
|
|
71
|
+
this.fail(`${path}.value`, `holds at most ${String(MAX_TEXT)} characters`);
|
|
72
|
+
else if (typeof held === 'number' && !Number.isFinite(held))
|
|
73
|
+
this.fail(`${path}.value`, 'must be a finite number');
|
|
74
|
+
else if (typeof held === 'string' || typeof held === 'number' || typeof held === 'boolean')
|
|
75
|
+
cell.value = held;
|
|
76
|
+
else if (held !== undefined)
|
|
77
|
+
this.fail(`${path}.value`, 'must be text, a number or true / false');
|
|
78
|
+
if (value.formula !== undefined) {
|
|
79
|
+
if (typeof value.formula !== 'string' || value.formula === '')
|
|
80
|
+
this.fail(`${path}.formula`, 'must be a formula without its =');
|
|
81
|
+
else if (value.formula.length > MAX_FORMULA)
|
|
82
|
+
this.fail(`${path}.formula`, `holds at most ${String(MAX_FORMULA)} characters`);
|
|
83
|
+
else if (cell.value !== undefined)
|
|
84
|
+
this.fail(path, 'holds a value or a formula, not both');
|
|
85
|
+
else
|
|
86
|
+
cell.formula = value.formula;
|
|
87
|
+
}
|
|
88
|
+
const style = this.style(value.style, `${path}.style`);
|
|
89
|
+
if (style)
|
|
90
|
+
cell.style = style;
|
|
91
|
+
return Object.keys(cell).length ? cell : undefined;
|
|
92
|
+
}
|
|
93
|
+
cells(value, path) {
|
|
94
|
+
const cells = {};
|
|
95
|
+
if (value === undefined)
|
|
96
|
+
return cells;
|
|
97
|
+
if (!isRecord(value)) {
|
|
98
|
+
this.fail(path, 'must be an object of cells by address');
|
|
99
|
+
return cells;
|
|
100
|
+
}
|
|
101
|
+
for (const [address, raw] of Object.entries(value)) {
|
|
102
|
+
const at = parseAddress(address);
|
|
103
|
+
if (!at ||
|
|
104
|
+
address !== address.toUpperCase() ||
|
|
105
|
+
at.row >= MAX_ROWS ||
|
|
106
|
+
at.column >= MAX_COLUMNS) {
|
|
107
|
+
this.fail(`${path}.${address}`, 'is not a cell of the grid (A1 … )');
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
const cell = this.cell(raw, `${path}.${address}`);
|
|
111
|
+
if (cell)
|
|
112
|
+
cells[address] = cell;
|
|
113
|
+
}
|
|
114
|
+
return cells;
|
|
115
|
+
}
|
|
116
|
+
merges(value, path) {
|
|
117
|
+
if (value === undefined)
|
|
118
|
+
return undefined;
|
|
119
|
+
if (!Array.isArray(value)) {
|
|
120
|
+
this.fail(path, 'must be a list of ranges');
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
123
|
+
const ranges = [];
|
|
124
|
+
const names = [];
|
|
125
|
+
value.forEach((raw, i) => {
|
|
126
|
+
const at = `${path}[${String(i)}]`;
|
|
127
|
+
const range = typeof raw === 'string' && raw === raw.toUpperCase() ? parseRange(raw) : null;
|
|
128
|
+
if (!range || range.bottom >= MAX_ROWS || range.right >= MAX_COLUMNS)
|
|
129
|
+
this.fail(at, 'must be a range like A1:C2');
|
|
130
|
+
else if (range.top === range.bottom && range.left === range.right)
|
|
131
|
+
this.fail(at, 'must cover more than one cell');
|
|
132
|
+
else if (ranges.some((other) => overlaps(other, range)))
|
|
133
|
+
this.fail(at, 'overlaps another merged range');
|
|
134
|
+
else {
|
|
135
|
+
ranges.push(range);
|
|
136
|
+
names.push(raw);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
return names.length ? names : undefined;
|
|
140
|
+
}
|
|
141
|
+
widths(value, path) {
|
|
142
|
+
if (value === undefined)
|
|
143
|
+
return undefined;
|
|
144
|
+
if (!isRecord(value)) {
|
|
145
|
+
this.fail(path, 'must be an object of widths by column');
|
|
146
|
+
return undefined;
|
|
147
|
+
}
|
|
148
|
+
const widths = {};
|
|
149
|
+
for (const [column, width] of Object.entries(value)) {
|
|
150
|
+
if (!/^[A-Z]{1,3}$/.test(column) ||
|
|
151
|
+
typeof width !== 'number' ||
|
|
152
|
+
!(width >= 24 && width <= 600))
|
|
153
|
+
this.fail(`${path}.${column}`, 'must be a column (A, B, …) and 24 to 600 pixels');
|
|
154
|
+
else
|
|
155
|
+
widths[column] = width;
|
|
156
|
+
}
|
|
157
|
+
return Object.keys(widths).length ? widths : undefined;
|
|
158
|
+
}
|
|
159
|
+
source(value, path) {
|
|
160
|
+
if (value === undefined)
|
|
161
|
+
return undefined;
|
|
162
|
+
if (!isRecord(value) || typeof value.name !== 'string' || value.name === '') {
|
|
163
|
+
this.fail(path, 'must be an object with the name of a source');
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
if (value.params !== undefined && !isRecord(value.params)) {
|
|
167
|
+
this.fail(`${path}.params`, NOT_OBJECT);
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
|
170
|
+
return { name: value.name, ...(value.params ? { params: value.params } : {}) };
|
|
171
|
+
}
|
|
172
|
+
sheet(value, path) {
|
|
173
|
+
if (!isRecord(value)) {
|
|
174
|
+
this.fail(path, NOT_OBJECT);
|
|
175
|
+
return undefined;
|
|
176
|
+
}
|
|
177
|
+
const { key, name } = value;
|
|
178
|
+
if (typeof key !== 'string' || !KEY.test(key) || UNSAFE.has(key)) {
|
|
179
|
+
this.fail(`${path}.key`, 'must be a key of letters, digits, _ and -');
|
|
180
|
+
return undefined;
|
|
181
|
+
}
|
|
182
|
+
if (typeof name !== 'string' ||
|
|
183
|
+
name.trim() === '' ||
|
|
184
|
+
name.length > 31 ||
|
|
185
|
+
/[\\/?*[\]:!']/.test(name)) {
|
|
186
|
+
this.fail(`${path}.name`, "must be 1 to 31 characters, none of \\ / ? * [ ] : ! '");
|
|
187
|
+
return undefined;
|
|
188
|
+
}
|
|
189
|
+
const merges = this.merges(value.merges, `${path}.merges`);
|
|
190
|
+
const widths = this.widths(value.widths, `${path}.widths`);
|
|
191
|
+
const source = this.source(value.source, `${path}.source`);
|
|
192
|
+
return {
|
|
193
|
+
key,
|
|
194
|
+
name,
|
|
195
|
+
cells: source ? {} : this.cells(value.cells, `${path}.cells`),
|
|
196
|
+
...(merges ? { merges } : {}),
|
|
197
|
+
...(widths ? { widths } : {}),
|
|
198
|
+
...(source ? { source } : {}),
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
export function parseBook(value) {
|
|
203
|
+
const gate = new Gate();
|
|
204
|
+
if (!isRecord(value) || !Array.isArray(value.sheets)) {
|
|
205
|
+
return { ok: false, issues: [{ path: 'sheets', message: 'must be a list of sheets' }] };
|
|
206
|
+
}
|
|
207
|
+
if (value.sheets.length === 0 || value.sheets.length > MAX_SHEETS)
|
|
208
|
+
gate.fail('sheets', `must hold 1 to ${String(MAX_SHEETS)} sheets`);
|
|
209
|
+
if (value.title !== undefined && typeof value.title !== 'string')
|
|
210
|
+
gate.fail('title', 'must be text');
|
|
211
|
+
const keys = new Set();
|
|
212
|
+
const names = new Set();
|
|
213
|
+
const sheets = [];
|
|
214
|
+
value.sheets.slice(0, MAX_SHEETS).forEach((raw, i) => {
|
|
215
|
+
const path = `sheets[${String(i)}]`;
|
|
216
|
+
const sheet = gate.sheet(raw, path);
|
|
217
|
+
if (!sheet)
|
|
218
|
+
return;
|
|
219
|
+
if (keys.has(sheet.key))
|
|
220
|
+
gate.fail(`${path}.key`, 'is the key of another sheet');
|
|
221
|
+
if (names.has(sheet.name.toLowerCase()))
|
|
222
|
+
gate.fail(`${path}.name`, 'is the name of another sheet');
|
|
223
|
+
keys.add(sheet.key);
|
|
224
|
+
names.add(sheet.name.toLowerCase());
|
|
225
|
+
sheets.push(sheet);
|
|
226
|
+
});
|
|
227
|
+
if (gate.issues.length)
|
|
228
|
+
return { ok: false, issues: gate.issues };
|
|
229
|
+
return {
|
|
230
|
+
ok: true,
|
|
231
|
+
book: { ...(typeof value.title === 'string' ? { title: value.title } : {}), sheets },
|
|
232
|
+
};
|
|
233
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { type Book, type BookCell, type BookSheet, type BookSource, type CellPos, type CellRange, type CellStyle, type CellValue } from './book.js';
|
|
2
|
+
export interface BookState {
|
|
3
|
+
book: Book;
|
|
4
|
+
/** The key of the sheet shown. */
|
|
5
|
+
sheet: string;
|
|
6
|
+
/** The cell that takes the keyboard. In a merged range, its top left cell. */
|
|
7
|
+
active: CellPos;
|
|
8
|
+
/** The selection's other corner; the active cell when one cell is selected. */
|
|
9
|
+
end: CellPos;
|
|
10
|
+
/** The text being typed into the active cell; `null` when no cell is being edited. */
|
|
11
|
+
editing: string | null;
|
|
12
|
+
undo: readonly Book[];
|
|
13
|
+
redo: readonly Book[];
|
|
14
|
+
}
|
|
15
|
+
export type BookDirection = 'up' | 'down' | 'left' | 'right';
|
|
16
|
+
/** A change to how cells look: a key set to `null` is taken off. */
|
|
17
|
+
export type StylePatch = {
|
|
18
|
+
[Key in keyof CellStyle]?: CellStyle[Key] | null;
|
|
19
|
+
};
|
|
20
|
+
/** What a source answered: the columns' labels, then a row of values for each record. */
|
|
21
|
+
export interface SourceRows {
|
|
22
|
+
columns: readonly string[];
|
|
23
|
+
rows: readonly (readonly (CellValue | null)[])[];
|
|
24
|
+
}
|
|
25
|
+
export type BookMsg =
|
|
26
|
+
/** Another sheet. */
|
|
27
|
+
{
|
|
28
|
+
type: 'show';
|
|
29
|
+
sheet: string;
|
|
30
|
+
}
|
|
31
|
+
/** A click (`extend`: with Shift, or a drag). */
|
|
32
|
+
| {
|
|
33
|
+
type: 'select';
|
|
34
|
+
cell: CellPos;
|
|
35
|
+
extend?: boolean;
|
|
36
|
+
}
|
|
37
|
+
/** An arrow key. `extend`: with Shift. */
|
|
38
|
+
| {
|
|
39
|
+
type: 'move';
|
|
40
|
+
direction: BookDirection;
|
|
41
|
+
extend?: boolean;
|
|
42
|
+
}
|
|
43
|
+
/** Start editing the active cell: over what it holds (F2, a double click), or with `text` in its place (typing). */
|
|
44
|
+
| {
|
|
45
|
+
type: 'edit';
|
|
46
|
+
text?: string;
|
|
47
|
+
}
|
|
48
|
+
/** The text being edited, as it is typed. */
|
|
49
|
+
| {
|
|
50
|
+
type: 'type';
|
|
51
|
+
text: string;
|
|
52
|
+
}
|
|
53
|
+
/** Keep what was typed. */
|
|
54
|
+
| {
|
|
55
|
+
type: 'commit';
|
|
56
|
+
}
|
|
57
|
+
/** Drop what was typed. */
|
|
58
|
+
| {
|
|
59
|
+
type: 'cancel';
|
|
60
|
+
}
|
|
61
|
+
/** Put `text` into the active cell as if it were typed there. */
|
|
62
|
+
| {
|
|
63
|
+
type: 'set';
|
|
64
|
+
text: string;
|
|
65
|
+
}
|
|
66
|
+
/** Empty the selected cells; how they look stays. */
|
|
67
|
+
| {
|
|
68
|
+
type: 'clear';
|
|
69
|
+
}
|
|
70
|
+
/** Rows of typed text, from the selection's top left cell. */
|
|
71
|
+
| {
|
|
72
|
+
type: 'paste';
|
|
73
|
+
cells: readonly (readonly string[])[];
|
|
74
|
+
}
|
|
75
|
+
/** Change how the selected cells look. */
|
|
76
|
+
| {
|
|
77
|
+
type: 'style';
|
|
78
|
+
style: StylePatch;
|
|
79
|
+
}
|
|
80
|
+
/** Show the selection as one cell: its top left cell keeps what it holds, the others are emptied. */
|
|
81
|
+
| {
|
|
82
|
+
type: 'merge';
|
|
83
|
+
}
|
|
84
|
+
/** Split every merged range the selection touches. */
|
|
85
|
+
| {
|
|
86
|
+
type: 'unmerge';
|
|
87
|
+
}
|
|
88
|
+
/** A column's width in pixels. */
|
|
89
|
+
| {
|
|
90
|
+
type: 'width';
|
|
91
|
+
column: number;
|
|
92
|
+
width: number;
|
|
93
|
+
}
|
|
94
|
+
/** A new sheet, shown; with `source`, a data sheet. */
|
|
95
|
+
| {
|
|
96
|
+
type: 'add-sheet';
|
|
97
|
+
name?: string;
|
|
98
|
+
source?: BookSource;
|
|
99
|
+
} | {
|
|
100
|
+
type: 'rename-sheet';
|
|
101
|
+
sheet: string;
|
|
102
|
+
name: string;
|
|
103
|
+
} | {
|
|
104
|
+
type: 'remove-sheet';
|
|
105
|
+
sheet: string;
|
|
106
|
+
}
|
|
107
|
+
/** What a data sheet's source answered. Not a change to the book as it is saved. */
|
|
108
|
+
| {
|
|
109
|
+
type: 'fill';
|
|
110
|
+
sheet: string;
|
|
111
|
+
data: SourceRows;
|
|
112
|
+
} | {
|
|
113
|
+
type: 'undo';
|
|
114
|
+
} | {
|
|
115
|
+
type: 'redo';
|
|
116
|
+
};
|
|
117
|
+
export interface BookUpdate {
|
|
118
|
+
state: BookState;
|
|
119
|
+
/** The book as it is saved is no longer what it was. */
|
|
120
|
+
changed: boolean;
|
|
121
|
+
}
|
|
122
|
+
export declare function openBook(book: Book, sheet?: string): BookState;
|
|
123
|
+
/** The sheet shown. */
|
|
124
|
+
export declare function shownSheet(state: BookState): BookSheet | undefined;
|
|
125
|
+
/** The selected rectangle: from the active cell to the other corner, grown over every merged range it touches. */
|
|
126
|
+
export declare function selectionOf(state: BookState): CellRange;
|
|
127
|
+
/** The sheets of a book without what their sources answered: the book as it is saved. */
|
|
128
|
+
export declare function savedBook(book: Book): Book;
|
|
129
|
+
/** The state after a message, and whether the book as it is saved changed. */
|
|
130
|
+
export declare function updateBook(state: BookState, msg: BookMsg): BookUpdate;
|
|
131
|
+
/** The selected cells as the text a clipboard takes: what each shows as it was typed. */
|
|
132
|
+
export declare function copied(state: BookState, text: (cell: BookCell | undefined, at: CellPos) => string): string[][];
|