@blockdia/scratch-blocks 0.1.0

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 (243) hide show
  1. package/.nvmrc +1 -0
  2. package/LICENSE +674 -0
  3. package/README.md +57 -0
  4. package/TRADEMARK +1 -0
  5. package/blockly_compressed_horizontal.js +2268 -0
  6. package/blockly_compressed_vertical.js +2296 -0
  7. package/blockly_uncompressed_horizontal.js +1212 -0
  8. package/blockly_uncompressed_vertical.js +1212 -0
  9. package/blocks_common/colour.js +61 -0
  10. package/blocks_common/math.js +159 -0
  11. package/blocks_common/matrix.js +54 -0
  12. package/blocks_common/note.js +58 -0
  13. package/blocks_common/text.js +57 -0
  14. package/blocks_compressed.js +37 -0
  15. package/blocks_compressed_horizontal.js +68 -0
  16. package/blocks_compressed_vertical.js +216 -0
  17. package/blocks_horizontal/control.js +212 -0
  18. package/blocks_horizontal/default_toolbox.js +139 -0
  19. package/blocks_horizontal/event.js +190 -0
  20. package/blocks_horizontal/wedo.js +325 -0
  21. package/blocks_vertical/control.js +532 -0
  22. package/blocks_vertical/data.js +666 -0
  23. package/blocks_vertical/default_toolbox.js +571 -0
  24. package/blocks_vertical/event.js +329 -0
  25. package/blocks_vertical/extensions.js +294 -0
  26. package/blocks_vertical/looks.js +591 -0
  27. package/blocks_vertical/motion.js +587 -0
  28. package/blocks_vertical/operators.js +470 -0
  29. package/blocks_vertical/procedures.js +1051 -0
  30. package/blocks_vertical/sensing.js +555 -0
  31. package/blocks_vertical/sound.js +246 -0
  32. package/blocks_vertical/vertical_extensions.js +289 -0
  33. package/build/gen_blocks.js +1 -0
  34. package/build/test_expect.js +1 -0
  35. package/build/test_input.js +1 -0
  36. package/build.py +647 -0
  37. package/cleanup.sh +101 -0
  38. package/core/block.js +1837 -0
  39. package/core/block_animations.js +107 -0
  40. package/core/block_drag_surface.js +299 -0
  41. package/core/block_dragger.js +424 -0
  42. package/core/block_events.js +531 -0
  43. package/core/block_render_svg_horizontal.js +892 -0
  44. package/core/block_render_svg_vertical.js +1734 -0
  45. package/core/block_svg.js +1355 -0
  46. package/core/blockly.js +620 -0
  47. package/core/blocks.js +37 -0
  48. package/core/bubble.js +664 -0
  49. package/core/bubble_dragger.js +285 -0
  50. package/core/colours.js +177 -0
  51. package/core/comment.js +293 -0
  52. package/core/comment_events.js +539 -0
  53. package/core/connection.js +776 -0
  54. package/core/connection_db.js +300 -0
  55. package/core/constants.js +408 -0
  56. package/core/contextmenu.js +534 -0
  57. package/core/css.js +1354 -0
  58. package/core/data_category.js +490 -0
  59. package/core/dragged_connection_manager.js +260 -0
  60. package/core/dropdowndiv.js +408 -0
  61. package/core/events.js +429 -0
  62. package/core/events_abstract.js +113 -0
  63. package/core/extensions.js +450 -0
  64. package/core/field.js +810 -0
  65. package/core/field_angle.js +410 -0
  66. package/core/field_checkbox.js +133 -0
  67. package/core/field_colour.js +253 -0
  68. package/core/field_colour_slider.js +387 -0
  69. package/core/field_date.js +353 -0
  70. package/core/field_dropdown.js +447 -0
  71. package/core/field_iconmenu.js +309 -0
  72. package/core/field_image.js +200 -0
  73. package/core/field_label.js +136 -0
  74. package/core/field_label_serializable.js +125 -0
  75. package/core/field_matrix.js +566 -0
  76. package/core/field_note.js +850 -0
  77. package/core/field_number.js +366 -0
  78. package/core/field_numberdropdown.js +77 -0
  79. package/core/field_textdropdown.js +164 -0
  80. package/core/field_textinput.js +675 -0
  81. package/core/field_textinput_removable.js +105 -0
  82. package/core/field_variable.js +385 -0
  83. package/core/field_variable_getter.js +185 -0
  84. package/core/field_vertical_separator.js +161 -0
  85. package/core/flyout_base.js +935 -0
  86. package/core/flyout_button.js +324 -0
  87. package/core/flyout_dragger.js +83 -0
  88. package/core/flyout_extension_category_header.js +159 -0
  89. package/core/flyout_horizontal.js +475 -0
  90. package/core/flyout_vertical.js +770 -0
  91. package/core/generator.js +426 -0
  92. package/core/gesture.js +1010 -0
  93. package/core/grid.js +227 -0
  94. package/core/icon.js +205 -0
  95. package/core/inject.js +496 -0
  96. package/core/input.js +285 -0
  97. package/core/insertion_marker_manager.js +678 -0
  98. package/core/intersection_observer.js +102 -0
  99. package/core/msg.js +62 -0
  100. package/core/mutator.js +426 -0
  101. package/core/names.js +198 -0
  102. package/core/options.js +244 -0
  103. package/core/procedures.js +739 -0
  104. package/core/rendered_connection.js +417 -0
  105. package/core/scratch_block_comment.js +646 -0
  106. package/core/scratch_blocks_utils.js +246 -0
  107. package/core/scratch_bubble.js +699 -0
  108. package/core/scratch_events.js +131 -0
  109. package/core/scratch_msgs.js +85 -0
  110. package/core/scrollbar.js +875 -0
  111. package/core/toolbox.js +803 -0
  112. package/core/tooltip.js +337 -0
  113. package/core/touch.js +226 -0
  114. package/core/trashcan.js +343 -0
  115. package/core/ui_events.js +91 -0
  116. package/core/ui_menu_utils.js +68 -0
  117. package/core/utils.js +825 -0
  118. package/core/variable_events.js +259 -0
  119. package/core/variable_map.js +415 -0
  120. package/core/variable_model.js +116 -0
  121. package/core/variables.js +674 -0
  122. package/core/warning.js +199 -0
  123. package/core/widgetdiv.js +344 -0
  124. package/core/workspace.js +673 -0
  125. package/core/workspace_audio.js +170 -0
  126. package/core/workspace_comment.js +426 -0
  127. package/core/workspace_comment_render_svg.js +706 -0
  128. package/core/workspace_comment_svg.js +609 -0
  129. package/core/workspace_drag_surface_svg.js +195 -0
  130. package/core/workspace_dragger.js +132 -0
  131. package/core/workspace_svg.js +2382 -0
  132. package/core/xml.js +930 -0
  133. package/core/zoom_controls.js +301 -0
  134. package/dist/horizontal.js +222 -0
  135. package/dist/vertical.js +222 -0
  136. package/dist/web/horizontal.js +232 -0
  137. package/dist/web/vertical.js +232 -0
  138. package/i18n/common.py +234 -0
  139. package/i18n/create_messages.py +162 -0
  140. package/i18n/create_scratch_msgs.js +69 -0
  141. package/i18n/dedup_json.py +73 -0
  142. package/i18n/js_to_json.js +46 -0
  143. package/i18n/js_to_json.py +136 -0
  144. package/i18n/json_to_js.js +52 -0
  145. package/i18n/json_to_js.py +185 -0
  146. package/i18n/sync_tx_translations.js +111 -0
  147. package/i18n/test_scratch_msgs.js +87 -0
  148. package/i18n/tests.py +47 -0
  149. package/i18n/xliff_to_json.py +232 -0
  150. package/local_build.sh +70 -0
  151. package/media/click.mp3 +0 -0
  152. package/media/click.ogg +0 -0
  153. package/media/click.wav +0 -0
  154. package/media/comment-arrow-down.svg +10 -0
  155. package/media/comment-arrow-up.svg +10 -0
  156. package/media/delete-x.svg +10 -0
  157. package/media/delete.mp3 +0 -0
  158. package/media/delete.ogg +0 -0
  159. package/media/delete.wav +0 -0
  160. package/media/dropdown-arrow-dark.svg +1 -0
  161. package/media/dropdown-arrow.svg +1 -0
  162. package/media/extensions/microbit-block-icon.svg +130 -0
  163. package/media/extensions/music-block-icon.svg +17 -0
  164. package/media/extensions/pen-block-icon.svg +19 -0
  165. package/media/extensions/wedo2-block-icon.svg +36 -0
  166. package/media/eyedropper.svg +22 -0
  167. package/media/green-flag.svg +17 -0
  168. package/media/handclosed.cur +0 -0
  169. package/media/handdelete.cur +0 -0
  170. package/media/handopen.cur +0 -0
  171. package/media/icons/arrow.svg +12 -0
  172. package/media/icons/arrow_button.svg +1 -0
  173. package/media/icons/control_forever.svg +1 -0
  174. package/media/icons/control_repeat.svg +1 -0
  175. package/media/icons/control_stop.svg +1 -0
  176. package/media/icons/control_wait.svg +1 -0
  177. package/media/icons/event_broadcast_blue.svg +1 -0
  178. package/media/icons/event_broadcast_coral.svg +1 -0
  179. package/media/icons/event_broadcast_green.svg +1 -0
  180. package/media/icons/event_broadcast_magenta.svg +1 -0
  181. package/media/icons/event_broadcast_orange.svg +1 -0
  182. package/media/icons/event_broadcast_purple.svg +1 -0
  183. package/media/icons/event_when-broadcast-received_blue.svg +1 -0
  184. package/media/icons/event_when-broadcast-received_coral.svg +1 -0
  185. package/media/icons/event_when-broadcast-received_green.svg +1 -0
  186. package/media/icons/event_when-broadcast-received_magenta.svg +1 -0
  187. package/media/icons/event_when-broadcast-received_orange.svg +1 -0
  188. package/media/icons/event_when-broadcast-received_purple.svg +1 -0
  189. package/media/icons/event_whenflagclicked.svg +1 -0
  190. package/media/icons/remove.svg +19 -0
  191. package/media/icons/set-led_blue.svg +1 -0
  192. package/media/icons/set-led_coral.svg +1 -0
  193. package/media/icons/set-led_green.svg +1 -0
  194. package/media/icons/set-led_magenta.svg +1 -0
  195. package/media/icons/set-led_mystery.svg +1 -0
  196. package/media/icons/set-led_orange.svg +1 -0
  197. package/media/icons/set-led_purple.svg +1 -0
  198. package/media/icons/set-led_white.svg +1 -0
  199. package/media/icons/set-led_yellow.svg +1 -0
  200. package/media/icons/wedo_motor-clockwise.svg +1 -0
  201. package/media/icons/wedo_motor-counterclockwise.svg +1 -0
  202. package/media/icons/wedo_motor-speed_fast.svg +1 -0
  203. package/media/icons/wedo_motor-speed_med.svg +1 -0
  204. package/media/icons/wedo_motor-speed_slow.svg +1 -0
  205. package/media/icons/wedo_when-distance_close.svg +1 -0
  206. package/media/icons/wedo_when-tilt-backward.svg +1 -0
  207. package/media/icons/wedo_when-tilt-forward.svg +1 -0
  208. package/media/icons/wedo_when-tilt-left.svg +1 -0
  209. package/media/icons/wedo_when-tilt-right.svg +1 -0
  210. package/media/icons/wedo_when-tilt.svg +1 -0
  211. package/media/repeat.svg +18 -0
  212. package/media/rotate-left.svg +1 -0
  213. package/media/rotate-right.svg +1 -0
  214. package/media/sprites.png +0 -0
  215. package/media/status-not-ready.svg +13 -0
  216. package/media/status-ready.svg +13 -0
  217. package/media/zoom-in.svg +1 -0
  218. package/media/zoom-out.svg +1 -0
  219. package/media/zoom-reset.svg +1 -0
  220. package/msg/js/en.js +290 -0
  221. package/msg/json/en.json +285 -0
  222. package/msg/messages.js +359 -0
  223. package/msg/scratch_msgs.js +22969 -0
  224. package/package.json +60 -0
  225. package/pull_from_blockly.sh +151 -0
  226. package/renovate.json5 +15 -0
  227. package/shim/blockly_compressed_horizontal-blocks_compressed.js +1 -0
  228. package/shim/blockly_compressed_horizontal.Blockly.js +1 -0
  229. package/shim/blockly_compressed_horizontal.goog.js +1 -0
  230. package/shim/blockly_compressed_horizontal.js +1 -0
  231. package/shim/blockly_compressed_vertical-blocks_compressed.js +1 -0
  232. package/shim/blockly_compressed_vertical.Blockly.js +1 -0
  233. package/shim/blockly_compressed_vertical.goog.js +1 -0
  234. package/shim/blockly_compressed_vertical.js +1 -0
  235. package/shim/blocks_compressed_horizontal-blockly_compressed_horizontal-messages.js +1 -0
  236. package/shim/blocks_compressed_horizontal.js +1 -0
  237. package/shim/blocks_compressed_vertical-blockly_compressed_vertical-messages.js +1 -0
  238. package/shim/blocks_compressed_vertical.js +1 -0
  239. package/shim/gh-pages.js +1 -0
  240. package/shim/horizontal.js +1 -0
  241. package/shim/index.js +17 -0
  242. package/shim/vertical.js +1 -0
  243. package/universal-python.js +82 -0
@@ -0,0 +1,675 @@
1
+ /**
2
+ * @license
3
+ * Visual Blocks Editor
4
+ *
5
+ * Copyright 2012 Google Inc.
6
+ * https://developers.google.com/blockly/
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+
21
+ /**
22
+ * @fileoverview Text input field.
23
+ * @author fraser@google.com (Neil Fraser)
24
+ */
25
+ 'use strict';
26
+
27
+ goog.provide('Blockly.FieldTextInput');
28
+
29
+ goog.require('Blockly.BlockSvg.render');
30
+ goog.require('Blockly.Colours');
31
+ goog.require('Blockly.Field');
32
+ goog.require('Blockly.Msg');
33
+ goog.require('Blockly.scratchBlocksUtils');
34
+ goog.require('Blockly.utils');
35
+
36
+ goog.require('goog.asserts');
37
+ goog.require('goog.dom');
38
+ goog.require('goog.dom.TagName');
39
+ goog.require('goog.userAgent');
40
+
41
+
42
+ /**
43
+ * Class for an editable text field.
44
+ * @param {string} text The initial content of the field.
45
+ * @param {Function=} opt_validator An optional function that is called
46
+ * to validate any constraints on what the user entered. Takes the new
47
+ * text as an argument and returns either the accepted text, a replacement
48
+ * text, or null to abort the change.
49
+ * @param {RegExp=} opt_restrictor An optional regular expression to restrict
50
+ * typed text to. Text that doesn't match the restrictor will never show
51
+ * in the text field.
52
+ * @extends {Blockly.Field}
53
+ * @constructor
54
+ */
55
+ Blockly.FieldTextInput = function(text, opt_validator, opt_restrictor) {
56
+ Blockly.FieldTextInput.superClass_.constructor.call(this, text,
57
+ opt_validator);
58
+ this.setRestrictor(opt_restrictor);
59
+ this.addArgType('text');
60
+ };
61
+ goog.inherits(Blockly.FieldTextInput, Blockly.Field);
62
+
63
+ /**
64
+ * Construct a FieldTextInput from a JSON arg object,
65
+ * dereferencing any string table references.
66
+ * @param {!Object} options A JSON object with options (text, class, and
67
+ * spellcheck).
68
+ * @returns {!Blockly.FieldTextInput} The new field instance.
69
+ * @package
70
+ * @nocollapse
71
+ */
72
+ Blockly.FieldTextInput.fromJson = function(options) {
73
+ var text = Blockly.utils.replaceMessageReferences(options['text']) || '';
74
+ var field = new Blockly.FieldTextInput(text, options['class']);
75
+ if (typeof options['spellcheck'] === 'boolean') {
76
+ field.setSpellcheck(options['spellcheck']);
77
+ }
78
+ return field;
79
+ };
80
+
81
+ /**
82
+ * Length of animations in seconds.
83
+ */
84
+ Blockly.FieldTextInput.ANIMATION_TIME = 0.25;
85
+
86
+ /**
87
+ * Padding to use for text measurement for the field during editing, in px.
88
+ */
89
+ Blockly.FieldTextInput.TEXT_MEASURE_PADDING_MAGIC = 45;
90
+
91
+ /**
92
+ * The HTML input element for the user to type, or null if no FieldTextInput
93
+ * editor is currently open.
94
+ * @type {HTMLInputElement}
95
+ * @private
96
+ */
97
+ Blockly.FieldTextInput.htmlInput_ = null;
98
+
99
+ /**
100
+ * Mouse cursor style when over the hotspot that initiates the editor.
101
+ */
102
+ Blockly.FieldTextInput.prototype.CURSOR = 'text';
103
+
104
+ /**
105
+ * Allow browser to spellcheck this field.
106
+ * @private
107
+ */
108
+ Blockly.FieldTextInput.prototype.spellcheck_ = true;
109
+
110
+ /**
111
+ * Install this text field on a block.
112
+ */
113
+ Blockly.FieldTextInput.prototype.init = function() {
114
+ if (this.fieldGroup_) {
115
+ // Field has already been initialized once.
116
+ return;
117
+ }
118
+
119
+ var notInShadow = !this.sourceBlock_.isShadow();
120
+
121
+ if (notInShadow) {
122
+ this.className_ += ' blocklyEditableLabel';
123
+ }
124
+
125
+ Blockly.FieldTextInput.superClass_.init.call(this);
126
+
127
+ // If not in a shadow block, draw a box.
128
+ if (notInShadow) {
129
+ this.box_ = Blockly.utils.createSvgElement('rect',
130
+ {
131
+ 'x': 0,
132
+ 'y': 0,
133
+ 'width': this.size_.width,
134
+ 'height': this.size_.height,
135
+ 'fill': this.sourceBlock_.getColourTertiary()
136
+ }
137
+ );
138
+ this.fieldGroup_.insertBefore(this.box_, this.textElement_);
139
+ }
140
+ };
141
+
142
+ /**
143
+ * Close the input widget if this input is being deleted.
144
+ */
145
+ Blockly.FieldTextInput.prototype.dispose = function() {
146
+ Blockly.WidgetDiv.hideIfOwner(this);
147
+ Blockly.FieldTextInput.superClass_.dispose.call(this);
148
+ };
149
+
150
+ /**
151
+ * Set the value of this field.
152
+ * @param {?string} newValue New value.
153
+ * @override
154
+ */
155
+ Blockly.FieldTextInput.prototype.setValue = function(newValue) {
156
+ if (newValue === null) {
157
+ return; // No change if null.
158
+ }
159
+ if (this.sourceBlock_) {
160
+ var validated = this.callValidator(newValue);
161
+ // If the new value is invalid, validation returns null.
162
+ // In this case we still want to display the illegal result.
163
+ if (validated !== null) {
164
+ newValue = validated;
165
+ }
166
+ }
167
+ Blockly.Field.prototype.setValue.call(this, newValue);
168
+ };
169
+
170
+ /**
171
+ * Set the text in this field and fire a change event.
172
+ * @param {*} newText New text.
173
+ */
174
+ Blockly.FieldTextInput.prototype.setText = function(newText) {
175
+ if (newText === null) {
176
+ // No change if null.
177
+ return;
178
+ }
179
+ newText = String(newText);
180
+ if (newText === this.text_) {
181
+ // No change.
182
+ return;
183
+ }
184
+ if (this.sourceBlock_ && Blockly.Events.isEnabled()) {
185
+ Blockly.Events.fire(new Blockly.Events.BlockChange(
186
+ this.sourceBlock_, 'field', this.name, this.text_, newText));
187
+ }
188
+ Blockly.Field.prototype.setText.call(this, newText);
189
+ };
190
+
191
+ /**
192
+ * Set whether this field is spellchecked by the browser.
193
+ * @param {boolean} check True if checked.
194
+ */
195
+ Blockly.FieldTextInput.prototype.setSpellcheck = function(check) {
196
+ this.spellcheck_ = check;
197
+ };
198
+
199
+ /**
200
+ * Set the restrictor regex for this text input.
201
+ * Text that doesn't match the restrictor will never show in the text field.
202
+ * @param {?RegExp} restrictor Regular expression to restrict text.
203
+ */
204
+ Blockly.FieldTextInput.prototype.setRestrictor = function(restrictor) {
205
+ this.restrictor_ = restrictor;
206
+ };
207
+
208
+ /**
209
+ * Show the inline free-text editor on top of the text.
210
+ * @param {boolean=} opt_quietInput True if editor should be created without
211
+ * focus. Defaults to false.
212
+ * @param {boolean=} opt_readOnly True if editor should be created with HTML
213
+ * input set to read-only, to prevent virtual keyboards.
214
+ * @param {boolean=} opt_withArrow True to show drop-down arrow in text editor.
215
+ * @param {Function=} opt_arrowCallback Callback for when drop-down arrow clicked.
216
+ * @private
217
+ */
218
+ Blockly.FieldTextInput.prototype.showEditor_ = function(
219
+ opt_quietInput, opt_readOnly, opt_withArrow, opt_arrowCallback) {
220
+ this.workspace_ = this.sourceBlock_.workspace;
221
+ var quietInput = opt_quietInput || false;
222
+ var readOnly = opt_readOnly || false;
223
+ Blockly.WidgetDiv.show(this, this.sourceBlock_.RTL,
224
+ this.widgetDispose_(), this.widgetDisposeAnimationFinished_(),
225
+ Blockly.FieldTextInput.ANIMATION_TIME);
226
+ var div = Blockly.WidgetDiv.DIV;
227
+ // Apply text-input-specific fixed CSS
228
+ div.className += ' fieldTextInput';
229
+ // Create the input.
230
+ var htmlInput =
231
+ goog.dom.createDom(goog.dom.TagName.INPUT, 'blocklyHtmlInput');
232
+ htmlInput.setAttribute('spellcheck', this.spellcheck_);
233
+ if (readOnly) {
234
+ htmlInput.setAttribute('readonly', 'true');
235
+ }
236
+ /** @type {!HTMLInputElement} */
237
+ Blockly.FieldTextInput.htmlInput_ = htmlInput;
238
+ div.appendChild(htmlInput);
239
+
240
+ if (opt_withArrow) {
241
+ // Move text in input to account for displayed drop-down arrow.
242
+ if (this.sourceBlock_.RTL) {
243
+ htmlInput.style.paddingLeft = (this.arrowSize_ + Blockly.BlockSvg.DROPDOWN_ARROW_PADDING) + 'px';
244
+ } else {
245
+ htmlInput.style.paddingRight = (this.arrowSize_ + Blockly.BlockSvg.DROPDOWN_ARROW_PADDING) + 'px';
246
+ }
247
+ // Create the arrow.
248
+ var dropDownArrow =
249
+ goog.dom.createDom(goog.dom.TagName.IMG, 'blocklyTextDropDownArrow');
250
+ dropDownArrow.setAttribute('src',
251
+ Blockly.mainWorkspace.options.pathToMedia + 'dropdown-arrow-dark.svg');
252
+ dropDownArrow.style.width = this.arrowSize_ + 'px';
253
+ dropDownArrow.style.height = this.arrowSize_ + 'px';
254
+ dropDownArrow.style.top = this.arrowY_ + 'px';
255
+ dropDownArrow.style.cursor = 'pointer';
256
+ // Magic number for positioning the drop-down arrow on top of the text editor.
257
+ var dropdownArrowMagic = '11px';
258
+ if (this.sourceBlock_.RTL) {
259
+ dropDownArrow.style.left = dropdownArrowMagic;
260
+ } else {
261
+ dropDownArrow.style.right = dropdownArrowMagic;
262
+ }
263
+ if (opt_arrowCallback) {
264
+ htmlInput.dropDownArrowMouseWrapper_ = Blockly.bindEvent_(dropDownArrow,
265
+ 'mousedown', this, opt_arrowCallback);
266
+ }
267
+ div.appendChild(dropDownArrow);
268
+ }
269
+
270
+ htmlInput.value = htmlInput.defaultValue = this.text_;
271
+ htmlInput.oldValue_ = null;
272
+ this.validate_();
273
+ this.resizeEditor_();
274
+ if (!quietInput) {
275
+ htmlInput.focus();
276
+ htmlInput.select();
277
+ // For iOS only
278
+ htmlInput.setSelectionRange(0, 99999);
279
+ }
280
+
281
+ this.bindEvents_(htmlInput, quietInput || readOnly);
282
+
283
+ // Add animation transition properties
284
+ var transitionProperties = 'box-shadow ' + Blockly.FieldTextInput.ANIMATION_TIME + 's';
285
+ if (Blockly.BlockSvg.FIELD_TEXTINPUT_ANIMATE_POSITIONING) {
286
+ div.style.transition += ',padding ' + Blockly.FieldTextInput.ANIMATION_TIME + 's,' +
287
+ 'width ' + Blockly.FieldTextInput.ANIMATION_TIME + 's,' +
288
+ 'height ' + Blockly.FieldTextInput.ANIMATION_TIME + 's,' +
289
+ 'margin-left ' + Blockly.FieldTextInput.ANIMATION_TIME + 's';
290
+ }
291
+ div.style.transition = transitionProperties;
292
+ htmlInput.style.transition = 'font-size ' + Blockly.FieldTextInput.ANIMATION_TIME + 's';
293
+ // The animated properties themselves
294
+ htmlInput.style.fontSize = Blockly.BlockSvg.FIELD_TEXTINPUT_FONTSIZE_FINAL + 'pt';
295
+ div.style.boxShadow = '0px 0px 0px 4px ' + Blockly.Colours.fieldShadow;
296
+ };
297
+
298
+ /**
299
+ * Bind handlers for user input on this field and size changes on the workspace.
300
+ * @param {!HTMLInputElement} htmlInput The htmlInput created in showEditor, to
301
+ * which event handlers will be bound.
302
+ * @param {boolean} bindGlobalKeypress Whether to bind a keypress listener to enable
303
+ * keyboard editing without focusing the field.
304
+ * @private
305
+ */
306
+ Blockly.FieldTextInput.prototype.bindEvents_ = function(
307
+ htmlInput, bindGlobalKeypress) {
308
+ // Bind to keydown -- trap Enter without IME and Esc to hide.
309
+ htmlInput.onKeyDownWrapper_ =
310
+ Blockly.bindEventWithChecks_(htmlInput, 'keydown', this,
311
+ this.onHtmlInputKeyDown_);
312
+ // Bind to keyup -- trap Enter; resize after every keystroke.
313
+ htmlInput.onKeyUpWrapper_ =
314
+ Blockly.bindEventWithChecks_(htmlInput, 'keyup', this,
315
+ this.onHtmlInputChange_);
316
+ // Bind to keyPress -- repeatedly resize when holding down a key.
317
+ htmlInput.onKeyPressWrapper_ =
318
+ Blockly.bindEventWithChecks_(htmlInput, 'keypress', this,
319
+ this.onHtmlInputChange_);
320
+ // For modern browsers (IE 9+, Chrome, Firefox, etc.) that support the
321
+ // DOM input event, also trigger onHtmlInputChange_ then. The input event
322
+ // is triggered on keypress but after the value of the text input
323
+ // has updated, allowing us to resize the block at that time.
324
+ htmlInput.onInputWrapper_ =
325
+ Blockly.bindEvent_(htmlInput, 'input', this, this.onHtmlInputChange_);
326
+ htmlInput.onWorkspaceChangeWrapper_ = this.resizeEditor_.bind(this);
327
+ this.workspace_.addChangeListener(htmlInput.onWorkspaceChangeWrapper_);
328
+
329
+ if (bindGlobalKeypress) {
330
+ htmlInput.onDocumentKeyDownWrapper_ =
331
+ Blockly.bindEventWithChecks_(document, 'keydown', this,
332
+ this.onDocumentKeyDown_);
333
+ }
334
+ };
335
+
336
+ /**
337
+ * Unbind handlers for user input and workspace size changes.
338
+ * @param {!HTMLInputElement} htmlInput The html for this text input.
339
+ * @private
340
+ */
341
+ Blockly.FieldTextInput.prototype.unbindEvents_ = function(htmlInput) {
342
+ Blockly.unbindEvent_(htmlInput.onKeyDownWrapper_);
343
+ Blockly.unbindEvent_(htmlInput.onKeyUpWrapper_);
344
+ Blockly.unbindEvent_(htmlInput.onKeyPressWrapper_);
345
+ Blockly.unbindEvent_(htmlInput.onInputWrapper_);
346
+ this.workspace_.removeChangeListener(
347
+ htmlInput.onWorkspaceChangeWrapper_);
348
+
349
+ // Remove document handler only if it was added (e.g. in quiet mode)
350
+ if (htmlInput.onDocumentKeyDownWrapper_) {
351
+ Blockly.unbindEvent_(htmlInput.onDocumentKeyDownWrapper_);
352
+ }
353
+ };
354
+
355
+ /**
356
+ * Handle key down to the editor.
357
+ * @param {!Event} e Keyboard event.
358
+ * @private
359
+ */
360
+ Blockly.FieldTextInput.prototype.onHtmlInputKeyDown_ = function(e) {
361
+ var htmlInput = Blockly.FieldTextInput.htmlInput_;
362
+ var tabKey = 9, enterKey = 13, escKey = 27;
363
+ if (e.keyCode == enterKey) {
364
+ Blockly.WidgetDiv.hide();
365
+ Blockly.DropDownDiv.hideWithoutAnimation();
366
+ } else if (e.keyCode == escKey) {
367
+ htmlInput.value = htmlInput.defaultValue;
368
+ Blockly.WidgetDiv.hide();
369
+ Blockly.DropDownDiv.hideWithoutAnimation();
370
+ } else if (e.keyCode == tabKey) {
371
+ Blockly.WidgetDiv.hide();
372
+ Blockly.DropDownDiv.hideWithoutAnimation();
373
+ this.sourceBlock_.tab(this, !e.shiftKey);
374
+ e.preventDefault();
375
+ }
376
+ };
377
+
378
+ Blockly.FieldTextInput.prototype.onDocumentKeyDown_ = function(e) {
379
+ var htmlInput = Blockly.FieldTextInput.htmlInput_;
380
+ var targetMatches = e.target === htmlInput;
381
+ var targetIsInput = e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA';
382
+ if (targetMatches || !targetIsInput) { // Ignore keys into other inputs
383
+ htmlInput.removeAttribute('readonly');
384
+ htmlInput.value = ''; // Reset the input, new value is picked up by input keypress
385
+ htmlInput.focus();
386
+ Blockly.unbindEvent_(htmlInput.onDocumentKeyDownWrapper_);
387
+ htmlInput.onDocumentKeyDownWrapper_ = null;
388
+ }
389
+ };
390
+
391
+ /**
392
+ * Key codes that are whitelisted from the restrictor.
393
+ * These are only needed and used on Gecko (Firefox).
394
+ * See: https://github.com/LLK/scratch-blocks/issues/503.
395
+ */
396
+ Blockly.FieldTextInput.GECKO_KEYCODE_WHITELIST = [
397
+ 97, // Select all, META-A.
398
+ 99, // Copy, META-C.
399
+ 118, // Paste, META-V.
400
+ 120 // Cut, META-X.
401
+ ];
402
+
403
+ /**
404
+ * Handle a change to the editor.
405
+ * @param {!Event} e Keyboard event.
406
+ * @private
407
+ */
408
+ Blockly.FieldTextInput.prototype.onHtmlInputChange_ = function(e) {
409
+ // Check if the key matches the restrictor.
410
+ if (e.type === 'keypress' && this.restrictor_) {
411
+ var keyCode;
412
+ var isWhitelisted = false;
413
+ if (goog.userAgent.GECKO) {
414
+ // e.keyCode is not available in Gecko.
415
+ keyCode = e.charCode;
416
+ // Gecko reports control characters (e.g., left, right, copy, paste)
417
+ // in the key event - whitelist these from being restricted.
418
+ // < 32 and 127 (delete) are control characters.
419
+ // See: http://www.theasciicode.com.ar/ascii-control-characters/delete-ascii-code-127.html
420
+ if (keyCode < 32 || keyCode == 127) {
421
+ isWhitelisted = true;
422
+ } else if (e.metaKey || e.ctrlKey) {
423
+ // For combos (ctrl-v, ctrl-c, etc.), Gecko reports the ASCII letter
424
+ // and the metaKey/ctrlKey flags.
425
+ isWhitelisted = Blockly.FieldTextInput.GECKO_KEYCODE_WHITELIST.indexOf(keyCode) > -1;
426
+ }
427
+ } else {
428
+ keyCode = e.keyCode;
429
+ }
430
+ var char = String.fromCharCode(keyCode);
431
+ if (!isWhitelisted && !this.restrictor_.test(char) && e.preventDefault) {
432
+ // Failed to pass restrictor.
433
+ e.preventDefault();
434
+ return;
435
+ }
436
+ }
437
+ var htmlInput = Blockly.FieldTextInput.htmlInput_;
438
+ // Update source block.
439
+ var text = htmlInput.value;
440
+ if (text !== htmlInput.oldValue_) {
441
+ htmlInput.oldValue_ = text;
442
+ this.setText(text);
443
+ this.validate_();
444
+ } else if (goog.userAgent.WEBKIT) {
445
+ // Cursor key. Render the source block to show the caret moving.
446
+ // Chrome only (version 26, OS X).
447
+ this.sourceBlock_.render();
448
+ }
449
+ this.resizeEditor_();
450
+ };
451
+
452
+ /**
453
+ * Check to see if the contents of the editor validates.
454
+ * Style the editor accordingly.
455
+ * @private
456
+ */
457
+ Blockly.FieldTextInput.prototype.validate_ = function() {
458
+ var valid = true;
459
+ goog.asserts.assertObject(Blockly.FieldTextInput.htmlInput_);
460
+ var htmlInput = Blockly.FieldTextInput.htmlInput_;
461
+ if (this.sourceBlock_) {
462
+ valid = this.callValidator(htmlInput.value);
463
+ }
464
+ if (valid === null) {
465
+ Blockly.utils.addClass(htmlInput, 'blocklyInvalidInput');
466
+ } else {
467
+ Blockly.utils.removeClass(htmlInput, 'blocklyInvalidInput');
468
+ }
469
+ };
470
+
471
+ /**
472
+ * Resize the editor and the underlying block to fit the text.
473
+ * @private
474
+ */
475
+ Blockly.FieldTextInput.prototype.resizeEditor_ = function() {
476
+ var scale = this.sourceBlock_.workspace.scale;
477
+ var div = Blockly.WidgetDiv.DIV;
478
+
479
+ var initialWidth;
480
+ if (this.sourceBlock_.isShadow()) {
481
+ initialWidth = this.sourceBlock_.getHeightWidth().width * scale;
482
+ } else {
483
+ initialWidth = this.size_.width * scale;
484
+ }
485
+
486
+ var width;
487
+ if (Blockly.BlockSvg.FIELD_TEXTINPUT_EXPAND_PAST_TRUNCATION) {
488
+ // Resize the box based on the measured width of the text, pre-truncation
489
+ var textWidth = Blockly.scratchBlocksUtils.measureText(
490
+ Blockly.FieldTextInput.htmlInput_.style.fontSize,
491
+ Blockly.FieldTextInput.htmlInput_.style.fontFamily,
492
+ Blockly.FieldTextInput.htmlInput_.style.fontWeight,
493
+ Blockly.FieldTextInput.htmlInput_.value
494
+ );
495
+ // Size drawn in the canvas needs padding and scaling
496
+ textWidth += Blockly.FieldTextInput.TEXT_MEASURE_PADDING_MAGIC;
497
+ textWidth *= scale;
498
+ width = textWidth;
499
+ } else {
500
+ // Set width to (truncated) block size.
501
+ width = initialWidth;
502
+ }
503
+ // The width must be at least FIELD_WIDTH and at most FIELD_WIDTH_MAX_EDIT
504
+ width = Math.max(width, Blockly.BlockSvg.FIELD_WIDTH_MIN_EDIT * scale);
505
+ width = Math.min(width, Blockly.BlockSvg.FIELD_WIDTH_MAX_EDIT * scale);
506
+ // Add 1px to width and height to account for border (pre-scale)
507
+ div.style.width = (width / scale + 1) + 'px';
508
+ div.style.height = (Blockly.BlockSvg.FIELD_HEIGHT_MAX_EDIT + 1) + 'px';
509
+ div.style.transform = 'scale(' + scale + ')';
510
+
511
+ // Use margin-left to animate repositioning of the box (value is unscaled).
512
+ // This is the difference between the default position and the positioning
513
+ // after growing the box.
514
+ div.style.marginLeft = -0.5 * (width - initialWidth) + 'px';
515
+
516
+ // Add 0.5px to account for slight difference between SVG and CSS border
517
+ var borderRadius = this.getBorderRadius() + 0.5;
518
+ div.style.borderRadius = borderRadius + 'px';
519
+ Blockly.FieldTextInput.htmlInput_.style.borderRadius = borderRadius + 'px';
520
+ // Pull stroke colour from the existing shadow block
521
+ var strokeColour = this.sourceBlock_.getColourTertiary();
522
+ div.style.borderColor = strokeColour;
523
+
524
+ var xy = this.getAbsoluteXY_();
525
+ // Account for border width, post-scale
526
+ xy.x -= scale / 2;
527
+ xy.y -= scale / 2;
528
+ // In RTL mode block fields and LTR input fields the left edge moves,
529
+ // whereas the right edge is fixed. Reposition the editor.
530
+ if (this.sourceBlock_.RTL) {
531
+ xy.x += width;
532
+ xy.x -= div.offsetWidth * scale;
533
+ xy.x += 1 * scale;
534
+ }
535
+ // Shift by a few pixels to line up exactly.
536
+ xy.y += 1 * scale;
537
+ if (goog.userAgent.GECKO && Blockly.WidgetDiv.DIV.style.top) {
538
+ // Firefox mis-reports the location of the border by a pixel
539
+ // once the WidgetDiv is moved into position.
540
+ xy.x += 2 * scale;
541
+ xy.y += 1 * scale;
542
+ }
543
+ if (goog.userAgent.WEBKIT) {
544
+ xy.y -= 1 * scale;
545
+ }
546
+ // Finally, set the actual style
547
+ div.style.left = xy.x + 'px';
548
+ div.style.top = xy.y + 'px';
549
+ };
550
+
551
+ /**
552
+ * Border radius for drawing this field, called when rendering the owning shadow block.
553
+ * @return {Number} Border radius in px.
554
+ */
555
+ Blockly.FieldTextInput.prototype.getBorderRadius = function() {
556
+ if (this.sourceBlock_.getOutputShape() == Blockly.OUTPUT_SHAPE_ROUND) {
557
+ return Blockly.BlockSvg.NUMBER_FIELD_CORNER_RADIUS;
558
+ }
559
+ return Blockly.BlockSvg.TEXT_FIELD_CORNER_RADIUS;
560
+ };
561
+
562
+ /**
563
+ * Close the editor, save the results, and start animating the disposal of elements.
564
+ * @return {!Function} Closure to call on destruction of the WidgetDiv.
565
+ * @private
566
+ */
567
+ Blockly.FieldTextInput.prototype.widgetDispose_ = function() {
568
+ var thisField = this;
569
+ return function() {
570
+ var div = Blockly.WidgetDiv.DIV;
571
+ var htmlInput = Blockly.FieldTextInput.htmlInput_;
572
+ // Save the edit (if it validates).
573
+ thisField.maybeSaveEdit_();
574
+
575
+ thisField.unbindEvents_(htmlInput);
576
+ if (htmlInput.dropDownArrowMouseWrapper_) {
577
+ Blockly.unbindEvent_(htmlInput.dropDownArrowMouseWrapper_);
578
+ }
579
+ Blockly.Events.setGroup(false);
580
+
581
+ // Animation of disposal
582
+ htmlInput.style.fontSize = Blockly.BlockSvg.FIELD_TEXTINPUT_FONTSIZE_INITIAL + 'pt';
583
+ div.style.boxShadow = '';
584
+ // Resize to actual size of final source block.
585
+ if (thisField.sourceBlock_) {
586
+ if (thisField.sourceBlock_.isShadow()) {
587
+ var size = thisField.sourceBlock_.getHeightWidth();
588
+ div.style.width = (size.width + 1) + 'px';
589
+ div.style.height = (size.height + 1) + 'px';
590
+ } else {
591
+ div.style.width = (thisField.size_.width + 1) + 'px';
592
+ div.style.height = (Blockly.BlockSvg.FIELD_HEIGHT_MAX_EDIT + 1) + 'px';
593
+ }
594
+ }
595
+ div.style.marginLeft = 0;
596
+ };
597
+ };
598
+
599
+ /**
600
+ * Final disposal of the text field's elements and properties.
601
+ * @return {!Function} Closure to call on finish animation of the WidgetDiv.
602
+ * @private
603
+ */
604
+ Blockly.FieldTextInput.prototype.widgetDisposeAnimationFinished_ = function() {
605
+ return function() {
606
+ // Delete style properties.
607
+ var style = Blockly.WidgetDiv.DIV.style;
608
+ style.width = 'auto';
609
+ style.height = 'auto';
610
+ style.fontSize = '';
611
+ // Reset class
612
+ Blockly.WidgetDiv.DIV.className = 'blocklyWidgetDiv';
613
+ // Remove all styles
614
+ Blockly.WidgetDiv.DIV.removeAttribute('style');
615
+ Blockly.FieldTextInput.htmlInput_.style.transition = '';
616
+ Blockly.FieldTextInput.htmlInput_ = null;
617
+ };
618
+ };
619
+
620
+ Blockly.FieldTextInput.prototype.maybeSaveEdit_ = function() {
621
+ var htmlInput = Blockly.FieldTextInput.htmlInput_;
622
+ // Save the edit (if it validates).
623
+ var text = htmlInput.value;
624
+ if (this.sourceBlock_) {
625
+ var text1 = this.callValidator(text);
626
+ if (text1 === null) {
627
+ // Invalid edit.
628
+ text = htmlInput.defaultValue;
629
+ } else {
630
+ // Validation function has changed the text.
631
+ text = text1;
632
+ if (this.onFinishEditing_) {
633
+ this.onFinishEditing_(text);
634
+ }
635
+ }
636
+ }
637
+ this.setText(text);
638
+ this.sourceBlock_.rendered && this.sourceBlock_.render();
639
+ };
640
+
641
+ /**
642
+ * Ensure that only a number may be entered.
643
+ * @param {string} text The user's text.
644
+ * @return {?string} A string representing a valid number, or null if invalid.
645
+ */
646
+ Blockly.FieldTextInput.numberValidator = function(text) {
647
+ console.warn('Blockly.FieldTextInput.numberValidator is deprecated. ' +
648
+ 'Use Blockly.FieldNumber instead.');
649
+ if (text === null) {
650
+ return null;
651
+ }
652
+ text = String(text);
653
+ // TODO: Handle cases like 'ten', '1.203,14', etc.
654
+ // 'O' is sometimes mistaken for '0' by inexperienced users.
655
+ text = text.replace(/O/ig, '0');
656
+ // Strip out thousands separators.
657
+ text = text.replace(/,/g, '');
658
+ var n = parseFloat(text || 0);
659
+ return isNaN(n) ? null : String(n);
660
+ };
661
+
662
+ /**
663
+ * Ensure that only a nonnegative integer may be entered.
664
+ * @param {string} text The user's text.
665
+ * @return {?string} A string representing a valid int, or null if invalid.
666
+ */
667
+ Blockly.FieldTextInput.nonnegativeIntegerValidator = function(text) {
668
+ var n = Blockly.FieldTextInput.numberValidator(text);
669
+ if (n) {
670
+ n = String(Math.max(0, Math.floor(n)));
671
+ }
672
+ return n;
673
+ };
674
+
675
+ Blockly.Field.register('field_input', Blockly.FieldTextInput);