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
16 changes: 3 additions & 13 deletions src/actions/action_menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,12 @@ export interface ScopeWithConnection extends ContextMenuRegistry.Scope {
* Keyboard shortcut to show the action menu on Cmr/Ctrl/Alt+Enter key.
*/
export class ActionMenu {
/**
* Function provided by the navigation controller to say whether navigation
* is allowed.
*/
private canCurrentlyNavigate: (ws: WorkspaceSvg) => boolean;

/**
* Registration name for the keyboard shortcut.
*/
private shortcutName = Constants.SHORTCUT_NAMES.MENU;

constructor(
private navigation: Navigation,
canNavigate: (ws: WorkspaceSvg) => boolean,
) {
this.canCurrentlyNavigate = canNavigate;
}
constructor(private navigation: Navigation) {}

/**
* Install this action.
Expand All @@ -68,7 +57,8 @@ export class ActionMenu {
private registerShortcut() {
const menuShortcut: ShortcutRegistry.KeyboardShortcut = {
name: Constants.SHORTCUT_NAMES.MENU,
preconditionFn: (workspace) => this.canCurrentlyNavigate(workspace),
preconditionFn: (workspace) =>
this.navigation.canCurrentlyNavigate(workspace),
callback: (workspace) => {
switch (this.navigation.getState(workspace)) {
case Constants.STATE.WORKSPACE:
Expand Down
17 changes: 9 additions & 8 deletions src/actions/arrow_navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ const KeyCodes = BlocklyUtils.KeyCodes;
* Class for registering shortcuts for navigating the workspace with arrow keys.
*/
export class ArrowNavigation {
constructor(
private navigation: Navigation,
private canCurrentlyNavigate: (ws: WorkspaceSvg) => boolean,
) {}
constructor(private navigation: Navigation) {}

/**
* Gives the cursor to the field to handle if the cursor is on a field.
Expand Down Expand Up @@ -56,7 +53,8 @@ export class ArrowNavigation {
/** Go to the next location to the right. */
right: {
name: Constants.SHORTCUT_NAMES.RIGHT,
preconditionFn: (workspace) => this.canCurrentlyNavigate(workspace),
preconditionFn: (workspace) =>
this.navigation.canCurrentlyNavigate(workspace),
callback: (workspace, e, shortcut) => {
const toolbox = workspace.getToolbox() as Toolbox;
let isHandled = false;
Expand Down Expand Up @@ -93,7 +91,8 @@ export class ArrowNavigation {
/** Go to the next location to the left. */
left: {
name: Constants.SHORTCUT_NAMES.LEFT,
preconditionFn: (workspace) => this.canCurrentlyNavigate(workspace),
preconditionFn: (workspace) =>
this.navigation.canCurrentlyNavigate(workspace),
callback: (workspace, e, shortcut) => {
const toolbox = workspace.getToolbox() as Toolbox;
let isHandled = false;
Expand Down Expand Up @@ -128,7 +127,8 @@ export class ArrowNavigation {
/** Go down to the next location. */
down: {
name: Constants.SHORTCUT_NAMES.DOWN,
preconditionFn: (workspace) => this.canCurrentlyNavigate(workspace),
preconditionFn: (workspace) =>
this.navigation.canCurrentlyNavigate(workspace),
callback: (workspace, e, shortcut) => {
const toolbox = workspace.getToolbox() as Toolbox;
const flyout = workspace.getFlyout();
Expand Down Expand Up @@ -169,7 +169,8 @@ export class ArrowNavigation {
/** Go up to the previous location. */
up: {
name: Constants.SHORTCUT_NAMES.UP,
preconditionFn: (workspace) => this.canCurrentlyNavigate(workspace),
preconditionFn: (workspace) =>
this.navigation.canCurrentlyNavigate(workspace),
callback: (workspace, e, shortcut) => {
const flyout = workspace.getFlyout();
const toolbox = workspace.getToolbox() as Toolbox;
Expand Down
19 changes: 4 additions & 15 deletions src/actions/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,7 @@ export class Clipboard {
/** The workspace a copy or cut keyboard shortcut happened in. */
private copyWorkspace: WorkspaceSvg | null = null;

/**
* Function provided by the navigation controller to say whether editing
* is allowed.
*/
private canCurrentlyEdit: (ws: WorkspaceSvg) => boolean;

constructor(
private navigation: Navigation,
canEdit: (ws: WorkspaceSvg) => boolean,
) {
this.canCurrentlyEdit = canEdit;
}
constructor(private navigation: Navigation) {}

/**
* Install these actions as both keyboard shortcuts and context menu items.
Expand Down Expand Up @@ -141,7 +130,7 @@ export class Clipboard {
* @returns True iff `cutCallback` function should be called.
*/
private cutPrecondition(workspace: WorkspaceSvg) {
if (this.canCurrentlyEdit(workspace)) {
if (this.navigation.canCurrentlyEdit(workspace)) {
const curNode = workspace.getCursor()?.getCurNode();
if (curNode && curNode.getSourceBlock()) {
const sourceBlock = curNode.getSourceBlock();
Expand Down Expand Up @@ -236,7 +225,7 @@ export class Clipboard {
* @returns True iff `copyCallback` function should be called.
*/
private copyPrecondition(workspace: WorkspaceSvg) {
if (!this.canCurrentlyEdit(workspace)) return false;
if (!this.navigation.canCurrentlyEdit(workspace)) return false;
switch (this.navigation.getState(workspace)) {
case Constants.STATE.WORKSPACE: {
const curNode = workspace?.getCursor()?.getCurNode();
Expand Down Expand Up @@ -348,7 +337,7 @@ export class Clipboard {
private pastePrecondition(workspace: WorkspaceSvg) {
if (!this.copyData || !this.copyWorkspace) return false;

return this.canCurrentlyEdit(workspace) && !Gesture.inProgress();
return this.navigation.canCurrentlyEdit(workspace) && !Gesture.inProgress();
}

/**
Expand Down
20 changes: 5 additions & 15 deletions src/actions/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,12 @@ export class DeleteAction {
*/
private oldContextMenuItem: ContextMenuRegistry.RegistryItem | null = null;

/**
* Function provided by the navigation controller to say whether editing
* is allowed.
*/
private canCurrentlyEdit: (ws: WorkspaceSvg) => boolean;

/**
* Registration name for the keyboard shortcut.
*/
private deleteShortcutName = Constants.SHORTCUT_NAMES.DELETE;

constructor(
private navigation: Navigation,
canEdit: (ws: WorkspaceSvg) => boolean,
) {
this.canCurrentlyEdit = canEdit;
}
constructor(private navigation: Navigation) {}

/**
* Install this action as both a keyboard shortcut and a context menu item.
Expand Down Expand Up @@ -148,10 +137,11 @@ export class DeleteAction {
* @returns True iff `deleteCallback` function should be called.
*/
private deletePrecondition(workspace: WorkspaceSvg) {
if (!this.canCurrentlyEdit(workspace)) return false;

const sourceBlock = workspace.getCursor()?.getCurNode()?.getSourceBlock();
return !!sourceBlock?.isDeletable();
return (
this.navigation.canCurrentlyEdit(workspace) &&
!!sourceBlock?.isDeletable()
);
}

/**
Expand Down
16 changes: 3 additions & 13 deletions src/actions/disconnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,12 @@ const KeyCodes = BlocklyUtils.KeyCodes;
* item.
*/
export class DisconnectAction {
/**
* Function provided by the navigation controller to say whether editing
* is allowed.
*/
private canCurrentlyEdit: (ws: WorkspaceSvg) => boolean;

/**
* Registration name for the keyboard shortcut.
*/
private shortcutName = Constants.SHORTCUT_NAMES.DISCONNECT;

constructor(
private navigation: Navigation,
canEdit: (ws: WorkspaceSvg) => boolean,
) {
this.canCurrentlyEdit = canEdit;
}
constructor(private navigation: Navigation) {}

/**
* Install this action as both a keyboard shortcut and a context menu item.
Expand All @@ -63,7 +52,8 @@ export class DisconnectAction {
private registerShortcut() {
const disconnectShortcut: ShortcutRegistry.KeyboardShortcut = {
name: this.shortcutName,
preconditionFn: (workspace) => this.canCurrentlyEdit(workspace),
preconditionFn: (workspace) =>
this.navigation.canCurrentlyEdit(workspace),
callback: (workspace) => {
switch (this.navigation.getState(workspace)) {
case Constants.STATE.WORKSPACE:
Expand Down
6 changes: 3 additions & 3 deletions src/actions/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*/

import {ContextMenuRegistry} from 'blockly';
import type {WorkspaceSvg} from 'blockly';
import {LineCursor} from '../line_cursor';
import {Navigation} from 'src/navigation';

/**
* Action to edit a block. This just moves the cursor to the first
Expand All @@ -29,7 +29,7 @@ import {LineCursor} from '../line_cursor';
* is already a corresponding "right" shortcut item.
*/
export class EditAction {
constructor(private canCurrentlyNavigate: (ws: WorkspaceSvg) => boolean) {}
constructor(private navigation: Navigation) {}

/**
* Install this action as a context menu item.
Expand All @@ -54,7 +54,7 @@ export class EditAction {
displayText: 'Edit Block contents (→︎)',
preconditionFn: (scope: ContextMenuRegistry.Scope) => {
const workspace = scope.block?.workspace;
if (!workspace || !this.canCurrentlyNavigate(workspace)) {
if (!workspace || !this.navigation.canCurrentlyNavigate(workspace)) {
return 'disabled';
}
const cursor = workspace.getCursor() as LineCursor | null;
Expand Down
8 changes: 3 additions & 5 deletions src/actions/enter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ const KeyCodes = BlocklyUtils.KeyCodes;
* Class for registering a shortcut for the enter action.
*/
export class EnterAction {
constructor(
private navigation: Navigation,
private canCurrentlyEdit: (ws: WorkspaceSvg) => boolean,
) {}
constructor(private navigation: Navigation) {}

/**
* Adds the enter action shortcut to the registry.
Expand All @@ -47,7 +44,8 @@ export class EnterAction {
*/
ShortcutRegistry.registry.register({
name: Constants.SHORTCUT_NAMES.EDIT_OR_CONFIRM,
preconditionFn: (workspace) => this.canCurrentlyEdit(workspace),
preconditionFn: (workspace) =>
this.navigation.canCurrentlyEdit(workspace),
callback: (workspace, event) => {
event.preventDefault();

Expand Down
10 changes: 3 additions & 7 deletions src/actions/exit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

import {ShortcutRegistry, utils as BlocklyUtils} from 'blockly/core';

import type {WorkspaceSvg} from 'blockly/core';

import * as Constants from '../constants';
import type {Navigation} from '../navigation';

Expand All @@ -17,18 +15,16 @@ const KeyCodes = BlocklyUtils.KeyCodes;
* Class for registering a shortcut for the exit action.
*/
export class ExitAction {
constructor(
private navigation: Navigation,
private canCurrentlyNavigate: (ws: WorkspaceSvg) => boolean,
) {}
constructor(private navigation: Navigation) {}

/**
* Adds the exit action shortcut to the registry.
*/
install() {
ShortcutRegistry.registry.register({
name: Constants.SHORTCUT_NAMES.EXIT,
preconditionFn: (workspace) => this.canCurrentlyNavigate(workspace),
preconditionFn: (workspace) =>
this.navigation.canCurrentlyNavigate(workspace),
callback: (workspace) => {
switch (this.navigation.getState(workspace)) {
case Constants.STATE.FLYOUT:
Expand Down
15 changes: 2 additions & 13 deletions src/actions/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,12 @@ const KeyCodes = BlocklyUtils.KeyCodes;
* item.
*/
export class InsertAction {
/**
* Function provided by the navigation controller to say whether editing
* is allowed.
*/
private canCurrentlyEdit: (ws: WorkspaceSvg) => boolean;

/**
* Registration name for the keyboard shortcut.
*/
private insertShortcutName = Constants.SHORTCUT_NAMES.INSERT;

constructor(
private navigation: Navigation,
canEdit: (ws: WorkspaceSvg) => boolean,
) {
this.canCurrentlyEdit = canEdit;
}
constructor(private navigation: Navigation) {}

/**
* Install this action as both a keyboard shortcut and a context menu item.
Expand Down Expand Up @@ -112,7 +101,7 @@ export class InsertAction {
* @returns True iff `insertCallback` function should be called.
*/
private insertPrecondition(workspace: WorkspaceSvg): boolean {
return this.canCurrentlyEdit(workspace);
return this.navigation.canCurrentlyEdit(workspace);
}

/**
Expand Down
11 changes: 5 additions & 6 deletions src/actions/mover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ export class Mover {
*/
private oldDragStrategy: IDragStrategy | null = null;

constructor(
protected navigation: Navigation,
protected canEdit: (ws: WorkspaceSvg) => boolean,
) {}
constructor(protected navigation: Navigation) {}

private shortcuts: ShortcutRegistry.KeyboardShortcut[] = [
// Begin and end move.
Expand Down Expand Up @@ -212,7 +209,7 @@ export class Mover {

return !!(
this.navigation.getState(workspace) === Constants.STATE.WORKSPACE &&
this.canEdit(workspace) &&
this.navigation.canCurrentlyEdit(workspace) &&
!this.moves.has(workspace) && // No move in progress.
block?.isMovable()
);
Expand All @@ -226,7 +223,9 @@ export class Mover {
* @returns True iff we are moving.
*/
isMoving(workspace: WorkspaceSvg) {
return this.canEdit(workspace) && this.moves.has(workspace);
return (
this.navigation.canCurrentlyEdit(workspace) && this.moves.has(workspace)
);
}

/**
Expand Down
Loading