mirror of
https://github.com/google/blockly.git
synced 2026-01-29 11:40:14 +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).
276 lines
7.7 KiB
TypeScript
276 lines
7.7 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2012 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* Checkbox field. Checked or not checked.
|
|
*
|
|
* @class
|
|
*/
|
|
// Former goog.module ID: Blockly.FieldCheckbox
|
|
|
|
// Unused import preserved for side-effects. Remove if unneeded.
|
|
import './events/events_block_change.js';
|
|
|
|
import {Field, FieldConfig, FieldValidator} from './field.js';
|
|
import * as fieldRegistry from './field_registry.js';
|
|
import * as aria from './utils/aria.js';
|
|
import * as dom from './utils/dom.js';
|
|
|
|
type BoolString = 'TRUE' | 'FALSE';
|
|
type CheckboxBool = BoolString | boolean;
|
|
|
|
/**
|
|
* Class for a checkbox field.
|
|
*/
|
|
export class FieldCheckbox extends Field<CheckboxBool> {
|
|
/** Default character for the checkmark. */
|
|
static readonly CHECK_CHAR = '✓';
|
|
private checkChar: string;
|
|
|
|
/**
|
|
* Serializable fields are saved by the serializer, non-serializable fields
|
|
* are not. Editable fields should also be serializable.
|
|
*/
|
|
override SERIALIZABLE = true;
|
|
|
|
/**
|
|
* NOTE: The default value is set in `Field`, so maintain that value instead
|
|
* of overwriting it here or in the constructor.
|
|
*/
|
|
override value_: boolean | null = this.value_;
|
|
|
|
/**
|
|
* @param value The initial value of the field. Should either be 'TRUE',
|
|
* 'FALSE' or a boolean. Defaults to 'FALSE'. Also accepts
|
|
* Field.SKIP_SETUP if you wish to skip setup (only used by subclasses
|
|
* that want to handle configuration and setting the field value after
|
|
* their own constructors have run).
|
|
* @param validator A function that is called to validate changes to the
|
|
* field's value. Takes in a value ('TRUE' or 'FALSE') & returns a
|
|
* validated value ('TRUE' or 'FALSE'), or null to abort the change.
|
|
* @param config A map of options used to configure the field.
|
|
* See the [field creation documentation]{@link
|
|
* https://developers.google.com/blockly/guides/create-custom-blocks/fields/built-in-fields/checkbox#creation}
|
|
* for a list of properties this parameter supports.
|
|
*/
|
|
constructor(
|
|
value?: CheckboxBool | typeof Field.SKIP_SETUP,
|
|
validator?: FieldCheckboxValidator,
|
|
config?: FieldCheckboxConfig,
|
|
) {
|
|
super(Field.SKIP_SETUP);
|
|
|
|
/**
|
|
* Character for the check mark. Used to apply a different check mark
|
|
* character to individual fields.
|
|
*/
|
|
this.checkChar = FieldCheckbox.CHECK_CHAR;
|
|
|
|
if (value === Field.SKIP_SETUP) return;
|
|
if (config) {
|
|
this.configure_(config);
|
|
}
|
|
this.setValue(value);
|
|
if (validator) {
|
|
this.setValidator(validator);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Configure the field based on the given map of options.
|
|
*
|
|
* @param config A map of options to configure the field based on.
|
|
*/
|
|
protected override configure_(config: FieldCheckboxConfig) {
|
|
super.configure_(config);
|
|
if (config.checkCharacter) this.checkChar = config.checkCharacter;
|
|
}
|
|
|
|
/**
|
|
* Saves this field's value.
|
|
*
|
|
* @returns The boolean value held by this field.
|
|
* @internal
|
|
*/
|
|
override saveState(): AnyDuringMigration {
|
|
const legacyState = this.saveLegacyState(FieldCheckbox);
|
|
if (legacyState !== null) {
|
|
return legacyState;
|
|
}
|
|
return this.getValueBoolean();
|
|
}
|
|
|
|
/**
|
|
* Create the block UI for this checkbox.
|
|
*/
|
|
override initView() {
|
|
super.initView();
|
|
|
|
const textElement = this.getTextElement();
|
|
dom.addClass(this.fieldGroup_!, 'blocklyCheckboxField');
|
|
textElement.style.display = this.value_ ? 'block' : 'none';
|
|
|
|
const element = this.getFocusableElement();
|
|
aria.setRole(element, aria.Role.CHECKBOX);
|
|
aria.setState(
|
|
element,
|
|
aria.State.LABEL,
|
|
this.name ? `Checkbox ${this.name}` : 'Checkbox',
|
|
);
|
|
}
|
|
|
|
override render_() {
|
|
if (this.textContent_) {
|
|
this.textContent_.nodeValue = this.getDisplayText_();
|
|
}
|
|
this.updateSize_(this.getConstants()!.FIELD_CHECKBOX_X_OFFSET);
|
|
}
|
|
|
|
override getDisplayText_() {
|
|
return this.checkChar;
|
|
}
|
|
|
|
/**
|
|
* Set the character used for the check mark.
|
|
*
|
|
* @param character The character to use for the check mark, or null to use
|
|
* the default.
|
|
*/
|
|
setCheckCharacter(character: string | null) {
|
|
this.checkChar = character || FieldCheckbox.CHECK_CHAR;
|
|
this.forceRerender();
|
|
}
|
|
|
|
/** Toggle the state of the checkbox on click. */
|
|
protected override showEditor_() {
|
|
this.setValue(!this.value_);
|
|
}
|
|
|
|
/**
|
|
* Ensure that the input value is valid ('TRUE' or 'FALSE').
|
|
*
|
|
* @param newValue The input value.
|
|
* @returns A valid value ('TRUE' or 'FALSE), or null if invalid.
|
|
*/
|
|
protected override doClassValidation_(
|
|
newValue?: AnyDuringMigration,
|
|
): BoolString | null {
|
|
if (newValue === true || newValue === 'TRUE') {
|
|
return 'TRUE';
|
|
}
|
|
if (newValue === false || newValue === 'FALSE') {
|
|
return 'FALSE';
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Update the value of the field, and update the checkElement.
|
|
*
|
|
* @param newValue The value to be saved. The default validator guarantees
|
|
* that this is a either 'TRUE' or 'FALSE'.
|
|
*/
|
|
protected override doValueUpdate_(newValue: BoolString) {
|
|
this.value_ = this.convertValueToBool(newValue);
|
|
// Update visual.
|
|
if (this.textElement_) {
|
|
this.textElement_.style.display = this.value_ ? 'block' : 'none';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the value of this field, either 'TRUE' or 'FALSE'.
|
|
*
|
|
* @returns The value of this field.
|
|
*/
|
|
override getValue(): BoolString {
|
|
return this.value_ ? 'TRUE' : 'FALSE';
|
|
}
|
|
|
|
/**
|
|
* Get the boolean value of this field.
|
|
*
|
|
* @returns The boolean value of this field.
|
|
*/
|
|
getValueBoolean(): boolean | null {
|
|
return this.value_;
|
|
}
|
|
|
|
/**
|
|
* Get the text of this field. Used when the block is collapsed.
|
|
*
|
|
* @returns Text representing the value of this field ('true' or 'false').
|
|
*/
|
|
override getText(): string {
|
|
return String(this.convertValueToBool(this.value_));
|
|
}
|
|
|
|
/**
|
|
* Convert a value into a pure boolean.
|
|
*
|
|
* Converts 'TRUE' to true and 'FALSE' to false correctly, everything else
|
|
* is cast to a boolean.
|
|
*
|
|
* @param value The value to convert.
|
|
* @returns The converted value.
|
|
*/
|
|
private convertValueToBool(value: CheckboxBool | null): boolean {
|
|
if (typeof value === 'string') return value === 'TRUE';
|
|
return !!value;
|
|
}
|
|
|
|
/**
|
|
* Construct a FieldCheckbox from a JSON arg object.
|
|
*
|
|
* @param options A JSON object with options (checked).
|
|
* @returns The new field instance.
|
|
* @nocollapse
|
|
* @internal
|
|
*/
|
|
static override fromJson(
|
|
options: FieldCheckboxFromJsonConfig,
|
|
): FieldCheckbox {
|
|
// `this` might be a subclass of FieldCheckbox if that class doesn't
|
|
// 'override' the static fromJson method.
|
|
return new this(options.checked, undefined, options);
|
|
}
|
|
}
|
|
|
|
fieldRegistry.register('field_checkbox', FieldCheckbox);
|
|
|
|
FieldCheckbox.prototype.DEFAULT_VALUE = false;
|
|
|
|
/**
|
|
* Config options for the checkbox field.
|
|
*/
|
|
export interface FieldCheckboxConfig extends FieldConfig {
|
|
checkCharacter?: string;
|
|
}
|
|
|
|
/**
|
|
* fromJson config options for the checkbox field.
|
|
*/
|
|
export interface FieldCheckboxFromJsonConfig extends FieldCheckboxConfig {
|
|
checked?: boolean;
|
|
}
|
|
|
|
/**
|
|
* A function that is called to validate changes to the field's value before
|
|
* they are set.
|
|
*
|
|
* @see {@link https://developers.google.com/blockly/guides/create-custom-blocks/fields/validators#return_values}
|
|
* @param newValue The value to be validated.
|
|
* @returns One of three instructions for setting the new value: `T`, `null`,
|
|
* or `undefined`.
|
|
*
|
|
* - `T` to set this function's returned value instead of `newValue`.
|
|
*
|
|
* - `null` to invoke `doValueInvalid_` and not set a value.
|
|
*
|
|
* - `undefined` to set `newValue` as is.
|
|
*/
|
|
export type FieldCheckboxValidator = FieldValidator<CheckboxBool>;
|