-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathloops.ts
More file actions
418 lines (398 loc) · 11.3 KB
/
loops.ts
File metadata and controls
418 lines (398 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/**
* @license
* Copyright 2012 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
// Former goog.module ID: Blockly.libraryBlocks.loops
import type {Block} from '../core/block.js';
import {
createBlockDefinitionsFromJsonArray,
defineBlocks,
} from '../core/common.js';
import * as ContextMenu from '../core/contextmenu.js';
import type {
ContextMenuOption,
LegacyContextMenuOption,
} from '../core/contextmenu_registry.js';
import * as Events from '../core/events/events.js';
import type {Abstract as AbstractEvent} from '../core/events/events_abstract.js';
import * as eventUtils from '../core/events/utils.js';
import * as Extensions from '../core/extensions.js';
import '../core/field_dropdown.js';
import '../core/field_label.js';
import '../core/field_number.js';
import '../core/field_variable.js';
import {FieldVariable} from '../core/field_variable.js';
import '../core/icons/warning_icon.js';
import {Msg} from '../core/msg.js';
import {WorkspaceSvg} from '../core/workspace_svg.js';
/**
* A dictionary of the block definitions provided by this module.
*/
export const blocks = createBlockDefinitionsFromJsonArray([
// Block for repeat n times (external number).
{
'type': 'controls_repeat_ext',
'message0': '%{BKY_CONTROLS_REPEAT_TITLE}',
'args0': [
{
'type': 'input_value',
'name': 'TIMES',
'check': 'Number',
},
],
'message1': '%{BKY_CONTROLS_REPEAT_INPUT_DO} %1',
'args1': [
{
'type': 'input_statement',
'name': 'DO',
},
],
'previousStatement': null,
'nextStatement': null,
'style': 'loop_blocks',
'tooltip': '%{BKY_CONTROLS_REPEAT_TOOLTIP}',
'helpUrl': '%{BKY_CONTROLS_REPEAT_HELPURL}',
},
// Block for repeat n times (internal number).
// The 'controls_repeat_ext' block is preferred as it is more flexible.
{
'type': 'controls_repeat',
'message0': '%{BKY_CONTROLS_REPEAT_TITLE}',
'args0': [
{
'type': 'field_number',
'name': 'TIMES',
'value': 10,
'min': 0,
'precision': 1,
},
],
'message1': '%{BKY_CONTROLS_REPEAT_INPUT_DO} %1',
'args1': [
{
'type': 'input_statement',
'name': 'DO',
},
],
'previousStatement': null,
'nextStatement': null,
'style': 'loop_blocks',
'tooltip': '%{BKY_CONTROLS_REPEAT_TOOLTIP}',
'helpUrl': '%{BKY_CONTROLS_REPEAT_HELPURL}',
},
// Block for 'do while/until' loop.
{
'type': 'controls_whileUntil',
'message0': '%1 %2',
'args0': [
{
'type': 'field_dropdown',
'name': 'MODE',
'ariaTypeName': 'Repeat type',
'options': [
['%{BKY_CONTROLS_WHILEUNTIL_OPERATOR_WHILE}', 'WHILE', 'While'],
['%{BKY_CONTROLS_WHILEUNTIL_OPERATOR_UNTIL}', 'UNTIL', 'Until'],
],
},
{
'type': 'input_value',
'name': 'BOOL',
'check': 'Boolean',
},
],
'message1': '%{BKY_CONTROLS_REPEAT_INPUT_DO} %1',
'args1': [
{
'type': 'input_statement',
'name': 'DO',
},
],
'previousStatement': null,
'nextStatement': null,
'style': 'loop_blocks',
'helpUrl': '%{BKY_CONTROLS_WHILEUNTIL_HELPURL}',
'extensions': ['controls_whileUntil_tooltip'],
},
// Block for 'for' loop.
{
'type': 'controls_for',
'message0': '%{BKY_CONTROLS_FOR_TITLE}',
'args0': [
{
'type': 'field_variable',
'name': 'VAR',
'variable': null,
},
{
'type': 'input_value',
'name': 'FROM',
'check': 'Number',
'align': 'RIGHT',
},
{
'type': 'input_value',
'name': 'TO',
'check': 'Number',
'align': 'RIGHT',
},
{
'type': 'input_value',
'name': 'BY',
'check': 'Number',
'align': 'RIGHT',
},
],
'message1': '%{BKY_CONTROLS_REPEAT_INPUT_DO} %1',
'args1': [
{
'type': 'input_statement',
'name': 'DO',
},
],
'inputsInline': true,
'previousStatement': null,
'nextStatement': null,
'style': 'loop_blocks',
'helpUrl': '%{BKY_CONTROLS_FOR_HELPURL}',
'extensions': ['contextMenu_newGetVariableBlock', 'controls_for_tooltip'],
},
// Block for 'for each' loop.
{
'type': 'controls_forEach',
'message0': '%{BKY_CONTROLS_FOREACH_TITLE}',
'args0': [
{
'type': 'field_variable',
'name': 'VAR',
'variable': null,
},
{
'type': 'input_value',
'name': 'LIST',
'check': 'Array',
},
],
'message1': '%{BKY_CONTROLS_REPEAT_INPUT_DO} %1',
'args1': [
{
'type': 'input_statement',
'name': 'DO',
},
],
'previousStatement': null,
'nextStatement': null,
'style': 'loop_blocks',
'helpUrl': '%{BKY_CONTROLS_FOREACH_HELPURL}',
'extensions': [
'contextMenu_newGetVariableBlock',
'controls_forEach_tooltip',
],
},
// Block for flow statements: continue, break.
{
'type': 'controls_flow_statements',
'message0': '%1',
'args0': [
{
'type': 'field_dropdown',
'name': 'FLOW',
'ariaTypeName': 'Continue type',
'options': [
[
'%{BKY_CONTROLS_FLOW_STATEMENTS_OPERATOR_BREAK}',
'BREAK',
'Break loop',
],
[
'%{BKY_CONTROLS_FLOW_STATEMENTS_OPERATOR_CONTINUE}',
'CONTINUE',
'Continue loop',
],
],
},
],
'previousStatement': null,
'style': 'loop_blocks',
'helpUrl': '%{BKY_CONTROLS_FLOW_STATEMENTS_HELPURL}',
'suppressPrefixSuffix': true,
'extensions': ['controls_flow_tooltip', 'controls_flow_in_loop_check'],
},
]);
/**
* Tooltips for the 'controls_whileUntil' block, keyed by MODE value.
*
* @see {Extensions#buildTooltipForDropdown}
*/
const WHILE_UNTIL_TOOLTIPS = {
'WHILE': '%{BKY_CONTROLS_WHILEUNTIL_TOOLTIP_WHILE}',
'UNTIL': '%{BKY_CONTROLS_WHILEUNTIL_TOOLTIP_UNTIL}',
};
Extensions.register(
'controls_whileUntil_tooltip',
Extensions.buildTooltipForDropdown('MODE', WHILE_UNTIL_TOOLTIPS),
);
/**
* Tooltips for the 'controls_flow_statements' block, keyed by FLOW value.
*
* @see {Extensions#buildTooltipForDropdown}
*/
const BREAK_CONTINUE_TOOLTIPS = {
'BREAK': '%{BKY_CONTROLS_FLOW_STATEMENTS_TOOLTIP_BREAK}',
'CONTINUE': '%{BKY_CONTROLS_FLOW_STATEMENTS_TOOLTIP_CONTINUE}',
};
Extensions.register(
'controls_flow_tooltip',
Extensions.buildTooltipForDropdown('FLOW', BREAK_CONTINUE_TOOLTIPS),
);
/** Type of a block that has CUSTOM_CONTEXT_MENU_CREATE_VARIABLES_GET_MIXIN */
type CustomContextMenuBlock = Block & CustomContextMenuMixin;
interface CustomContextMenuMixin extends CustomContextMenuMixinType {}
type CustomContextMenuMixinType =
typeof CUSTOM_CONTEXT_MENU_CREATE_VARIABLES_GET_MIXIN;
/**
* Mixin to add a context menu item to create a 'variables_get' block.
* Used by blocks 'controls_for' and 'controls_forEach'.
*/
const CUSTOM_CONTEXT_MENU_CREATE_VARIABLES_GET_MIXIN = {
/**
* Add context menu option to create getter block for the loop's variable.
* (customContextMenu support limited to web BlockSvg.)
*
* @param options List of menu options to add to.
*/
customContextMenu: function (
this: CustomContextMenuBlock,
options: Array<ContextMenuOption | LegacyContextMenuOption>,
) {
if (this.isInFlyout) {
return;
}
const varField = this.getField('VAR') as FieldVariable;
const variable = varField.getVariable()!;
const varName = variable.getName();
if (!this.isCollapsed() && varName !== null) {
const getVarBlockState = {
type: 'variables_get',
fields: {VAR: varField.saveState(true)},
};
options.push({
enabled: true,
text: Msg['VARIABLES_SET_CREATE_GET'].replace('%1', varName),
callback: ContextMenu.callbackFactory(this, getVarBlockState),
});
}
},
};
Extensions.registerMixin(
'contextMenu_newGetVariableBlock',
CUSTOM_CONTEXT_MENU_CREATE_VARIABLES_GET_MIXIN,
);
Extensions.register(
'controls_for_tooltip',
Extensions.buildTooltipWithFieldText('%{BKY_CONTROLS_FOR_TOOLTIP}', 'VAR'),
);
Extensions.register(
'controls_forEach_tooltip',
Extensions.buildTooltipWithFieldText(
'%{BKY_CONTROLS_FOREACH_TOOLTIP}',
'VAR',
),
);
/**
* List of block types that are loops and thus do not need warnings.
* To add a new loop type add this to your code:
*
* // If using the Blockly npm package and es6 import syntax:
* import {loops} from 'blockly/blocks';
* loops.loopTypes.add('custom_loop');
*
* // Else if using Closure Compiler and goog.modules:
* const {loopTypes} = goog.require('Blockly.libraryBlocks.loops');
* loopTypes.add('custom_loop');
*
* // Else if using blockly_compressed + blockss_compressed.js in browser:
* Blockly.libraryBlocks.loopTypes.add('custom_loop');
*/
export const loopTypes: Set<string> = new Set([
'controls_repeat',
'controls_repeat_ext',
'controls_forEach',
'controls_for',
'controls_whileUntil',
]);
/**
* Type of a block that has CONTROL_FLOW_IN_LOOP_CHECK_MIXIN
*
* @internal
*/
export type ControlFlowInLoopBlock = Block & ControlFlowInLoopMixin;
interface ControlFlowInLoopMixin extends ControlFlowInLoopMixinType {}
type ControlFlowInLoopMixinType = typeof CONTROL_FLOW_IN_LOOP_CHECK_MIXIN;
/**
* The language-neutral ID for when the reason why a block is disabled is
* because the block is only valid inside of a loop.
*/
const CONTROL_FLOW_NOT_IN_LOOP_DISABLED_REASON = 'CONTROL_FLOW_NOT_IN_LOOP';
/**
* This mixin adds a check to make sure the 'controls_flow_statements' block
* is contained in a loop. Otherwise a warning is added to the block.
*/
const CONTROL_FLOW_IN_LOOP_CHECK_MIXIN = {
/**
* Is this block enclosed (at any level) by a loop?
*
* @returns The nearest surrounding loop, or null if none.
*/
getSurroundLoop: function (this: ControlFlowInLoopBlock): Block | null {
// eslint-disable-next-line @typescript-eslint/no-this-alias
let block: Block | null = this;
do {
if (loopTypes.has(block.type)) {
return block;
}
block = block.getSurroundParent();
} while (block);
return null;
},
/**
* Called whenever anything on the workspace changes.
* Add warning if this flow block is not nested inside a loop.
*/
onchange: function (this: ControlFlowInLoopBlock, e: AbstractEvent) {
const ws = this.workspace as WorkspaceSvg;
// Don't change state if:
// * It's at the start of a drag.
// * It's not a move event.
if (
!ws.isDragging ||
ws.isDragging() ||
(e.type !== Events.BLOCK_MOVE && e.type !== Events.BLOCK_CREATE)
) {
return;
}
const enabled = !!this.getSurroundLoop();
this.setWarningText(
enabled ? null : Msg['CONTROLS_FLOW_STATEMENTS_WARNING'],
);
if (!this.isInFlyout) {
try {
// There is no need to record the enable/disable change on the undo/redo
// list since the change will be automatically recreated when replayed.
eventUtils.setRecordUndo(false);
this.setDisabledReason(
!enabled,
CONTROL_FLOW_NOT_IN_LOOP_DISABLED_REASON,
);
} finally {
eventUtils.setRecordUndo(true);
}
}
},
};
Extensions.registerMixin(
'controls_flow_in_loop_check',
CONTROL_FLOW_IN_LOOP_CHECK_MIXIN,
);
// Register provided blocks.
defineBlocks(blocks);