Skip to content

Commit 7da8602

Browse files
committed
fix(FieldGridDropdown): Handle presence of HTMLElement in MenuOptions
1 parent 1f83540 commit 7da8602

1 file changed

Lines changed: 33 additions & 5 deletions

File tree

  • plugins/field-grid-dropdown/src

plugins/field-grid-dropdown/src/grid.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import {utils, browserEvents, MenuOption, FieldDropdown} from 'blockly/core';
7+
import {
8+
browserEvents,
9+
FieldDropdown,
10+
ImageProperties,
11+
MenuOption,
12+
utils,
13+
} from 'blockly/core';
814
import {GridItem} from './grid_item';
915

1016
/**
@@ -100,11 +106,11 @@ export class Grid {
100106

101107
const [label, value] = item;
102108
const content = (() => {
103-
if (typeof label === 'object') {
109+
if (isImageProperties(label)) {
104110
// Convert ImageProperties to an HTMLImageElement.
105-
const image = new Image(label['width'], label['height']);
106-
image.src = label['src'];
107-
image.alt = label['alt'] || '';
111+
const image = new Image(label.width, label.height);
112+
image.src = label.src;
113+
image.alt = label.alt || '';
108114
return image;
109115
}
110116
return label;
@@ -293,3 +299,25 @@ export class Grid {
293299
return this.itemAtIndex(index);
294300
}
295301
}
302+
303+
/**
304+
* Returns whether or not an object conforms to the ImageProperties
305+
* interface.
306+
*
307+
* @param obj The object to test.
308+
* @returns True iff the object conforms to ImageProperties.
309+
*/
310+
function isImageProperties(obj: any): obj is ImageProperties {
311+
return (
312+
obj &&
313+
typeof obj === 'object' &&
314+
'src' in obj &&
315+
typeof obj.src === 'string' &&
316+
'alt' in obj &&
317+
typeof obj.alt === 'string' &&
318+
'width' in obj &&
319+
typeof obj.width === 'number' &&
320+
'height' in obj &&
321+
typeof obj.height === 'number'
322+
);
323+
}

0 commit comments

Comments
 (0)