Files
blockly/core/dialog.ts
Ben Henning f68081bf60 feat: Make fields focusable (#8923)
## The basics

- [x] I [validated my changes](https://developers.google.com/blockly/guides/contribute/core#making_and_verifying_a_change)

## The details
### Resolves

Fixes #8922
Fixes #8929
Fixes part of #8771

### Proposed Changes

This PR introduces support for fields to be focusable (and thus navigable with keyboard navigation when paired with downstream changes to `LineCursor` and the keyboard navigation plugin). This is a largely isolated change in how it fundamentally works:
- `Field` was updated to become an `IFocusableNode`. Note that it uses a specific string-based ID schema in order to ensure that it can be properly linked back to its unique block (which helps make the search for the field in `WorkspaceSvg` a bit more efficient). This could be done with a globally unique ID, instead, but all fields would need to be searched vs. just those for the field's parent block.
- The drop-down and widget divs have been updated to manage ephemeral focus with `FocusManager` when they're open for non-system dialogs (ephemeral focus isn't needed for system dialogs/prompts since those already take/restore focus in a native way that `FocusManager` will respond to--this may require future work, however, if the restoration causes unexpected behavior for users). This approach was done due to a suggestion from @maribethb as the alternative would be a more complicated breaking change (forcing `Field` subclasses to properly manage ephemeral focus). It may still be the case that certain cases will need to do so, but widget and drop-down divs seem to address the majority of possibilities.

**Important**: `Input`s are not explicitly being supported here. As far as I can tell, we can't run into a case where `LineCursor` tries to set an input node, though perhaps I simply haven't come across this case. Supporting `Fields` and `Connections` (per #8928) seems to cover the main needed cases, though making `Input`s focusable may be a future requirement.

### Reason for Changes

This is part of an ongoing effort to ensure key components of Blockly are focusable so that they can be keyboard-navigable (with other needed changes yet both in Core Blockly and the keyboard navigation plugin).

Note that #8929 isn't broadly addressed since making widget & drop down divs manage ephemeral focus directly addresses a large class of cases. Additional cases may arise where a plugin or Blockly integration may require additional effort to make keyboard navigation work for their field--this may be best addressed with documentation and guidance.

### Test Coverage

No new tests have been added. It's certainly possible to add unit tests for the focusable configurations being introduced in this PR, but it may not be highly beneficial. It's largely assumed that the individual implementations should work due to a highly tested FocusManager, and it may be the case that the interactions of the components working together is far more important to verify (that is, the end user flows). The latter is planned to be tackled as part of #8915.

### Documentation

No new documentation is planned, however it may be prudent to update the field documentation in the future to explain how to utilize ephemeral focus when specifically building compatibility for keyboard navigation.

### Additional Information

This includes changes that have been pulled from #8875.
2025-04-30 15:54:21 -07:00

168 lines
4.8 KiB
TypeScript

/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
// Former goog.module ID: Blockly.dialog
import type {ToastOptions} from './toast.js';
import {Toast} from './toast.js';
import type {WorkspaceSvg} from './workspace_svg.js';
const defaultAlert = function (message: string, opt_callback?: () => void) {
window.alert(message);
if (opt_callback) {
opt_callback();
}
};
let alertImplementation = defaultAlert;
const defaultConfirm = function (
message: string,
callback: (result: boolean) => void,
) {
callback(window.confirm(message));
};
let confirmImplementation = defaultConfirm;
const defaultPrompt = function (
message: string,
defaultValue: string,
callback: (result: string | null) => void,
) {
// NOTE TO DEVELOPER: Ephemeral focus doesn't need to be taken for the native
// window prompt since it prevents focus from changing while open.
callback(window.prompt(message, defaultValue));
};
let promptImplementation = defaultPrompt;
const defaultToast = Toast.show.bind(Toast);
let toastImplementation = defaultToast;
/**
* Wrapper to window.alert() that app developers may override via setAlert to
* provide alternatives to the modal browser window.
*
* @param message The message to display to the user.
* @param opt_callback The callback when the alert is dismissed.
*/
export function alert(message: string, opt_callback?: () => void) {
alertImplementation(message, opt_callback);
}
/**
* Sets the function to be run when Blockly.dialog.alert() is called.
*
* @param alertFunction The function to be run, or undefined to restore the
* default implementation.
* @see Blockly.dialog.alert
*/
export function setAlert(
alertFunction: (
message: string,
callback?: () => void,
) => void = defaultAlert,
) {
alertImplementation = alertFunction;
}
/**
* Wrapper to window.confirm() that app developers may override via setConfirm
* to provide alternatives to the modal browser window.
*
* @param message The message to display to the user.
* @param callback The callback for handling user response.
*/
export function confirm(message: string, callback: (result: boolean) => void) {
confirmImplementation(message, callback);
}
/**
* Sets the function to be run when Blockly.dialog.confirm() is called.
*
* @param confirmFunction The function to be run, or undefined to restore the
* default implementation.
* @see Blockly.dialog.confirm
*/
export function setConfirm(
confirmFunction: (
message: string,
callback: (result: boolean) => void,
) => void = defaultConfirm,
) {
confirmImplementation = confirmFunction;
}
/**
* Wrapper to window.prompt() that app developers may override via setPrompt to
* provide alternatives to the modal browser window. Built-in browser prompts
* are often used for better text input experience on mobile device. We strongly
* recommend testing mobile when overriding this.
*
* @param message The message to display to the user.
* @param defaultValue The value to initialize the prompt with.
* @param callback The callback for handling user response.
*/
export function prompt(
message: string,
defaultValue: string,
callback: (result: string | null) => void,
) {
promptImplementation(message, defaultValue, callback);
}
/**
* Sets the function to be run when Blockly.dialog.prompt() is called.
*
* **Important**: When overridding this, be aware that non-native prompt
* experiences may require managing ephemeral focus in FocusManager. This isn't
* needed for the native window prompt because it prevents focus from being
* changed while open.
*
* @param promptFunction The function to be run, or undefined to restore the
* default implementation.
* @see Blockly.dialog.prompt
*/
export function setPrompt(
promptFunction: (
message: string,
defaultValue: string,
callback: (result: string | null) => void,
) => void = defaultPrompt,
) {
promptImplementation = promptFunction;
}
/**
* Displays a temporary notification atop the workspace. Blockly provides a
* default toast implementation, but developers may provide their own via
* setToast. For simple appearance customization, CSS should be sufficient.
*
* @param workspace The workspace to display the toast notification atop.
* @param options Configuration options for the notification, including its
* message and duration.
*/
export function toast(workspace: WorkspaceSvg, options: ToastOptions) {
toastImplementation(workspace, options);
}
/**
* Sets the function to be run when Blockly.dialog.toast() is called.
*
* @param toastFunction The function to be run, or undefined to restore the
* default implementation.
* @see Blockly.dialog.toast
*/
export function setToast(
toastFunction: (
workspace: WorkspaceSvg,
options: ToastOptions,
) => void = defaultToast,
) {
toastImplementation = toastFunction;
}