File size: 10,506 Bytes
fbe84a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**

 * @license

 * Visual Blocks Editor

 *

 * Copyright 2017 Google Inc.

 * https://developers.google.com/blockly/

 *

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *   http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

/**

 * @fileoverview Extensions for vertical blocks in scratch-blocks.

 * The following extensions can be used to describe a block in Scratch terms.

 * For instance, a block in the operators colour scheme with a number output

 * would have the "colours_operators" and "output_number" extensions.

 * @author fenichel@google.com (Rachel Fenichel)

 */

'use strict';

goog.provide('Blockly.ScratchBlocks.VerticalExtensions');

goog.require('Blockly.Colours');
goog.require('Blockly.constants');


/**

 * Helper function that generates an extension based on a category name.

 * The generated function will set primary, secondary, and tertiary colours

 * based on the category name.

 * @param {String} category The name of the category to set colours for.

 * @return {function} An extension function that sets colours based on the given

 *     category.

 */
Blockly.ScratchBlocks.VerticalExtensions.colourHelper = function(category) {
  var colours = Blockly.Colours[category];
  if (!(colours && colours.primary && colours.secondary && colours.tertiary)) {
    throw new Error('Could not find colours for category "' + category + '"');
  }
  /**

   * Set the primary, secondary, and tertiary colours on this block for the

   * given category.

   * @this {Blockly.Block}

   */
  return function() {
    this.setColourFromRawValues_(colours.primary, colours.secondary,
        colours.tertiary);
  };
};

/**

 * Extension to set the colours of a text field, which are all the same.

 */
Blockly.ScratchBlocks.VerticalExtensions.COLOUR_TEXTFIELD = function() {
  this.setColourFromRawValues_(Blockly.Colours.textField,
      Blockly.Colours.textField, Blockly.Colours.textField);
};

/**

 * Extension to make a block fit into a stack of statements, regardless of its

 * inputs.  That means the block should have a previous connection and a next

 * connection and have inline inputs.

 * @this {Blockly.Block}

 * @readonly

 */
Blockly.ScratchBlocks.VerticalExtensions.SHAPE_PROCEDURE = function() {
  this.setInputsInline(true);
  this.setPreviousStatement(true, 'procedure');
  this.setNextStatement(true, 'procedure');
};

/**

 * Extension to make a block fit into a stack of statements, regardless of its

 * inputs.  That means the block should have a previous connection and a next

 * connection and have inline inputs.

 * @this {Blockly.Block}

 * @readonly

 */
Blockly.ScratchBlocks.VerticalExtensions.SHAPE_CASE = function() {
  this.setInputsInline(true);
  this.setPreviousStatement(true, 'switchCase');
  this.setNextStatement(true, 'switchCase');
};

/**

 * Extension to make a block fit into a stack of statements, regardless of its

 * inputs.  That means the block should have a previous connection and a next

 * connection and have inline inputs.

 * @this {Blockly.Block}

 * @readonly

 */
Blockly.ScratchBlocks.VerticalExtensions.SHAPE_STATEMENT = function() {
  this.setInputsInline(true);
  this.setPreviousStatement(true, 'normal');
  this.setNextStatement(true, 'normal');
};

/**

 * Extension to make a block be shaped as a hat block, regardless of its

 * inputs.  That means the block should have a next connection and have inline

 * inputs, but have no previous connection.

 * @this {Blockly.Block}

 * @readonly

 */
Blockly.ScratchBlocks.VerticalExtensions.SHAPE_HAT = function() {
  this.setInputsInline(true);
  this.setNextStatement(true, 'normal');
};

/**

 * Extension to make a block be shaped as an end block, regardless of its

 * inputs.  That means the block should have a previous connection and have

 * inline inputs, but have no next connection.

 * @this {Blockly.Block}

 * @readonly

 */
Blockly.ScratchBlocks.VerticalExtensions.SHAPE_END = function() {
  this.setInputsInline(true);
  this.setPreviousStatement(true, 'normal');
};

/**

 * Extension to make represent a number reporter in Scratch-Blocks.

 * That means the block has inline inputs, a round output shape, and a 'Number'

 * output type.

 * @this {Blockly.Block}

 * @readonly

 */
Blockly.ScratchBlocks.VerticalExtensions.OUTPUT_NUMBER = function() {
  this.setInputsInline(true);
  this.setOutputShape(Blockly.OUTPUT_SHAPE_ROUND);
  this.setOutput(true, 'Number');
};

/**

 * Extension to make represent a string reporter in Scratch-Blocks.

 * That means the block has inline inputs, a round output shape, and a 'String'

 * output type.

 * @this {Blockly.Block}

 * @readonly

 */
Blockly.ScratchBlocks.VerticalExtensions.OUTPUT_STRING = function() {
  this.setInputsInline(true);
  this.setOutputShape(Blockly.OUTPUT_SHAPE_ROUND);
  this.setOutput(true, 'String');
};

/**

 * Extension to make represent a boolean reporter in Scratch-Blocks.

 * That means the block has inline inputs, a round output shape, and a 'Boolean'

 * output type.

 * @this {Blockly.Block}

 * @readonly

 */
Blockly.ScratchBlocks.VerticalExtensions.OUTPUT_BOOLEAN = function() {
  this.setInputsInline(true);
  this.setOutputShape(Blockly.OUTPUT_SHAPE_HEXAGONAL);
  this.setOutput(true, 'Boolean');
};

/**

 * Mixin to add a context menu for a procedure definition block.

 * It adds the "edit" option and removes the "duplicate" option.

 * @mixin

 * @augments Blockly.Block

 * @package

 * @readonly

 */
Blockly.ScratchBlocks.VerticalExtensions.PROCEDURE_DEF_CONTEXTMENU = {
  /**

   * Add the "edit" option and removes the "duplicate" option from the context

   * menu.

   * @param {!Array.<!Object>} menuOptions List of menu options to edit.

   * @this Blockly.Block

   */
  customContextMenu: function(menuOptions) {
    // Add the edit option at the end.
    menuOptions.push(Blockly.Procedures.makeEditOption(this));

    // Find the delete option and update its callback to be specific to
    // functions.
    for (var i = 0, option; option = menuOptions[i]; i++) {
      if (option.text == Blockly.Msg.DELETE_BLOCK) {
        var input = this.getInput('custom_block');
        // this is the root block, not the shadow block.
        if (input && input.connection && input.connection.targetBlock()) {
          var procCode = input.connection.targetBlock().getProcCode();
        } else {
          return;
        }
        var rootBlock = this;
        option.callback = function() {
          var didDelete = Blockly.Procedures.deleteProcedureDefCallback(
              procCode, rootBlock);
          if (!didDelete) {
            alert(Blockly.Msg.PROCEDURE_USED);
          }
        };
      }
    }
    // Find and remove the duplicate option
    for (var i = 0, option; option = menuOptions[i]; i++) {
      if (option.text == Blockly.Msg.DUPLICATE) {
        menuOptions.splice(i, 1);
        break;
      }
    }
  }
};

/**

 * Mixin to add a context menu for a procedure call block.

 * It adds the "edit" option and the "define" option.

 * @mixin

 * @augments Blockly.Block

 * @package

 * @readonly

 */
Blockly.ScratchBlocks.VerticalExtensions.PROCEDURE_CALL_CONTEXTMENU = {
  /**

   * Add the "edit" option to the context menu.

   * @todo Add "go to definition" option once implemented.

   * @param {!Array.<!Object>} menuOptions List of menu options to edit.

   * @this Blockly.Block

   */
  customContextMenu: function(menuOptions) {
    menuOptions.push(Blockly.Procedures.makeEditOption(this));
  }
};


Blockly.ScratchBlocks.VerticalExtensions.SCRATCH_EXTENSION = function() {
  this.isScratchExtension = true;
};

/**

 * Register all extensions for scratch-blocks.

 * @package

 */
Blockly.ScratchBlocks.VerticalExtensions.registerAll = function() {
  var categoryNames =
      ['control', 'data', 'data_lists', 'sounds', 'motion', 'looks', 'event',
        'sensing', 'pen', 'operators', 'more'];
  // Register functions for all category colours.
  for (var i = 0; i < categoryNames.length; i++) {
    var name = categoryNames[i];
    Blockly.Extensions.register('colours_' + name,
        Blockly.ScratchBlocks.VerticalExtensions.colourHelper(name));
  }

  // Text fields transcend categories.
  Blockly.Extensions.register('colours_textfield',
      Blockly.ScratchBlocks.VerticalExtensions.COLOUR_TEXTFIELD);

  // Register extensions for common block shapes.
  Blockly.Extensions.register('shape_procedure',
      Blockly.ScratchBlocks.VerticalExtensions.SHAPE_PROCEDURE);
  Blockly.Extensions.register('shape_case',
      Blockly.ScratchBlocks.VerticalExtensions.SHAPE_CASE);
  Blockly.Extensions.register('shape_statement',
      Blockly.ScratchBlocks.VerticalExtensions.SHAPE_STATEMENT);
  Blockly.Extensions.register('shape_hat',
      Blockly.ScratchBlocks.VerticalExtensions.SHAPE_HAT);
  Blockly.Extensions.register('shape_end',
      Blockly.ScratchBlocks.VerticalExtensions.SHAPE_END);

  // Output shapes and types are related.
  Blockly.Extensions.register('output_number',
      Blockly.ScratchBlocks.VerticalExtensions.OUTPUT_NUMBER);
  Blockly.Extensions.register('output_string',
      Blockly.ScratchBlocks.VerticalExtensions.OUTPUT_STRING);
  Blockly.Extensions.register('output_boolean',
      Blockly.ScratchBlocks.VerticalExtensions.OUTPUT_BOOLEAN);

  // Custom procedures have interesting context menus.
  Blockly.Extensions.registerMixin('procedure_def_contextmenu',
      Blockly.ScratchBlocks.VerticalExtensions.PROCEDURE_DEF_CONTEXTMENU);
  Blockly.Extensions.registerMixin('procedure_call_contextmenu',
      Blockly.ScratchBlocks.VerticalExtensions.PROCEDURE_CALL_CONTEXTMENU);

  // Extension blocks have slightly different block rendering.
  Blockly.Extensions.register('scratch_extension',
      Blockly.ScratchBlocks.VerticalExtensions.SCRATCH_EXTENSION);
};

Blockly.ScratchBlocks.VerticalExtensions.registerAll();