@yak-io/javascript 0.11.0 → 0.11.1
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/dist/client.d.ts +11 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/embed.d.ts.map +1 -1
- package/dist/index.cjs +22 -4
- package/dist/index.cjs.map +2 -2
- package/dist/index.js +22 -4
- package/dist/index.js.map +2 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -282,7 +282,8 @@ var YakClient = class {
|
|
|
282
282
|
* ```
|
|
283
283
|
*/
|
|
284
284
|
sendPrompt(prompt) {
|
|
285
|
-
|
|
285
|
+
const target = this.getActiveTarget();
|
|
286
|
+
if (!target) {
|
|
286
287
|
logger.warn("Cannot send prompt: iframe not ready");
|
|
287
288
|
return;
|
|
288
289
|
}
|
|
@@ -290,21 +291,37 @@ var YakClient = class {
|
|
|
290
291
|
type: "yak:prompt",
|
|
291
292
|
payload: { prompt }
|
|
292
293
|
};
|
|
293
|
-
|
|
294
|
+
target.window.postMessage(message, target.origin);
|
|
294
295
|
}
|
|
295
296
|
/**
|
|
296
297
|
* Send a focus request to the chatbot iframe
|
|
297
298
|
* This will focus the chat input field
|
|
298
299
|
*/
|
|
299
300
|
sendFocus() {
|
|
300
|
-
|
|
301
|
+
const target = this.getActiveTarget();
|
|
302
|
+
if (!target) {
|
|
301
303
|
logger.warn("Cannot send focus: iframe not ready");
|
|
302
304
|
return;
|
|
303
305
|
}
|
|
304
306
|
const message = {
|
|
305
307
|
type: "yak:focus"
|
|
306
308
|
};
|
|
307
|
-
|
|
309
|
+
target.window.postMessage(message, target.origin);
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* The live postMessage target. Prefers `readyTarget` — the window that
|
|
313
|
+
* completed the `yak:ready` handshake — and falls back to the iframe's
|
|
314
|
+
* `contentWindow`. The two are normally identical, but they're populated by
|
|
315
|
+
* different events (the ready postMessage vs. the DOM `load` event) and can
|
|
316
|
+
* briefly diverge (e.g. a remount under React StrictMode, where `isReady`
|
|
317
|
+
* goes true before — or without — the load event repopulating
|
|
318
|
+
* `iframeWindow`). Trusting `readyTarget` keeps `sendFocus`/`sendPrompt`
|
|
319
|
+
* consistent with `isReady()` and avoids spurious "iframe not ready" warns.
|
|
320
|
+
*/
|
|
321
|
+
getActiveTarget() {
|
|
322
|
+
if (this.readyTarget) return this.readyTarget;
|
|
323
|
+
if (this.iframeWindow) return { window: this.iframeWindow, origin: this.getIframeOrigin() };
|
|
324
|
+
return null;
|
|
308
325
|
}
|
|
309
326
|
/**
|
|
310
327
|
* Check if the iframe is ready to receive messages
|
|
@@ -1616,6 +1633,7 @@ var YakEmbed = class {
|
|
|
1616
1633
|
if (!this.mounted) return;
|
|
1617
1634
|
this.mounted = false;
|
|
1618
1635
|
this.client.unmount();
|
|
1636
|
+
this.client.setIframeWindow(null);
|
|
1619
1637
|
if (this.unsubscribeVoice) {
|
|
1620
1638
|
this.unsubscribeVoice();
|
|
1621
1639
|
this.unsubscribeVoice = null;
|