Skip to content

Commit 30c5286

Browse files
authored
fix: callbackkey for flyoutbuttons not case sensitive (#742)
1 parent f4560fd commit 30c5286

File tree

2 files changed

+81
-7
lines changed

2 files changed

+81
-7
lines changed

src/actions/enter.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -332,14 +332,15 @@ export class EnterAction {
332332
// @ts-expect-error private field access
333333
workspace.flyoutButtonCallbacks;
334334

335-
const info = button.info;
336-
if ('callbackkey' in info) {
337-
const buttonCallback = flyoutButtonCallbacks.get(info.callbackkey);
338-
if (!buttonCallback) {
339-
throw new Error('No callback function found for flyout button.');
340-
}
341-
buttonCallback(button);
335+
// TODO: Remove cast once blockly 12.4.0 is the minimum version of Blockly required.
336+
// button.callbackKey is private until that version.
337+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
338+
const callbackKey = (button as any).callbackKey;
339+
const buttonCallback = flyoutButtonCallbacks.get(callbackKey);
340+
if (!buttonCallback) {
341+
throw new Error('No callback function found for flyout button.');
342342
}
343+
buttonCallback(button);
343344
}
344345

345346
/**

test/webdriverio/test/flyout_test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import * as chai from 'chai';
88
import * as Blockly from 'blockly';
9+
import * as webdriverio from 'webdriverio';
910
import {
1011
testSetup,
1112
testFileLocations,
@@ -18,6 +19,7 @@ import {
1819
keyRight,
1920
getCurrentFocusNodeId,
2021
getCurrentFocusedBlockId,
22+
tabNavigateToToolbox,
2123
} from './test_setup.js';
2224

2325
suite('Toolbox and flyout test', function () {
@@ -244,6 +246,77 @@ suite('Toolbox and flyout test', function () {
244246
chai.assert.strictEqual(focusedId, elemId);
245247
chai.assert.isTrue(flyoutIsOpen);
246248
});
249+
250+
suite('Flyout buttons', function () {
251+
setup(async function () {
252+
// New toolbox definition
253+
const toolbox = {
254+
'kind': 'categoryToolbox',
255+
'contents': [
256+
{
257+
'kind': 'category',
258+
'name': 'test category',
259+
'contents': [
260+
{
261+
'kind': 'button',
262+
'text': 'A button',
263+
// Note lowercase
264+
'callbackkey': 'buttonCallback',
265+
},
266+
{
267+
'kind': 'button',
268+
'text': 'Second button',
269+
// Note camelCase
270+
'callbackKey': 'buttonCallback',
271+
},
272+
],
273+
},
274+
],
275+
};
276+
277+
// Replace toolbox contents & register button callback
278+
await this.browser.execute((toolboxDef) => {
279+
const ws = Blockly.getMainWorkspace() as Blockly.WorkspaceSvg;
280+
ws.updateToolbox(toolboxDef);
281+
ws.registerButtonCallback('buttonCallback', () => {
282+
alert('Button pressed');
283+
});
284+
}, toolbox);
285+
});
286+
teardown(async function () {
287+
try {
288+
await this.browser.acceptAlert();
289+
} catch {
290+
// Don't do anything if the alert isn't present, the test already failed elsewhere.
291+
}
292+
});
293+
test('callbackkey is activated with enter', async function () {
294+
await tabNavigateToToolbox(this.browser);
295+
await this.browser.pause(PAUSE_TIME);
296+
297+
// First thing in the toolbox is the first button
298+
// Press Enter to activate it.
299+
await keyRight(this.browser);
300+
await sendKeyAndWait(this.browser, webdriverio.Key.Enter);
301+
302+
// This errors if there is no alert present
303+
await this.browser.getAlertText();
304+
});
305+
306+
test('callbackKey is activated with enter', async function () {
307+
await tabNavigateToToolbox(this.browser);
308+
await this.browser.pause(PAUSE_TIME);
309+
310+
// Navigate to second button.
311+
// Press Enter to activate it.
312+
await keyRight(this.browser);
313+
await keyDown(this.browser);
314+
await sendKeyAndWait(this.browser, webdriverio.Key.Enter);
315+
316+
// This errors if there is no alert present
317+
await this.browser.getAlertText();
318+
});
319+
});
247320
});
248321

249322
/**

0 commit comments

Comments
 (0)