Skip to content

Commit 9a03eea

Browse files
authored
refactor: migrate scratch-blocks to Typescript (#224)
* chore: rename checkbox_buble.js to checkbox_bubble.ts * refactor: convert CheckboxBubble to TS * chore: rename status_indicator_label.js to status_indicator_label.ts * refactor: convert StatusIndicatorLabel to Typescript * chore: rename status_indicator_label_flyout_inflater.js to status_indicator_label_flyout_inflater.ts * refactor: convert StatusIndicatorLabelFlyoutInflater to TS * chore: rename recyclable_block_flyout_inflater.js to recyclable_block_flyout_inflater.ts * refactor: convert RecyclableBlockFlyoutInflater to TS * chore: rename procedures.js to procedures.ts * refactor: modernize and convert procedures.ts to Typescript * chore: rename variables.js to variables.ts * refactor: convert variables.ts to Typescript * refactor: modernize variables.ts * chore: rename shadows.js to shadows.ts * refactor: convert shadows.ts to Typescript * chore: rename scratch_variable_model.js to scratch_variable_model.ts * refactor: convert ScratchVariableModel to Typescript * chore: rename scratch_variable_map.js to scratch_variable_map.ts * refactor: convert ScratchVariableMap to Typescript * chore: rename scratch_dragger.js to scratch_dragger.ts * refactor: convert ScratchDragger to Typescript * chore: add TSDoc to ScratchDragger * chore: rename scratch_continuous_toolbox.js to scratch_continuous_toolbox.ts * refactor: convert ScratchContinuousToolbox to Typescript * chore: rename scratch_continuous_category.js to scratch_continuous_category.ts * refactor: convert ScratchContinuousCategory to Typescript * chore: rename scratch_connection_checker.js to scratch_connection_checker.ts * refactor: convert ScratchConnectionChecker to Typescript * chore: rename scratch_comment_icon.js to scratch_comment_icon.ts * refactor: convert ScratchCommentIcon to Typescript * chore: rename data_category.js to data_category.ts * refactor: convert data_category.ts to Typescript * refactor: modernize data_category.ts * chore: rename scratch_block_paster.js to scratch_block_paster.ts * refactor: convert ScratchBlockPaster to Typescript * chore: rename glows.js to glows.ts * refactor: convert glows.ts to Typescript * chore: rename flyout_checkbox_icon.js to flyout_checkbox_icon.ts * refactor: convert FlyoutCheckboxIcon to Typescript * refactor: convert css.js to Typescript * chore: rename context_menu_items.js to context_menu_items.ts * refactor: convert context_menu_items.ts to Typescript * refactor: convert constants.js to Typescript * chore: add license to constants.ts * refactor: convert colours.js to Typescript * chore: remove the unneded categories.js * chore: rename block_reporting.js to block_reporting.ts * refactor: convert block_reporting.ts to Typescript * chore: rename colour.js to colour.ts * refactor: convert colour.ts to Typescript * refactor: convert control.js to Typescript * refactor: convert event.js to Typescript * refactor: convert looks.js to Typescript * refactor: convert math.js to Typescript * refactor: convert matrix.js to Typescript * refactor: convert motion.js to Typescript * refactor: convert note.js to Typescript * refactor: convert operators.js to Typescript * refactor: convert sensing.js to Typescript * refactor: convert sound.js to Typescript * refactor: convert text.js to Typescript * chore: rename data.js to data.ts * refactor: convert data.ts to Typescript * chore: rename procedures.js to procedures.ts * refactor: convert procedures.ts to Typescript * refactor: modernize procedures.ts * chore: rename vertical_extensions.js to vertical_extensions.ts * refactor: convert vertical_extensions.ts to Typescript * chore: rename field_angle.js to scratch_field_angle.ts * refactor: convert ScratchFieldAngle to Typescript * refactor: clean up ScratchFieldAngle * refactor: add typings for `this` to block definitions * chore: rename field_variable.js to scratch_field_variable.ts * refactor: convert ScratchFieldVariable to Typescript * chore: rename field_vertical_separator.js to field_vertical_separator.ts * refactor: convert FieldVerticalSeparator to Typescript * chore: rename field_variable_getter.js to field_variable_getter.ts * refactor: convert FieldVariableGetter to Typescript * chore: rename field_textinput_removable.js to field_textinput_removable.ts * refactor: convert FieldTextInputRemovable to Typescript * chore: rename field_number.js to scratch_field_number.ts * refactor: convert ScratchFieldNumber to Typescript * chore: rename field_note.js to field_note.ts * refactor: convert FieldNote to Typescript * chore: rename field_matrix.js to field_matrix.ts * refactor: convert FieldMatrix to Typescript * chore: rename field_dropdown.js to scratch_field_dropdown.ts * refactor: convert ScratchFieldDropdown to Typescript * chore: rename field_colour_slider.js to field_colour_slider.ts * refactor: convert FieldColourSlider to Typescript * chore: rename events_block_comment_base.js to events_block_comment_base.ts * refactor: convert BlockCommentBase to Typescript * chore: rename events_block_comment_change.js to events_block_comment_change.ts * refactor: convert BlockCommentChange to Typescript * chore: rename events_block_comment_collapse.js to events_block_comment_collapse.ts * refactor: convert BlockCommentCollapse to Typescript * chore: rename events_block_comment_create.js to events_block_comment_create.ts * refactor: convert BlockCommentCreate to Typescript * chore: rename events_block_comment_delete.js to events_block_comment_delete.ts * refactor: convert BlockCommentDelete to Typescript * chore: rename events_block_comment_move.js to events_block_comment_move.ts * refactor: convert BlockCommentMove to Typescript * chore: rename events_block_comment_resize.js to events_block_comment_resize.ts * refactor: convert BlockCommentResize to Typescript * chore: rename events_block_drag_end.js to events_block_drag_end.ts * refactor: convert BlockDragEnd to Typescript * chore: rename events_block_drag_outside.js to events_block_drag_outside.ts * refactor: convert BlockDragOutside to Typescript * chore: rename events_scratch_variable_create.js to events_scratch_variable_create.ts * refactor: convert ScratchVariableCreate to Typescript * chore: rename bowler_hat.js to bowler_hat.ts * refactor: convert BowlerHat to Typescript * chore: rename constants.js to constants.ts * refactor: convert ConstantProvider to Typescript * chore: rename drawer.js to drawer.ts * refactor: convert Drawer to Typescript * chore: rename path_object.js to path_object.ts * refactor: convert PathObject to Typescript * chore: rename render_info.js to render_info.ts * refactor: convert RenderInfo to Typescript * chore: rename renderer.js to renderer.ts * refactor: convert Renderer to Typescript * chore: rename scratch_blocks_utils.js to scratch_blocks_utils.ts * refactor: remove unused functions * refactor: convert scratch_blocks_utils.ts to Typescript
1 parent 75e6e6c commit 9a03eea

File tree

76 files changed

+3494
-3282
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+3494
-3282
lines changed
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import * as Blockly from "blockly/core";
2-
import { Colours } from "./colours.js";
2+
import { Colours } from "./colours";
33

4-
export function reportValue(id, value) {
5-
const block =
6-
Blockly.getMainWorkspace().getBlockById(id) ||
7-
Blockly.getMainWorkspace().getFlyout().getWorkspace().getBlockById(id);
4+
export function reportValue(id: string, value: string) {
5+
const block = (Blockly.getMainWorkspace().getBlockById(id) ||
6+
(Blockly.getMainWorkspace() as Blockly.WorkspaceSvg)
7+
.getFlyout()
8+
.getWorkspace()
9+
.getBlockById(id)) as Blockly.BlockSvg;
810
if (!block) {
911
throw "Tried to report value on block that does not exist.";
1012
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@
2323
* @author fraser@google.com (Neil Fraser)
2424
*/
2525
import * as Blockly from "blockly/core";
26-
import * as Constants from "../constants.js";
26+
import * as Constants from "../constants";
2727

2828
/**
2929
* Pick a random colour.
30-
* @return {string} #RRGGBB for random colour.
30+
*
31+
* @returns #RRGGBB for random colour.
3132
*/
32-
function randomColour() {
33-
var num = Math.floor(Math.random() * Math.pow(2, 24));
33+
function randomColour(): string {
34+
const num = Math.floor(Math.random() * Math.pow(2, 24));
3435
return "#" + ("00000" + num.toString(16)).substr(-6);
3536
}
3637

3738
Blockly.Blocks["colour_picker"] = {
3839
/**
3940
* Block for colour picker.
40-
* @this Blockly.Block
4141
*/
42-
init: function () {
42+
init: function (this: Blockly.Block) {
4343
this.jsonInit({
4444
message0: "%1",
4545
args0: [
Lines changed: 21 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@
1919
*/
2020

2121
import * as Blockly from "blockly/core";
22-
import { Categories } from "../categories.js";
2322

2423
Blockly.Blocks["control_forever"] = {
2524
/**
2625
* Block for repeat n times (external number).
2726
* https://blockly-demo.appspot.com/static/demos/blockfactory/index.html#5eke39
28-
* @this Blockly.Block
2927
*/
30-
init: function () {
28+
init: function (this: Blockly.Block) {
3129
const ws = this.workspace.options.parentWorkspace || this.workspace;
3230
this.jsonInit({
3331
id: "control_forever",
@@ -51,7 +49,6 @@ Blockly.Blocks["control_forever"] = {
5149
flip_rtl: true,
5250
},
5351
],
54-
category: Categories.control,
5552
extensions: ["colours_control", "shape_end"],
5653
});
5754
},
@@ -61,9 +58,8 @@ Blockly.Blocks["control_repeat"] = {
6158
/**
6259
* Block for repeat n times (external number).
6360
* https://blockly-demo.appspot.com/static/demos/blockfactory/index.html#so57n9
64-
* @this Blockly.Block
6561
*/
66-
init: function () {
62+
init: function (this: Blockly.Block) {
6763
const ws = this.workspace.options.parentWorkspace || this.workspace;
6864
this.jsonInit({
6965
id: "control_repeat",
@@ -93,7 +89,6 @@ Blockly.Blocks["control_repeat"] = {
9389
flip_rtl: true,
9490
},
9591
],
96-
category: Categories.control,
9792
extensions: ["colours_control", "shape_statement"],
9893
});
9994
},
@@ -102,9 +97,8 @@ Blockly.Blocks["control_repeat"] = {
10297
Blockly.Blocks["control_if"] = {
10398
/**
10499
* Block for if-then.
105-
* @this Blockly.Block
106100
*/
107-
init: function () {
101+
init: function (this: Blockly.Block) {
108102
this.jsonInit({
109103
type: "control_if",
110104
message0: Blockly.Msg.CONTROL_IF,
@@ -122,7 +116,6 @@ Blockly.Blocks["control_if"] = {
122116
name: "SUBSTACK",
123117
},
124118
],
125-
category: Categories.control,
126119
extensions: ["colours_control", "shape_statement"],
127120
});
128121
},
@@ -131,9 +124,8 @@ Blockly.Blocks["control_if"] = {
131124
Blockly.Blocks["control_if_else"] = {
132125
/**
133126
* Block for if-else.
134-
* @this Blockly.Block
135127
*/
136-
init: function () {
128+
init: function (this: Blockly.Block) {
137129
this.jsonInit({
138130
type: "control_if_else",
139131
message0: Blockly.Msg.CONTROL_IF,
@@ -159,7 +151,6 @@ Blockly.Blocks["control_if_else"] = {
159151
name: "SUBSTACK2",
160152
},
161153
],
162-
category: Categories.control,
163154
extensions: ["colours_control", "shape_statement"],
164155
});
165156
},
@@ -168,13 +159,12 @@ Blockly.Blocks["control_if_else"] = {
168159
Blockly.Blocks["control_stop"] = {
169160
/**
170161
* Block for stop all scripts.
171-
* @this Blockly.Block
172162
*/
173-
init: function () {
174-
var ALL_SCRIPTS = "all";
175-
var THIS_SCRIPT = "this script";
176-
var OTHER_SCRIPTS = "other scripts in sprite";
177-
var stopDropdown = new Blockly.FieldDropdown(
163+
init: function (this: Blockly.Block) {
164+
const ALL_SCRIPTS = "all";
165+
const THIS_SCRIPT = "this script";
166+
const OTHER_SCRIPTS = "other scripts in sprite";
167+
const stopDropdown = new Blockly.FieldDropdown(
178168
function () {
179169
if (
180170
this.sourceBlock_ &&
@@ -205,9 +195,8 @@ Blockly.Blocks["control_stop"] = {
205195
Blockly.Blocks["control_wait"] = {
206196
/**
207197
* Block to wait (pause) stack.
208-
* @this Blockly.Block
209198
*/
210-
init: function () {
199+
init: function (this: Blockly.Block) {
211200
this.jsonInit({
212201
id: "control_wait",
213202
message0: Blockly.Msg.CONTROL_WAIT,
@@ -217,7 +206,6 @@ Blockly.Blocks["control_wait"] = {
217206
name: "DURATION",
218207
},
219208
],
220-
category: Categories.control,
221209
extensions: ["colours_control", "shape_statement"],
222210
});
223211
},
@@ -226,9 +214,8 @@ Blockly.Blocks["control_wait"] = {
226214
Blockly.Blocks["control_wait_until"] = {
227215
/**
228216
* Block to wait until a condition becomes true.
229-
* @this Blockly.Block
230217
*/
231-
init: function () {
218+
init: function (this: Blockly.Block) {
232219
this.jsonInit({
233220
message0: Blockly.Msg.CONTROL_WAITUNTIL,
234221
args0: [
@@ -238,7 +225,6 @@ Blockly.Blocks["control_wait_until"] = {
238225
check: "Boolean",
239226
},
240227
],
241-
category: Categories.control,
242228
extensions: ["colours_control", "shape_statement"],
243229
});
244230
},
@@ -247,9 +233,8 @@ Blockly.Blocks["control_wait_until"] = {
247233
Blockly.Blocks["control_repeat_until"] = {
248234
/**
249235
* Block to repeat until a condition becomes true.
250-
* @this Blockly.Block
251236
*/
252-
init: function () {
237+
init: function (this: Blockly.Block) {
253238
const ws = this.workspace.options.parentWorkspace || this.workspace;
254239
this.jsonInit({
255240
message0: Blockly.Msg.CONTROL_REPEATUNTIL,
@@ -279,7 +264,6 @@ Blockly.Blocks["control_repeat_until"] = {
279264
flip_rtl: true,
280265
},
281266
],
282-
category: Categories.control,
283267
extensions: ["colours_control", "shape_statement"],
284268
});
285269
},
@@ -290,7 +274,7 @@ Blockly.Blocks["control_while"] = {
290274
* Block to repeat until a condition becomes false.
291275
* (This is an obsolete "hacked" block, for compatibility with 2.0.)
292276
*/
293-
init: function () {
277+
init: function (this: Blockly.Block) {
294278
const ws = this.workspace.options.parentWorkspace || this.workspace;
295279
this.jsonInit({
296280
message0: Blockly.Msg.CONTROL_WHILE,
@@ -320,7 +304,6 @@ Blockly.Blocks["control_while"] = {
320304
flip_rtl: true,
321305
},
322306
],
323-
category: Categories.control,
324307
extensions: ["colours_control", "shape_statement"],
325308
});
326309
},
@@ -330,9 +313,8 @@ Blockly.Blocks["control_for_each"] = {
330313
/**
331314
* Block for for-each. This is an obsolete block that is implemented for
332315
* compatibility with Scratch 2.0 projects.
333-
* @this Blockly.Block
334316
*/
335-
init: function () {
317+
init: function (this: Blockly.Block) {
336318
this.jsonInit({
337319
type: "control_for_each",
338320
message0: Blockly.Msg.CONTROL_FOREACH,
@@ -353,7 +335,6 @@ Blockly.Blocks["control_for_each"] = {
353335
name: "SUBSTACK",
354336
},
355337
],
356-
category: Categories.control,
357338
extensions: ["colours_control", "shape_statement"],
358339
});
359340
},
@@ -362,14 +343,12 @@ Blockly.Blocks["control_for_each"] = {
362343
Blockly.Blocks["control_start_as_clone"] = {
363344
/**
364345
* Block for "when I start as a clone" hat.
365-
* @this Blockly.Block
366346
*/
367-
init: function () {
347+
init: function (this: Blockly.Block) {
368348
this.jsonInit({
369349
id: "control_start_as_clone",
370350
message0: Blockly.Msg.CONTROL_STARTASCLONE,
371351
args0: [],
372-
category: Categories.control,
373352
extensions: ["colours_control", "shape_hat"],
374353
});
375354
},
@@ -383,9 +362,8 @@ Blockly.Blocks["control_create_clone_of_menu"] = {};
383362
Blockly.Blocks["control_create_clone_of"] = {
384363
/**
385364
* Block for "create clone of..."
386-
* @this Blockly.Block
387365
*/
388-
init: function () {
366+
init: function (this: Blockly.Block) {
389367
this.jsonInit({
390368
id: "control_start_as_clone",
391369
message0: Blockly.Msg.CONTROL_CREATECLONEOF,
@@ -395,7 +373,6 @@ Blockly.Blocks["control_create_clone_of"] = {
395373
name: "CLONE_OPTION",
396374
},
397375
],
398-
category: Categories.control,
399376
extensions: ["colours_control", "shape_statement"],
400377
});
401378
},
@@ -404,13 +381,11 @@ Blockly.Blocks["control_create_clone_of"] = {
404381
Blockly.Blocks["control_delete_this_clone"] = {
405382
/**
406383
* Block for "delete this clone."
407-
* @this Blockly.Block
408384
*/
409-
init: function () {
385+
init: function (this: Blockly.Block) {
410386
this.jsonInit({
411387
message0: Blockly.Msg.CONTROL_DELETETHISCLONE,
412388
args0: [],
413-
category: Categories.control,
414389
extensions: ["colours_control", "shape_end"],
415390
});
416391
},
@@ -420,12 +395,10 @@ Blockly.Blocks["control_get_counter"] = {
420395
/**
421396
* Block to get the counter value. This is an obsolete block that is
422397
* implemented for compatibility with Scratch 2.0 projects.
423-
* @this Blockly.Block
424398
*/
425-
init: function () {
399+
init: function (this: Blockly.Block) {
426400
this.jsonInit({
427401
message0: Blockly.Msg.CONTROL_COUNTER,
428-
category: Categories.control,
429402
extensions: ["colours_control", "output_number"],
430403
});
431404
},
@@ -435,12 +408,10 @@ Blockly.Blocks["control_incr_counter"] = {
435408
/**
436409
* Block to add one to the counter value. This is an obsolete block that is
437410
* implemented for compatibility with Scratch 2.0 projects.
438-
* @this Blockly.Block
439411
*/
440-
init: function () {
412+
init: function (this: Blockly.Block) {
441413
this.jsonInit({
442414
message0: Blockly.Msg.CONTROL_INCRCOUNTER,
443-
category: Categories.control,
444415
extensions: ["colours_control", "shape_statement"],
445416
});
446417
},
@@ -450,12 +421,10 @@ Blockly.Blocks["control_clear_counter"] = {
450421
/**
451422
* Block to clear the counter value. This is an obsolete block that is
452423
* implemented for compatibility with Scratch 2.0 projects.
453-
* @this Blockly.Block
454424
*/
455-
init: function () {
425+
init: function (this: Blockly.Block) {
456426
this.jsonInit({
457427
message0: Blockly.Msg.CONTROL_CLEARCOUNTER,
458-
category: Categories.control,
459428
extensions: ["colours_control", "shape_statement"],
460429
});
461430
},
@@ -473,9 +442,8 @@ Blockly.Blocks["control_all_at_once"] = {
473442
* reporter that is always true (e.g. "if 1 = 1"). Also note that the
474443
* Scratch 2.0 spec for this block is "warpSpeed", but the label shows
475444
* "all at once".
476-
* @this Blockly.Block
477445
*/
478-
init: function () {
446+
init: function (this: Blockly.Block) {
479447
this.jsonInit({
480448
message0: Blockly.Msg.CONTROL_ALLATONCE,
481449
message1: "%1", // Statement
@@ -485,7 +453,6 @@ Blockly.Blocks["control_all_at_once"] = {
485453
name: "SUBSTACK",
486454
},
487455
],
488-
category: Categories.control,
489456
extensions: ["colours_control", "shape_statement"],
490457
});
491458
},

0 commit comments

Comments
 (0)