Skip to content

Commit c9f8795

Browse files
committed
Reimplement and use Text Dropdowns (scratch-blocks)
1 parent 3589ef8 commit c9f8795

File tree

4 files changed

+93
-18
lines changed

4 files changed

+93
-18
lines changed

blocks_vertical/data.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,35 @@ Blockly.Blocks['data_listindexrandom'] = {
226226
}
227227
};
228228

229+
Blockly.Blocks['data_listindex'] = {
230+
/**
231+
* List index menu, with all secret options.
232+
* @this Blockly.Block
233+
*/
234+
init: function() {
235+
this.jsonInit({
236+
"message0": "%1",
237+
"args0": [
238+
{
239+
"type": "field_numberdropdown",
240+
"name": "INDEX",
241+
"value": "1",
242+
"min": 1,
243+
"precision": 1,
244+
"options": [
245+
["1", "1"],
246+
[Blockly.Msg.DATA_INDEX_LAST, "last"],
247+
[Blockly.Msg.DATA_INDEX_ALL, "all"],
248+
[Blockly.Msg.DATA_INDEX_RANDOM, "random"]
249+
]
250+
}
251+
],
252+
"category": Blockly.Categories.data,
253+
"extensions": ["colours_textfield", "output_string"]
254+
});
255+
}
256+
};
257+
229258
Blockly.Blocks['data_addtolist'] = {
230259
/**
231260
* Block to add item to list.

core/data_category.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ Blockly.DataCategory = function(workspace) {
7676
Blockly.DataCategory.addAddToList(xmlList, firstVariable);
7777
Blockly.DataCategory.addSep(xmlList);
7878
Blockly.DataCategory.addDeleteOfList(xmlList, firstVariable);
79-
Blockly.DataCategory.addDeleteAllOfList(xmlList, firstVariable);
79+
// Blockly.DataCategory.addDeleteAllOfList(xmlList, firstVariable);
8080
Blockly.DataCategory.addInsertAtList(xmlList, firstVariable);
8181
Blockly.DataCategory.addReplaceItemOfList(xmlList, firstVariable);
8282
Blockly.DataCategory.addSep(xmlList);
@@ -249,27 +249,30 @@ Blockly.DataCategory.addDeleteOfList = function(xmlList, variable) {
249249
// <block type="data_deleteoflist">
250250
// <field name="LIST" variabletype="list" id="">variablename</field>
251251
// <value name="INDEX">
252-
// <shadow type="math_integer">
252+
// <shadow type="data_listindex">
253253
// <field name="NUM">1</field>
254254
// </shadow>
255255
// </value>
256256
// </block>
257257
Blockly.DataCategory.addBlock(xmlList, variable, 'data_deleteoflist', 'LIST',
258-
[['INDEX', 'math_integer', 1]]);
258+
[['INDEX', 'data_listindex', 1]]);
259259
};
260260

261261
/**
262262
* Construct and add a data_deleteoflist block to xmlList.
263+
* Removed from NitroBolt
263264
* @param {!Array.<!Element>} xmlList Array of XML block elements.
264265
* @param {?Blockly.VariableModel} variable Variable to select in the field.
265266
*/
267+
/*
266268
Blockly.DataCategory.addDeleteAllOfList = function(xmlList, variable) {
267269
// <block type="data_deletealloflist">
268270
// <field name="LIST" variabletype="list" id="">variablename</field>
269271
// </block>
270272
Blockly.DataCategory.addBlock(xmlList, variable, 'data_deletealloflist',
271273
'LIST');
272274
};
275+
*/
273276

274277
/**
275278
* Construct and add a data_insertatlist block to xmlList.
@@ -280,7 +283,7 @@ Blockly.DataCategory.addInsertAtList = function(xmlList, variable) {
280283
// <block type="data_insertatlist">
281284
// <field name="LIST" variabletype="list" id="">variablename</field>
282285
// <value name="INDEX">
283-
// <shadow type="math_integer">
286+
// <shadow type="data_listindexrandom">
284287
// <field name="NUM">1</field>
285288
// </shadow>
286289
// </value>
@@ -291,7 +294,7 @@ Blockly.DataCategory.addInsertAtList = function(xmlList, variable) {
291294
// </value>
292295
// </block>
293296
Blockly.DataCategory.addBlock(xmlList, variable, 'data_insertatlist', 'LIST',
294-
[['INDEX', 'math_integer', 1], ['ITEM', 'text', Blockly.Msg.DEFAULT_LIST_ITEM]]);
297+
[['INDEX', 'data_listindexrandom', 1], ['ITEM', 'text', Blockly.Msg.DEFAULT_LIST_ITEM]]);
295298
};
296299

297300
/**
@@ -303,7 +306,7 @@ Blockly.DataCategory.addReplaceItemOfList = function(xmlList, variable) {
303306
// <block type="data_replaceitemoflist">
304307
// <field name="LIST" variabletype="list" id="">variablename</field>
305308
// <value name="INDEX">
306-
// <shadow type="math_integer">
309+
// <shadow type="data_listindexrandom">
307310
// <field name="NUM">1</field>
308311
// </shadow>
309312
// </value>
@@ -314,7 +317,7 @@ Blockly.DataCategory.addReplaceItemOfList = function(xmlList, variable) {
314317
// </value>
315318
// </block>
316319
Blockly.DataCategory.addBlock(xmlList, variable, 'data_replaceitemoflist',
317-
'LIST', [['INDEX', 'math_integer', 1], ['ITEM', 'text', Blockly.Msg.DEFAULT_LIST_ITEM]]);
320+
'LIST', [['INDEX', 'data_listindexrandom', 1], ['ITEM', 'text', Blockly.Msg.DEFAULT_LIST_ITEM]]);
318321
};
319322

320323
/**
@@ -326,13 +329,13 @@ Blockly.DataCategory.addItemOfList = function(xmlList, variable) {
326329
// <block type="data_itemoflist">
327330
// <field name="LIST" variabletype="list" id="">variablename</field>
328331
// <value name="INDEX">
329-
// <shadow type="math_integer">
332+
// <shadow type="data_listindexrandom">
330333
// <field name="NUM">1</field>
331334
// </shadow>
332335
// </value>
333336
// </block>
334337
Blockly.DataCategory.addBlock(xmlList, variable, 'data_itemoflist', 'LIST',
335-
[['INDEX', 'math_integer', 1]]);
338+
[['INDEX', 'data_listindexrandom', 1]]);
336339
};
337340

338341
/** Construct and add a data_itemnumoflist block to xmlList.
@@ -510,13 +513,13 @@ Blockly.DataCategory.addDeleteDimensionInTable = function(xmlList, variable) {
510513
// <block type="data_deletedimensionintable">
511514
// <field name="TABLE" variabletype="table" id="">variablename</field>
512515
// <value name="INDEX">
513-
// <shadow type="math_integer">
516+
// <shadow type="data_listindexrandom">
514517
// <field name="NUM">1</field>
515518
// </shadow>
516519
// </value>
517520
// </block>
518521
Blockly.DataCategory.addBlock(xmlList, variable, 'data_deletedimensionintable', 'TABLE',
519-
[['INDEX', 'math_integer', 1]]);
522+
[['INDEX', 'data_listindexrandom', 1]]);
520523
};
521524

522525
/**
@@ -564,13 +567,13 @@ Blockly.DataCategory.addItemsOfDimensionOfTable = function(xmlList, variable) {
564567
// <block type="data_itemsofdimensionoftable">
565568
// <field name="TABLE" variabletype="table" id="">variablename</field>
566569
// <value name="INDEX">
567-
// <shadow type="math_integer">
570+
// <shadow type="data_listindexrandom">
568571
// <field name="NUM">1</field>
569572
// </shadow>
570573
// </value>
571574
// </block>
572575
Blockly.DataCategory.addBlock(xmlList, variable, 'data_itemsofdimensionoftable', 'TABLE',
573-
[['INDEX', 'math_integer', 1]]);
576+
[['INDEX', 'data_listindexrandom', 1]]);
574577
};
575578

576579
/**
@@ -582,13 +585,13 @@ Blockly.DataCategory.addLengthOfDimensionOfTable = function(xmlList, variable) {
582585
// <block type="data_lengthofdimensionoftable">
583586
// <field name="TABLE" variabletype="table" id="">variablename</field>
584587
// <value name="INDEX">
585-
// <shadow type="math_integer">
588+
// <shadow type="data_listindexrandom">
586589
// <field name="NUM">1</field>
587590
// </shadow>
588591
// </value>
589592
// </block>
590593
Blockly.DataCategory.addBlock(xmlList, variable, 'data_lengthofdimensionoftable', 'TABLE',
591-
[['INDEX', 'math_integer', 1]]);
594+
[['INDEX', 'data_listindexrandom', 1]]);
592595
};
593596

594597
/**

core/field_textdropdown.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ Blockly.FieldTextDropdown.prototype.init = function() {
9696
'width': this.arrowSize_ + 'px'
9797
});
9898
this.arrow_.setAttributeNS('http://www.w3.org/1999/xlink',
99-
'xlink:href', Blockly.mainWorkspace.options.pathToMedia + 'dropdown-arrow-dark.svg');
99+
'xlink:href', Blockly.mainWorkspace.options.pathToMedia +
100+
Blockly.FieldTextInput.getTextMenuArrowAsset());
100101
this.arrow_.style.cursor = 'pointer';
101102
this.fieldGroup_.appendChild(this.arrow_);
102103
this.mouseUpWrapper_ =
@@ -140,7 +141,32 @@ Blockly.FieldTextDropdown.prototype.showEditor_ = function() {
140141
* (human-readable text, language-neutral name).
141142
* @private
142143
*/
143-
Blockly.FieldTextDropdown.prototype.getOptions_ = Blockly.FieldDropdown.prototype.getOptions_;
144+
Blockly.FieldTextDropdown.prototype.getOptions = Blockly.FieldDropdown.prototype.getOptions;
145+
146+
/**
147+
* Handle the selection of an item in the dropdown menu.
148+
* @param {!goog.ui.Menu} menu The Menu component clicked.
149+
* @param {!goog.ui.MenuItem} menuItem The MenuItem selected within menu.
150+
*/
151+
Blockly.FieldTextDropdown.prototype.onItemSelected = Blockly.FieldDropdown.prototype.onItemSelected;
152+
153+
/**
154+
* Get the language-neutral value from this dropdown menu.
155+
* @return {string} Current text.
156+
*/
157+
Blockly.FieldTextDropdown.prototype.getValue = Blockly.FieldDropdown.prototype.getValue;
158+
159+
/**
160+
* Set the language-neutral value for this dropdown menu.
161+
* @param {string} newValue New value to set.
162+
*/
163+
Blockly.FieldTextDropdown.prototype.setValue = Blockly.FieldDropdown.prototype.setValue;
164+
165+
/**
166+
* @return {boolean} True if the option list is generated by a function.
167+
* Otherwise false.
168+
*/
169+
Blockly.FieldTextDropdown.prototype.isOptionListDynamic = Blockly.FieldDropdown.prototype.isOptionListDynamic;
144170

145171
/**
146172
* Position a drop-down arrow at the appropriate location at render-time.

core/field_textinput.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,22 @@ Blockly.FieldTextInput.TEXT_MEASURE_PADDING_MAGIC = 45;
9696
*/
9797
Blockly.FieldTextInput.htmlInput_ = null;
9898

99+
/**
100+
* Select caret asset for text menus based on native theme mode.
101+
* In default mode, keep the original text-dropdown caret.
102+
* In non-default modes, use the regular dropdown caret.
103+
*
104+
* This is a bit hacky, but it works
105+
* @returns {string} Relative media file name.
106+
* @package
107+
*/
108+
Blockly.FieldTextInput.getTextMenuArrowAsset = function() {
109+
var textField = String(Blockly.Colours.textField || '').trim().toLowerCase();
110+
var textFieldText = String(Blockly.Colours.textFieldText || '').trim().toLowerCase();
111+
var defaultTheme = textField === '#ffffff' && textFieldText === '#575e75';
112+
return defaultTheme ? 'dropdown-arrow-dark.svg' : 'dropdown-arrow.svg';
113+
};
114+
99115
/**
100116
* Mouse cursor style when over the hotspot that initiates the editor.
101117
*/
@@ -248,7 +264,8 @@ Blockly.FieldTextInput.prototype.showEditor_ = function(
248264
var dropDownArrow =
249265
goog.dom.createDom(goog.dom.TagName.IMG, 'blocklyTextDropDownArrow');
250266
dropDownArrow.setAttribute('src',
251-
Blockly.mainWorkspace.options.pathToMedia + 'dropdown-arrow-dark.svg');
267+
Blockly.mainWorkspace.options.pathToMedia +
268+
Blockly.FieldTextInput.getTextMenuArrowAsset());
252269
dropDownArrow.style.width = this.arrowSize_ + 'px';
253270
dropDownArrow.style.height = this.arrowSize_ + 'px';
254271
dropDownArrow.style.top = this.arrowY_ + 'px';

0 commit comments

Comments
 (0)