@atom.io/spa 0.0.8 → 0.0.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.
- package/README.md +194 -30
- package/README.mdx +78 -20
- package/dist/BrandMarkScene-CBYk-RJ2.js +140 -0
- package/dist/BrandMarkScene-CBYk-RJ2.js.map +1 -0
- package/dist/astro-application.d.ts +1 -1
- package/dist/astro-client.d.ts +1 -1
- package/dist/astro-client.d.ts.map +1 -1
- package/dist/astro-client.js +5 -4
- package/dist/astro-client.js.map +1 -1
- package/dist/astro-resumability-client.d.ts +1 -1
- package/dist/astro-server.js +4 -4
- package/dist/astro-server.js.map +1 -1
- package/dist/{astro-shared-83hxw7-M.js → astro-shared-DBwINQav.js} +2 -2
- package/dist/{astro-shared-83hxw7-M.js.map → astro-shared-DBwINQav.js.map} +1 -1
- package/dist/{astro-shared-DZp1x8RQ.d.ts → astro-shared-KxK9OUSj.d.ts} +2 -2
- package/dist/{astro-shared-DZp1x8RQ.d.ts.map → astro-shared-KxK9OUSj.d.ts.map} +1 -1
- package/dist/astro.d.ts +1 -1
- package/dist/astro.js +1 -1
- package/dist/brand-css.d.ts +2 -0
- package/dist/brand-model.d.ts +32 -0
- package/dist/brand-model.d.ts.map +1 -0
- package/dist/brand-model.js +120 -0
- package/dist/brand-model.js.map +1 -0
- package/dist/brand.css +37 -0
- package/dist/brand.d.ts +13 -0
- package/dist/brand.d.ts.map +1 -0
- package/dist/brand.js +94 -0
- package/dist/brand.js.map +1 -0
- package/dist/{capabilities-DNcnGRv_.d.ts → capabilities-ChgSs6bp.d.ts} +34 -6
- package/dist/capabilities-ChgSs6bp.d.ts.map +1 -0
- package/dist/client.min.js +1 -1
- package/dist/core-DD2giR6E.js +451 -0
- package/dist/core-DD2giR6E.js.map +1 -0
- package/dist/development/BrandMarkScene-BDdC7K5M.js +140 -0
- package/dist/development/BrandMarkScene-BDdC7K5M.js.map +1 -0
- package/dist/development/astro-client.js +5 -4
- package/dist/development/astro-client.js.map +1 -1
- package/dist/development/astro-server.js +4 -4
- package/dist/development/astro-server.js.map +1 -1
- package/dist/development/{astro-shared-83hxw7-M.js → astro-shared-DBwINQav.js} +2 -2
- package/dist/development/{astro-shared-83hxw7-M.js.map → astro-shared-DBwINQav.js.map} +1 -1
- package/dist/development/astro.js +1 -1
- package/dist/development/brand-model.js +120 -0
- package/dist/development/brand-model.js.map +1 -0
- package/dist/development/brand.css +37 -0
- package/dist/development/brand.js +94 -0
- package/dist/development/brand.js.map +1 -0
- package/dist/development/core-DrUd8x25.js +465 -0
- package/dist/development/core-DrUd8x25.js.map +1 -0
- package/dist/development/{dom-DQ0M3rLL.js → dom-CEVtlaav.js} +152 -36
- package/dist/development/dom-CEVtlaav.js.map +1 -0
- package/dist/development/floating.js +2 -6
- package/dist/development/floating.js.map +1 -1
- package/dist/development/hot.js +164 -0
- package/dist/development/hot.js.map +1 -0
- package/dist/development/identity-B8wTFkyW.js +121 -0
- package/dist/development/identity-B8wTFkyW.js.map +1 -0
- package/dist/development/index.js +5 -5
- package/dist/development/jsx-runtime-BGDPK50B.js +49 -0
- package/dist/development/jsx-runtime-BGDPK50B.js.map +1 -0
- package/dist/development/jsx-runtime.js +1 -25
- package/dist/development/konva.js +12 -5
- package/dist/development/konva.js.map +1 -1
- package/dist/development/motion.js +41 -39
- package/dist/development/motion.js.map +1 -1
- package/dist/development/renderer.js +41 -40
- package/dist/development/renderer.js.map +1 -1
- package/dist/development/{server-renderer-DCLoMdOX.js → server-renderer-zAqEL1g1.js} +8 -18
- package/dist/development/server-renderer-zAqEL1g1.js.map +1 -0
- package/dist/development/server.js +1 -1
- package/dist/{src-Bme1kz2b.js → development/src-CB7ruwRs.js} +3 -3
- package/dist/development/src-CB7ruwRs.js.map +1 -0
- package/dist/development/three-CsZ6Od1C.js +527 -0
- package/dist/development/three-CsZ6Od1C.js.map +1 -0
- package/dist/development/three.js +1 -508
- package/dist/development/vite-WUiz1ju6.js +545 -0
- package/dist/development/vite-WUiz1ju6.js.map +1 -0
- package/dist/development/vite.js +1 -1
- package/dist/{dom-D8g3roX9.js → dom-DUpa3Sj2.js} +141 -33
- package/dist/dom-DUpa3Sj2.js.map +1 -0
- package/dist/floating.d.ts +1 -1
- package/dist/floating.d.ts.map +1 -1
- package/dist/floating.js +2 -6
- package/dist/floating.js.map +1 -1
- package/dist/hot.d.ts +15 -0
- package/dist/hot.d.ts.map +1 -0
- package/dist/hot.js +19 -0
- package/dist/hot.js.map +1 -0
- package/dist/identity-ByJswZtF.js +121 -0
- package/dist/identity-ByJswZtF.js.map +1 -0
- package/dist/{index-BFnXIfOj.d.ts → index-DdA39aMv.d.ts} +4 -4
- package/dist/{index-BFnXIfOj.d.ts.map → index-DdA39aMv.d.ts.map} +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +5 -5
- package/dist/jsx-runtime-BGDPK50B.js +49 -0
- package/dist/jsx-runtime-BGDPK50B.js.map +1 -0
- package/dist/{jsx-runtime-DQnpyT_g.d.ts → jsx-runtime-CCvcEosg.d.ts} +14 -9
- package/dist/jsx-runtime-CCvcEosg.d.ts.map +1 -0
- package/dist/jsx-runtime.d.ts +1 -1
- package/dist/jsx-runtime.js +1 -25
- package/dist/konva.d.ts +46 -9
- package/dist/konva.d.ts.map +1 -1
- package/dist/konva.js +12 -5
- package/dist/konva.js.map +1 -1
- package/dist/motion.d.ts +3 -1
- package/dist/motion.d.ts.map +1 -1
- package/dist/motion.js +41 -39
- package/dist/motion.js.map +1 -1
- package/dist/renderer.d.ts +1 -1
- package/dist/renderer.d.ts.map +1 -1
- package/dist/renderer.js +39 -40
- package/dist/renderer.js.map +1 -1
- package/dist/{server-renderer-D0rsFL_-.js → server-renderer-D7CdyOMo.js} +8 -18
- package/dist/server-renderer-D7CdyOMo.js.map +1 -0
- package/dist/server.d.ts +1 -1
- package/dist/server.js +1 -1
- package/dist/size.json +12 -12
- package/dist/{development/src-DoQgCX-O.js → src-B2W_TA_X.js} +3 -3
- package/dist/src-B2W_TA_X.js.map +1 -0
- package/dist/three-DcYxRXPW.js +527 -0
- package/dist/three-DcYxRXPW.js.map +1 -0
- package/dist/three.d.ts +24 -3
- package/dist/three.d.ts.map +1 -1
- package/dist/three.js +1 -508
- package/dist/vite-WUiz1ju6.js +545 -0
- package/dist/vite-WUiz1ju6.js.map +1 -0
- package/dist/vite.d.ts.map +1 -1
- package/dist/vite.js +1 -1
- package/package.json +45 -18
- package/src/BrandMark.module.css +25 -0
- package/src/BrandMark.tsx +106 -0
- package/src/BrandMarkScene.module.css +10 -0
- package/src/BrandMarkScene.tsx +116 -0
- package/src/astro-client.ts +3 -2
- package/src/astro-server.ts +2 -2
- package/src/brand-css.d.ts +2 -0
- package/src/brand-model.ts +177 -0
- package/src/brand.ts +3 -0
- package/src/capabilities.ts +48 -21
- package/src/compiler-hmr.ts +297 -0
- package/src/compiler.ts +127 -25
- package/src/core.ts +282 -33
- package/src/dom-forms.ts +127 -0
- package/src/dom.ts +124 -53
- package/src/floating.ts +2 -6
- package/src/hot.ts +191 -0
- package/src/identity.ts +39 -0
- package/src/index.ts +2 -0
- package/src/jsx-runtime.ts +6 -26
- package/src/jsx-types.d.ts +8 -3
- package/src/konva.ts +66 -10
- package/src/lasertag.d.ts +1 -0
- package/src/motion-layout.ts +55 -26
- package/src/motion.ts +3 -0
- package/src/presence.ts +11 -23
- package/src/props.ts +55 -0
- package/src/renderer.ts +91 -71
- package/src/server-renderer.ts +18 -13
- package/src/three.ts +92 -46
- package/src/use-loadable.ts +2 -2
- package/src/vite.ts +44 -4
- package/dist/capabilities-DNcnGRv_.d.ts.map +0 -1
- package/dist/capabilities-nOIitQ-_.js +0 -65
- package/dist/capabilities-nOIitQ-_.js.map +0 -1
- package/dist/core-B4rJ0xdc.js +0 -268
- package/dist/core-B4rJ0xdc.js.map +0 -1
- package/dist/development/capabilities-nOIitQ-_.js +0 -65
- package/dist/development/capabilities-nOIitQ-_.js.map +0 -1
- package/dist/development/core-BHA7I1nf.js +0 -271
- package/dist/development/core-BHA7I1nf.js.map +0 -1
- package/dist/development/dom-DQ0M3rLL.js.map +0 -1
- package/dist/development/jsx-runtime.js.map +0 -1
- package/dist/development/server-renderer-DCLoMdOX.js.map +0 -1
- package/dist/development/src-DoQgCX-O.js.map +0 -1
- package/dist/development/three.js.map +0 -1
- package/dist/development/vite-Bmyb6P5w.js +0 -265
- package/dist/development/vite-Bmyb6P5w.js.map +0 -1
- package/dist/dom-D8g3roX9.js.map +0 -1
- package/dist/jsx-runtime-DQnpyT_g.d.ts.map +0 -1
- package/dist/jsx-runtime.js.map +0 -1
- package/dist/server-renderer-D0rsFL_-.js.map +0 -1
- package/dist/src-Bme1kz2b.js.map +0 -1
- package/dist/three.js.map +0 -1
- package/dist/vite-Bmyb6P5w.js +0 -265
- package/dist/vite-Bmyb6P5w.js.map +0 -1
package/src/dom-forms.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type { Scope } from "./core.ts"
|
|
2
|
+
import { enqueue, isPresent } from "./core.ts"
|
|
3
|
+
|
|
4
|
+
type Control = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement
|
|
5
|
+
const changes = new WeakMap<EventListener, EventListener>()
|
|
6
|
+
const requests = new WeakMap<Element, () => void>()
|
|
7
|
+
const events = [`input`, `change`, `click`]
|
|
8
|
+
const wake = () => {}
|
|
9
|
+
|
|
10
|
+
/** Keep native input events, using per-edit change semantics for text/range controls. */
|
|
11
|
+
export function changeHandler(handler: EventListener): EventListener {
|
|
12
|
+
let wrapped = changes.get(handler)
|
|
13
|
+
if (!wrapped) {
|
|
14
|
+
wrapped = (event) => {
|
|
15
|
+
const target = event.target as Control | null
|
|
16
|
+
const editing =
|
|
17
|
+
target?.localName === `textarea` ||
|
|
18
|
+
(target?.localName === `input` &&
|
|
19
|
+
!/^(checkbox|radio|file)$/.test(target.type))
|
|
20
|
+
if (event.type === (editing ? `input` : `change`))
|
|
21
|
+
handler.call(event.currentTarget, event)
|
|
22
|
+
}
|
|
23
|
+
changes.set(handler, wrapped)
|
|
24
|
+
}
|
|
25
|
+
return wrapped
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function controlValue(control: Control, value: string | number): void {
|
|
29
|
+
if (
|
|
30
|
+
control.localName === `input` &&
|
|
31
|
+
control.type === `number` &&
|
|
32
|
+
typeof value === `number` &&
|
|
33
|
+
control.value !== `` &&
|
|
34
|
+
Number(control.value) === value
|
|
35
|
+
)
|
|
36
|
+
return
|
|
37
|
+
if (control.value !== String(value)) control.value = String(value)
|
|
38
|
+
if (control.localName === `select`) {
|
|
39
|
+
const select = control as HTMLSelectElement
|
|
40
|
+
if (select.selectedIndex < 0) {
|
|
41
|
+
const first = [...select.options].find((option) => !option.disabled)
|
|
42
|
+
if (first) first.selected = true
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function requestRestore(control: Control): void {
|
|
48
|
+
requests.get(control)?.()
|
|
49
|
+
if (control.localName !== `input` || control.type !== `radio`) return
|
|
50
|
+
const input = control as HTMLInputElement
|
|
51
|
+
for (const peer of (
|
|
52
|
+
input.getRootNode() as ParentNode
|
|
53
|
+
).querySelectorAll<HTMLInputElement>(`input`)) {
|
|
54
|
+
if (
|
|
55
|
+
peer !== input &&
|
|
56
|
+
peer.type === `radio` &&
|
|
57
|
+
peer.name === input.name &&
|
|
58
|
+
peer.form === input.form
|
|
59
|
+
)
|
|
60
|
+
requests.get(peer)?.()
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Restore controlled fields after event handlers and all queued option/prop updates. */
|
|
65
|
+
export function bindForm(
|
|
66
|
+
control: Control,
|
|
67
|
+
props: Record<string, any>,
|
|
68
|
+
scope: Scope,
|
|
69
|
+
): void {
|
|
70
|
+
let active: Event | undefined
|
|
71
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
72
|
+
const restore = () => {
|
|
73
|
+
if (!isPresent(scope)) return true
|
|
74
|
+
const value = props[`value`]
|
|
75
|
+
const checked = control.localName === `input` ? props[`checked`] : undefined
|
|
76
|
+
if (active?.eventPhase) return value == null && checked == null
|
|
77
|
+
if (value != null) controlValue(control, value)
|
|
78
|
+
if (checked != null) {
|
|
79
|
+
const input = control as HTMLInputElement
|
|
80
|
+
if (input.checked !== Boolean(checked)) input.checked = Boolean(checked)
|
|
81
|
+
}
|
|
82
|
+
return true
|
|
83
|
+
}
|
|
84
|
+
const finish = () => {
|
|
85
|
+
active = undefined
|
|
86
|
+
clearTimeout(timer)
|
|
87
|
+
enqueue(scope, wake)
|
|
88
|
+
}
|
|
89
|
+
const capture = (event: Event) => {
|
|
90
|
+
if (event.type === `click` && !/^(checkbox|radio)$/.test(control.type))
|
|
91
|
+
return
|
|
92
|
+
active = event
|
|
93
|
+
clearTimeout(timer)
|
|
94
|
+
// A stopped native event never reaches the root's bubble listener.
|
|
95
|
+
timer = setTimeout(() => {
|
|
96
|
+
requestRestore(control)
|
|
97
|
+
}, 0)
|
|
98
|
+
}
|
|
99
|
+
const root = scope._root
|
|
100
|
+
if (!root._forms) {
|
|
101
|
+
root._forms = new Set()
|
|
102
|
+
const end = (event: Event) => {
|
|
103
|
+
const target = event.target as Control
|
|
104
|
+
// Checkbox/radio change follows click and input; its handlers must see the edit.
|
|
105
|
+
if (
|
|
106
|
+
event.type === `click` ||
|
|
107
|
+
(event.type === `input` && /^(checkbox|radio)$/.test(target.type))
|
|
108
|
+
)
|
|
109
|
+
return
|
|
110
|
+
requestRestore(target)
|
|
111
|
+
}
|
|
112
|
+
for (const event of events) root._host!.addEventListener(event, end)
|
|
113
|
+
root._cleanups.add(() => {
|
|
114
|
+
for (const event of events) root._host!.removeEventListener(event, end)
|
|
115
|
+
})
|
|
116
|
+
}
|
|
117
|
+
const forms = root._forms
|
|
118
|
+
forms.add(restore)
|
|
119
|
+
requests.set(control, finish)
|
|
120
|
+
for (const event of events) control.addEventListener(event, capture, true)
|
|
121
|
+
scope._cleanups.add(() => {
|
|
122
|
+
clearTimeout(timer)
|
|
123
|
+
forms.delete(restore)
|
|
124
|
+
requests.delete(control)
|
|
125
|
+
for (const event of events) control.removeEventListener(event, capture, true)
|
|
126
|
+
})
|
|
127
|
+
}
|
package/src/dom.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { Canonical } from "atom.io/foundations/canonical"
|
|
2
|
+
|
|
1
3
|
import { capabilityProps } from "./capabilities.ts"
|
|
2
4
|
import type {
|
|
3
5
|
Accessor,
|
|
@@ -17,7 +19,12 @@ import {
|
|
|
17
19
|
untrack,
|
|
18
20
|
within,
|
|
19
21
|
} from "./core.ts"
|
|
20
|
-
import {
|
|
22
|
+
import { bindForm, changeHandler, controlValue } from "./dom-forms.ts"
|
|
23
|
+
import { hotRoot, trackRoot } from "./hot.ts"
|
|
24
|
+
import { identifyChildren, trackKeys } from "./identity.ts"
|
|
25
|
+
import { Fragment } from "./jsx-runtime.ts"
|
|
26
|
+
import type { PresenceCollection } from "./presence.ts"
|
|
27
|
+
import { instanceProps } from "./props.ts"
|
|
21
28
|
|
|
22
29
|
const HTML = `http://www.w3.org/1999/xhtml`
|
|
23
30
|
const SVG = `http://www.w3.org/2000/svg`
|
|
@@ -25,7 +32,20 @@ type Cursor = {
|
|
|
25
32
|
node: Node | null
|
|
26
33
|
report(expected: Node | null, found: Node | null): void
|
|
27
34
|
}
|
|
28
|
-
type RecordNode = {
|
|
35
|
+
type RecordNode = {
|
|
36
|
+
_value: Children
|
|
37
|
+
_scope: Scope
|
|
38
|
+
_start: Node
|
|
39
|
+
_end: Node
|
|
40
|
+
_index: number
|
|
41
|
+
}
|
|
42
|
+
function compatible(record: RecordNode, child: Children): boolean {
|
|
43
|
+
return (
|
|
44
|
+
typeof record._value === typeof child &&
|
|
45
|
+
(record._value as SpaElement | null)?.type ===
|
|
46
|
+
(child as SpaElement | null)?.type
|
|
47
|
+
)
|
|
48
|
+
}
|
|
29
49
|
|
|
30
50
|
function finish(cursor?: Cursor): void {
|
|
31
51
|
if (cursor?.node) {
|
|
@@ -106,11 +126,26 @@ function property(
|
|
|
106
126
|
.slice(2, capture ? -7 : undefined)
|
|
107
127
|
.toLowerCase()
|
|
108
128
|
.replace(`doubleclick`, `dblclick`)
|
|
109
|
-
|
|
129
|
+
const events =
|
|
130
|
+
event === `change`
|
|
131
|
+
? [`input`, `change`]
|
|
132
|
+
: [event === `focus` ? `focusin` : event === `blur` ? `focusout` : event]
|
|
133
|
+
if (previous)
|
|
134
|
+
for (const name of events)
|
|
135
|
+
element.removeEventListener(
|
|
136
|
+
name,
|
|
137
|
+
event === `change` ? changeHandler(previous) : previous,
|
|
138
|
+
capture,
|
|
139
|
+
)
|
|
110
140
|
if (value != null) {
|
|
111
141
|
if (import.meta.env.DEV && typeof value !== `function`)
|
|
112
142
|
throw new Error(`spa: event handler`)
|
|
113
|
-
|
|
143
|
+
for (const name of events)
|
|
144
|
+
element.addEventListener(
|
|
145
|
+
name,
|
|
146
|
+
event === `change` ? changeHandler(value) : value,
|
|
147
|
+
capture,
|
|
148
|
+
)
|
|
114
149
|
}
|
|
115
150
|
return
|
|
116
151
|
}
|
|
@@ -123,13 +158,26 @@ function property(
|
|
|
123
158
|
typeof value !== `string`
|
|
124
159
|
)
|
|
125
160
|
throw new Error(`spa: CSS string required`)
|
|
161
|
+
if (
|
|
162
|
+
element.namespaceURI === SVG &&
|
|
163
|
+
key === `focusable` &&
|
|
164
|
+
typeof value === `boolean`
|
|
165
|
+
)
|
|
166
|
+
value = String(value)
|
|
126
167
|
if (
|
|
127
168
|
element.namespaceURI === HTML &&
|
|
128
169
|
key in element &&
|
|
129
170
|
!/^(style|form|list)$/.test(key)
|
|
130
171
|
) {
|
|
131
172
|
const target = element as any
|
|
132
|
-
|
|
173
|
+
if (
|
|
174
|
+
key === `value` &&
|
|
175
|
+
/^(input|textarea|select)$/.test(element.localName) &&
|
|
176
|
+
value != null
|
|
177
|
+
)
|
|
178
|
+
controlValue(target, value)
|
|
179
|
+
else target[key] = value ?? (typeof target[key] === `boolean` ? false : ``)
|
|
180
|
+
if (value == null) element.removeAttribute(key)
|
|
133
181
|
} else if (value == null || (value === false && !/^(aria-|data-)/.test(key)))
|
|
134
182
|
element.removeAttribute(key)
|
|
135
183
|
else
|
|
@@ -147,18 +195,13 @@ function mount(
|
|
|
147
195
|
namespace: string,
|
|
148
196
|
cursor?: Cursor,
|
|
149
197
|
source = () => value,
|
|
198
|
+
initialKey?: Canonical,
|
|
150
199
|
): void {
|
|
151
200
|
if (typeof value === `function`) {
|
|
152
|
-
comment(`{`, parent, before, owner, cursor)
|
|
201
|
+
const begin = comment(`{`, parent, before, owner, cursor)
|
|
153
202
|
let end: Node | null = null
|
|
154
|
-
let records = new Map<
|
|
155
|
-
|
|
156
|
-
(record) => record._scope,
|
|
157
|
-
(record, child) =>
|
|
158
|
-
typeof record._value === typeof child &&
|
|
159
|
-
(record._value as SpaElement | null)?.type ===
|
|
160
|
-
(child as SpaElement | null)?.type,
|
|
161
|
-
)
|
|
203
|
+
let records = new Map<string | number, RecordNode>()
|
|
204
|
+
let presence: PresenceCollection<RecordNode, Children> | undefined
|
|
162
205
|
const retire = (
|
|
163
206
|
record: RecordNode,
|
|
164
207
|
key: unknown,
|
|
@@ -171,68 +214,68 @@ function mount(
|
|
|
171
214
|
let initial = cursor
|
|
172
215
|
observe(
|
|
173
216
|
owner,
|
|
174
|
-
() => (source() as () => Children)(),
|
|
175
|
-
(
|
|
176
|
-
|
|
177
|
-
|
|
217
|
+
() => identifyChildren((source() as () => Children)()),
|
|
218
|
+
(values) => {
|
|
219
|
+
presence ??= owner._root._presence?.collection(
|
|
220
|
+
(record: RecordNode) => record._scope,
|
|
221
|
+
compatible,
|
|
222
|
+
)
|
|
223
|
+
const kept = new Map<string | number, RecordNode>()
|
|
224
|
+
let previous: RecordNode | undefined
|
|
178
225
|
const focus = parent.ownerDocument!.activeElement as HTMLElement | null
|
|
179
226
|
for (let index = 0; index < values.length; index++) {
|
|
180
|
-
const child = values[index]
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
const key = explicit == null ? index : String(explicit)
|
|
184
|
-
if (import.meta.env.DEV && kept.has(key))
|
|
185
|
-
throw new Error(`spa: duplicate key`)
|
|
186
|
-
let record = records.get(key)
|
|
187
|
-
if (
|
|
188
|
-
record &&
|
|
189
|
-
(typeof record._value !== typeof child ||
|
|
190
|
-
(record._value as SpaElement | null)?.type !== vnode?.type)
|
|
191
|
-
) {
|
|
227
|
+
const { value: child, key, id } = values[index]
|
|
228
|
+
let record = records.get(id)
|
|
229
|
+
if (record && !compatible(record, child)) {
|
|
192
230
|
retire(
|
|
193
231
|
record,
|
|
194
|
-
|
|
232
|
+
id,
|
|
195
233
|
child == null || typeof child === `boolean`,
|
|
196
|
-
|
|
234
|
+
record._index,
|
|
197
235
|
)
|
|
198
236
|
record = undefined
|
|
199
237
|
}
|
|
200
|
-
record ??= presence?.take(
|
|
238
|
+
record ??= presence?.take(id, child)
|
|
201
239
|
if (!record) {
|
|
240
|
+
const at = previous ? previous._end.nextSibling : begin.nextSibling
|
|
202
241
|
const childOwner = createScope(owner)
|
|
203
242
|
record = {
|
|
243
|
+
_index: index,
|
|
204
244
|
_value: child,
|
|
205
245
|
_scope: childOwner,
|
|
206
|
-
_start: comment(`[`, parent,
|
|
246
|
+
_start: comment(`[`, parent, at, childOwner, initial),
|
|
207
247
|
_end: null!,
|
|
208
248
|
}
|
|
209
249
|
const held = record
|
|
210
250
|
mount(
|
|
211
251
|
child,
|
|
212
252
|
parent,
|
|
213
|
-
|
|
253
|
+
at,
|
|
214
254
|
childOwner,
|
|
215
255
|
namespace,
|
|
216
256
|
initial,
|
|
217
257
|
() => held._value,
|
|
258
|
+
key,
|
|
218
259
|
)
|
|
219
|
-
record._end = comment(
|
|
220
|
-
`]`,
|
|
221
|
-
parent,
|
|
222
|
-
end ?? before,
|
|
223
|
-
childOwner,
|
|
224
|
-
initial,
|
|
225
|
-
)
|
|
260
|
+
record._end = comment(`]`, parent, at, childOwner, initial)
|
|
226
261
|
} else {
|
|
262
|
+
// Frozen descriptors opt into reuse; mutable props and accessors must refresh.
|
|
263
|
+
const reusable =
|
|
264
|
+
record._value === child &&
|
|
265
|
+
typeof child === `object` &&
|
|
266
|
+
child !== null &&
|
|
267
|
+
Object.isFrozen(child) &&
|
|
268
|
+
Object.isFrozen((child as SpaElement).props)
|
|
227
269
|
record._value = child
|
|
228
270
|
owner._root._presence?.resume(record._scope)
|
|
229
|
-
invalidate(record._scope)
|
|
271
|
+
if (!reusable) invalidate(record._scope)
|
|
230
272
|
}
|
|
231
|
-
|
|
273
|
+
record._index = index
|
|
274
|
+
previous = record
|
|
275
|
+
kept.set(id, record)
|
|
232
276
|
}
|
|
233
277
|
for (const [key, record] of records)
|
|
234
|
-
if (!kept.has(key))
|
|
235
|
-
retire(record, key, true, [...records.keys()].indexOf(key))
|
|
278
|
+
if (!kept.has(key)) retire(record, key, true, record._index)
|
|
236
279
|
if (!initial) {
|
|
237
280
|
let at = end ?? before
|
|
238
281
|
const rendered = presence?.merge([...kept.values()]) ?? [
|
|
@@ -285,7 +328,7 @@ function mount(
|
|
|
285
328
|
if (import.meta.env.DEV && (!(`type` in value) || !(`props` in value)))
|
|
286
329
|
throw new Error(`spa: invalid child`)
|
|
287
330
|
const vnode = value as SpaElement
|
|
288
|
-
const props =
|
|
331
|
+
const props = instanceProps(() => (source() as SpaElement).props, initialKey)
|
|
289
332
|
if (!vnode.type || vnode.type === Fragment) {
|
|
290
333
|
mount(() => props[`children`], parent, before, owner, namespace, cursor)
|
|
291
334
|
return
|
|
@@ -295,7 +338,7 @@ function mount(
|
|
|
295
338
|
const output = within(childOwner, () =>
|
|
296
339
|
untrack(() => (vnode.type as Function)(props)),
|
|
297
340
|
)
|
|
298
|
-
mount(output, parent, before, childOwner, namespace, cursor)
|
|
341
|
+
mount(trackKeys(output), parent, before, childOwner, namespace, cursor)
|
|
299
342
|
return
|
|
300
343
|
}
|
|
301
344
|
const ns = vnode.type === `svg` ? SVG : namespace
|
|
@@ -306,6 +349,13 @@ function mount(
|
|
|
306
349
|
owner,
|
|
307
350
|
cursor,
|
|
308
351
|
) as Element
|
|
352
|
+
if (ns === HTML && /^(input|textarea|select)$/.test(vnode.type))
|
|
353
|
+
bindForm(element as HTMLInputElement, props, owner)
|
|
354
|
+
if (ns === HTML && (props[`autoFocus`] || props[`autofocus`])) {
|
|
355
|
+
owner._root._mounts!.push(() => {
|
|
356
|
+
if (!owner._dead) (element as HTMLElement).focus()
|
|
357
|
+
})
|
|
358
|
+
}
|
|
309
359
|
if (vnode.type === `astro-slot`) {
|
|
310
360
|
if (!cursor) element.innerHTML = props[`innerHTML`] ?? ``
|
|
311
361
|
} else if (ns === HTML && /^(textarea|title|style|script)$/.test(vnode.type)) {
|
|
@@ -373,7 +423,7 @@ function mount(
|
|
|
373
423
|
owner._root._mounts!.unshift(() => {
|
|
374
424
|
if (!owner._dead) {
|
|
375
425
|
owner._cleanups.add(() => ref(null))
|
|
376
|
-
ref(element)
|
|
426
|
+
within(owner, () => ref(element))
|
|
377
427
|
}
|
|
378
428
|
})
|
|
379
429
|
}
|
|
@@ -388,6 +438,7 @@ function start(
|
|
|
388
438
|
host: Element,
|
|
389
439
|
options: RootOptions,
|
|
390
440
|
hydrating: boolean,
|
|
441
|
+
before: Node | null = null,
|
|
391
442
|
): SpaRoot {
|
|
392
443
|
let reported = false
|
|
393
444
|
const cursor: Cursor | undefined = hydrating
|
|
@@ -411,21 +462,23 @@ function start(
|
|
|
411
462
|
? marker.nodeValue.slice(2)
|
|
412
463
|
: undefined
|
|
413
464
|
const root = createRoot(options)
|
|
465
|
+
root._host = host
|
|
414
466
|
if (prefix !== undefined && options.idPrefix === undefined)
|
|
415
467
|
root._prefix = prefix
|
|
468
|
+
if (import.meta.env.DEV) trackRoot(root)
|
|
416
469
|
try {
|
|
417
|
-
comment(`w:${root._prefix}`, host,
|
|
470
|
+
comment(`w:${root._prefix}`, host, before, root, cursor)
|
|
418
471
|
within(root, () => {
|
|
419
472
|
mount(
|
|
420
|
-
untrack(setup),
|
|
473
|
+
trackKeys(untrack(setup)),
|
|
421
474
|
host,
|
|
422
|
-
|
|
475
|
+
before,
|
|
423
476
|
root,
|
|
424
477
|
host.namespaceURI === SVG ? SVG : HTML,
|
|
425
478
|
cursor,
|
|
426
479
|
)
|
|
427
480
|
})
|
|
428
|
-
comment(`/w`, host,
|
|
481
|
+
comment(`/w`, host, before, root, cursor)
|
|
429
482
|
finish(cursor)
|
|
430
483
|
root.flush()
|
|
431
484
|
} catch (error) {
|
|
@@ -443,6 +496,12 @@ export function render(
|
|
|
443
496
|
host: Element,
|
|
444
497
|
options: RootOptions = {},
|
|
445
498
|
): SpaRoot {
|
|
499
|
+
if (import.meta.env.DEV)
|
|
500
|
+
return hotRoot(
|
|
501
|
+
(next, before) => start(setup, host, next, false, before),
|
|
502
|
+
options,
|
|
503
|
+
host,
|
|
504
|
+
)
|
|
446
505
|
return start(setup, host, options, false)
|
|
447
506
|
}
|
|
448
507
|
export function hydrate(
|
|
@@ -450,5 +509,17 @@ export function hydrate(
|
|
|
450
509
|
host: Element,
|
|
451
510
|
options: RootOptions = {},
|
|
452
511
|
): SpaRoot {
|
|
512
|
+
if (import.meta.env.DEV) {
|
|
513
|
+
let initial = true
|
|
514
|
+
return hotRoot(
|
|
515
|
+
(next, before) => {
|
|
516
|
+
const current = start(setup, host, next, initial, before)
|
|
517
|
+
initial = false
|
|
518
|
+
return current
|
|
519
|
+
},
|
|
520
|
+
options,
|
|
521
|
+
host,
|
|
522
|
+
)
|
|
523
|
+
}
|
|
453
524
|
return start(setup, host, options, true)
|
|
454
525
|
}
|
package/src/floating.ts
CHANGED
|
@@ -13,7 +13,7 @@ import type {
|
|
|
13
13
|
CapabilityTarget,
|
|
14
14
|
} from "./capabilities.ts"
|
|
15
15
|
import type { Scope } from "./core.ts"
|
|
16
|
-
import {
|
|
16
|
+
import { onMount, reportScopeError, within } from "./core.ts"
|
|
17
17
|
|
|
18
18
|
export interface FloatingAttributes<Kind extends string = `dom`> {
|
|
19
19
|
[`floating:reference`]?: Kind extends `dom` ? ReferenceElement | null : never
|
|
@@ -108,11 +108,7 @@ function attach(target: CapabilityTarget, scope?: Scope): CapabilityBinding {
|
|
|
108
108
|
})
|
|
109
109
|
return
|
|
110
110
|
}
|
|
111
|
-
|
|
112
|
-
fail(scope._root, error)
|
|
113
|
-
} catch (failure) {
|
|
114
|
-
scope._root.error(failure)
|
|
115
|
-
}
|
|
111
|
+
reportScopeError(scope, error)
|
|
116
112
|
}
|
|
117
113
|
const updatePosition = () => {
|
|
118
114
|
const anchor = reference
|
package/src/hot.ts
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { IMPLICIT } from "atom.io/internal"
|
|
2
|
+
|
|
3
|
+
import type { Component, Root, RootOptions } from "./core.ts"
|
|
4
|
+
import { onCleanup, owner } from "./core.ts"
|
|
5
|
+
|
|
6
|
+
interface Handle {
|
|
7
|
+
dispose(): void
|
|
8
|
+
flush(): void
|
|
9
|
+
}
|
|
10
|
+
type Factory = (options: RootOptions, before: Node | null) => Handle
|
|
11
|
+
interface View {
|
|
12
|
+
modules: Set<string>
|
|
13
|
+
options: RootOptions
|
|
14
|
+
refreshing: boolean
|
|
15
|
+
refresh(): void
|
|
16
|
+
report(error: unknown): void
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function createHotRuntime() {
|
|
20
|
+
const components = new Map<string, { current: Component; proxy: Component }>()
|
|
21
|
+
const roots = new WeakMap<Root, View>()
|
|
22
|
+
const views = new Set<View>()
|
|
23
|
+
let mounting: View | undefined
|
|
24
|
+
function track(root: Root): void {
|
|
25
|
+
if (mounting) {
|
|
26
|
+
roots.set(root, mounting)
|
|
27
|
+
mounting.options.idPrefix ??= root._prefix
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function onDispose(cleanup: () => void): void {
|
|
31
|
+
const view = roots.get(owner!._root)
|
|
32
|
+
onCleanup(() => {
|
|
33
|
+
if (!view?.refreshing) cleanup()
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
function register<T extends Component>(
|
|
37
|
+
id: string,
|
|
38
|
+
name: string,
|
|
39
|
+
implementation: T,
|
|
40
|
+
): T {
|
|
41
|
+
const key = JSON.stringify([id, name])
|
|
42
|
+
let record = components.get(key)
|
|
43
|
+
if (record) record.current = implementation
|
|
44
|
+
else {
|
|
45
|
+
const created = {
|
|
46
|
+
current: implementation as Component,
|
|
47
|
+
proxy: null! as Component,
|
|
48
|
+
}
|
|
49
|
+
created.proxy = Object.assign(
|
|
50
|
+
(...args: Parameters<Component>) => {
|
|
51
|
+
if (owner) {
|
|
52
|
+
const root = owner._root
|
|
53
|
+
const view = roots.get(root) ?? mounting
|
|
54
|
+
if (view) {
|
|
55
|
+
roots.set(root, view)
|
|
56
|
+
view.options.idPrefix ??= root._prefix
|
|
57
|
+
view.modules.add(id)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
// Compiled JSX mount helpers can take options after their first argument.
|
|
61
|
+
return created.current(...args)
|
|
62
|
+
},
|
|
63
|
+
{ spa: true },
|
|
64
|
+
)
|
|
65
|
+
record = created
|
|
66
|
+
components.set(key, record)
|
|
67
|
+
}
|
|
68
|
+
return record.proxy as T
|
|
69
|
+
}
|
|
70
|
+
function manage(
|
|
71
|
+
factory: Factory,
|
|
72
|
+
options: RootOptions,
|
|
73
|
+
host?: Element,
|
|
74
|
+
): Handle {
|
|
75
|
+
const settings = { ...options, store: options.store ?? IMPLICIT.STORE }
|
|
76
|
+
const anchor = host?.ownerDocument.createComment(`spa-hot`)
|
|
77
|
+
let current: Handle
|
|
78
|
+
let errorNode: HTMLElement | undefined
|
|
79
|
+
let disposed = false
|
|
80
|
+
const placeAnchor = () => {
|
|
81
|
+
if (!anchor || anchor.parentNode) return
|
|
82
|
+
let inside = false
|
|
83
|
+
for (const node of [...host!.childNodes]) {
|
|
84
|
+
if (node.nodeType === 8 && node.nodeValue === `w:${settings.idPrefix}`)
|
|
85
|
+
inside = true
|
|
86
|
+
if (inside && node.nodeType === 8 && node.nodeValue === `/w`) {
|
|
87
|
+
host!.insertBefore(anchor, node.nextSibling)
|
|
88
|
+
return
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
host!.append(anchor)
|
|
92
|
+
}
|
|
93
|
+
const boot = () => {
|
|
94
|
+
const previous = mounting
|
|
95
|
+
mounting = view
|
|
96
|
+
try {
|
|
97
|
+
current = factory(settings, anchor?.parentNode ? anchor : null)
|
|
98
|
+
} finally {
|
|
99
|
+
mounting = previous
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const view: View = {
|
|
103
|
+
modules: new Set(),
|
|
104
|
+
options: settings,
|
|
105
|
+
refreshing: false,
|
|
106
|
+
refresh() {
|
|
107
|
+
view.refreshing = true
|
|
108
|
+
try {
|
|
109
|
+
placeAnchor()
|
|
110
|
+
current.dispose()
|
|
111
|
+
errorNode?.remove()
|
|
112
|
+
boot()
|
|
113
|
+
} finally {
|
|
114
|
+
view.refreshing = false
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
report(error) {
|
|
118
|
+
const target =
|
|
119
|
+
host ?? (typeof document === `undefined` ? undefined : document.body)
|
|
120
|
+
if (target) {
|
|
121
|
+
errorNode = target.ownerDocument.createElement(`pre`)
|
|
122
|
+
errorNode.setAttribute(`data-spa-hot-error`, ``)
|
|
123
|
+
errorNode.textContent = `SPA hot refresh failed: ${String(error)}`
|
|
124
|
+
target.insertBefore(errorNode, anchor?.parentNode ? anchor : null)
|
|
125
|
+
}
|
|
126
|
+
options.onError?.(error)
|
|
127
|
+
},
|
|
128
|
+
}
|
|
129
|
+
views.add(view)
|
|
130
|
+
try {
|
|
131
|
+
boot()
|
|
132
|
+
} catch (error) {
|
|
133
|
+
views.delete(view)
|
|
134
|
+
throw error
|
|
135
|
+
}
|
|
136
|
+
if (view.modules.size) placeAnchor()
|
|
137
|
+
return {
|
|
138
|
+
flush() {
|
|
139
|
+
current.flush()
|
|
140
|
+
},
|
|
141
|
+
dispose() {
|
|
142
|
+
if (disposed) return
|
|
143
|
+
disposed = true
|
|
144
|
+
views.delete(view)
|
|
145
|
+
errorNode?.remove()
|
|
146
|
+
anchor?.remove()
|
|
147
|
+
current.dispose()
|
|
148
|
+
},
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
function refreshViews(id: string): void {
|
|
152
|
+
for (const view of [...views]) {
|
|
153
|
+
if (!views.has(view) || !view.modules.has(id)) continue
|
|
154
|
+
try {
|
|
155
|
+
view.refresh()
|
|
156
|
+
} catch (error) {
|
|
157
|
+
view.report(error)
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return { register, root: manage, refresh: refreshViews, track, onDispose }
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const runtime = import.meta.env.DEV ? createHotRuntime() : undefined
|
|
165
|
+
|
|
166
|
+
/** Development protocol used by the Vite transform. */
|
|
167
|
+
export function registerComponent<T extends Component>(
|
|
168
|
+
id: string,
|
|
169
|
+
name: string,
|
|
170
|
+
implementation: T,
|
|
171
|
+
): T {
|
|
172
|
+
return runtime ? runtime.register(id, name, implementation) : implementation
|
|
173
|
+
}
|
|
174
|
+
export function refresh(id: string): void {
|
|
175
|
+
runtime?.refresh(id)
|
|
176
|
+
}
|
|
177
|
+
export function trackRoot(root: Root): void {
|
|
178
|
+
runtime?.track(root)
|
|
179
|
+
}
|
|
180
|
+
/** Release root lifetime resources, retaining them through development remounts. */
|
|
181
|
+
export function onRootDispose(cleanup: () => void): void {
|
|
182
|
+
if (runtime) runtime.onDispose(cleanup)
|
|
183
|
+
else onCleanup(cleanup)
|
|
184
|
+
}
|
|
185
|
+
export function hotRoot(
|
|
186
|
+
factory: Factory,
|
|
187
|
+
options: RootOptions,
|
|
188
|
+
host?: Element,
|
|
189
|
+
): Handle {
|
|
190
|
+
return runtime ? runtime.root(factory, options, host) : factory(options, null)
|
|
191
|
+
}
|
package/src/identity.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Canonical } from "atom.io/foundations/canonical"
|
|
2
|
+
import { packCanonical } from "atom.io/foundations/canonical"
|
|
3
|
+
|
|
4
|
+
import type { Children, SpaElement } from "./core.ts"
|
|
5
|
+
|
|
6
|
+
type Props = SpaElement[`props`]
|
|
7
|
+
type Key = Canonical | undefined
|
|
8
|
+
type Identity = string | number
|
|
9
|
+
|
|
10
|
+
function childProps(value: Children): Props | undefined {
|
|
11
|
+
return (value as SpaElement | null)?.props
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function hasKey(value: Children): boolean {
|
|
15
|
+
if (Array.isArray(value)) return value.some(hasKey)
|
|
16
|
+
const props = childProps(value)
|
|
17
|
+
return props !== undefined && Object.hasOwn(props, `key`)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Direct output opts into key tracking when it first mounts. */
|
|
21
|
+
export function trackKeys(value: Children): Children {
|
|
22
|
+
return hasKey(value) ? () => value : value
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function identifyChildren(
|
|
26
|
+
output: Children,
|
|
27
|
+
duplicate = `spa: duplicate key`,
|
|
28
|
+
): { value: Children; key: Key; id: Identity }[] {
|
|
29
|
+
const values = ([output] as unknown[]).flat(Infinity) as Children[]
|
|
30
|
+
const seen = new Set<string>()
|
|
31
|
+
return values.map((value, index) => {
|
|
32
|
+
const key: Key = childProps(value)?.[`key`]
|
|
33
|
+
if (key === undefined) return { value, key, id: index }
|
|
34
|
+
const id = packCanonical(key)
|
|
35
|
+
if (seen.has(id)) throw new Error(duplicate)
|
|
36
|
+
seen.add(id)
|
|
37
|
+
return { value, key, id }
|
|
38
|
+
})
|
|
39
|
+
}
|