mirror of
https://github.com/google/blockly.git
synced 2026-01-07 00:50:27 +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 part of #8207 Fixes part of #3370 ### Proposed Changes This introduces initial broad ARIA integration in order to enable at least basic screen reader support when using keyboard navigation. Largely this involves introducing ARIA roles and labels in a bunch of places, sometimes done in a way to override normal built-in behaviors of the accessibility node tree in order to get a richer first-class output for Blockly (such as for blocks and workspaces). ### Reason for Changes ARIA is the fundamental basis for configuring how focusable nodes in Blockly are represented to the user when using a screen reader. As such, all focusable nodes requires labels and roles in order to correctly communicate their contexts. The specific approach taken in this PR is to simply add labels and roles to all nodes where obvious with some extra work done for `WorkspaceSvg` and `BlockSvg` in order to represent blocks as a tree (since that seems to be the best fitting ARIA role per those available: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Roles). The custom work specifically for blocks includes: - Overriding the role description to be 'block' rather than 'tree item' (which is the default). - Overriding the position, level, and number of sibling counts since those are normally determined based on the DOM tree and blocks are not laid out in the tree the same way they are visually or logically (so these computations were incorrect). This is also the reason for a bunch of extra computation logic being introduced. One note on some of the labels being nonsensical (e.g. 'DoNotOverride?'): this was done intentionally to try and ensure _all_ focusable nodes (that can be focused) have labels, even when the specifics of what that label should be aren't yet clear. More components had these temporary labels until testing revealed how exactly they would behave from a screen reader perspective (at which point their roles and labels were updated as needed). The temporary labels act as an indicator when navigating through the UI, and some of the nodes can't easily be reached (for reasons) and thus may never actually need a label. More work is needed in understanding both what components need labels and what those labels should be, but that will be done beyond this PR. ### Test Coverage No tests are added to this as it's experimental and not a final implementation. The keyboard navigation tests are failing due to a visibility expansion of `connectionCandidate` in `BlockDragStrategy`. There's no way to avoid this breakage, unfortunately. Instead, this PR will be merged and then https://github.com/google/blockly-keyboard-experimentation/pull/684 will be finalized and merged to fix it. There's some additional work that will happen both in that branch and in a later PR in core Blockly to integrate the two experimentation branches as part of #9283 so that CI passes correctly for both branches. ### Documentation No documentation is needed at this time. ### Additional Information This work is experimental and is meant to serve two purposes: - Provide a foundation for testing and iterating the core screen reader experience in Blockly. - Provide a reference point for designing a long-term solution that accounts for all requirements collected during user testing. This code should never be merged into `develop` as it stands. Instead, it will be redesigned with maintainability, testing, and correctness in mind at a future date (see https://github.com/google/blockly-keyboard-experimentation/discussions/673).
240 lines
6.2 KiB
TypeScript
240 lines
6.2 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2023 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
// Former goog.module ID: Blockly.Warning
|
|
|
|
import type {BlockSvg} from '../block_svg.js';
|
|
import {TextBubble} from '../bubbles/text_bubble.js';
|
|
import {EventType} from '../events/type.js';
|
|
import * as eventUtils from '../events/utils.js';
|
|
import type {IBubble} from '../interfaces/i_bubble.js';
|
|
import type {IHasBubble} from '../interfaces/i_has_bubble.js';
|
|
import * as renderManagement from '../render_management.js';
|
|
import {Size} from '../utils.js';
|
|
import * as aria from '../utils/aria.js';
|
|
import {Coordinate} from '../utils/coordinate.js';
|
|
import * as dom from '../utils/dom.js';
|
|
import {Rect} from '../utils/rect.js';
|
|
import {Svg} from '../utils/svg.js';
|
|
import {Icon} from './icon.js';
|
|
import {IconType} from './icon_types.js';
|
|
|
|
/** The size of the warning icon in workspace-scale units. */
|
|
const SIZE = 17;
|
|
|
|
/**
|
|
* An icon that warns the user that something is wrong with their block.
|
|
*
|
|
* For example, this could be used to warn them about incorrect field values,
|
|
* or incorrect placement of the block (putting it somewhere it doesn't belong).
|
|
*/
|
|
export class WarningIcon extends Icon implements IHasBubble {
|
|
/** The type string used to identify this icon. */
|
|
static readonly TYPE = IconType.WARNING;
|
|
|
|
/**
|
|
* The weight this icon has relative to other icons. Icons with more positive
|
|
* weight values are rendered farther toward the end of the block.
|
|
*/
|
|
static readonly WEIGHT = 2;
|
|
|
|
/** A map of warning IDs to warning text. */
|
|
private textMap: Map<string, string> = new Map();
|
|
|
|
/** The bubble used to display the warnings to the user. */
|
|
private textBubble: TextBubble | null = null;
|
|
|
|
/** @internal */
|
|
constructor(protected readonly sourceBlock: BlockSvg) {
|
|
super(sourceBlock);
|
|
}
|
|
|
|
override getType(): IconType<WarningIcon> {
|
|
return WarningIcon.TYPE;
|
|
}
|
|
|
|
override initView(pointerdownListener: (e: PointerEvent) => void): void {
|
|
if (this.svgRoot) return; // Already initialized.
|
|
|
|
super.initView(pointerdownListener);
|
|
|
|
// Triangle with rounded corners.
|
|
dom.createSvgElement(
|
|
Svg.PATH,
|
|
{
|
|
'class': 'blocklyIconShape',
|
|
'd': 'M2,15Q-1,15 0.5,12L6.5,1.7Q8,-1 9.5,1.7L15.5,12Q17,15 14,15z',
|
|
},
|
|
this.svgRoot,
|
|
);
|
|
// Can't use a real '!' text character since different browsers and
|
|
// operating systems render it differently. Body of exclamation point.
|
|
dom.createSvgElement(
|
|
Svg.PATH,
|
|
{
|
|
'class': 'blocklyIconSymbol',
|
|
'd': 'm7,4.8v3.16l0.27,2.27h1.46l0.27,-2.27v-3.16z',
|
|
},
|
|
this.svgRoot,
|
|
);
|
|
// Dot of exclamation point.
|
|
dom.createSvgElement(
|
|
Svg.RECT,
|
|
{
|
|
'class': 'blocklyIconSymbol',
|
|
'x': '7',
|
|
'y': '11',
|
|
'height': '2',
|
|
'width': '2',
|
|
},
|
|
this.svgRoot,
|
|
);
|
|
dom.addClass(this.svgRoot!, 'blocklyWarningIcon');
|
|
|
|
this.recomputeAriaLabel();
|
|
}
|
|
|
|
private recomputeAriaLabel() {
|
|
aria.setState(
|
|
this.svgRoot!,
|
|
aria.State.LABEL,
|
|
this.bubbleIsVisible() ? 'Close Warning' : 'Open Warning',
|
|
);
|
|
}
|
|
|
|
override dispose() {
|
|
super.dispose();
|
|
this.textBubble?.dispose();
|
|
}
|
|
|
|
override getWeight(): number {
|
|
return WarningIcon.WEIGHT;
|
|
}
|
|
|
|
override getSize(): Size {
|
|
return new Size(SIZE, SIZE);
|
|
}
|
|
|
|
override applyColour(): void {
|
|
super.applyColour();
|
|
this.textBubble?.setColour(this.sourceBlock.getColour());
|
|
}
|
|
|
|
override updateCollapsed(): void {
|
|
// We are shown when collapsed, so do nothing! I.e. skip the default
|
|
// behavior of hiding.
|
|
}
|
|
|
|
/** Tells blockly that this icon is shown when the block is collapsed. */
|
|
override isShownWhenCollapsed(): boolean {
|
|
return true;
|
|
}
|
|
|
|
/** Updates the location of the icon's bubble if it is open. */
|
|
override onLocationChange(blockOrigin: Coordinate): void {
|
|
super.onLocationChange(blockOrigin);
|
|
this.textBubble?.setAnchorLocation(this.getAnchorLocation());
|
|
}
|
|
|
|
/**
|
|
* Adds a warning message to this warning icon.
|
|
*
|
|
* @param text The text of the message to add.
|
|
* @param id The id of the message to add.
|
|
* @internal
|
|
*/
|
|
addMessage(text: string, id: string): this {
|
|
if (this.textMap.get(id) === text) return this;
|
|
|
|
if (text) {
|
|
this.textMap.set(id, text);
|
|
} else {
|
|
this.textMap.delete(id);
|
|
}
|
|
|
|
this.textBubble?.setText(this.getText());
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* @returns the display text for this icon. Includes all warning messages
|
|
* concatenated together with newlines.
|
|
* @internal
|
|
*/
|
|
getText(): string {
|
|
return [...this.textMap.values()].join('\n');
|
|
}
|
|
|
|
/** Toggles the visibility of the bubble. */
|
|
override onClick(): void {
|
|
super.onClick();
|
|
this.setBubbleVisible(!this.bubbleIsVisible());
|
|
}
|
|
|
|
override isClickableInFlyout(): boolean {
|
|
return false;
|
|
}
|
|
|
|
bubbleIsVisible(): boolean {
|
|
return !!this.textBubble;
|
|
}
|
|
|
|
async setBubbleVisible(visible: boolean): Promise<void> {
|
|
if (this.bubbleIsVisible() === visible) return;
|
|
|
|
await renderManagement.finishQueuedRenders();
|
|
|
|
if (visible) {
|
|
this.textBubble = new TextBubble(
|
|
this.getText(),
|
|
this.sourceBlock.workspace,
|
|
this.getAnchorLocation(),
|
|
this.getBubbleOwnerRect(),
|
|
);
|
|
this.applyColour();
|
|
} else {
|
|
this.textBubble?.dispose();
|
|
this.textBubble = null;
|
|
}
|
|
|
|
eventUtils.fire(
|
|
new (eventUtils.get(EventType.BUBBLE_OPEN))(
|
|
this.sourceBlock,
|
|
visible,
|
|
'warning',
|
|
),
|
|
);
|
|
|
|
this.recomputeAriaLabel();
|
|
}
|
|
|
|
/** See IHasBubble.getBubble. */
|
|
getBubble(): IBubble | null {
|
|
return this.textBubble;
|
|
}
|
|
|
|
/**
|
|
* @returns the location the bubble should be anchored to.
|
|
* I.E. the middle of this icon.
|
|
*/
|
|
private getAnchorLocation(): Coordinate {
|
|
const midIcon = SIZE / 2;
|
|
return Coordinate.sum(
|
|
this.workspaceLocation,
|
|
new Coordinate(midIcon, midIcon),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @returns the rect the bubble should avoid overlapping.
|
|
* I.E. the block that owns this icon.
|
|
*/
|
|
private getBubbleOwnerRect(): Rect {
|
|
const bbox = this.sourceBlock.getSvgRoot().getBBox();
|
|
return new Rect(bbox.y, bbox.y + bbox.height, bbox.x, bbox.x + bbox.width);
|
|
}
|
|
}
|