Skip to content

Commit 46b7b93

Browse files
committed
fix: intel macs opens tabs twice on clicking html live preview a tags
1 parent 6eea5d6 commit 46b7b93

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/LiveDevelopment/BrowserScripts/LivePreviewTransportRemote.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,25 +298,51 @@
298298
targetElement = targetElement.parentElement;
299299
}
300300

301-
if (targetElement && targetElement.tagName === 'A' && (targetElement.target === '_blank')) {
301+
if (window.__PHOENIX_EMBED_INFO && window.__PHOENIX_EMBED_INFO.isTauri &&
302+
targetElement && targetElement.tagName === 'A' && (targetElement.target === '_blank')) {
303+
// in desktop phoenix builds, tauri will not open anchor tags in browser if it is in
304+
// an iframe(except for the intel mac bug)
305+
// in normal browsers, we dont need to do this and the borwser will do its thing.
302306
const href = getAbsoluteUrl(targetElement.getAttribute('href'));
303307
window.parent.postMessage({
304308
handlerName: "ph-liveServer",
305309
eventName: 'embeddedIframeHrefClick',
306310
href: href
307311
}, "*");
312+
event.preventDefault(); // in intel mac desktop, tauri seems to open in browser
313+
// causing 2 tabs to open. in m1 macs its not there. so we prevent default behavior.
308314
}
309315
});
310316
document.addEventListener('contextmenu', function(event) {
311317
(document.activeElement || document.body).focus();
312318
});
313319
document.addEventListener('keydown', function(event) {
314-
if (event.key === 'Escape' || event.key === 'Esc') { // Check for Escape key
315-
// Perform the desired action for the Escape key
320+
if (window.__PHOENIX_EMBED_INFO &&
321+
(event.key === 'Escape' || event.key === 'Esc')) { // Check for Escape key
322+
// Perform the desired action for the Escape key only if its within iframe inside phoenix
316323
window.parent.postMessage({
317324
handlerName: "ph-liveServer",
318325
eventName: 'embeddedEscapeKeyPressed'
319326
}, "*");
320327
}
321328
});
329+
330+
// this is for managing who am i context in iframes embedded in phoenix to have special handling.
331+
window.addEventListener('message', function(event) {
332+
if (!TRANSPORT_CONFIG.TRUSTED_ORIGINS_EMBED[event.origin]) {
333+
return; // Ignore messages from unexpected origins
334+
}
335+
336+
window.__PHOENIX_EMBED_INFO = {
337+
isTauri: event.data.isTauri
338+
};
339+
});
340+
if(window.self !== window.parent){
341+
// in an iframe
342+
window.parent.postMessage({
343+
handlerName: "ph-liveServer",
344+
eventName: 'whoAmIframePhoenix',
345+
href: location.href
346+
}, "*");
347+
}
322348
}(this));

src/LiveDevelopment/MultiBrowserImpl/transports/LivePreviewTransport.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ define(function (require, exports, module) {
7777
`TRANSPORT_CONFIG.PHOENIX_INSTANCE_ID = "${Phoenix.PHOENIX_INSTANCE_ID}";\n` +
7878
`TRANSPORT_CONFIG.LIVE_DEV_REMOTE_WORKER_SCRIPTS_FILE_NAME = "${LiveDevProtocol.LIVE_DEV_REMOTE_WORKER_SCRIPTS_FILE_NAME}";\n` +
7979
`TRANSPORT_CONFIG.LIVE_PREVIEW_DEBUG_ENABLED = ${logger.loggingOptions.logLivePreview};\n`+
80+
`TRANSPORT_CONFIG.TRUSTED_ORIGINS_EMBED = ${JSON.stringify(Phoenix.TRUSTED_ORIGINS)};\n`+
8081
transportScript;
8182
return LivePreviewTransportRemote.replace(replaceString, transportScript)
8283
+ "\n";

0 commit comments

Comments
 (0)