ui-sdk-table-web 0.0.0-stage → 0.2.7
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 +7 -2
- package/dist/table-licenses.md +411 -0
- package/dist/table-reference-licenses.md +411 -0
- package/dist/table-reference.js +10631 -0
- package/dist/table.js +10147 -0
- package/docs/api-web-components.md +319 -0
- package/docs/cell-editing.md +234 -0
- package/docs/cells-and-slots.md +106 -0
- package/docs/component-body.md +42 -0
- package/docs/component-cell.md +31 -0
- package/docs/component-col.md +31 -0
- package/docs/component-edit-error.md +31 -0
- package/docs/component-editor.md +31 -0
- package/docs/component-fill-handle.md +31 -0
- package/docs/component-foot.md +33 -0
- package/docs/component-head.md +33 -0
- package/docs/component-header-cell.md +31 -0
- package/docs/component-header-row.md +33 -0
- package/docs/component-plain-row.md +33 -0
- package/docs/component-resizer.md +33 -0
- package/docs/component-root.md +40 -0
- package/docs/component-row.md +33 -0
- package/docs/component-spacer.md +31 -0
- package/docs/controller-and-view.md +108 -0
- package/docs/examples-agents.md +62 -0
- package/docs/examples-column-definitions.md +172 -0
- package/docs/examples-formula-writer.md +262 -0
- package/docs/examples-styling.md +180 -0
- package/docs/examples.md +125 -0
- package/docs/excel-formulas.md +119 -0
- package/docs/export.md +107 -0
- package/docs/fill.md +111 -0
- package/docs/first-table.md +111 -0
- package/docs/formats.md +158 -0
- package/docs/groups-and-totals.md +117 -0
- package/docs/history.md +126 -0
- package/docs/installation.md +52 -0
- package/docs/llms.txt +13 -0
- package/docs/pages-and-server-mode.md +219 -0
- package/docs/range-copy-and-paste.md +108 -0
- package/docs/reference-columns.md +179 -0
- package/docs/reference-controller.md +137 -0
- package/docs/reference-export.md +59 -0
- package/docs/reference-formulas.md +42 -0
- package/docs/reference-implementation.md +29 -0
- package/docs/reference-messages.md +55 -0
- package/docs/reference-view.md +116 -0
- package/docs/row-numbers-and-letters.md +109 -0
- package/docs/rows.md +310 -0
- package/docs/selection-and-keyboard.md +134 -0
- package/docs/sheet-formulas.md +322 -0
- package/docs/sheet-options.md +192 -0
- package/docs/skills.md +41 -0
- package/docs/sort-filter-and-search.md +268 -0
- package/docs/state-and-events.md +86 -0
- package/docs/styled-table.md +229 -0
- package/docs/table-components.md +100 -0
- package/docs/web-components.md +113 -0
- package/package.json +49 -3
- package/src/browser.ts +3 -0
- package/src/index.ts +77 -0
- package/src/reference-browser.ts +3 -0
- package/src/reference-dom.ts +206 -0
- package/src/reference.ts +63 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# Controller and view
|
|
2
|
+
|
|
3
|
+
Change the table from a control beside it, and read what it shows now.
|
|
4
|
+
|
|
5
|
+
A toolbar, a search field, a filter control and the pages of a table live outside the table. They act through a controller and read a view. The same controller is what an agent calls.
|
|
6
|
+
|
|
7
|
+
```ts title="outside.ts"
|
|
8
|
+
import { type ColumnDef } from 'ui-sdk-table-core'
|
|
9
|
+
|
|
10
|
+
import { defineReferenceTable, type ReferenceTableElement } from 'ui-sdk-table-web/reference'
|
|
11
|
+
|
|
12
|
+
type Flight = { id: string; flight: string; to: string; gate: string; delay: number }
|
|
13
|
+
|
|
14
|
+
const flights: Flight[] = [
|
|
15
|
+
{ id: 'f1', flight: 'SK 411', to: 'Oslo', gate: 'A4', delay: 0 },
|
|
16
|
+
{ id: 'f2', flight: 'LH 870', to: 'Frankfurt', gate: 'B12', delay: 25 },
|
|
17
|
+
{ id: 'f3', flight: 'AY 132', to: 'Helsinki', gate: 'A9', delay: 5 },
|
|
18
|
+
{ id: 'f4', flight: 'BA 795', to: 'London', gate: 'C2', delay: 40 },
|
|
19
|
+
{ id: 'f5', flight: 'KL 1170', to: 'Amsterdam', gate: 'B3', delay: 0 },
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
const columns: ColumnDef<Flight>[] = [
|
|
23
|
+
{ id: 'flight', header: 'Flight', sortable: true },
|
|
24
|
+
{ id: 'to', header: 'To', sortable: true },
|
|
25
|
+
{ id: 'gate', header: 'Gate' },
|
|
26
|
+
{ id: 'delay', header: 'Delay, min', sortable: true, format: 'integer' },
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
function button(label: string, action: () => void) {
|
|
30
|
+
const el = document.createElement('button')
|
|
31
|
+
el.type = 'button'
|
|
32
|
+
el.textContent = label
|
|
33
|
+
el.addEventListener('click', action)
|
|
34
|
+
return el
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export default function mount(host: HTMLElement) {
|
|
38
|
+
defineReferenceTable()
|
|
39
|
+
const table = document.createElement('ui-reference-table') as ReferenceTableElement<Flight>
|
|
40
|
+
table.setAttribute('aria-label', 'Departures')
|
|
41
|
+
table.rows = flights
|
|
42
|
+
table.columns = columns
|
|
43
|
+
|
|
44
|
+
host.append(
|
|
45
|
+
button('Most delayed first', () => table.api?.sortBy('delay', 'desc')),
|
|
46
|
+
button('Only delayed', () => table.api?.setColumnFilter('delay', { min: 1 })),
|
|
47
|
+
button('Every flight', () => table.api?.clearFilters()),
|
|
48
|
+
)
|
|
49
|
+
const status = document.createElement('p')
|
|
50
|
+
status.setAttribute('role', 'status')
|
|
51
|
+
host.append(status)
|
|
52
|
+
host.append(table)
|
|
53
|
+
const api = table.api
|
|
54
|
+
if (!api) return
|
|
55
|
+
function update() {
|
|
56
|
+
const view = table.api?.getView()
|
|
57
|
+
if (!view) return
|
|
58
|
+
status.textContent = `${view.dataRowCount} of ${flights.length} flights${view.sort[0] ? `, sorted by ${view.sort[0].id}` : ''}`
|
|
59
|
+
}
|
|
60
|
+
update()
|
|
61
|
+
return api.subscribe(update)
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Press Most delayed first, then Only delayed. The line above the table reads the view: how many flights pass and the sort. Every flight brings the rows back.
|
|
66
|
+
|
|
67
|
+
## The controller
|
|
68
|
+
|
|
69
|
+
Read `table.api` after the element is connected. There’s no separate controller to create or pass to the element.
|
|
70
|
+
|
|
71
|
+
Use these methods to build controls:
|
|
72
|
+
|
|
73
|
+
- **Sort and filter:** `sortBy`, `toggleSort`, `setSearch`, `inputSearch`, `flushSearch`, `setColumnFilter`, `clearFilters`, `clearColumnFilters`.
|
|
74
|
+
- **Groups and pages:** `setGroupBy`, `setPage`, `setPageSize`, `nextPage`, `previousPage`, `firstPage`, `lastPage`.
|
|
75
|
+
- **Rows:** `expand`, `collapse`, `toggleExpanded`, `expandAll`, `collapseAll`, `select`, `toggleSelected`, `selectAll`, `selectRows`, `deselectRows`, `clearSelection`, `pinRow`, `scrollToRow`.
|
|
76
|
+
- **Columns:** `setColumnVisibility`, `toggleColumnVisibility`, `showAllColumns`, `hideAllColumns`, `moveColumn`, `setColumnOrder`, `setColumnWidth`, `pinColumn`, the `reset…` methods, `setColumnAggregate`.
|
|
77
|
+
- **Cells:** `editCell`, `startEdit`, `cancelEdit`, `selectCells`, `copyCells`, `pasteCells` and the methods of the data sheet.
|
|
78
|
+
- **Reads:** `getView`, `getFacets`, `getTotals`, `getSelectionSummary`, `exportData`, `subscribe`.
|
|
79
|
+
|
|
80
|
+
A method that gets a wrong argument throws a message that lists the valid values, such as `No column "age" to sort by. Columns: name, score.`
|
|
81
|
+
|
|
82
|
+
## The view
|
|
83
|
+
|
|
84
|
+
Call `getView()` for the current state. To keep a control in sync, read once when the table is ready, then read again in a `subscribe` listener. Call the returned function when the control is removed.
|
|
85
|
+
|
|
86
|
+
| Field | What it holds |
|
|
87
|
+
| --- | --- |
|
|
88
|
+
| `sort`, `filter`, `groupBy`, `expanded`, `selected` | The state |
|
|
89
|
+
| `search`, `searchPending` | The text of a search field, and whether it waits for a pause |
|
|
90
|
+
| `page`, `pageSize`, `pageCount`, `rowCount`, `pageUnit`, `lastPageKnown` | The pages |
|
|
91
|
+
| `dataRowCount` | How many data rows pass the filters, child rows too |
|
|
92
|
+
| `columns`, `visibleColumns` | Every column that holds cells, and the columns in view in their order |
|
|
93
|
+
| `selectable` | The ids of the rows that `selectAll` selects |
|
|
94
|
+
| `hasExpandable`, `hasExpanded` | Whether something can open, and whether something is open |
|
|
95
|
+
| `loading` | The `loading` prop |
|
|
96
|
+
| `activeCell`, `cellSelection`, `canUndo`, `canRedo` | For a sheet |
|
|
97
|
+
|
|
98
|
+
The listener receives the keys that changed when state, rows or columns change. Use it to refresh other reads too, such as `getFacets(id)` for a filter control.
|
|
99
|
+
|
|
100
|
+
## Names
|
|
101
|
+
|
|
102
|
+
- **Types:** `PageUnit`.
|
|
103
|
+
|
|
104
|
+
For shared methods and result types, see [Controller](./reference-controller.md) and [View](./reference-view.md).
|
|
105
|
+
|
|
106
|
+
For custom elements, properties and DOM events, see the [Web Components API reference](./api-web-components.md).
|
|
107
|
+
|
|
108
|
+
See the [More tiles of the sink](https://ui-sdk-table.fdts.dev/?page=sink&only=more).
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Web Components: Agents
|
|
2
|
+
|
|
3
|
+
[All examples and setup](./examples.md)
|
|
4
|
+
|
|
5
|
+
## agent
|
|
6
|
+
|
|
7
|
+
```ts title="agent.ts"
|
|
8
|
+
import { type ColumnDef } from 'ui-sdk-table-core'
|
|
9
|
+
import { createAgent } from '@ui-sdk/web/agent'
|
|
10
|
+
import { defineReferenceTable, type ReferenceTableElement } from 'ui-sdk-table-web/reference'
|
|
11
|
+
|
|
12
|
+
type Ship = { id: string; name: string; port: string; tons: number }
|
|
13
|
+
|
|
14
|
+
const ships: Ship[] = [
|
|
15
|
+
{ id: 's1', name: 'Northern Star', port: 'Bergen', tons: 3200 },
|
|
16
|
+
{ id: 's2', name: 'Blue Heron', port: 'Rotterdam', tons: 5100 },
|
|
17
|
+
{ id: 's3', name: 'Sea Lark', port: 'Bergen', tons: 1800 },
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
const columns: ColumnDef<Ship>[] = [
|
|
21
|
+
{ id: 'name', header: 'Ship', sortable: true },
|
|
22
|
+
{ id: 'port', header: 'Port', sortable: true },
|
|
23
|
+
{ id: 'tons', header: 'Tons', sortable: true, format: 'integer' },
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
function button(label: string, action: () => void) {
|
|
27
|
+
const el = document.createElement('button')
|
|
28
|
+
el.type = 'button'
|
|
29
|
+
el.textContent = label
|
|
30
|
+
el.addEventListener('click', action)
|
|
31
|
+
return el
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export default function mount(host: HTMLElement) {
|
|
35
|
+
defineReferenceTable()
|
|
36
|
+
const table = document.createElement('ui-reference-table') as ReferenceTableElement<Ship>
|
|
37
|
+
table.setAttribute('aria-label', 'Ships in port')
|
|
38
|
+
table.rows = ships
|
|
39
|
+
table.columns = columns
|
|
40
|
+
|
|
41
|
+
const status = document.createElement('p')
|
|
42
|
+
status.setAttribute('role', 'status')
|
|
43
|
+
host.append(status)
|
|
44
|
+
status.textContent = 'No action yet.'
|
|
45
|
+
host.append(table)
|
|
46
|
+
const agent = createAgent()
|
|
47
|
+
async function sort(id: string) {
|
|
48
|
+
const result = await agent.act({
|
|
49
|
+
steps: [{ ref: 'Ships in port', action: 'sortBy', args: [id, 'desc'] }],
|
|
50
|
+
})
|
|
51
|
+
status.textContent = result.ok ? 'sortBy done.' : `Refused: ${result.error?.message ?? ''}`
|
|
52
|
+
}
|
|
53
|
+
host.prepend(
|
|
54
|
+
button('Heaviest first', () => {
|
|
55
|
+
void sort('tons')
|
|
56
|
+
}),
|
|
57
|
+
button('Sort by weight', () => {
|
|
58
|
+
void sort('weight')
|
|
59
|
+
}),
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
```
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# Web Components: Column definitions
|
|
2
|
+
|
|
3
|
+
[All examples and setup](./examples.md)
|
|
4
|
+
|
|
5
|
+
## formats
|
|
6
|
+
|
|
7
|
+
```ts title="formats.ts"
|
|
8
|
+
import { type ColumnDef } from 'ui-sdk-table-core'
|
|
9
|
+
|
|
10
|
+
import { defineReferenceTable, type ReferenceTableElement } from 'ui-sdk-table-web/reference'
|
|
11
|
+
|
|
12
|
+
type Book = {
|
|
13
|
+
id: string
|
|
14
|
+
title: string
|
|
15
|
+
author: { first: string; last: string }
|
|
16
|
+
price: number
|
|
17
|
+
sold: number
|
|
18
|
+
share: number
|
|
19
|
+
published: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const books: Book[] = [
|
|
23
|
+
{
|
|
24
|
+
id: 'b1',
|
|
25
|
+
title: 'The River Road',
|
|
26
|
+
author: { first: 'Ada', last: 'Lindqvist' },
|
|
27
|
+
price: 18.5,
|
|
28
|
+
sold: 12480,
|
|
29
|
+
share: 0.312,
|
|
30
|
+
published: '2024-03-14',
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
id: 'b2',
|
|
34
|
+
title: 'Small Hours',
|
|
35
|
+
author: { first: 'Tomas', last: 'Reyes' },
|
|
36
|
+
price: 24,
|
|
37
|
+
sold: 8120,
|
|
38
|
+
share: 0.203,
|
|
39
|
+
published: '2023-09-02',
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
id: 'b3',
|
|
43
|
+
title: 'Salt and Iron',
|
|
44
|
+
author: { first: 'Maya', last: 'Okafor' },
|
|
45
|
+
price: 15.75,
|
|
46
|
+
sold: 19400,
|
|
47
|
+
share: 0.485,
|
|
48
|
+
published: '2025-01-20',
|
|
49
|
+
},
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
const columns: ColumnDef<Book>[] = [
|
|
53
|
+
{ id: 'title', header: 'Title', sortable: true },
|
|
54
|
+
// A value that is not one property of the row comes from a function.
|
|
55
|
+
{
|
|
56
|
+
id: 'author',
|
|
57
|
+
header: 'Author',
|
|
58
|
+
sortable: true,
|
|
59
|
+
value: (book) => `${book.author.last}, ${book.author.first}`,
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
id: 'published',
|
|
63
|
+
header: 'Published',
|
|
64
|
+
sortable: true,
|
|
65
|
+
format: 'date',
|
|
66
|
+
formatOptions: { dateStyle: 'medium', timeZone: 'UTC' },
|
|
67
|
+
},
|
|
68
|
+
// A header group: one header above the columns that it holds.
|
|
69
|
+
{
|
|
70
|
+
id: 'sales',
|
|
71
|
+
header: 'Sales',
|
|
72
|
+
columns: [
|
|
73
|
+
{ id: 'sold', header: 'Copies', sortable: true, format: 'integer' },
|
|
74
|
+
{
|
|
75
|
+
id: 'price',
|
|
76
|
+
header: 'Price',
|
|
77
|
+
sortable: true,
|
|
78
|
+
format: 'currency',
|
|
79
|
+
formatOptions: { currency: 'USD', showUnit: true },
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
id: 'share',
|
|
83
|
+
header: 'Share',
|
|
84
|
+
sortable: true,
|
|
85
|
+
format: 'percentage',
|
|
86
|
+
formatOptions: { showUnit: true, maximumFractionDigits: 1 },
|
|
87
|
+
},
|
|
88
|
+
],
|
|
89
|
+
},
|
|
90
|
+
]
|
|
91
|
+
|
|
92
|
+
export default function mount(host: HTMLElement) {
|
|
93
|
+
defineReferenceTable()
|
|
94
|
+
const table = document.createElement('ui-reference-table') as ReferenceTableElement<Book>
|
|
95
|
+
table.setAttribute('aria-label', 'Books and their sales')
|
|
96
|
+
table.rows = books
|
|
97
|
+
table.columns = columns
|
|
98
|
+
table.locale = 'en-US'
|
|
99
|
+
|
|
100
|
+
host.append(table)
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## layout
|
|
105
|
+
|
|
106
|
+
```ts title="layout.ts"
|
|
107
|
+
import { type ColumnDef } from 'ui-sdk-table-core'
|
|
108
|
+
|
|
109
|
+
import { defineReferenceTable, type ReferenceTableElement } from 'ui-sdk-table-web/reference'
|
|
110
|
+
|
|
111
|
+
type City = { id: string; name: string; country: string; people: number; area: number }
|
|
112
|
+
|
|
113
|
+
const cities: City[] = [
|
|
114
|
+
{ id: 'c1', name: 'Lisbon', country: 'Portugal', people: 545000, area: 100 },
|
|
115
|
+
{ id: 'c2', name: 'Vienna', country: 'Austria', people: 1982000, area: 415 },
|
|
116
|
+
{ id: 'c3', name: 'Oslo', country: 'Norway', people: 709000, area: 454 },
|
|
117
|
+
]
|
|
118
|
+
|
|
119
|
+
const columns: ColumnDef<City>[] = [
|
|
120
|
+
// A city has no name to hide: the column stays.
|
|
121
|
+
{ id: 'name', header: 'City', width: 140, hideable: false },
|
|
122
|
+
{ id: 'country', header: 'Country', width: 140 },
|
|
123
|
+
{ id: 'people', header: 'People', width: 130, format: 'integer' },
|
|
124
|
+
{ id: 'area', header: 'Area, km²', width: 130, format: 'integer' },
|
|
125
|
+
]
|
|
126
|
+
|
|
127
|
+
function button(label: string, action: () => void) {
|
|
128
|
+
const el = document.createElement('button')
|
|
129
|
+
el.type = 'button'
|
|
130
|
+
el.textContent = label
|
|
131
|
+
el.addEventListener('click', action)
|
|
132
|
+
return el
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export default function mount(host: HTMLElement) {
|
|
136
|
+
defineReferenceTable()
|
|
137
|
+
const table = document.createElement('ui-reference-table') as ReferenceTableElement<City>
|
|
138
|
+
table.setAttribute('aria-label', 'Cities')
|
|
139
|
+
table.rows = cities
|
|
140
|
+
table.columns = columns
|
|
141
|
+
table.resizable = true
|
|
142
|
+
table.reorderable = true
|
|
143
|
+
table.columnVisibility = {}
|
|
144
|
+
table.columnPinning = {}
|
|
145
|
+
table.addEventListener('columnvisibilitychange', ({ detail }) => {
|
|
146
|
+
table.columnVisibility = detail.value
|
|
147
|
+
})
|
|
148
|
+
table.addEventListener('columnpinningchange', ({ detail }) => {
|
|
149
|
+
table.columnPinning = detail.value
|
|
150
|
+
})
|
|
151
|
+
const country = button('Hide the country', () => table.api?.toggleColumnVisibility('country'))
|
|
152
|
+
const city = button('Pin the city', () =>
|
|
153
|
+
table.api?.pinColumn('name', table.columnPinning?.start?.includes('name') ? null : 'start'),
|
|
154
|
+
)
|
|
155
|
+
host.append(
|
|
156
|
+
country,
|
|
157
|
+
city,
|
|
158
|
+
button('Reset the widths', () => table.api?.resetColumnWidths()),
|
|
159
|
+
)
|
|
160
|
+
host.append(table)
|
|
161
|
+
const api = table.api
|
|
162
|
+
if (!api) return
|
|
163
|
+
function update() {
|
|
164
|
+
const view = table.api?.getView()
|
|
165
|
+
if (!view) return
|
|
166
|
+
country.textContent = `${table.columnVisibility?.country === false ? 'Show' : 'Hide'} the country`
|
|
167
|
+
city.textContent = `${table.columnPinning?.start?.includes('name') ? 'Free' : 'Pin'} the city`
|
|
168
|
+
}
|
|
169
|
+
update()
|
|
170
|
+
return api.subscribe(update)
|
|
171
|
+
}
|
|
172
|
+
```
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
# Web Components: A formula writer
|
|
2
|
+
|
|
3
|
+
[All examples and setup](./examples.md)
|
|
4
|
+
|
|
5
|
+
## writer
|
|
6
|
+
|
|
7
|
+
```ts title="writer.ts"
|
|
8
|
+
import {
|
|
9
|
+
type ColumnDef,
|
|
10
|
+
type FormulaView,
|
|
11
|
+
applyEdits,
|
|
12
|
+
rowNumberColumn,
|
|
13
|
+
type CellFormulas,
|
|
14
|
+
} from 'ui-sdk-table-core'
|
|
15
|
+
import {
|
|
16
|
+
createFormulaEngine,
|
|
17
|
+
FORMULA_FUNCTIONS,
|
|
18
|
+
formulaHelp,
|
|
19
|
+
insertReference,
|
|
20
|
+
} from 'ui-sdk-table-core/formulas'
|
|
21
|
+
import { defineReferenceTable, type ReferenceTableElement } from 'ui-sdk-table-web/reference'
|
|
22
|
+
|
|
23
|
+
type Line = { id: string; item: string; price: number | null; qty: number | null; total?: unknown }
|
|
24
|
+
|
|
25
|
+
const lines: Line[] = [
|
|
26
|
+
{ id: 'l1', item: 'Desk lamp', price: 39, qty: 4, total: 156 },
|
|
27
|
+
{ id: 'l2', item: 'Monitor arm', price: 89, qty: 2, total: 178 },
|
|
28
|
+
{ id: 'l3', item: 'Keyboard', price: 59, qty: 3, total: 177 },
|
|
29
|
+
{ id: 'l4', item: 'Order total', price: null, qty: null, total: null },
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
const columns: ColumnDef<Line>[] = [
|
|
33
|
+
rowNumberColumn<Line>(),
|
|
34
|
+
{ id: 'item', header: 'Item', allowFormula: false },
|
|
35
|
+
{ id: 'price', header: 'Price', format: 'decimal' },
|
|
36
|
+
{ id: 'qty', header: 'Qty', format: 'integer' },
|
|
37
|
+
{ id: 'total', header: 'Total', format: 'decimal' },
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
const formulas: CellFormulas = {
|
|
41
|
+
cells: {
|
|
42
|
+
l1: { total: '=[price]*[qty]' },
|
|
43
|
+
l2: { total: '=[price]*[qty]' },
|
|
44
|
+
l3: { total: '=[price]*[qty]' },
|
|
45
|
+
},
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function button(label: string, action: () => void) {
|
|
49
|
+
const el = document.createElement('button')
|
|
50
|
+
el.type = 'button'
|
|
51
|
+
el.textContent = label
|
|
52
|
+
el.addEventListener('click', action)
|
|
53
|
+
return el
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export default function mount(host: HTMLElement) {
|
|
57
|
+
defineReferenceTable()
|
|
58
|
+
const table = document.createElement('ui-reference-table') as ReferenceTableElement<Line>
|
|
59
|
+
table.setAttribute('aria-label', 'An order with a formula writer')
|
|
60
|
+
table.rows = lines
|
|
61
|
+
table.columns = columns
|
|
62
|
+
table.locale = 'en-US'
|
|
63
|
+
table.sheet = true
|
|
64
|
+
table.columnLetters = true
|
|
65
|
+
table.addEventListener('celledit', ({ detail }) => {
|
|
66
|
+
table.rows = applyEdits(table.rows ?? [], detail.value)
|
|
67
|
+
})
|
|
68
|
+
table.formulas = createFormulaEngine()
|
|
69
|
+
table.cellFormulas = formulas
|
|
70
|
+
table.addEventListener('cellformulaschange', ({ detail }) => {
|
|
71
|
+
table.cellFormulas = detail.value
|
|
72
|
+
})
|
|
73
|
+
const form = document.createElement('form')
|
|
74
|
+
const address = document.createElement('output')
|
|
75
|
+
address.dataset.testid = 'address'
|
|
76
|
+
const field = document.createElement('input')
|
|
77
|
+
field.setAttribute('aria-label', 'Formula of the active cell')
|
|
78
|
+
field.placeholder = 'Press a cell, then type ='
|
|
79
|
+
field.autocomplete = 'off'
|
|
80
|
+
field.spellcheck = false
|
|
81
|
+
const status = document.createElement('p')
|
|
82
|
+
status.setAttribute('role', 'status')
|
|
83
|
+
form.append(address, field, status)
|
|
84
|
+
host.append(form)
|
|
85
|
+
let draft: FormulaView | null = null
|
|
86
|
+
let editing = false
|
|
87
|
+
const focusCell = () => table.querySelector<HTMLElement>('[data-active]')?.focus()
|
|
88
|
+
function endPicking() {
|
|
89
|
+
editing = false
|
|
90
|
+
if (picked) status.textContent = ''
|
|
91
|
+
picked = null
|
|
92
|
+
if (table.pickCells) table.pickCells = false
|
|
93
|
+
}
|
|
94
|
+
function cancel() {
|
|
95
|
+
draft = null
|
|
96
|
+
endPicking()
|
|
97
|
+
status.textContent = ''
|
|
98
|
+
field.removeAttribute('aria-invalid')
|
|
99
|
+
}
|
|
100
|
+
function read() {
|
|
101
|
+
const cell = table.api?.getView().formula
|
|
102
|
+
if (draft && (draft.rowId !== cell?.rowId || draft.columnId !== cell?.columnId)) cancel()
|
|
103
|
+
address.textContent = cell?.address || '—'
|
|
104
|
+
field.disabled = !cell
|
|
105
|
+
if (!draft && field.value !== (cell?.text ?? '')) field.value = cell?.text ?? ''
|
|
106
|
+
help()
|
|
107
|
+
}
|
|
108
|
+
field.addEventListener('input', () => {
|
|
109
|
+
editing = field.matches(':focus')
|
|
110
|
+
draft ??= table.api?.getView().formula ?? null
|
|
111
|
+
field.removeAttribute('aria-invalid')
|
|
112
|
+
status.textContent = ''
|
|
113
|
+
picked = null
|
|
114
|
+
help()
|
|
115
|
+
})
|
|
116
|
+
field.addEventListener('keydown', (event) => {
|
|
117
|
+
if (event.key !== 'Escape') return
|
|
118
|
+
event.preventDefault()
|
|
119
|
+
cancel()
|
|
120
|
+
read()
|
|
121
|
+
focusCell()
|
|
122
|
+
})
|
|
123
|
+
form.addEventListener('submit', (event) => {
|
|
124
|
+
event.preventDefault()
|
|
125
|
+
void commit()
|
|
126
|
+
})
|
|
127
|
+
async function commit() {
|
|
128
|
+
endPicking()
|
|
129
|
+
const cell = draft
|
|
130
|
+
try {
|
|
131
|
+
if (cell) await table.api?.editCell(cell.rowId, cell.columnId, field.value)
|
|
132
|
+
cancel()
|
|
133
|
+
read()
|
|
134
|
+
focusCell()
|
|
135
|
+
} catch (error) {
|
|
136
|
+
field.setAttribute('aria-invalid', 'true')
|
|
137
|
+
status.textContent =
|
|
138
|
+
error instanceof Error
|
|
139
|
+
? typeof error.cause === 'string'
|
|
140
|
+
? error.cause
|
|
141
|
+
: error.message
|
|
142
|
+
: String(error)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const hint = document.createElement('p')
|
|
146
|
+
hint.dataset.testid = 'hint'
|
|
147
|
+
const suggestions = document.createElement('ul')
|
|
148
|
+
suggestions.setAttribute('aria-label', 'Functions')
|
|
149
|
+
form.insertBefore(hint, status)
|
|
150
|
+
form.insertBefore(suggestions, status)
|
|
151
|
+
let picked: { start: number; end: number } | null = null
|
|
152
|
+
let marksKey = ''
|
|
153
|
+
let markedRows: readonly Line[] | undefined
|
|
154
|
+
function help() {
|
|
155
|
+
const api = table.api
|
|
156
|
+
if (!api) return
|
|
157
|
+
const value = field.value
|
|
158
|
+
const caret = field.selectionEnd ?? 0
|
|
159
|
+
const help = formulaHelp(value, caret, { locale: 'en-US' })
|
|
160
|
+
const picking =
|
|
161
|
+
editing &&
|
|
162
|
+
!!draft &&
|
|
163
|
+
value.startsWith('=') &&
|
|
164
|
+
(field.selectionStart ?? 0) > 0 &&
|
|
165
|
+
(help.reference || !!picked)
|
|
166
|
+
if (table.pickCells !== picking) table.pickCells = picking
|
|
167
|
+
hint.replaceChildren()
|
|
168
|
+
const call = help.call
|
|
169
|
+
if (call?.info) {
|
|
170
|
+
hint.append(`${call.name}(`)
|
|
171
|
+
call.info.arguments.forEach((arg, i) => {
|
|
172
|
+
if (i) hint.append(', ')
|
|
173
|
+
if (i !== call.argument) hint.append(arg.name)
|
|
174
|
+
else {
|
|
175
|
+
const current = document.createElement('strong')
|
|
176
|
+
current.textContent = arg.name
|
|
177
|
+
hint.append(current)
|
|
178
|
+
}
|
|
179
|
+
})
|
|
180
|
+
hint.append(')')
|
|
181
|
+
}
|
|
182
|
+
suggestions.replaceChildren()
|
|
183
|
+
const token = help.token
|
|
184
|
+
if (draft && token && (token.kind === 'name' || token.kind === 'function')) {
|
|
185
|
+
for (const info of FORMULA_FUNCTIONS.filter((info) =>
|
|
186
|
+
info.name.startsWith(token.text.toUpperCase()),
|
|
187
|
+
).slice(0, 6)) {
|
|
188
|
+
const item = document.createElement('li')
|
|
189
|
+
const choose = button(`${info.name} ${info.help}`, () => {
|
|
190
|
+
const next = insertReference(field.value, token, `${info.name}(`)
|
|
191
|
+
field.value = next.text
|
|
192
|
+
picked = null
|
|
193
|
+
field.focus()
|
|
194
|
+
editing = field.matches(':focus')
|
|
195
|
+
field.setSelectionRange(next.end, next.end)
|
|
196
|
+
helpAtCaret()
|
|
197
|
+
})
|
|
198
|
+
choose.addEventListener('pointerdown', (event) => event.preventDefault())
|
|
199
|
+
item.append(choose)
|
|
200
|
+
suggestions.append(item)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
const cell = draft ?? api.getView().formula
|
|
204
|
+
const key = JSON.stringify([cell?.rowId, cell?.columnId, value])
|
|
205
|
+
if (key !== marksKey || markedRows !== table.rows) {
|
|
206
|
+
marksKey = key
|
|
207
|
+
markedRows = table.rows
|
|
208
|
+
const refs = cell && value.startsWith('=') ? api.getFormulaReferences(value, cell) : []
|
|
209
|
+
const mark = api.getReferenceMarks(refs)
|
|
210
|
+
table.cellAttrs = refs.length
|
|
211
|
+
? (ctx) => {
|
|
212
|
+
const hit = ctx.kind === 'cell' && mark(ctx.node.id, ctx.column.id)
|
|
213
|
+
return hit
|
|
214
|
+
? {
|
|
215
|
+
'data-reference': String(hit.index % 4),
|
|
216
|
+
'data-reference-edge': hit.edges || undefined,
|
|
217
|
+
}
|
|
218
|
+
: undefined
|
|
219
|
+
}
|
|
220
|
+
: undefined
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
function helpAtCaret() {
|
|
224
|
+
if (field.selectionStart !== field.selectionEnd || picked?.end !== field.selectionEnd)
|
|
225
|
+
picked = null
|
|
226
|
+
help()
|
|
227
|
+
}
|
|
228
|
+
for (const event of ['focus', 'select', 'click', 'keyup'])
|
|
229
|
+
field.addEventListener(event, helpAtCaret)
|
|
230
|
+
field.addEventListener('blur', endPicking)
|
|
231
|
+
table.addEventListener('pickedcellschange', ({ detail }) => {
|
|
232
|
+
if (!detail.value || !table.pickCells) return
|
|
233
|
+
const ref = table.api?.getRangeAddress(detail.value)
|
|
234
|
+
if (!ref) return
|
|
235
|
+
const next = insertReference(
|
|
236
|
+
field.value,
|
|
237
|
+
picked ?? { start: field.selectionStart ?? 0, end: field.selectionEnd ?? 0 },
|
|
238
|
+
ref,
|
|
239
|
+
)
|
|
240
|
+
field.value = next.text
|
|
241
|
+
picked = { start: next.start, end: next.end }
|
|
242
|
+
status.textContent = `Picked ${ref}`
|
|
243
|
+
field.setSelectionRange(next.end, next.end)
|
|
244
|
+
help()
|
|
245
|
+
})
|
|
246
|
+
field.addEventListener('keydown', (event) => {
|
|
247
|
+
if (!table.pickCells || event.altKey) return
|
|
248
|
+
const arrows = ['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'] as const
|
|
249
|
+
const key = arrows.find((key) => key === event.key)
|
|
250
|
+
if (!key) return
|
|
251
|
+
event.preventDefault()
|
|
252
|
+
table.api?.movePick(key, {
|
|
253
|
+
extend: event.shiftKey,
|
|
254
|
+
edge: event.ctrlKey || event.metaKey,
|
|
255
|
+
start: picked === null,
|
|
256
|
+
})
|
|
257
|
+
})
|
|
258
|
+
host.append(table)
|
|
259
|
+
read()
|
|
260
|
+
return table.api?.subscribe(read)
|
|
261
|
+
}
|
|
262
|
+
```
|