-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer.html
162 lines (142 loc) · 6.16 KB
/
transfer.html
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
<html>
<head>
<link rel="stylesheet" href="/doccdn/semantic.css" />
<link rel="stylesheet" href="/doccdn/blockly.css" />
<link rel="stylesheet" href="/docfiles/style.css" />
<link rel="stylesheet" href="/docfiles/vs.css" />
<link rel="stylesheet" href="/docfiles/target.css" />
<script src="/doccdn/jquery.js"></script>
<script type="text/javascript" src="/blb/pxtlib.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pouchdb/5.2.1/pouchdb.min.js"></script>
<style>
/* Sandbox */
.footer {
position:absolute;
bottom:0rem;
right:1rem;
z-index:100;
margin-bottom: 0.2rem !important;
}
.footer .item{
font-size: 0.8rem !important;
}
</style>
</head>
<body>
<div id="root" class="ui container">
<h1>Microsoft MakeCode for micro:bit</h1>
<footer class="ui horizontal small divided link list footer">
<a target="_blank" class="item" href="https://makecode.com">Microsoft MakeCode</a>
<a target="_blank" class="item" href="https://makecode.com/termsofuse">Terms of Use </a>
<a target="_blank" class="item" href="https://makecode.com/privacy">Privacy </a>
</footer>
</div>
<script>
function getAllAsync(db, name) {
return db.allDocs({
include_docs: true,
startkey: name + "--",
endkey: name + "--\uffff"
}).then(function (resp) { return resp.rows.map(function (e) { return e.doc; }) })
}
function transfer(targetOrigin, resp) {
if (!targetOrigin || !window.parent || window.parent == window)
return;
console.log('transferring projects to ' + targetOrigin);
console.log('sending database')
window.parent.postMessage({
type: 'transfer',
action: 'export',
data: resp
}, targetOrigin);
console.log('waiting for clear command')
window.addEventListener('message', function (ev) {
var origin = event.origin || event.originalEvent.origin;
if (origin != targetOrigin) {
console.error('clear command coming from unknown origin ' + origin)
return;
}
var data = ev.data;
if (data && data.type == 'transfer' && data.action == 'clear') {
console.log('dropping databases')
db.destroy()
.then(function () {
console.log('databases dropped')
})
} else {
console.log('unknown message')
console.log(data);
}
})
}
function render(targetOrigin, resp) {
console.log('rendering headers')
if (resp.header.length == 0) {
$('#root').append('<div class="ui message success">It looks like all your programs have been imported already!</div>');
return;
}
var programs = $('<div id="programs" ui="ui cards"></div>');
$('#root').append(programs);
resp.header.forEach(function (header, i) {
console.log('injecting ' + header.name)
delete header._id;
delete header._rev;
delete header.id;
var text = resp.text[i].files;
var d = $('<a target="_blank" class="ui card link" />');
var ctnt = $('<div class="ui content">');
d.append(ctnt);
var hd = $('<div class="ui header"/>').text(header.name);
ctnt.append(hd);
var url = (targetOrigin ? 'https://' + targetOrigin : "/") + '#transfer:' + window.btoa(JSON.stringify({ header: header, text: text }));
d.attr('href', url);
programs.append(d);
})
}
$(document).ready(function () {
console.log('opening database')
var targetOrigin = '';
var storageId = '';
// https://pxt.microbit.org/api/transfer/makecode.microbit.org?storageid=([a-z]+)
var m = /^https:\/\/[a-z0-9.\/]+\/api\/transfer\/([^?]+)\?storageid=([a-z]+)$/i.exec(window.location.href);
if (m) {
targetOrigin = 'https://' + m[1];
storageId = m[2];
} else {
var m = /\?storageid=([a-z]+)$/i.exec(window.location.href);
if (m) {
storageId = m[1];
}
}
$('#root').append($('<h2/>').text('Migrate your programs to ' + (targetOrigin || 'the editor')));
$('#root').append($('<p/>').text('Click on a project to import it in ' + (targetOrigin || 'the editor')));
var db = new PouchDB("pxt-" + storageId, { revs_limit: 2 })
var resp = {};
db.get('pouchdbsupportabletest')
.catch(function (error) {
if (error) console.error(error);
if (error && error.error && error.name == 'indexed_db_went_bad') {
console.log('database corrupted');
window.parent.postMessage({
type: 'transfer',
action: 'error',
message: 'dbcorrupt'
}, targetOrigin);
return;
}
console.log('database opened')
getAllAsync(db, "header")
.then(function (rh) {
resp.header = rh;
console.log('headers loaded')
return getAllAsync(db, "text")
}).then(function (rt) {
resp.text = rt;
transfer(targetOrigin, resp);
render(targetOrigin, resp);
})
});
});
</script>
</body>
</html>