-
Notifications
You must be signed in to change notification settings - Fork 642
/
Copy pathpatch.ts
405 lines (362 loc) · 17.5 KB
/
patch.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
export function patchBlocks(pkgTargetVersion: string, dom: Element) {
if (pxt.semver.majorCmp(pkgTargetVersion || "0.0.0", "7.0.13") <= 0) {
// Variable pin param
/*
<block type="device_get_digital_pin">
<field name="name">DigitalPin.P0</field>
</block>
converts to
<block type="device_get_digital_pin">
<value name="name">
<shadow type="digital_pin">
<field name="pin">DigitalPin.P0</field>
</shadow>
</value>
</block>
*/
pxt.U.toArray(dom.querySelectorAll("block[type=device_get_digital_pin]"))
.concat(pxt.U.toArray(dom.querySelectorAll("shadow[type=device_get_digital_pin]")))
.concat(pxt.U.toArray(dom.querySelectorAll("block[type=device_set_digital_pin]")))
.concat(pxt.U.toArray(dom.querySelectorAll("block[type=device_get_analog_pin]")))
.concat(pxt.U.toArray(dom.querySelectorAll("shadow[type=device_get_analog_pin]")))
.concat(pxt.U.toArray(dom.querySelectorAll("block[type=device_set_analog_pin]")))
.concat(pxt.U.toArray(dom.querySelectorAll("block[type=device_set_analog_period]")))
.concat(pxt.U.toArray(dom.querySelectorAll("block[type=pins_pulse_in]")))
.concat(pxt.U.toArray(dom.querySelectorAll("shadow[type=pins_pulse_in]")))
.concat(pxt.U.toArray(dom.querySelectorAll("block[type=device_set_servo_pin]")))
.concat(pxt.U.toArray(dom.querySelectorAll("block[type=device_set_servo_pulse]")))
.concat(pxt.U.toArray(dom.querySelectorAll("block[type=device_analog_set_pitch_pin]")))
.concat(pxt.U.toArray(dom.querySelectorAll("block[type=device_set_pull]")))
.concat(pxt.U.toArray(dom.querySelectorAll("block[type=device_set_pin_events]")))
.concat(pxt.U.toArray(dom.querySelectorAll("block[type=pin_neopixel_matrix_width]")))
.concat(pxt.U.toArray(dom.querySelectorAll("block[type=spi_pins]")))
.concat(pxt.U.toArray(dom.querySelectorAll("block[type=pin_set_audio_pin]")))
.forEach(node => {
const blockType = node.getAttribute("type");
pxt.U.toArray(node.children)
.filter(oldPinNode => {
if (oldPinNode.tagName != "field") return false;
switch (blockType) {
case "device_get_digital_pin":
case "device_set_digital_pin":
case "device_get_analog_pin":
case "device_set_analog_pin":
case "pins_pulse_in":
case "device_set_servo_pin":
case "device_analog_set_pitch_pin":
case "pin_set_audio_pin":
return oldPinNode.getAttribute("name") === "name";
case "device_set_analog_period":
case "device_set_pull":
case "device_set_pin_events":
case "pin_neopixel_matrix_width":
return oldPinNode.getAttribute("name") === "pin";
case "device_set_servo_pulse":
return oldPinNode.getAttribute("name") === "value";
case "spi_pins":
return ["mosi", "miso", "sck"].includes(oldPinNode.getAttribute("name"));
}
return false;
})
.forEach(oldPinNode => {
const valueNode = node.ownerDocument.createElement("value");
valueNode.setAttribute("name", oldPinNode.getAttribute("name"));
let nodeText = oldPinNode.textContent;
const pinShadowNode = node.ownerDocument.createElement("shadow");
const [enumName, pinName] = nodeText.split(".");
let pinBlockType;
switch (enumName) {
case "DigitalPin":
pinBlockType = "digital_pin_shadow";
break;
case "AnalogPin":
pinBlockType = "analog_pin_shadow";
break;
}
if (!pinBlockType) return;
// If this is one of the read/write pins, narrow to the read write shadow
if (blockType === "device_get_analog_pin") {
switch (pinName) {
case "P0":
case "P1":
case "P2":
case "P3":
case "P4":
case "P10":
pinBlockType = "analog_read_write_pin_shadow";
nodeText = `AnalogReadWritePin.${pinName}`;
break;
}
}
pinShadowNode.setAttribute("type", pinBlockType);
const fieldNode = node.ownerDocument.createElement("field");
fieldNode.setAttribute("name", "pin");
fieldNode.textContent = nodeText;
pinShadowNode.appendChild(fieldNode);
valueNode.appendChild(pinShadowNode);
node.replaceChild(valueNode, oldPinNode);
});
});
}
if (pxt.semver.majorCmp(pkgTargetVersion || "0.0.0", "5.0.12") <= 0) {
// Eighth note misspelling
/*
<block type="basic_show_icon">
<field name="i">IconNames.EigthNote</field>
</block>
converts to
<block type="basic_show_icon">
<field name="i">IconNames.EighthNote</field>
</block>
*/
pxt.U.toArray(dom.querySelectorAll("block[type=basic_show_icon]>field[name=i]"))
.filter(node => node.textContent === "IconNames.EigthNote")
.forEach(node => node.textContent = "IconNames.EighthNote");
// Italian translation error
/*
<shadow type="device_note">
<field name="note">466</field>
</shadow>
converts to
<shadow type="device_note">
<field name="name">466</field>
</shadow>
*/
pxt.U.toArray(dom.querySelectorAll("shadow[type=device_note]>field[name=note]"))
.forEach(node => node.setAttribute("name", "name"));
}
// is this a old script?
if (pxt.semver.majorCmp(pkgTargetVersion || "0.0.0", "1.0.0") >= 0) return;
// showleds
/**
<block type="device_show_leds">
<field name="LED00">FALSE</field>
<field name="LED10">FALSE</field>
<field name="LED20">FALSE</field>
<field name="LED30">FALSE</field>
<field name="LED40">FALSE</field>
<field name="LED01">FALSE</field>
<field name="LED11">FALSE</field>
<field name="LED21">FALSE</field>
<field name="LED31">TRUE</field>
<field name="LED41">FALSE</field>
<field name="LED02">FALSE</field>
<field name="LED12">FALSE</field>
<field name="LED22">FALSE</field>
<field name="LED32">FALSE</field>
<field name="LED42">FALSE</field>
<field name="LED03">FALSE</field>
<field name="LED13">TRUE</field>
<field name="LED23">FALSE</field>
<field name="LED33">FALSE</field>
<field name="LED43">FALSE</field>
<field name="LED04">FALSE</field>
<field name="LED14">FALSE</field>
<field name="LED24">FALSE</field>
<field name="LED34">FALSE</field>
<field name="LED44">FALSE</field>
</block>
converts to
<block type="device_show_leds">
<field name="LEDS">`
. . . . .
. . . # .
. . . . .
. # . . .
. . . . .
`
</field>
</block>
*/
pxt.U.toArray(dom.querySelectorAll("block[type=device_show_leds]"))
.concat(pxt.U.toArray(dom.querySelectorAll("block[type=device_build_image]")))
.concat(pxt.U.toArray(dom.querySelectorAll("shadow[type=device_build_image]")))
.concat(pxt.U.toArray(dom.querySelectorAll("block[type=device_build_big_image]")))
.concat(pxt.U.toArray(dom.querySelectorAll("shadow[type=device_build_big_image]")))
.forEach(node => {
// don't rewrite if already upgraded, eg. field LEDS already present
if (pxt.U.toArray(node.children).filter(child => child.tagName == "field" && "LEDS" == child.getAttribute("name"))[0])
return;
// read LEDxx value and assmebly into a new field
const leds: string[][] = [[], [], [], [], []];
pxt.U.toArray(node.children)
.filter(child => child.tagName == "field" && /^LED\d+$/.test(child.getAttribute("name")))
.forEach(lednode => {
let n = lednode.getAttribute("name");
let col = parseInt(n[3]);
let row = parseInt(n[4]);
leds[row][col] = lednode.innerHTML == "TRUE" ? "#" : ".";
// remove node
node.removeChild(lednode);
});
// add new field
const f = node.ownerDocument.createElement("field");
f.setAttribute("name", "LEDS");
const s = '`\n' + leds.map(row => row.join('')).join('\n') + '\n`';
f.appendChild(node.ownerDocument.createTextNode(s));
node.insertBefore(f, null);
});
// radio
/*
<block type="radio_on_packet" x="174" y="120">
<mutation callbackproperties="receivedNumber" renamemap="{}"></mutation>
<field name="receivedNumber">receivedNumber</field>
</block>
<block type="radio_on_packet" disabled="true" x="127" y="263">
<mutation callbackproperties="receivedString,receivedNumber" renamemap="{"receivedString":"name","receivedNumber":"value"}"></mutation>
<field name="receivedString">name</field>
<field name="receivedNumber">value</field>
</block>
<block type="radio_on_packet" disabled="true" x="162" y="420">
<mutation callbackproperties="receivedString" renamemap="{}"></mutation>
<field name="receivedString">receivedString</field>
</block>
converts to
<block type="radio_on_number" x="196" y="208">
<field name="HANDLER_receivedNumber" id="DCy(W;1)*jLWQUpoy4Mm" variabletype="">receivedNumber</field>
</block>
<block type="radio_on_value" x="134" y="408">
<field name="HANDLER_name" id="*d-Jm^MJXO]Djs(dTR*?" variabletype="">name</field>
<field name="HANDLER_value" id="A6HQjH[k^X43o3h775+G" variabletype="">value</field>
</block>
<block type="radio_on_string" x="165" y="583">
<field name="HANDLER_receivedString" id="V9KsE!h$(iO?%W:[32CV" variabletype="">receivedString</field>
</block>
*/
const varids: pxt.Map<string> = {};
function addField(node: Element, renameMap: pxt.Map<string>, name: string) {
const f = node.ownerDocument.createElement("field");
f.setAttribute("name", "HANDLER_" + name)
f.setAttribute("id", varids[renameMap[name] || name]);
f.appendChild(node.ownerDocument.createTextNode(name));
node.appendChild(f);
}
pxt.U.toArray(dom.querySelectorAll("variable")).forEach(node => varids[node.innerHTML] = node.getAttribute("id"));
pxt.U.toArray(dom.querySelectorAll("block[type=radio_on_packet]"))
.forEach(node => {
const mutation = node.querySelector("mutation");
if (!mutation) return;
const renameMap = JSON.parse(node.getAttribute("renamemap") || "{}");
const props = mutation.getAttribute("callbackproperties");
if (props) {
const parts = props.split(",");
// It's tempting to generate radio_on_number if parts.length === 0 but
// that would create a variable named "receivedNumber" and possibly shadow
// an existing variable in the user's program. It's safer to stick to the
// old block.
if (parts.length === 1) {
if (parts[0] === "receivedNumber") {
node.setAttribute("type", "radio_on_number");
node.removeChild(node.querySelector("field[name=receivedNumber]"));
addField(node, renameMap, "receivedNumber");
}
else if (parts[0] === "receivedString") {
node.setAttribute("type", "radio_on_string");
node.removeChild(node.querySelector("field[name=receivedString]"));
addField(node, renameMap, "receivedString");
}
else {
return;
}
node.removeChild(mutation);
}
else if (parts.length === 2 && parts.indexOf("receivedNumber") !== -1 && parts.indexOf("receivedString") !== -1) {
node.setAttribute("type", "radio_on_value");
node.removeChild(node.querySelector("field[name=receivedNumber]"));
node.removeChild(node.querySelector("field[name=receivedString]"));
addField(node, renameMap, "name");
addField(node, renameMap, "value");
node.removeChild(mutation);
}
}
})
// device_random now refers to randomRange() so we need to add the missing lower bound argument
pxt.U.toArray(dom.querySelectorAll("block[type=device_random]"))
.concat(pxt.U.toArray(dom.querySelectorAll("shadow[type=device_random]")))
.forEach(node => {
if (getValue(node, "min")) return;
const v = node.ownerDocument.createElement("value");
v.setAttribute("name", "min");
addNumberShadow(v);
node.appendChild(v);
});
/*
<block type="math_arithmetic">
<field name="OP">DIVIDE</field>
<value name="A">
<shadow type="math_number"><field name="NUM">0</field></shadow>
<block type="math_number"><field name="NUM">2</field></block>
</value>
<value name="B">
<shadow type="math_number"><field name="NUM">1</field></shadow>
<block type="math_number"><field name="NUM">3</field></block>
</value>
</block>
*/
pxt.U.toArray(dom.querySelectorAll("block[type=math_arithmetic]"))
.concat(pxt.U.toArray(dom.querySelectorAll("shadow[type=math_arithmetic]")))
.forEach(node => {
const op = getField(node, "OP");
if (!op || op.textContent.trim() !== "DIVIDE") return;
// Convert to integer division
/*
<block type="math_js_op">
<mutation op-type="infix"></mutation>
<field name="OP">idiv</field>
<value name="ARG0">
<shadow type="math_number"><field name="NUM">0</field></shadow>
</value>
<value name="ARG1">
<shadow type="math_number"><field name="NUM">0</field></shadow>
</value>
</block>
*/
node.setAttribute("type", "math_js_op");
op.textContent = "idiv";
const mutation = node.ownerDocument.createElement("mutation");
mutation.setAttribute("op-type", "infix");
// mutation has to be first or Blockly will drop the second argument
node.insertBefore(mutation, node.firstChild);
const a = getValue(node, "A");
if (a) a.setAttribute("name", "ARG0");
const b = getValue(node, "B");
if (b) b.setAttribute("name", "ARG1");
});
renameField(dom, "math_number_minmax", "NUM", "SLIDER");
renameField(dom, "device_note", "note", "name");
}
function renameField(dom: Element, blockType: string, oldName: string, newName: string) {
pxt.U.toArray(dom.querySelectorAll(`block[type=${blockType}]`))
.concat(pxt.U.toArray(dom.querySelectorAll(`shadow[type=${blockType}]`)))
.forEach(node => {
const thefield = getField(node, oldName);
if (thefield) {
thefield.setAttribute("name", newName);
}
});
}
function getField(parent: Element, name: string) {
return getFieldOrValue(parent, name, true);
}
function getValue(parent: Element, name: string) {
return getFieldOrValue(parent, name, false);
}
function getFieldOrValue(parent: Element, name: string, isField: boolean) {
const nodeType = isField ? "field" : "value";
for (let i = 0; i < parent.children.length; i++) {
const child = parent.children.item(i);
if (child.tagName === nodeType && child.getAttribute("name") === name) {
return child;
}
}
return undefined;
}
function addNumberShadow(valueNode: Element) {
const s = valueNode.ownerDocument.createElement("shadow");
s.setAttribute("type", "math_number");
const f = valueNode.ownerDocument.createElement("field");
f.setAttribute("name", "NUM");
f.textContent = "0";
s.appendChild(f);
valueNode.appendChild(s);
}