Skip to content

Commit 8a9e9d9

Browse files
authored
refactor: Update to use IFocusableNode in place of INavigable. (#520)
* refactor: Update to use `IFocusableNode` in place of `INavigable`. * chore: Fix tests. * refactor: Use .equal instead of .to.include
1 parent 7cd35aa commit 8a9e9d9

File tree

12 files changed

+49
-175
lines changed

12 files changed

+49
-175
lines changed

src/actions/clipboard.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
getFocusManager,
1818
} from 'blockly';
1919
import * as Constants from '../constants';
20-
import type {BlockSvg, WorkspaceSvg, INavigable} from 'blockly';
20+
import type {BlockSvg, WorkspaceSvg} from 'blockly';
2121
import {Navigation} from '../navigation';
2222
import {getShortActionShortcut} from '../shortcut_formatting';
2323
import * as Blockly from 'blockly';
@@ -366,9 +366,7 @@ export class Clipboard {
366366
? workspace
367367
: this.copyWorkspace;
368368

369-
const targetNode = FocusableTreeTraverser.findFocusedNode(
370-
pasteWorkspace,
371-
) as unknown as INavigable<any>;
369+
const targetNode = FocusableTreeTraverser.findFocusedNode(pasteWorkspace);
372370
// If we're pasting in the flyout it still targets the workspace. Focus
373371
// first as to ensure correct selection handling.
374372
getFocusManager().focusTree(workspace);

src/actions/delete.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
LineCursor,
1313
} from 'blockly';
1414
import * as Constants from '../constants';
15-
import type {BlockSvg, WorkspaceSvg} from 'blockly';
15+
import type {WorkspaceSvg} from 'blockly';
1616
import {Navigation} from '../navigation';
1717

1818
const KeyCodes = BlocklyUtils.KeyCodes;

src/actions/disconnect.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
utils as BlocklyUtils,
1212
} from 'blockly';
1313
import * as Constants from '../constants';
14-
import type {WorkspaceSvg, INavigable} from 'blockly';
14+
import type {WorkspaceSvg, IFocusableNode} from 'blockly';
1515
import {Navigation} from '../navigation';
1616

1717
const KeyCodes = BlocklyUtils.KeyCodes;
@@ -79,7 +79,7 @@ export class DisconnectAction {
7979
if (!cursor) {
8080
return;
8181
}
82-
let curNode: INavigable<any> | null = cursor.getCurNode();
82+
let curNode: IFocusableNode | null = cursor.getCurNode();
8383
let wasVisitingConnection = true;
8484
while (
8585
curNode &&

src/actions/enter.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
FocusableTreeTraverser,
1818
} from 'blockly/core';
1919

20-
import type {Block, INavigable} from 'blockly/core';
20+
import type {Block} from 'blockly/core';
2121

2222
import * as Constants from '../constants';
2323
import type {Navigation} from '../navigation';
@@ -59,7 +59,6 @@ export class EnterAction {
5959

6060
let flyoutCursor;
6161
let curNode;
62-
let nodeType;
6362

6463
switch (this.navigation.getState(workspace)) {
6564
case Constants.STATE.WORKSPACE:
@@ -128,9 +127,7 @@ export class EnterAction {
128127
Events.setGroup(true);
129128
}
130129

131-
const stationaryNode = FocusableTreeTraverser.findFocusedNode(
132-
workspace,
133-
) as unknown as INavigable<any>;
130+
const stationaryNode = FocusableTreeTraverser.findFocusedNode(workspace);
134131
const newBlock = this.createNewBlock(workspace);
135132
if (!newBlock) return;
136133
const insertStartPoint = stationaryNode

src/flyout_cursor.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class FlyoutCursor extends Blockly.LineCursor {
3333
* @returns The next element, or null if the current node is
3434
* not set or there is no next value.
3535
*/
36-
override next(): Blockly.INavigable<any> | null {
36+
override next(): Blockly.IFocusableNode | null {
3737
const curNode = this.getCurNode();
3838
if (!curNode) {
3939
return null;
@@ -46,22 +46,13 @@ export class FlyoutCursor extends Blockly.LineCursor {
4646
return newNode;
4747
}
4848

49-
/**
50-
* This is a no-op since a flyout cursor can not go in.
51-
*
52-
* @returns Always null.
53-
*/
54-
override in(): null {
55-
return null;
56-
}
57-
5849
/**
5950
* Moves the cursor to the previous stack of blocks in the flyout.
6051
*
6152
* @returns The previous element, or null if the current
6253
* node is not set or there is no previous value.
6354
*/
64-
override prev(): Blockly.INavigable<any> | null {
55+
override prev(): Blockly.IFocusableNode | null {
6556
const curNode = this.getCurNode();
6657
if (!curNode) {
6758
return null;
@@ -74,16 +65,7 @@ export class FlyoutCursor extends Blockly.LineCursor {
7465
return newNode;
7566
}
7667

77-
/**
78-
* This is a no-op since a flyout cursor can not go out.
79-
*
80-
* @returns Always null.
81-
*/
82-
override out(): null {
83-
return null;
84-
}
85-
86-
override setCurNode(node: Blockly.INavigable<any> | null) {
68+
override setCurNode(node: Blockly.IFocusableNode | null) {
8769
super.setCurNode(node);
8870

8971
let bounds: Blockly.utils.Rect | undefined;

src/index.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,6 @@ import * as Blockly from 'blockly/core';
88
import {NavigationController} from './navigation_controller';
99
import {enableBlocksOnDrag} from './disabled_blocks';
1010

11-
/** Options object for KeyboardNavigation instances. */
12-
export interface NavigationOptions {
13-
cursor: Partial<Blockly.CursorOptions>;
14-
}
15-
16-
/** Default options for LineCursor instances. */
17-
const defaultOptions: NavigationOptions = {
18-
cursor: {},
19-
};
20-
2111
/** Plugin for keyboard navigation. */
2212
export class KeyboardNavigation {
2313
/** The workspace. */
@@ -38,19 +28,11 @@ export class KeyboardNavigation {
3828
/**
3929
* Constructs the keyboard navigation.
4030
*
41-
* @param workspace The workspace that the plugin will
42-
* be added to.
43-
* @param options Options.
31+
* @param workspace The workspace that the plugin will be added to.
4432
*/
45-
constructor(
46-
workspace: Blockly.WorkspaceSvg,
47-
options: Partial<NavigationOptions>,
48-
) {
33+
constructor(workspace: Blockly.WorkspaceSvg) {
4934
this.workspace = workspace;
5035

51-
// Regularise options and apply defaults.
52-
options = {...defaultOptions, ...options};
53-
5436
this.navigationController = new NavigationController();
5537
this.navigationController.init();
5638
this.navigationController.addWorkspace(workspace);
@@ -59,7 +41,7 @@ export class KeyboardNavigation {
5941
this.originalTheme = workspace.getTheme();
6042
this.setGlowTheme();
6143

62-
this.cursor = new Blockly.LineCursor(workspace, options.cursor);
44+
this.cursor = new Blockly.LineCursor(workspace);
6345

6446
// Add the event listener to enable disabled blocks on drag.
6547
workspace.addChangeListener(enableBlocksOnDrag);

src/keyboard_drag_strategy.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
RenderedConnection,
1111
dragging,
1212
utils,
13-
INavigable,
1413
} from 'blockly';
1514
import {Direction, getDirectionFromXY} from './drag_direction';
1615
import {showUnconstrainedMoveHint} from './hints';

src/navigation.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import * as Constants from './constants';
1515
import {
1616
registrationName as cursorRegistrationName,
1717
registrationType as cursorRegistrationType,
18-
FlyoutCursor,
1918
} from './flyout_cursor';
2019

2120
/**
@@ -151,7 +150,7 @@ export class Navigation {
151150
if (FlyoutCursorClass) {
152151
flyoutWorkspace
153152
.getMarkerManager()
154-
.setCursor(new Blockly.LineCursor(flyout.getWorkspace()));
153+
.setCursor(new FlyoutCursorClass(flyout));
155154
}
156155
}
157156

@@ -258,7 +257,7 @@ export class Navigation {
258257
}
259258

260259
private isFlyoutItemDisposed(
261-
node: Blockly.INavigable<any>,
260+
node: Blockly.IFocusableNode,
262261
sourceBlock: Blockly.BlockSvg | null,
263262
) {
264263
if (sourceBlock?.disposed) {
@@ -368,9 +367,7 @@ export class Navigation {
368367
: flyoutContents[flyoutContents.length - 1];
369368
if (!defaultFlyoutItem) return false;
370369
const defaultFlyoutItemElement = defaultFlyoutItem.getElement();
371-
flyoutCursor.setCurNode(
372-
defaultFlyoutItemElement as unknown as Blockly.INavigable<any>,
373-
);
370+
flyoutCursor.setCurNode(defaultFlyoutItemElement);
374371
return true;
375372
}
376373

@@ -420,11 +417,11 @@ export class Navigation {
420417
* @param workspace The main workspace the flyout is on.
421418
* @returns The flyout's cursor or null if no flyout exists.
422419
*/
423-
getFlyoutCursor(workspace: Blockly.WorkspaceSvg): FlyoutCursor | null {
420+
getFlyoutCursor(workspace: Blockly.WorkspaceSvg): Blockly.LineCursor | null {
424421
const flyout = workspace.getFlyout();
425422
const cursor = flyout ? flyout.getWorkspace().getCursor() : null;
426423

427-
return cursor as FlyoutCursor;
424+
return cursor;
428425
}
429426

430427
/**
@@ -437,7 +434,7 @@ export class Navigation {
437434
* wrong.
438435
*/
439436
findInsertStartPoint(
440-
stationaryNode: Blockly.INavigable<any>,
437+
stationaryNode: Blockly.IFocusableNode,
441438
movingBlock: Blockly.BlockSvg,
442439
): Blockly.RenderedConnection | null {
443440
const movingHasOutput = !!movingBlock.outputConnection;
@@ -517,7 +514,7 @@ export class Navigation {
517514
* @returns True if the connection was successful, false otherwise.
518515
*/
519516
tryToConnectBlock(
520-
stationaryNode: Blockly.INavigable<any>,
517+
stationaryNode: Blockly.IFocusableNode,
521518
movingBlock: Blockly.BlockSvg,
522519
): boolean {
523520
const destConnection = this.findInsertStartPoint(

test/index.html

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,6 @@
190190
<option value="zelos">Zelos</option>
191191
</select>
192192
</div>
193-
<div>
194-
<label for="stackConnections">Disable stack connections:</label>
195-
<input
196-
type="checkbox"
197-
name="noStack"
198-
id="noStack"
199-
onChange="document.forms.options.submit()" />
200-
</div>
201193
</form>
202194
</div>
203195
</div>

test/index.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ function getOptions() {
4949
renderer = 'thrasos';
5050
}
5151

52-
const noStackParam = params.get('noStack');
53-
const stackConnections = !noStackParam;
54-
5552
const toolboxParam = params.get('toolbox');
5653
const toolbox = toolboxParam ?? 'toolbox';
5754
const toolboxObject =
@@ -66,13 +63,10 @@ function getOptions() {
6663
(document.getElementById('toolbox') as HTMLSelectElement).value = toolbox;
6764
(document.getElementById('renderer') as HTMLSelectElement).value = renderer;
6865
(document.getElementById('scenario') as HTMLSelectElement).value = scenario;
69-
(document.getElementById('noStack') as HTMLInputElement).checked =
70-
!stackConnections;
7166
});
7267

7368
return {
7469
scenario,
75-
stackConnections,
7670
renderer,
7771
toolbox: toolboxObject,
7872
};
@@ -85,7 +79,7 @@ function getOptions() {
8579
* @returns The created workspace.
8680
*/
8781
function createWorkspace(): Blockly.WorkspaceSvg {
88-
const {scenario, stackConnections, renderer, toolbox} = getOptions();
82+
const {scenario, renderer, toolbox} = getOptions();
8983

9084
const injectOptions = {
9185
toolbox,
@@ -97,10 +91,7 @@ function createWorkspace(): Blockly.WorkspaceSvg {
9791
}
9892
const workspace = Blockly.inject(blocklyDiv, injectOptions);
9993

100-
const navigationOptions = {
101-
cursor: {stackConnections},
102-
};
103-
new KeyboardNavigation(workspace, navigationOptions);
94+
new KeyboardNavigation(workspace);
10495
registerRunCodeShortcut();
10596

10697
// Disable blocks that aren't inside the setup or draw loops.

0 commit comments

Comments
 (0)