|
4 | 4 | * SPDX-License-Identifier: Apache-2.0 |
5 | 5 | */ |
6 | 6 |
|
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'; |
8 | 14 | import {GridItem} from './grid_item'; |
9 | 15 |
|
10 | 16 | /** |
@@ -100,11 +106,11 @@ export class Grid { |
100 | 106 |
|
101 | 107 | const [label, value] = item; |
102 | 108 | const content = (() => { |
103 | | - if (typeof label === 'object') { |
| 109 | + if (isImageProperties(label)) { |
104 | 110 | // 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 || ''; |
108 | 114 | return image; |
109 | 115 | } |
110 | 116 | return label; |
@@ -293,3 +299,25 @@ export class Grid { |
293 | 299 | return this.itemAtIndex(index); |
294 | 300 | } |
295 | 301 | } |
| 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