Skip to content

Commit 00882a8

Browse files
authored
fix: Fix styling and focusing of connections in shadow DOM (#9952)
1 parent b69c38d commit 00882a8

3 files changed

Lines changed: 18 additions & 7 deletions

File tree

packages/blockly/core/keyboard_navigation_controller.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
import {getMainWorkspace} from './common.js';
8+
import type {WorkspaceSvg} from './workspace_svg.js';
9+
710
/**
811
* The KeyboardNavigationController handles coordinating Blockly-wide
912
* keyboard navigation behavior, such as enabling/disabling full
@@ -69,10 +72,12 @@ export class KeyboardNavigationController {
6972

7073
/** Adds or removes the css class that indicates keyboard navigation is active. */
7174
private updateActiveVisualization() {
75+
const root = (getMainWorkspace() as WorkspaceSvg).getInjectionDiv()
76+
.parentElement;
7277
if (this.isActive) {
73-
document.body.classList.add(this.activeClassName);
78+
root?.classList.add(this.activeClassName);
7479
} else {
75-
document.body.classList.remove(this.activeClassName);
80+
root?.classList.remove(this.activeClassName);
7681
}
7782
}
7883
}

packages/blockly/core/rendered_connection.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -720,9 +720,10 @@ export class RenderedConnection
720720
private findHighlightSvg(): SVGPathElement | null {
721721
// This cast is valid as TypeScript's definition is wrong. See:
722722
// https://github.com/microsoft/TypeScript/issues/60996.
723-
return document.getElementById(this.id) as
724-
| unknown
725-
| null as SVGPathElement | null;
723+
const root = this.getSourceBlock().getSvgRoot().getRootNode() as
724+
| ShadowRoot
725+
| HTMLDocument;
726+
return root.getElementById(this.id) as SVGPathElement | null;
726727
}
727728
}
728729

packages/blockly/tests/mocha/keyboard_navigation_controller_test.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
suite('Keyboard Navigation Controller', function () {
1414
setup(function () {
1515
sharedTestSetup.call(this);
16+
this.workspace = Blockly.inject('blocklyDiv');
1617
Blockly.keyboardNavigationController.setIsActive(false);
1718
});
1819

@@ -24,14 +25,18 @@ suite('Keyboard Navigation Controller', function () {
2425
test('Setting active keyboard navigation adds css class', function () {
2526
Blockly.keyboardNavigationController.setIsActive(true);
2627
assert.isTrue(
27-
document.body.classList.contains('blocklyKeyboardNavigation'),
28+
Blockly.getMainWorkspace()
29+
.getInjectionDiv()
30+
.parentElement.classList.contains('blocklyKeyboardNavigation'),
2831
);
2932
});
3033

3134
test('Disabling active keyboard navigation removes css class', function () {
3235
Blockly.keyboardNavigationController.setIsActive(false);
3336
assert.isFalse(
34-
document.body.classList.contains('blocklyKeyboardNavigation'),
37+
Blockly.getMainWorkspace()
38+
.getInjectionDiv()
39+
.parentElement.classList.contains('blocklyKeyboardNavigation'),
3540
);
3641
});
3742
});

0 commit comments

Comments
 (0)