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

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 (85) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/main.js +1 -1
  3. package/dist/main.js.LICENSE.txt +0 -12
  4. package/package.json +4 -4
  5. package/src/{block_reporting.js → block_reporting.ts} +7 -5
  6. package/src/blocks/{colour.js → colour.ts} +6 -6
  7. package/src/blocks/{control.js → control.ts} +21 -54
  8. package/src/blocks/{data.js → data.ts} +134 -142
  9. package/src/blocks/{event.js → event.ts} +12 -33
  10. package/src/blocks/{looks.js → looks.ts} +24 -73
  11. package/src/blocks/{math.js → math.ts} +6 -11
  12. package/src/blocks/{matrix.js → matrix.ts} +2 -3
  13. package/src/blocks/{motion.js → motion.ts} +23 -70
  14. package/src/blocks/{note.js → note.ts} +2 -3
  15. package/src/blocks/{operators.js → operators.ts} +18 -55
  16. package/src/blocks/{procedures.js → procedures.ts} +418 -269
  17. package/src/blocks/{sensing.js → sensing.ts} +21 -61
  18. package/src/blocks/{sound.js → sound.ts} +9 -28
  19. package/src/blocks/{text.js → text.ts} +1 -2
  20. package/src/blocks/{vertical_extensions.js → vertical_extensions.ts} +63 -100
  21. package/src/checkable_continuous_flyout.ts +112 -0
  22. package/src/{checkbox_bubble.js → checkbox_bubble.ts} +40 -58
  23. package/src/{colours.js → colours.ts} +11 -4
  24. package/src/{constants.js → constants.ts} +13 -0
  25. package/src/{context_menu_items.js → context_menu_items.ts} +18 -12
  26. package/src/{css.js → css.ts} +13 -7
  27. package/src/{data_category.js → data_category.ts} +216 -150
  28. package/src/events/{events_block_comment_base.js → events_block_comment_base.ts} +23 -4
  29. package/src/events/{events_block_comment_change.js → events_block_comment_change.ts} +29 -5
  30. package/src/events/{events_block_comment_collapse.js → events_block_comment_collapse.ts} +24 -6
  31. package/src/events/{events_block_comment_create.js → events_block_comment_create.ts} +36 -10
  32. package/src/events/{events_block_comment_delete.js → events_block_comment_delete.ts} +6 -2
  33. package/src/events/{events_block_comment_move.js → events_block_comment_move.ts} +36 -6
  34. package/src/events/events_block_comment_resize.ts +88 -0
  35. package/src/events/events_block_drag_end.ts +49 -0
  36. package/src/events/events_block_drag_outside.ts +44 -0
  37. package/src/events/{events_scratch_variable_create.js → events_scratch_variable_create.ts} +28 -15
  38. package/src/fields/{field_colour_slider.js → field_colour_slider.ts} +117 -106
  39. package/src/fields/{field_matrix.js → field_matrix.ts} +189 -215
  40. package/src/fields/{field_note.js → field_note.ts} +227 -286
  41. package/src/fields/{field_textinput_removable.js → field_textinput_removable.ts} +17 -20
  42. package/src/fields/{field_variable_getter.js → field_variable_getter.ts} +28 -17
  43. package/src/fields/{field_vertical_separator.js → field_vertical_separator.ts} +14 -30
  44. package/src/fields/{field_angle.js → scratch_field_angle.ts} +124 -80
  45. package/src/fields/{field_dropdown.js → scratch_field_dropdown.ts} +9 -7
  46. package/src/fields/{field_number.js → scratch_field_number.ts} +60 -55
  47. package/src/fields/{field_variable.js → scratch_field_variable.ts} +46 -27
  48. package/src/{flyout_checkbox_icon.js → flyout_checkbox_icon.ts} +15 -19
  49. package/src/{glows.js → glows.ts} +29 -18
  50. package/src/index.ts +62 -63
  51. package/src/procedures.ts +462 -0
  52. package/src/recyclable_block_flyout_inflater.ts +51 -0
  53. package/src/renderer/{bowler_hat.js → bowler_hat.ts} +1 -1
  54. package/src/renderer/{constants.js → constants.ts} +26 -12
  55. package/src/renderer/{drawer.js → drawer.ts} +8 -3
  56. package/src/renderer/{path_object.js → path_object.ts} +2 -2
  57. package/src/renderer/{render_info.js → render_info.ts} +19 -7
  58. package/src/renderer/renderer.ts +76 -0
  59. package/src/{scratch_block_paster.js → scratch_block_paster.ts} +9 -7
  60. package/src/scratch_blocks_utils.ts +39 -0
  61. package/src/{scratch_comment_icon.js → scratch_comment_icon.ts} +43 -26
  62. package/src/scratch_connection_checker.ts +44 -0
  63. package/src/{scratch_continuous_category.js → scratch_continuous_category.ts} +20 -13
  64. package/src/scratch_continuous_toolbox.ts +70 -0
  65. package/src/{scratch_dragger.js → scratch_dragger.ts} +97 -28
  66. package/src/{scratch_variable_map.js → scratch_variable_map.ts} +4 -1
  67. package/src/scratch_variable_model.ts +30 -0
  68. package/src/{shadows.js → shadows.ts} +8 -4
  69. package/src/{status_indicator_label.js → status_indicator_label.ts} +24 -36
  70. package/src/{status_indicator_label_flyout_inflater.js → status_indicator_label_flyout_inflater.ts} +13 -9
  71. package/src/{variables.js → variables.ts} +153 -123
  72. package/tsconfig.json +5 -0
  73. package/src/categories.js +0 -15
  74. package/src/checkable_continuous_flyout.js +0 -138
  75. package/src/events/events_block_comment_resize.js +0 -52
  76. package/src/events/events_block_drag_end.js +0 -33
  77. package/src/events/events_block_drag_outside.js +0 -30
  78. package/src/procedures.js +0 -425
  79. package/src/recyclable_block_flyout_inflater.js +0 -194
  80. package/src/renderer/renderer.js +0 -74
  81. package/src/scratch_blocks_utils.js +0 -148
  82. package/src/scratch_connection_checker.js +0 -29
  83. package/src/scratch_continuous_toolbox.js +0 -78
  84. package/src/scratch_variable_model.js +0 -24
  85. /package/{continuous-toolbox.d.ts → types/continuous-toolbox.d.ts} +0 -0
@@ -23,52 +23,60 @@
23
23
  * @author fraser@google.com (Neil Fraser)
24
24
  */
25
25
  import * as Blockly from "blockly/core";
26
- import { FieldColour } from "@blockly/field-colour";
26
+ import { FieldColour, FieldColourFromJsonConfig } from "@blockly/field-colour";
27
+
28
+ enum ColourChannel {
29
+ HUE = "hue",
30
+ SATURATION = "saturation",
31
+ BRIGHTNESS = "brightness",
32
+ }
27
33
 
28
34
  /**
29
35
  * Class for a slider-based colour input field.
30
- * @param {string} colour The initial colour in '#rrggbb' format.
31
- * @param {Function=} opt_validator A function that is executed when a new
32
- * colour is selected. Its sole argument is the new colour value. Its
33
- * return value becomes the selected colour, unless it is undefined, in
34
- * which case the new colour stands, or it is null, in which case the change
35
- * is aborted.
36
- * @extends {Blockly.Field}
37
- * @constructor
38
36
  */
39
37
  export class FieldColourSlider extends FieldColour {
40
38
  /**
41
39
  * Function to be called if eyedropper can be activated.
42
40
  * If defined, an eyedropper button will be added to the color picker.
43
41
  * The button calls this function with a callback to update the field value.
44
- * BEWARE: This is not a stable API, so it is being marked as private. It may change.
45
- * @private
42
+ * BEWARE: This is not a stable API. It may change.
46
43
  */
47
- static activateEyedropper_ = null;
44
+ static activateEyedropper_: (
45
+ callback: (colour: string) => void
46
+ ) => void | null = null;
48
47
 
49
- constructor(colour, opt_validator) {
50
- super(colour, opt_validator);
48
+ /**
49
+ * Path to the eyedropper svg icon.
50
+ */
51
+ EYEDROPPER_PATH = "eyedropper.svg";
52
+ SERIALIZABLE = true;
53
+ EDITABLE = true;
51
54
 
52
- /**
53
- * Path to the eyedropper svg icon.
54
- */
55
- this.EYEDROPPER_PATH = "eyedropper.svg";
56
- this.SERIALIZABLE = true;
57
- this.EDITABLE = true;
58
- }
55
+ private hueChangeEventKey_?: Blockly.browserEvents.Data;
56
+ private saturationChangeEventKey_?: Blockly.browserEvents.Data;
57
+ private brightnessChangeEventKey_?: Blockly.browserEvents.Data;
58
+ private hueSlider_?: HTMLInputElement;
59
+ private saturationSlider_?: HTMLInputElement;
60
+ private brightnessSlider_?: HTMLInputElement;
61
+ private hueReadout_?: Element;
62
+ private saturationReadout_?: Element;
63
+ private brightnessReadout_?: Element;
64
+ private hue_?: number;
65
+ private saturation_?: number;
66
+ private brightness_?: number;
67
+ private eyedropperEventData_?: Blockly.browserEvents.Data;
59
68
 
60
69
  /**
61
70
  * Construct a FieldColourSlider from a JSON arg object.
62
- * @param {!Object} options A JSON object with options (colour).
63
- * @returns {!Blockly.FieldColourSlider} The new field instance.
64
- * @package
65
- * @nocollapse
71
+ *
72
+ * @param options A JSON object with options (colour).
73
+ * @returns The new field instance.
66
74
  */
67
- static fromJson(options) {
75
+ static fromJson(options: FieldColourFromJsonConfig): FieldColourSlider {
68
76
  return new FieldColourSlider(options["colour"]);
69
77
  }
70
78
 
71
- doValueUpdate_(newValue) {
79
+ doValueUpdate_(newValue: string) {
72
80
  super.doValueUpdate_(newValue);
73
81
  this.updateSliderHandles_();
74
82
  this.updateDom_();
@@ -76,25 +84,25 @@ export class FieldColourSlider extends FieldColour {
76
84
 
77
85
  /**
78
86
  * Create the hue, saturation or value CSS gradient for the slide backgrounds.
79
- * @param {string} channel – Either "hue", "saturation" or "value".
80
- * @return {string} Array colour hex colour stops for the given channel
81
- * @private
87
+ *
88
+ * @param channel Either "hue", "saturation" or "value".
89
+ * @return Array colour hex colour stops for the given channel
82
90
  */
83
- createColourStops_(channel) {
84
- var stops = [];
85
- for (var n = 0; n <= 360; n += 20) {
91
+ private createColourStops_(channel: ColourChannel): string[] {
92
+ const stops = [];
93
+ for (let n = 0; n <= 360; n += 20) {
86
94
  switch (channel) {
87
- case "hue":
95
+ case ColourChannel.HUE:
88
96
  stops.push(
89
97
  Blockly.utils.colour.hsvToHex(n, this.saturation_, this.brightness_)
90
98
  );
91
99
  break;
92
- case "saturation":
100
+ case ColourChannel.SATURATION:
93
101
  stops.push(
94
102
  Blockly.utils.colour.hsvToHex(this.hue_, n / 360, this.brightness_)
95
103
  );
96
104
  break;
97
- case "brightness":
105
+ case ColourChannel.BRIGHTNESS:
98
106
  stops.push(
99
107
  Blockly.utils.colour.hsvToHex(
100
108
  this.hue_,
@@ -112,25 +120,24 @@ export class FieldColourSlider extends FieldColour {
112
120
 
113
121
  /**
114
122
  * Set the gradient CSS properties for the given node and channel
115
- * @param {Node} node - The DOM node the gradient will be set on.
116
- * @param {string} channel Either "hue", "saturation" or "value".
117
- * @private
123
+ *
124
+ * @param node The DOM node the gradient will be set on.
125
+ * @param channel Either "hue", "saturation" or "value".
118
126
  */
119
- setGradient_(node, channel) {
120
- var gradient = this.createColourStops_(channel).join(",");
127
+ private setGradient_(node: HTMLElement, channel: ColourChannel) {
128
+ const gradient = this.createColourStops_(channel).join(",");
121
129
  node.style["background"] = `linear-gradient(to right, ${gradient})`;
122
130
  }
123
131
 
124
132
  /**
125
133
  * Update the readouts and slider backgrounds after value has changed.
126
- * @private
127
134
  */
128
- updateDom_() {
135
+ private updateDom_() {
129
136
  if (this.hueSlider_) {
130
137
  // Update the slider backgrounds
131
- this.setGradient_(this.hueSlider_, "hue");
132
- this.setGradient_(this.saturationSlider_, "saturation");
133
- this.setGradient_(this.brightnessSlider_, "brightness");
138
+ this.setGradient_(this.hueSlider_, ColourChannel.HUE);
139
+ this.setGradient_(this.saturationSlider_, ColourChannel.SATURATION);
140
+ this.setGradient_(this.brightnessSlider_, ColourChannel.BRIGHTNESS);
134
141
 
135
142
  // Update the readouts
136
143
  this.hueReadout_.textContent = Math.floor(
@@ -147,28 +154,27 @@ export class FieldColourSlider extends FieldColour {
147
154
 
148
155
  /**
149
156
  * Update the slider handle positions from the current field value.
150
- * @private
151
157
  */
152
- updateSliderHandles_() {
158
+ private updateSliderHandles_() {
153
159
  if (this.hueSlider_) {
154
- this.hueSlider_.value = this.hue_;
155
- this.saturationSlider_.value = this.saturation_;
156
- this.brightnessSlider_.value = this.brightness_;
160
+ this.hueSlider_.value = `${this.hue_}`;
161
+ this.saturationSlider_.value = `${this.saturation_}`;
162
+ this.brightnessSlider_.value = `${this.brightness_}`;
157
163
  }
158
164
  }
159
165
 
160
166
  /**
161
- * Create label and readout DOM elements, returning the readout
162
- * @param {string} labelText - Text for the label
163
- * @return {Array} The container node and the readout node.
164
- * @private
167
+ * Create label and readout DOM elements, returning the readout.
168
+ *
169
+ * @param labelText Text for the label
170
+ * @return The container node and the readout node.
165
171
  */
166
- createLabelDom_(labelText) {
167
- var labelContainer = document.createElement("div");
172
+ private createLabelDom_(labelText: string): Element[] {
173
+ const labelContainer = document.createElement("div");
168
174
  labelContainer.setAttribute("class", "scratchColourPickerLabel");
169
- var readout = document.createElement("span");
175
+ const readout = document.createElement("span");
170
176
  readout.setAttribute("class", "scratchColourPickerReadout");
171
- var label = document.createElement("span");
177
+ const label = document.createElement("span");
172
178
  label.setAttribute("class", "scratchColourPickerLabelText");
173
179
  label.textContent = labelText;
174
180
  labelContainer.appendChild(label);
@@ -178,71 +184,69 @@ export class FieldColourSlider extends FieldColour {
178
184
 
179
185
  /**
180
186
  * Factory for creating the different slider callbacks
181
- * @param {string} channel - One of "hue", "saturation" or "brightness"
182
- * @return {function} the callback for slider update
183
- * @private
187
+ *
188
+ * @param channel One of "hue", "saturation" or "brightness"
189
+ * @returns The callback for slider update
184
190
  */
185
- sliderCallbackFactory_(channel) {
186
- var thisField = this;
187
- return function (event) {
188
- var channelValue = event.target.value;
191
+ private sliderCallbackFactory_(
192
+ channel: ColourChannel
193
+ ): (event: PointerEvent) => void {
194
+ return (event: PointerEvent) => {
195
+ const channelValue = (event.target as HTMLInputElement).value;
189
196
  switch (channel) {
190
- case "hue":
191
- thisField.hue_ = channelValue;
197
+ case ColourChannel.HUE:
198
+ this.hue_ = Number(channelValue);
192
199
  break;
193
- case "saturation":
194
- thisField.saturation_ = channelValue;
200
+ case ColourChannel.SATURATION:
201
+ this.saturation_ = Number(channelValue);
195
202
  break;
196
- case "brightness":
197
- thisField.brightness_ = channelValue;
203
+ case ColourChannel.BRIGHTNESS:
204
+ this.brightness_ = Number(channelValue);
198
205
  break;
199
206
  }
200
- var colour = Blockly.utils.colour.hsvToHex(
201
- thisField.hue_,
202
- thisField.saturation_,
203
- thisField.brightness_
207
+ const colour = Blockly.utils.colour.hsvToHex(
208
+ this.hue_,
209
+ this.saturation_,
210
+ this.brightness_
204
211
  );
205
212
  if (colour !== null) {
206
- thisField.setValue(colour, true);
213
+ this.setValue(colour, true);
207
214
  }
208
215
  };
209
216
  }
210
217
 
211
218
  /**
212
219
  * Activate the eyedropper, passing in a callback for setting the field value.
213
- * @private
214
220
  */
215
- activateEyedropperInternal_() {
216
- var thisField = this;
217
- FieldColourSlider.activateEyedropper_(function (chosenColour) {
221
+ private activateEyedropperInternal_() {
222
+ FieldColourSlider.activateEyedropper_((chosenColour: string) => {
218
223
  // Update the internal hue/saturation/brightness values so sliders update.
219
224
  const components = Blockly.utils.colour.hexToRgb(chosenColour);
220
- const { hue, saturation, value } = thisField.rgbToHsv(
225
+ const { hue, saturation, value } = this.rgbToHsv(
221
226
  components[0],
222
227
  components[1],
223
228
  components[2]
224
229
  );
225
- thisField.hue_ = hue;
226
- thisField.saturation_ = saturation;
227
- thisField.brightness_ = value;
228
- thisField.setValue(chosenColour);
230
+ this.hue_ = hue;
231
+ this.saturation_ = saturation;
232
+ this.brightness_ = value;
233
+ this.setValue(chosenColour);
229
234
  });
230
235
  }
231
236
 
232
237
  /**
233
238
  * Create hue, saturation and brightness sliders under the colour field.
234
- * @private
235
239
  */
236
240
  showEditor_() {
237
241
  Blockly.DropDownDiv.hideWithoutAnimation();
238
242
  Blockly.DropDownDiv.clearContent();
239
- var div = Blockly.DropDownDiv.getContentDiv();
243
+ const div = Blockly.DropDownDiv.getContentDiv();
240
244
  div.className = "scratchColourPicker";
241
245
 
242
246
  // Init color component values that are used while the editor is open
243
247
  // in order to keep the slider values stable.
244
248
  const components = Blockly.utils.colour.hexToRgb(this.getValue());
245
- var { hue, saturation, value } = this.rgbToHsv(
249
+ const { hue, saturation, value } = this.rgbToHsv(
246
250
  components[0],
247
251
  components[1],
248
252
  components[2]
@@ -251,45 +255,45 @@ export class FieldColourSlider extends FieldColour {
251
255
  this.saturation_ = saturation;
252
256
  this.brightness_ = value;
253
257
 
254
- var hueElements = this.createLabelDom_(Blockly.Msg.COLOUR_HUE_LABEL);
258
+ const hueElements = this.createLabelDom_(Blockly.Msg.COLOUR_HUE_LABEL);
255
259
  div.appendChild(hueElements[0]);
256
260
  this.hueReadout_ = hueElements[1];
257
261
  this.hueSlider_ = document.createElement("input");
258
262
  this.hueSlider_.type = "range";
259
- this.hueSlider_.min = 0;
260
- this.hueSlider_.max = 360;
263
+ this.hueSlider_.min = "0";
264
+ this.hueSlider_.max = "360";
261
265
  this.hueSlider_.className = "scratchColourSlider";
262
266
  div.appendChild(this.hueSlider_);
263
267
 
264
- var saturationElements = this.createLabelDom_(
268
+ const saturationElements = this.createLabelDom_(
265
269
  Blockly.Msg.COLOUR_SATURATION_LABEL
266
270
  );
267
271
  div.appendChild(saturationElements[0]);
268
272
  this.saturationReadout_ = saturationElements[1];
269
273
  this.saturationSlider_ = document.createElement("input");
270
274
  this.saturationSlider_.type = "range";
271
- this.saturationSlider_.step = 0.001;
272
- this.saturationSlider_.min = 0;
273
- this.saturationSlider_.max = 1.0;
275
+ this.saturationSlider_.step = "0.001";
276
+ this.saturationSlider_.min = "0";
277
+ this.saturationSlider_.max = "1.0";
274
278
  this.saturationSlider_.className = "scratchColourSlider";
275
279
  div.appendChild(this.saturationSlider_);
276
280
 
277
- var brightnessElements = this.createLabelDom_(
281
+ const brightnessElements = this.createLabelDom_(
278
282
  Blockly.Msg.COLOUR_BRIGHTNESS_LABEL
279
283
  );
280
284
  div.appendChild(brightnessElements[0]);
281
285
  this.brightnessReadout_ = brightnessElements[1];
282
286
  this.brightnessSlider_ = document.createElement("input");
283
287
  this.brightnessSlider_.type = "range";
284
- this.brightnessSlider_.min = 0;
285
- this.brightnessSlider_.max = 255;
288
+ this.brightnessSlider_.min = "0";
289
+ this.brightnessSlider_.max = "255";
286
290
  this.brightnessSlider_.className = "scratchColourSlider";
287
291
  div.appendChild(this.brightnessSlider_);
288
292
 
289
293
  if (FieldColourSlider.activateEyedropper_) {
290
- var button = document.createElement("button");
294
+ const button = document.createElement("button");
291
295
  button.setAttribute("class", "scratchEyedropper");
292
- var image = document.createElement("img");
296
+ const image = document.createElement("img");
293
297
  image.src =
294
298
  Blockly.getMainWorkspace().options.pathToMedia + this.EYEDROPPER_PATH;
295
299
  button.appendChild(image);
@@ -303,7 +307,10 @@ export class FieldColourSlider extends FieldColour {
303
307
  }
304
308
 
305
309
  Blockly.DropDownDiv.setColour("#ffffff", "#dddddd");
306
- Blockly.DropDownDiv.showPositionedByBlock(this, this.sourceBlock_);
310
+ Blockly.DropDownDiv.showPositionedByBlock(
311
+ this,
312
+ this.getSourceBlock() as Blockly.BlockSvg
313
+ );
307
314
 
308
315
  // Set value updates the slider positions
309
316
  // Do this before attaching callbacks to avoid extra events from initial set
@@ -313,19 +320,19 @@ export class FieldColourSlider extends FieldColour {
313
320
  this.hueSlider_,
314
321
  "input",
315
322
  this,
316
- this.sliderCallbackFactory_("hue")
323
+ this.sliderCallbackFactory_(ColourChannel.HUE)
317
324
  );
318
325
  this.saturationChangeEventKey_ = Blockly.browserEvents.bind(
319
326
  this.saturationSlider_,
320
327
  "input",
321
328
  this,
322
- this.sliderCallbackFactory_("saturation")
329
+ this.sliderCallbackFactory_(ColourChannel.SATURATION)
323
330
  );
324
331
  this.brightnessChangeEventKey_ = Blockly.browserEvents.bind(
325
332
  this.brightnessSlider_,
326
333
  "input",
327
334
  this,
328
- this.sliderCallbackFactory_("brightness")
335
+ this.sliderCallbackFactory_(ColourChannel.BRIGHTNESS)
329
336
  );
330
337
  }
331
338
 
@@ -347,7 +354,11 @@ export class FieldColourSlider extends FieldColour {
347
354
  }
348
355
 
349
356
  // From Closure
350
- rgbToHsv(red, green, blue) {
357
+ rgbToHsv(
358
+ red: number,
359
+ green: number,
360
+ blue: number
361
+ ): { hue: number; saturation: number; value: number } {
351
362
  const max = Math.max(Math.max(red, green), blue);
352
363
  const min = Math.min(Math.min(red, green), blue);
353
364
  let hue;