mirror of
https://github.com/google/blockly.git
synced 2026-01-18 22:37:09 +01:00
Fixing JSON support for images in dropdowns. Adding tests. (#851)
Fixes #848.
This commit is contained in:
committed by
GitHub
parent
7b0275cd70
commit
0da5dda1f0
@@ -244,22 +244,26 @@ Blockly.FieldDropdown.prototype.trimOptions_ = function() {
|
||||
if (!goog.isArray(options)) {
|
||||
return;
|
||||
}
|
||||
// Replace message strings.
|
||||
var hasImages = false;
|
||||
|
||||
// Localize label text and image alt text.
|
||||
for (var i = 0; i < options.length; i++) {
|
||||
var rawText = options[i][0];
|
||||
var localizedText = Blockly.utils.replaceMessageReferences(rawText);
|
||||
options[i][0] = localizedText;
|
||||
var label = options[i][0];
|
||||
if (typeof label == 'string') {
|
||||
options[i][0] = Blockly.utils.replaceMessageReferences(label);
|
||||
} else {
|
||||
if (label.alt != null) {
|
||||
options[i][0].alt = Blockly.utils.replaceMessageReferences(label.alt);
|
||||
}
|
||||
hasImages = true;
|
||||
}
|
||||
}
|
||||
if (options.length < 2) {
|
||||
return; // Nothing to trim.
|
||||
if (hasImages || options.length < 2) {
|
||||
return; // Do nothing if too few items or at least one label is an image.
|
||||
}
|
||||
var strings = [];
|
||||
for (var i = 0; i < options.length; i++) {
|
||||
var text = options[i][0];
|
||||
if (typeof text != 'string') {
|
||||
return; // No text splitting if there is an image in the list.
|
||||
}
|
||||
strings.push(text);
|
||||
strings.push(options[i][0]);
|
||||
}
|
||||
var shortest = Blockly.utils.shortestStringLength(strings);
|
||||
var prefixLength = Blockly.utils.commonWordPrefix(strings, shortest);
|
||||
|
||||
@@ -163,6 +163,12 @@ function test_json_dropdown() {
|
||||
assertEquals(dropdown, block.getField(FIELD_NAME));
|
||||
assertEquals(Blockly.FieldDropdown, dropdown.constructor);
|
||||
assertEquals(VALUE0, dropdown.getValue());
|
||||
|
||||
var options = dropdown.getOptions_();
|
||||
assertEquals(LABEL0, options[0][0]);
|
||||
assertEquals(VALUE0, options[0][1]);
|
||||
assertEquals(LABEL1, options[1][0]);
|
||||
assertEquals(VALUE1, options[1][1]);
|
||||
} finally {
|
||||
block && block.dispose(); // Disposes of dropdown, too.
|
||||
workspace.dispose();
|
||||
@@ -170,3 +176,85 @@ function test_json_dropdown() {
|
||||
}
|
||||
}
|
||||
|
||||
function test_json_dropdown_image() {
|
||||
var BLOCK_TYPE = 'test_json_dropdown';
|
||||
var FIELD_NAME = 'FIELD_NAME';
|
||||
var IMAGE1_ALT_TEXT = 'Localized message.';
|
||||
Blockly.Msg['ALT_TEXT'] = IMAGE1_ALT_TEXT;
|
||||
var IMAGE0 = {
|
||||
'width': 12,
|
||||
'height': 34,
|
||||
'src': 'http://image0.src',
|
||||
'alt': 'IMAGE0 alt text'
|
||||
};
|
||||
var VALUE0 = 'VALUE0';
|
||||
var IMAGE1 = {
|
||||
'width': 56,
|
||||
'height': 78,
|
||||
'src': 'http://image1.src',
|
||||
'alt': '%{BKY_ALT_TEXT}'
|
||||
};
|
||||
var VALUE1 = 'VALUE1';
|
||||
var IMAGE2 = {
|
||||
'width': 90,
|
||||
'height': 123,
|
||||
'src': 'http://image2.src'
|
||||
};
|
||||
var VALUE2 = 'VALUE2';
|
||||
|
||||
var workspace = new Blockly.Workspace();
|
||||
var block;
|
||||
try {
|
||||
Blockly.defineBlocksWithJsonArray([{
|
||||
"type": BLOCK_TYPE,
|
||||
"message0": "%1",
|
||||
"args0": [
|
||||
{
|
||||
"type": "field_dropdown",
|
||||
"name": FIELD_NAME,
|
||||
"options": [
|
||||
[IMAGE0, VALUE0],
|
||||
[IMAGE1, VALUE1],
|
||||
[IMAGE2, VALUE2]
|
||||
]
|
||||
}
|
||||
]
|
||||
}]);
|
||||
|
||||
block = new Blockly.Block(workspace, BLOCK_TYPE);
|
||||
assertEquals(1, block.inputList.length);
|
||||
assertEquals(1, block.inputList[0].fieldRow.length);
|
||||
var dropdown = block.inputList[0].fieldRow[0];
|
||||
assertEquals(dropdown, block.getField(FIELD_NAME));
|
||||
assertEquals(Blockly.FieldDropdown, dropdown.constructor);
|
||||
assertEquals(VALUE0, dropdown.getValue());
|
||||
|
||||
var options = dropdown.getOptions_();
|
||||
var image0 = options[0][0];
|
||||
assertEquals(IMAGE0.width, image0.width);
|
||||
assertEquals(IMAGE0.height, image0.height);
|
||||
assertEquals(IMAGE0.src, image0.src);
|
||||
assertEquals(IMAGE0.alt, image0.alt);
|
||||
assertEquals(VALUE0, options[0][1]);
|
||||
|
||||
var image1 = options[1][0];
|
||||
assertEquals(IMAGE1.width, image1.width);
|
||||
assertEquals(IMAGE1.height, image1.height);
|
||||
assertEquals(IMAGE1.src, image1.src);
|
||||
assertEquals(IMAGE1.alt, IMAGE1_ALT_TEXT); // Via Msg reference
|
||||
assertEquals(VALUE1, options[1][1]);
|
||||
|
||||
var image2 = options[2][0];
|
||||
assertEquals(IMAGE2.width, image2.width);
|
||||
assertEquals(IMAGE2.height, image2.height);
|
||||
assertEquals(IMAGE2.src, image2.src);
|
||||
assert(image2.alt == null); // No alt specified.
|
||||
assertEquals(VALUE2, options[2][1]);
|
||||
} finally {
|
||||
block && block.dispose(); // Disposes of dropdown, too.
|
||||
workspace.dispose();
|
||||
delete Blockly.Blocks[BLOCK_TYPE];
|
||||
delete Blockly.Msg['ALTTEXT'];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user