Skip to content

Commit eeaad33

Browse files
refactor: move editable/navigable checks to navigation
Less indirection and more freedom for actions to pick finer grained preconditions.
1 parent 473e827 commit eeaad33

File tree

13 files changed

+99
-182
lines changed

13 files changed

+99
-182
lines changed

src/actions/action_menu.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,12 @@ export interface ScopeWithConnection extends ContextMenuRegistry.Scope {
3030
* Keyboard shortcut to show the action menu on Cmr/Ctrl/Alt+Enter key.
3131
*/
3232
export class ActionMenu {
33-
/**
34-
* Function provided by the navigation controller to say whether navigation
35-
* is allowed.
36-
*/
37-
private canCurrentlyNavigate: (ws: WorkspaceSvg) => boolean;
38-
3933
/**
4034
* Registration name for the keyboard shortcut.
4135
*/
4236
private shortcutName = Constants.SHORTCUT_NAMES.MENU;
4337

44-
constructor(
45-
private navigation: Navigation,
46-
canNavigate: (ws: WorkspaceSvg) => boolean,
47-
) {
48-
this.canCurrentlyNavigate = canNavigate;
49-
}
38+
constructor(private navigation: Navigation) {}
5039

5140
/**
5241
* Install this action.
@@ -68,7 +57,8 @@ export class ActionMenu {
6857
private registerShortcut() {
6958
const menuShortcut: ShortcutRegistry.KeyboardShortcut = {
7059
name: Constants.SHORTCUT_NAMES.MENU,
71-
preconditionFn: (workspace) => this.canCurrentlyNavigate(workspace),
60+
preconditionFn: (workspace) =>
61+
this.navigation.canCurrentlyNavigate(workspace),
7262
callback: (workspace) => {
7363
switch (this.navigation.getState(workspace)) {
7464
case Constants.STATE.WORKSPACE:

src/actions/arrow_navigation.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ const KeyCodes = BlocklyUtils.KeyCodes;
1717
* Class for registering shortcuts for navigating the workspace with arrow keys.
1818
*/
1919
export class ArrowNavigation {
20-
constructor(
21-
private navigation: Navigation,
22-
private canCurrentlyNavigate: (ws: WorkspaceSvg) => boolean,
23-
) {}
20+
constructor(private navigation: Navigation) {}
2421

2522
/**
2623
* Gives the cursor to the field to handle if the cursor is on a field.
@@ -56,7 +53,8 @@ export class ArrowNavigation {
5653
/** Go to the next location to the right. */
5754
right: {
5855
name: Constants.SHORTCUT_NAMES.RIGHT,
59-
preconditionFn: (workspace) => this.canCurrentlyNavigate(workspace),
56+
preconditionFn: (workspace) =>
57+
this.navigation.canCurrentlyNavigate(workspace),
6058
callback: (workspace, e, shortcut) => {
6159
const toolbox = workspace.getToolbox() as Toolbox;
6260
let isHandled = false;
@@ -93,7 +91,8 @@ export class ArrowNavigation {
9391
/** Go to the next location to the left. */
9492
left: {
9593
name: Constants.SHORTCUT_NAMES.LEFT,
96-
preconditionFn: (workspace) => this.canCurrentlyNavigate(workspace),
94+
preconditionFn: (workspace) =>
95+
this.navigation.canCurrentlyNavigate(workspace),
9796
callback: (workspace, e, shortcut) => {
9897
const toolbox = workspace.getToolbox() as Toolbox;
9998
let isHandled = false;
@@ -128,7 +127,8 @@ export class ArrowNavigation {
128127
/** Go down to the next location. */
129128
down: {
130129
name: Constants.SHORTCUT_NAMES.DOWN,
131-
preconditionFn: (workspace) => this.canCurrentlyNavigate(workspace),
130+
preconditionFn: (workspace) =>
131+
this.navigation.canCurrentlyNavigate(workspace),
132132
callback: (workspace, e, shortcut) => {
133133
const toolbox = workspace.getToolbox() as Toolbox;
134134
const flyout = workspace.getFlyout();
@@ -169,7 +169,8 @@ export class ArrowNavigation {
169169
/** Go up to the previous location. */
170170
up: {
171171
name: Constants.SHORTCUT_NAMES.UP,
172-
preconditionFn: (workspace) => this.canCurrentlyNavigate(workspace),
172+
preconditionFn: (workspace) =>
173+
this.navigation.canCurrentlyNavigate(workspace),
173174
callback: (workspace, e, shortcut) => {
174175
const flyout = workspace.getFlyout();
175176
const toolbox = workspace.getToolbox() as Toolbox;

src/actions/clipboard.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,7 @@ export class Clipboard {
4444
/** The workspace a copy or cut keyboard shortcut happened in. */
4545
private copyWorkspace: WorkspaceSvg | null = null;
4646

47-
/**
48-
* Function provided by the navigation controller to say whether editing
49-
* is allowed.
50-
*/
51-
private canCurrentlyEdit: (ws: WorkspaceSvg) => boolean;
52-
53-
constructor(
54-
private navigation: Navigation,
55-
canEdit: (ws: WorkspaceSvg) => boolean,
56-
) {
57-
this.canCurrentlyEdit = canEdit;
58-
}
47+
constructor(private navigation: Navigation) {}
5948

6049
/**
6150
* Install these actions as both keyboard shortcuts and context menu items.
@@ -141,7 +130,7 @@ export class Clipboard {
141130
* @returns True iff `cutCallback` function should be called.
142131
*/
143132
private cutPrecondition(workspace: WorkspaceSvg) {
144-
if (this.canCurrentlyEdit(workspace)) {
133+
if (this.navigation.canCurrentlyEdit(workspace)) {
145134
const curNode = workspace.getCursor()?.getCurNode();
146135
if (curNode && curNode.getSourceBlock()) {
147136
const sourceBlock = curNode.getSourceBlock();
@@ -236,7 +225,7 @@ export class Clipboard {
236225
* @returns True iff `copyCallback` function should be called.
237226
*/
238227
private copyPrecondition(workspace: WorkspaceSvg) {
239-
if (!this.canCurrentlyEdit(workspace)) return false;
228+
if (!this.navigation.canCurrentlyEdit(workspace)) return false;
240229
switch (this.navigation.getState(workspace)) {
241230
case Constants.STATE.WORKSPACE: {
242231
const curNode = workspace?.getCursor()?.getCurNode();
@@ -348,7 +337,7 @@ export class Clipboard {
348337
private pastePrecondition(workspace: WorkspaceSvg) {
349338
if (!this.copyData || !this.copyWorkspace) return false;
350339

351-
return this.canCurrentlyEdit(workspace) && !Gesture.inProgress();
340+
return this.navigation.canCurrentlyEdit(workspace) && !Gesture.inProgress();
352341
}
353342

354343
/**

src/actions/delete.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,12 @@ export class DeleteAction {
2828
*/
2929
private oldContextMenuItem: ContextMenuRegistry.RegistryItem | null = null;
3030

31-
/**
32-
* Function provided by the navigation controller to say whether editing
33-
* is allowed.
34-
*/
35-
private canCurrentlyEdit: (ws: WorkspaceSvg) => boolean;
36-
3731
/**
3832
* Registration name for the keyboard shortcut.
3933
*/
4034
private deleteShortcutName = Constants.SHORTCUT_NAMES.DELETE;
4135

42-
constructor(
43-
private navigation: Navigation,
44-
canEdit: (ws: WorkspaceSvg) => boolean,
45-
) {
46-
this.canCurrentlyEdit = canEdit;
47-
}
36+
constructor(private navigation: Navigation) {}
4837

4938
/**
5039
* Install this action as both a keyboard shortcut and a context menu item.
@@ -148,10 +137,11 @@ export class DeleteAction {
148137
* @returns True iff `deleteCallback` function should be called.
149138
*/
150139
private deletePrecondition(workspace: WorkspaceSvg) {
151-
if (!this.canCurrentlyEdit(workspace)) return false;
152-
153140
const sourceBlock = workspace.getCursor()?.getCurNode()?.getSourceBlock();
154-
return !!sourceBlock?.isDeletable();
141+
return (
142+
this.navigation.canCurrentlyEdit(workspace) &&
143+
!!sourceBlock?.isDeletable()
144+
);
155145
}
156146

157147
/**

src/actions/disconnect.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,12 @@ const KeyCodes = BlocklyUtils.KeyCodes;
2424
* item.
2525
*/
2626
export class DisconnectAction {
27-
/**
28-
* Function provided by the navigation controller to say whether editing
29-
* is allowed.
30-
*/
31-
private canCurrentlyEdit: (ws: WorkspaceSvg) => boolean;
32-
3327
/**
3428
* Registration name for the keyboard shortcut.
3529
*/
3630
private shortcutName = Constants.SHORTCUT_NAMES.DISCONNECT;
3731

38-
constructor(
39-
private navigation: Navigation,
40-
canEdit: (ws: WorkspaceSvg) => boolean,
41-
) {
42-
this.canCurrentlyEdit = canEdit;
43-
}
32+
constructor(private navigation: Navigation) {}
4433

4534
/**
4635
* Install this action as both a keyboard shortcut and a context menu item.
@@ -63,7 +52,8 @@ export class DisconnectAction {
6352
private registerShortcut() {
6453
const disconnectShortcut: ShortcutRegistry.KeyboardShortcut = {
6554
name: this.shortcutName,
66-
preconditionFn: (workspace) => this.canCurrentlyEdit(workspace),
55+
preconditionFn: (workspace) =>
56+
this.navigation.canCurrentlyEdit(workspace),
6757
callback: (workspace) => {
6858
switch (this.navigation.getState(workspace)) {
6959
case Constants.STATE.WORKSPACE:

src/actions/edit.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
*/
66

77
import {ContextMenuRegistry} from 'blockly';
8-
import type {WorkspaceSvg} from 'blockly';
98
import {LineCursor} from '../line_cursor';
9+
import {Navigation} from 'src/navigation';
1010

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

3434
/**
3535
* Install this action as a context menu item.
@@ -54,7 +54,7 @@ export class EditAction {
5454
displayText: 'Edit Block contents (→︎)',
5555
preconditionFn: (scope: ContextMenuRegistry.Scope) => {
5656
const workspace = scope.block?.workspace;
57-
if (!workspace || !this.canCurrentlyNavigate(workspace)) {
57+
if (!workspace || !this.navigation.canCurrentlyNavigate(workspace)) {
5858
return 'disabled';
5959
}
6060
const cursor = workspace.getCursor() as LineCursor | null;

src/actions/enter.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ const KeyCodes = BlocklyUtils.KeyCodes;
2929
* Class for registering a shortcut for the enter action.
3030
*/
3131
export class EnterAction {
32-
constructor(
33-
private navigation: Navigation,
34-
private canCurrentlyEdit: (ws: WorkspaceSvg) => boolean,
35-
) {}
32+
constructor(private navigation: Navigation) {}
3633

3734
/**
3835
* Adds the enter action shortcut to the registry.
@@ -47,7 +44,8 @@ export class EnterAction {
4744
*/
4845
ShortcutRegistry.registry.register({
4946
name: Constants.SHORTCUT_NAMES.EDIT_OR_CONFIRM,
50-
preconditionFn: (workspace) => this.canCurrentlyEdit(workspace),
47+
preconditionFn: (workspace) =>
48+
this.navigation.canCurrentlyEdit(workspace),
5149
callback: (workspace, event) => {
5250
event.preventDefault();
5351

src/actions/exit.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

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

9-
import type {WorkspaceSvg} from 'blockly/core';
10-
119
import * as Constants from '../constants';
1210
import type {Navigation} from '../navigation';
1311

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

2520
/**
2621
* Adds the exit action shortcut to the registry.
2722
*/
2823
install() {
2924
ShortcutRegistry.registry.register({
3025
name: Constants.SHORTCUT_NAMES.EXIT,
31-
preconditionFn: (workspace) => this.canCurrentlyNavigate(workspace),
26+
preconditionFn: (workspace) =>
27+
this.navigation.canCurrentlyNavigate(workspace),
3228
callback: (workspace) => {
3329
switch (this.navigation.getState(workspace)) {
3430
case Constants.STATE.FLYOUT:

src/actions/insert.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,12 @@ const KeyCodes = BlocklyUtils.KeyCodes;
2323
* item.
2424
*/
2525
export class InsertAction {
26-
/**
27-
* Function provided by the navigation controller to say whether editing
28-
* is allowed.
29-
*/
30-
private canCurrentlyEdit: (ws: WorkspaceSvg) => boolean;
31-
3226
/**
3327
* Registration name for the keyboard shortcut.
3428
*/
3529
private insertShortcutName = Constants.SHORTCUT_NAMES.INSERT;
3630

37-
constructor(
38-
private navigation: Navigation,
39-
canEdit: (ws: WorkspaceSvg) => boolean,
40-
) {
41-
this.canCurrentlyEdit = canEdit;
42-
}
31+
constructor(private navigation: Navigation) {}
4332

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

118107
/**

src/actions/mover.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ export class Mover {
5656
*/
5757
private oldDragStrategy: IDragStrategy | null = null;
5858

59-
constructor(
60-
protected navigation: Navigation,
61-
protected canEdit: (ws: WorkspaceSvg) => boolean,
62-
) {}
59+
constructor(protected navigation: Navigation) {}
6360

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

213210
return !!(
214211
this.navigation.getState(workspace) === Constants.STATE.WORKSPACE &&
215-
this.canEdit(workspace) &&
212+
this.navigation.canCurrentlyEdit(workspace) &&
216213
!this.moves.has(workspace) && // No move in progress.
217214
block?.isMovable()
218215
);
@@ -226,7 +223,9 @@ export class Mover {
226223
* @returns True iff we are moving.
227224
*/
228225
isMoving(workspace: WorkspaceSvg) {
229-
return this.canEdit(workspace) && this.moves.has(workspace);
226+
return (
227+
this.navigation.canCurrentlyEdit(workspace) && this.moves.has(workspace)
228+
);
230229
}
231230

232231
/**

0 commit comments

Comments
 (0)