Skip to content

Commit

Permalink
fix: mitigate alert and confirm apis not working in embeded live prev…
Browse files Browse the repository at this point in the history
…iew in windows desktop
  • Loading branch information
abose committed Jul 15, 2024
1 parent 943d2b9 commit 6cbae7e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
32 changes: 28 additions & 4 deletions src/LiveDevelopment/BrowserScripts/LivePreviewTransportRemote.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,39 @@
}
});

let alertStr = "Alert", confirmStr = "Confirm";

function alertPatch(message) {
// in tauri windows, only the prompt API works. so we use it for alerts as well
// we cant use html alerts here as the alert api will pause js which we cant do via js alone.
prompt(alertStr, message);
}

function confirmPatch(message) {
// in tauri windows, only the prompt API works. so we use it for confirm as well
// we cant use html alerts here as the alert api will pause js which we cant do via js alone.
const response = prompt(confirmStr, message);
// prompt will return null if cancel is pressed
return !!response;
}

// this is for managing who am i context in iframes embedded in phoenix to have special handling.
window.addEventListener('message', function(event) {
if (!TRANSPORT_CONFIG.TRUSTED_ORIGINS_EMBED[event.origin]) {
return; // Ignore messages from unexpected origins
}

window.__PHOENIX_EMBED_INFO = {
isTauri: event.data.isTauri
};
if(event.data.type === "WHO_AM_I_RESPONSE") {
window.__PHOENIX_EMBED_INFO = {
isTauri: event.data.isTauri
};
alertStr = event.data.alertStr || alertStr;
confirmStr = event.data.confirmStr || confirmStr;
if(event.data.isTauri && event.data.platform === 'win') {
// patch alert and confirm as in windows iframes in tauri, these are not present.
window.alert = alertPatch;
window.confirm = confirmPatch;
}
}
});
if(window.self !== window.parent){
// in an iframe
Expand Down
2 changes: 1 addition & 1 deletion src/assets/phoenix-splash/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<img id="logo" src="images/phoenix-logo.svg"/>
<div id="MainText">
<h1>Phoenix Code</h1>
<span>Code Creatively: Visual Editing Tailored for Developers</span>
<span>The text editor designed to make coding as simple and fun as playing a video game</span>
<br><br>
<button id="load-status-display-btn" class="primary-button">Loading Editor...</button>
<p style= "color: white; font-size:13px;" id="load-status-display-text" >...</p>
Expand Down
5 changes: 4 additions & 1 deletion src/extensionsIntegrated/Phoenix-live-preview/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ define(function (require, exports, module) {
const iframeDom = $iframe[0];
iframeDom.contentWindow.postMessage({
type: "WHO_AM_I_RESPONSE",
isTauri: Phoenix.isNativeApp
isTauri: Phoenix.isNativeApp,
platform: Phoenix.platform,
alertStr: Strings.ALERT,
confirmStr: Strings.CONFIRM
}, "*"); // this is not sensitive info, and is only dispatched if requested by the iframe
}
});
Expand Down
2 changes: 2 additions & 0 deletions src/nls/root/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ define({
"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>.",

// Live Preview error strings
"ALERT": "Alert",
"CONFIRM": "Confirm",
"ERROR_LAUNCHING_BROWSER_TITLE": "Error Launching Browser",
"ERROR_CANT_FIND_CHROME": "The Google Chrome browser could not be found. Please make sure it is installed.",
"ERROR_LAUNCHING_BROWSER": "An error occurred when launching the browser. (error {0})",
Expand Down

0 comments on commit 6cbae7e

Please sign in to comment.