ui-sdk-table-web 0.2.7 → 0.2.10

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.
@@ -1,206 +1,206 @@
1
- import { applyAttrs, attrs, bind, commit, partState, subscribe } from '@ui-sdk/core/renderer'
2
- import { tableElements } from 'ui-sdk-table-core/elements'
3
- import type { table } from 'ui-sdk-table-core'
4
- import {
5
- renderReference,
6
- referenceStructureKeys,
7
- type PartAttrs,
8
- type ReferenceContent,
9
- type ReferenceFactory,
10
- type ReferenceKind,
11
- type ReferenceOptions,
12
- type ReferenceState,
13
- } from 'ui-sdk-table-core/reference'
14
-
15
- type Instance = ReturnType<typeof table>
16
- type Item = { kind: ReferenceKind; props: PartAttrs; children?: (state: ReferenceState) => Content }
17
- type Content = ReferenceContent<Item | Node>
18
- export type WebContent = Element | Text | string | null | undefined
19
- const factory: ReferenceFactory<Item | WebContent> = (kind, props, children) => ({
20
- kind,
21
- props,
22
- children,
23
- })
24
- const tags = {
25
- ...tableElements,
26
- 'header-cell': 'th',
27
- button: 'button',
28
- colgroup: 'colgroup',
29
- } as const
30
- const partProps = new Set(['value', 'row', 'col', 'index', 'size', 'header', 'colspan', 'rowspan'])
31
-
32
- type Entry = {
33
- node: Node
34
- update(item: Item | Node | string): void
35
- destroy(): void
36
- }
37
-
38
- function write(el: HTMLElement, name: string, value: unknown, old: unknown) {
39
- if (name === 'key' || (partProps.has(name) && name !== 'colspan' && name !== 'rowspan')) return
40
- if (/^on/i.test(name)) {
41
- const event = name.slice(2).toLowerCase()
42
- if (typeof old === 'function') el.removeEventListener(event, old as EventListener)
43
- if (typeof value === 'function') el.addEventListener(event, value as EventListener)
44
- } else if (value === true || typeof value === 'number' || typeof value === 'string') {
45
- el.setAttribute(name, value === true ? '' : String(value))
46
- } else {
47
- el.removeAttribute(name)
48
- }
49
- }
50
-
51
- function childrenOf(parent: HTMLElement, instance: Instance) {
52
- let entries = new Map<string, Entry>()
53
- return {
54
- update(content: Content) {
55
- const next = new Map<string, Entry>()
56
- let at = parent.firstChild
57
- function visit(item: Content) {
58
- if (Array.isArray(item)) {
59
- item.forEach(visit)
60
- return
61
- }
62
- if (item == null) return
63
- const descriptor = typeof item === 'object' && !(item instanceof Node) ? item : undefined
64
- const key = JSON.stringify([
65
- descriptor?.kind ?? typeof item,
66
- descriptor?.props.key ?? descriptor?.props.value ?? next.size,
67
- ])
68
- let entry = entries.get(key)
69
- if (entry && item instanceof Node && entry.node !== item) {
70
- if (at === entry.node) at = entry.node.nextSibling
71
- entry.destroy()
72
- entry = undefined
73
- }
74
- entry ??= descriptor
75
- ? elementEntry(instance, descriptor, parent.ownerDocument)
76
- : {
77
- node: item instanceof Node ? item : parent.ownerDocument.createTextNode(''),
78
- update(value) {
79
- if (typeof value === 'string' && this.node.nodeValue !== value)
80
- this.node.nodeValue = value
81
- },
82
- destroy() {
83
- this.node.parentNode?.removeChild(this.node)
84
- },
85
- }
86
- next.set(key, entry)
87
- if (entry.node !== at) {
88
- if (entry.node.parentNode === parent && typeof parent.moveBefore === 'function')
89
- parent.moveBefore(entry.node, at)
90
- else if (
91
- at &&
92
- entry.node.parentNode === parent &&
93
- entry.node.contains(parent.ownerDocument.activeElement)
94
- ) {
95
- // Older browsers blur a focused subtree on insertBefore. Move its siblings instead.
96
- const after = entry.node.nextSibling
97
- while (at && at !== entry.node) {
98
- const sibling: ChildNode | null = at.nextSibling
99
- parent.insertBefore(at, after)
100
- at = sibling
101
- }
102
- } else parent.insertBefore(entry.node, at)
103
- }
104
- entry.update(item)
105
- at = entry.node.nextSibling
106
- }
107
- visit(content)
108
- for (const [key, entry] of entries) if (!next.has(key)) entry.destroy()
109
- entries = next
110
- },
111
- destroy() {
112
- for (const entry of entries.values()) entry.destroy()
113
- entries.clear()
114
- },
115
- }
116
- }
117
-
118
- function elementEntry(instance: Instance, initial: Item, doc: Document): Entry {
119
- const el = doc.createElement(tags[initial.kind])
120
- const children = childrenOf(el, instance)
121
- let current = initial
122
- const slot = initial.kind === 'header-cell' ? 'cell' : initial.kind
123
- const part =
124
- slot === 'button' || slot === 'colgroup' ? undefined : instance.part(slot, initial.props)
125
- let unbind: (() => void) | undefined
126
- let unsubscribe: (() => void) | undefined
127
- let previous: PartAttrs = {}
128
- const render = () => {
129
- if (part) applyAttrs(el, attrs(part))
130
- children.update(current.children?.(part ? (partState(part) as ReferenceState) : {}))
131
- }
132
- let queued = false
133
- let disposed = false
134
- return {
135
- node: el,
136
- update(next) {
137
- if (typeof next === 'string' || next instanceof Node) return
138
- current = next
139
- for (const name of Object.keys(previous))
140
- if (!(name in next.props)) write(el, name, undefined, previous[name])
141
- for (const [name, value] of Object.entries(next.props))
142
- if (value !== previous[name]) write(el, name, value, previous[name])
143
- if (part) {
144
- if (!unbind) {
145
- unbind = bind(part, el)
146
- unsubscribe = subscribe(part, () => {
147
- if (queued) return
148
- queued = true
149
- queueMicrotask(() => {
150
- if (!disposed && queued) render()
151
- queued = false
152
- })
153
- })
154
- } else if ([...partProps].some((key) => previous[key] !== next.props[key]))
155
- commit(part, next.props)
156
- }
157
- previous = next.props
158
- queued = false
159
- render()
160
- },
161
- destroy() {
162
- disposed = true
163
- unsubscribe?.()
164
- children.destroy()
165
- unbind?.()
166
- el.remove()
167
- },
168
- }
169
- }
170
-
171
- export function mountReference(
172
- el: HTMLElement,
173
- instance: Instance,
174
- options: () => Omit<ReferenceOptions<WebContent>, 'shown'>,
175
- ) {
176
- const tree = childrenOf(el, instance)
177
- const read = () => referenceStructureKeys.map((key) => instance.get()[key])
178
- let seen = read()
179
- let live = true
180
- let pending = false
181
- const render = () => {
182
- pending = false
183
- tree.update(renderReference<Item | WebContent>(instance.api, factory, options()))
184
- }
185
- const update = () => {
186
- if (pending) return
187
- pending = true
188
- queueMicrotask(() => {
189
- if (live && pending) render()
190
- })
191
- }
192
- const stop = instance.subscribe(() => {
193
- const next = read()
194
- if (next.some((value, i) => value !== seen[i])) update()
195
- seen = next
196
- })
197
- update()
198
- return {
199
- update,
200
- destroy() {
201
- live = false
202
- stop()
203
- tree.destroy()
204
- },
205
- }
206
- }
1
+ import { applyAttrs, attrs, bind, commit, partState, subscribe } from '@ui-sdk/core/renderer'
2
+ import { tableElements } from 'ui-sdk-table-core/elements'
3
+ import type { table } from 'ui-sdk-table-core'
4
+ import {
5
+ renderReference,
6
+ referenceStructureKeys,
7
+ type PartAttrs,
8
+ type ReferenceContent,
9
+ type ReferenceFactory,
10
+ type ReferenceKind,
11
+ type ReferenceOptions,
12
+ type ReferenceState,
13
+ } from 'ui-sdk-table-core/reference'
14
+
15
+ type Instance = ReturnType<typeof table>
16
+ type Item = { kind: ReferenceKind; props: PartAttrs; children?: (state: ReferenceState) => Content }
17
+ type Content = ReferenceContent<Item | Node>
18
+ export type WebContent = Element | Text | string | null | undefined
19
+ const factory: ReferenceFactory<Item | WebContent> = (kind, props, children) => ({
20
+ kind,
21
+ props,
22
+ children,
23
+ })
24
+ const tags = {
25
+ ...tableElements,
26
+ 'header-cell': 'th',
27
+ button: 'button',
28
+ colgroup: 'colgroup',
29
+ } as const
30
+ const partProps = new Set(['value', 'row', 'col', 'index', 'size', 'header', 'colspan', 'rowspan'])
31
+
32
+ type Entry = {
33
+ node: Node
34
+ update(item: Item | Node | string): void
35
+ destroy(): void
36
+ }
37
+
38
+ function write(el: HTMLElement, name: string, value: unknown, old: unknown) {
39
+ if (name === 'key' || (partProps.has(name) && name !== 'colspan' && name !== 'rowspan')) return
40
+ if (/^on/i.test(name)) {
41
+ const event = name.slice(2).toLowerCase()
42
+ if (typeof old === 'function') el.removeEventListener(event, old as EventListener)
43
+ if (typeof value === 'function') el.addEventListener(event, value as EventListener)
44
+ } else if (value === true || typeof value === 'number' || typeof value === 'string') {
45
+ el.setAttribute(name, value === true ? '' : String(value))
46
+ } else {
47
+ el.removeAttribute(name)
48
+ }
49
+ }
50
+
51
+ function childrenOf(parent: HTMLElement, instance: Instance) {
52
+ let entries = new Map<string, Entry>()
53
+ return {
54
+ update(content: Content) {
55
+ const next = new Map<string, Entry>()
56
+ let at = parent.firstChild
57
+ function visit(item: Content) {
58
+ if (Array.isArray(item)) {
59
+ item.forEach(visit)
60
+ return
61
+ }
62
+ if (item == null) return
63
+ const descriptor = typeof item === 'object' && !(item instanceof Node) ? item : undefined
64
+ const key = JSON.stringify([
65
+ descriptor?.kind ?? typeof item,
66
+ descriptor?.props.key ?? descriptor?.props.value ?? next.size,
67
+ ])
68
+ let entry = entries.get(key)
69
+ if (entry && item instanceof Node && entry.node !== item) {
70
+ if (at === entry.node) at = entry.node.nextSibling
71
+ entry.destroy()
72
+ entry = undefined
73
+ }
74
+ entry ??= descriptor
75
+ ? elementEntry(instance, descriptor, parent.ownerDocument)
76
+ : {
77
+ node: item instanceof Node ? item : parent.ownerDocument.createTextNode(''),
78
+ update(value) {
79
+ if (typeof value === 'string' && this.node.nodeValue !== value)
80
+ this.node.nodeValue = value
81
+ },
82
+ destroy() {
83
+ this.node.parentNode?.removeChild(this.node)
84
+ },
85
+ }
86
+ next.set(key, entry)
87
+ if (entry.node !== at) {
88
+ if (entry.node.parentNode === parent && typeof parent.moveBefore === 'function')
89
+ parent.moveBefore(entry.node, at)
90
+ else if (
91
+ at &&
92
+ entry.node.parentNode === parent &&
93
+ entry.node.contains(parent.ownerDocument.activeElement)
94
+ ) {
95
+ // Older browsers blur a focused subtree on insertBefore. Move its siblings instead.
96
+ const after = entry.node.nextSibling
97
+ while (at && at !== entry.node) {
98
+ const sibling: ChildNode | null = at.nextSibling
99
+ parent.insertBefore(at, after)
100
+ at = sibling
101
+ }
102
+ } else parent.insertBefore(entry.node, at)
103
+ }
104
+ entry.update(item)
105
+ at = entry.node.nextSibling
106
+ }
107
+ visit(content)
108
+ for (const [key, entry] of entries) if (!next.has(key)) entry.destroy()
109
+ entries = next
110
+ },
111
+ destroy() {
112
+ for (const entry of entries.values()) entry.destroy()
113
+ entries.clear()
114
+ },
115
+ }
116
+ }
117
+
118
+ function elementEntry(instance: Instance, initial: Item, doc: Document): Entry {
119
+ const el = doc.createElement(tags[initial.kind])
120
+ const children = childrenOf(el, instance)
121
+ let current = initial
122
+ const slot = initial.kind === 'header-cell' ? 'cell' : initial.kind
123
+ const part =
124
+ slot === 'button' || slot === 'colgroup' ? undefined : instance.part(slot, initial.props)
125
+ let unbind: (() => void) | undefined
126
+ let unsubscribe: (() => void) | undefined
127
+ let previous: PartAttrs = {}
128
+ const render = () => {
129
+ if (part) applyAttrs(el, attrs(part))
130
+ children.update(current.children?.(part ? (partState(part) as ReferenceState) : {}))
131
+ }
132
+ let queued = false
133
+ let disposed = false
134
+ return {
135
+ node: el,
136
+ update(next) {
137
+ if (typeof next === 'string' || next instanceof Node) return
138
+ current = next
139
+ for (const name of Object.keys(previous))
140
+ if (!(name in next.props)) write(el, name, undefined, previous[name])
141
+ for (const [name, value] of Object.entries(next.props))
142
+ if (value !== previous[name]) write(el, name, value, previous[name])
143
+ if (part) {
144
+ if (!unbind) {
145
+ unbind = bind(part, el)
146
+ unsubscribe = subscribe(part, () => {
147
+ if (queued) return
148
+ queued = true
149
+ queueMicrotask(() => {
150
+ if (!disposed && queued) render()
151
+ queued = false
152
+ })
153
+ })
154
+ } else if ([...partProps].some((key) => previous[key] !== next.props[key]))
155
+ commit(part, next.props)
156
+ }
157
+ previous = next.props
158
+ queued = false
159
+ render()
160
+ },
161
+ destroy() {
162
+ disposed = true
163
+ unsubscribe?.()
164
+ children.destroy()
165
+ unbind?.()
166
+ el.remove()
167
+ },
168
+ }
169
+ }
170
+
171
+ export function mountReference(
172
+ el: HTMLElement,
173
+ instance: Instance,
174
+ options: () => Omit<ReferenceOptions<WebContent>, 'shown'>,
175
+ ) {
176
+ const tree = childrenOf(el, instance)
177
+ const read = () => referenceStructureKeys.map((key) => instance.get()[key])
178
+ let seen = read()
179
+ let live = true
180
+ let pending = false
181
+ const render = () => {
182
+ pending = false
183
+ tree.update(renderReference<Item | WebContent>(instance.api, factory, options()))
184
+ }
185
+ const update = () => {
186
+ if (pending) return
187
+ pending = true
188
+ queueMicrotask(() => {
189
+ if (live && pending) render()
190
+ })
191
+ }
192
+ const stop = instance.subscribe(() => {
193
+ const next = read()
194
+ if (next.some((value, i) => value !== seen[i])) update()
195
+ seen = next
196
+ })
197
+ update()
198
+ return {
199
+ update,
200
+ destroy() {
201
+ live = false
202
+ stop()
203
+ tree.destroy()
204
+ },
205
+ }
206
+ }
package/src/reference.ts CHANGED
@@ -1,63 +1,63 @@
1
- import { createParts } from '@ui-sdk/web'
2
- import { table, type TableController } from 'ui-sdk-table-core'
3
- import type { ReferenceSlots, TableAttrsProps } from 'ui-sdk-table-core/reference'
4
- import type { TableElement, TableElementApi } from './index'
5
- import { mountReference, type WebContent } from './reference-dom'
6
-
7
- export type ReferenceTableElement<Row = unknown> = Omit<TableElement<Row>, 'api'> &
8
- TableAttrsProps<Row> & {
9
- readonly api?: TableController & TableElementApi
10
- renderers?: ReferenceSlots<Row, WebContent>
11
- }
12
-
13
- declare global {
14
- interface HTMLElementTagNameMap {
15
- 'ui-reference-table': ReferenceTableElement
16
- }
17
- }
18
-
19
- type View = ReturnType<typeof mountReference>
20
- const views = new WeakMap<
21
- HTMLElement,
22
- { instance: ReturnType<typeof table>; view: View; generation: number }
23
- >()
24
-
25
- export function defineReferenceTable() {
26
- return createParts(table, {
27
- root: {
28
- tag: 'ui-reference-table',
29
- display: 'table',
30
- kinds: { controller: null },
31
- defaults: { groupTotals: true, sortChildren: true },
32
- properties: ['renderers', 'rowAttrs', 'cellAttrs', 'sectionAttrs'],
33
- render(host, instance) {
34
- const el = host as ReferenceTableElement
35
- let record = views.get(el)
36
- if (record?.instance !== instance) {
37
- record?.view.destroy()
38
- const view = mountReference(el, instance, () => ({
39
- slots: el.renderers,
40
- rowAttrs: el.rowAttrs,
41
- cellAttrs: el.cellAttrs,
42
- sectionAttrs: el.sectionAttrs,
43
- }))
44
- record = { instance, view, generation: 0 }
45
- views.set(el, record)
46
- Object.defineProperty(el, 'api', {
47
- configurable: true,
48
- get: () => (el.isConnected ? views.get(el)?.instance.api : undefined),
49
- })
50
- } else record.view.update()
51
- const generation = ++record.generation
52
- return () => {
53
- // The SDK runs cleanup both before the next property update and on disconnect.
54
- queueMicrotask(() => {
55
- if (views.get(el) !== record || record.generation !== generation) return
56
- record.view.destroy()
57
- views.delete(el)
58
- })
59
- }
60
- },
61
- },
62
- })
63
- }
1
+ import { createParts } from '@ui-sdk/web'
2
+ import { table, type TableController } from 'ui-sdk-table-core'
3
+ import type { ReferenceSlots, TableAttrsProps } from 'ui-sdk-table-core/reference'
4
+ import type { TableElement, TableElementApi } from './index'
5
+ import { mountReference, type WebContent } from './reference-dom'
6
+
7
+ export type ReferenceTableElement<Row = unknown> = Omit<TableElement<Row>, 'api'> &
8
+ TableAttrsProps<Row> & {
9
+ readonly api?: TableController & TableElementApi
10
+ renderers?: ReferenceSlots<Row, WebContent>
11
+ }
12
+
13
+ declare global {
14
+ interface HTMLElementTagNameMap {
15
+ 'ui-reference-table': ReferenceTableElement
16
+ }
17
+ }
18
+
19
+ type View = ReturnType<typeof mountReference>
20
+ const views = new WeakMap<
21
+ HTMLElement,
22
+ { instance: ReturnType<typeof table>; view: View; generation: number }
23
+ >()
24
+
25
+ export function defineReferenceTable() {
26
+ return createParts(table, {
27
+ root: {
28
+ tag: 'ui-reference-table',
29
+ display: 'table',
30
+ kinds: { controller: null },
31
+ defaults: { groupTotals: true, sortChildren: true },
32
+ properties: ['renderers', 'rowAttrs', 'cellAttrs', 'sectionAttrs'],
33
+ render(host, instance) {
34
+ const el = host as ReferenceTableElement
35
+ let record = views.get(el)
36
+ if (record?.instance !== instance) {
37
+ record?.view.destroy()
38
+ const view = mountReference(el, instance, () => ({
39
+ slots: el.renderers,
40
+ rowAttrs: el.rowAttrs,
41
+ cellAttrs: el.cellAttrs,
42
+ sectionAttrs: el.sectionAttrs,
43
+ }))
44
+ record = { instance, view, generation: 0 }
45
+ views.set(el, record)
46
+ Object.defineProperty(el, 'api', {
47
+ configurable: true,
48
+ get: () => (el.isConnected ? views.get(el)?.instance.api : undefined),
49
+ })
50
+ } else record.view.update()
51
+ const generation = ++record.generation
52
+ return () => {
53
+ // The SDK runs cleanup both before the next property update and on disconnect.
54
+ queueMicrotask(() => {
55
+ if (views.get(el) !== record || record.generation !== generation) return
56
+ record.view.destroy()
57
+ views.delete(el)
58
+ })
59
+ }
60
+ },
61
+ },
62
+ })
63
+ }