@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.
@@ -0,0 +1,382 @@
1
+ /* @20baht/spreadsheet: <baht-spreadsheet>'s classes. It reads the base --baht-* tokens (space, type,
2
+ colour, radius) of @20baht/ui/tokens.css: link that sheet first, then this one. */
3
+
4
+ baht-spreadsheet {
5
+ display: block;
6
+ min-height: 0;
7
+ }
8
+ .baht-spreadsheet {
9
+ display: flex;
10
+ flex-direction: column;
11
+ height: 100%;
12
+ min-height: 0;
13
+ border: 1px solid var(--baht-color-border, #d6dce8);
14
+ border-radius: var(--baht-radius-md, 8px);
15
+ background: var(--baht-color-surface, #fff);
16
+ font-family: var(--baht-font-family, system-ui, sans-serif);
17
+ font-size: var(--baht-text-sm, 14px);
18
+ color: var(--baht-color-text, #1a2233);
19
+ overflow: hidden;
20
+ position: relative;
21
+ }
22
+
23
+ /* ---- tools ---- */
24
+ .baht-spreadsheet-tools {
25
+ display: flex;
26
+ flex-wrap: wrap;
27
+ align-items: center;
28
+ gap: 2px;
29
+ padding: var(--baht-space-1, 4px) var(--baht-space-2, 8px);
30
+ border-bottom: 1px solid var(--baht-color-border, #d6dce8);
31
+ }
32
+ .baht-spreadsheet-gap {
33
+ width: 1px;
34
+ height: 18px;
35
+ margin: 0 var(--baht-space-1, 4px);
36
+ background: var(--baht-color-border, #d6dce8);
37
+ }
38
+ .baht-spreadsheet-tool {
39
+ appearance: none;
40
+ display: inline-flex;
41
+ align-items: center;
42
+ justify-content: center;
43
+ min-width: 28px;
44
+ height: 28px;
45
+ padding: 0 6px;
46
+ border: 1px solid transparent;
47
+ border-radius: var(--baht-radius-sm, 6px);
48
+ background: none;
49
+ font: inherit;
50
+ color: inherit;
51
+ cursor: pointer;
52
+ }
53
+ .baht-spreadsheet-tool:hover:not(:disabled) {
54
+ background: var(--baht-color-primary-wash, #eef4ff);
55
+ }
56
+ .baht-spreadsheet-tool[aria-pressed='true'] {
57
+ border-color: var(--baht-color-primary, #0755d9);
58
+ background: var(--baht-color-primary-wash, #eef4ff);
59
+ color: var(--baht-color-primary, #0755d9);
60
+ }
61
+ .baht-spreadsheet-tool:disabled {
62
+ opacity: 0.4;
63
+ cursor: default;
64
+ }
65
+ .baht-spreadsheet-tool:focus-visible,
66
+ .baht-spreadsheet-tab:focus-visible,
67
+ .baht-spreadsheet-format:focus-visible {
68
+ outline: 2px solid var(--baht-color-primary, #0755d9);
69
+ outline-offset: 1px;
70
+ }
71
+ .baht-spreadsheet-tool [data-look='bold'] {
72
+ font-weight: 700;
73
+ }
74
+ .baht-spreadsheet-tool [data-look='italic'] {
75
+ font-style: italic;
76
+ font-family: Georgia, serif;
77
+ }
78
+ .baht-spreadsheet-tool [data-look='underline'] {
79
+ text-decoration: underline;
80
+ }
81
+ .baht-spreadsheet-colour {
82
+ position: relative;
83
+ display: inline-flex;
84
+ align-items: center;
85
+ justify-content: center;
86
+ width: 28px;
87
+ height: 28px;
88
+ border-radius: var(--baht-radius-sm, 6px);
89
+ cursor: pointer;
90
+ }
91
+ .baht-spreadsheet-colour:hover {
92
+ background: var(--baht-color-primary-wash, #eef4ff);
93
+ }
94
+ .baht-spreadsheet-colour input {
95
+ position: absolute;
96
+ inset: 0;
97
+ width: 100%;
98
+ height: 100%;
99
+ opacity: 0;
100
+ cursor: pointer;
101
+ }
102
+ .baht-spreadsheet-colour [data-swatch] {
103
+ font-weight: 700;
104
+ line-height: 1;
105
+ padding-bottom: 2px;
106
+ border-bottom: 4px solid var(--swatch);
107
+ }
108
+ .baht-spreadsheet-colour [data-swatch='fill'] {
109
+ padding: 1px 4px 2px;
110
+ border: 1px solid var(--baht-color-border, #d6dce8);
111
+ border-bottom-width: 4px;
112
+ border-bottom-color: var(--swatch);
113
+ border-radius: 3px;
114
+ }
115
+ .baht-spreadsheet-format {
116
+ height: 28px;
117
+ margin-left: var(--baht-space-1, 4px);
118
+ border: 1px solid var(--baht-color-border, #d6dce8);
119
+ border-radius: var(--baht-radius-sm, 6px);
120
+ background: var(--baht-color-surface, #fff);
121
+ font: inherit;
122
+ font-size: var(--baht-text-xs, 12px);
123
+ color: inherit;
124
+ }
125
+
126
+ /* ---- the formula bar ---- */
127
+ .baht-spreadsheet-bar {
128
+ display: flex;
129
+ align-items: center;
130
+ gap: var(--baht-space-2, 8px);
131
+ padding: var(--baht-space-1, 4px) var(--baht-space-2, 8px);
132
+ border-bottom: 1px solid var(--baht-color-border, #d6dce8);
133
+ }
134
+ .baht-spreadsheet-name {
135
+ min-width: 72px;
136
+ font-size: var(--baht-text-xs, 12px);
137
+ font-variant-numeric: tabular-nums;
138
+ color: var(--baht-color-text-muted, #5b6679);
139
+ }
140
+ .baht-spreadsheet-fx {
141
+ font-style: italic;
142
+ font-family: Georgia, serif;
143
+ color: var(--baht-color-text-muted, #5b6679);
144
+ }
145
+ .baht-spreadsheet-bar-input {
146
+ flex: 1;
147
+ min-width: 0;
148
+ height: 26px;
149
+ padding: 0 var(--baht-space-2, 8px);
150
+ border: 1px solid transparent;
151
+ border-radius: var(--baht-radius-sm, 6px);
152
+ background: none;
153
+ font: inherit;
154
+ color: inherit;
155
+ }
156
+ .baht-spreadsheet-bar-input:focus {
157
+ outline: none;
158
+ border-color: var(--baht-color-primary, #0755d9);
159
+ }
160
+
161
+ .baht-spreadsheet-notice {
162
+ display: flex;
163
+ align-items: center;
164
+ justify-content: space-between;
165
+ gap: var(--baht-space-2, 8px);
166
+ padding: var(--baht-space-1, 4px) var(--baht-space-3, 12px);
167
+ border-bottom: 1px solid var(--baht-color-border, #d6dce8);
168
+ background: var(--baht-color-primary-wash, #eef4ff);
169
+ font-size: var(--baht-text-xs, 12px);
170
+ }
171
+ .baht-spreadsheet-notice[data-failed] {
172
+ background: color-mix(in srgb, var(--baht-color-danger, #b42318) 12%, transparent);
173
+ color: var(--baht-color-danger, #b42318);
174
+ }
175
+ .baht-spreadsheet-notice .baht-spreadsheet-tool {
176
+ border-color: currentColor;
177
+ font-size: var(--baht-text-xs, 12px);
178
+ }
179
+
180
+ /* ---- the grid ---- */
181
+ .baht-spreadsheet-scroll {
182
+ flex: 1;
183
+ min-height: 0;
184
+ overflow: auto;
185
+ }
186
+ .baht-spreadsheet-grid {
187
+ border-collapse: separate;
188
+ border-spacing: 0;
189
+ table-layout: fixed;
190
+ width: max-content;
191
+ font-variant-numeric: tabular-nums;
192
+ outline: none;
193
+ user-select: none;
194
+ }
195
+ .baht-spreadsheet-grid col.baht-spreadsheet-corner {
196
+ width: 44px;
197
+ }
198
+ .baht-spreadsheet-grid th,
199
+ .baht-spreadsheet-grid td {
200
+ height: var(--baht-spreadsheet-row, 26px);
201
+ padding: 0 6px;
202
+ border-right: 1px solid var(--baht-color-border, #d6dce8);
203
+ border-bottom: 1px solid var(--baht-color-border, #d6dce8);
204
+ overflow: hidden;
205
+ white-space: nowrap;
206
+ text-overflow: ellipsis;
207
+ box-sizing: border-box;
208
+ }
209
+ .baht-spreadsheet-grid th {
210
+ position: sticky;
211
+ z-index: 1;
212
+ background: var(--baht-color-surface-alt, #f3f5f9);
213
+ font-weight: 500;
214
+ font-size: var(--baht-text-xs, 12px);
215
+ color: var(--baht-color-text-muted, #5b6679);
216
+ text-align: center;
217
+ }
218
+ .baht-spreadsheet-grid thead th {
219
+ top: 0;
220
+ z-index: 2;
221
+ }
222
+ .baht-spreadsheet-grid tbody th,
223
+ .baht-spreadsheet-grid th.baht-spreadsheet-corner {
224
+ left: 0;
225
+ }
226
+ .baht-spreadsheet-grid th.baht-spreadsheet-corner {
227
+ z-index: 3;
228
+ }
229
+ .baht-spreadsheet-grid th[data-selected] {
230
+ /* mixed, not the wash itself: a header stays opaque over the rows that scroll under it */
231
+ background: color-mix(
232
+ in srgb,
233
+ var(--baht-color-primary, #0755d9) 16%,
234
+ var(--baht-color-surface-alt, #f3f5f9)
235
+ );
236
+ color: var(--baht-color-primary, #0755d9);
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);
242
+ position: relative;
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);
248
+ cursor: cell;
249
+ }
250
+ .baht-spreadsheet-grid td[data-selected] {
251
+ --baht-spreadsheet-mark: inset 0 0 0 999px
252
+ color-mix(in srgb, var(--baht-color-primary, #0755d9) 12%, transparent);
253
+ }
254
+ .baht-spreadsheet-grid td[data-filling] {
255
+ --baht-spreadsheet-mark: inset 0 0 0 1px var(--baht-color-primary, #0755d9);
256
+ }
257
+ .baht-spreadsheet-grid td[data-active] {
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;
271
+ }
272
+ .baht-spreadsheet-grid td[data-error] {
273
+ color: var(--baht-color-danger, #b42318);
274
+ }
275
+ .baht-spreadsheet-spacer td {
276
+ padding: 0;
277
+ border: 0;
278
+ height: auto;
279
+ }
280
+ .baht-spreadsheet-resize {
281
+ position: absolute;
282
+ top: 0;
283
+ right: -3px;
284
+ width: 7px;
285
+ height: 100%;
286
+ cursor: col-resize;
287
+ touch-action: none;
288
+ }
289
+ .baht-spreadsheet-editor {
290
+ position: absolute;
291
+ inset: 0;
292
+ width: 100%;
293
+ height: 100%;
294
+ padding: 0 6px;
295
+ border: 0;
296
+ background: var(--baht-color-surface, #fff);
297
+ font: inherit;
298
+ color: var(--baht-color-text, #1a2233);
299
+ outline: 2px solid var(--baht-color-primary, #0755d9);
300
+ outline-offset: -2px;
301
+ box-sizing: border-box;
302
+ }
303
+
304
+ /* ---- sheets ---- */
305
+ .baht-spreadsheet-tabs {
306
+ display: flex;
307
+ flex-wrap: wrap;
308
+ align-items: center;
309
+ gap: 2px;
310
+ padding: 2px var(--baht-space-2, 8px);
311
+ border-top: 1px solid var(--baht-color-border, #d6dce8);
312
+ background: var(--baht-color-surface-alt, #f3f5f9);
313
+ }
314
+ .baht-spreadsheet-tab {
315
+ appearance: none;
316
+ display: inline-flex;
317
+ align-items: center;
318
+ gap: 6px;
319
+ padding: var(--baht-space-1, 4px) var(--baht-space-3, 12px);
320
+ border: 0;
321
+ border-bottom: 2px solid transparent;
322
+ background: none;
323
+ font: inherit;
324
+ color: var(--baht-color-text-muted, #5b6679);
325
+ cursor: pointer;
326
+ }
327
+ .baht-spreadsheet-tab[aria-selected='true'] {
328
+ border-bottom-color: var(--baht-color-primary, #0755d9);
329
+ background: var(--baht-color-surface, #fff);
330
+ color: var(--baht-color-text, #1a2233);
331
+ font-weight: 600;
332
+ }
333
+ .baht-spreadsheet-badge {
334
+ padding: 0 6px;
335
+ border-radius: 999px;
336
+ background: var(--baht-color-primary-wash, #eef4ff);
337
+ font-size: 10px;
338
+ font-weight: 600;
339
+ letter-spacing: 0.02em;
340
+ text-transform: uppercase;
341
+ color: var(--baht-color-primary, #0755d9);
342
+ }
343
+ .baht-spreadsheet-rename {
344
+ width: 120px;
345
+ height: 26px;
346
+ padding: 0 var(--baht-space-2, 8px);
347
+ border: 1px solid var(--baht-color-primary, #0755d9);
348
+ border-radius: var(--baht-radius-sm, 6px);
349
+ font: inherit;
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/tsv.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export declare function toTsv(cells: readonly (readonly string[])[]): string;
2
+ /** The rows of a clipboard's text. The line break a spreadsheet ends it with is not a row. */
3
+ export declare function fromTsv(text: string): string[][];
package/dist/tsv.js ADDED
@@ -0,0 +1,23 @@
1
+ const quote = (cell) => /["\t\n\r]/.test(cell) ? `"${cell.replaceAll('"', '""')}"` : cell;
2
+ export function toTsv(cells) {
3
+ return cells.map((row) => row.map(quote).join('\t')).join('\n');
4
+ }
5
+ const CELL = /"((?:[^"]|"")*)"?([^\t\n]*)|([^\t\n]*)/y;
6
+ export function fromTsv(text) {
7
+ const source = text.replace(/\r\n?/g, '\n').replace(/\n$/, '');
8
+ if (source === '')
9
+ return [];
10
+ const rows = [[]];
11
+ CELL.lastIndex = 0;
12
+ for (;;) {
13
+ const match = CELL.exec(source);
14
+ const quoted = match[1];
15
+ rows.at(-1).push(quoted === undefined ? (match[3] ?? '') : quoted.replaceAll('""', '"') + (match[2] ?? ''));
16
+ const next = source[CELL.lastIndex];
17
+ if (next === undefined)
18
+ return rows;
19
+ if (next === '\n')
20
+ rows.push([]);
21
+ CELL.lastIndex++;
22
+ }
23
+ }
@@ -0,0 +1,38 @@
1
+ import { type Book, type BorderSide, type CellAlign } from './book.js';
2
+ export interface WorkbookCell {
3
+ /** What the cell holds; for a formula, what it works out to here (a file's reader works it out again). */
4
+ value: string | number | boolean | null;
5
+ /** The formula, without its `=`. */
6
+ formula?: string;
7
+ bold?: boolean;
8
+ italic?: boolean;
9
+ underline?: boolean;
10
+ /** `#rrggbb`. */
11
+ color?: string;
12
+ fill?: string;
13
+ align?: CellAlign;
14
+ /** The sides a thin line is drawn on. */
15
+ border?: BorderSide[];
16
+ /** An Excel number format. */
17
+ format?: string;
18
+ }
19
+ export interface WorkbookSheet {
20
+ name: string;
21
+ cells: WorkbookCell[][];
22
+ merges: {
23
+ row: number;
24
+ column: number;
25
+ rows: number;
26
+ columns: number;
27
+ }[];
28
+ /** In characters. */
29
+ widths: number[];
30
+ /** A data sheet keeps its first row in view. */
31
+ frozen: number;
32
+ }
33
+ /**
34
+ * The book's sheets, each laid out for a workbook: every cell with its look, a formula as the
35
+ * formula and the value it has here, the merged ranges, the column widths. Hand the result to
36
+ * `@20baht/export`'s `xlsx()`.
37
+ */
38
+ export declare function bookWorkbook(book: Book): WorkbookSheet[];
@@ -0,0 +1,42 @@
1
+ import { addressOf, columnName, DEFAULT_WIDTH, extentOf, mergesOf, } from './book.js';
2
+ import { computeBook } from './compute.js';
3
+ import { isError } from './formula.js';
4
+ const FORMATS = {
5
+ number: '#,##0.00',
6
+ integer: '#,##0',
7
+ percent: '0.00%',
8
+ date: 'yyyy-mm-dd',
9
+ datetime: 'yyyy-mm-dd hh:mm',
10
+ };
11
+ const PIXELS_PER_CHARACTER = 7;
12
+ const held = (value) => (isError(value) ? value.error : value);
13
+ function laid(sheet, at) {
14
+ const { rows, columns } = extentOf(sheet);
15
+ const cells = Array.from({ length: rows }, (_, row) => Array.from({ length: columns }, (_unused, column) => {
16
+ const address = addressOf({ row, column });
17
+ const cell = Object.hasOwn(sheet.cells, address) ? sheet.cells[address] : undefined;
18
+ const { format, ...look } = cell?.style ?? {};
19
+ return {
20
+ value: held(at(row, column)),
21
+ ...(cell?.formula === undefined ? {} : { formula: cell.formula }),
22
+ ...look,
23
+ ...(format ? { format: FORMATS[format] } : {}),
24
+ };
25
+ }));
26
+ return {
27
+ name: sheet.name,
28
+ cells,
29
+ merges: mergesOf(sheet).map((range) => ({
30
+ row: range.top,
31
+ column: range.left,
32
+ rows: range.bottom - range.top + 1,
33
+ columns: range.right - range.left + 1,
34
+ })),
35
+ widths: Array.from({ length: columns }, (_, column) => Math.round((sheet.widths?.[columnName(column)] ?? DEFAULT_WIDTH) / PIXELS_PER_CHARACTER)),
36
+ frozen: sheet.source ? 1 : 0,
37
+ };
38
+ }
39
+ export function bookWorkbook(book) {
40
+ const values = computeBook(book);
41
+ return book.sheets.map((sheet) => laid(sheet, (row, column) => values.at(sheet.key, row, column)));
42
+ }
package/package.json CHANGED
@@ -1,6 +1,77 @@
1
1
  {
2
2
  "name": "@20baht/spreadsheet",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.0",
4
+ "type": "module",
5
+ "engines": {
6
+ "node": ">=20.19.0"
7
+ },
8
+ "description": "A book of free cells from one JSON document: values and formulas (SUM, COUNT, IF, COUNTIF, …), bold / italic / colours, merged ranges, and data sheets a host fills from its own records. Headless: the authoring gate, the formulas, the grid session, the .xlsx layout. /element: <baht-spreadsheet>, a grid worked like Excel.",
9
+ "license": "MIT",
10
+ "author": "Nattapong Promthong",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/bank20baht/20baht.git",
14
+ "directory": "packages/core/spreadsheet"
15
+ },
16
+ "homepage": "https://github.com/bank20baht/20baht/tree/main/packages/core/spreadsheet#readme",
17
+ "bugs": {
18
+ "url": "https://github.com/bank20baht/20baht/issues"
19
+ },
20
+ "keywords": [
21
+ "spreadsheet",
22
+ "formulas",
23
+ "excel",
24
+ "xlsx",
25
+ "datagrid",
26
+ "json",
27
+ "web-components",
28
+ "lit",
29
+ "bahtui"
30
+ ],
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "files": [
35
+ "dist"
36
+ ],
37
+ "exports": {
38
+ ".": {
39
+ "types": "./dist/index.d.ts",
40
+ "import": "./dist/index.js",
41
+ "default": "./dist/index.js"
42
+ },
43
+ "./element": {
44
+ "types": "./dist/element.d.ts",
45
+ "import": "./dist/element.js",
46
+ "default": "./dist/element.js"
47
+ },
48
+ "./spreadsheet.css": "./dist/spreadsheet.css",
49
+ "./custom-elements.json": "./dist/custom-elements.json",
50
+ "./package.json": "./package.json"
51
+ },
52
+ "scripts": {
53
+ "build": "rm -rf dist && tsc -p tsconfig.json --removeComments && tsc -p tsconfig.json --emitDeclarationOnly && cp src/spreadsheet.css dist/spreadsheet.css && bun run analyze",
54
+ "analyze": "custom-elements-manifest analyze",
55
+ "test": "vitest run",
56
+ "test:watch": "vitest",
57
+ "test:coverage": "vitest run --coverage"
58
+ },
59
+ "dependencies": {
60
+ "@20baht/element": ">=0.1.0 <0.2.0",
61
+ "lit": "^3.3.0"
62
+ },
63
+ "devDependencies": {
64
+ "@20baht/export": "workspace:*",
65
+ "@custom-elements-manifest/analyzer": "^0.11.0",
66
+ "happy-dom": "^21.0.4",
67
+ "typescript": "^5.9.3",
68
+ "vitest": "^3.2.4"
69
+ },
70
+ "main": "./dist/index.js",
71
+ "types": "./dist/index.d.ts",
72
+ "sideEffects": [
73
+ "./dist/element.js",
74
+ "./dist/elements/baht-*.js"
75
+ ],
76
+ "customElements": "dist/custom-elements.json"
77
+ }