mirror of
https://github.com/google/blockly.git
synced 2025-12-16 06:10:12 +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 #8965 Fixes #8978 Fixes #8970 Fixes https://github.com/google/blockly-keyboard-experimentation/issues/523 Fixes https://github.com/google/blockly-keyboard-experimentation/issues/547 Fixes part of #8910 ### Proposed Changes Fives groups of changes are included in this PR: 1. Support for automatic tab index management for focusable trees. 2. Support for automatic tab index management for focusable nodes. 3. Support for automatically hiding the flyout when back navigating from the toolbox. 4. A fix for `FocusManager` losing DOM syncing that was introduced in #9082. 5. Some cleanups for flyout and some tests for previous behavior changes to `FocusManager`. ### Reason for Changes Infrastructure changes reasoning: - Automatically managing tab indexes for both focusable trees and roots can largely reduce the difficulty of providing focusable nodes/trees and generally interacting with `FocusManager`. This facilitates a more automated navigation experience. - The fix for losing DOM syncing is possibly not reliable, but there are at least now tests to cover for it. This may be a case where a `try{} finally{}` could be warranted, but the code will stay as-is unless requested otherwise. `Flyout` changes: - `Flyout` no longer needs to be a focusable tree, but removing that would be an API breakage. Instead, it throws for most of the normal tree/node calls as it should no longer be used as such. Instead, its workspace has been made top-level tabbable (in addition to the main workspace) which solves the extra tab stop issues and general confusing inconsistencies between the flyout, toolbox, and workspace. - `Flyout` now correctly auto-selects the first block (#9103 notwithstanding). Technically it did before, however the extra `Flyout` tabstop before its workspace caused the inconsistency (since focusing the `Flyout` itself did not auto-select, only selecting its workspace did). Important caveats: - `getAttribute` is used in place of directly fetching `.tabIndex` since the latter can apparently default to `-1` (and possibly `0`) in cases when it's not actually set. This is a very surprising behavior that leads to incorrect test results. - Sometimes tab index still needs to be introduced (such as in cases where native DOM focus is needed, e.g. via `focus()` calls or clicking). This is demonstrated both by updates to `FocusManager`'s tests as well as toolbox's category and separator. This can be slightly tricky to miss as large parts of Blockly now depend on focus to represent their state, so clicking either needs to be managed by Blockly (with corresponding `focusNode` calls) or automatic (with a tab index defined for the element that can be clicked, or which has a child that can be clicked). Note that nearly all elements used for testing focus in the test `index.html` page have had their tab indexes removed to lean on `FocusManager`'s automatic tab management (though as mentioned above there is still some manual tab index management required for `focus()`-specific tests). ### Test Coverage New tests were added for all of the updated behaviors to `FocusManager`, including a new need to explicitly provide (and reset) tab indexes for all `focus()`-esque tests. This also includes adding new tests for some behaviors introduced in past PRs (a la #8910). Note that all of the new and affected conditionals in `FocusManager` have been verified as having at least 1 test that breaks when it's removed (inverted conditions weren't thoroughly tested, but it's expected that they should also be well covered now). Additional tests to cover the actual navigation flows will be added to the keyboard experimentation plugin repository as part of https://github.com/google/blockly-keyboard-experimentation/pull/557 (this PR needs to be merged first). For manual testing, I mainly verified keyboard navigation with some cursory mouse & click testing in the simple playground. @rachel-fenichel also performed more thorough mouse & click testing (that yielded an actual issue that was fixed--see discussion below). The core webdriver tests have been verified to have seemingly the same existing failures with and without these changes. All of the following new keyboard navigation plugin tests have been verified as failing without the fixes introduced in this branch (and passing with them): - `Tab navigating to flyout should auto-select first block` - `Keyboard nav to different toolbox category should auto-select first block` - `Keyboard nav to different toolbox category and block should select different block` - `Tab navigate away from toolbox restores focus to initial element` - `Tab navigate away from toolbox closes flyout` - `Tab navigate away from flyout to toolbox and away closes flyout` - `Tabbing to the workspace after selecting flyout block should close the flyout` - `Tabbing to the workspace after selecting flyout block via workspace toolbox shortcut should close the flyout` - `Tabbing back from workspace should reopen the flyout` - `Navigation position in workspace should be retained when tabbing to flyout and back` - `Clicking outside Blockly with focused toolbox closes the flyout` - `Clicking outside Blockly with focused flyout closes the flyout` - `Clicking on toolbox category focuses it and opens flyout` ### Documentation No documentation changes are needed beyond the code doc changes included in the PR. ### Additional Information An additional PR will be introduced for the keyboard experimentation plugin repository to add tests there (see test coverage above). This description will be updated with a link to that PR once it exists.
261 lines
7.2 KiB
TypeScript
261 lines
7.2 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2019 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
// Former goog.module ID: Blockly.blockRendering.PathObject
|
|
|
|
import type {BlockSvg} from '../../block_svg.js';
|
|
import type {Connection} from '../../connection.js';
|
|
import {RenderedConnection} from '../../rendered_connection.js';
|
|
import type {BlockStyle} from '../../theme.js';
|
|
import {Coordinate} from '../../utils/coordinate.js';
|
|
import * as dom from '../../utils/dom.js';
|
|
import {Svg} from '../../utils/svg.js';
|
|
import type {ConstantProvider} from './constants.js';
|
|
import type {IPathObject} from './i_path_object.js';
|
|
|
|
/**
|
|
* An object that handles creating and setting each of the SVG elements
|
|
* used by the renderer.
|
|
*/
|
|
export class PathObject implements IPathObject {
|
|
svgRoot: SVGElement;
|
|
svgPath: SVGElement;
|
|
|
|
constants: ConstantProvider;
|
|
style: BlockStyle;
|
|
|
|
/** Highlight paths associated with connections. */
|
|
private connectionHighlights = new WeakMap<RenderedConnection, SVGElement>();
|
|
|
|
/** Locations of connection highlights. */
|
|
private highlightOffsets = new WeakMap<RenderedConnection, Coordinate>();
|
|
|
|
/**
|
|
* @param root The root SVG element.
|
|
* @param style The style object to use for colouring.
|
|
* @param constants The renderer's constants.
|
|
*/
|
|
constructor(
|
|
root: SVGElement,
|
|
style: BlockStyle,
|
|
constants: ConstantProvider,
|
|
) {
|
|
this.constants = constants;
|
|
this.style = style;
|
|
this.svgRoot = root;
|
|
|
|
/** The primary path of the block. */
|
|
this.svgPath = dom.createSvgElement(
|
|
Svg.PATH,
|
|
{'class': 'blocklyPath'},
|
|
this.svgRoot,
|
|
);
|
|
|
|
this.setClass_('blocklyBlock', true);
|
|
}
|
|
|
|
/**
|
|
* Set the path generated by the renderer onto the respective SVG element.
|
|
*
|
|
* @param pathString The path.
|
|
*/
|
|
setPath(pathString: string) {
|
|
this.svgPath.setAttribute('d', pathString);
|
|
}
|
|
|
|
/**
|
|
* Flip the SVG paths in RTL.
|
|
*/
|
|
flipRTL() {
|
|
// Mirror the block's path.
|
|
this.svgPath.setAttribute('transform', 'scale(-1 1)');
|
|
}
|
|
|
|
/**
|
|
* Apply the stored colours to the block's path, taking into account whether
|
|
* the paths belong to a shadow block.
|
|
*
|
|
* @param block The source block.
|
|
*/
|
|
applyColour(block: BlockSvg) {
|
|
this.svgPath.setAttribute('stroke', this.style.colourTertiary);
|
|
this.svgPath.setAttribute('fill', this.style.colourPrimary);
|
|
|
|
this.updateShadow_(block.isShadow());
|
|
this.updateDisabled_(!block.isEnabled() || block.getInheritedDisabled());
|
|
}
|
|
|
|
/**
|
|
* Set the style.
|
|
*
|
|
* @param blockStyle The block style to use.
|
|
*/
|
|
setStyle(blockStyle: BlockStyle) {
|
|
this.style = blockStyle;
|
|
}
|
|
|
|
/**
|
|
* Add or remove the given CSS class on the path object's root SVG element.
|
|
*
|
|
* @param className The name of the class to add or remove
|
|
* @param add True if the class should be added. False if it should be
|
|
* removed.
|
|
*/
|
|
protected setClass_(className: string, add: boolean) {
|
|
if (!className) {
|
|
return;
|
|
}
|
|
if (add) {
|
|
dom.addClass(this.svgRoot, className);
|
|
} else {
|
|
dom.removeClass(this.svgRoot, className);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set whether the block shows a highlight or not. Block highlighting is
|
|
* often used to visually mark blocks currently being executed.
|
|
*
|
|
* @param enable True if highlighted.
|
|
*/
|
|
|
|
updateHighlighted(enable: boolean) {
|
|
if (enable) {
|
|
this.setClass_('blocklyHighlighted', true);
|
|
} else {
|
|
this.setClass_('blocklyHighlighted', false);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Updates the look of the block to reflect a shadow state.
|
|
*
|
|
* @param shadow True if the block is a shadow block.
|
|
*/
|
|
protected updateShadow_(shadow: boolean) {
|
|
if (shadow) {
|
|
this.setClass_('blocklyShadow', true);
|
|
this.svgPath.setAttribute('stroke', 'none');
|
|
this.svgPath.setAttribute('fill', this.style.colourSecondary);
|
|
} else {
|
|
this.setClass_('blocklyShadow', false);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Updates the look of the block to reflect a disabled state.
|
|
*
|
|
* @param disabled True if disabled.
|
|
*/
|
|
protected updateDisabled_(disabled: boolean) {
|
|
this.setClass_('blocklyDisabled', disabled);
|
|
this.setClass_('blocklyDisabledPattern', disabled);
|
|
}
|
|
|
|
/**
|
|
* Add or remove styling showing that a block is selected.
|
|
*
|
|
* @param enable True if selection is enabled, false otherwise.
|
|
*/
|
|
updateSelected(enable: boolean) {
|
|
this.setClass_('blocklySelected', enable);
|
|
}
|
|
|
|
/**
|
|
* Add or remove styling showing that a block is dragged over a delete area.
|
|
*
|
|
* @param enable True if the block is being dragged over a delete area, false
|
|
* otherwise.
|
|
*/
|
|
updateDraggingDelete(enable: boolean) {
|
|
this.setClass_('blocklyDraggingDelete', enable);
|
|
}
|
|
|
|
/**
|
|
* Add or remove styling showing that a block is an insertion marker.
|
|
*
|
|
* @param enable True if the block is an insertion marker, false otherwise.
|
|
*/
|
|
updateInsertionMarker(enable: boolean) {
|
|
this.setClass_('blocklyInsertionMarker', enable);
|
|
}
|
|
|
|
/**
|
|
* Add or remove styling showing that a block is movable.
|
|
*
|
|
* @param enable True if the block is movable, false otherwise.
|
|
*/
|
|
updateMovable(enable: boolean) {
|
|
this.setClass_('blocklyDraggable', enable);
|
|
}
|
|
|
|
/**
|
|
* Add or remove styling that shows that if the dragging block is dropped,
|
|
* this block will be replaced. If a shadow block, it will disappear.
|
|
* Otherwise it will bump.
|
|
*
|
|
* @param enable True if styling should be added.
|
|
*/
|
|
updateReplacementFade(enable: boolean) {
|
|
this.setClass_('blocklyReplaceable', enable);
|
|
}
|
|
|
|
/**
|
|
* Add or remove styling that shows that if the dragging block is dropped,
|
|
* this block will be connected to the input.
|
|
*
|
|
* @param _conn The connection on the input to highlight.
|
|
* @param _enable True if styling should be added.
|
|
*/
|
|
updateShapeForInputHighlight(_conn: Connection, _enable: boolean) {
|
|
// NOOP
|
|
}
|
|
|
|
/** Adds the given path as a connection highlight for the given connection. */
|
|
addConnectionHighlight(
|
|
connection: RenderedConnection,
|
|
connectionPath: string,
|
|
offset: Coordinate,
|
|
rtl: boolean,
|
|
): SVGElement {
|
|
const transformation =
|
|
`translate(${offset.x}, ${offset.y})` + (rtl ? ' scale(-1 1)' : '');
|
|
|
|
const previousHighlight = this.connectionHighlights.get(connection);
|
|
if (previousHighlight) {
|
|
// Since a connection already exists, make sure that its path and
|
|
// transform are correct.
|
|
previousHighlight.setAttribute('d', connectionPath);
|
|
previousHighlight.setAttribute('transform', transformation);
|
|
return previousHighlight;
|
|
}
|
|
|
|
const highlight = dom.createSvgElement(
|
|
Svg.PATH,
|
|
{
|
|
'id': connection.id,
|
|
'class': 'blocklyHighlightedConnectionPath',
|
|
'style': 'display: none;',
|
|
'd': connectionPath,
|
|
'transform': transformation,
|
|
},
|
|
this.svgRoot,
|
|
);
|
|
this.connectionHighlights.set(connection, highlight);
|
|
return highlight;
|
|
}
|
|
|
|
/**
|
|
* Removes any highlight associated with the given connection, if it exists.
|
|
*/
|
|
removeConnectionHighlight(connection: RenderedConnection) {
|
|
const highlight = this.connectionHighlights.get(connection);
|
|
if (!highlight) return;
|
|
dom.removeNode(highlight);
|
|
this.connectionHighlights.delete(connection);
|
|
}
|
|
}
|