Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: window.alert, confirm and prompt apis not present in embedded tauri mac live preview #1814

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 117 additions & 4 deletions src/LiveDevelopment/BrowserScripts/LivePreviewTransportRemote.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,16 +329,129 @@
}
});

function alertPatch(message, titleText) {
// Create the modal container
const modal = document.createElement('div');
modal.style.position = 'fixed';
modal.style.top = '0';
modal.style.left = '0';
modal.style.width = '100%';
modal.style.height = '100vh';
modal.style.backgroundColor = 'rgba(0,0,0,0.5)';
modal.style.display = 'flex';
modal.style.justifyContent = 'center';
modal.style.alignItems = 'center';
modal.style.zIndex = '1000000000';

// Create the modal content box
const modalContent = document.createElement('div');
modalContent.style.backgroundColor = 'white';
modalContent.style.padding = '20px';
modalContent.style.borderRadius = '5px';
modalContent.style.minWidth = '300px';
modalContent.style.margin = 'auto';
modalContent.style.textAlign = 'center';

// Add title to the modal with the current page URL
const title = document.createElement('h3');
title.textContent = titleText || "alert"; // not translated as window.alert is same in all languages.
title.style.marginBottom = '10px';

// Add text to the modal
const text = document.createElement('p');
text.textContent = message;
text.style.marginBottom = '20px';

// Create OK button to close the modal
const button = document.createElement('button');
button.textContent = 'OK';
button.style.padding = '10px 20px';
button.style.border = 'none';
button.style.backgroundColor = '#007BFF';
button.style.color = 'white';
button.style.borderRadius = '5px';
button.style.cursor = 'pointer';

button.onclick = function() {
document.body.removeChild(modal);
};

// Append elements
modalContent.appendChild(title);
modalContent.appendChild(text);
modalContent.appendChild(button);
modal.appendChild(modalContent);
document.body.appendChild(modal);
}

function unsupportedConfirm() {
alertPatch(TRANSPORT_CONFIG.STRINGS.UNSUPPORTED_DOM_APIS_CONFIRM, "window.confirm");
}
function unsupportedPrompt() {
alertPatch(TRANSPORT_CONFIG.STRINGS.UNSUPPORTED_DOM_APIS_CONFIRM, "window.prompt");
}

// all externally opened live previews have the phcodeLivePreview="true" query string parameter set.
const currentUrl = new URL(window.location.href);
const queryParams = new URLSearchParams(currentUrl.search);
const isExternalBrowser = queryParams.get("phcodeLivePreview") === "true";
const isTauri = TRANSPORT_CONFIG.IS_NATIVE_APP;
const platform = TRANSPORT_CONFIG.PLATFORM;

let alertQueue = [], confirmCalled = false, promptCalled = false;
let addToQueue = true;
if(!isExternalBrowser){
// this is an embedded iframe we always take hold of the alert api for better ux within the live preivew frame.
window.__PHOENIX_EMBED_INFO = {isTauri, platform};
const shouldPatchAlert = (isTauri && platform === "mac");
if(shouldPatchAlert){
// In Mac embedded live preview iframe in tauri, alert, prompt, and confirm apis
// are not available, so we need to patch the other apis in mac
window.alert = function (...args) {
// at this time, we cant add our html alert as body is not loaded yet. So we queue alerts.
addToQueue && alertQueue.push(...args);
};
window.confirm = function () {
// confirm and prompt is no-op in mac, we just need to show that the api is not supported, so we just
// keep a flag.
confirmCalled = true;
};
window.prompt = function () {
promptCalled = true;
};
function drainAlertQueues() {
addToQueue = false;
if(confirmCalled) {
unsupportedConfirm();
}
if(promptCalled) {
unsupportedPrompt();
}
for(let i=0; i<alertQueue.length; i++) {
alertPatch(alertQueue[i]);
}
alertQueue = [];
window.alert = alertPatch;
window.confirm = unsupportedConfirm;
window.prompt = unsupportedPrompt;
}

document.addEventListener('DOMContentLoaded', function() {
drainAlertQueues();
});
}
}

// 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
}
if(event.data.type === "WHO_AM_I_RESPONSE") {
window.__PHOENIX_EMBED_INFO = {
isTauri: event.data.isTauri,
platform: event.data.platform
};
if(!window.__PHOENIX_EMBED_INFO){
// this is set from transport config. We should be here
console.error("Expected window.__PHOENIX_EMBED_INFO to be set, but not???");
}
}
});
if(window.self !== window.parent){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ define(function (require, exports, module) {

const LiveDevProtocol = require("LiveDevelopment/MultiBrowserImpl/protocol/LiveDevProtocol"),
EventDispatcher = require("utils/EventDispatcher"),
Strings = require("strings"),
Metrics = require("utils/Metrics");

const METRIC_SEND_INTERVAL_MS = 1000;
Expand Down Expand Up @@ -75,9 +76,14 @@ define(function (require, exports, module) {
_transportBridge.getRemoteTransportScript()) || "";
transportScript = "const TRANSPORT_CONFIG={};" +
`TRANSPORT_CONFIG.PHOENIX_INSTANCE_ID = "${Phoenix.PHOENIX_INSTANCE_ID}";\n` +
`TRANSPORT_CONFIG.IS_NATIVE_APP = ${Phoenix.isNativeApp};\n` +
`TRANSPORT_CONFIG.PLATFORM = "${Phoenix.platform}";\n` +
`TRANSPORT_CONFIG.LIVE_DEV_REMOTE_WORKER_SCRIPTS_FILE_NAME = "${LiveDevProtocol.LIVE_DEV_REMOTE_WORKER_SCRIPTS_FILE_NAME}";\n` +
`TRANSPORT_CONFIG.LIVE_PREVIEW_DEBUG_ENABLED = ${logger.loggingOptions.logLivePreview};\n`+
`TRANSPORT_CONFIG.TRUSTED_ORIGINS_EMBED = ${JSON.stringify(Phoenix.TRUSTED_ORIGINS)};\n`+
`TRANSPORT_CONFIG.STRINGS = {
UNSUPPORTED_DOM_APIS_CONFIRM: "${Strings.UNSUPPORTED_DOM_APIS_CONFIRM}"
};\n`+
transportScript;
return LivePreviewTransportRemote.replace(replaceString, transportScript)
+ "\n";
Expand Down
3 changes: 1 addition & 2 deletions src/nls/root/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ 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",
"UNSUPPORTED_DOM_APIS_CONFIRM": "The `window.confirm` and `window.prompt` APIs are unavailable in the embedded Live Preview of {APP_NAME}. Please popout Live Preview to use these APIs in the browser.",
"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
Loading