Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/actions/action_menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class ActionMenu {
);
},
callback: (workspace) => {
switch (this.navigation.getState(workspace)) {
switch (this.navigation.getState()) {
case Constants.STATE.WORKSPACE:
case Constants.STATE.FLYOUT:
return this.openActionMenu(workspace);
Expand Down
8 changes: 4 additions & 4 deletions src/actions/arrow_navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class ArrowNavigation {
? workspace.targetWorkspace?.getFlyout()
: workspace.getFlyout();
let isHandled = false;
switch (this.navigation.getState(workspace)) {
switch (this.navigation.getState()) {
case Constants.STATE.WORKSPACE:
isHandled = this.fieldShortcutHandler(workspace, shortcut);
if (!isHandled && workspace) {
Expand Down Expand Up @@ -97,7 +97,7 @@ export class ArrowNavigation {
? workspace.targetWorkspace?.getToolbox()
: workspace.getToolbox();
let isHandled = false;
switch (this.navigation.getState(workspace)) {
switch (this.navigation.getState()) {
case Constants.STATE.WORKSPACE:
isHandled = this.fieldShortcutHandler(workspace, shortcut);
if (!isHandled && workspace) {
Expand Down Expand Up @@ -161,7 +161,7 @@ export class ArrowNavigation {
callback: (workspace, e, shortcut) => {
keyboardNavigationController.setIsActive(true);
let isHandled = false;
switch (this.navigation.getState(workspace)) {
switch (this.navigation.getState()) {
case Constants.STATE.WORKSPACE:
isHandled = this.fieldShortcutHandler(workspace, shortcut);
if (!isHandled && workspace) {
Expand Down Expand Up @@ -223,7 +223,7 @@ export class ArrowNavigation {
callback: (workspace, e, shortcut) => {
keyboardNavigationController.setIsActive(true);
let isHandled = false;
switch (this.navigation.getState(workspace)) {
switch (this.navigation.getState()) {
case Constants.STATE.WORKSPACE:
isHandled = this.fieldShortcutHandler(workspace, shortcut);
if (!isHandled) {
Expand Down
4 changes: 3 additions & 1 deletion src/actions/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,9 @@ export class Clipboard {
!!this.oldCopyShortcut?.callback &&
this.oldCopyShortcut.callback(workspace, e, shortcut, scope);
if (didCopy) {
this.copyWorkspace = workspace;
this.copyWorkspace = workspace.isFlyout
? workspace.targetWorkspace
: workspace;
showCopiedHint(workspace);
}
return didCopy;
Expand Down
2 changes: 1 addition & 1 deletion src/actions/disconnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
Events,
ShortcutRegistry,
utils as BlocklyUtils,
Connection,

Check warning on line 12 in src/actions/disconnect.ts

View workflow job for this annotation

GitHub Actions / Eslint check

'Connection' is defined but never used
ConnectionType,

Check warning on line 13 in src/actions/disconnect.ts

View workflow job for this annotation

GitHub Actions / Eslint check

'ConnectionType' is defined but never used
keyboardNavigationController,
} from 'blockly';
import * as Constants from '../constants';
Expand Down Expand Up @@ -58,7 +58,7 @@
this.navigation.canCurrentlyEdit(workspace),
callback: (workspace) => {
keyboardNavigationController.setIsActive(true);
switch (this.navigation.getState(workspace)) {
switch (this.navigation.getState()) {
case Constants.STATE.WORKSPACE:
this.disconnectBlocks(workspace);
return true;
Expand Down
75 changes: 58 additions & 17 deletions src/actions/enter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

import {
Events,
Msg,
ShortcutRegistry,
utils as BlocklyUtils,
getFocusManager,

Check warning on line 11 in src/actions/enter.ts

View workflow job for this annotation

GitHub Actions / Eslint check

'getFocusManager' is defined but never used
BlockSvg,
FlyoutButton,
RenderedConnection,
Expand Down Expand Up @@ -54,33 +53,48 @@
*/
ShortcutRegistry.registry.register({
name: Constants.SHORTCUT_NAMES.EDIT_OR_CONFIRM,
preconditionFn: (workspace) =>
this.navigation.canCurrentlyEdit(workspace),
callback: (workspace, event) => {
preconditionFn: (workspace): boolean => {
switch (this.navigation.getState()) {
case Constants.STATE.WORKSPACE:
return this.shouldHandleEnterForWS(workspace);
case Constants.STATE.FLYOUT: {
// If we're in the flyout the only supported actions are inserting
// blocks or clicking buttons, so don't handle this if the
// main workspace is read only.
const targetWorkspace = workspace.isFlyout
? workspace.targetWorkspace
: workspace;
return !!targetWorkspace && !targetWorkspace.isReadOnly();
}
default:
return false;
}
},
callback: (workspace, event): boolean => {
event.preventDefault();

const targetWorkspace = workspace.isFlyout
? workspace.targetWorkspace
: workspace;
if (!targetWorkspace) return false;

let flyoutCursor;
let curNode;

switch (this.navigation.getState(workspace)) {
switch (this.navigation.getState()) {
case Constants.STATE.WORKSPACE:
this.handleEnterForWS(workspace);
return true;
return this.handleEnterForWS(workspace);
case Constants.STATE.FLYOUT:
if (!workspace.targetWorkspace) return false;
flyoutCursor = this.navigation.getFlyoutCursor(
workspace.targetWorkspace,
);
flyoutCursor = this.navigation.getFlyoutCursor(targetWorkspace);
if (!flyoutCursor) {
return false;
}
curNode = flyoutCursor.getCurNode();
if (curNode instanceof BlockSvg) {
this.insertFromFlyout(workspace.targetWorkspace);
this.insertFromFlyout(targetWorkspace);
} else if (curNode instanceof FlyoutButton) {
this.triggerButtonCallback(workspace);
}

return true;
default:
return false;
Expand All @@ -90,30 +104,57 @@
});
}

/**
* Checks if the enter key should do anything for this ws.
*
* @param workspace The workspace to check.
* @returns True if the enter action should be handled.
*/
private shouldHandleEnterForWS(workspace: WorkspaceSvg): boolean {
const cursor = workspace.getCursor();
const curNode = cursor?.getCurNode();
if (!curNode) return false;
if (curNode instanceof Field) return curNode.isClickable();
if (
curNode instanceof RenderedConnection ||
curNode instanceof WorkspaceSvg
) {
return !workspace.isReadOnly();
}
// Returning true is sometimes incorrect for icons, but there's no API to check.
if (curNode instanceof icons.Icon) return true;
return false;
}

/**
* Handles hitting the enter key on the workspace.
*
* @param workspace The workspace.
* @returns True if the enter was handled, false otherwise.
*/
private handleEnterForWS(workspace: WorkspaceSvg) {
private handleEnterForWS(workspace: WorkspaceSvg): boolean {
const cursor = workspace.getCursor();
if (!cursor) return;
const curNode = cursor.getCurNode();
if (!curNode) return;
const curNode = cursor?.getCurNode();
if (!curNode) return false;
if (curNode instanceof Field) {
curNode.showEditor();
return true;
} else if (curNode instanceof BlockSvg) {
if (!this.tryShowFullBlockFieldEditor(curNode)) {
showHelpHint(workspace);
}
return true;
} else if (
curNode instanceof RenderedConnection ||
curNode instanceof WorkspaceSvg
) {
this.navigation.openToolboxOrFlyout(workspace);
return true;
} else if (curNode instanceof icons.Icon) {
curNode.onClick();
return true;
}
return false;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/actions/exit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class ExitAction {
preconditionFn: (workspace) =>
this.navigation.canCurrentlyNavigate(workspace),
callback: (workspace) => {
switch (this.navigation.getState(workspace)) {
switch (this.navigation.getState()) {
case Constants.STATE.FLYOUT:
case Constants.STATE.TOOLBOX:
getFocusManager().focusTree(workspace.targetWorkspace ?? workspace);
Expand Down
2 changes: 1 addition & 1 deletion src/actions/mover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class Mover {
*/
canMove(workspace: WorkspaceSvg, block: BlockSvg) {
return !!(
this.navigation.getState(workspace) === Constants.STATE.WORKSPACE &&
this.navigation.getState() === Constants.STATE.WORKSPACE &&
this.navigation.canCurrentlyEdit(workspace) &&
!this.moves.has(workspace) && // No move in progress.
block?.isMovable()
Expand Down
11 changes: 8 additions & 3 deletions src/actions/ws_movement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* The distance to move the cursor when the cursor is on the workspace.
*/
const WS_MOVE_DISTANCE = 40;

Check warning on line 24 in src/actions/ws_movement.ts

View workflow job for this annotation

GitHub Actions / Eslint check

'WS_MOVE_DISTANCE' is assigned a value but never used

/**
* Logic for free movement of the cursor on the workspace with keyboard
Expand Down Expand Up @@ -68,11 +68,16 @@
/** Move the cursor to the workspace. */
{
name: Constants.SHORTCUT_NAMES.CREATE_WS_CURSOR,
preconditionFn: (workspace) =>
this.navigation.canCurrentlyEdit(workspace),
preconditionFn: (workspace) => {
return true;
},
callback: (workspace) => {
const targetWorkspace = workspace.isFlyout
? workspace.targetWorkspace
: workspace;
if (!targetWorkspace) return false;
keyboardNavigationController.setIsActive(true);
return this.createWSCursor(workspace);
return this.createWSCursor(targetWorkspace);
},
keyCodes: [KeyCodes.W],
},
Expand Down
11 changes: 4 additions & 7 deletions src/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,9 @@ export class Navigation {
* Note that this assumes a workspace with passive focus (including for its
* toolbox or flyout) has a state of NOWHERE.
*
* @param workspace The workspace to get the state of.
* @returns The state of the given workspace.
*/
getState(workspace: Blockly.WorkspaceSvg): Constants.STATE {
getState(): Constants.STATE {
const focusedTree = Blockly.getFocusManager().getFocusedTree();
if (focusedTree instanceof Blockly.WorkspaceSvg) {
if (focusedTree.isFlyout) {
Expand All @@ -105,9 +104,7 @@ export class Navigation {
return Constants.STATE.WORKSPACE;
}
} else if (focusedTree instanceof Blockly.Toolbox) {
if (workspace === focusedTree.getWorkspace()) {
return Constants.STATE.TOOLBOX;
}
return Constants.STATE.TOOLBOX;
} else if (focusedTree instanceof Blockly.Flyout) {
return Constants.STATE.FLYOUT;
}
Expand Down Expand Up @@ -222,7 +219,7 @@ export class Navigation {
}
} else if (
e.type === Blockly.Events.BLOCK_CREATE &&
this.getState(mainWorkspace) === Constants.STATE.FLYOUT
this.getState() === Constants.STATE.FLYOUT
) {
// When variables are created, that recreates the flyout contents, leaving the
// cursor in an invalid state.
Expand Down Expand Up @@ -825,7 +822,7 @@ export class Navigation {
: workspace.keyboardAccessibilityMode;
return (
!!accessibilityMode &&
this.getState(workspace) !== Constants.STATE.NOWHERE &&
this.getState() !== Constants.STATE.NOWHERE &&
!Blockly.getFocusManager().ephemeralFocusTaken()
);
}
Expand Down
5 changes: 2 additions & 3 deletions src/navigation_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,10 @@ export class NavigationController {
/** Move focus to or from the toolbox. */
focusToolbox: {
name: Constants.SHORTCUT_NAMES.TOOLBOX,
preconditionFn: (workspace) =>
!workspace.isDragging() && this.navigation.canCurrentlyEdit(workspace),
preconditionFn: (workspace) => !workspace.isDragging(),
callback: (workspace) => {
keyboardNavigationController.setIsActive(true);
switch (this.navigation.getState(workspace)) {
switch (this.navigation.getState()) {
case Constants.STATE.WORKSPACE:
Blockly.getFocusManager().focusTree(
workspace.getToolbox() ??
Expand Down
13 changes: 11 additions & 2 deletions test/webdriverio/test/keyboard_mode_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
PAUSE_TIME,
getBlockElementById,
tabNavigateToWorkspace,
clickBlock,
} from './test_setup.js';
import {Key} from 'webdriverio';

Expand Down Expand Up @@ -125,8 +126,7 @@ suite(

await this.browser.pause(PAUSE_TIME);
// Right click a block
const element = await getBlockElementById(this.browser, 'controls_if_1');
await element.click({button: 'right'});
clickBlock(this.browser, 'controls_if_1', {button: 'right'});
await this.browser.pause(PAUSE_TIME);

chai.assert.isFalse(await isKeyboardNavigating(this.browser));
Expand All @@ -140,6 +140,15 @@ suite(
await this.browser.pause(PAUSE_TIME);
// Drag a block
const element = await getBlockElementById(this.browser, 'controls_if_1');

await this.browser.execute(() => {
const ws = Blockly.getMainWorkspace() as Blockly.WorkspaceSvg;
const block = ws.getBlockById('controls_if_1') as Blockly.BlockSvg;
ws.scrollBoundsIntoView(
block.getBoundingRectangleWithoutChildren(),
10,
);
});
await element.dragAndDrop({x: 10, y: 10});
await this.browser.pause(PAUSE_TIME);

Expand Down
56 changes: 56 additions & 0 deletions test/webdriverio/test/test_setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,59 @@ export async function contextMenuExists(
const item = await browser.$(`div=${itemText}`);
return await item.waitForExist({timeout: 200, reverse: reverse});
}

/**
* Find a clickable element on the block and click it.
* We can't always use the block's SVG root because clicking will always happen
* in the middle of the block's bounds (including children) by default, which
* causes problems if it has holes (e.g. statement inputs). Instead, this tries
* to get the first text field on the block. It falls back on the block's SVG root.
*
* @param browser The active WebdriverIO Browser object.
* @param blockId The id of the block to click, as an interactable element.
* @param clickOptions The options to pass to webdriverio's element.click function.
* @return A Promise that resolves when the actions are completed.
*/
export async function clickBlock(
browser: WebdriverIO.Browser,
blockId: string,
clickOptions?: Partial<webdriverio.ClickOptions> | undefined,
) {
const findableId = 'clickTargetElement';
// In the browser context, find the element that we want and give it a findable ID.
await browser.execute(
(blockId, newElemId) => {
const ws = Blockly.getMainWorkspace() as Blockly.WorkspaceSvg;
const block = ws.getBlockById(blockId) as Blockly.BlockSvg;
// Ensure the block we want to click is within the viewport.
ws.scrollBoundsIntoView(block.getBoundingRectangleWithoutChildren(), 10);
if (!block.isCollapsed()) {
for (const input of block.inputList) {
for (const field of input.fieldRow) {
if (field instanceof Blockly.FieldLabel) {
const svgRoot = field.getSvgRoot();
if (svgRoot) {
svgRoot.id = newElemId;
return;
}
}
}
}
}
// No label field found. Fall back to the block's SVG root.
block.getSvgRoot().id = newElemId;
},
blockId,
findableId,
);

// In the test context, get the Webdriverio Element that we've identified.
const elem = await browser.$(`#${findableId}`);

await elem.click(clickOptions);

// In the browser context, remove the ID.
await browser.execute((elemId) => {
document.getElementById(elemId)?.removeAttribute('id');
}, findableId);
}
Loading