@firetable/project-xiaochun 0.1.14 → 0.1.15
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-CN.md +105 -17
- package/README.md +105 -17
- package/dist/avatar-element.d.ts +23 -6
- package/dist/chunks/{chunk-DUU5PTKT.js → chunk-KKRZ3SUD.js} +50 -2
- package/dist/chunks/chunk-KKRZ3SUD.js.map +7 -0
- package/dist/chunks/chunk-KQSKHPVF.js +957 -0
- package/dist/chunks/chunk-KQSKHPVF.js.map +7 -0
- package/dist/chunks/{chunk-HLVD6HQM.js → chunk-T6EHFEEZ.js} +81 -9
- package/dist/chunks/chunk-T6EHFEEZ.js.map +7 -0
- package/dist/client.d.ts +127 -6
- package/dist/element.cjs +538 -74
- package/dist/element.cjs.map +4 -4
- package/dist/element.js +4 -4
- package/dist/gesture-box.d.ts +72 -0
- package/dist/index.cjs +565 -74
- package/dist/index.cjs.map +4 -4
- package/dist/index.d.ts +1 -1
- package/dist/index.js +22 -4
- package/dist/index.js.map +1 -1
- package/dist/loader.global.js +2 -2
- package/dist/loader.global.js.map +4 -4
- package/dist/protocol.cjs +49 -1
- package/dist/protocol.cjs.map +2 -2
- package/dist/protocol.d.ts +178 -9
- package/dist/protocol.js +20 -2
- package/dist/react.cjs +507 -80
- package/dist/react.cjs.map +4 -4
- package/dist/react.d.ts +17 -1
- package/dist/react.js +49 -15
- package/dist/react.js.map +2 -2
- package/package.json +5 -3
- package/dist/chunks/chunk-DUU5PTKT.js.map +0 -7
- package/dist/chunks/chunk-HLVD6HQM.js.map +0 -7
- package/dist/chunks/chunk-OLVSOJML.js +0 -579
- package/dist/chunks/chunk-OLVSOJML.js.map +0 -7
|
@@ -1,579 +0,0 @@
|
|
|
1
|
-
/*! @firetable/project-xiaochun v0.1.14 | MIT | https://github.com/FireTable/project-xiaochun */
|
|
2
|
-
import {
|
|
3
|
-
XC_DEFAULT_SRC,
|
|
4
|
-
isXcEnvelope,
|
|
5
|
-
normalizeOrigin,
|
|
6
|
-
xcMessage
|
|
7
|
-
} from "./chunk-DUU5PTKT.js";
|
|
8
|
-
|
|
9
|
-
// src/client.ts
|
|
10
|
-
var FALLBACK_PLACEHOLDER = "data:image/svg+xml;utf8," + encodeURIComponent(
|
|
11
|
-
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 120 180"><defs><linearGradient id="g" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#f6b8ae"/><stop offset="1" stop-color="#ea8377"/></linearGradient></defs><ellipse cx="60" cy="170" rx="34" ry="6" fill="#000" opacity=".12"/><circle cx="60" cy="48" r="26" fill="url(#g)" opacity=".75"/><path d="M26 160c2-40 18-62 34-62s32 22 34 62z" fill="url(#g)" opacity=".6"/></svg>'
|
|
12
|
-
);
|
|
13
|
-
var css = (v, d) => v === void 0 ? d : typeof v === "number" ? `${v}px` : v;
|
|
14
|
-
function createXiaochun(options) {
|
|
15
|
-
if (typeof window === "undefined" || typeof document === "undefined") {
|
|
16
|
-
throw new Error("[project-xiaochun] createXiaochun must run in a browser");
|
|
17
|
-
}
|
|
18
|
-
const container = typeof options.container === "string" ? document.querySelector(options.container) : options.container;
|
|
19
|
-
if (!container) throw new Error(`[project-xiaochun] container not found: ${String(options.container)}`);
|
|
20
|
-
const src = options.src ?? XC_DEFAULT_SRC;
|
|
21
|
-
let srcUrl;
|
|
22
|
-
try {
|
|
23
|
-
srcUrl = new URL(src, window.location.href);
|
|
24
|
-
} catch {
|
|
25
|
-
throw new Error(`[project-xiaochun] invalid src: ${src}`);
|
|
26
|
-
}
|
|
27
|
-
const frameOrigin = normalizeOrigin(options.origin ?? srcUrl.origin);
|
|
28
|
-
if (!frameOrigin) throw new Error("[project-xiaochun] invalid origin (wildcards are not allowed)");
|
|
29
|
-
const trusted = /* @__PURE__ */ new Set([frameOrigin]);
|
|
30
|
-
for (const o of options.allowedOrigins ?? []) {
|
|
31
|
-
const n = normalizeOrigin(o);
|
|
32
|
-
if (!n) throw new Error(`[project-xiaochun] invalid allowedOrigins entry: ${o} (wildcards are not allowed)`);
|
|
33
|
-
trusted.add(n);
|
|
34
|
-
}
|
|
35
|
-
const transparent = options.transparent ?? false;
|
|
36
|
-
const passthrough = options.passthrough ?? transparent;
|
|
37
|
-
const lazy = options.lazy ?? true;
|
|
38
|
-
const position = options.position ?? "inline";
|
|
39
|
-
const handshakeTimeout = options.handshakeTimeout ?? 2e4;
|
|
40
|
-
const wrapper = document.createElement("div");
|
|
41
|
-
wrapper.setAttribute("data-xiaochun", "");
|
|
42
|
-
const ws = wrapper.style;
|
|
43
|
-
ws.position = position === "inline" ? "relative" : "fixed";
|
|
44
|
-
ws.width = css(options.width, "320px");
|
|
45
|
-
ws.height = css(options.height, "480px");
|
|
46
|
-
ws.maxWidth = "100%";
|
|
47
|
-
ws.overflow = "visible";
|
|
48
|
-
ws.contain = "layout style";
|
|
49
|
-
ws.borderRadius = "var(--xc-radius, 0)";
|
|
50
|
-
ws.boxShadow = "var(--xc-shadow, none)";
|
|
51
|
-
if (!transparent) ws.background = "var(--xc-bg, transparent)";
|
|
52
|
-
if (position === "bottom-right") {
|
|
53
|
-
ws.right = "var(--xc-offset-x, 16px)";
|
|
54
|
-
ws.bottom = "var(--xc-offset-y, 16px)";
|
|
55
|
-
}
|
|
56
|
-
if (position === "bottom-left") {
|
|
57
|
-
ws.left = "var(--xc-offset-x, 16px)";
|
|
58
|
-
ws.bottom = "var(--xc-offset-y, 16px)";
|
|
59
|
-
}
|
|
60
|
-
if (position !== "inline") ws.zIndex = `var(--xc-z-index, ${options.zIndex ?? 2147483e3})`;
|
|
61
|
-
ws.pointerEvents = "none";
|
|
62
|
-
wrapper.setAttribute("part", "wrapper");
|
|
63
|
-
container.appendChild(wrapper);
|
|
64
|
-
let placeholderEl = null;
|
|
65
|
-
if (options.placeholder !== false) {
|
|
66
|
-
if (options.placeholder instanceof HTMLElement) placeholderEl = options.placeholder;
|
|
67
|
-
else {
|
|
68
|
-
const img = document.createElement("img");
|
|
69
|
-
img.src = options.placeholder || FALLBACK_PLACEHOLDER;
|
|
70
|
-
img.alt = "Project XiaoChun";
|
|
71
|
-
img.decoding = "async";
|
|
72
|
-
img.width = 320;
|
|
73
|
-
img.height = 480;
|
|
74
|
-
placeholderEl = img;
|
|
75
|
-
}
|
|
76
|
-
const ps = placeholderEl.style;
|
|
77
|
-
placeholderEl.setAttribute("part", "placeholder");
|
|
78
|
-
ps.borderRadius = "var(--xc-radius, 0)";
|
|
79
|
-
ps.position = "absolute";
|
|
80
|
-
ps.inset = "0";
|
|
81
|
-
ps.width = "100%";
|
|
82
|
-
ps.height = "100%";
|
|
83
|
-
ps.objectFit = "contain";
|
|
84
|
-
ps.transition = "opacity .25s ease";
|
|
85
|
-
ps.pointerEvents = "auto";
|
|
86
|
-
ps.cursor = lazy === false ? "default" : "pointer";
|
|
87
|
-
placeholderEl.addEventListener("click", () => activate());
|
|
88
|
-
wrapper.appendChild(placeholderEl);
|
|
89
|
-
}
|
|
90
|
-
const listeners = /* @__PURE__ */ new Map();
|
|
91
|
-
const emit = (ev, payload) => {
|
|
92
|
-
listeners.get(ev)?.forEach((cb) => {
|
|
93
|
-
try {
|
|
94
|
-
cb(payload);
|
|
95
|
-
} catch (e) {
|
|
96
|
-
console.error("[project-xiaochun] listener error", e);
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
};
|
|
100
|
-
let resolveReady;
|
|
101
|
-
let rejectReady;
|
|
102
|
-
const ready = new Promise((res, rej) => {
|
|
103
|
-
resolveReady = res;
|
|
104
|
-
rejectReady = rej;
|
|
105
|
-
});
|
|
106
|
-
ready.catch(() => {
|
|
107
|
-
});
|
|
108
|
-
let iframe = null;
|
|
109
|
-
let port = null;
|
|
110
|
-
let destroyed = false;
|
|
111
|
-
let userPaused = false;
|
|
112
|
-
let autoPaused = false;
|
|
113
|
-
let loaded = false;
|
|
114
|
-
let lastHit = false;
|
|
115
|
-
const queue = [];
|
|
116
|
-
const pending = /* @__PURE__ */ new Map();
|
|
117
|
-
let idSeq = 0;
|
|
118
|
-
let handshakeTimer = null;
|
|
119
|
-
let io = null;
|
|
120
|
-
let idleHandle = null;
|
|
121
|
-
const postToFrame = (type, payload, id, transfer) => {
|
|
122
|
-
if (!port) {
|
|
123
|
-
queue.push({ type, payload, id, transfer });
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
port.postMessage(xcMessage(type, payload, id), transfer ?? []);
|
|
127
|
-
};
|
|
128
|
-
const send = (type, payload, wait = false, transfer, fixedId) => {
|
|
129
|
-
if (destroyed) return Promise.reject(new Error("[project-xiaochun] instance destroyed"));
|
|
130
|
-
if (!iframe) activate();
|
|
131
|
-
const id = fixedId ?? `c${++idSeq}`;
|
|
132
|
-
const p = wait ? new Promise((resolve, reject) => pending.set(id, { resolve, reject, sawEnd: false })) : Promise.resolve();
|
|
133
|
-
postToFrame(type, payload, id, transfer);
|
|
134
|
-
return p;
|
|
135
|
-
};
|
|
136
|
-
const toBuffer = (d) => d instanceof ArrayBuffer ? d : d.buffer.slice(d.byteOffset, d.byteOffset + d.byteLength);
|
|
137
|
-
const bindAbort = (signal, id) => {
|
|
138
|
-
if (!signal) return;
|
|
139
|
-
const abort = () => {
|
|
140
|
-
if (!destroyed) postToFrame("xc.audio.end", { abort: true }, id);
|
|
141
|
-
};
|
|
142
|
-
if (signal.aborted) abort();
|
|
143
|
-
else signal.addEventListener("abort", abort, { once: true });
|
|
144
|
-
};
|
|
145
|
-
const speakAudio = async (source, opts = {}) => {
|
|
146
|
-
if (destroyed) throw new Error("[project-xiaochun] instance destroyed");
|
|
147
|
-
if (!iframe) activate();
|
|
148
|
-
const { signal, transfer, fetch: fetchWhere, ...rest } = opts;
|
|
149
|
-
const payload = { ...rest };
|
|
150
|
-
const list = [];
|
|
151
|
-
if (typeof source === "string") {
|
|
152
|
-
if (fetchWhere === "frame") payload.source = source;
|
|
153
|
-
else {
|
|
154
|
-
const res = await fetch(source);
|
|
155
|
-
if (!res.ok) throw new Error(`[project-xiaochun] audio fetch failed: HTTP ${res.status}`);
|
|
156
|
-
const buf = await res.arrayBuffer();
|
|
157
|
-
payload.source = buf;
|
|
158
|
-
list.push(buf);
|
|
159
|
-
payload.mimeType ?? (payload.mimeType = res.headers.get("content-type") ?? void 0);
|
|
160
|
-
}
|
|
161
|
-
} else if (typeof Blob !== "undefined" && source instanceof Blob) {
|
|
162
|
-
payload.source = source;
|
|
163
|
-
payload.mimeType ?? (payload.mimeType = source.type || void 0);
|
|
164
|
-
} else {
|
|
165
|
-
const buf = toBuffer(source);
|
|
166
|
-
payload.source = buf;
|
|
167
|
-
if (!(source instanceof ArrayBuffer) || transfer !== false) list.push(buf);
|
|
168
|
-
}
|
|
169
|
-
if (destroyed) throw new Error("[project-xiaochun] instance destroyed");
|
|
170
|
-
const id = `c${++idSeq}`;
|
|
171
|
-
const done = send("xc.audio", payload, true, list, id);
|
|
172
|
-
bindAbort(signal, id);
|
|
173
|
-
return done;
|
|
174
|
-
};
|
|
175
|
-
const speakAudioStream = (opts) => {
|
|
176
|
-
if (destroyed) throw new Error("[project-xiaochun] instance destroyed");
|
|
177
|
-
const { signal, transfer, fetch: _f, mimeType: _m, ...rest } = opts;
|
|
178
|
-
const id = `c${++idSeq}`;
|
|
179
|
-
let started = false;
|
|
180
|
-
let ended = false;
|
|
181
|
-
let fmt = rest.format === "pcm16" || rest.format === "float32" ? rest.format : null;
|
|
182
|
-
let done = Promise.resolve();
|
|
183
|
-
const handle2 = {
|
|
184
|
-
write(data) {
|
|
185
|
-
if (ended) throw new Error("[project-xiaochun] audio stream already ended");
|
|
186
|
-
if (destroyed) throw new Error("[project-xiaochun] instance destroyed");
|
|
187
|
-
if (data instanceof Int16Array) fmt ?? (fmt = "pcm16");
|
|
188
|
-
else if (data instanceof Float32Array) fmt ?? (fmt = "float32");
|
|
189
|
-
if (!fmt) throw new Error("[project-xiaochun] speakAudioStream: set opts.format when writing raw ArrayBuffers");
|
|
190
|
-
const buf = toBuffer(data);
|
|
191
|
-
const body = { data: buf, format: fmt, sampleRate: rest.sampleRate, channels: rest.channels };
|
|
192
|
-
if (!started) {
|
|
193
|
-
started = true;
|
|
194
|
-
body.text = rest.text;
|
|
195
|
-
body.motion = rest.motion;
|
|
196
|
-
body.lipsync = rest.lipsync;
|
|
197
|
-
done = send("xc.audio.chunk", body, true, [buf], id);
|
|
198
|
-
bindAbort(signal, id);
|
|
199
|
-
} else {
|
|
200
|
-
postToFrame("xc.audio.chunk", body, id, !(data instanceof ArrayBuffer) || transfer !== false ? [buf] : []);
|
|
201
|
-
}
|
|
202
|
-
},
|
|
203
|
-
end() {
|
|
204
|
-
if (ended) return;
|
|
205
|
-
ended = true;
|
|
206
|
-
if (started) postToFrame("xc.audio.end", {}, id);
|
|
207
|
-
},
|
|
208
|
-
abort() {
|
|
209
|
-
if (ended && !started) return;
|
|
210
|
-
ended = true;
|
|
211
|
-
if (started) postToFrame("xc.audio.end", { abort: true }, id);
|
|
212
|
-
},
|
|
213
|
-
get done() {
|
|
214
|
-
return done;
|
|
215
|
-
}
|
|
216
|
-
};
|
|
217
|
-
return handle2;
|
|
218
|
-
};
|
|
219
|
-
function buildUrl() {
|
|
220
|
-
const u = new URL(srcUrl.href);
|
|
221
|
-
u.searchParams.set("host", window.location.origin);
|
|
222
|
-
if (transparent) u.searchParams.set("transparent", "1");
|
|
223
|
-
if (options.ui) u.searchParams.set("ui", "1");
|
|
224
|
-
if (options.lang) u.searchParams.set("lang", options.lang);
|
|
225
|
-
if (options.heavy) u.searchParams.set("heavy", options.heavy);
|
|
226
|
-
if (options.controls) u.searchParams.set("controls", "1");
|
|
227
|
-
if (options.model) u.searchParams.set("outfit", options.model);
|
|
228
|
-
return u.href;
|
|
229
|
-
}
|
|
230
|
-
function activate() {
|
|
231
|
-
if (destroyed || iframe) return;
|
|
232
|
-
cancelIdle();
|
|
233
|
-
io?.disconnect();
|
|
234
|
-
io = null;
|
|
235
|
-
const f = document.createElement("iframe");
|
|
236
|
-
f.title = "Project XiaoChun";
|
|
237
|
-
f.loading = "lazy";
|
|
238
|
-
f.allow = options.crossOriginIsolated === true ? "microphone; autoplay; cross-origin-isolated" : "microphone; autoplay";
|
|
239
|
-
f.referrerPolicy = "strict-origin-when-cross-origin";
|
|
240
|
-
if (options.sandbox !== false) {
|
|
241
|
-
f.setAttribute("sandbox", options.sandbox ?? "allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox");
|
|
242
|
-
}
|
|
243
|
-
f.setAttribute("allowtransparency", "true");
|
|
244
|
-
f.setAttribute("scrolling", "no");
|
|
245
|
-
const s = f.style;
|
|
246
|
-
s.position = "absolute";
|
|
247
|
-
s.inset = "0";
|
|
248
|
-
s.width = "100%";
|
|
249
|
-
s.height = "100%";
|
|
250
|
-
s.border = "0";
|
|
251
|
-
s.background = "transparent";
|
|
252
|
-
s.colorScheme = "normal";
|
|
253
|
-
s.borderRadius = "var(--xc-radius, 0)";
|
|
254
|
-
f.setAttribute("part", "iframe");
|
|
255
|
-
s.opacity = "0";
|
|
256
|
-
s.transition = "opacity .25s ease";
|
|
257
|
-
s.pointerEvents = passthrough ? "none" : "auto";
|
|
258
|
-
f.src = buildUrl();
|
|
259
|
-
iframe = f;
|
|
260
|
-
wrapper.appendChild(f);
|
|
261
|
-
if (placeholderEl) placeholderEl.style.cursor = "default";
|
|
262
|
-
handshakeTimer = setTimeout(() => {
|
|
263
|
-
if (!port) emit("error", { code: "timeout", message: `no xc.ready from ${frameOrigin} within ${handshakeTimeout}ms` });
|
|
264
|
-
}, handshakeTimeout);
|
|
265
|
-
setupAutoPause();
|
|
266
|
-
setupPassthrough();
|
|
267
|
-
}
|
|
268
|
-
const onWindowMessage = (ev) => {
|
|
269
|
-
if (destroyed || !iframe || ev.source !== iframe.contentWindow) return;
|
|
270
|
-
if (!trusted.has(ev.origin)) {
|
|
271
|
-
emit("error", { code: "origin_denied", message: `ignored message from untrusted origin ${ev.origin}` });
|
|
272
|
-
return;
|
|
273
|
-
}
|
|
274
|
-
const data = ev.data;
|
|
275
|
-
if (!isXcEnvelope(data) || data.type !== "xc.ready" || port) return;
|
|
276
|
-
const ch = new MessageChannel();
|
|
277
|
-
port = ch.port1;
|
|
278
|
-
port.onmessage = onPortMessage;
|
|
279
|
-
iframe.contentWindow.postMessage(
|
|
280
|
-
xcMessage("xc.init", { hostOrigin: window.location.origin }),
|
|
281
|
-
ev.origin,
|
|
282
|
-
[ch.port2]
|
|
283
|
-
);
|
|
284
|
-
if (handshakeTimer) {
|
|
285
|
-
clearTimeout(handshakeTimer);
|
|
286
|
-
handshakeTimer = null;
|
|
287
|
-
}
|
|
288
|
-
emit("handshake", data.payload);
|
|
289
|
-
for (const m of queue.splice(0)) port.postMessage(xcMessage(m.type, m.payload, m.id), m.transfer ?? []);
|
|
290
|
-
syncPause();
|
|
291
|
-
};
|
|
292
|
-
window.addEventListener("message", onWindowMessage);
|
|
293
|
-
function onPortMessage(ev) {
|
|
294
|
-
const data = ev.data;
|
|
295
|
-
if (!isXcEnvelope(data)) return;
|
|
296
|
-
switch (data.type) {
|
|
297
|
-
case "xc.load.progress":
|
|
298
|
-
emit("progress", data.payload);
|
|
299
|
-
break;
|
|
300
|
-
case "xc.loaded":
|
|
301
|
-
if (!loaded) {
|
|
302
|
-
loaded = true;
|
|
303
|
-
if (iframe) iframe.style.opacity = "1";
|
|
304
|
-
if (placeholderEl) {
|
|
305
|
-
const el = placeholderEl;
|
|
306
|
-
el.style.opacity = "0";
|
|
307
|
-
el.style.pointerEvents = "none";
|
|
308
|
-
setTimeout(() => el.remove(), 300);
|
|
309
|
-
placeholderEl = null;
|
|
310
|
-
}
|
|
311
|
-
resolveReady();
|
|
312
|
-
}
|
|
313
|
-
emit("ready", data.payload);
|
|
314
|
-
break;
|
|
315
|
-
case "xc.state":
|
|
316
|
-
emit("state", data.payload);
|
|
317
|
-
break;
|
|
318
|
-
case "xc.stt":
|
|
319
|
-
emit("stt", data.payload);
|
|
320
|
-
break;
|
|
321
|
-
case "xc.utterance": {
|
|
322
|
-
emit("utterance", data.payload);
|
|
323
|
-
const p = data.id ? pending.get(data.id) : void 0;
|
|
324
|
-
if (p && data.payload.phase === "end") {
|
|
325
|
-
pending.delete(data.id);
|
|
326
|
-
p.resolve();
|
|
327
|
-
}
|
|
328
|
-
break;
|
|
329
|
-
}
|
|
330
|
-
case "xc.hit-region": {
|
|
331
|
-
const { hit } = data.payload;
|
|
332
|
-
emit("hit-region", data.payload);
|
|
333
|
-
if (passthrough && iframe && hit !== lastHit) {
|
|
334
|
-
lastHit = hit;
|
|
335
|
-
iframe.style.pointerEvents = hit ? "auto" : "none";
|
|
336
|
-
}
|
|
337
|
-
break;
|
|
338
|
-
}
|
|
339
|
-
case "xc.error": {
|
|
340
|
-
const { code, message, command } = data.payload;
|
|
341
|
-
emit("error", { code, message, command });
|
|
342
|
-
const p = data.id ? pending.get(data.id) : void 0;
|
|
343
|
-
if (p && data.id) {
|
|
344
|
-
pending.delete(data.id);
|
|
345
|
-
p.reject(new Error(`[${code}] ${message}`));
|
|
346
|
-
}
|
|
347
|
-
break;
|
|
348
|
-
}
|
|
349
|
-
default:
|
|
350
|
-
break;
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
const POINTER_MIN_INTERVAL_MS = 32;
|
|
354
|
-
const POINTER_MIN_MOVE_PX = 2;
|
|
355
|
-
let pointerRaf = 0;
|
|
356
|
-
let lastPtr = null;
|
|
357
|
-
let lastPosted = null;
|
|
358
|
-
let rectCache = null;
|
|
359
|
-
let rectRo = null;
|
|
360
|
-
let rectAt = 0;
|
|
361
|
-
const invalidateRect = () => {
|
|
362
|
-
rectCache = null;
|
|
363
|
-
};
|
|
364
|
-
const getRect = () => {
|
|
365
|
-
if (!iframe) return null;
|
|
366
|
-
const now = performance.now();
|
|
367
|
-
if (!rectCache || now - rectAt > 400) {
|
|
368
|
-
rectCache = iframe.getBoundingClientRect();
|
|
369
|
-
rectAt = now;
|
|
370
|
-
}
|
|
371
|
-
return rectCache;
|
|
372
|
-
};
|
|
373
|
-
const onHostPointer = (e) => {
|
|
374
|
-
if (!iframe || !port || iframe.style.pointerEvents === "auto") return;
|
|
375
|
-
lastPtr = { x: e.clientX, y: e.clientY };
|
|
376
|
-
if (pointerRaf) return;
|
|
377
|
-
pointerRaf = requestAnimationFrame(() => {
|
|
378
|
-
pointerRaf = 0;
|
|
379
|
-
if (!iframe || !port || !lastPtr) return;
|
|
380
|
-
const r = getRect();
|
|
381
|
-
if (!r) return;
|
|
382
|
-
const x = lastPtr.x - r.left, y = lastPtr.y - r.top;
|
|
383
|
-
if (x < 0 || y < 0 || x > r.width || y > r.height) {
|
|
384
|
-
lastPosted = null;
|
|
385
|
-
if (lastHit) {
|
|
386
|
-
lastHit = false;
|
|
387
|
-
iframe.style.pointerEvents = "none";
|
|
388
|
-
}
|
|
389
|
-
return;
|
|
390
|
-
}
|
|
391
|
-
const now = performance.now();
|
|
392
|
-
if (lastPosted && now - lastPosted.t < POINTER_MIN_INTERVAL_MS) return;
|
|
393
|
-
if (lastPosted && Math.abs(x - lastPosted.x) < POINTER_MIN_MOVE_PX && Math.abs(y - lastPosted.y) < POINTER_MIN_MOVE_PX) return;
|
|
394
|
-
lastPosted = { x, y, t: now };
|
|
395
|
-
port.postMessage(xcMessage("xc.pointer", { x, y }));
|
|
396
|
-
});
|
|
397
|
-
};
|
|
398
|
-
function setupPassthrough() {
|
|
399
|
-
if (!passthrough) return;
|
|
400
|
-
window.addEventListener("pointermove", onHostPointer, { passive: true });
|
|
401
|
-
window.addEventListener("scroll", invalidateRect, { passive: true, capture: true });
|
|
402
|
-
window.addEventListener("resize", invalidateRect, { passive: true });
|
|
403
|
-
if (typeof ResizeObserver !== "undefined" && iframe) {
|
|
404
|
-
rectRo = new ResizeObserver(invalidateRect);
|
|
405
|
-
rectRo.observe(iframe);
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
let pauseIo = null;
|
|
409
|
-
function setupAutoPause() {
|
|
410
|
-
if (options.autoPause === false || typeof IntersectionObserver === "undefined") return;
|
|
411
|
-
pauseIo = new IntersectionObserver((entries) => {
|
|
412
|
-
const visible = entries[entries.length - 1]?.isIntersecting ?? true;
|
|
413
|
-
autoPaused = !visible;
|
|
414
|
-
syncPause();
|
|
415
|
-
});
|
|
416
|
-
pauseIo.observe(wrapper);
|
|
417
|
-
}
|
|
418
|
-
let sentPaused = false;
|
|
419
|
-
function syncPause() {
|
|
420
|
-
if (!port) return;
|
|
421
|
-
const want = userPaused || autoPaused;
|
|
422
|
-
if (want === sentPaused) return;
|
|
423
|
-
sentPaused = want;
|
|
424
|
-
port.postMessage(xcMessage(want ? "xc.pause" : "xc.resume"));
|
|
425
|
-
}
|
|
426
|
-
function cancelIdle() {
|
|
427
|
-
if (idleHandle === null) return;
|
|
428
|
-
const w = window;
|
|
429
|
-
if (w.cancelIdleCallback) w.cancelIdleCallback(idleHandle);
|
|
430
|
-
else clearTimeout(idleHandle);
|
|
431
|
-
idleHandle = null;
|
|
432
|
-
}
|
|
433
|
-
function scheduleLazy() {
|
|
434
|
-
if (lazy === false) {
|
|
435
|
-
activate();
|
|
436
|
-
return;
|
|
437
|
-
}
|
|
438
|
-
if (lazy === "click") return;
|
|
439
|
-
const go = () => {
|
|
440
|
-
const w = window;
|
|
441
|
-
idleHandle = w.requestIdleCallback ? w.requestIdleCallback(() => {
|
|
442
|
-
idleHandle = null;
|
|
443
|
-
activate();
|
|
444
|
-
}, { timeout: 3e3 }) : setTimeout(activate, 1200);
|
|
445
|
-
};
|
|
446
|
-
if (typeof IntersectionObserver === "undefined") {
|
|
447
|
-
go();
|
|
448
|
-
return;
|
|
449
|
-
}
|
|
450
|
-
io = new IntersectionObserver((entries) => {
|
|
451
|
-
if (entries.some((e) => e.isIntersecting)) {
|
|
452
|
-
io?.disconnect();
|
|
453
|
-
io = null;
|
|
454
|
-
go();
|
|
455
|
-
}
|
|
456
|
-
}, { rootMargin: `${options.lazyMargin ?? 200}px` });
|
|
457
|
-
io.observe(wrapper);
|
|
458
|
-
}
|
|
459
|
-
let handle = null;
|
|
460
|
-
if (options.draggable && position !== "inline") {
|
|
461
|
-
handle = document.createElement("div");
|
|
462
|
-
handle.setAttribute("aria-label", "drag");
|
|
463
|
-
Object.assign(handle.style, {
|
|
464
|
-
position: "absolute",
|
|
465
|
-
left: "0",
|
|
466
|
-
top: "0",
|
|
467
|
-
width: "28px",
|
|
468
|
-
height: "28px",
|
|
469
|
-
cursor: "grab",
|
|
470
|
-
pointerEvents: "auto",
|
|
471
|
-
borderRadius: "8px",
|
|
472
|
-
background: "rgba(0,0,0,.25)",
|
|
473
|
-
color: "#fff",
|
|
474
|
-
font: "14px/28px system-ui",
|
|
475
|
-
textAlign: "center",
|
|
476
|
-
userSelect: "none",
|
|
477
|
-
touchAction: "none",
|
|
478
|
-
zIndex: "2",
|
|
479
|
-
opacity: "0.5"
|
|
480
|
-
});
|
|
481
|
-
handle.textContent = "\u283F";
|
|
482
|
-
let drag = null;
|
|
483
|
-
handle.addEventListener("pointerdown", (e) => {
|
|
484
|
-
const r = wrapper.getBoundingClientRect();
|
|
485
|
-
drag = { dx: e.clientX - r.left, dy: e.clientY - r.top };
|
|
486
|
-
handle.setPointerCapture(e.pointerId);
|
|
487
|
-
});
|
|
488
|
-
handle.addEventListener("pointermove", (e) => {
|
|
489
|
-
if (!drag) return;
|
|
490
|
-
ws.left = `${Math.min(Math.max(0, e.clientX - drag.dx), window.innerWidth - 40)}px`;
|
|
491
|
-
ws.top = `${Math.min(Math.max(0, e.clientY - drag.dy), window.innerHeight - 40)}px`;
|
|
492
|
-
ws.right = "auto";
|
|
493
|
-
ws.bottom = "auto";
|
|
494
|
-
invalidateRect();
|
|
495
|
-
});
|
|
496
|
-
const end = () => {
|
|
497
|
-
drag = null;
|
|
498
|
-
};
|
|
499
|
-
handle.addEventListener("pointerup", end);
|
|
500
|
-
handle.addEventListener("pointercancel", end);
|
|
501
|
-
wrapper.appendChild(handle);
|
|
502
|
-
}
|
|
503
|
-
scheduleLazy();
|
|
504
|
-
const api = {
|
|
505
|
-
element: wrapper,
|
|
506
|
-
get iframe() {
|
|
507
|
-
return iframe;
|
|
508
|
-
},
|
|
509
|
-
ready,
|
|
510
|
-
activate,
|
|
511
|
-
say: (text, opts) => send("xc.say", { text, mode: opts?.mode }, true),
|
|
512
|
-
speakAudio,
|
|
513
|
-
speakAudioStream,
|
|
514
|
-
motion: (m) => send("xc.motion", typeof m === "string" ? m.startsWith("http") || m.startsWith("/") ? { url: m } : { name: m } : m),
|
|
515
|
-
expression: (name) => send("xc.expression", { name }),
|
|
516
|
-
lookAt: (x, y) => send("xc.lookAt", { x, y }),
|
|
517
|
-
setModel: (m) => send("xc.setModel", typeof m === "string" ? /^(https?:)?\/|\.vrm/i.test(m) ? { url: m } : { outfit: m } : m),
|
|
518
|
-
setConfig: (cfg) => send("xc.setConfig", cfg),
|
|
519
|
-
startListening: () => send("xc.mic", { enabled: true }),
|
|
520
|
-
stopListening: () => send("xc.mic", { enabled: false }),
|
|
521
|
-
mic: (enabled) => send("xc.mic", { enabled }),
|
|
522
|
-
pause() {
|
|
523
|
-
userPaused = true;
|
|
524
|
-
syncPause();
|
|
525
|
-
},
|
|
526
|
-
resume() {
|
|
527
|
-
userPaused = false;
|
|
528
|
-
syncPause();
|
|
529
|
-
},
|
|
530
|
-
destroy() {
|
|
531
|
-
if (destroyed) return;
|
|
532
|
-
try {
|
|
533
|
-
port?.postMessage(xcMessage("xc.destroy"));
|
|
534
|
-
} catch {
|
|
535
|
-
}
|
|
536
|
-
destroyed = true;
|
|
537
|
-
cancelIdle();
|
|
538
|
-
io?.disconnect();
|
|
539
|
-
pauseIo?.disconnect();
|
|
540
|
-
if (handshakeTimer) clearTimeout(handshakeTimer);
|
|
541
|
-
if (pointerRaf) cancelAnimationFrame(pointerRaf);
|
|
542
|
-
window.removeEventListener("message", onWindowMessage);
|
|
543
|
-
window.removeEventListener("pointermove", onHostPointer);
|
|
544
|
-
window.removeEventListener("scroll", invalidateRect, true);
|
|
545
|
-
window.removeEventListener("resize", invalidateRect);
|
|
546
|
-
rectRo?.disconnect();
|
|
547
|
-
try {
|
|
548
|
-
port?.close();
|
|
549
|
-
} catch {
|
|
550
|
-
}
|
|
551
|
-
port = null;
|
|
552
|
-
for (const p of pending.values()) p.reject(new Error("[project-xiaochun] instance destroyed"));
|
|
553
|
-
pending.clear();
|
|
554
|
-
queue.length = 0;
|
|
555
|
-
if (!loaded) rejectReady(new Error("[project-xiaochun] destroyed before ready"));
|
|
556
|
-
wrapper.remove();
|
|
557
|
-
iframe = null;
|
|
558
|
-
emit("destroy", void 0);
|
|
559
|
-
listeners.clear();
|
|
560
|
-
},
|
|
561
|
-
on(event, cb) {
|
|
562
|
-
let set = listeners.get(event);
|
|
563
|
-
if (!set) {
|
|
564
|
-
set = /* @__PURE__ */ new Set();
|
|
565
|
-
listeners.set(event, set);
|
|
566
|
-
}
|
|
567
|
-
set.add(cb);
|
|
568
|
-
return () => {
|
|
569
|
-
set.delete(cb);
|
|
570
|
-
};
|
|
571
|
-
}
|
|
572
|
-
};
|
|
573
|
-
return api;
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
export {
|
|
577
|
-
createXiaochun
|
|
578
|
-
};
|
|
579
|
-
//# sourceMappingURL=chunk-OLVSOJML.js.map
|