mirror of
https://github.com/google/blockly.git
synced 2026-01-16 05:17:09 +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).
401 lines
11 KiB
TypeScript
401 lines
11 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2011 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
// Former goog.module ID: Blockly.inject
|
|
|
|
import type {BlocklyOptions} from './blockly_options.js';
|
|
import * as browserEvents from './browser_events.js';
|
|
import * as bumpObjects from './bump_objects.js';
|
|
import * as common from './common.js';
|
|
import * as Css from './css.js';
|
|
import * as dropDownDiv from './dropdowndiv.js';
|
|
import {Grid} from './grid.js';
|
|
import {Options} from './options.js';
|
|
import {ScrollbarPair} from './scrollbar_pair.js';
|
|
import * as Tooltip from './tooltip.js';
|
|
import * as Touch from './touch.js';
|
|
import * as aria from './utils/aria.js';
|
|
import * as dom from './utils/dom.js';
|
|
import {Svg} from './utils/svg.js';
|
|
import * as WidgetDiv from './widgetdiv.js';
|
|
import {WorkspaceSvg} from './workspace_svg.js';
|
|
|
|
/**
|
|
* Inject a Blockly editor into the specified container element (usually a div).
|
|
*
|
|
* @param container Containing element, or its ID, or a CSS selector.
|
|
* @param opt_options Optional dictionary of options.
|
|
* @returns Newly created main workspace.
|
|
*/
|
|
export function inject(
|
|
container: Element | string,
|
|
opt_options?: BlocklyOptions,
|
|
): WorkspaceSvg {
|
|
let containerElement: Element | null = null;
|
|
if (typeof container === 'string') {
|
|
containerElement =
|
|
document.getElementById(container) || document.querySelector(container);
|
|
} else {
|
|
containerElement = container;
|
|
}
|
|
// Verify that the container is in document.
|
|
if (
|
|
!document.contains(containerElement) &&
|
|
document !== containerElement?.ownerDocument
|
|
) {
|
|
throw Error('Error: container is not in current document');
|
|
}
|
|
const options = new Options(opt_options || ({} as BlocklyOptions));
|
|
const subContainer = document.createElement('div');
|
|
dom.addClass(subContainer, 'injectionDiv');
|
|
if (opt_options?.rtl) {
|
|
dom.addClass(subContainer, 'blocklyRTL');
|
|
}
|
|
|
|
containerElement!.appendChild(subContainer);
|
|
const svg = createDom(subContainer, options);
|
|
|
|
const workspace = createMainWorkspace(subContainer, svg, options);
|
|
|
|
init(workspace);
|
|
|
|
// Keep focus on the first workspace so entering keyboard navigation looks
|
|
// correct.
|
|
common.setMainWorkspace(workspace);
|
|
|
|
common.svgResize(workspace);
|
|
|
|
subContainer.addEventListener('focusin', function () {
|
|
common.setMainWorkspace(workspace);
|
|
});
|
|
|
|
browserEvents.conditionalBind(
|
|
subContainer,
|
|
'keydown',
|
|
null,
|
|
common.globalShortcutHandler,
|
|
);
|
|
|
|
// See: https://stackoverflow.com/a/48590836 for a reference.
|
|
const ariaAnnouncementSpan = document.createElement('span');
|
|
ariaAnnouncementSpan.id = 'blocklyAriaAnnounce';
|
|
aria.setState(ariaAnnouncementSpan, aria.State.LIVE, 'polite');
|
|
subContainer.appendChild(ariaAnnouncementSpan);
|
|
|
|
return workspace;
|
|
}
|
|
|
|
/**
|
|
* Create the SVG image.
|
|
*
|
|
* @param container Containing element.
|
|
* @param options Dictionary of options.
|
|
* @returns Newly created SVG image.
|
|
*/
|
|
function createDom(container: HTMLElement, options: Options): SVGElement {
|
|
// Sadly browsers (Chrome vs Firefox) are currently inconsistent in laying
|
|
// out content in RTL mode. Therefore Blockly forces the use of LTR,
|
|
// then manually positions content in RTL as needed.
|
|
container.setAttribute('dir', 'LTR');
|
|
|
|
// Load CSS.
|
|
Css.inject(options.hasCss, options.pathToMedia);
|
|
|
|
// Build the SVG DOM.
|
|
/*
|
|
<svg
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
xmlns:html="http://www.w3.org/1999/xhtml"
|
|
xmlns:xlink="http://www.w3.org/1999/xlink"
|
|
version="1.1"
|
|
class="blocklySvg">
|
|
...
|
|
</svg>
|
|
*/
|
|
const svg = dom.createSvgElement(
|
|
Svg.SVG,
|
|
{
|
|
'xmlns': dom.SVG_NS,
|
|
'xmlns:html': dom.HTML_NS,
|
|
'xmlns:xlink': dom.XLINK_NS,
|
|
'version': '1.1',
|
|
'class': 'blocklySvg',
|
|
},
|
|
container,
|
|
);
|
|
/*
|
|
<defs>
|
|
... filters go here ...
|
|
</defs>
|
|
*/
|
|
const defs = dom.createSvgElement(Svg.DEFS, {}, svg);
|
|
// Each filter/pattern needs a unique ID for the case of multiple Blockly
|
|
// instances on a page. Browser behaviour becomes undefined otherwise.
|
|
// https://neil.fraser.name/news/2015/11/01/
|
|
const rnd = String(Math.random()).substring(2);
|
|
|
|
options.gridPattern = Grid.createDom(
|
|
rnd,
|
|
options.gridOptions,
|
|
defs,
|
|
container,
|
|
);
|
|
return svg;
|
|
}
|
|
|
|
/**
|
|
* Create a main workspace and add it to the SVG.
|
|
*
|
|
* @param svg SVG element with pattern defined.
|
|
* @param options Dictionary of options.
|
|
* @returns Newly created main workspace.
|
|
*/
|
|
function createMainWorkspace(
|
|
injectionDiv: HTMLElement,
|
|
svg: SVGElement,
|
|
options: Options,
|
|
): WorkspaceSvg {
|
|
options.parentWorkspace = null;
|
|
const mainWorkspace = new WorkspaceSvg(options);
|
|
const wsOptions = mainWorkspace.options;
|
|
mainWorkspace.scale = wsOptions.zoomOptions.startScale;
|
|
svg.appendChild(
|
|
mainWorkspace.createDom('blocklyMainBackground', injectionDiv),
|
|
);
|
|
|
|
// Set the theme name and renderer name onto the injection div.
|
|
const rendererClassName = mainWorkspace.getRenderer().getClassName();
|
|
if (rendererClassName) {
|
|
dom.addClass(injectionDiv, rendererClassName);
|
|
}
|
|
const themeClassName = mainWorkspace.getTheme().getClassName();
|
|
if (themeClassName) {
|
|
dom.addClass(injectionDiv, themeClassName);
|
|
}
|
|
|
|
if (!wsOptions.hasCategories && wsOptions.languageTree) {
|
|
// Add flyout as an <svg> that is a sibling of the workspace SVG.
|
|
const flyout = mainWorkspace.addFlyout(Svg.SVG);
|
|
dom.insertAfter(flyout, svg);
|
|
}
|
|
if (wsOptions.hasTrashcan) {
|
|
mainWorkspace.addTrashcan();
|
|
}
|
|
if (wsOptions.zoomOptions && wsOptions.zoomOptions.controls) {
|
|
mainWorkspace.addZoomControls();
|
|
}
|
|
// Register the workspace svg as a UI component.
|
|
mainWorkspace
|
|
.getThemeManager()
|
|
.subscribe(svg, 'workspaceBackgroundColour', 'background-color');
|
|
|
|
// A null translation will also apply the correct initial scale.
|
|
mainWorkspace.translate(0, 0);
|
|
|
|
mainWorkspace.addChangeListener(
|
|
bumpObjects.bumpIntoBoundsHandler(mainWorkspace),
|
|
);
|
|
|
|
// The SVG is now fully assembled.
|
|
common.svgResize(mainWorkspace);
|
|
WidgetDiv.createDom();
|
|
dropDownDiv.createDom();
|
|
Tooltip.createDom();
|
|
return mainWorkspace;
|
|
}
|
|
|
|
/**
|
|
* Initialize Blockly with various handlers.
|
|
*
|
|
* @param mainWorkspace Newly created main workspace.
|
|
*/
|
|
function init(mainWorkspace: WorkspaceSvg) {
|
|
const options = mainWorkspace.options;
|
|
const svg = mainWorkspace.getParentSvg();
|
|
|
|
// Suppress the browser's context menu.
|
|
browserEvents.conditionalBind(
|
|
svg.parentNode as Element,
|
|
'contextmenu',
|
|
null,
|
|
function (e: Event) {
|
|
if (!browserEvents.isTargetInput(e)) {
|
|
e.preventDefault();
|
|
}
|
|
},
|
|
);
|
|
|
|
const workspaceResizeHandler = browserEvents.conditionalBind(
|
|
window,
|
|
'resize',
|
|
null,
|
|
function () {
|
|
// Don't hide all the chaff. Leave the dropdown and widget divs open if
|
|
// possible.
|
|
Tooltip.hide();
|
|
mainWorkspace.hideComponents(true);
|
|
dropDownDiv.repositionForWindowResize();
|
|
WidgetDiv.repositionForWindowResize();
|
|
common.svgResize(mainWorkspace);
|
|
bumpObjects.bumpTopObjectsIntoBounds(mainWorkspace);
|
|
},
|
|
);
|
|
mainWorkspace.setResizeHandlerWrapper(workspaceResizeHandler);
|
|
|
|
bindDocumentEvents();
|
|
|
|
if (options.languageTree) {
|
|
const toolbox = mainWorkspace.getToolbox();
|
|
const flyout = mainWorkspace.getFlyout(true);
|
|
if (toolbox) {
|
|
toolbox.init();
|
|
} else if (flyout) {
|
|
// Build a fixed flyout with the root blocks.
|
|
flyout.init(mainWorkspace);
|
|
flyout.show(options.languageTree);
|
|
if (typeof flyout.scrollToStart === 'function') {
|
|
flyout.scrollToStart();
|
|
}
|
|
}
|
|
}
|
|
|
|
if (options.hasTrashcan) {
|
|
mainWorkspace.trashcan!.init();
|
|
}
|
|
if (options.zoomOptions && options.zoomOptions.controls) {
|
|
mainWorkspace.zoomControls_!.init();
|
|
}
|
|
|
|
if (options.moveOptions && options.moveOptions.scrollbars) {
|
|
const horizontalScroll =
|
|
options.moveOptions.scrollbars === true ||
|
|
!!options.moveOptions.scrollbars.horizontal;
|
|
const verticalScroll =
|
|
options.moveOptions.scrollbars === true ||
|
|
!!options.moveOptions.scrollbars.vertical;
|
|
mainWorkspace.scrollbar = new ScrollbarPair(
|
|
mainWorkspace,
|
|
horizontalScroll,
|
|
verticalScroll,
|
|
'blocklyMainWorkspaceScrollbar',
|
|
);
|
|
mainWorkspace.scrollbar.resize();
|
|
} else {
|
|
mainWorkspace.setMetrics({x: 0.5, y: 0.5});
|
|
}
|
|
|
|
// Load the sounds.
|
|
if (options.hasSounds) {
|
|
loadSounds(options.pathToMedia, mainWorkspace);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Whether event handlers have been bound. Document event handlers will only
|
|
* be bound once, even if Blockly is destroyed and reinjected.
|
|
*/
|
|
let documentEventsBound = false;
|
|
|
|
/**
|
|
* Bind document events, but only once. Destroying and reinjecting Blockly
|
|
* should not bind again.
|
|
* Bind events for scrolling the workspace.
|
|
* Most of these events should be bound to the SVG's surface.
|
|
* However, 'mouseup' has to be on the whole document so that a block dragged
|
|
* out of bounds and released will know that it has been released.
|
|
*/
|
|
function bindDocumentEvents() {
|
|
if (!documentEventsBound) {
|
|
browserEvents.conditionalBind(document, 'scroll', null, function () {
|
|
const workspaces = common.getAllWorkspaces();
|
|
for (let i = 0, workspace; (workspace = workspaces[i]); i++) {
|
|
if (workspace instanceof WorkspaceSvg) {
|
|
workspace.updateInverseScreenCTM();
|
|
}
|
|
}
|
|
});
|
|
// longStop needs to run to stop the context menu from showing up. It
|
|
// should run regardless of what other touch event handlers have run.
|
|
browserEvents.bind(document, 'touchend', null, Touch.longStop);
|
|
browserEvents.bind(document, 'touchcancel', null, Touch.longStop);
|
|
}
|
|
documentEventsBound = true;
|
|
}
|
|
|
|
/**
|
|
* Load sounds for the given workspace.
|
|
*
|
|
* @param pathToMedia The path to the media directory.
|
|
* @param workspace The workspace to load sounds for.
|
|
*/
|
|
function loadSounds(pathToMedia: string, workspace: WorkspaceSvg) {
|
|
const audioMgr = workspace.getAudioManager();
|
|
audioMgr.load(
|
|
[
|
|
pathToMedia + 'click.mp3',
|
|
pathToMedia + 'click.wav',
|
|
pathToMedia + 'click.ogg',
|
|
],
|
|
'click',
|
|
);
|
|
audioMgr.load(
|
|
[
|
|
pathToMedia + 'disconnect.wav',
|
|
pathToMedia + 'disconnect.mp3',
|
|
pathToMedia + 'disconnect.ogg',
|
|
],
|
|
'disconnect',
|
|
);
|
|
audioMgr.load(
|
|
[
|
|
pathToMedia + 'delete.mp3',
|
|
pathToMedia + 'delete.ogg',
|
|
pathToMedia + 'delete.wav',
|
|
],
|
|
'delete',
|
|
);
|
|
|
|
// Bind temporary hooks that preload the sounds.
|
|
const soundBinds: browserEvents.Data[] = [];
|
|
/**
|
|
*
|
|
*/
|
|
function unbindSounds() {
|
|
while (soundBinds.length) {
|
|
const oldSoundBinding = soundBinds.pop();
|
|
if (oldSoundBinding) {
|
|
browserEvents.unbind(oldSoundBinding);
|
|
}
|
|
}
|
|
audioMgr.preload();
|
|
}
|
|
|
|
// These are bound on mouse/touch events with
|
|
// Blockly.browserEvents.conditionalBind, so they restrict the touch
|
|
// identifier that will be recognized. But this is really something that
|
|
// happens on a click, not a drag, so that's not necessary.
|
|
|
|
// Android ignores any sound not loaded as a result of a user action.
|
|
soundBinds.push(
|
|
browserEvents.conditionalBind(
|
|
document,
|
|
'pointermove',
|
|
null,
|
|
unbindSounds,
|
|
true,
|
|
),
|
|
);
|
|
soundBinds.push(
|
|
browserEvents.conditionalBind(
|
|
document,
|
|
'touchstart',
|
|
null,
|
|
unbindSounds,
|
|
true,
|
|
),
|
|
);
|
|
}
|