-
Notifications
You must be signed in to change notification settings - Fork 592
/
Copy patheditorcontroller.ts
451 lines (425 loc) · 23.3 KB
/
editorcontroller.ts
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/// <reference path="../localtypings/pxteditor.d.ts" />
import { runValidatorPlan } from "./code-validation/runValidatorPlan";
import IProjectView = pxt.editor.IProjectView;
import { IFrameEmbeddedClient } from "../pxtservices/iframeEmbeddedClient";
import { saveProjectAsync } from "./projectImport";
const pendingRequests: pxt.Map<{
resolve: (res?: pxt.editor.EditorMessageResponse | PromiseLike<pxt.editor.EditorMessageResponse>) => void;
reject: (err: any) => void;
}> = {};
let iframeClient: IFrameEmbeddedClient;
/**
* Binds incoming window messages to the project view.
* Requires the "allowParentController" flag in the pxtarget.json/appTheme object.
*
* When the project view receives a request (EditorMessageRequest),
* it starts the command and returns the result upon completion.
* The response (EditorMessageResponse) contains the request id and result.
* Some commands may be async, use the ``id`` field to correlate to the original request.
*/
export function bindEditorMessages(getEditorAsync: () => Promise<IProjectView>) {
const allowEditorMessages = pxt.appTarget.appTheme.allowParentController || pxt.shell.isControllerMode();
const allowExtensionMessages = pxt.appTarget.appTheme.allowPackageExtensions;
const allowSimTelemetry = pxt.appTarget.appTheme.allowSimulatorTelemetry;
if (!allowEditorMessages && !allowExtensionMessages && !allowSimTelemetry) return;
const handleMessage = (msg: MessageEvent) => {
const data = msg.data as pxt.editor.EditorMessage;
if (!data || !/^pxt(host|editor|pkgext|sim)$/.test(data.type)) return false;
if (data.type === "pxtpkgext" && allowExtensionMessages) {
// Messages sent to the editor iframe from a child iframe containing an extension
getEditorAsync().then(projectView => {
projectView.handleExtensionRequest(data as pxt.editor.ExtensionRequest);
})
}
else if (data.type === "pxtsim" && allowSimTelemetry) {
const event = data as pxt.editor.EditorMessageEventRequest;
if (event.action === "event") {
if (event.category || event.message) {
pxt.reportError(event.category, event.message, event.data as pxt.Map<string>)
}
else {
pxt.tickEvent(event.tick, event.data);
}
}
}
else if (allowEditorMessages) {
// Messages sent to the editor from the parent frame
let p = Promise.resolve();
let resp: any = undefined;
if (data.type == "pxthost") { // response from the host
const req = pendingRequests[data.id];
if (!req) {
pxt.debug(`pxthost: unknown request ${data.id}`);
} else {
p = p.then(() => req.resolve(data as pxt.editor.EditorMessageResponse));
}
} else if (data.type == "pxteditor") { // request from the editor
p = p.then(() => {
return getEditorAsync().then(projectView => {
const req = data as pxt.editor.EditorMessageRequest;
pxt.debug(`pxteditor: ${req.action}`);
switch (req.action.toLowerCase() as pxt.editor.EditorMessageRequest["action"]) {
case "switchjavascript": return Promise.resolve().then(() => projectView.openJavaScript());
case "switchpython": return Promise.resolve().then(() => projectView.openPython());
case "switchblocks": return Promise.resolve().then(() => projectView.openBlocks());
case "startsimulator": return Promise.resolve().then(() => projectView.startSimulator());
case "restartsimulator": return Promise.resolve().then(() => projectView.restartSimulator());
case "hidesimulator": return Promise.resolve().then(() => projectView.collapseSimulator());
case "showsimulator": return Promise.resolve().then(() => projectView.expandSimulator());
case "closeflyout": return Promise.resolve().then(() => projectView.closeFlyout());
case "unloadproject": return Promise.resolve().then(() => projectView.unloadProjectAsync());
case "saveproject": return projectView.saveProjectAsync();
case "compile": return projectView.compile();
case "redo": return Promise.resolve()
.then(() => {
const editor = projectView.editor;
if (editor && editor.hasRedo())
editor.redo();
});
case "undo": return Promise.resolve()
.then(() => {
const editor = projectView.editor;
if (editor && editor.hasUndo())
editor.undo();
});
case "setscale": {
const zoommsg = data as pxt.editor.EditorMessageSetScaleRequest;
return Promise.resolve()
.then(() => projectView.editor.setScale(zoommsg.scale));
}
case "stopsimulator": {
const stop = data as pxt.editor.EditorMessageStopRequest;
return Promise.resolve()
.then(() => projectView.stopSimulator(stop.unload));
}
case "newproject": {
const create = data as pxt.editor.EditorMessageNewProjectRequest;
return Promise.resolve()
.then(() => projectView.newProject(create.options));
}
case "importproject": {
const load = data as pxt.editor.EditorMessageImportProjectRequest;
return Promise.resolve()
.then(() => projectView.importProjectAsync(load.project, {
filters: load.filters,
searchBar: load.searchBar
}));
}
case "importexternalproject": {
const importExternal = data as pxt.editor.EditorMessageImportExternalProjectRequest
return saveProjectAsync(importExternal.project)
.then(importId => {
const importUrl = location.origin + location.pathname + `#embedimport:${importId}`;
resp = {
importUrl
} as Partial<pxt.editor.EditorMessageImportExternalProjectResponse>
});
}
case "openheader": {
const open = data as pxt.editor.EditorMessageOpenHeaderRequest;
return projectView.openProjectByHeaderIdAsync(open.headerId)
}
case "startactivity": {
const msg = data as pxt.editor.EditorMessageStartActivity;
let tutorialPath = msg.path;
let editorProjectName: string = undefined;
if (/^([jt]s|py|blocks?):/i.test(tutorialPath)) {
if (/^py:/i.test(tutorialPath))
editorProjectName = pxt.PYTHON_PROJECT_NAME;
else if (/^[jt]s:/i.test(tutorialPath))
editorProjectName = pxt.JAVASCRIPT_PROJECT_NAME;
else
editorProjectName = pxt.BLOCKS_PROJECT_NAME;
tutorialPath = tutorialPath.substr(tutorialPath.indexOf(':') + 1)
}
return Promise.resolve()
.then(() => projectView.startActivity({
activity: msg.activityType,
path: tutorialPath,
title: msg.title,
editor: editorProjectName,
previousProjectHeaderId: msg.previousProjectHeaderId,
carryoverPreviousCode: msg.carryoverPreviousCode
}));
}
case "importtutorial": {
const load = data as pxt.editor.EditorMessageImportTutorialRequest;
return Promise.resolve()
.then(() => projectView.importTutorialAsync(load.markdown));
}
case "proxytosim": {
const simmsg = data as pxt.editor.EditorMessageSimulatorMessageProxyRequest;
return Promise.resolve()
.then(() => projectView.proxySimulatorMessage(simmsg.content));
}
case "renderblocks": {
const rendermsg = data as pxt.editor.EditorMessageRenderBlocksRequest;
return Promise.resolve()
.then(() => projectView.renderBlocksAsync(rendermsg))
.then(r => {
return r.xml.then((svg: any) => {
resp = svg.xml;
})
});
}
case "renderxml": {
const rendermsg = data as pxt.editor.EditorMessageRenderXmlRequest;
return Promise.resolve()
.then(() => {
const r = projectView.renderXml(rendermsg);
return r.resultXml.then((svg: any) => {
resp = svg.xml;
})
});
}
case "renderbyblockid": {
const rendermsg = data as pxt.editor.EditorMessageRenderByBlockIdRequest;
return Promise.resolve()
.then(() => projectView.renderByBlockIdAsync(rendermsg))
.then(r => {
return r.resultXml.then((svg: any) => {
resp = svg.xml;
})
});
}
case "runeval": {
const evalmsg = data as pxt.editor.EditorMessageRunEvalRequest;
const plan = evalmsg.validatorPlan;
const planLib = evalmsg.planLib;
return Promise.resolve()
.then(() => {
const blocks = projectView.getBlocks();
return runValidatorPlan(blocks, plan, planLib)})
.then (results => {
resp = results;
});
}
case "gettoolboxcategories": {
const msg = data as pxt.editor.EditorMessageGetToolboxCategoriesRequest;
return Promise.resolve()
.then(() => {
resp = projectView.getToolboxCategories(msg.advanced);
});
}
case "getblockastext": {
const msg = data as pxt.editor.EditorMessageGetBlockAsTextRequest;
return Promise.resolve()
.then(() => {
const readableName = projectView.getBlockAsText(msg.blockId);
resp = { blockAsText: readableName } as pxt.editor.EditorMessageGetBlockAsTextResponse;
});
}
case "renderpython": {
const rendermsg = data as pxt.editor.EditorMessageRenderPythonRequest;
return Promise.resolve()
.then(() => projectView.renderPythonAsync(rendermsg))
.then(r => {
resp = r.python;
});
}
case "toggletrace": {
const togglemsg = data as pxt.editor.EditorMessageToggleTraceRequest;
return Promise.resolve()
.then(() => projectView.toggleTrace(togglemsg.intervalSpeed));
}
case "settracestate": {
const trcmsg = data as pxt.editor.EditorMessageSetTraceStateRequest;
return Promise.resolve()
.then(() => projectView.setTrace(trcmsg.enabled, trcmsg.intervalSpeed));
}
case "setsimulatorfullscreen": {
const fsmsg = data as pxt.editor.EditorMessageSetSimulatorFullScreenRequest;
return Promise.resolve()
.then(() => projectView.setSimulatorFullScreen(fsmsg.enabled));
}
case "togglehighcontrast": {
return Promise.resolve()
.then(() => projectView.toggleHighContrast());
}
case "sethighcontrast": {
const hcmsg = data as pxt.editor.EditorMessageSetHighContrastRequest;
return Promise.resolve()
.then(() => projectView.setHighContrast(hcmsg.on));
}
case "togglegreenscreen": {
return Promise.resolve()
.then(() => projectView.toggleGreenScreen());
}
case "print": {
return Promise.resolve()
.then(() => projectView.printCode());
}
case "pair": {
return projectView.pairAsync().then(() => {});
}
case "info": {
return Promise.resolve()
.then(() => {
resp = {
versions: pxt.appTarget.versions,
locale: ts.pxtc.Util.userLanguage(),
availableLocales: pxt.appTarget.appTheme.availableLocales
} as pxt.editor.InfoMessage;
});
}
case "shareproject": {
const msg = data as pxt.editor.EditorShareRequest;
return projectView.anonymousPublishHeaderByIdAsync(msg.headerId, msg.projectName)
.then(scriptInfo => {
resp = scriptInfo;
});
}
case "savelocalprojectstocloud": {
const msg = data as pxt.editor.EditorMessageSaveLocalProjectsToCloud;
return projectView.saveLocalProjectsToCloudAsync(msg.headerIds)
.then(guidMap => {
resp = <pxt.editor.EditorMessageSaveLocalProjectsToCloudResponse>{
headerIdMap: guidMap
};
})
}
case "requestprojectcloudstatus": {
// Responses are sent as separate "projectcloudstatus" messages.
const msg = data as pxt.editor.EditorMessageRequestProjectCloudStatus;
return projectView.requestProjectCloudStatus(msg.headerIds);
}
case "convertcloudprojectstolocal": {
const msg = data as pxt.editor.EditorMessageConvertCloudProjectsToLocal;
return projectView.convertCloudProjectsToLocal(msg.userId);
}
case "setlanguagerestriction": {
const msg = data as pxt.editor.EditorSetLanguageRestriction;
if (msg.restriction === "no-blocks") {
pxt.warn("no-blocks language restriction is not supported");
throw new Error("no-blocks language restriction is not supported")
}
return projectView.setLanguageRestrictionAsync(msg.restriction);
}
case "precachetutorial": {
const msg = data as pxt.editor.PrecacheTutorialRequest;
const tutorialData = msg.data;
const lang = msg.lang || pxt.Util.userLanguage();
return pxt.github.db.cacheReposAsync(tutorialData)
.then(async () => {
if (typeof tutorialData.markdown === "string") {
// the markdown needs to be cached in the translation db
const db = await pxt.BrowserUtils.translationDbAsync();
await db.setAsync(lang, tutorialData.path, undefined, undefined, tutorialData.markdown);
}
});
}
}
return Promise.resolve();
});
})
}
p.then(() => sendResponse(data, resp, true, undefined),
(err) => sendResponse(data, resp, false, err))
}
return true;
};
iframeClient = new IFrameEmbeddedClient(handleMessage);
}
/**
* Sends analytics messages upstream to container if any
*/
let controllerAnalyticsEnabled = false;
export function enableControllerAnalytics() {
if (controllerAnalyticsEnabled) return;
const hasOnPostHostMessage = !!pxt.commands.onPostHostMessage;
const hasAllowParentController = pxt.appTarget.appTheme.allowParentController;
const isInsideIFrame = pxt.BrowserUtils.isIFrame();
if (!(hasOnPostHostMessage || (hasAllowParentController && isInsideIFrame))) {
return;
}
const te = pxt.tickEvent;
pxt.tickEvent = function (id: string, data?: pxt.Map<string | number>): void {
if (te) te(id, data);
postHostMessageAsync(<pxt.editor.EditorMessageEventRequest>{
type: 'pxthost',
action: 'event',
tick: id,
response: false,
data
});
}
const rexp = pxt.reportException;
pxt.reportException = function (err: any, data: pxt.Map<string>): void {
if (rexp) rexp(err, data);
try {
postHostMessageAsync(<pxt.editor.EditorMessageEventRequest>{
type: 'pxthost',
action: 'event',
tick: 'error',
message: err.message,
response: false,
data
})
} catch (e) {
}
};
const re = pxt.reportError;
pxt.reportError = function (cat: string, msg: string, data?: pxt.Map<string | number>): void {
if (re) re(cat, msg, data);
postHostMessageAsync(<pxt.editor.EditorMessageEventRequest>{
type: 'pxthost',
action: 'event',
tick: 'error',
category: cat,
message: msg,
data
})
}
controllerAnalyticsEnabled = true;
}
function sendResponse(request: pxt.editor.EditorMessage, resp: any, success: boolean, error: any) {
if (request.response) {
const toSend = {
type: request.type,
id: request.id,
resp,
success,
error
};
if (iframeClient) {
iframeClient.postMessage(toSend)
}
else {
window.parent.postMessage(toSend, "*");
}
}
}
/**
* Determines if host messages should be posted
*/
export function shouldPostHostMessages() {
return pxt.appTarget.appTheme.allowParentController && pxt.BrowserUtils.isIFrame();
}
/**
* Posts a message from the editor to the host
*/
export function postHostMessageAsync(msg: pxt.editor.EditorMessageRequest): Promise<pxt.editor.EditorMessageResponse> {
return new Promise<pxt.editor.EditorMessageResponse>((resolve, reject) => {
const env = pxt.Util.clone(msg);
env.id = ts.pxtc.Util.guidGen();
if (msg.response)
pendingRequests[env.id] = { resolve, reject };
if (iframeClient) {
iframeClient.postMessage(env);
}
else {
window.parent.postMessage(env, "*");
}
// Post to editor extension if it wants to be notified of these messages.
// Note this is a one-way notification. Responses are not supported.
if (pxt.commands.onPostHostMessage) {
try {
pxt.commands.onPostHostMessage(env);
} catch (err) {
pxt.reportException(err);
}
}
if (!msg.response)
resolve(undefined)
})
}