@atom.io/spa 0.0.0 → 0.0.2
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 +201 -6
- package/README.mdx +62 -1
- package/dist/astro-application.d.ts +1 -1
- package/dist/astro-client.d.ts +1 -1
- package/dist/astro-client.js +7 -4
- package/dist/astro-client.js.map +1 -1
- package/dist/astro-server.js +4 -3
- package/dist/astro-server.js.map +1 -1
- package/dist/astro-shared-BT45auwR.js.map +1 -1
- package/dist/{astro-shared-DkZaqX7L.d.ts → astro-shared-DyzbUi6c.d.ts} +4 -2
- package/dist/astro-shared-DyzbUi6c.d.ts.map +1 -0
- package/dist/astro.d.ts +1 -1
- package/dist/astro.js +1 -1
- package/dist/{core-Dvqm7w2y.js → capabilities-BuZUQpiD.js} +64 -2
- package/dist/capabilities-BuZUQpiD.js.map +1 -0
- package/dist/{core-BhiA9fFi.d.ts → capabilities-yC9TsHDI.d.ts} +20 -2
- package/dist/capabilities-yC9TsHDI.d.ts.map +1 -0
- package/dist/client.min.js +1 -1
- package/dist/development/astro-client.js +7 -4
- package/dist/development/astro-client.js.map +1 -1
- package/dist/development/astro-server.js +4 -3
- package/dist/development/astro-server.js.map +1 -1
- package/dist/development/astro-shared-BT45auwR.js.map +1 -1
- package/dist/development/astro.js +1 -1
- package/dist/development/{core-DB3sQz4h.js → capabilities-DhOJmeoJ.js} +64 -2
- package/dist/development/capabilities-DhOJmeoJ.js.map +1 -0
- package/dist/development/{dom-D9cY6l1R.js → dom-BKc6V_Kz.js} +8 -3
- package/dist/development/dom-BKc6V_Kz.js.map +1 -0
- package/dist/development/index.js +3 -38
- package/dist/development/jsx-runtime.js.map +1 -1
- package/dist/development/konva.js +121 -0
- package/dist/development/konva.js.map +1 -0
- package/dist/development/motion.js +217 -0
- package/dist/development/motion.js.map +1 -0
- package/dist/development/renderer.js +177 -0
- package/dist/development/renderer.js.map +1 -0
- package/dist/development/server.js +17 -7
- package/dist/development/server.js.map +1 -1
- package/dist/development/src-Dh-d6w4y.js +38 -0
- package/dist/development/src-Dh-d6w4y.js.map +1 -0
- package/dist/development/three.js +438 -0
- package/dist/development/three.js.map +1 -0
- package/dist/development/vite-CSHoxzXU.js +240 -0
- package/dist/development/vite-CSHoxzXU.js.map +1 -0
- package/dist/development/vite.js +1 -1
- package/dist/{dom-BR9o9wZW.js → dom-B2rm8o2L.js} +8 -3
- package/dist/dom-B2rm8o2L.js.map +1 -0
- package/dist/index-BdwxmSKg.d.ts +24 -0
- package/dist/index-BdwxmSKg.d.ts.map +1 -0
- package/dist/index.d.ts +4 -24
- package/dist/index.js +3 -38
- package/dist/{jsx-runtime-BzGV18VO.d.ts → jsx-runtime-CZU7F8Qz.d.ts} +6 -4
- package/dist/jsx-runtime-CZU7F8Qz.d.ts.map +1 -0
- package/dist/jsx-runtime.d.ts +2 -2
- package/dist/jsx-runtime.js.map +1 -1
- package/dist/konva.d.ts +32 -0
- package/dist/konva.d.ts.map +1 -0
- package/dist/konva.js +121 -0
- package/dist/konva.js.map +1 -0
- package/dist/motion.d.ts +44 -0
- package/dist/motion.d.ts.map +1 -0
- package/dist/motion.js +217 -0
- package/dist/motion.js.map +1 -0
- package/dist/renderer.d.ts +24 -0
- package/dist/renderer.d.ts.map +1 -0
- package/dist/renderer.js +177 -0
- package/dist/renderer.js.map +1 -0
- package/dist/server.d.ts +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +17 -7
- package/dist/server.js.map +1 -1
- package/dist/size.json +16 -16
- package/dist/src-BZxX4xbU.js +38 -0
- package/dist/src-BZxX4xbU.js.map +1 -0
- package/dist/three.d.ts +110 -0
- package/dist/three.d.ts.map +1 -0
- package/dist/three.js +438 -0
- package/dist/three.js.map +1 -0
- package/dist/vite-CSHoxzXU.js +240 -0
- package/dist/vite-CSHoxzXU.js.map +1 -0
- package/dist/vite.js +1 -1
- package/package.json +176 -131
- package/src/astro-client.ts +1 -0
- package/src/astro-server.ts +1 -0
- package/src/astro-shared.ts +3 -0
- package/src/capabilities.ts +88 -0
- package/src/compiler.ts +251 -177
- package/src/core.ts +11 -0
- package/src/dom.ts +4 -1
- package/src/index.ts +5 -0
- package/src/jsx-runtime.ts +4 -0
- package/src/jsx-types.d.ts +3 -1
- package/src/konva.ts +159 -0
- package/src/motion.ts +406 -0
- package/src/renderer.ts +241 -0
- package/src/server.ts +21 -13
- package/src/three-events.ts +330 -0
- package/src/three.ts +350 -0
- package/dist/astro-shared-DkZaqX7L.d.ts.map +0 -1
- package/dist/core-BhiA9fFi.d.ts.map +0 -1
- package/dist/core-Dvqm7w2y.js.map +0 -1
- package/dist/development/core-DB3sQz4h.js.map +0 -1
- package/dist/development/dom-D9cY6l1R.js.map +0 -1
- package/dist/development/index.js.map +0 -1
- package/dist/development/vite-BadrFSIM.js +0 -126
- package/dist/development/vite-BadrFSIM.js.map +0 -1
- package/dist/dom-BR9o9wZW.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/jsx-runtime-BzGV18VO.d.ts.map +0 -1
- package/dist/vite-BadrFSIM.js +0 -126
- package/dist/vite-BadrFSIM.js.map +0 -1
package/src/renderer.ts
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import type { CapabilityTarget } from "./capabilities.ts"
|
|
2
|
+
import { capabilityProps } from "./capabilities.ts"
|
|
3
|
+
import type { Child, Component, RootOptions, Scope, VNode } from "./core.ts"
|
|
4
|
+
import {
|
|
5
|
+
createRoot,
|
|
6
|
+
createScope,
|
|
7
|
+
dispose,
|
|
8
|
+
fail,
|
|
9
|
+
invalidate,
|
|
10
|
+
observe,
|
|
11
|
+
owner,
|
|
12
|
+
untrack,
|
|
13
|
+
within,
|
|
14
|
+
} from "./core.ts"
|
|
15
|
+
import { Fragment, merge } from "./jsx-runtime.ts"
|
|
16
|
+
|
|
17
|
+
export interface SceneHost<N> {
|
|
18
|
+
target?(node: N): CapabilityTarget
|
|
19
|
+
create(type: string, props: Record<string, any>): N
|
|
20
|
+
update(
|
|
21
|
+
node: N,
|
|
22
|
+
props: Record<string, any>,
|
|
23
|
+
previous: Record<string, any>,
|
|
24
|
+
): void
|
|
25
|
+
children(parent: N, children: N[]): void
|
|
26
|
+
destroy(node: N): void
|
|
27
|
+
}
|
|
28
|
+
interface Entry<N> {
|
|
29
|
+
scope: Scope
|
|
30
|
+
value: Child
|
|
31
|
+
nodes: () => N[]
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface SceneRenderer<N> {
|
|
35
|
+
mount(setup: () => Child, parent: N): void
|
|
36
|
+
render(
|
|
37
|
+
setup: () => Child,
|
|
38
|
+
parent: N,
|
|
39
|
+
options?: RootOptions,
|
|
40
|
+
): { flush(): void; dispose(): void }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** A scene renderer shares SPA's owner, store, scheduler, and component lifecycle. */
|
|
44
|
+
export function createSceneRenderer<N>(host: SceneHost<N>): SceneRenderer<N> {
|
|
45
|
+
function mount(
|
|
46
|
+
source: () => Child,
|
|
47
|
+
scope: Scope,
|
|
48
|
+
changed: () => void,
|
|
49
|
+
): () => N[] {
|
|
50
|
+
let entries: Entry<N>[] = []
|
|
51
|
+
observe(scope, source, (next) => {
|
|
52
|
+
const values = (
|
|
53
|
+
Array.isArray(next) ? next.flat(Infinity) : [next]
|
|
54
|
+
) as Child[]
|
|
55
|
+
const old = new Map(
|
|
56
|
+
entries.map((entry, i) => [key(entry.value, i), entry]),
|
|
57
|
+
)
|
|
58
|
+
const seen = new Set<string>()
|
|
59
|
+
entries = values.map((value, i) => {
|
|
60
|
+
const id = key(value, i)
|
|
61
|
+
if (seen.has(id)) throw new Error(`spa: duplicate scene key`)
|
|
62
|
+
seen.add(id)
|
|
63
|
+
let entry = old.get(id)
|
|
64
|
+
old.delete(id)
|
|
65
|
+
if (entry && !same(entry.value, value)) {
|
|
66
|
+
dispose(entry.scope)
|
|
67
|
+
entry = undefined
|
|
68
|
+
}
|
|
69
|
+
if (entry) {
|
|
70
|
+
entry.value = value
|
|
71
|
+
invalidate(entry.scope)
|
|
72
|
+
return entry
|
|
73
|
+
}
|
|
74
|
+
const childScope = createScope(scope)
|
|
75
|
+
const created: Entry<N> = { scope: childScope, value, nodes: null! }
|
|
76
|
+
created.nodes = build(() => created.value, childScope, changed)
|
|
77
|
+
return created
|
|
78
|
+
})
|
|
79
|
+
for (const entry of old.values()) dispose(entry.scope)
|
|
80
|
+
changed()
|
|
81
|
+
})
|
|
82
|
+
return () => entries.flatMap((entry) => entry.nodes())
|
|
83
|
+
}
|
|
84
|
+
function key(value: Child, i: number): string {
|
|
85
|
+
const explicit = (value as VNode | null)?.props?.[`key`]
|
|
86
|
+
return explicit == null ? `index:${i}` : `key:${explicit}`
|
|
87
|
+
}
|
|
88
|
+
function same(a: Child, b: Child): boolean {
|
|
89
|
+
return (
|
|
90
|
+
typeof a === typeof b &&
|
|
91
|
+
(a as VNode | null)?.type === (b as VNode | null)?.type
|
|
92
|
+
)
|
|
93
|
+
}
|
|
94
|
+
function build(
|
|
95
|
+
source: () => Child,
|
|
96
|
+
scope: Scope,
|
|
97
|
+
changed: () => void,
|
|
98
|
+
): () => N[] {
|
|
99
|
+
const value = untrack(source)
|
|
100
|
+
if (typeof value === `function`)
|
|
101
|
+
return mount(() => (source() as () => Child)(), scope, changed)
|
|
102
|
+
if (value == null || typeof value === `boolean`) return () => []
|
|
103
|
+
if (typeof value !== `object`)
|
|
104
|
+
throw new Error(`spa: scene children must be nodes`)
|
|
105
|
+
if (!(`type` in value) || !(`props` in value))
|
|
106
|
+
throw new Error(`spa: invalid scene child`)
|
|
107
|
+
const vnode = value
|
|
108
|
+
const props = merge(() => (source() as VNode).props)
|
|
109
|
+
if (!vnode.type || vnode.type === Fragment)
|
|
110
|
+
return mount(() => props[`children`], scope, changed)
|
|
111
|
+
if (typeof vnode.type === `function`) {
|
|
112
|
+
const output = within(scope, () =>
|
|
113
|
+
untrack(() => (vnode.type as Component)(props)),
|
|
114
|
+
)
|
|
115
|
+
return mount(() => output, scope, changed)
|
|
116
|
+
}
|
|
117
|
+
const node = host.create(
|
|
118
|
+
vnode.type,
|
|
119
|
+
untrack(() => props),
|
|
120
|
+
)
|
|
121
|
+
scope._cleanups.add(() => {
|
|
122
|
+
host.destroy(node)
|
|
123
|
+
})
|
|
124
|
+
const nativeProps = capabilityProps(
|
|
125
|
+
scope,
|
|
126
|
+
host.target?.(node) ?? { kind: `scene`, node },
|
|
127
|
+
)
|
|
128
|
+
let previous: Record<string, any> = {}
|
|
129
|
+
observe(
|
|
130
|
+
scope,
|
|
131
|
+
() => ({ ...props }),
|
|
132
|
+
(all) => {
|
|
133
|
+
const next = nativeProps(all)
|
|
134
|
+
host.update(node, next, previous)
|
|
135
|
+
previous = next
|
|
136
|
+
},
|
|
137
|
+
)
|
|
138
|
+
let children: () => N[] = () => []
|
|
139
|
+
const sync = () => {
|
|
140
|
+
host.children(node, children())
|
|
141
|
+
}
|
|
142
|
+
children = mount(() => props[`children`], scope, sync)
|
|
143
|
+
sync()
|
|
144
|
+
const ref = untrack(() => props[`ref`])
|
|
145
|
+
if (ref)
|
|
146
|
+
scope._root._mounts!.unshift(() => {
|
|
147
|
+
if (!scope._dead) {
|
|
148
|
+
scope._cleanups.add(() => ref(null))
|
|
149
|
+
ref(node)
|
|
150
|
+
}
|
|
151
|
+
})
|
|
152
|
+
return () => [node]
|
|
153
|
+
}
|
|
154
|
+
function attach(setup: () => Child, parent: N, scope: Scope) {
|
|
155
|
+
let nodes: () => N[] = () => []
|
|
156
|
+
const sync = () => {
|
|
157
|
+
host.children(parent, nodes())
|
|
158
|
+
}
|
|
159
|
+
nodes = mount(() => untrack(setup), scope, sync)
|
|
160
|
+
sync()
|
|
161
|
+
scope._cleanups.add(() => {
|
|
162
|
+
host.children(parent, [])
|
|
163
|
+
})
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
/** Attach beneath the current component; disposal follows its lifetime. */
|
|
167
|
+
mount(setup: () => Child, parent: N): void {
|
|
168
|
+
if (!owner) throw new Error(`spa: scene mount requires an owner`)
|
|
169
|
+
const scope = createScope(owner)
|
|
170
|
+
try {
|
|
171
|
+
attach(setup, parent, scope)
|
|
172
|
+
} catch (error) {
|
|
173
|
+
fail(scope, error)
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
render(setup: () => Child, parent: N, options: RootOptions = {}) {
|
|
177
|
+
const root = createRoot(options)
|
|
178
|
+
try {
|
|
179
|
+
within(root, () => {
|
|
180
|
+
attach(setup, parent, root)
|
|
181
|
+
})
|
|
182
|
+
root.flush()
|
|
183
|
+
} catch (error) {
|
|
184
|
+
fail(root, error)
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
flush: root.flush,
|
|
188
|
+
dispose: () => {
|
|
189
|
+
dispose(root)
|
|
190
|
+
},
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Define a typed scene component without introducing another JSX runtime. */
|
|
197
|
+
export function sceneComponent<P>(type: string): (props: P) => VNode {
|
|
198
|
+
return (props) => ({ type, props: props as Record<string, any> })
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** Bind imperative scene work to the current owner and its reactive dependencies. */
|
|
202
|
+
export function bindScene<T>(read: () => T, apply: (value: T) => void): void {
|
|
203
|
+
observe(owner!, read, apply)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Scene drawing follows an installed capability clock, or the browser clock. */
|
|
207
|
+
export function onSceneFrame(callback: (time: number) => void): void {
|
|
208
|
+
const scope = owner!
|
|
209
|
+
const clocks = [...scope._root._capabilities.values()].filter(
|
|
210
|
+
(capability) => capability.scheduleFrame,
|
|
211
|
+
)
|
|
212
|
+
if (clocks.length > 1)
|
|
213
|
+
throw new Error(`spa: multiple frame clock capabilities`)
|
|
214
|
+
const draw = (time: number) => {
|
|
215
|
+
if (scope._dead) return
|
|
216
|
+
try {
|
|
217
|
+
callback(time)
|
|
218
|
+
} catch (error) {
|
|
219
|
+
try {
|
|
220
|
+
fail(scope._root, error)
|
|
221
|
+
} catch (failure) {
|
|
222
|
+
scope._root.error(failure)
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
let stop: () => void
|
|
227
|
+
if (clocks[0]) stop = clocks[0].scheduleFrame!(draw)
|
|
228
|
+
else {
|
|
229
|
+
let frame = 0
|
|
230
|
+
const tick = (time: number) => {
|
|
231
|
+
if (scope._dead) return
|
|
232
|
+
draw(time)
|
|
233
|
+
if (!scope._dead) frame = requestAnimationFrame(tick)
|
|
234
|
+
}
|
|
235
|
+
frame = requestAnimationFrame(tick)
|
|
236
|
+
stop = () => {
|
|
237
|
+
cancelAnimationFrame(frame)
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
scope._cleanups.add(stop)
|
|
241
|
+
}
|
package/src/server.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { capabilityNamespace, validateCapabilities } from "./capabilities.ts"
|
|
1
2
|
import type { Child, RootOptions, Scope, VNode } from "./core.ts"
|
|
2
3
|
import {
|
|
3
4
|
createRoot,
|
|
@@ -20,7 +21,7 @@ const escape = (value: unknown): string =>
|
|
|
20
21
|
const voidTags =
|
|
21
22
|
/^(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/
|
|
22
23
|
const booleans = new Set(
|
|
23
|
-
`allowfullscreen async autofocus autoplay checked controls default defer disabled formnovalidate
|
|
24
|
+
`allowfullscreen async autofocus autoplay checked controls credentialless default defer disabled disablepictureinpicture disableremoteplayback formnovalidate inert ismap itemscope loop multiple muted nomodule novalidate open playsinline readonly required reversed selected shadowrootclonable shadowrootdelegatesfocus shadowrootserializable`.split(
|
|
24
25
|
` `,
|
|
25
26
|
),
|
|
26
27
|
)
|
|
@@ -73,10 +74,12 @@ function stringify(
|
|
|
73
74
|
if (type === `astro-slot`)
|
|
74
75
|
return `<astro-slot${props[`name`] ? ` name="${escape(props[`name`])}"` : ``}>${props[`innerHTML`] ?? ``}</astro-slot>`
|
|
75
76
|
const ns = type === `svg` ? `svg` : namespace
|
|
77
|
+
validateCapabilities(owner, props)
|
|
76
78
|
let attrs = ``
|
|
77
79
|
let textValue: string | number | undefined
|
|
78
80
|
for (let key of Object.keys(props)) {
|
|
79
81
|
if (
|
|
82
|
+
capabilityNamespace(key) !== undefined ||
|
|
80
83
|
key === `children` ||
|
|
81
84
|
key === `ref` ||
|
|
82
85
|
key === `key` ||
|
|
@@ -90,26 +93,31 @@ function stringify(
|
|
|
90
93
|
)
|
|
91
94
|
throw new Error(`spa: owned content`)
|
|
92
95
|
let attribute = props[key]
|
|
96
|
+
if (ns === `html`) {
|
|
97
|
+
if (key === `defaultValue` && (type === `input` || type === `textarea`)) {
|
|
98
|
+
if (props[`value`] != null) continue
|
|
99
|
+
key = `value`
|
|
100
|
+
} else if (key === `defaultChecked` && type === `input`) {
|
|
101
|
+
if (props[`checked`] != null) continue
|
|
102
|
+
key = `checked`
|
|
103
|
+
}
|
|
104
|
+
}
|
|
93
105
|
if (key === `className`) key = `class`
|
|
94
106
|
if (key === `htmlFor`) key = `for`
|
|
95
107
|
if (!/^[^\s"'<>/=]+$/.test(key)) throw new Error(`spa: invalid attribute`)
|
|
96
108
|
if (key === `value` && (type === `select` || type === `textarea`)) {
|
|
97
|
-
textValue = attribute
|
|
109
|
+
textValue = attribute ?? textValue
|
|
98
110
|
continue
|
|
99
111
|
}
|
|
100
112
|
if (key === `style` && attribute != null && typeof attribute !== `string`)
|
|
101
113
|
throw new TypeError(`spa: CSS string required`)
|
|
102
|
-
const boolean =
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
)
|
|
108
|
-
|
|
109
|
-
attrs +=
|
|
110
|
-
boolean || (attribute === true && !/^(aria-|data-)/.test(key))
|
|
111
|
-
? ` ${key}=""`
|
|
112
|
-
: ` ${key}="${escape(attribute)}"`
|
|
114
|
+
const boolean =
|
|
115
|
+
ns === `html` &&
|
|
116
|
+
(booleans.has(key.toLowerCase()) ||
|
|
117
|
+
(/^(capture|download|hidden)$/.test(key) &&
|
|
118
|
+
typeof attribute === `boolean`))
|
|
119
|
+
if (attribute == null || (boolean && !attribute)) continue
|
|
120
|
+
attrs += boolean ? ` ${key}=""` : ` ${key}="${escape(attribute)}"`
|
|
113
121
|
}
|
|
114
122
|
if (
|
|
115
123
|
type === `option` &&
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import * as THREE from "three"
|
|
2
|
+
|
|
3
|
+
export interface ThreePointerEvent<N extends THREE.Object3D = THREE.Object3D> {
|
|
4
|
+
type: string
|
|
5
|
+
nativeEvent: PointerEvent
|
|
6
|
+
target: THREE.Object3D
|
|
7
|
+
currentTarget: N
|
|
8
|
+
intersection: THREE.Intersection
|
|
9
|
+
ray: THREE.Ray
|
|
10
|
+
pointerId: number
|
|
11
|
+
button: number
|
|
12
|
+
buttons: number
|
|
13
|
+
clientX: number
|
|
14
|
+
clientY: number
|
|
15
|
+
stopPropagation(): void
|
|
16
|
+
preventDefault(): void
|
|
17
|
+
setPointerCapture(): void
|
|
18
|
+
releasePointerCapture(): void
|
|
19
|
+
hasPointerCapture(): boolean
|
|
20
|
+
}
|
|
21
|
+
export type ThreeEventHandler<N extends THREE.Object3D = THREE.Object3D> = (
|
|
22
|
+
event: ThreePointerEvent<N>,
|
|
23
|
+
) => void
|
|
24
|
+
export interface ThreeEventAttributes<
|
|
25
|
+
N extends THREE.Object3D = THREE.Object3D,
|
|
26
|
+
> {
|
|
27
|
+
onPointerDown?: ThreeEventHandler<N>
|
|
28
|
+
onPointerUp?: ThreeEventHandler<N>
|
|
29
|
+
onPointerMove?: ThreeEventHandler<N>
|
|
30
|
+
onPointerEnter?: ThreeEventHandler<N>
|
|
31
|
+
onPointerLeave?: ThreeEventHandler<N>
|
|
32
|
+
onPointerOver?: ThreeEventHandler<N>
|
|
33
|
+
onPointerOut?: ThreeEventHandler<N>
|
|
34
|
+
onPointerCancel?: ThreeEventHandler<N>
|
|
35
|
+
onClick?: ThreeEventHandler<N>
|
|
36
|
+
}
|
|
37
|
+
type Hit = { node: THREE.Object3D; intersection: THREE.Intersection }
|
|
38
|
+
type Handler = ThreeEventHandler
|
|
39
|
+
const listeners = new WeakMap<THREE.Object3D, Map<string, Set<Handler>>>()
|
|
40
|
+
const managers = new Set<{ remove(node: THREE.Object3D): void }>()
|
|
41
|
+
|
|
42
|
+
export function listenThree(
|
|
43
|
+
node: THREE.Object3D,
|
|
44
|
+
type: string,
|
|
45
|
+
handler: Handler,
|
|
46
|
+
): () => void {
|
|
47
|
+
const events = listeners.get(node) ?? new Map<string, Set<Handler>>()
|
|
48
|
+
listeners.set(node, events)
|
|
49
|
+
const handlers = events.get(type) ?? new Set<Handler>()
|
|
50
|
+
events.set(type, handlers)
|
|
51
|
+
handlers.add(handler)
|
|
52
|
+
return () => {
|
|
53
|
+
handlers.delete(handler)
|
|
54
|
+
if (!handlers.size) events.delete(type)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
export function forgetThree(node: THREE.Object3D): void {
|
|
58
|
+
const errors: unknown[] = []
|
|
59
|
+
for (const manager of managers) {
|
|
60
|
+
try {
|
|
61
|
+
manager.remove(node)
|
|
62
|
+
} catch (error) {
|
|
63
|
+
errors.push(error)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
listeners.delete(node)
|
|
67
|
+
if (errors.length) throw new AggregateError(errors)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface ThreeEventManager {
|
|
71
|
+
update(): void
|
|
72
|
+
/** Release hover, press, and capture state for a removed subtree. */
|
|
73
|
+
remove(node: THREE.Object3D): void
|
|
74
|
+
dispose(): void
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Attach raycast pointer routing to a canvas; usable with borrowed scene roots. */
|
|
78
|
+
export function createThreeEvents(
|
|
79
|
+
canvas: HTMLCanvasElement,
|
|
80
|
+
scene: THREE.Object3D,
|
|
81
|
+
camera: () => THREE.Camera,
|
|
82
|
+
onError: (error: unknown) => void = (error) => {
|
|
83
|
+
throw error
|
|
84
|
+
},
|
|
85
|
+
): ThreeEventManager {
|
|
86
|
+
const raycaster = new THREE.Raycaster()
|
|
87
|
+
const pointer = new THREE.Vector2()
|
|
88
|
+
const hovering = new Map<number, Hit>()
|
|
89
|
+
const pressed = new Map<number, Hit>()
|
|
90
|
+
const captures = new Map<number, Hit>()
|
|
91
|
+
const last = new Map<number, PointerEvent>()
|
|
92
|
+
let dead = false
|
|
93
|
+
const path = (node: THREE.Object3D) => {
|
|
94
|
+
const nodes: THREE.Object3D[] = []
|
|
95
|
+
for (let at: THREE.Object3D | null = node; at; at = at.parent) {
|
|
96
|
+
nodes.push(at)
|
|
97
|
+
if (at === scene) return nodes
|
|
98
|
+
}
|
|
99
|
+
return []
|
|
100
|
+
}
|
|
101
|
+
const contains = (node: THREE.Object3D, child: THREE.Object3D) => {
|
|
102
|
+
for (let at: THREE.Object3D | null = child; at; at = at.parent)
|
|
103
|
+
if (at === node) return true
|
|
104
|
+
return false
|
|
105
|
+
}
|
|
106
|
+
const eligible = (intersection: THREE.Intersection) => {
|
|
107
|
+
const nodes = path(intersection.object)
|
|
108
|
+
if (!nodes.length || nodes.some((node) => !node.visible)) return false
|
|
109
|
+
const material = (intersection.object as THREE.Mesh).material
|
|
110
|
+
return (
|
|
111
|
+
!material ||
|
|
112
|
+
(Array.isArray(material)
|
|
113
|
+
? material[intersection.face?.materialIndex ?? 0]?.visible !== false
|
|
114
|
+
: material.visible)
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
const pick = (event: PointerEvent): Hit | undefined => {
|
|
118
|
+
const bounds = canvas.getBoundingClientRect()
|
|
119
|
+
if (!bounds.width || !bounds.height) return undefined
|
|
120
|
+
const active = camera()
|
|
121
|
+
scene.updateWorldMatrix(true, true)
|
|
122
|
+
active.updateWorldMatrix(true, false)
|
|
123
|
+
pointer.set(
|
|
124
|
+
((event.clientX - bounds.left) / bounds.width) * 2 - 1,
|
|
125
|
+
1 - ((event.clientY - bounds.top) / bounds.height) * 2,
|
|
126
|
+
)
|
|
127
|
+
raycaster.layers.mask = active.layers.mask
|
|
128
|
+
raycaster.setFromCamera(pointer, active)
|
|
129
|
+
if (
|
|
130
|
+
event.clientX < bounds.left ||
|
|
131
|
+
event.clientX > bounds.right ||
|
|
132
|
+
event.clientY < bounds.top ||
|
|
133
|
+
event.clientY > bounds.bottom
|
|
134
|
+
)
|
|
135
|
+
return undefined
|
|
136
|
+
const intersection = raycaster.intersectObject(scene, true).find(eligible)
|
|
137
|
+
return intersection ? { node: intersection.object, intersection } : undefined
|
|
138
|
+
}
|
|
139
|
+
const nativeCapture = (id: number) => {
|
|
140
|
+
if (canvas.hasPointerCapture?.(id)) canvas.releasePointerCapture(id)
|
|
141
|
+
}
|
|
142
|
+
const dispatch = (
|
|
143
|
+
type: string,
|
|
144
|
+
hit: Hit,
|
|
145
|
+
nativeEvent: PointerEvent,
|
|
146
|
+
nodes = path(hit.node),
|
|
147
|
+
) => {
|
|
148
|
+
let stopped = false
|
|
149
|
+
for (const node of nodes) {
|
|
150
|
+
if (dead) break
|
|
151
|
+
const event: ThreePointerEvent = {
|
|
152
|
+
type,
|
|
153
|
+
nativeEvent,
|
|
154
|
+
target: hit.node,
|
|
155
|
+
currentTarget: node,
|
|
156
|
+
intersection: hit.intersection,
|
|
157
|
+
ray: raycaster.ray.clone(),
|
|
158
|
+
pointerId: nativeEvent.pointerId,
|
|
159
|
+
button: nativeEvent.button,
|
|
160
|
+
buttons: nativeEvent.buttons,
|
|
161
|
+
clientX: nativeEvent.clientX,
|
|
162
|
+
clientY: nativeEvent.clientY,
|
|
163
|
+
stopPropagation() {
|
|
164
|
+
stopped = true
|
|
165
|
+
},
|
|
166
|
+
preventDefault() {
|
|
167
|
+
nativeEvent.preventDefault()
|
|
168
|
+
},
|
|
169
|
+
setPointerCapture() {
|
|
170
|
+
canvas.setPointerCapture(nativeEvent.pointerId)
|
|
171
|
+
captures.set(nativeEvent.pointerId, {
|
|
172
|
+
node,
|
|
173
|
+
intersection: hit.intersection,
|
|
174
|
+
})
|
|
175
|
+
},
|
|
176
|
+
releasePointerCapture() {
|
|
177
|
+
if (captures.get(nativeEvent.pointerId)?.node === node) {
|
|
178
|
+
captures.delete(nativeEvent.pointerId)
|
|
179
|
+
nativeCapture(nativeEvent.pointerId)
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
hasPointerCapture() {
|
|
183
|
+
return captures.get(nativeEvent.pointerId)?.node === node
|
|
184
|
+
},
|
|
185
|
+
}
|
|
186
|
+
for (const handler of [...(listeners.get(node)?.get(type) ?? [])])
|
|
187
|
+
if (!dead && listeners.get(node)?.get(type)?.has(handler)) handler(event)
|
|
188
|
+
if (stopped) break
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
const hover = (event: PointerEvent, next?: Hit) => {
|
|
192
|
+
const previous = hovering.get(event.pointerId)
|
|
193
|
+
if (previous?.node === next?.node) {
|
|
194
|
+
if (next) hovering.set(event.pointerId, next)
|
|
195
|
+
return
|
|
196
|
+
}
|
|
197
|
+
const oldPath = previous ? path(previous.node) : []
|
|
198
|
+
const newPath = next ? path(next.node) : []
|
|
199
|
+
// Publish state first so handlers may remove nodes or dispose this manager safely.
|
|
200
|
+
if (next) hovering.set(event.pointerId, next)
|
|
201
|
+
else hovering.delete(event.pointerId)
|
|
202
|
+
if (previous) {
|
|
203
|
+
dispatch(`pointerout`, previous, event)
|
|
204
|
+
for (const node of oldPath)
|
|
205
|
+
if (!newPath.includes(node))
|
|
206
|
+
dispatch(`pointerleave`, previous, event, [node])
|
|
207
|
+
}
|
|
208
|
+
if (next && path(next.node).length) {
|
|
209
|
+
dispatch(`pointerover`, next, event)
|
|
210
|
+
for (const node of newPath.reverse())
|
|
211
|
+
if (!oldPath.includes(node))
|
|
212
|
+
dispatch(`pointerenter`, next, event, [node])
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
const cancel = (event: PointerEvent) => {
|
|
216
|
+
const hit = captures.get(event.pointerId) ?? pressed.get(event.pointerId)
|
|
217
|
+
captures.delete(event.pointerId)
|
|
218
|
+
pressed.delete(event.pointerId)
|
|
219
|
+
try {
|
|
220
|
+
if (hit) dispatch(`pointercancel`, hit, event)
|
|
221
|
+
} finally {
|
|
222
|
+
nativeCapture(event.pointerId)
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
const handle = (event: PointerEvent) => {
|
|
226
|
+
try {
|
|
227
|
+
if (event.type === `lostpointercapture`) {
|
|
228
|
+
cancel(event)
|
|
229
|
+
return
|
|
230
|
+
}
|
|
231
|
+
if (event.type === `pointercancel`) {
|
|
232
|
+
cancel(event)
|
|
233
|
+
hover(event)
|
|
234
|
+
last.delete(event.pointerId)
|
|
235
|
+
return
|
|
236
|
+
}
|
|
237
|
+
if (event.type === `pointerleave`) {
|
|
238
|
+
last.set(event.pointerId, event)
|
|
239
|
+
hover(event)
|
|
240
|
+
return
|
|
241
|
+
}
|
|
242
|
+
last.set(event.pointerId, event)
|
|
243
|
+
const hit = pick(event)
|
|
244
|
+
hover(event, hit)
|
|
245
|
+
if (dead) return
|
|
246
|
+
const capture = captures.get(event.pointerId)
|
|
247
|
+
if (capture && hit && contains(capture.node, hit.node))
|
|
248
|
+
capture.intersection = hit.intersection
|
|
249
|
+
const delivery = capture ?? hit
|
|
250
|
+
if (event.type === `pointerdown`) {
|
|
251
|
+
if (hit) {
|
|
252
|
+
pressed.set(event.pointerId, hit)
|
|
253
|
+
canvas.setPointerCapture?.(event.pointerId)
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
if (event.type === `pointerup`) {
|
|
257
|
+
const down = pressed.get(event.pointerId)
|
|
258
|
+
pressed.delete(event.pointerId)
|
|
259
|
+
if (down && down.node !== delivery?.node)
|
|
260
|
+
dispatch(`pointercancel`, down, event)
|
|
261
|
+
if (delivery) dispatch(`pointerup`, delivery, event)
|
|
262
|
+
if (down && down.node === hit?.node && event.button === 0)
|
|
263
|
+
dispatch(`click`, hit, event)
|
|
264
|
+
captures.delete(event.pointerId)
|
|
265
|
+
nativeCapture(event.pointerId)
|
|
266
|
+
if (event.pointerType === `touch`) {
|
|
267
|
+
hover(event)
|
|
268
|
+
last.delete(event.pointerId)
|
|
269
|
+
}
|
|
270
|
+
} else if (delivery) dispatch(event.type, delivery, event)
|
|
271
|
+
} catch (error) {
|
|
272
|
+
onError(error)
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
const events = [
|
|
276
|
+
`pointerdown`,
|
|
277
|
+
`pointerup`,
|
|
278
|
+
`pointermove`,
|
|
279
|
+
`pointerleave`,
|
|
280
|
+
`pointercancel`,
|
|
281
|
+
`lostpointercapture`,
|
|
282
|
+
] as const
|
|
283
|
+
for (const type of events) canvas.addEventListener(type, handle)
|
|
284
|
+
const manager = {
|
|
285
|
+
/** Re-pick stationary pointers after camera, geometry, or transforms change. */
|
|
286
|
+
update() {
|
|
287
|
+
if (dead) return
|
|
288
|
+
try {
|
|
289
|
+
for (const event of last.values())
|
|
290
|
+
if (event.type !== `pointerleave`) hover(event, pick(event))
|
|
291
|
+
} catch (error) {
|
|
292
|
+
onError(error)
|
|
293
|
+
}
|
|
294
|
+
},
|
|
295
|
+
remove(node: THREE.Object3D) {
|
|
296
|
+
const errors: unknown[] = []
|
|
297
|
+
for (const [id, hit] of hovering)
|
|
298
|
+
if (contains(node, hit.node)) {
|
|
299
|
+
try {
|
|
300
|
+
hover(last.get(id)!)
|
|
301
|
+
} catch (error) {
|
|
302
|
+
errors.push(error)
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
for (const [id, hit] of [...pressed, ...captures])
|
|
306
|
+
if (contains(node, hit.node)) {
|
|
307
|
+
try {
|
|
308
|
+
cancel(last.get(id)!)
|
|
309
|
+
} catch (error) {
|
|
310
|
+
errors.push(error)
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
if (errors.length) throw new AggregateError(errors)
|
|
314
|
+
},
|
|
315
|
+
dispose() {
|
|
316
|
+
if (dead) return
|
|
317
|
+
dead = true
|
|
318
|
+
for (const type of events) canvas.removeEventListener(type, handle)
|
|
319
|
+
for (const id of new Set([...pressed.keys(), ...captures.keys()]))
|
|
320
|
+
nativeCapture(id)
|
|
321
|
+
hovering.clear()
|
|
322
|
+
pressed.clear()
|
|
323
|
+
captures.clear()
|
|
324
|
+
last.clear()
|
|
325
|
+
managers.delete(manager)
|
|
326
|
+
},
|
|
327
|
+
}
|
|
328
|
+
managers.add(manager)
|
|
329
|
+
return manager
|
|
330
|
+
}
|