Skip to content

Commit 8a89e88

Browse files
committed
feat: add onPhoenixEmbeddedInfoAvailable callback and isPhoenixEmbeddedIframe support
Add a callback mechanism to detect when a live preview iframe is embedded within Phoenix vs popped out. The callback is guaranteed to fire in embedded iframes but not in non-embedded frames. Also pass isPhoenixEmbeddedIframe in the WHO_AM_I_RESPONSE handshake.
1 parent c182394 commit 8a89e88

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

src/LiveDevelopment/BrowserScripts/LivePreviewTransportRemote.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,28 @@
441441
let alertQueue = [], confirmCalled = false, promptCalled = false;
442442
let addToQueue = true;
443443
window.__PHOENIX_APP_INFO = {isTauri, platform};
444+
const _embeddedInfoCallbacks = [];
444445
if(!isExternalBrowser){
445446
// this is an embedded iframe we always take hold of the alert api for better ux within the live preivew frame.
446-
window.__PHOENIX_EMBED_INFO = {isTauri, platform};
447+
// __PHOENIX_EMBED_INFO will be present in browser popped out windows as its actually an embedded
448+
// iframe inside phcode.live server, so to detect if its a truly embedded iframe within phoenix code
449+
// itself, rely on the isEmbeddedIframe which will be set with `WHO_AM_I_RESPONSE` handshake
450+
window.__PHOENIX_EMBED_INFO = {isTauri, platform, isPhoenixEmbeddedIframe: undefined};
451+
/**
452+
* Register a callback to be notified when `isPhoenixEmbeddedIframe` is resolved.
453+
* The callback is guaranteed to fire in embedded iframes, but NOT guaranteed to fire
454+
* in non-embedded (popped out) frames. Do not rely on this to confirm non-embedded state;
455+
* instead, assume non-embedded by default and use this only to detect embedded iframes.
456+
* If `isPhoenixEmbeddedIframe` is already determined, the callback fires immediately.
457+
* @param {function(boolean): void} callback - receives `true` if embedded, `false` if popped out.
458+
*/
459+
window.__PHOENIX_EMBED_INFO.onPhoenixEmbeddedInfoAvailable = function (callback) {
460+
if (window.__PHOENIX_EMBED_INFO.isPhoenixEmbeddedIframe !== undefined) {
461+
callback(window.__PHOENIX_EMBED_INFO.isPhoenixEmbeddedIframe);
462+
} else {
463+
_embeddedInfoCallbacks.push(callback);
464+
}
465+
};
447466
const shouldPatchAlert = (isTauri && platform === "mac");
448467
if(shouldPatchAlert){
449468
// In Mac embedded live preview iframe in tauri, alert, prompt, and confirm apis
@@ -493,6 +512,13 @@
493512
// this is set from transport config. We should be here
494513
console.error("Expected window.__PHOENIX_EMBED_INFO to be set, but not???");
495514
}
515+
// all embedded iframes will have this set to distinguish between embedded iframes and popped out iframes
516+
const isPhoenixEmbeddedIframe = !!event.data.isPhoenixEmbeddedIframe;
517+
window.__PHOENIX_EMBED_INFO.isPhoenixEmbeddedIframe = isPhoenixEmbeddedIframe;
518+
for (let i = 0; i < _embeddedInfoCallbacks.length; i++) {
519+
_embeddedInfoCallbacks[i](isPhoenixEmbeddedIframe);
520+
}
521+
_embeddedInfoCallbacks.length = 0;
496522
}
497523
});
498524
if(window.self !== window.parent){

src/extensionsIntegrated/Phoenix-live-preview/main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ define(function (require, exports, module) {
194194
iframeDom.contentWindow.postMessage({
195195
type: "WHO_AM_I_RESPONSE",
196196
isTauri: Phoenix.isNativeApp,
197-
platform: Phoenix.platform
197+
platform: Phoenix.platform,
198+
isPhoenixEmbeddedIframe: true
198199
}, "*"); // this is not sensitive info, and is only dispatched if requested by the iframe
199200
}
200201
});

src/extensionsIntegrated/Phoenix-live-preview/markdown.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
if(event.data.type === "WHO_AM_I_RESPONSE") {
2929
window.__PHOENIX_EMBED_INFO = {
3030
isTauri: event.data.isTauri,
31-
platform: event.data.platform
31+
platform: event.data.platform,
32+
isEmbeddedIframe: event.data.isEmbeddedIframe
3233
};
3334
} else if(event.data.type === "_TEST_FOCUS_CLICK") { // for integ tests
3435
document.body.click();

0 commit comments

Comments
 (0)