Skip to content

Commit 1644144

Browse files
committed
fix: window.alert, confirm and prompt apis not present in embedded tauri mac live preview
1 parent bbdfb85 commit 1644144

File tree

3 files changed

+118
-6
lines changed

3 files changed

+118
-6
lines changed

src/LiveDevelopment/BrowserScripts/LivePreviewTransportRemote.js

Lines changed: 111 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,16 +329,123 @@
329329
}
330330
});
331331

332+
function alertPatch(message) {
333+
// Create the modal container
334+
const modal = document.createElement('div');
335+
modal.style.position = 'fixed';
336+
modal.style.top = '0';
337+
modal.style.left = '0';
338+
modal.style.width = '100%';
339+
modal.style.height = '100vh';
340+
modal.style.backgroundColor = 'rgba(0,0,0,0.5)';
341+
modal.style.display = 'flex';
342+
modal.style.justifyContent = 'center';
343+
modal.style.alignItems = 'center';
344+
modal.style.zIndex = '1000000000';
345+
346+
// Create the modal content box
347+
const modalContent = document.createElement('div');
348+
modalContent.style.backgroundColor = 'white';
349+
modalContent.style.padding = '20px';
350+
modalContent.style.borderRadius = '5px';
351+
modalContent.style.minWidth = '300px';
352+
modalContent.style.margin = 'auto';
353+
modalContent.style.textAlign = 'center';
354+
355+
// Add title to the modal with the current page URL
356+
const title = document.createElement('h3');
357+
title.textContent = "alert"; // not translated as window.alert is same in all languages.
358+
title.style.marginBottom = '10px';
359+
360+
// Add text to the modal
361+
const text = document.createElement('p');
362+
text.textContent = message;
363+
text.style.marginBottom = '20px';
364+
365+
// Create OK button to close the modal
366+
const button = document.createElement('button');
367+
button.textContent = 'OK';
368+
button.style.padding = '10px 20px';
369+
button.style.border = 'none';
370+
button.style.backgroundColor = '#007BFF';
371+
button.style.color = 'white';
372+
button.style.borderRadius = '5px';
373+
button.style.cursor = 'pointer';
374+
375+
button.onclick = function() {
376+
document.body.removeChild(modal);
377+
};
378+
379+
// Append elements
380+
modalContent.appendChild(title);
381+
modalContent.appendChild(text);
382+
modalContent.appendChild(button);
383+
modal.appendChild(modalContent);
384+
document.body.appendChild(modal);
385+
}
386+
387+
function unsupported() {
388+
alertPatch(TRANSPORT_CONFIG.STRINGS.UNSUPPORTED_DOM_APIS_CONFIRM);
389+
}
390+
391+
// all externally opened live previews have the phcodeLivePreview="true" query string parameter set.
392+
const currentUrl = new URL(window.location.href);
393+
const queryParams = new URLSearchParams(currentUrl.search);
394+
const isExternalBrowser = queryParams.get("phcodeLivePreview") === "true";
395+
const isTauri = TRANSPORT_CONFIG.IS_NATIVE_APP;
396+
const platform = TRANSPORT_CONFIG.PLATFORM;
397+
398+
let alertQueue = [], confirmOrPromptCalled = false;
399+
let addToQueue = true;
400+
if(!isExternalBrowser){
401+
// this is an embedded iframe we always take hold of the alert api for better ux within the live preivew frame.
402+
window.__PHOENIX_EMBED_INFO = {isTauri, platform};
403+
const shouldPatchAlert = (isTauri && platform === "mac");
404+
if(shouldPatchAlert){
405+
// In Mac embedded live preview iframe in tauri, alert, prompt, and confirm apis
406+
// are not available, so we need to patch the other apis in mac
407+
window.alert = function (...args) {
408+
// at this time, we cant add our html alert as body is not loaded yet. So we queue alerts.
409+
addToQueue && alertQueue.push(...args);
410+
};
411+
window.confirm = function () {
412+
// confirm and prompt is no-op in mac, we just need to show that the api is not supported, so we just
413+
// keep a flag.
414+
confirmOrPromptCalled = true;
415+
};
416+
window.prompt = function () {
417+
confirmOrPromptCalled = true;
418+
};
419+
function drainAlertQueues() {
420+
addToQueue = false;
421+
if(confirmOrPromptCalled) {
422+
unsupported();
423+
}
424+
for(let i=0; i<alertQueue.length; i++) {
425+
alertPatch(alertQueue[i]);
426+
}
427+
alertQueue = [];
428+
window.alert = alertPatch;
429+
window.confirm = unsupported;
430+
window.prompt = unsupported;
431+
}
432+
433+
document.addEventListener('DOMContentLoaded', function() {
434+
drainAlertQueues();
435+
});
436+
}
437+
}
438+
332439
// this is for managing who am i context in iframes embedded in phoenix to have special handling.
333440
window.addEventListener('message', function(event) {
334441
if (!TRANSPORT_CONFIG.TRUSTED_ORIGINS_EMBED[event.origin]) {
335442
return; // Ignore messages from unexpected origins
336443
}
337444
if(event.data.type === "WHO_AM_I_RESPONSE") {
338-
window.__PHOENIX_EMBED_INFO = {
339-
isTauri: event.data.isTauri,
340-
platform: event.data.platform
341-
};
445+
if(!window.__PHOENIX_EMBED_INFO){
446+
// this is set from transport config. We should be here
447+
console.error("Expected window.__PHOENIX_EMBED_INFO to be set, but not???");
448+
}
342449
}
343450
});
344451
if(window.self !== window.parent){

src/LiveDevelopment/MultiBrowserImpl/transports/LivePreviewTransport.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ define(function (require, exports, module) {
2727

2828
const LiveDevProtocol = require("LiveDevelopment/MultiBrowserImpl/protocol/LiveDevProtocol"),
2929
EventDispatcher = require("utils/EventDispatcher"),
30+
Strings = require("strings"),
3031
Metrics = require("utils/Metrics");
3132

3233
const METRIC_SEND_INTERVAL_MS = 1000;
@@ -75,9 +76,14 @@ define(function (require, exports, module) {
7576
_transportBridge.getRemoteTransportScript()) || "";
7677
transportScript = "const TRANSPORT_CONFIG={};" +
7778
`TRANSPORT_CONFIG.PHOENIX_INSTANCE_ID = "${Phoenix.PHOENIX_INSTANCE_ID}";\n` +
79+
`TRANSPORT_CONFIG.IS_NATIVE_APP = ${Phoenix.isNativeApp};\n` +
80+
`TRANSPORT_CONFIG.PLATFORM = "${Phoenix.platform}";\n` +
7881
`TRANSPORT_CONFIG.LIVE_DEV_REMOTE_WORKER_SCRIPTS_FILE_NAME = "${LiveDevProtocol.LIVE_DEV_REMOTE_WORKER_SCRIPTS_FILE_NAME}";\n` +
7982
`TRANSPORT_CONFIG.LIVE_PREVIEW_DEBUG_ENABLED = ${logger.loggingOptions.logLivePreview};\n`+
8083
`TRANSPORT_CONFIG.TRUSTED_ORIGINS_EMBED = ${JSON.stringify(Phoenix.TRUSTED_ORIGINS)};\n`+
84+
`TRANSPORT_CONFIG.STRINGS = {
85+
UNSUPPORTED_DOM_APIS_CONFIRM: "${Strings.UNSUPPORTED_DOM_APIS_CONFIRM}"
86+
};\n`+
8187
transportScript;
8288
return LivePreviewTransportRemote.replace(replaceString, transportScript)
8389
+ "\n";

src/nls/root/strings.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ define({
128128
"ERROR_MAX_FILES": "This project contains more than 30,000 files. Features that operate across multiple files may be disabled or behave as if the project is empty. <a href='https://github.com/adobe/brackets/wiki/Large-Projects'>Read more about working with large projects</a>.",
129129

130130
// Live Preview error strings
131-
"ALERT": "Alert",
132-
"CONFIRM": "Confirm",
131+
"UNSUPPORTED_DOM_APIS_CONFIRM": "window.confirm and window.prompt APIS are not available in embedded Live Preview in {APP_NAME}. Please popout Live Preview to use these APIs in the browser.",
133132
"ERROR_LAUNCHING_BROWSER_TITLE": "Error Launching Browser",
134133
"ERROR_CANT_FIND_CHROME": "The Google Chrome browser could not be found. Please make sure it is installed.",
135134
"ERROR_LAUNCHING_BROWSER": "An error occurred when launching the browser. (error {0})",

0 commit comments

Comments
 (0)