@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,22 @@
1
+ /** The serial of a wall clock; `null` when there is no such day or time. */
2
+ export declare function serialOf(year: number, month: number, day: number, hour?: number, minute?: number, second?: number): number | null;
3
+ /** A moment as the serial of its wall clock where this runs: what a source hands over as a `Date`. */
4
+ export declare function dateSerial(date: Date): number | null;
5
+ /** The wall clock of a serial, to the minute. */
6
+ export declare function serialParts(serial: number): {
7
+ year: number;
8
+ month: number;
9
+ day: number;
10
+ hour: number;
11
+ minute: number;
12
+ };
13
+ /** A serial as `2026-12-31`, with `time` as `2026-12-31 14:30`. */
14
+ export declare function dateText(serial: number, time: boolean): string;
15
+ /**
16
+ * A typed date as its serial: `2026-12-31`, `2026-12-31 14:30`, or day first `31/12/2026`.
17
+ * `time` says a time of day was written. `null` for anything else.
18
+ */
19
+ export declare function parseDateText(text: string): {
20
+ serial: number;
21
+ time: boolean;
22
+ } | null;
package/dist/dates.js ADDED
@@ -0,0 +1,53 @@
1
+ const DAY = 86_400_000;
2
+ const EPOCH = Date.UTC(1899, 11, 30);
3
+ export function serialOf(year, month, day, hour = 0, minute = 0, second = 0) {
4
+ const at = new Date(Date.UTC(year, month - 1, day));
5
+ const real = year >= 1900 &&
6
+ year <= 9999 &&
7
+ at.getUTCFullYear() === year &&
8
+ at.getUTCMonth() === month - 1 &&
9
+ at.getUTCDate() === day &&
10
+ hour < 24 &&
11
+ minute < 60 &&
12
+ second < 60;
13
+ if (!real)
14
+ return null;
15
+ return (at.getTime() - EPOCH) / DAY + (hour * 3600 + minute * 60 + second) / 86_400;
16
+ }
17
+ export function dateSerial(date) {
18
+ if (Number.isNaN(date.getTime()))
19
+ return null;
20
+ return serialOf(date.getFullYear(), date.getMonth() + 1, date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds());
21
+ }
22
+ export function serialParts(serial) {
23
+ const seconds = Math.round(serial * 86_400);
24
+ const at = new Date(EPOCH + seconds * 1000);
25
+ return {
26
+ year: at.getUTCFullYear(),
27
+ month: at.getUTCMonth() + 1,
28
+ day: at.getUTCDate(),
29
+ hour: at.getUTCHours(),
30
+ minute: at.getUTCMinutes(),
31
+ };
32
+ }
33
+ const two = (n) => String(n).padStart(2, '0');
34
+ export function dateText(serial, time) {
35
+ if (!Number.isFinite(serial) || serial < 0 || serial > 2_958_466)
36
+ return String(serial);
37
+ const { year, month, day, hour, minute } = serialParts(serial);
38
+ const date = `${String(year).padStart(4, '0')}-${two(month)}-${two(day)}`;
39
+ return time ? `${date} ${two(hour)}:${two(minute)}` : date;
40
+ }
41
+ const ISO = /^(\d{4})-(\d{1,2})-(\d{1,2})(?:[ T](\d{1,2}):(\d{2})(?::(\d{2}))?)?$/;
42
+ const DAY_FIRST = /^(\d{1,2})\/(\d{1,2})\/(\d{4})(?: (\d{1,2}):(\d{2})(?::(\d{2}))?)?$/;
43
+ export function parseDateText(text) {
44
+ const iso = ISO.exec(text.trim());
45
+ const dayFirst = iso ? null : DAY_FIRST.exec(text.trim());
46
+ const found = iso ?? dayFirst;
47
+ if (!found)
48
+ return null;
49
+ const [, a, b, c, hour, minute, second] = found;
50
+ const [year, month, day] = iso ? [a, b, c] : [c, b, a];
51
+ const serial = serialOf(Number(year), Number(month), Number(day), Number(hour ?? 0), Number(minute ?? 0), Number(second ?? 0));
52
+ return serial === null ? null : { serial, time: hour !== undefined };
53
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Lit's `@customElement`, tolerating a tag being defined already (two copies of this package in
3
+ * one page keep the first and warn). The Custom Elements Manifest analyzer keys on this name.
4
+ */
5
+ export declare function customElement(tag: string): (cls: CustomElementConstructor) => void;
package/dist/define.js ADDED
@@ -0,0 +1,6 @@
1
+ import { defineOnce } from '@20baht/element';
2
+ export function customElement(tag) {
3
+ return (cls) => {
4
+ defineOnce(tag, cls, '@20baht/spreadsheet');
5
+ };
6
+ }
@@ -0,0 +1,3 @@
1
+ export { BahtSpreadsheet, SPREADSHEET_LABELS, type SpreadsheetLabels, type SpreadsheetLoader, } from './elements/baht-spreadsheet.js';
2
+ export type { Book, BookSheet, BookSource } from './book.js';
3
+ export type { SourceRows } from './session.js';
@@ -0,0 +1 @@
1
+ export { BahtSpreadsheet, SPREADSHEET_LABELS, } from './elements/baht-spreadsheet.js';
@@ -0,0 +1,158 @@
1
+ import { type PropertyValues, type TemplateResult } from 'lit';
2
+ import { BahtElement } from '@20baht/element';
3
+ import { type Book, type BookSheet, type BookSource } from '../book.js';
4
+ import { type SourceRows } from '../session.js';
5
+ /** Every word the element shows of its own. Give `labels` the ones to say otherwise. */
6
+ export interface SpreadsheetLabels {
7
+ bold: string;
8
+ italic: string;
9
+ underline: string;
10
+ textColor: string;
11
+ fillColor: string;
12
+ noFill: string;
13
+ alignLeft: string;
14
+ alignCenter: string;
15
+ alignRight: string;
16
+ borderAll: string;
17
+ borderOuter: string;
18
+ borderNone: string;
19
+ merge: string;
20
+ unmerge: string;
21
+ insertRow: string;
22
+ insertColumn: string;
23
+ removeRow: string;
24
+ removeColumn: string;
25
+ fillHandle: string;
26
+ format: string;
27
+ formatGeneral: string;
28
+ formatNumber: string;
29
+ formatInteger: string;
30
+ formatPercent: string;
31
+ formatDate: string;
32
+ formatDateTime: string;
33
+ undo: string;
34
+ redo: string;
35
+ cell: string;
36
+ formula: string;
37
+ addSheet: string;
38
+ removeSheet: string;
39
+ renameSheet: string;
40
+ dataSheet: string;
41
+ refresh: string;
42
+ loading: string;
43
+ loadFailed: string;
44
+ readOnlyData: string;
45
+ }
46
+ export declare const SPREADSHEET_LABELS: SpreadsheetLabels;
47
+ /** What a data sheet's source answers with: the host's function. */
48
+ export type SpreadsheetLoader = (source: BookSource, sheet: BookSheet) => Promise<SourceRows>;
49
+ /**
50
+ * A book (spec 12) worked like a spreadsheet: free cells holding text, numbers and formulas, bold /
51
+ * italic / colours, merged ranges, several sheets, and data sheets the host fills (`load`).
52
+ * Click or use the arrows to select, type to write, `=` to start a formula, Shift to select a
53
+ * rectangle, copy and paste with Excel, Delete to empty, Ctrl+B / I / U, Ctrl+Z / Y.
54
+ *
55
+ * @element baht-spreadsheet
56
+ * @fires {CustomEvent<{ value: Book }>} baht-change - The book changed: the book as it is saved, the data sheets empty.
57
+ */
58
+ export declare class BahtSpreadsheet extends BahtElement {
59
+ /** The book (spec 12 §1). Set as a property: `el.value = json`. Starts as the host gives it, then follows the grid (`baht-change`). */
60
+ value: Book;
61
+ /** Shown, selected and copied, never changed. */
62
+ readonly: boolean;
63
+ /** Answers a data sheet's source. Called when a book is opened, a data sheet is added and on `refresh()`. */
64
+ load: SpreadsheetLoader | undefined;
65
+ /** The element's own words, the ones to say otherwise (`SPREADSHEET_LABELS` has them all). */
66
+ labels: Partial<SpreadsheetLabels>;
67
+ private session;
68
+ /** The first row drawn. */
69
+ private first;
70
+ /** The data sheets being loaded, and the ones whose load failed, by key. */
71
+ private pending;
72
+ private failures;
73
+ /** The sheet whose name is being typed. */
74
+ private renaming;
75
+ /** A column being dragged wider or narrower. */
76
+ private resizing;
77
+ /** The cells the fill handle is being dragged over: what was selected, and how far it goes now. */
78
+ private filling;
79
+ /** Where the grid's menu is open, from the element's top left corner. */
80
+ private menu;
81
+ /** The next `value` change is the element's own, already in the session. */
82
+ private ownValue;
83
+ /** The book's data sheets are to be loaded after this update. */
84
+ private stale;
85
+ private dragging;
86
+ private computed;
87
+ /** The book with what its data sheets hold now: what `bookWorkbook()` takes for a file. */
88
+ get book(): Book;
89
+ /** Ask every data sheet's source again (or one sheet's). */
90
+ refresh(sheet?: string): Promise<void>;
91
+ /** Add a data sheet, show it and ask its source. */
92
+ addDataSheet(name: string, source: BookSource): Promise<void>;
93
+ private get words();
94
+ private get values();
95
+ protected willUpdate(changed: PropertyValues<this>): void;
96
+ protected updated(): void;
97
+ private barFocused;
98
+ private send;
99
+ private grid;
100
+ /** Scroll so the selection's moving corner is in view. */
101
+ private reveal;
102
+ private scrolled;
103
+ private gridKey;
104
+ private onKey;
105
+ /** Ctrl (or ⌘) with a letter; copy, cut and paste arrive as their own events. */
106
+ private shortcut;
107
+ /** Ctrl+D / Ctrl+R: the selection's first row down, or its first column across. */
108
+ private fillFromEdge;
109
+ private editorKey;
110
+ private barKey;
111
+ private barInput;
112
+ private shownText;
113
+ private copy;
114
+ private paste;
115
+ private placeOf;
116
+ private pointerDown;
117
+ /** The fill handle pressed: the selection is spread as far as it is dragged, along one way. */
118
+ private fillStart;
119
+ /** How far a fill goes with the pointer on `cell`: down or up, or across, whichever is further. */
120
+ private fillTo;
121
+ private pointerOver;
122
+ /** A right click on a cell: the rows and columns to put in or take out. */
123
+ private contextMenu;
124
+ private menuAway;
125
+ private closeMenu;
126
+ disconnectedCallback(): void;
127
+ private menuBox;
128
+ private doubleClick;
129
+ private resizeStart;
130
+ private get activeStyle();
131
+ private toggle;
132
+ private look;
133
+ protected render(): TemplateResult;
134
+ private tool;
135
+ private tools;
136
+ private formatLabel;
137
+ /** The name of what is selected, and what the active cell holds as it was typed. */
138
+ private bar;
139
+ /** A data sheet says what it is, that it is loading, or that it could not. */
140
+ private notice;
141
+ private widthOf;
142
+ private table;
143
+ private row;
144
+ /**
145
+ * A cell's lines as CSS. Its bottom and right are its own edges; its top and left are the
146
+ * neighbour's, so they are drawn inside it, and only when the neighbour draws none there.
147
+ */
148
+ private lines;
149
+ /** The square at the selection's corner that spreads it; not while typing, nor where nothing is written. */
150
+ private handle;
151
+ private cell;
152
+ private tabs;
153
+ }
154
+ declare global {
155
+ interface HTMLElementTagNameMap {
156
+ 'baht-spreadsheet': BahtSpreadsheet;
157
+ }
158
+ }