forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiframeworkspace.ts
More file actions
80 lines (67 loc) · 2.36 KB
/
iframeworkspace.ts
File metadata and controls
80 lines (67 loc) · 2.36 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
import * as mem from "./memoryworkspace";
type Header = pxt.workspace.Header;
type ScriptText = pxt.workspace.ScriptText;
type WorkspaceProvider = pxt.workspace.WorkspaceProvider;
function loadedAsync(): Promise<void> {
return pxt.editor.postHostMessageAsync(<pxt.editor.EditorWorkspaceSyncRequest>{
type: "pxthost",
action: "workspaceloaded",
response: true
}).then(() => { })
}
let lastSyncState: pxt.editor.EditorSyncState
function listAsync() {
return pxt.editor.postHostMessageAsync(<pxt.editor.EditorWorkspaceSyncRequest>{
type: "pxthost",
action: "workspacesync",
response: true
}).then((msg: pxt.editor.EditorWorkspaceSyncResponse) => {
(msg.projects || []).forEach(mem.merge);
lastSyncState = msg.editor
// controllerId is a unique identifier of the controller source
pxt.tickEvent("pxt.controller", { controllerId: msg.controllerId });
return mem.provider.listAsync()
})
}
function getSyncState() { return lastSyncState }
function getAsync(h: Header): Promise<pxt.workspace.File> {
return mem.provider.getAsync(h)
}
function setAsync(h: Header, prevVer: any, text?: ScriptText) {
return mem.provider.setAsync(h, prevVer, text)
.then(() => {
const projectText = (text || (mem.projects[h.id] && mem.projects[h.id].text));
return pxt.editor.postHostMessageAsync(<pxt.editor.EditorWorkspaceSaveRequest>{
type: "pxthost",
action: "workspacesave",
project: { header: h, text: projectText },
response: false
})
}).then(() => { })
}
function resetAsync(): Promise<void> {
return mem.provider.resetAsync()
.then(() => pxt.editor.postHostMessageAsync(<pxt.editor.EditorWorkspaceSyncRequest>{
type: "pxthost",
action: "workspacereset",
response: true
})).then(() => { })
}
function fireEvent(ev: pxt.editor.events.Event) {
// Send the message up the chain
pxt.editor.postHostMessageAsync(<pxt.editor.EditorWorkspaceEvent>{
type: "pxthost",
action: "workspaceevent",
response: false,
event: ev
})
}
export const provider: WorkspaceProvider = {
getAsync,
setAsync,
listAsync,
resetAsync,
loadedAsync,
getSyncState,
fireEvent
}