Skip to content

Commit d3b6ab4

Browse files
authored
The great Blockly swap (#5496)
* fix gesture field editor and types * fix references * bump pxt-core to the new blockly version * bump core and common-packages * bump core again * fix blocks tests * bump core again
1 parent d454f20 commit d3b6ab4

6 files changed

Lines changed: 101 additions & 24 deletions

File tree

docs/projects.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
{
6767
"name": "Courses",
6868
"url": "/courses",
69-
"imageUrl": "/static/courses/csintro.jpg"
69+
"imageUrl": "/static/courses/first-lessons.png"
7070
},
7171
{
7272
"name": "Jacdac",

docs/projects/SUMMARY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@
132132
* [Cyber Arcade: Programming and Making with micro:bit](https://makered.org/resources/cyber-arcade-programming-and-making-with-microbit/)
133133
* [Learn All About micro:bit](https://goo.gl/XTPYpP)
134134
* [Coding and Innovation](https://sites.google.com/view/utahcodingproject/microbits/coding-innovation)
135+
* [micro:bit Starter Lessons](https://mrmorrison.co.uk/microbit/starter/)
136+
* [micro:bit Beyond Basics](https://mrmorrison.co.uk/microbit/beyondbasics/)
137+
* [micro:bit Data and Sustainability](https://mrmorrison.co.uk/microbit/datasustainability/)
135138
* [First Steps](https://microbit.org/get-started/first-steps/introduction/)
136139
* [Make it: code it](https://microbit.org/projects/make-it-code-it/)
137140
* [Networking with the micro:bit](https://microbit.nominetresearch.uk/)

editor/extension.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/// <reference path="../node_modules/pxt-core/localtypings/pxtarget.d.ts" />
2-
/// <reference path="../node_modules/pxt-core/built/pxtblocks.d.ts" />
32
/// <reference path="../node_modules/pxt-core/built/pxtcompiler.d.ts" />
43
/// <reference path="../node_modules/pxt-core/built/pxtlib.d.ts" />
54
/// <reference path="../node_modules/pxt-core/localtypings/pxteditor.d.ts" />

fieldeditors/field_gestures.ts

Lines changed: 90 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,107 @@
1-
/// <reference path="../node_modules/pxt-core/localtypings/blockly.d.ts"/>
2-
/// <reference path="../node_modules/pxt-core/built/pxtblocks.d.ts"/>
3-
/// <reference path="../node_modules/pxt-core/built/pxtsim.d.ts"/>
1+
/// <reference path="../node_modules/pxt-core/localtypings/pxtblockly.d.ts"/>
42

5-
export interface FieldGesturesOptions extends pxtblockly.FieldImagesOptions {
3+
const pxtblockly = pxt.blocks.requirePxtBlockly()
4+
const Blockly = pxt.blocks.requireBlockly();
5+
6+
export interface FieldGesturesOptions {
67
columns?: string;
78
width?: string;
89
}
910

10-
export class FieldGestures extends pxtblockly.FieldImages implements Blockly.FieldCustom {
11+
export class FieldGestures extends pxtblockly.FieldImages {
1112
public isFieldCustom_ = true;
1213

1314
constructor(text: string, options: FieldGesturesOptions, validator?: Function) {
14-
super(text, options, validator);
15-
15+
super(text, options as any, validator);
1616
this.columns_ = parseInt(options.columns) || 4;
1717
this.width_ = parseInt(options.width) || 350;
18+
1819
this.addLabel_ = true;
20+
}
1921

20-
this.renderSelectedImage_ = Blockly.FieldDropdown.prototype.renderSelectedText_;
21-
this.updateSize_ = (Blockly.Field as any).prototype.updateSize_;
22+
protected render_(): void {
23+
if (this.addLabel_) {
24+
this.renderSelectedText_()
25+
this.positionBorderRect_();
26+
}
27+
else {
28+
super.render_();
29+
}
2230
}
2331

24-
trimOptions_() {
32+
/** Renders the selected option, which must be text. */
33+
protected renderSelectedText_() {
34+
// Retrieves the selected option to display through getText_.
35+
this.getTextContent().nodeValue = this.getDisplayText_();
36+
const textElement = this.getTextElement();
37+
Blockly.utils.dom.addClass(textElement, 'blocklyDropdownText');
38+
textElement.setAttribute('text-anchor', 'start');
39+
40+
// Height and width include the border rect.
41+
const hasBorder = !!this.borderRect_;
42+
const height = Math.max(
43+
hasBorder ? this.getConstants()!.FIELD_DROPDOWN_BORDER_RECT_HEIGHT : 0,
44+
this.getConstants()!.FIELD_TEXT_HEIGHT,
45+
);
46+
const textWidth = Blockly.utils.dom.getFastTextWidth(
47+
this.getTextElement(),
48+
this.getConstants()!.FIELD_TEXT_FONTSIZE,
49+
this.getConstants()!.FIELD_TEXT_FONTWEIGHT,
50+
this.getConstants()!.FIELD_TEXT_FONTFAMILY,
51+
);
52+
const xPadding = hasBorder
53+
? this.getConstants()!.FIELD_BORDER_RECT_X_PADDING
54+
: 0;
55+
let arrowWidth = 0;
56+
if (this.getSvgArrow()) {
57+
arrowWidth = this.positionSVGArrow_(
58+
textWidth + xPadding,
59+
height / 2 - this.getConstants()!.FIELD_DROPDOWN_SVG_ARROW_SIZE / 2,
60+
);
61+
}
62+
this.size_.width = textWidth + arrowWidth + xPadding * 2;
63+
this.size_.height = height;
64+
65+
this.positionTextElement_(xPadding, textWidth);
2566
}
2667

27-
protected buttonClick_ = function (e: any) {
28-
let value = e.target.getAttribute('data-value');
29-
this.setValue(value);
30-
Blockly.DropDownDiv.hide();
31-
};
68+
positionSVGArrow_(x: number, y: number): number {
69+
const svgArrow = this.getSvgArrow();
70+
if (!svgArrow) {
71+
return 0;
72+
}
73+
const block = this.getSourceBlock();
74+
const hasBorder = !!this.borderRect_;
75+
const xPadding = hasBorder
76+
? this.getConstants()!.FIELD_BORDER_RECT_X_PADDING
77+
: 0;
78+
const textPadding = this.getConstants()!.FIELD_DROPDOWN_SVG_ARROW_PADDING;
79+
const svgArrowSize = this.getConstants()!.FIELD_DROPDOWN_SVG_ARROW_SIZE;
80+
const arrowX = block.RTL ? xPadding : x + textPadding;
81+
svgArrow.setAttribute(
82+
'transform',
83+
'translate(' + arrowX + ',' + y + ')',
84+
);
85+
return svgArrowSize + textPadding;
86+
}
87+
88+
// This hack exists because svgArrow is private in Blockly's field dropdown.
89+
// It should always be the last image element in the field group
90+
protected getSvgArrow() {
91+
if (this.fieldGroup_) {
92+
const children = this.fieldGroup_.children;
93+
94+
let lastImage: SVGImageElement;
95+
96+
for (let i = 0; i < children.length; i++) {
97+
if (children.item(i).tagName.toLowerCase() === "image") {
98+
lastImage = children.item(i) as SVGImageElement;
99+
}
100+
}
101+
102+
return lastImage;
103+
}
104+
105+
return undefined;
106+
}
32107
}

libs/core/blocks-test/test.blocks

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
<field name="name">AnalogPin.P4</field>
154154
<value name="value">
155155
<shadow type="math_number_minmax" id=",:%8N*FL3wa-zFQ[+^$9">
156-
<mutation min="0" max="1023" label="Value"></mutation>
156+
<mutation min="0" max="1023" label="Value" precision="0"></mutation>
157157
<field name="SLIDER">1023</field>
158158
</shadow>
159159
<block type="device_get_analog_pin">
@@ -165,7 +165,7 @@
165165
<field name="name">DigitalPin.P10</field>
166166
<value name="value">
167167
<shadow type="math_number_minmax" id="W~pvdj%jFEj?EHmT{81Z">
168-
<mutation min="0" max="1" label="Value"></mutation>
168+
<mutation min="0" max="1" label="Value" precision="0"></mutation>
169169
<field name="SLIDER">0</field>
170170
</shadow>
171171
<block type="device_get_digital_pin">
@@ -221,7 +221,7 @@
221221
<field name="name">AnalogPin.P20</field>
222222
<value name="value">
223223
<shadow type="math_number_minmax" id="xC9lYf`{!gA-A8R+#~L.">
224-
<mutation min="0" max="180" label="Value"></mutation>
224+
<mutation min="0" max="180" label="Value" precision="0"></mutation>
225225
<field name="SLIDER">180</field>
226226
</shadow>
227227
<block type="device_acceleration">
@@ -419,7 +419,7 @@
419419
</value>
420420
<value name="brightness">
421421
<shadow type="math_number_minmax" id="IjJTgf5z!,K`qd`~pCB3">
422-
<mutation min="0" max="255" label="Brightness"></mutation>
422+
<mutation min="0" max="255" label="Brightness" precision="0"></mutation>
423423
<field name="SLIDER">255</field>
424424
</shadow>
425425
<block type="device_get_brightness"></block>
@@ -428,7 +428,7 @@
428428
<block type="device_set_brightness">
429429
<value name="value">
430430
<shadow type="math_number_minmax" id="WAUzdT@1z{%:UNn!_dU(">
431-
<mutation min="0" max="255" label="Value"></mutation>
431+
<mutation min="0" max="255" label="Value" precision="0"></mutation>
432432
<field name="SLIDER">255</field>
433433
</shadow>
434434
<block type="device_note">

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"typescript": "4.2.3"
4646
},
4747
"dependencies": {
48-
"pxt-common-packages": "11.1.1",
49-
"pxt-core": "9.3.15"
48+
"pxt-common-packages": "12.0.1",
49+
"pxt-core": "10.0.6"
5050
}
5151
}

0 commit comments

Comments
 (0)