scratch-blocks 2.0.0-spork.2 → 2.0.0-spork.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/main.js +1 -1
  3. package/package.json +1 -1
  4. package/src/{block_reporting.js → block_reporting.ts} +7 -5
  5. package/src/blocks/{colour.js → colour.ts} +6 -6
  6. package/src/blocks/{control.js → control.ts} +21 -54
  7. package/src/blocks/{data.js → data.ts} +134 -142
  8. package/src/blocks/{event.js → event.ts} +12 -33
  9. package/src/blocks/{looks.js → looks.ts} +24 -73
  10. package/src/blocks/{math.js → math.ts} +6 -11
  11. package/src/blocks/{matrix.js → matrix.ts} +2 -3
  12. package/src/blocks/{motion.js → motion.ts} +23 -70
  13. package/src/blocks/{note.js → note.ts} +2 -3
  14. package/src/blocks/{operators.js → operators.ts} +18 -55
  15. package/src/blocks/{procedures.js → procedures.ts} +418 -269
  16. package/src/blocks/{sensing.js → sensing.ts} +21 -61
  17. package/src/blocks/{sound.js → sound.ts} +9 -28
  18. package/src/blocks/{text.js → text.ts} +1 -2
  19. package/src/blocks/{vertical_extensions.js → vertical_extensions.ts} +63 -100
  20. package/src/checkable_continuous_flyout.js +2 -2
  21. package/src/{checkbox_bubble.js → checkbox_bubble.ts} +36 -53
  22. package/src/{colours.js → colours.ts} +11 -4
  23. package/src/{constants.js → constants.ts} +13 -0
  24. package/src/{context_menu_items.js → context_menu_items.ts} +18 -12
  25. package/src/{data_category.js → data_category.ts} +216 -150
  26. package/src/events/{events_block_comment_base.js → events_block_comment_base.ts} +23 -4
  27. package/src/events/{events_block_comment_change.js → events_block_comment_change.ts} +29 -5
  28. package/src/events/{events_block_comment_collapse.js → events_block_comment_collapse.ts} +24 -6
  29. package/src/events/{events_block_comment_create.js → events_block_comment_create.ts} +36 -10
  30. package/src/events/{events_block_comment_delete.js → events_block_comment_delete.ts} +6 -2
  31. package/src/events/{events_block_comment_move.js → events_block_comment_move.ts} +36 -6
  32. package/src/events/events_block_comment_resize.ts +88 -0
  33. package/src/events/events_block_drag_end.ts +49 -0
  34. package/src/events/events_block_drag_outside.ts +44 -0
  35. package/src/events/{events_scratch_variable_create.js → events_scratch_variable_create.ts} +28 -15
  36. package/src/fields/{field_colour_slider.js → field_colour_slider.ts} +117 -106
  37. package/src/fields/{field_matrix.js → field_matrix.ts} +189 -215
  38. package/src/fields/{field_note.js → field_note.ts} +227 -286
  39. package/src/fields/{field_textinput_removable.js → field_textinput_removable.ts} +17 -20
  40. package/src/fields/{field_variable_getter.js → field_variable_getter.ts} +28 -17
  41. package/src/fields/{field_vertical_separator.js → field_vertical_separator.ts} +14 -30
  42. package/src/fields/{field_angle.js → scratch_field_angle.ts} +124 -80
  43. package/src/fields/{field_dropdown.js → scratch_field_dropdown.ts} +9 -7
  44. package/src/fields/{field_number.js → scratch_field_number.ts} +60 -55
  45. package/src/fields/{field_variable.js → scratch_field_variable.ts} +46 -27
  46. package/src/{flyout_checkbox_icon.js → flyout_checkbox_icon.ts} +15 -19
  47. package/src/{glows.js → glows.ts} +29 -18
  48. package/src/index.ts +59 -60
  49. package/src/procedures.ts +462 -0
  50. package/src/{recyclable_block_flyout_inflater.js → recyclable_block_flyout_inflater.ts} +35 -35
  51. package/src/renderer/{bowler_hat.js → bowler_hat.ts} +1 -1
  52. package/src/renderer/{constants.js → constants.ts} +26 -12
  53. package/src/renderer/{drawer.js → drawer.ts} +8 -3
  54. package/src/renderer/{path_object.js → path_object.ts} +2 -2
  55. package/src/renderer/{render_info.js → render_info.ts} +19 -7
  56. package/src/renderer/renderer.ts +76 -0
  57. package/src/{scratch_block_paster.js → scratch_block_paster.ts} +9 -7
  58. package/src/scratch_blocks_utils.ts +39 -0
  59. package/src/{scratch_comment_icon.js → scratch_comment_icon.ts} +43 -26
  60. package/src/scratch_connection_checker.ts +44 -0
  61. package/src/{scratch_continuous_category.js → scratch_continuous_category.ts} +20 -13
  62. package/src/{scratch_continuous_toolbox.js → scratch_continuous_toolbox.ts} +20 -18
  63. package/src/{scratch_dragger.js → scratch_dragger.ts} +97 -28
  64. package/src/{scratch_variable_map.js → scratch_variable_map.ts} +4 -1
  65. package/src/scratch_variable_model.ts +30 -0
  66. package/src/{shadows.js → shadows.ts} +8 -4
  67. package/src/{status_indicator_label.js → status_indicator_label.ts} +24 -36
  68. package/src/{status_indicator_label_flyout_inflater.js → status_indicator_label_flyout_inflater.ts} +9 -7
  69. package/src/{variables.js → variables.ts} +153 -123
  70. package/tsconfig.json +5 -0
  71. package/src/categories.js +0 -15
  72. package/src/events/events_block_comment_resize.js +0 -52
  73. package/src/events/events_block_drag_end.js +0 -33
  74. package/src/events/events_block_drag_outside.js +0 -30
  75. package/src/procedures.js +0 -425
  76. package/src/renderer/renderer.js +0 -74
  77. package/src/scratch_blocks_utils.js +0 -148
  78. package/src/scratch_connection_checker.js +0 -29
  79. package/src/scratch_variable_model.js +0 -24
  80. /package/src/{css.js → css.ts} +0 -0
  81. /package/{continuous-toolbox.d.ts → types/continuous-toolbox.d.ts} +0 -0
@@ -25,181 +25,152 @@
25
25
  */
26
26
  import * as Blockly from "blockly/core";
27
27
 
28
+ enum PaintStyle {
29
+ FILL = "fill",
30
+ CLEAR = "clear",
31
+ }
32
+
33
+ enum LEDState {
34
+ ON = "1",
35
+ OFF = "0",
36
+ }
37
+
28
38
  /**
29
39
  * Class for a matrix field.
30
- * @param {number} matrix The default matrix value represented by a 25-bit integer.
31
- * @extends {Blockly.Field}
32
- * @constructor
33
40
  */
34
- class FieldMatrix extends Blockly.Field {
35
- originalStyle;
36
-
37
- constructor(matrix) {
38
- super(matrix);
39
- /**
40
- * Array of SVGElement<rect> for matrix thumbnail image on block field.
41
- * @type {!Array<SVGElement>}
42
- * @private
43
- */
44
- this.ledThumbNodes_ = [];
45
- /**
46
- * Array of SVGElement<rect> for matrix editor in dropdown menu.
47
- * @type {!Array<SVGElement>}
48
- * @private
49
- */
50
- this.ledButtons_ = [];
51
- /**
52
- * SVGElement for LED matrix in editor.
53
- * @type {?SVGElement}
54
- * @private
55
- */
56
- this.matrixStage_ = null;
57
- /**
58
- * SVG image for dropdown arrow.
59
- * @type {?SVGElement}
60
- * @private
61
- */
62
- this.arrow_ = null;
63
- /**
64
- * String indicating matrix paint style.
65
- * value can be [null, 'fill', 'clear'].
66
- * @type {?String}
67
- * @private
68
- */
69
- this.paintStyle_ = null;
70
- /**
71
- * Touch event wrapper.
72
- * Runs when the field is selected.
73
- * @type {!Array}
74
- * @private
75
- */
76
- this.mouseDownWrapper_ = null;
77
- /**
78
- * Touch event wrapper.
79
- * Runs when the clear button editor button is selected.
80
- * @type {!Array}
81
- * @private
82
- */
83
- this.clearButtonWrapper_ = null;
84
- /**
85
- * Touch event wrapper.
86
- * Runs when the fill button editor button is selected.
87
- * @type {!Array}
88
- * @private
89
- */
90
- this.fillButtonWrapper_ = null;
91
- /**
92
- * Touch event wrapper.
93
- * Runs when the matrix editor is touched.
94
- * @type {!Array}
95
- * @private
96
- */
97
- this.matrixTouchWrapper_ = null;
98
- /**
99
- * Touch event wrapper.
100
- * Runs when the matrix editor touch event moves.
101
- * @type {!Array}
102
- * @private
103
- */
104
- this.matrixMoveWrapper_ = null;
105
- /**
106
- * Touch event wrapper.
107
- * Runs when the matrix editor is released.
108
- * @type {!Array}
109
- * @private
110
- */
111
- this.matrixReleaseWrapper_ = null;
41
+ class FieldMatrix extends Blockly.Field<string> {
42
+ private originalStyle?: string;
112
43
 
113
- this.SERIALIZABLE = true;
114
- }
44
+ /**
45
+ * Array of SVGElement<rect> for matrix thumbnail image on block field.
46
+ */
47
+ private ledThumbNodes_: SVGElement[] = [];
48
+ /**
49
+ * Array of SVGElement<rect> for matrix editor in dropdown menu.
50
+ */
51
+ private ledButtons_: SVGElement[] = [];
52
+
53
+ /**
54
+ * SVGElement for LED matrix in editor.
55
+ */
56
+ private matrixStage_: SVGElement | null = null;
57
+
58
+ /**
59
+ * SVG image for dropdown arrow.
60
+ */
61
+ private arrow_: SVGElement | null = null;
62
+
63
+ /**
64
+ * String indicating matrix paint style.
65
+ * value can be [null, 'fill', 'clear'].
66
+ */
67
+ private paintStyle_: PaintStyle | null = null;
68
+
69
+ /**
70
+ * Touch event wrapper.
71
+ * Runs when the field is selected.
72
+ */
73
+ private mouseDownWrapper_: Blockly.browserEvents.Data | null = null;
74
+
75
+ /**
76
+ * Touch event wrapper.
77
+ * Runs when the clear button editor button is selected.
78
+ */
79
+ private clearButtonWrapper_: Blockly.browserEvents.Data | null = null;
80
+
81
+ /**
82
+ * Touch event wrapper.
83
+ * Runs when the fill button editor button is selected.
84
+ */
85
+ private fillButtonWrapper_: Blockly.browserEvents.Data | null = null;
86
+
87
+ /**
88
+ * Touch event wrapper.
89
+ * Runs when the matrix editor is touched.
90
+ */
91
+ private matrixTouchWrapper_: Blockly.browserEvents.Data | null = null;
92
+
93
+ /**
94
+ * Touch event wrapper.
95
+ * Runs when the matrix editor touch event moves.
96
+ */
97
+ private matrixMoveWrapper_: Blockly.browserEvents.Data | null = null;
98
+
99
+ /**
100
+ * Touch event wrapper.
101
+ * Runs when the matrix editor is released.
102
+ */
103
+ private matrixReleaseWrapper_: Blockly.browserEvents.Data | null = null;
104
+
105
+ SERIALIZABLE = true;
115
106
 
116
107
  /**
117
108
  * Construct a FieldMatrix from a JSON arg object.
118
- * @param {!Object} options A JSON object with options (matrix).
119
- * @returns {!Blockly.FieldMatrix} The new field instance.
120
- * @package
121
- * @nocollapse
109
+ * @param options A JSON object with options (matrix).
110
+ * @returns The new field instance.
122
111
  */
123
- static fromJson(options) {
112
+ static fromJson(options: FieldMatrixConfig): FieldMatrix {
124
113
  return new FieldMatrix(options["matrix"]);
125
114
  }
126
115
 
127
116
  /**
128
117
  * Fixed size of the matrix thumbnail in the input field, in px.
129
- * @type {number}
130
- * @const
131
118
  */
132
- static THUMBNAIL_SIZE = 26;
119
+ static readonly THUMBNAIL_SIZE = 26;
133
120
 
134
121
  /**
135
122
  * Fixed size of each matrix thumbnail node, in px.
136
- * @type {number}
137
- * @const
138
123
  */
139
- static THUMBNAIL_NODE_SIZE = 4;
124
+ static readonly THUMBNAIL_NODE_SIZE = 4;
140
125
 
141
126
  /**
142
127
  * Fixed size of each matrix thumbnail node, in px.
143
- * @type {number}
144
- * @const
145
128
  */
146
- static THUMBNAIL_NODE_PAD = 1;
129
+ static readonly THUMBNAIL_NODE_PAD = 1;
147
130
 
148
131
  /**
149
132
  * Fixed size of arrow icon in drop down menu, in px.
150
- * @type {number}
151
- * @const
152
133
  */
153
- static ARROW_SIZE = 12;
134
+ static readonly ARROW_SIZE = 12;
154
135
 
155
136
  /**
156
137
  * Fixed size of each button inside the 5x5 matrix, in px.
157
- * @type {number}
158
- * @const
159
138
  */
160
- static MATRIX_NODE_SIZE = 18;
139
+ static readonly MATRIX_NODE_SIZE = 18;
161
140
 
162
141
  /**
163
142
  * Fixed corner radius for 5x5 matrix buttons, in px.
164
- * @type {number}
165
- * @const
166
143
  */
167
- static MATRIX_NODE_RADIUS = 4;
144
+ static readonly MATRIX_NODE_RADIUS = 4;
168
145
 
169
146
  /**
170
147
  * Fixed padding for 5x5 matrix buttons, in px.
171
- * @type {number}
172
- * @const
173
148
  */
174
- static MATRIX_NODE_PAD = 5;
149
+ static readonly MATRIX_NODE_PAD = 5;
175
150
 
176
151
  /**
177
152
  * String with 25 '0' chars.
178
153
  * Used for clearing a matrix or filling an LED node array.
179
- * @type {string}
180
- * @const
181
154
  */
182
- static ZEROS = "0000000000000000000000000";
155
+ static readonly ZEROS = "0000000000000000000000000";
183
156
 
184
157
  /**
185
158
  * String with 25 '1' chars.
186
159
  * Used for filling a matrix.
187
- * @type {string}
188
- * @const
189
160
  */
190
- static ONES = "1111111111111111111111111";
161
+ static readonly ONES = "1111111111111111111111111";
191
162
 
192
163
  /**
193
164
  * Called when the field is placed on a block.
194
- * @param {Block} block The owning block.
195
165
  */
196
166
  initView() {
197
167
  // Build the DOM.
198
168
  this.updateSize_();
199
- const dropdownArrowPadding = this.getConstants().GRID_UNIT * 2;
200
- var thumbX = dropdownArrowPadding / 2;
201
- var thumbY = (this.size_.height - FieldMatrix.THUMBNAIL_SIZE) / 2;
202
- var thumbnail = Blockly.utils.dom.createSvgElement(
169
+ const dropdownArrowPadding =
170
+ (this.getConstants() as Blockly.zelos.ConstantProvider).GRID_UNIT * 2;
171
+ const thumbX = dropdownArrowPadding / 2;
172
+ const thumbY = (this.size_.height - FieldMatrix.THUMBNAIL_SIZE) / 2;
173
+ const thumbnail = Blockly.utils.dom.createSvgElement(
203
174
  "g",
204
175
  {
205
176
  transform: "translate(" + thumbX + ", " + thumbY + ")",
@@ -209,11 +180,11 @@ class FieldMatrix extends Blockly.Field {
209
180
  this.fieldGroup_
210
181
  );
211
182
  this.ledThumbNodes_ = [];
212
- var nodeSize = FieldMatrix.THUMBNAIL_NODE_SIZE;
213
- var nodePad = FieldMatrix.THUMBNAIL_NODE_PAD;
214
- for (var i = 0; i < 5; i++) {
215
- for (var n = 0; n < 5; n++) {
216
- var attr = {
183
+ const nodeSize = FieldMatrix.THUMBNAIL_NODE_SIZE;
184
+ const nodePad = FieldMatrix.THUMBNAIL_NODE_PAD;
185
+ for (let i = 0; i < 5; i++) {
186
+ for (let n = 0; n < 5; n++) {
187
+ const attr = {
217
188
  x: (nodeSize + nodePad) * n + nodePad,
218
189
  y: (nodeSize + nodePad) * i + nodePad,
219
190
  width: nodeSize,
@@ -230,8 +201,8 @@ class FieldMatrix extends Blockly.Field {
230
201
  }
231
202
 
232
203
  if (!this.arrow_) {
233
- var arrowX = FieldMatrix.THUMBNAIL_SIZE + dropdownArrowPadding * 1.5;
234
- var arrowY = (this.size_.height - FieldMatrix.ARROW_SIZE) / 2;
204
+ const arrowX = FieldMatrix.THUMBNAIL_SIZE + dropdownArrowPadding * 1.5;
205
+ const arrowY = (this.size_.height - FieldMatrix.ARROW_SIZE) / 2;
235
206
  this.arrow_ = Blockly.utils.dom.createSvgElement(
236
207
  "image",
237
208
  {
@@ -250,13 +221,13 @@ class FieldMatrix extends Blockly.Field {
250
221
  }
251
222
  }
252
223
 
253
- doClassValidation_(matrix) {
224
+ doClassValidation_(matrix: string) {
254
225
  return matrix
255
226
  ? matrix + FieldMatrix.ZEROS.substr(0, 25 - matrix.length)
256
227
  : matrix;
257
228
  }
258
229
 
259
- doValueUpdate_(newValue) {
230
+ doValueUpdate_(newValue: string) {
260
231
  super.doValueUpdate_(newValue);
261
232
  if (newValue) {
262
233
  this.updateMatrix_();
@@ -265,12 +236,11 @@ class FieldMatrix extends Blockly.Field {
265
236
 
266
237
  /**
267
238
  * Show the drop-down menu for editing this field.
268
- * @private
269
239
  */
270
240
  showEditor_() {
271
- var div = Blockly.DropDownDiv.getContentDiv();
241
+ const div = Blockly.DropDownDiv.getContentDiv();
272
242
  // Build the SVG DOM.
273
- var matrixSize =
243
+ const matrixSize =
274
244
  FieldMatrix.MATRIX_NODE_SIZE * 5 + FieldMatrix.MATRIX_NODE_PAD * 6;
275
245
  this.matrixStage_ = Blockly.utils.dom.createSvgElement(
276
246
  "svg",
@@ -286,15 +256,15 @@ class FieldMatrix extends Blockly.Field {
286
256
  );
287
257
  // Create the 5x5 matrix
288
258
  this.ledButtons_ = [];
289
- for (var i = 0; i < 5; i++) {
290
- for (var n = 0; n < 5; n++) {
291
- var x =
259
+ for (let i = 0; i < 5; i++) {
260
+ for (let n = 0; n < 5; n++) {
261
+ const x =
292
262
  FieldMatrix.MATRIX_NODE_SIZE * n +
293
263
  FieldMatrix.MATRIX_NODE_PAD * (n + 1);
294
- var y =
264
+ const y =
295
265
  FieldMatrix.MATRIX_NODE_SIZE * i +
296
266
  FieldMatrix.MATRIX_NODE_PAD * (i + 1);
297
- var attr = {
267
+ const attr = {
298
268
  x: x + "px",
299
269
  y: y + "px",
300
270
  width: FieldMatrix.MATRIX_NODE_SIZE,
@@ -302,7 +272,7 @@ class FieldMatrix extends Blockly.Field {
302
272
  rx: FieldMatrix.MATRIX_NODE_RADIUS,
303
273
  ry: FieldMatrix.MATRIX_NODE_RADIUS,
304
274
  };
305
- var led = Blockly.utils.dom.createSvgElement(
275
+ const led = Blockly.utils.dom.createSvgElement(
306
276
  "rect",
307
277
  attr,
308
278
  this.matrixStage_
@@ -312,18 +282,17 @@ class FieldMatrix extends Blockly.Field {
312
282
  }
313
283
  }
314
284
  // Div for lower button menu
315
- var buttonDiv = document.createElement("div");
285
+ const buttonDiv = document.createElement("div");
316
286
  // Button to clear matrix
317
- var clearButtonDiv = document.createElement("div");
287
+ const clearButtonDiv = document.createElement("div");
318
288
  clearButtonDiv.className = "scratchMatrixButtonDiv";
319
- var clearButton = this.createButton_(
320
- this.sourceBlock_.getColourSecondary()
321
- );
289
+ const sourceBlock = this.getSourceBlock() as Blockly.BlockSvg;
290
+ const clearButton = this.createButton_(sourceBlock.getColourSecondary());
322
291
  clearButtonDiv.appendChild(clearButton);
323
292
  // Button to fill matrix
324
- var fillButtonDiv = document.createElement("div");
293
+ const fillButtonDiv = document.createElement("div");
325
294
  fillButtonDiv.className = "scratchMatrixButtonDiv";
326
- var fillButton = this.createButton_("#FFFFFF");
295
+ const fillButton = this.createButton_("#FFFFFF");
327
296
  fillButtonDiv.appendChild(fillButton);
328
297
 
329
298
  buttonDiv.appendChild(clearButtonDiv);
@@ -331,12 +300,12 @@ class FieldMatrix extends Blockly.Field {
331
300
  div.appendChild(buttonDiv);
332
301
 
333
302
  Blockly.DropDownDiv.setColour(
334
- this.sourceBlock_.getColour(),
335
- this.sourceBlock_.getColourTertiary()
303
+ sourceBlock.getColour(),
304
+ sourceBlock.getColourTertiary()
336
305
  );
337
306
  Blockly.DropDownDiv.showPositionedByBlock(
338
307
  this,
339
- this.sourceBlock_,
308
+ sourceBlock,
340
309
  this.dropdownDispose_.bind(this)
341
310
  );
342
311
 
@@ -359,7 +328,6 @@ class FieldMatrix extends Blockly.Field {
359
328
  this.fillMatrix_
360
329
  );
361
330
 
362
- const sourceBlock = this.getSourceBlock();
363
331
  const style = sourceBlock.style;
364
332
  if (sourceBlock.isShadow()) {
365
333
  this.originalStyle = sourceBlock.getStyleName();
@@ -367,7 +335,9 @@ class FieldMatrix extends Blockly.Field {
367
335
  } else if (this.borderRect_) {
368
336
  this.borderRect_.setAttribute(
369
337
  "fill",
370
- style.colourQuaternary ?? style.colourTertiary
338
+ "colourQuaternary" in style
339
+ ? `${style.colourQuaternary}`
340
+ : style.colourTertiary
371
341
  );
372
342
  }
373
343
 
@@ -384,11 +354,12 @@ class FieldMatrix extends Blockly.Field {
384
354
 
385
355
  /**
386
356
  * Make an svg object that resembles a 3x3 matrix to be used as a button.
387
- * @param {string} fill The color to fill the matrix nodes.
388
- * @return {SvgElement} The button svg element.
357
+ *
358
+ * @param fill The color to fill the matrix nodes.
359
+ * @returns The button svg element.
389
360
  */
390
- createButton_(fill) {
391
- var button = Blockly.utils.dom.createSvgElement("svg", {
361
+ createButton_(fill: string): SVGElement {
362
+ const button = Blockly.utils.dom.createSvgElement("svg", {
392
363
  xmlns: "http://www.w3.org/2000/svg",
393
364
  "xmlns:html": "http://www.w3.org/1999/xhtml",
394
365
  "xmlns:xlink": "http://www.w3.org/1999/xlink",
@@ -396,10 +367,10 @@ class FieldMatrix extends Blockly.Field {
396
367
  height: FieldMatrix.MATRIX_NODE_SIZE + "px",
397
368
  width: FieldMatrix.MATRIX_NODE_SIZE + "px",
398
369
  });
399
- var nodeSize = FieldMatrix.MATRIX_NODE_SIZE / 4;
400
- var nodePad = FieldMatrix.MATRIX_NODE_SIZE / 16;
401
- for (var i = 0; i < 3; i++) {
402
- for (var n = 0; n < 3; n++) {
370
+ const nodeSize = FieldMatrix.MATRIX_NODE_SIZE / 4;
371
+ const nodePad = FieldMatrix.MATRIX_NODE_SIZE / 16;
372
+ for (let i = 0; i < 3; i++) {
373
+ for (let n = 0; n < 3; n++) {
403
374
  Blockly.utils.dom.createSvgElement(
404
375
  "rect",
405
376
  {
@@ -420,22 +391,18 @@ class FieldMatrix extends Blockly.Field {
420
391
 
421
392
  /**
422
393
  * Redraw the matrix with the current value.
423
- * @private
424
394
  */
425
- updateMatrix_() {
395
+ private updateMatrix_() {
426
396
  const matrix = this.getValue();
427
- for (var i = 0; i < matrix.length; i++) {
428
- if (matrix[i] === "0") {
397
+ const sourceBlock = this.getSourceBlock() as Blockly.BlockSvg;
398
+ for (let i = 0; i < matrix.length; i++) {
399
+ if (matrix[i] === LEDState.OFF) {
429
400
  this.fillMatrixNode_(
430
401
  this.ledButtons_,
431
402
  i,
432
- this.sourceBlock_.getColourSecondary()
433
- );
434
- this.fillMatrixNode_(
435
- this.ledThumbNodes_,
436
- i,
437
- this.sourceBlock_.getColour()
403
+ sourceBlock.getColourSecondary()
438
404
  );
405
+ this.fillMatrixNode_(this.ledThumbNodes_, i, sourceBlock.getColour());
439
406
  } else {
440
407
  this.fillMatrixNode_(this.ledButtons_, i, "#FFFFFF");
441
408
  this.fillMatrixNode_(this.ledThumbNodes_, i, "#FFFFFF");
@@ -445,34 +412,37 @@ class FieldMatrix extends Blockly.Field {
445
412
 
446
413
  /**
447
414
  * Clear the matrix.
448
- * @param {!Event} e Mouse event.
415
+ *
416
+ * @param e Mouse event.
449
417
  */
450
- clearMatrix_(e) {
418
+ clearMatrix_(e: PointerEvent) {
451
419
  if (e.button != 0) return;
452
420
  this.setValue(FieldMatrix.ZEROS);
453
421
  }
454
422
 
455
423
  /**
456
424
  * Fill the matrix.
457
- * @param {!Event} e Mouse event.
425
+ *
426
+ * @param e Mouse event.
458
427
  */
459
- fillMatrix_(e) {
428
+ fillMatrix_(e: PointerEvent) {
460
429
  if (e.button != 0) return;
461
430
  this.setValue(FieldMatrix.ONES);
462
431
  }
463
432
 
464
433
  /**
465
434
  * Fill matrix node with specified colour.
466
- * @param {!Array<SVGElement>} node The array of matrix nodes.
467
- * @param {!number} index The index of the matrix node.
468
- * @param {!string} fill The fill colour in '#rrggbb' format.
435
+ *
436
+ * @param node The array of matrix nodes.
437
+ * @param index The index of the matrix node.
438
+ * @param fill The fill colour in '#rrggbb' format.
469
439
  */
470
- fillMatrixNode_(node, index, fill) {
440
+ fillMatrixNode_(node: SVGElement[], index: number, fill: string) {
471
441
  if (!node || !node[index] || !fill) return;
472
442
  node[index].setAttribute("fill", fill);
473
443
  }
474
444
 
475
- setLEDNode_(led, state) {
445
+ setLEDNode_(led: number, state: LEDState) {
476
446
  if (led < 0 || led > 24) return;
477
447
  const oldMatrix = this.getValue();
478
448
  const newMatrix =
@@ -480,30 +450,31 @@ class FieldMatrix extends Blockly.Field {
480
450
  this.setValue(newMatrix);
481
451
  }
482
452
 
483
- fillLEDNode_(led) {
453
+ fillLEDNode_(led: number) {
484
454
  if (led < 0 || led > 24) return;
485
- this.setLEDNode_(led, "1");
455
+ this.setLEDNode_(led, LEDState.ON);
486
456
  }
487
457
 
488
- clearLEDNode_(led) {
458
+ clearLEDNode_(led: number) {
489
459
  if (led < 0 || led > 24) return;
490
- this.setLEDNode_(led, "0");
460
+ this.setLEDNode_(led, LEDState.OFF);
491
461
  }
492
462
 
493
- toggleLEDNode_(led) {
463
+ toggleLEDNode_(led: number) {
494
464
  if (led < 0 || led > 24) return;
495
- if (this.getValue().charAt(led) === "0") {
496
- this.setLEDNode_(led, "1");
465
+ if (this.getValue().charAt(led) === LEDState.OFF) {
466
+ this.setLEDNode_(led, LEDState.ON);
497
467
  } else {
498
- this.setLEDNode_(led, "0");
468
+ this.setLEDNode_(led, LEDState.OFF);
499
469
  }
500
470
  }
501
471
 
502
472
  /**
503
473
  * Toggle matrix nodes on and off.
504
- * @param {!Event} e Mouse event.
474
+ *
475
+ * @param e Mouse event.
505
476
  */
506
- onMouseDown(e) {
477
+ onMouseDown(e: PointerEvent) {
507
478
  this.matrixMoveWrapper_ = Blockly.browserEvents.bind(
508
479
  document.body,
509
480
  "mousemove",
@@ -516,12 +487,12 @@ class FieldMatrix extends Blockly.Field {
516
487
  this,
517
488
  this.onMouseUp
518
489
  );
519
- var ledHit = this.checkForLED_(e);
490
+ const ledHit = this.checkForLED_(e);
520
491
  if (ledHit > -1) {
521
- if (this.getValue().charAt(ledHit) === "0") {
522
- this.paintStyle_ = "fill";
492
+ if (this.getValue().charAt(ledHit) === LEDState.OFF) {
493
+ this.paintStyle_ = PaintStyle.FILL;
523
494
  } else {
524
- this.paintStyle_ = "clear";
495
+ this.paintStyle_ = PaintStyle.CLEAR;
525
496
  }
526
497
  this.toggleLEDNode_(ledHit);
527
498
  this.updateMatrix_();
@@ -532,7 +503,6 @@ class FieldMatrix extends Blockly.Field {
532
503
 
533
504
  /**
534
505
  * Unbind mouse move event and clear the paint style.
535
- * @param {!Event} e Mouse move event.
536
506
  */
537
507
  onMouseUp() {
538
508
  Blockly.browserEvents.unbind(this.matrixMoveWrapper_);
@@ -544,16 +514,17 @@ class FieldMatrix extends Blockly.Field {
544
514
 
545
515
  /**
546
516
  * Toggle matrix nodes on and off by dragging mouse.
547
- * @param {!Event} e Mouse move event.
517
+ *
518
+ * @param e Mouse move event.
548
519
  */
549
- onMouseMove(e) {
520
+ onMouseMove(e: PointerEvent) {
550
521
  e.preventDefault();
551
522
  if (this.paintStyle_) {
552
- var led = this.checkForLED_(e);
523
+ const led = this.checkForLED_(e);
553
524
  if (led < 0) return;
554
- if (this.paintStyle_ === "clear") {
525
+ if (this.paintStyle_ === PaintStyle.CLEAR) {
555
526
  this.clearLEDNode_(led);
556
- } else if (this.paintStyle_ === "fill") {
527
+ } else if (this.paintStyle_ === PaintStyle.FILL) {
557
528
  this.fillLEDNode_(led);
558
529
  }
559
530
  }
@@ -561,29 +532,28 @@ class FieldMatrix extends Blockly.Field {
561
532
 
562
533
  /**
563
534
  * Check if mouse coordinates collide with a matrix node.
564
- * @param {!Event} e Mouse move event.
565
- * @return {number} The matching matrix node or -1 for none.
566
- */
567
- checkForLED_(e) {
568
- var bBox = this.matrixStage_.getBoundingClientRect();
569
- var nodeSize = FieldMatrix.MATRIX_NODE_SIZE;
570
- var nodePad = FieldMatrix.MATRIX_NODE_PAD;
571
- var dx = e.clientX - bBox.left;
572
- var dy = e.clientY - bBox.top;
573
- var min = nodePad / 2;
574
- var max = bBox.width - nodePad / 2;
535
+ *
536
+ * @param e Mouse move event.
537
+ * @returns The matching matrix node or -1 for none.
538
+ */
539
+ checkForLED_(e: PointerEvent): number {
540
+ const bBox = this.matrixStage_.getBoundingClientRect();
541
+ const nodeSize = FieldMatrix.MATRIX_NODE_SIZE;
542
+ const nodePad = FieldMatrix.MATRIX_NODE_PAD;
543
+ const dx = e.clientX - bBox.left;
544
+ const dy = e.clientY - bBox.top;
545
+ const min = nodePad / 2;
546
+ const max = bBox.width - nodePad / 2;
575
547
  if (dx < min || dx > max || dy < min || dy > max) {
576
548
  return -1;
577
549
  }
578
- var xDiv = Math.trunc((dx - nodePad / 2) / (nodeSize + nodePad));
579
- var yDiv = Math.trunc((dy - nodePad / 2) / (nodeSize + nodePad));
550
+ const xDiv = Math.trunc((dx - nodePad / 2) / (nodeSize + nodePad));
551
+ const yDiv = Math.trunc((dy - nodePad / 2) / (nodeSize + nodePad));
580
552
  return xDiv + yDiv * nodePad;
581
553
  }
582
554
 
583
555
  /**
584
556
  * Clean up this FieldMatrix, as well as the inherited Field.
585
- * @return {!Function} Closure to call on destruction of the WidgetDiv.
586
- * @private
587
557
  */
588
558
  dispose() {
589
559
  super.dispose();
@@ -608,8 +578,8 @@ class FieldMatrix extends Blockly.Field {
608
578
  }
609
579
  }
610
580
 
611
- updateSize_(margin) {
612
- const constants = this.getConstants();
581
+ updateSize_() {
582
+ const constants = this.getConstants() as Blockly.zelos.ConstantProvider;
613
583
  let totalHeight = constants.FIELD_TEXT_HEIGHT;
614
584
 
615
585
  this.size_.height = totalHeight;
@@ -622,10 +592,14 @@ class FieldMatrix extends Blockly.Field {
622
592
  }
623
593
 
624
594
  getClickTarget_() {
625
- return this.sourceBlock_.getSvgRoot();
595
+ return (this.getSourceBlock() as Blockly.BlockSvg).getSvgRoot();
626
596
  }
627
597
  }
628
598
 
599
+ interface FieldMatrixConfig extends Blockly.FieldConfig {
600
+ matrix: string;
601
+ }
602
+
629
603
  /**
630
604
  * Register the field and any dependencies.
631
605
  */