Skip to content

Commit

Permalink
fix: readd the data blocks (#29)
Browse files Browse the repository at this point in the history
* fix: readd the data blocks

* fix: import constants correctly
  • Loading branch information
gonfunko authored Apr 19, 2024
1 parent 9f5f135 commit fafed65
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 56 deletions.
105 changes: 50 additions & 55 deletions blocks_vertical/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,10 @@
* limitations under the License.
*/

'use strict';

goog.provide('Blockly.Blocks.data');
goog.provide('Blockly.Constants.Data');

goog.require('Blockly.Blocks');
goog.require('Blockly.Colours');
goog.require('Blockly.constants');
goog.require('Blockly.ScratchBlocks.VerticalExtensions');
import * as Blockly from 'blockly/core';
import {Categories} from '../src/categories.js';
import * as Constants from '../src/constants.js';
import * as scratchBlocksUtils from '../core/scratch_blocks_utils.js';


Blockly.Blocks['data_variable'] = {
Expand All @@ -46,7 +41,7 @@ Blockly.Blocks['data_variable'] = {
"variableType": ""
}
],
"category": Blockly.Categories.data,
"category": Categories.data,
"checkboxInFlyout": true,
"extensions": ["contextMenu_getVariableBlock", "colours_data", "output_string"]
});
Expand All @@ -71,7 +66,7 @@ Blockly.Blocks['data_setvariableto'] = {
"name": "VALUE"
}
],
"category": Blockly.Categories.data,
"category": Categories.data,
"extensions": ["colours_data", "shape_statement"]
});
}
Expand All @@ -95,7 +90,7 @@ Blockly.Blocks['data_changevariableby'] = {
"name": "VALUE"
}
],
"category": Blockly.Categories.data,
"category": Categories.data,
"extensions": ["colours_data", "shape_statement"]
});
}
Expand All @@ -117,7 +112,7 @@ Blockly.Blocks['data_showvariable'] = {
],
"previousStatement": null,
"nextStatement": null,
"category": Blockly.Categories.data,
"category": Categories.data,
"extensions": ["colours_data"]
});
}
Expand All @@ -139,7 +134,7 @@ Blockly.Blocks['data_hidevariable'] = {
],
"previousStatement": null,
"nextStatement": null,
"category": Blockly.Categories.data,
"category": Categories.data,
"extensions": ["colours_data"]
});
}
Expand All @@ -158,10 +153,10 @@ Blockly.Blocks['data_listcontents'] = {
"type": "field_variable_getter",
"text": "",
"name": "LIST",
"variableType": Blockly.LIST_VARIABLE_TYPE
"variableType": Constants.LIST_VARIABLE_TYPE
}
],
"category": Blockly.Categories.dataLists,
"category": Categories.dataLists,
"extensions": ["contextMenu_getListBlock", "colours_data_lists", "output_string"],
"checkboxInFlyout": true
});
Expand Down Expand Up @@ -190,7 +185,7 @@ Blockly.Blocks['data_listindexall'] = {
]
}
],
"category": Blockly.Categories.data,
"category": Categories.data,
"extensions": ["colours_textfield", "output_string"]
});
}
Expand Down Expand Up @@ -218,7 +213,7 @@ Blockly.Blocks['data_listindexrandom'] = {
]
}
],
"category": Blockly.Categories.data,
"category": Categories.data,
"extensions": ["colours_textfield", "output_string"]
});
}
Expand All @@ -240,10 +235,10 @@ Blockly.Blocks['data_addtolist'] = {
{
"type": "field_variable",
"name": "LIST",
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
"variableTypes": [Constants.LIST_VARIABLE_TYPE]
}
],
"category": Blockly.Categories.dataLists,
"category": Categories.dataLists,
"extensions": ["colours_data_lists", "shape_statement"]
});
}
Expand All @@ -265,10 +260,10 @@ Blockly.Blocks['data_deleteoflist'] = {
{
"type": "field_variable",
"name": "LIST",
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
"variableTypes": [Constants.LIST_VARIABLE_TYPE]
}
],
"category": Blockly.Categories.dataLists,
"category": Categories.dataLists,
"extensions": ["colours_data_lists", "shape_statement"]
});
}
Expand All @@ -286,10 +281,10 @@ Blockly.Blocks['data_deletealloflist'] = {
{
"type": "field_variable",
"name": "LIST",
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
"variableTypes": [Constants.LIST_VARIABLE_TYPE]
}
],
"category": Blockly.Categories.dataLists,
"category": Categories.dataLists,
"extensions": ["colours_data_lists", "shape_statement"]
});
}
Expand All @@ -315,10 +310,10 @@ Blockly.Blocks['data_insertatlist'] = {
{
"type": "field_variable",
"name": "LIST",
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
"variableTypes": [Constants.LIST_VARIABLE_TYPE]
}
],
"category": Blockly.Categories.dataLists,
"category": Categories.dataLists,
"extensions": ["colours_data_lists", "shape_statement"]
});
}
Expand All @@ -340,14 +335,14 @@ Blockly.Blocks['data_replaceitemoflist'] = {
{
"type": "field_variable",
"name": "LIST",
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
"variableTypes": [Constants.LIST_VARIABLE_TYPE]
},
{
"type": "input_value",
"name": "ITEM"
}
],
"category": Blockly.Categories.dataLists,
"category": Categories.dataLists,
"extensions": ["colours_data_lists", "shape_statement"]
});
}
Expand All @@ -369,11 +364,11 @@ Blockly.Blocks['data_itemoflist'] = {
{
"type": "field_variable",
"name": "LIST",
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
"variableTypes": [Constants.LIST_VARIABLE_TYPE]
}
],
"output": null,
"category": Blockly.Categories.dataLists,
"category": Categories.dataLists,
"extensions": ["colours_data_lists"],
"outputShape": Blockly.OUTPUT_SHAPE_ROUND
});
Expand All @@ -396,11 +391,11 @@ Blockly.Blocks['data_itemnumoflist'] = {
{
"type": "field_variable",
"name": "LIST",
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
"variableTypes": [Constants.LIST_VARIABLE_TYPE]
}
],
"output": null,
"category": Blockly.Categories.dataLists,
"category": Categories.dataLists,
"extensions": ["colours_data_lists"],
"outputShape": Blockly.OUTPUT_SHAPE_ROUND
});
Expand All @@ -419,10 +414,10 @@ Blockly.Blocks['data_lengthoflist'] = {
{
"type": "field_variable",
"name": "LIST",
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
"variableTypes": [Constants.LIST_VARIABLE_TYPE]
}
],
"category": Blockly.Categories.dataLists,
"category": Categories.dataLists,
"extensions": ["colours_data_lists", "output_number"]
});
}
Expand All @@ -440,14 +435,14 @@ Blockly.Blocks['data_listcontainsitem'] = {
{
"type": "field_variable",
"name": "LIST",
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
"variableTypes": [Constants.LIST_VARIABLE_TYPE]
},
{
"type": "input_value",
"name": "ITEM"
}
],
"category": Blockly.Categories.dataLists,
"category": Categories.dataLists,
"extensions": ["colours_data_lists", "output_boolean"]
});
}
Expand All @@ -465,10 +460,10 @@ Blockly.Blocks['data_showlist'] = {
{
"type": "field_variable",
"name": "LIST",
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
"variableTypes": [Constants.LIST_VARIABLE_TYPE]
}
],
"category": Blockly.Categories.dataLists,
"category": Categories.dataLists,
"extensions": ["colours_data_lists", "shape_statement"]
});
}
Expand All @@ -486,10 +481,10 @@ Blockly.Blocks['data_hidelist'] = {
{
"type": "field_variable",
"name": "LIST",
"variableTypes": [Blockly.LIST_VARIABLE_TYPE]
"variableTypes": [Constants.LIST_VARIABLE_TYPE]
}
],
"category": Blockly.Categories.dataLists,
"category": Categories.dataLists,
"extensions": ["colours_data_lists", "shape_statement"]
});
}
Expand All @@ -503,7 +498,7 @@ Blockly.Blocks['data_hidelist'] = {
* @package
* @readonly
*/
Blockly.Constants.Data.CUSTOM_CONTEXT_MENU_GET_VARIABLE_MIXIN = {
const CUSTOM_CONTEXT_MENU_GET_VARIABLE_MIXIN = {
/**
* Add context menu option to change the selected variable.
* @param {!Array} options List of menu options to add to.
Expand All @@ -518,7 +513,7 @@ Blockly.Constants.Data.CUSTOM_CONTEXT_MENU_GET_VARIABLE_MIXIN = {
if (!this.isInFlyout) {
var variablesList = this.workspace.getVariablesOfType('');
variablesList.sort(function(a, b) {
return Blockly.scratchBlocksUtils.compareStrings(a.name, b.name);
return scratchBlocksUtils.compareStrings(a.name, b.name);
});
for (var i = 0; i < variablesList.length; i++) {
var varName = variablesList[i].name;
Expand All @@ -528,21 +523,21 @@ Blockly.Constants.Data.CUSTOM_CONTEXT_MENU_GET_VARIABLE_MIXIN = {
option.text = varName;

option.callback =
Blockly.Constants.Data.VARIABLE_OPTION_CALLBACK_FACTORY(this,
VARIABLE_OPTION_CALLBACK_FACTORY(this,
variablesList[i].getId(), fieldName);
options.push(option);
}
} else {
var renameOption = {
text: Blockly.Msg.RENAME_VARIABLE,
enabled: true,
callback: Blockly.Constants.Data.RENAME_OPTION_CALLBACK_FACTORY(this,
callback: RENAME_OPTION_CALLBACK_FACTORY(this,
fieldName)
};
var deleteOption = {
text: Blockly.Msg.DELETE_VARIABLE.replace('%1', currentVarName),
enabled: true,
callback: Blockly.Constants.Data.DELETE_OPTION_CALLBACK_FACTORY(this,
callback: DELETE_OPTION_CALLBACK_FACTORY(this,
fieldName)
};
options.push(renameOption);
Expand All @@ -552,7 +547,7 @@ Blockly.Constants.Data.CUSTOM_CONTEXT_MENU_GET_VARIABLE_MIXIN = {
};

Blockly.Extensions.registerMixin('contextMenu_getVariableBlock',
Blockly.Constants.Data.CUSTOM_CONTEXT_MENU_GET_VARIABLE_MIXIN);
CUSTOM_CONTEXT_MENU_GET_VARIABLE_MIXIN);

/**
* Mixin to add a context menu for a data_listcontents block. It adds one item for
Expand All @@ -562,7 +557,7 @@ Blockly.Extensions.registerMixin('contextMenu_getVariableBlock',
* @package
* @readonly
*/
Blockly.Constants.Data.CUSTOM_CONTEXT_MENU_GET_LIST_MIXIN = {
const CUSTOM_CONTEXT_MENU_GET_LIST_MIXIN = {
/**
* Add context menu option to change the selected list.
* @param {!Array} options List of menu options to add to.
Expand All @@ -577,7 +572,7 @@ Blockly.Constants.Data.CUSTOM_CONTEXT_MENU_GET_LIST_MIXIN = {
if (!this.isInFlyout) {
var variablesList = this.workspace.getVariablesOfType('list');
variablesList.sort(function(a, b) {
return Blockly.scratchBlocksUtils.compareStrings(a.name, b.name);
return scratchBlocksUtils.compareStrings(a.name, b.name);
});
for (var i = 0; i < variablesList.length; i++) {
var varName = variablesList[i].name;
Expand All @@ -587,21 +582,21 @@ Blockly.Constants.Data.CUSTOM_CONTEXT_MENU_GET_LIST_MIXIN = {
option.text = varName;

option.callback =
Blockly.Constants.Data.VARIABLE_OPTION_CALLBACK_FACTORY(this,
VARIABLE_OPTION_CALLBACK_FACTORY(this,
variablesList[i].getId(), fieldName);
options.push(option);
}
} else {
var renameOption = {
text: Blockly.Msg.RENAME_LIST,
enabled: true,
callback: Blockly.Constants.Data.RENAME_OPTION_CALLBACK_FACTORY(this,
callback: RENAME_OPTION_CALLBACK_FACTORY(this,
fieldName)
};
var deleteOption = {
text: Blockly.Msg.DELETE_LIST.replace('%1', currentVarName),
enabled: true,
callback: Blockly.Constants.Data.DELETE_OPTION_CALLBACK_FACTORY(this,
callback: DELETE_OPTION_CALLBACK_FACTORY(this,
fieldName)
};
options.push(renameOption);
Expand All @@ -610,7 +605,7 @@ Blockly.Constants.Data.CUSTOM_CONTEXT_MENU_GET_LIST_MIXIN = {
}
};
Blockly.Extensions.registerMixin('contextMenu_getListBlock',
Blockly.Constants.Data.CUSTOM_CONTEXT_MENU_GET_LIST_MIXIN);
CUSTOM_CONTEXT_MENU_GET_LIST_MIXIN);

/**
* Callback factory for dropdown menu options associated with a variable getter
Expand All @@ -622,7 +617,7 @@ Blockly.Extensions.registerMixin('contextMenu_getListBlock',
* @param {string} fieldName The name of the field to update on the block.
* @return {!function()} A function that updates the block with the new name.
*/
Blockly.Constants.Data.VARIABLE_OPTION_CALLBACK_FACTORY = function(block,
const VARIABLE_OPTION_CALLBACK_FACTORY = function(block,
id, fieldName) {
return function() {
var variableField = block.getField(fieldName);
Expand All @@ -640,7 +635,7 @@ Blockly.Constants.Data.VARIABLE_OPTION_CALLBACK_FACTORY = function(block,
* @param {string} fieldName The name of the field to inspect on the block.
* @return {!function()} A function that renames the variable.
*/
Blockly.Constants.Data.RENAME_OPTION_CALLBACK_FACTORY = function(block,
const RENAME_OPTION_CALLBACK_FACTORY = function(block,
fieldName) {
return function() {
var workspace = block.workspace;
Expand All @@ -656,7 +651,7 @@ Blockly.Constants.Data.RENAME_OPTION_CALLBACK_FACTORY = function(block,
* @param {string} fieldName The name of the field to inspect on the block.
* @return {!function()} A function that deletes the variable.
*/
Blockly.Constants.Data.DELETE_OPTION_CALLBACK_FACTORY = function(block,
const DELETE_OPTION_CALLBACK_FACTORY = function(block,
fieldName) {
return function() {
var workspace = block.workspace;
Expand Down
11 changes: 10 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,14 @@
* @const {string}
*/
const BROADCAST_MESSAGE_VARIABLE_TYPE = 'broadcast_msg';

export {BROADCAST_MESSAGE_VARIABLE_TYPE};


/**
* String representing the variable type of list blocks.
* This string, for use in differentiating between types of variables,
* indicates that the current variable is a list.
* @const {string}
*/
const LIST_VARIABLE_TYPE = 'list';
export {LIST_VARIABLE_TYPE};
Loading

0 comments on commit fafed65

Please sign in to comment.