-
-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathphoenix-builder-client.js
More file actions
192 lines (173 loc) · 6.92 KB
/
Copy pathphoenix-builder-client.js
File metadata and controls
192 lines (173 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
// Thin AMD wrapper over the boot-time phoenix-builder connection (window._phoenixBuilder).
// Registers Phoenix-API-dependent handlers (screenshot, FILE_CLOSE_ALL reload).
/*globals */
define(function (require, exports, module) {
const CommandManager = require("command/CommandManager");
const Commands = require("command/Commands");
const LiveDevProtocol = require("LiveDevelopment/MultiBrowserImpl/protocol/LiveDevProtocol");
const LiveDevMain = require("LiveDevelopment/main");
const WorkspaceManager = require("view/WorkspaceManager");
const boot = window._phoenixBuilder || null;
function _handleScreenshotRequest(msg) {
if (!Phoenix || !Phoenix.app || !Phoenix.app.screenShotBinary) {
boot.sendMessage({
type: "error",
id: msg.id,
message: "Screenshot API not available"
});
return;
}
// If targeting live preview frame and md viewer is active, redirect to md frame
let selector = msg.selector || undefined;
if (selector === "#panel-live-preview-frame") {
const mdFrame = document.getElementById("panel-md-preview-frame");
if (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 += chunkSize) {
const chunk = bytes.subarray(i, Math.min(i + chunkSize, 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: true } : undefined;
CommandManager.execute(Commands.FILE_CLOSE_ALL, closeArgs)
.done(function () {
boot.sendMessage({
type: "reload_response",
id: msg.id,
success: true
});
setTimeout(async function () {
await boot.dismantleTrustRing();
location.reload();
}, 100);
})
.fail(function (err) {
boot.sendMessage({
type: "reload_response",
id: msg.id,
success: false,
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"
});
});
}
// Fast path: already connected
if (LiveDevProtocol.getConnectionIds().length > 0) {
_evaluate();
return;
}
// Need to ensure live preview is open and connected
const panel = WorkspaceManager.getPanelForID("live-preview-panel");
if (!panel || !panel.isVisible()) {
CommandManager.execute("file.liveFilePreview");
} else {
LiveDevMain.openLivePreview();
}
// Wait for a live preview connection (up to 30s)
const TIMEOUT = 30000;
const POLL_INTERVAL = 500;
let settled = false;
let pollTimer = null;
function cleanup() {
settled = true;
if (pollTimer) {
clearInterval(pollTimer);
pollTimer = null;
}
LiveDevProtocol.off("ConnectionConnect.execJsLivePreview");
}
const timeoutTimer = setTimeout(function () {
if (settled) { return; }
cleanup();
boot.sendMessage({
type: "exec_js_live_preview_response",
id: msg.id,
error: "Timed out waiting for live preview connection (30s)"
});
}, TIMEOUT);
function onConnected() {
if (settled) { return; }
cleanup();
clearTimeout(timeoutTimer);
_evaluate();
}
LiveDevProtocol.on("ConnectionConnect.execJsLivePreview", onConnected);
// Safety-net poll in case the event was missed
pollTimer = setInterval(function () {
if (settled) {
clearInterval(pollTimer);
return;
}
if (LiveDevProtocol.getConnectionIds().length > 0) {
onConnected();
}
}, POLL_INTERVAL);
}
// Register handlers on the boot module
if (boot) {
boot.registerHandler("screenshot_request", _handleScreenshotRequest);
boot.registerHandler("reload_request", _handleReloadRequest);
boot.registerHandler("exec_js_live_preview_request", _handleExecJsLivePreviewRequest);
}
exports.connect = function (url) { if (boot) { boot.connect(url); } };
exports.disconnect = function () { if (boot) { boot.disconnect(); } };
exports.isConnected = function () { return boot ? boot.isConnected() : false; };
exports.getInstanceName = function () { return boot ? boot.getInstanceName() : ""; };
});