Skip to content

Commit 6c865bc

Browse files
committed
Allow some things to handle return on read only workspaces
1 parent 9130145 commit 6c865bc

File tree

9 files changed

+55
-33
lines changed

9 files changed

+55
-33
lines changed

src/actions/action_menu.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class ActionMenu {
5252
);
5353
},
5454
callback: (workspace) => {
55-
switch (this.navigation.getState(workspace)) {
55+
switch (this.navigation.getState()) {
5656
case Constants.STATE.WORKSPACE:
5757
case Constants.STATE.FLYOUT:
5858
return this.openActionMenu(workspace);

src/actions/arrow_navigation.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class ArrowNavigation {
6363
? workspace.targetWorkspace?.getFlyout()
6464
: workspace.getFlyout();
6565
let isHandled = false;
66-
switch (this.navigation.getState(workspace)) {
66+
switch (this.navigation.getState()) {
6767
case Constants.STATE.WORKSPACE:
6868
isHandled = this.fieldShortcutHandler(workspace, shortcut);
6969
if (!isHandled && workspace) {
@@ -97,7 +97,7 @@ export class ArrowNavigation {
9797
? workspace.targetWorkspace?.getToolbox()
9898
: workspace.getToolbox();
9999
let isHandled = false;
100-
switch (this.navigation.getState(workspace)) {
100+
switch (this.navigation.getState()) {
101101
case Constants.STATE.WORKSPACE:
102102
isHandled = this.fieldShortcutHandler(workspace, shortcut);
103103
if (!isHandled && workspace) {
@@ -161,7 +161,7 @@ export class ArrowNavigation {
161161
callback: (workspace, e, shortcut) => {
162162
keyboardNavigationController.setIsActive(true);
163163
let isHandled = false;
164-
switch (this.navigation.getState(workspace)) {
164+
switch (this.navigation.getState()) {
165165
case Constants.STATE.WORKSPACE:
166166
isHandled = this.fieldShortcutHandler(workspace, shortcut);
167167
if (!isHandled && workspace) {
@@ -223,7 +223,7 @@ export class ArrowNavigation {
223223
callback: (workspace, e, shortcut) => {
224224
keyboardNavigationController.setIsActive(true);
225225
let isHandled = false;
226-
switch (this.navigation.getState(workspace)) {
226+
switch (this.navigation.getState()) {
227227
case Constants.STATE.WORKSPACE:
228228
isHandled = this.fieldShortcutHandler(workspace, shortcut);
229229
if (!isHandled) {

src/actions/clipboard.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@ export class Clipboard {
207207
!!this.oldCutShortcut?.callback &&
208208
this.oldCutShortcut.callback(workspace, e, shortcut, scope);
209209
if (didCut) {
210-
this.copyWorkspace = workspace;
210+
this.copyWorkspace = workspace.isFlyout
211+
? workspace.targetWorkspace
212+
: workspace;
211213
showCutHint(workspace);
212214
}
213215
return didCut;
@@ -285,7 +287,9 @@ export class Clipboard {
285287
!!this.oldCopyShortcut?.callback &&
286288
this.oldCopyShortcut.callback(workspace, e, shortcut, scope);
287289
if (didCopy) {
288-
this.copyWorkspace = workspace;
290+
this.copyWorkspace = workspace.isFlyout
291+
? workspace.targetWorkspace
292+
: workspace;
289293
showCopiedHint(workspace);
290294
}
291295
return didCopy;

src/actions/disconnect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class DisconnectAction {
5858
this.navigation.canCurrentlyEdit(workspace),
5959
callback: (workspace) => {
6060
keyboardNavigationController.setIsActive(true);
61-
switch (this.navigation.getState(workspace)) {
61+
switch (this.navigation.getState()) {
6262
case Constants.STATE.WORKSPACE:
6363
this.disconnectBlocks(workspace);
6464
return true;

src/actions/enter.ts

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

77
import {
88
Events,
9-
Msg,
109
ShortcutRegistry,
1110
utils as BlocklyUtils,
1211
getFocusManager,
@@ -54,33 +53,53 @@ export class EnterAction {
5453
*/
5554
ShortcutRegistry.registry.register({
5655
name: Constants.SHORTCUT_NAMES.EDIT_OR_CONFIRM,
57-
preconditionFn: (workspace) =>
58-
this.navigation.canCurrentlyEdit(workspace),
59-
callback: (workspace, event) => {
56+
preconditionFn: (workspace): boolean => {
57+
switch (this.navigation.getState()) {
58+
case Constants.STATE.WORKSPACE:
59+
// The main workspace may or may not handle it depending on what's
60+
// selected, so always pass it through to the callback.
61+
return true;
62+
case Constants.STATE.FLYOUT: {
63+
// If we're in the flyout the only supported actions are inserting
64+
// blocks or clicking buttons, so don't handle this if the
65+
// main workspace is read only.
66+
const targetWorkspace = workspace.isFlyout
67+
? workspace.targetWorkspace
68+
: workspace;
69+
return !!targetWorkspace && !targetWorkspace.isReadOnly();
70+
}
71+
default:
72+
return false;
73+
}
74+
},
75+
callback: (workspace, event): boolean => {
6076
event.preventDefault();
6177

78+
const targetWorkspace = workspace.isFlyout
79+
? workspace.targetWorkspace
80+
: workspace;
81+
if (!targetWorkspace) return false;
82+
6283
let flyoutCursor;
6384
let curNode;
6485

65-
switch (this.navigation.getState(workspace)) {
86+
switch (this.navigation.getState()) {
6687
case Constants.STATE.WORKSPACE:
67-
this.handleEnterForWS(workspace);
68-
return true;
88+
return this.handleEnterForWS(workspace);
6989
case Constants.STATE.FLYOUT:
70-
if (!workspace.targetWorkspace) return false;
90+
if (targetWorkspace.isReadOnly()) return false;
7191
flyoutCursor = this.navigation.getFlyoutCursor(
72-
workspace.targetWorkspace,
92+
targetWorkspace,
7393
);
7494
if (!flyoutCursor) {
7595
return false;
7696
}
7797
curNode = flyoutCursor.getCurNode();
7898
if (curNode instanceof BlockSvg) {
79-
this.insertFromFlyout(workspace.targetWorkspace);
99+
this.insertFromFlyout(targetWorkspace);
80100
} else if (curNode instanceof FlyoutButton) {
81101
this.triggerButtonCallback(workspace);
82102
}
83-
84103
return true;
85104
default:
86105
return false;
@@ -94,12 +113,13 @@ export class EnterAction {
94113
* Handles hitting the enter key on the workspace.
95114
*
96115
* @param workspace The workspace.
116+
* @returns True if the enter was handled, false otherwise.
97117
*/
98-
private handleEnterForWS(workspace: WorkspaceSvg) {
118+
private handleEnterForWS(workspace: WorkspaceSvg): boolean {
99119
const cursor = workspace.getCursor();
100-
if (!cursor) return;
120+
if (!cursor) return false;
101121
const curNode = cursor.getCurNode();
102-
if (!curNode) return;
122+
if (!curNode) return false;
103123
if (curNode instanceof Field) {
104124
curNode.showEditor();
105125
} else if (curNode instanceof BlockSvg) {
@@ -114,6 +134,7 @@ export class EnterAction {
114134
} else if (curNode instanceof icons.Icon) {
115135
curNode.onClick();
116136
}
137+
return true;
117138
}
118139

119140
/**
@@ -278,7 +299,7 @@ export class EnterAction {
278299
if (block.isSimpleReporter()) {
279300
for (const input of block.inputList) {
280301
for (const field of input.fieldRow) {
281-
if (field.isClickable() && field.isFullBlockField()) {
302+
if (field.isFullBlockField()) {
282303
field.showEditor();
283304
return true;
284305
}

src/actions/exit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class ExitAction {
3131
preconditionFn: (workspace) =>
3232
this.navigation.canCurrentlyNavigate(workspace),
3333
callback: (workspace) => {
34-
switch (this.navigation.getState(workspace)) {
34+
switch (this.navigation.getState()) {
3535
case Constants.STATE.FLYOUT:
3636
case Constants.STATE.TOOLBOX:
3737
getFocusManager().focusTree(workspace.targetWorkspace ?? workspace);

src/actions/mover.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export class Mover {
8585
*/
8686
canMove(workspace: WorkspaceSvg, block: BlockSvg) {
8787
return !!(
88-
this.navigation.getState(workspace) === Constants.STATE.WORKSPACE &&
88+
this.navigation.getState() === Constants.STATE.WORKSPACE &&
8989
this.navigation.canCurrentlyEdit(workspace) &&
9090
!this.moves.has(workspace) && // No move in progress.
9191
block?.isMovable()

src/navigation.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,9 @@ export class Navigation {
9393
* Note that this assumes a workspace with passive focus (including for its
9494
* toolbox or flyout) has a state of NOWHERE.
9595
*
96-
* @param workspace The workspace to get the state of.
9796
* @returns The state of the given workspace.
9897
*/
99-
getState(workspace: Blockly.WorkspaceSvg): Constants.STATE {
98+
getState(): Constants.STATE {
10099
const focusedTree = Blockly.getFocusManager().getFocusedTree();
101100
if (focusedTree instanceof Blockly.WorkspaceSvg) {
102101
if (focusedTree.isFlyout) {
@@ -105,9 +104,7 @@ export class Navigation {
105104
return Constants.STATE.WORKSPACE;
106105
}
107106
} else if (focusedTree instanceof Blockly.Toolbox) {
108-
if (workspace === focusedTree.getWorkspace()) {
109-
return Constants.STATE.TOOLBOX;
110-
}
107+
return Constants.STATE.TOOLBOX;
111108
} else if (focusedTree instanceof Blockly.Flyout) {
112109
return Constants.STATE.FLYOUT;
113110
}
@@ -222,7 +219,7 @@ export class Navigation {
222219
}
223220
} else if (
224221
e.type === Blockly.Events.BLOCK_CREATE &&
225-
this.getState(mainWorkspace) === Constants.STATE.FLYOUT
222+
this.getState() === Constants.STATE.FLYOUT
226223
) {
227224
// When variables are created, that recreates the flyout contents, leaving the
228225
// cursor in an invalid state.
@@ -825,7 +822,7 @@ export class Navigation {
825822
: workspace.keyboardAccessibilityMode;
826823
return (
827824
!!accessibilityMode &&
828-
this.getState(workspace) !== Constants.STATE.NOWHERE &&
825+
this.getState() !== Constants.STATE.NOWHERE &&
829826
!Blockly.getFocusManager().ephemeralFocusTaken()
830827
);
831828
}

src/navigation_controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export class NavigationController {
200200
!workspace.isDragging() && this.navigation.canCurrentlyEdit(workspace),
201201
callback: (workspace) => {
202202
keyboardNavigationController.setIsActive(true);
203-
switch (this.navigation.getState(workspace)) {
203+
switch (this.navigation.getState()) {
204204
case Constants.STATE.WORKSPACE:
205205
Blockly.getFocusManager().focusTree(
206206
workspace.getToolbox() ??

0 commit comments

Comments
 (0)