Skip to content

Commit e6652d0

Browse files
committed
feat: Allow passing HTML elements to display in toasts.
1 parent 87af338 commit e6652d0

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/html_toast.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @license
3+
* Copyright 2025 Google LLC
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
import * as Blockly from 'blockly/core';
8+
9+
/**
10+
* Configuration options for toasts.
11+
*/
12+
interface HtmlToastOptions extends Blockly.ToastOptions {
13+
element?: HTMLElement;
14+
}
15+
16+
/**
17+
* Custom toast implementation that supports HTML elements in toast messages.
18+
*/
19+
class HtmlToast extends Blockly.Toast {
20+
/**
21+
* Creates the body of the toast for display.
22+
*
23+
* @param workspace The workspace the toast will be displayed on.
24+
* @param options Configuration options for toast appearance/behavior.
25+
* @returns The body for the toast.
26+
*/
27+
protected static override createDom(
28+
workspace: Blockly.WorkspaceSvg,
29+
options: Blockly.ToastOptions,
30+
) {
31+
const dom = super.createDom(workspace, options);
32+
const contents = dom.querySelector('div');
33+
if (
34+
contents &&
35+
'element' in options &&
36+
options.element instanceof HTMLElement
37+
) {
38+
contents.innerHTML = '';
39+
contents.appendChild(options.element);
40+
}
41+
return dom;
42+
}
43+
}
44+
45+
/**
46+
* Registers HtmlToast as the default toast implementation for Blockly. */
47+
export function registerHtmlToast() {
48+
Blockly.dialog.setToast(HtmlToast.show.bind(HtmlToast));
49+
}

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import * as Blockly from 'blockly/core';
88
import {NavigationController} from './navigation_controller';
99
import {enableBlocksOnDrag} from './disabled_blocks';
10+
import {registerHtmlToast} from './html_toast';
1011

1112
/** Plugin for keyboard navigation. */
1213
export class KeyboardNavigation {
@@ -82,6 +83,8 @@ export class KeyboardNavigation {
8283
});
8384
workspace.getSvgGroup().appendChild(this.workspaceFocusRing);
8485
this.resizeWorkspaceRings();
86+
87+
registerHtmlToast();
8588
}
8689

8790
private resizeWorkspaceRings() {

0 commit comments

Comments
 (0)