mirror of
https://github.com/google/blockly.git
synced 2026-01-06 00:20:37 +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 #9495 ### Proposed Changes Changes a bunch of ARIA role & label management to ensure that `Flyout` acts like a list rather than a tree. ### Reason for Changes `Flyout`s are always hierarchically flat so it doesn't make sense to model them as a tree. Instead, a menu is likely a better fit per https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Roles/menu_role: > A `menu` generally represents a grouping of common actions or functions that the user can invoke. The `menu` role is appropriate when a list of menu items is presented in a manner similar to a menu on a desktop application. Submenus, also known as pop-up menus, also have the role `menu`. However, there are important caveats that need to be considered and addressed: - As discussed below, menus introduce some unexpected compatibility issues with VoiceOver so this PR presently uses `list` and `listitem`s as a slightly more generic enumerating alternative for menus. - Menus (and to some extent lists) are stricter\* than trees in that they seem to impose a requirement that `menuitem`s cannot contain interactive elements (they are expected to be interactive themselves). This has led to a few specific changes: - Block children are now hidden when in the flyout (since they aren't navigable anyway). - Flyout buttons are themselves now the `menuitem` rather than their container parent, and they no longer use the role button. - Menus aren't really expected to contain labels but it isn't inherently disallowed. This is less of an issue with lists. - Because everything must be a `listitem` (or a few more specific alternatives) both blocks and buttons lack some context. Since not all `Flyout` items can be expected to be interactive, buttons and blocks have both had their labels updated to include an explicit indicator that they are buttons and blocks, respectively. Note that this does possibly go against convention for buttons in particular but it seems fine since this is an unusual (but seemingly correct) utilization of a `list` element. - To further provide context on blocks, the generated label for blocks in the `Flyout` is now its verbose label rather than the more compact form. \* This is largely a consequence of a few specific attributes of `menuitem` and `menu`s as a whole and very likely also applies to `tree`s and `treeitem`s (and `list`s and `listitems`s). However, now seemed like a good time to fix this especially in case some screen readers get confused rather than ignore nested interactive controls/follow semantic cloaking per the spec. Demo of it working on VoiceOver (per @gonfunko -- note this was the `menu` variant rather than the `list` variant of the PR):  ### Test Coverage This has been manually tested with ChromeVox. No automated tests are needed as part of this experimental change. ### Documentation No new documentation changes are needed for this experimental change. ### Additional Information None.
320 lines
8.7 KiB
TypeScript
320 lines
8.7 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2012 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* Image field. Used for pictures, icons, etc.
|
|
*
|
|
* @class
|
|
*/
|
|
// Former goog.module ID: Blockly.FieldImage
|
|
|
|
import {Field, FieldConfig} from './field.js';
|
|
import * as fieldRegistry from './field_registry.js';
|
|
import * as aria from './utils/aria.js';
|
|
import * as dom from './utils/dom.js';
|
|
import * as parsing from './utils/parsing.js';
|
|
import {Size} from './utils/size.js';
|
|
import {Svg} from './utils/svg.js';
|
|
|
|
/**
|
|
* Class for an image on a block.
|
|
*/
|
|
export class FieldImage extends Field<string> {
|
|
/**
|
|
* Vertical padding below the image, which is included in the reported height
|
|
* of the field.
|
|
*/
|
|
private static readonly Y_PADDING = 1;
|
|
protected readonly imageHeight: number;
|
|
|
|
/** The function to be called when this field is clicked. */
|
|
private clickHandler: ((p1: FieldImage) => void) | null = null;
|
|
|
|
/** The rendered field's image element. */
|
|
protected imageElement: SVGImageElement | null = null;
|
|
|
|
/**
|
|
* Editable fields usually show some sort of UI indicating they are
|
|
* editable. This field should not.
|
|
*/
|
|
override readonly EDITABLE = false;
|
|
|
|
/**
|
|
* Used to tell if the field needs to be rendered the next time the block is
|
|
* rendered. Image fields are statically sized, and only need to be
|
|
* rendered at initialization.
|
|
*/
|
|
protected override isDirty_ = false;
|
|
|
|
/** Whether to flip this image in RTL. */
|
|
private flipRtl = false;
|
|
|
|
/** Alt text of this image. */
|
|
private altText = '';
|
|
|
|
/**
|
|
* @param src The URL of the image.
|
|
* 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 width Width of the image.
|
|
* @param height Height of the image.
|
|
* @param alt Optional alt text for when block is collapsed.
|
|
* @param onClick Optional function to be called when the image is
|
|
* clicked. If onClick is defined, alt must also be defined.
|
|
* @param flipRtl Whether to flip the icon in RTL.
|
|
* @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/image#creation}
|
|
* for a list of properties this parameter supports.
|
|
*/
|
|
constructor(
|
|
src: string | typeof Field.SKIP_SETUP,
|
|
width: string | number,
|
|
height: string | number,
|
|
alt?: string,
|
|
onClick?: (p1: FieldImage) => void,
|
|
flipRtl?: boolean,
|
|
config?: FieldImageConfig,
|
|
) {
|
|
super(Field.SKIP_SETUP);
|
|
|
|
const imageHeight = Number(parsing.replaceMessageReferences(height));
|
|
const imageWidth = Number(parsing.replaceMessageReferences(width));
|
|
if (isNaN(imageHeight) || isNaN(imageWidth)) {
|
|
throw Error(
|
|
'Height and width values of an image field must cast to' + ' numbers.',
|
|
);
|
|
}
|
|
if (imageHeight <= 0 || imageWidth <= 0) {
|
|
throw Error(
|
|
'Height and width values of an image field must be greater' +
|
|
' than 0.',
|
|
);
|
|
}
|
|
|
|
/** The size of the area rendered by the field. */
|
|
this.size_ = new Size(imageWidth, imageHeight + FieldImage.Y_PADDING);
|
|
|
|
/**
|
|
* Store the image height, since it is different from the field height.
|
|
*/
|
|
this.imageHeight = imageHeight;
|
|
|
|
if (typeof onClick === 'function') {
|
|
this.clickHandler = onClick;
|
|
}
|
|
|
|
if (src === Field.SKIP_SETUP) return;
|
|
|
|
if (config) {
|
|
this.configure_(config);
|
|
} else {
|
|
this.flipRtl = !!flipRtl;
|
|
this.altText = parsing.replaceMessageReferences(alt) || '';
|
|
}
|
|
this.setValue(parsing.replaceMessageReferences(src));
|
|
}
|
|
|
|
/**
|
|
* 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: FieldImageConfig) {
|
|
super.configure_(config);
|
|
if (config.flipRtl) this.flipRtl = config.flipRtl;
|
|
if (config.alt) {
|
|
this.altText = parsing.replaceMessageReferences(config.alt);
|
|
}
|
|
}
|
|
|
|
override getAriaValue(): string {
|
|
return this.altText;
|
|
}
|
|
|
|
/**
|
|
* Create the block UI for this image.
|
|
*/
|
|
override initView() {
|
|
this.imageElement = dom.createSvgElement(
|
|
Svg.IMAGE,
|
|
{
|
|
'height': this.imageHeight + 'px',
|
|
'width': this.size_.width + 'px',
|
|
'alt': this.altText,
|
|
},
|
|
this.fieldGroup_,
|
|
);
|
|
this.imageElement.setAttributeNS(
|
|
dom.XLINK_NS,
|
|
'xlink:href',
|
|
this.value_ as string,
|
|
);
|
|
|
|
if (this.fieldGroup_) {
|
|
dom.addClass(this.fieldGroup_, 'blocklyImageField');
|
|
}
|
|
|
|
const isInFlyout = this.getSourceBlock()?.workspace?.isFlyout || false;
|
|
const element = this.getFocusableElement();
|
|
if (!isInFlyout && this.isClickable()) {
|
|
this.imageElement.style.cursor = 'pointer';
|
|
aria.setRole(element, aria.Role.BUTTON);
|
|
aria.setState(element, aria.State.LABEL, super.computeAriaLabel(true));
|
|
} else {
|
|
// The field isn't navigable unless it's clickable and outside the flyout.
|
|
aria.setRole(element, aria.Role.PRESENTATION);
|
|
}
|
|
}
|
|
|
|
override updateSize_() {}
|
|
// NOP
|
|
|
|
/**
|
|
* Ensure that the input value (the source URL) is a string.
|
|
*
|
|
* @param newValue The input value.
|
|
* @returns A string, or null if invalid.
|
|
*/
|
|
protected override doClassValidation_(newValue?: any): string | null {
|
|
if (typeof newValue !== 'string') {
|
|
return null;
|
|
}
|
|
return newValue;
|
|
}
|
|
|
|
/**
|
|
* Update the value of this image field, and update the displayed image.
|
|
*
|
|
* @param newValue The value to be saved. The default validator guarantees
|
|
* that this is a string.
|
|
*/
|
|
protected override doValueUpdate_(newValue: string) {
|
|
this.value_ = newValue;
|
|
if (this.imageElement) {
|
|
this.imageElement.setAttributeNS(dom.XLINK_NS, 'xlink:href', this.value_);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get whether to flip this image in RTL
|
|
*
|
|
* @returns True if we should flip in RTL.
|
|
*/
|
|
override getFlipRtl(): boolean {
|
|
return this.flipRtl;
|
|
}
|
|
|
|
/**
|
|
* Set the alt text of this image.
|
|
*
|
|
* @param alt New alt text.
|
|
*/
|
|
setAlt(alt: string | null) {
|
|
if (alt === this.altText) {
|
|
return;
|
|
}
|
|
this.altText = alt || '';
|
|
if (this.imageElement) {
|
|
this.imageElement.setAttribute('alt', this.altText);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check whether this field should be clickable.
|
|
*
|
|
* @returns Whether this field is clickable.
|
|
*/
|
|
isClickable(): boolean {
|
|
// Images are only clickable if they have a click handler and fulfill the
|
|
// contract to be clickable: enabled and attached to an editable block.
|
|
return super.isClickable() && !!this.clickHandler;
|
|
}
|
|
|
|
/**
|
|
* If field click is called, and click handler defined,
|
|
* call the handler.
|
|
*/
|
|
protected override showEditor_() {
|
|
if (this.clickHandler) {
|
|
this.clickHandler(this);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set the function that is called when this image is clicked.
|
|
*
|
|
* @param func The function that is called when the image is clicked, or null
|
|
* to remove.
|
|
*/
|
|
setOnClickHandler(func: ((p1: FieldImage) => void) | null) {
|
|
this.clickHandler = func;
|
|
}
|
|
|
|
/**
|
|
* Use the `getText_` developer hook to override the field's text
|
|
* representation.
|
|
* Return the image alt text instead.
|
|
*
|
|
* @returns The image alt text.
|
|
*/
|
|
protected override getText_(): string | null {
|
|
return this.altText;
|
|
}
|
|
|
|
/**
|
|
* Construct a FieldImage from a JSON arg object,
|
|
* dereferencing any string table references.
|
|
*
|
|
* @param options A JSON object with options (src, width, height, alt, and
|
|
* flipRtl).
|
|
* @returns The new field instance.
|
|
* @nocollapse
|
|
* @internal
|
|
*/
|
|
static override fromJson(options: FieldImageFromJsonConfig): FieldImage {
|
|
if (!options.src || !options.width || !options.height) {
|
|
throw new Error(
|
|
'src, width, and height values for an image field are' +
|
|
'required. The width and height must be non-zero.',
|
|
);
|
|
}
|
|
// `this` might be a subclass of FieldImage if that class doesn't override
|
|
// the static fromJson method.
|
|
return new this(
|
|
options.src,
|
|
options.width,
|
|
options.height,
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
options,
|
|
);
|
|
}
|
|
}
|
|
|
|
fieldRegistry.register('field_image', FieldImage);
|
|
|
|
FieldImage.prototype.DEFAULT_VALUE = '';
|
|
|
|
/**
|
|
* Config options for the image field.
|
|
*/
|
|
export interface FieldImageConfig extends FieldConfig {
|
|
flipRtl?: boolean;
|
|
alt?: string;
|
|
}
|
|
|
|
/**
|
|
* fromJson config options for the image field.
|
|
*/
|
|
export interface FieldImageFromJsonConfig extends FieldImageConfig {
|
|
src?: string;
|
|
width?: number;
|
|
height?: number;
|
|
}
|