Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/html_toast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import * as Blockly from 'blockly/core';

/**
* Configuration options for toasts.
*/
interface HtmlToastOptions extends Blockly.ToastOptions {
element?: HTMLElement;
}

/**
* Custom toast implementation that supports HTML elements in toast messages.
*
* After registering, call
`Blockly.dialog.toast(workspace, {element: <html element>, message: <text>});`
* to display an HTML-based toast.
*/
class HtmlToast extends Blockly.Toast {
/**
* Creates the body of the toast for display.
*
* @param workspace The workspace the toast will be displayed on.
* @param options Configuration options for toast appearance/behavior.
* @returns The body for the toast.
*/
protected static override createDom(
workspace: Blockly.WorkspaceSvg,
options: HtmlToastOptions,
) {
const dom = super.createDom(workspace, options);
const contents = dom.querySelector('div');
if (
contents &&
'element' in options &&
options.element instanceof HTMLElement
) {
contents.innerHTML = '';
contents.appendChild(options.element);
}
return dom;
}
}

/**
* Registers HtmlToast as the default toast implementation for Blockly.
*/
export function registerHtmlToast() {
Blockly.dialog.setToast(HtmlToast.show.bind(HtmlToast));
}
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import * as Blockly from 'blockly/core';
import {NavigationController} from './navigation_controller';
import {enableBlocksOnDrag} from './disabled_blocks';
import {registerHtmlToast} from './html_toast';

/** Plugin for keyboard navigation. */
export class KeyboardNavigation {
Expand Down Expand Up @@ -82,6 +83,8 @@ export class KeyboardNavigation {
});
workspace.getSvgGroup().appendChild(this.workspaceFocusRing);
this.resizeWorkspaceRings();

registerHtmlToast();
}

private resizeWorkspaceRings() {
Expand Down
38 changes: 38 additions & 0 deletions test/webdriverio/test/toast_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import * as chai from 'chai';
import * as Blockly from 'blockly/core';
import {PAUSE_TIME, testFileLocations, testSetup} from './test_setup.js';

suite('HTML toasts', function () {
setup(async function () {
this.browser = await testSetup(testFileLocations.BASE);
await this.browser.pause(PAUSE_TIME);
});

test('Can be displayed', async function () {
const equal = await this.browser.execute(() => {
const element = document.createElement('div');
element.id = 'testToast';
element.innerHTML = 'This is a <b>test</b>';

const options = {
element,
message: 'Placeholder',
};
Blockly.dialog.toast(
Blockly.getMainWorkspace() as Blockly.WorkspaceSvg,
options,
);

// Ensure that the element displayed in the toast is the one we specified.
return document.querySelector('.blocklyToast #testToast') === element;
});

chai.assert.isTrue(equal);
});
});
Loading