Skip to content

Commit 48600df

Browse files
authored
fix: Remove null checks for cursor. (#669)
* fix: Remove null checks for cursor. * chore: Fix lint errors. * chore: Satisfy the formatter.
1 parent 6a073a6 commit 48600df

File tree

10 files changed

+21
-45
lines changed

10 files changed

+21
-45
lines changed

src/actions/action_menu.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ export class ActionMenu {
8686
// TODO(#362): Pass this through the precondition and callback instead of making it up.
8787
const menuOpenEvent = new KeyboardEvent('keydown');
8888

89-
const cursor = workspace.getCursor();
90-
if (!cursor) throw new Error('workspace has no cursor');
91-
const node = cursor.getCurNode();
89+
const node = workspace.getCursor().getCurNode();
9290
if (!node) return false;
9391
// TODO(google/blockly#8847): Add typeguard for IContextMenu in core when this moves over
9492
if (

src/actions/arrow_navigation.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,7 @@ export class ArrowNavigation {
3838
workspace: WorkspaceSvg,
3939
shortcut: ShortcutRegistry.KeyboardShortcut,
4040
): boolean {
41-
const cursor = workspace.getCursor();
42-
if (!cursor || !cursor.getCurNode()) {
43-
return false;
44-
}
45-
const curNode = cursor.getCurNode();
41+
const curNode = workspace.getCursor().getCurNode();
4642
if (curNode instanceof Field) {
4743
return curNode.onShortcut(shortcut);
4844
}
@@ -70,7 +66,7 @@ export class ArrowNavigation {
7066
if (
7167
!this.navigation.defaultWorkspaceCursorPositionIfNeeded(workspace)
7268
) {
73-
workspace.getCursor()?.in();
69+
workspace.getCursor().in();
7470
}
7571
isHandled = true;
7672
}
@@ -103,7 +99,7 @@ export class ArrowNavigation {
10399
if (
104100
!this.navigation.defaultWorkspaceCursorPositionIfNeeded(workspace)
105101
) {
106-
workspace.getCursor()?.out();
102+
workspace.getCursor().out();
107103
}
108104
isHandled = true;
109105
}
@@ -169,7 +165,7 @@ export class ArrowNavigation {
169165
workspace,
170166
)
171167
) {
172-
workspace.getCursor()?.next();
168+
workspace.getCursor().next();
173169
}
174170
isHandled = true;
175171
}
@@ -182,7 +178,7 @@ export class ArrowNavigation {
182178
workspace.targetWorkspace,
183179
)
184180
) {
185-
workspace.getCursor()?.next();
181+
workspace.getCursor().next();
186182
}
187183
isHandled = true;
188184
}
@@ -232,7 +228,7 @@ export class ArrowNavigation {
232228
'last',
233229
)
234230
) {
235-
workspace.getCursor()?.prev();
231+
workspace.getCursor().prev();
236232
}
237233
isHandled = true;
238234
}
@@ -246,7 +242,7 @@ export class ArrowNavigation {
246242
'last',
247243
)
248244
) {
249-
workspace.getCursor()?.prev();
245+
workspace.getCursor().prev();
250246
}
251247
isHandled = true;
252248
}

src/actions/disconnect.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ export class DisconnectAction {
7878
*/
7979
disconnectBlocks(workspace: WorkspaceSvg) {
8080
const cursor = workspace.getCursor();
81-
if (!cursor) return;
8281
const curNode = cursor.getCurNode();
8382
if (!(curNode instanceof BlockSvg)) return;
8483

src/actions/edit.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import {
8-
ContextMenuRegistry,
9-
LineCursor,
10-
Msg,
11-
keyboardNavigationController,
12-
} from 'blockly';
7+
import {ContextMenuRegistry, Msg, keyboardNavigationController} from 'blockly';
138
import {Navigation} from 'src/navigation';
149
import {getMenuItem} from '../shortcut_formatting';
1510
import * as Constants from '../constants';
@@ -67,7 +62,7 @@ export class EditAction {
6762
if (!workspace || !this.navigation.canCurrentlyNavigate(workspace)) {
6863
return 'disabled';
6964
}
70-
const cursor = workspace.getCursor() as LineCursor | null;
65+
const cursor = workspace.getCursor();
7166
if (!cursor) return 'disabled';
7267
return cursor.atEndOfLine() ? 'hidden' : 'enabled';
7368
},

src/actions/enter.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ export class EnterAction {
116116
private shouldHandleEnterForWS(workspace: WorkspaceSvg): boolean {
117117
if (!this.navigation.canCurrentlyNavigate(workspace)) return false;
118118

119-
const cursor = workspace.getCursor();
120-
const curNode = cursor?.getCurNode();
119+
const curNode = workspace.getCursor().getCurNode();
121120
if (!curNode) return false;
122121
if (curNode instanceof Field) return curNode.isClickable();
123122
if (
@@ -143,7 +142,7 @@ export class EnterAction {
143142
*/
144143
private handleEnterForWS(workspace: WorkspaceSvg): boolean {
145144
const cursor = workspace.getCursor();
146-
const curNode = cursor?.getCurNode();
145+
const curNode = cursor.getCurNode();
147146
if (!curNode) return false;
148147
if (curNode instanceof Field) {
149148
curNode.showEditor();
@@ -168,7 +167,7 @@ export class EnterAction {
168167
// See icon_navigation_policy.
169168
if (curNode instanceof icons.MutatorIcon) {
170169
renderManagement.finishQueuedRenders().then(() => {
171-
cursor?.in();
170+
cursor.in();
172171
});
173172
}
174173
return true;

src/actions/move.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ export class MoveActions {
260260
const node = getFocusManager().getFocusedNode();
261261
if (node instanceof comments.RenderedWorkspaceComment) return node;
262262

263-
let block = workspace?.getCursor()?.getSourceBlock();
263+
let block = workspace.getCursor().getSourceBlock();
264264
if (!block) return undefined;
265265
while (block.isShadow()) {
266266
block = block.getParent();

src/actions/mover.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export class Mover {
160160
// In case a block is detached, ensure that it still retains focus
161161
// (otherwise dragging will break). This is also the point a new block's
162162
// initial insert position is scrolled into view.
163-
workspace.getCursor()?.setCurNode(draggable);
163+
workspace.getCursor().setCurNode(draggable);
164164
draggable.getFocusableElement().addEventListener('blur', blurListener);
165165

166166
// Register a keyboard shortcut under the key combos of all existing
@@ -257,7 +257,7 @@ export class Mover {
257257
);
258258

259259
if (dragStrategy.moveType === MoveType.Insert && target) {
260-
workspace.getCursor()?.setCurNode(target);
260+
workspace.getCursor().setCurNode(target);
261261
}
262262

263263
this.postDragEndCleanup(workspace, info);

src/actions/ws_movement.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,7 @@ export class WorkspaceMovement {
119119
* @param workspace The workspace the cursor is on.
120120
*/
121121
createWSCursor(workspace: WorkspaceSvg) {
122-
const cursor = workspace.getCursor();
123-
124-
if (!cursor) return false;
125-
126-
cursor.setCurNode(workspace);
122+
workspace.getCursor().setCurNode(workspace);
127123
return true;
128124
}
129125
}

src/navigation.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ export class Navigation {
7272
removeWorkspace(workspace: Blockly.WorkspaceSvg) {
7373
const workspaceIdx = this.workspaces.indexOf(workspace);
7474
const flyout = workspace.getFlyout();
75-
76-
if (workspace.getCursor()) {
77-
this.disableKeyboardAccessibility(workspace);
78-
}
75+
this.disableKeyboardAccessibility(workspace);
7976

8077
if (workspaceIdx > -1) {
8178
this.workspaces.splice(workspaceIdx, 1);
@@ -259,9 +256,9 @@ export class Navigation {
259256
) {
260257
const mutatedBlockId = e.blockId;
261258
const cursor = workspace.getCursor();
262-
const block = cursor?.getSourceBlock();
259+
const block = cursor.getSourceBlock();
263260
if (block && block.id === mutatedBlockId) {
264-
cursor?.setCurNode(block);
261+
cursor.setCurNode(block);
265262
}
266263
}
267264

@@ -346,9 +343,6 @@ export class Navigation {
346343
) {
347344
const topBlocks = workspace.getTopBlocks(true);
348345
const cursor = workspace.getCursor();
349-
if (!cursor) {
350-
return;
351-
}
352346
const disposed = cursor.getSourceBlock()?.disposed;
353347
if (cursor.getCurNode() && !disposed) {
354348
// Retain the cursor's previous position since it's set, but only if not
@@ -800,7 +794,7 @@ export class Navigation {
800794
*/
801795
paste(copyData: Blockly.ICopyData, workspace: Blockly.WorkspaceSvg): boolean {
802796
// Do this before clipoard.paste due to cursor/focus workaround in getCurNode.
803-
const targetNode = workspace.getCursor()?.getCurNode();
797+
const targetNode = workspace.getCursor().getCurNode();
804798

805799
Blockly.Events.setGroup(true);
806800
const block = Blockly.clipboard.paste(

test/webdriverio/test/workspace_comment_test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import * as chai from 'chai';
88
import * as Blockly from 'blockly';
99
import {
10-
contextMenuExists,
1110
focusOnBlock,
1211
getCurrentFocusNodeId,
1312
getFocusedBlockType,

0 commit comments

Comments
 (0)