Skip to content

Commit 40001aa

Browse files
committed
Allow some things to handle return on read only workspaces
1 parent 76388be commit 40001aa

File tree

9 files changed

+53
-29
lines changed

9 files changed

+53
-29
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
return this.openActionMenu(workspace);
5858
case Constants.STATE.FLYOUT: {

src/actions/arrow_navigation.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class ArrowNavigation {
6161
const toolbox = workspace.getToolbox() as Toolbox;
6262
const flyout = workspace.getFlyout();
6363
let isHandled = false;
64-
switch (this.navigation.getState(workspace)) {
64+
switch (this.navigation.getState()) {
6565
case Constants.STATE.WORKSPACE:
6666
isHandled = this.fieldShortcutHandler(workspace, shortcut);
6767
if (!isHandled && workspace) {
@@ -93,7 +93,7 @@ export class ArrowNavigation {
9393
): boolean => {
9494
const toolbox = workspace.getToolbox() as Toolbox;
9595
let isHandled = false;
96-
switch (this.navigation.getState(workspace)) {
96+
switch (this.navigation.getState()) {
9797
case Constants.STATE.WORKSPACE:
9898
isHandled = this.fieldShortcutHandler(workspace, shortcut);
9999
if (!isHandled && workspace) {
@@ -159,7 +159,7 @@ export class ArrowNavigation {
159159
const toolbox = workspace.getToolbox() as Toolbox;
160160
const flyout = workspace.getFlyout();
161161
let isHandled = false;
162-
switch (this.navigation.getState(workspace)) {
162+
switch (this.navigation.getState()) {
163163
case Constants.STATE.WORKSPACE:
164164
isHandled = this.fieldShortcutHandler(workspace, shortcut);
165165
if (!isHandled && workspace) {
@@ -217,7 +217,7 @@ export class ArrowNavigation {
217217
const flyout = workspace.getFlyout();
218218
const toolbox = workspace.getToolbox() as Toolbox;
219219
let isHandled = false;
220-
switch (this.navigation.getState(workspace)) {
220+
switch (this.navigation.getState()) {
221221
case Constants.STATE.WORKSPACE:
222222
isHandled = this.fieldShortcutHandler(workspace, shortcut);
223223
if (!isHandled) {

src/actions/clipboard.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ export class Clipboard {
210210
!!this.oldCutShortcut?.callback &&
211211
this.oldCutShortcut.callback(workspace, e, shortcut, scope);
212212
if (didCut) {
213-
this.copyWorkspace = workspace;
213+
this.copyWorkspace = workspace.isFlyout
214+
? workspace.targetWorkspace
215+
: workspace;
214216
showCutHint(workspace);
215217
}
216218
return didCut;
@@ -291,7 +293,9 @@ export class Clipboard {
291293
!!this.oldCopyShortcut?.callback &&
292294
this.oldCopyShortcut.callback(workspace, e, shortcut, scope);
293295
if (didCopy) {
294-
this.copyWorkspace = workspace;
296+
this.copyWorkspace = workspace.isFlyout
297+
? workspace.targetWorkspace
298+
: workspace;
295299
showCopiedHint(workspace);
296300
}
297301
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: 34 additions & 11 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,19 +53,41 @@ 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:
90+
if (targetWorkspace.isReadOnly()) return false;
7091
flyoutCursor = this.navigation.getFlyoutCursor(workspace);
7192
if (!flyoutCursor) {
7293
return false;
@@ -91,12 +112,13 @@ export class EnterAction {
91112
* Handles hitting the enter key on the workspace.
92113
*
93114
* @param workspace The workspace.
115+
* @returns True if the enter was handled, false otherwise.
94116
*/
95-
private handleEnterForWS(workspace: WorkspaceSvg) {
117+
private handleEnterForWS(workspace: WorkspaceSvg): boolean {
96118
const cursor = workspace.getCursor();
97-
if (!cursor) return;
119+
if (!cursor) return false;
98120
const curNode = cursor.getCurNode();
99-
if (!curNode) return;
121+
if (!curNode) return false;
100122
if (curNode instanceof Field) {
101123
curNode.showEditor();
102124
} else if (curNode instanceof BlockSvg) {
@@ -111,6 +133,7 @@ export class EnterAction {
111133
} else if (curNode instanceof icons.Icon) {
112134
curNode.onClick();
113135
}
136+
return true;
114137
}
115138

116139
/**
@@ -275,7 +298,7 @@ export class EnterAction {
275298
if (block.isSimpleReporter()) {
276299
for (const input of block.inputList) {
277300
for (const field of input.fieldRow) {
278-
if (field.isClickable() && field.isFullBlockField()) {
301+
if (field.isFullBlockField()) {
279302
field.showEditor();
280303
return true;
281304
}

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);

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() ?? workspace.getFlyout() ?? workspace,

0 commit comments

Comments
 (0)