forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphoenix-builder-client.js
More file actions
1 lines (1 loc) · 3.33 KB
/
Copy pathphoenix-builder-client.js
File metadata and controls
1 lines (1 loc) · 3.33 KB
1
define(function(require,exports,module){const CommandManager=require("command/CommandManager"),Commands=require("command/Commands"),LiveDevProtocol=require("LiveDevelopment/MultiBrowserImpl/protocol/LiveDevProtocol"),LiveDevMain=require("LiveDevelopment/main"),WorkspaceManager=require("view/WorkspaceManager"),boot=window._phoenixBuilder||null;function _handleScreenshotRequest(msg){if(!Phoenix||!Phoenix.app||!Phoenix.app.screenShotBinary)return void boot.sendMessage({type:"error",id:msg.id,message:"Screenshot API not available"});let selector=msg.selector||void 0;if("#panel-live-preview-frame"===selector){const mdFrame=document.getElementById("panel-md-preview-frame");mdFrame&&mdFrame.offsetParent&&(selector="#panel-md-preview-frame")}Phoenix.app.screenShotBinary(selector).then(function(bytes){let binary="";const chunkSize=8192;for(let i=0;i<bytes.length;i+=8192){const chunk=bytes.subarray(i,Math.min(i+8192,bytes.length));binary+=String.fromCharCode.apply(null,chunk)}const base64=btoa(binary);boot.sendMessage({type:"screenshot_response",id:msg.id,data:base64})}).catch(function(err){boot.sendMessage({type:"error",id:msg.id,message:err.message||"Screenshot failed"})})}function _handleReloadRequest(msg){const closeArgs=msg.forceClose?{_forceClose:!0}:void 0;CommandManager.execute(Commands.FILE_CLOSE_ALL,closeArgs).done(function(){boot.sendMessage({type:"reload_response",id:msg.id,success:!0}),setTimeout(async function(){await boot.dismantleTrustRing(),location.reload()},100)}).fail(function(err){boot.sendMessage({type:"reload_response",id:msg.id,success:!1,message:err&&err.message||"Close cancelled by user"})})}function _handleExecJsLivePreviewRequest(msg){function _evaluate(){LiveDevProtocol.evaluate(msg.code).done(function(evalResult){boot.sendMessage({type:"exec_js_live_preview_response",id:msg.id,result:JSON.stringify(evalResult)})}).fail(function(err){boot.sendMessage({type:"exec_js_live_preview_response",id:msg.id,error:err&&err.message||String(err)||"evaluate() failed"})})}if(LiveDevProtocol.getConnectionIds().length>0)return void _evaluate();const panel=WorkspaceManager.getPanelForID("live-preview-panel");panel&&panel.isVisible()?LiveDevMain.openLivePreview():CommandManager.execute("file.liveFilePreview");const TIMEOUT=3e4,POLL_INTERVAL=500;let settled=!1,pollTimer=null;function cleanup(){settled=!0,pollTimer&&(clearInterval(pollTimer),pollTimer=null),LiveDevProtocol.off("ConnectionConnect.execJsLivePreview")}const timeoutTimer=setTimeout(function(){settled||(cleanup(),boot.sendMessage({type:"exec_js_live_preview_response",id:msg.id,error:"Timed out waiting for live preview connection (30s)"}))},3e4);function onConnected(){settled||(cleanup(),clearTimeout(timeoutTimer),_evaluate())}LiveDevProtocol.on("ConnectionConnect.execJsLivePreview",onConnected),pollTimer=setInterval(function(){settled?clearInterval(pollTimer):LiveDevProtocol.getConnectionIds().length>0&&onConnected()},500)}boot&&(boot.registerHandler("screenshot_request",_handleScreenshotRequest),boot.registerHandler("reload_request",_handleReloadRequest),boot.registerHandler("exec_js_live_preview_request",_handleExecJsLivePreviewRequest)),exports.connect=function(url){boot&&boot.connect(url)},exports.disconnect=function(){boot&&boot.disconnect()},exports.isConnected=function(){return!!boot&&boot.isConnected()},exports.getInstanceName=function(){return boot?boot.getInstanceName():""}});