forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
1 lines (1 loc) · 7.3 KB
/
main.js
File metadata and controls
1 lines (1 loc) · 7.3 KB
1
window.jsPromise=function(jqueryOrJSPromise){if(jqueryOrJSPromise&&jqueryOrJSPromise.catch&&jqueryOrJSPromise.then&&jqueryOrJSPromise.finally)return jqueryOrJSPromise;if(!jqueryOrJSPromise||jqueryOrJSPromise&&!jqueryOrJSPromise.fail||jqueryOrJSPromise&&!jqueryOrJSPromise.done)throw console.error("this function expects a jquery promise with done and fail handlers"),new Error("this function expects a jquery promise with done and fail handlers");return new Promise((resolve,reject)=>{jqueryOrJSPromise.done(resolve).fail(reject)})},window.deferredToPromise=window.jsPromise,window.catchToNull=function(promise,logError){return new Promise(resolve=>{promise.then(resolve).catch(err=>{logError&&console.error(logError,err),resolve(null)})})},window.delay=function(waitTimeMs){return new Promise(resolve=>{setTimeout(resolve,waitTimeMs)})};let trackedScriptCount=0;function _setSplashScreenStatusUpdate(message1,message2){let splashScreenFrame=document.getElementById("splash-screen-frame");if(!splashScreenFrame)return window.debugMode||(window.scriptObserver&&window.scriptObserver.disconnect(),console.log("startup Watcher: Disconnected script load watcher.")),!1;let displayBtn1=splashScreenFrame.contentDocument.getElementById("load-status-display-btn"),displayText2=splashScreenFrame.contentDocument.getElementById("load-status-display-text");return displayBtn1.textContent=message1,displayText2.textContent=message2,!0}const callback=function(mutationsList){try{for(const mutation of mutationsList)if("childList"===mutation.type&&mutation.addedNodes.length>0&&mutation.addedNodes[0].src){trackedScriptCount++;let scriptAddedSplit=mutation.addedNodes[0].src.split("/");if(scriptAddedSplit.length>0){let message=`Loading (${trackedScriptCount})`;window.Phoenix&&window.Phoenix.firstBoot&&(message=`Installing (${trackedScriptCount})`),_setSplashScreenStatusUpdate(message,`${scriptAddedSplit[scriptAddedSplit.length-1]}`)}}}catch(e){console.error("Error in script mutation observer!",e)}},mainScripts=document.getElementById("main-scripts-head"),config={childList:!0};function _unregisterServiceWorkers(){return new Promise(resolve=>{"serviceWorker"in navigator?(console.warn("Recovering boot: unregistering all service workers..."),navigator.serviceWorker.getRegistrations().then(registrations=>{const unregisterPromises=[];for(let registration of registrations)console.warn("Recovering boot: unregistering",registration.scope),unregisterPromises.push(registration.unregister());Promise.allSettled(unregisterPromises).catch(console.error).then(()=>{console.warn("Recovering boot: Success, unregistered all service workers!"),resolve()})}).catch(err=>{console.error("Error getting service worker registrations for boot recovery!!!",err),window.logger&&window.logger.reportError(err,"Critical error Recovering boot, while resetting service worker registrations"),setTimeout(resolve,2e3)})):resolve()})}Phoenix.isNativeApp||(window.scriptObserver=new MutationObserver(callback),window.scriptObserver.observe(mainScripts,config)),require.config({paths:{text:"thirdparty/text/text",i18n:"thirdparty/i18n/i18n",fileSystemImpl:"filesystem/impls/appshell/AppshellFileSystem","preact-compat":"thirdparty/preact-compat/preact-compat.min",preact:"thirdparty/preact/preact"},map:{"*":{"thirdparty/CodeMirror2":"thirdparty/CodeMirror","thirdparty/preact":"preact-compat","view/PanelManager":"view/WorkspaceManager"}},waitSeconds:60}),window.location.search.indexOf("testEnvironment")>-1?require.config({paths:{"preferences/PreferencesImpl":"../test/TestPreferencesImpl"},locale:"en"}):require.config({locale:window.PhStore.getItem("locale")||window.navigator.language});const SESSION_RESTART_ONCE_DUE_TO_CRITICAL_ERROR="SESSION_RESTART_ONCE_DUE_TO_CRITICAL_ERROR";function applyStyles(element,styles){Object.assign(element.style,styles)}function confirmReload(title,message){const modal=document.createElement("div"),modalContent=document.createElement("div"),modalTitle=document.createElement("h2"),modalMessage=document.createElement("p"),buttonsContainer=document.createElement("div"),copyButton=document.createElement("button"),getHelpButton=document.createElement("button"),reloadButton=document.createElement("button");modalTitle.textContent=title,message.split("\n").forEach((part,index,array)=>{modalMessage.appendChild(document.createTextNode(part)),index<array.length-1&&modalMessage.appendChild(document.createElement("br"))}),copyButton.textContent="Copy Error",getHelpButton.textContent="Get Help",reloadButton.textContent=Phoenix.isNativeApp?"Quit App":"Restart App";const buttonStyles={padding:"10px 20px",marginRight:"10px",border:"none",color:"white",borderRadius:"5px",cursor:"pointer"},copyButtonStyles={backgroundColor:"#288edf"},getHelpButtonStyles={backgroundColor:"#4CAF50"},reloadButtonStyles={backgroundColor:"#4CAF50"};return modalTitle.style.color="red",applyStyles(modal,{color:"black",position:"fixed",top:"0",left:"0",width:"100%",height:"100%",backgroundColor:"rgba(0, 0, 0, 0.75)",zIndex:"10000000",display:"flex",justifyContent:"center",alignItems:"center"}),applyStyles(modalContent,{backgroundColor:"white",padding:"20px",borderRadius:"10px",textAlign:"center",boxShadow:"0 4px 8px rgba(0,0,0,0.2)"}),applyStyles(buttonsContainer,{marginTop:"20px",display:"flex",justifyContent:"space-around"}),applyStyles(copyButton,{...buttonStyles,...copyButtonStyles}),applyStyles(getHelpButton,{...buttonStyles,...getHelpButtonStyles}),applyStyles(reloadButton,{...buttonStyles,...reloadButtonStyles}),copyButton.onclick=function(){navigator.clipboard.writeText(message).then(()=>{alert("Error Message copied!")},err=>{console.error("Failed to copy text: ",err)})},getHelpButton.onclick=function(){Phoenix.app.openURLInDefaultBrowser("https://github.com/phcode-dev/phoenix/discussions")},modalContent.appendChild(modalTitle),modalContent.appendChild(modalMessage),buttonsContainer.appendChild(copyButton),buttonsContainer.appendChild(getHelpButton),buttonsContainer.appendChild(reloadButton),modalContent.appendChild(buttonsContainer),modal.appendChild(modalContent),document.body.appendChild(modal),new Promise(resolve=>{reloadButton.onclick=function(){resolve(!0),reloadButton.textContent=Phoenix.isNativeApp?"Quitting...":"Reloading...",reloadButton.style.color="darkgray",reloadButton.style.backgroundColor="grey"}})}function resetCacheAndRestart(){Phoenix.isNativeApp?setTimeout(()=>{Phoenix.app.closeWindow(!0)},1e3):window._resetCacheIfNeeded?window._resetCacheIfNeeded(!0).finally(()=>{setTimeout(()=>{_unregisterServiceWorkers().then(()=>{location.reload()})},3e3)}):setTimeout(()=>{location.reload()},3e3)}async function _recoverOnFailure(err){if(!Phoenix.isNativeApp&&!navigator.onLine)return void alert("No internet connection. Please check your connection and reload page.");window.logger&&window.logger.reportError(err,"Critical error when loading brackets. Trying to reload again.");const restartedOnce=sessionStorage.getItem(SESSION_RESTART_ONCE_DUE_TO_CRITICAL_ERROR);let shouldRestart;restartedOnce||Phoenix.isNativeApp?shouldRestart=await confirmReload("Oops! Something went wrong",err.message+"\n"+err.stack||err):(sessionStorage.setItem(SESSION_RESTART_ONCE_DUE_TO_CRITICAL_ERROR,"true"),shouldRestart=!0),shouldRestart&&resetCacheAndRestart()}define(function(require){require(["utils/Metrics","utils/Compatibility","utils/EventDispatcher"],function(){window.Metrics=require("utils/Metrics"),require(["brackets"],()=>{},_recoverOnFailure)})});