mirror of
https://github.com/google/blockly.git
synced 2026-01-10 10:27:08 +01:00
## The basics - [x] I [validated my changes](https://developers.google.com/blockly/guides/contribute/core#making_and_verifying_a_change) ## The details ### Resolves Fixes https://github.com/google/blockly-keyboard-experimentation/issues/563 ### Proposed Changes This expands the functionality introduced in #9213 to also include widget divs. ### Reason for Changes MakeCode makes use of widget div in several field editors, so the issues described in https://github.com/google/blockly-keyboard-experimentation/issues/563 aren't fully mitigated with #9213 alone. This PR essentially adds the same support for auto-closing as drop-down divs now have, and enables this functionality by default. Note the drop-down div change: it was missed in #9123 that the API change for drop-down div's `show` function is actually API-breaking, so this updates that API to be properly backward compatible (and reverts one test change that makes use of it). The `FocusManager` change actually corrects an implementation issue from #9123: not updating the tracked focus status before calling the callback can result in focus being inadvertently restored if the callback triggers returning focus due to a lost focus situation. This was wrong for drop-down divs, too, but it's harder to notice there because the dismissal of the drop-down div happens on a timer (which means there's sufficient time for `FocusManager`'s state to correct prior to attempting to return from the ephemeral focus state). Demonstration of fixed behavior (since the inline number editor uses a widget div): [Screen recording 2025-07-08 2.12.31 PM.webm](https://github.com/user-attachments/assets/7c3c7c3c-224c-48f4-b4af-bde86feecfa8) ### Test Coverage New widget div tests have been added to verify the new parameter and auto-close functionality. The `FocusManager` test was updated to account for the new, and correct, behavior around the internal tracked ephemeral focus state. Note that some `tabindex` state has been clarified and cleaned up in the test index page and `FocusManager`. It's fine (and preferable) for ephemeral-used elements to always be focusable rather than making them dynamically so (which avoids state bleed across test runs which was happening one of the new tests). https://github.com/google/blockly-keyboard-experimentation/pull/649 includes additional tests for validating widget behaviors. ### Documentation No new documentation should be needed here--the API documentation changes should be sufficient. One documentation update was made in `dropdowndiv.ts` that corrects the documentation parameter ordering. ### Additional Information Nothing further to add.
515 lines
17 KiB
JavaScript
515 lines
17 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2020 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
import {assert} from '../../node_modules/chai/chai.js';
|
|
import {
|
|
sharedTestSetup,
|
|
sharedTestTeardown,
|
|
} from './test_helpers/setup_teardown.js';
|
|
|
|
suite('WidgetDiv', function () {
|
|
setup(function () {
|
|
sharedTestSetup.call(this);
|
|
this.workspace = Blockly.inject('blocklyDiv');
|
|
this.setUpBlockWithField = function () {
|
|
const blockJson = {
|
|
'type': 'text',
|
|
'id': 'block_id',
|
|
'x': 10,
|
|
'y': 20,
|
|
'fields': {
|
|
'TEXT': '',
|
|
},
|
|
};
|
|
Blockly.serialization.blocks.append(blockJson, this.workspace);
|
|
return this.workspace.getBlockById('block_id');
|
|
};
|
|
// The workspace needs to be visible for focus-specific tests.
|
|
document.getElementById('blocklyDiv').style.visibility = 'visible';
|
|
});
|
|
teardown(function () {
|
|
sharedTestTeardown.call(this);
|
|
document.getElementById('blocklyDiv').style.visibility = 'hidden';
|
|
});
|
|
|
|
suite('positionWithAnchor', function () {
|
|
function makeBBox(left, top, width, height) {
|
|
return {
|
|
left: left,
|
|
right: left + width,
|
|
top: top,
|
|
bottom: top + height,
|
|
width: width,
|
|
height: height,
|
|
};
|
|
}
|
|
|
|
setup(function () {
|
|
Blockly.WidgetDiv.createDom();
|
|
this.viewportBBox = makeBBox(0, 0, 1000, 1003);
|
|
this.widgetSize = {
|
|
width: 100,
|
|
height: 102,
|
|
};
|
|
this.anchorSize = {
|
|
width: 90,
|
|
height: 91,
|
|
};
|
|
|
|
this.testWidgetPosition = function (
|
|
anchorBBox,
|
|
rtl,
|
|
expectedX,
|
|
expectedY,
|
|
expectedHeight,
|
|
) {
|
|
Blockly.WidgetDiv.positionWithAnchor(
|
|
this.viewportBBox,
|
|
anchorBBox,
|
|
this.widgetSize,
|
|
rtl,
|
|
);
|
|
const style = Blockly.WidgetDiv.getDiv().style;
|
|
assert.equal(style.left, expectedX + 'px', 'Left');
|
|
assert.equal(style.top, expectedY + 'px', 'Top');
|
|
assert.equal(style.height, expectedHeight + 'px', 'Height');
|
|
};
|
|
});
|
|
|
|
suite('LTR', function () {
|
|
test('noConflict', function () {
|
|
// Anchor placed in the middle.
|
|
const anchorBBox = makeBBox(
|
|
500,
|
|
500,
|
|
this.anchorSize.width,
|
|
this.anchorSize.height,
|
|
);
|
|
// The widget div should be placed just below at the left side of the
|
|
// anchor.
|
|
const expectedX = anchorBBox.left;
|
|
const expectedY = anchorBBox.top + this.anchorSize.height;
|
|
this.testWidgetPosition(
|
|
anchorBBox,
|
|
false,
|
|
expectedX,
|
|
expectedY,
|
|
this.widgetSize.height,
|
|
);
|
|
});
|
|
|
|
test('topConflict', function () {
|
|
// Anchor close to the top.
|
|
const anchorBBox = makeBBox(
|
|
500,
|
|
50,
|
|
this.anchorSize.width,
|
|
this.anchorSize.height,
|
|
);
|
|
// The widget div should be placed just below the anchor.
|
|
const expectedX = anchorBBox.left;
|
|
const expectedY = anchorBBox.top + this.anchorSize.height;
|
|
this.testWidgetPosition(
|
|
anchorBBox,
|
|
false,
|
|
expectedX,
|
|
expectedY,
|
|
this.widgetSize.height,
|
|
);
|
|
});
|
|
|
|
test('bottomConflict', function () {
|
|
// Anchor placed close to the bottom.
|
|
const anchorBBox = makeBBox(
|
|
500,
|
|
900,
|
|
this.anchorSize.width,
|
|
this.anchorSize.height,
|
|
);
|
|
// The widget div should be placed just above the anchor.
|
|
const expectedX = anchorBBox.left;
|
|
const expectedY = anchorBBox.top - this.widgetSize.height;
|
|
this.testWidgetPosition(
|
|
anchorBBox,
|
|
false,
|
|
expectedX,
|
|
expectedY,
|
|
this.widgetSize.height,
|
|
);
|
|
});
|
|
|
|
test('leftConflict', function () {
|
|
// Anchor placed close to the left side.
|
|
const anchorBBox = makeBBox(
|
|
50,
|
|
500,
|
|
this.anchorSize.width,
|
|
this.anchorSize.height,
|
|
);
|
|
// The widget div should be placed at the anchor.
|
|
const expectedX = anchorBBox.left;
|
|
const expectedY = anchorBBox.top + this.anchorSize.height;
|
|
this.testWidgetPosition(
|
|
anchorBBox,
|
|
false,
|
|
expectedX,
|
|
expectedY,
|
|
this.widgetSize.height,
|
|
);
|
|
});
|
|
|
|
test('rightConflict', function () {
|
|
// Anchor placed close to the right side.
|
|
const anchorBBox = makeBBox(
|
|
950,
|
|
500,
|
|
this.anchorSize.width,
|
|
this.anchorSize.height,
|
|
);
|
|
// The widget div should be placed as far right as possible--at the edge of
|
|
// the screen.
|
|
const expectedX = this.viewportBBox.width - this.widgetSize.width;
|
|
const expectedY = anchorBBox.top + this.anchorSize.height;
|
|
this.testWidgetPosition(
|
|
anchorBBox,
|
|
false,
|
|
expectedX,
|
|
expectedY,
|
|
this.widgetSize.height,
|
|
);
|
|
});
|
|
});
|
|
suite('RTL', function () {
|
|
test('noConflict', function () {
|
|
// Anchor placed in the middle
|
|
const anchorBBox = makeBBox(
|
|
500,
|
|
500,
|
|
this.anchorSize.width,
|
|
this.anchorSize.height,
|
|
);
|
|
// The widget div should be placed at the right side of the anchor.
|
|
const expectedX = anchorBBox.right - this.widgetSize.width;
|
|
const expectedY = anchorBBox.top + this.anchorSize.height;
|
|
this.testWidgetPosition(
|
|
anchorBBox,
|
|
true,
|
|
expectedX,
|
|
expectedY,
|
|
this.widgetSize.height,
|
|
);
|
|
});
|
|
|
|
test('topConflict', function () {
|
|
// Anchor close to the top.
|
|
const anchorBBox = makeBBox(
|
|
500,
|
|
50,
|
|
this.anchorSize.width,
|
|
this.anchorSize.height,
|
|
);
|
|
// The widget div should be placed just below the anchor.
|
|
const expectedX = anchorBBox.right - this.widgetSize.width;
|
|
const expectedY = anchorBBox.top + this.anchorSize.height;
|
|
this.testWidgetPosition(
|
|
anchorBBox,
|
|
true,
|
|
expectedX,
|
|
expectedY,
|
|
this.widgetSize.height,
|
|
);
|
|
});
|
|
|
|
test('bottomConflict', function () {
|
|
// Anchor placed close to the bottom.
|
|
const anchorBBox = makeBBox(
|
|
500,
|
|
900,
|
|
this.anchorSize.width,
|
|
this.anchorSize.height,
|
|
);
|
|
// The widget div should be placed just above the anchor.
|
|
const expectedX = anchorBBox.right - this.widgetSize.width;
|
|
const expectedY = anchorBBox.top - this.widgetSize.height;
|
|
this.testWidgetPosition(
|
|
anchorBBox,
|
|
true,
|
|
expectedX,
|
|
expectedY,
|
|
this.widgetSize.height,
|
|
);
|
|
});
|
|
|
|
test('leftConflict', function () {
|
|
// Anchor placed close to the left side.
|
|
const anchorBBox = makeBBox(
|
|
10,
|
|
500,
|
|
this.anchorSize.width,
|
|
this.anchorSize.height,
|
|
);
|
|
// The widget div should be placed as far left as possible--at the edge of
|
|
// the screen.
|
|
const expectedX = 0;
|
|
const expectedY = anchorBBox.top + this.anchorSize.height;
|
|
this.testWidgetPosition(
|
|
anchorBBox,
|
|
true,
|
|
expectedX,
|
|
expectedY,
|
|
this.widgetSize.height,
|
|
);
|
|
});
|
|
|
|
test('rightConflict', function () {
|
|
// Anchor placed close to the right side.
|
|
const anchorBBox = makeBBox(
|
|
950,
|
|
500,
|
|
this.anchorSize.width,
|
|
this.anchorSize.height,
|
|
);
|
|
// The widget div should be placed as far right as possible--at the edge of
|
|
// the screen.
|
|
const expectedX = this.viewportBBox.width - this.widgetSize.width;
|
|
const expectedY = anchorBBox.top + this.anchorSize.height;
|
|
this.testWidgetPosition(
|
|
anchorBBox,
|
|
true,
|
|
expectedX,
|
|
expectedY,
|
|
this.widgetSize.height,
|
|
);
|
|
});
|
|
});
|
|
});
|
|
|
|
suite('Keyboard Shortcuts', function () {
|
|
test('Escape dismisses WidgetDiv', function () {
|
|
let hidden = false;
|
|
Blockly.WidgetDiv.show(
|
|
this,
|
|
false,
|
|
() => {
|
|
hidden = true;
|
|
},
|
|
this.workspace,
|
|
false,
|
|
);
|
|
assert.isFalse(hidden);
|
|
Blockly.WidgetDiv.getDiv().dispatchEvent(
|
|
new KeyboardEvent('keydown', {
|
|
key: 'Escape',
|
|
keyCode: 27, // example values.
|
|
}),
|
|
);
|
|
assert.isTrue(hidden);
|
|
});
|
|
});
|
|
|
|
suite('show()', function () {
|
|
test('shows nowhere', function () {
|
|
const block = this.setUpBlockWithField();
|
|
const field = Array.from(block.getFields())[0];
|
|
|
|
Blockly.WidgetDiv.show(field, false, () => {});
|
|
|
|
// By default the div will not have a position.
|
|
const widgetDivElem = document.querySelector('.blocklyWidgetDiv');
|
|
assert.strictEqual(widgetDivElem.style.display, 'block');
|
|
assert.strictEqual(widgetDivElem.style.left, '');
|
|
assert.strictEqual(widgetDivElem.style.top, '');
|
|
});
|
|
|
|
test('with hide callback does not call callback', function () {
|
|
const block = this.setUpBlockWithField();
|
|
const field = Array.from(block.getFields())[0];
|
|
const onHideCallback = sinon.stub();
|
|
|
|
Blockly.WidgetDiv.show(field, false, () => {});
|
|
|
|
// Simply showing the div should never call the hide callback.
|
|
assert.strictEqual(onHideCallback.callCount, 0);
|
|
});
|
|
|
|
test('without managed ephemeral focus does not change focused node', function () {
|
|
const block = this.setUpBlockWithField();
|
|
const field = Array.from(block.getFields())[0];
|
|
Blockly.getFocusManager().focusNode(block);
|
|
|
|
Blockly.WidgetDiv.show(field, false, () => {}, null, false);
|
|
|
|
// Since managing ephemeral focus is disabled the current focused node shouldn't be changed.
|
|
const blockFocusableElem = block.getFocusableElement();
|
|
assert.strictEqual(Blockly.getFocusManager().getFocusedNode(), block);
|
|
assert.strictEqual(document.activeElement, blockFocusableElem);
|
|
});
|
|
|
|
test('with managed ephemeral focus focuses widget div', function () {
|
|
const block = this.setUpBlockWithField();
|
|
const field = Array.from(block.getFields())[0];
|
|
Blockly.getFocusManager().focusNode(block);
|
|
|
|
Blockly.WidgetDiv.show(field, false, () => {}, null, true);
|
|
|
|
// Managing ephemeral focus won't change getFocusedNode() but will change the actual element
|
|
// with DOM focus.
|
|
const widgetDivElem = document.querySelector('.blocklyWidgetDiv');
|
|
assert.strictEqual(Blockly.getFocusManager().getFocusedNode(), block);
|
|
assert.strictEqual(document.activeElement, widgetDivElem);
|
|
});
|
|
});
|
|
|
|
suite('hide()', function () {
|
|
test('initially keeps display empty', function () {
|
|
Blockly.WidgetDiv.hide();
|
|
|
|
// The display property starts as empty and stays that way until an owner is attached.
|
|
const widgetDivElem = document.querySelector('.blocklyWidgetDiv');
|
|
assert.strictEqual(widgetDivElem.style.display, '');
|
|
});
|
|
|
|
test('for showing div hides div', function () {
|
|
const block = this.setUpBlockWithField();
|
|
const field = Array.from(block.getFields())[0];
|
|
Blockly.WidgetDiv.show(field, false, () => {});
|
|
|
|
Blockly.WidgetDiv.hide();
|
|
|
|
// Technically this will trigger a CSS animation, but the property is still set to 0.
|
|
const widgetDivElem = document.querySelector('.blocklyWidgetDiv');
|
|
assert.strictEqual(widgetDivElem.style.display, 'none');
|
|
});
|
|
|
|
test('for showing div and hide callback calls callback', function () {
|
|
const block = this.setUpBlockWithField();
|
|
const field = Array.from(block.getFields())[0];
|
|
const onHideCallback = sinon.stub();
|
|
Blockly.WidgetDiv.show(field, false, onHideCallback);
|
|
|
|
Blockly.WidgetDiv.hide();
|
|
|
|
// Hiding the div should trigger the hide callback.
|
|
assert.strictEqual(onHideCallback.callCount, 1);
|
|
});
|
|
|
|
test('for showing div without ephemeral focus does not change focus', function () {
|
|
const block = this.setUpBlockWithField();
|
|
const field = Array.from(block.getFields())[0];
|
|
Blockly.getFocusManager().focusNode(block);
|
|
Blockly.WidgetDiv.show(field, false, () => {}, null, false);
|
|
|
|
Blockly.WidgetDiv.hide();
|
|
|
|
// Hiding the div shouldn't change what would have already been focused.
|
|
const blockFocusableElem = block.getFocusableElement();
|
|
assert.strictEqual(Blockly.getFocusManager().getFocusedNode(), block);
|
|
assert.strictEqual(document.activeElement, blockFocusableElem);
|
|
});
|
|
|
|
test('for showing div with ephemeral focus restores DOM focus', function () {
|
|
const block = this.setUpBlockWithField();
|
|
const field = Array.from(block.getFields())[0];
|
|
Blockly.getFocusManager().focusNode(block);
|
|
Blockly.WidgetDiv.show(field, false, () => {}, null, true);
|
|
|
|
Blockly.WidgetDiv.hide();
|
|
|
|
// Hiding the div should restore focus back to the block.
|
|
const blockFocusableElem = block.getFocusableElement();
|
|
assert.strictEqual(Blockly.getFocusManager().getFocusedNode(), block);
|
|
assert.strictEqual(document.activeElement, blockFocusableElem);
|
|
});
|
|
|
|
test('for showing nested div with ephemeral focus restores DOM focus', function () {
|
|
const block = this.setUpBlockWithField();
|
|
const field = Array.from(block.getFields())[0];
|
|
Blockly.getFocusManager().focusNode(block);
|
|
const nestedDiv = document.createElement('div');
|
|
nestedDiv.tabIndex = -1;
|
|
Blockly.WidgetDiv.getDiv().appendChild(nestedDiv);
|
|
Blockly.WidgetDiv.show(field, false, () => {}, null, true);
|
|
nestedDiv.focus(); // It's valid to focus this during ephemeral focus.
|
|
|
|
// Hiding will cause the now focused child div to be removed, leading to
|
|
// ephemeral focus being lost if the implementation doesn't handle
|
|
// returning ephemeral focus correctly.
|
|
Blockly.WidgetDiv.hide();
|
|
|
|
// Hiding the div should restore focus back to the block.
|
|
const blockFocusableElem = block.getFocusableElement();
|
|
assert.strictEqual(Blockly.getFocusManager().getFocusedNode(), block);
|
|
assert.strictEqual(document.activeElement, blockFocusableElem);
|
|
});
|
|
|
|
test('without auto close on lost focus lost focus does not hide widget div', function () {
|
|
const block = this.setUpBlockWithField();
|
|
const field = Array.from(block.getFields())[0];
|
|
Blockly.getFocusManager().focusNode(block);
|
|
Blockly.WidgetDiv.show(field, false, () => {}, null, true, false);
|
|
|
|
// Focus an element outside of the widget.
|
|
document.getElementById('nonTreeElementForEphemeralFocus').focus();
|
|
|
|
// Even though the widget lost focus, it should still be visible.
|
|
const widgetDivElem = document.querySelector('.blocklyWidgetDiv');
|
|
assert.strictEqual(widgetDivElem.style.display, 'block');
|
|
});
|
|
|
|
test('with auto close on lost focus lost focus hides widget div', function () {
|
|
const block = this.setUpBlockWithField();
|
|
const field = Array.from(block.getFields())[0];
|
|
Blockly.getFocusManager().focusNode(block);
|
|
Blockly.WidgetDiv.show(field, false, () => {}, null, true, true);
|
|
|
|
// Focus an element outside of the widget.
|
|
document.getElementById('nonTreeElementForEphemeralFocus').focus();
|
|
|
|
// The widget should now be hidden since it lost focus.
|
|
const widgetDivElem = document.querySelector('.blocklyWidgetDiv');
|
|
assert.strictEqual(widgetDivElem.style.display, 'none');
|
|
});
|
|
|
|
test('with auto close on lost focus lost focus with nested div hides widget div', function () {
|
|
const block = this.setUpBlockWithField();
|
|
const field = Array.from(block.getFields())[0];
|
|
Blockly.getFocusManager().focusNode(block);
|
|
const nestedDiv = document.createElement('div');
|
|
nestedDiv.tabIndex = -1;
|
|
Blockly.WidgetDiv.getDiv().appendChild(nestedDiv);
|
|
Blockly.WidgetDiv.show(field, false, () => {}, null, true, true);
|
|
nestedDiv.focus(); // It's valid to focus this during ephemeral focus.
|
|
|
|
// Focus an element outside of the widget.
|
|
document.getElementById('nonTreeElementForEphemeralFocus').focus();
|
|
|
|
// The widget should now be hidden since it lost focus.
|
|
const widgetDivElem = document.querySelector('.blocklyWidgetDiv');
|
|
assert.strictEqual(widgetDivElem.style.display, 'none');
|
|
});
|
|
|
|
test('with auto close on lost focus lost focus with nested div does not restore DOM focus', function () {
|
|
const block = this.setUpBlockWithField();
|
|
const field = Array.from(block.getFields())[0];
|
|
Blockly.getFocusManager().focusNode(block);
|
|
const nestedDiv = document.createElement('div');
|
|
nestedDiv.tabIndex = -1;
|
|
Blockly.WidgetDiv.getDiv().appendChild(nestedDiv);
|
|
Blockly.WidgetDiv.show(field, false, () => {}, null, true, true);
|
|
nestedDiv.focus(); // It's valid to focus this during ephemeral focus.
|
|
|
|
// Focus an element outside of the widget.
|
|
const elem = document.getElementById('nonTreeElementForEphemeralFocus');
|
|
elem.focus();
|
|
|
|
// Auto hiding should not restore focus back to the block since ephemeral
|
|
// focus was lost before it was returned.
|
|
assert.isNull(Blockly.getFocusManager().getFocusedNode());
|
|
assert.strictEqual(document.activeElement, elem);
|
|
});
|
|
});
|
|
});
|