Skip to content

Commit 05ff1bd

Browse files
Merge branch 'april-ut' of github.com:microbit-matt-hillsdon/blockly-keyboard-experimentation into april-ut
2 parents 7f75eaa + 2d2a062 commit 05ff1bd

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/actions/move.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ const createSerializedKey = ShortcutRegistry.registry.createSerializedKey.bind(
2222
* Actions for moving blocks with keyboard shortcuts.
2323
*/
2424
export class MoveActions {
25+
/**
26+
* Stored to enable us to restore monkey patch.
27+
*/
28+
private oldShortcutRegistryOnKeyDown:
29+
| typeof ShortcutRegistry.registry.onKeyDown
30+
| null = null;
31+
2532
constructor(private mover: Mover) {}
2633

2734
private shortcuts: ShortcutRegistry.KeyboardShortcut[] = [
@@ -154,6 +161,32 @@ export class MoveActions {
154161
for (const menuItem of this.menuItems) {
155162
ContextMenuRegistry.registry.register(menuItem);
156163
}
164+
165+
// Monkey patch shortcut registry to ignore all non-move-related
166+
// actions during a move.
167+
this.oldShortcutRegistryOnKeyDown = ShortcutRegistry.registry.onKeyDown;
168+
ShortcutRegistry.registry.onKeyDown = (workspace, e) => {
169+
if (!this.oldShortcutRegistryOnKeyDown) return false;
170+
// @ts-expect-error private method
171+
const key = ShortcutRegistry.registry.serializeKeyEvent(e);
172+
const moveShortcutNames =
173+
ShortcutRegistry.registry.getShortcutNamesByKeyCode(key);
174+
if (
175+
!this.shortcuts.some((shortcut) =>
176+
moveShortcutNames?.includes(shortcut.name),
177+
)
178+
) {
179+
if (this.mover.isMoving(workspace)) {
180+
return false;
181+
}
182+
}
183+
184+
return this.oldShortcutRegistryOnKeyDown.call(
185+
ShortcutRegistry.registry,
186+
workspace,
187+
e,
188+
);
189+
};
157190
}
158191

159192
/**
@@ -166,5 +199,9 @@ export class MoveActions {
166199
for (const menuItem of this.menuItems) {
167200
ContextMenuRegistry.registry.unregister(menuItem.id);
168201
}
202+
203+
if (this.oldShortcutRegistryOnKeyDown) {
204+
ShortcutRegistry.registry.onKeyDown = this.oldShortcutRegistryOnKeyDown;
205+
}
169206
}
170207
}

src/actions/mover.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ export class Mover {
167167
this.unpatchDragStrategy(info.block);
168168
this.moves.delete(workspace);
169169

170+
// Delay scroll until after block has finished moving.
171+
setTimeout(() => this.scrollCurrentBlockIntoView(workspace), 0);
170172
return true;
171173
}
172174

@@ -215,6 +217,8 @@ export class Mover {
215217
this.unpatchWorkspace(workspace);
216218
this.unpatchDragStrategy(info.block);
217219
this.moves.delete(workspace);
220+
// Delay scroll until after block has finished moving.
221+
setTimeout(() => this.scrollCurrentBlockIntoView(workspace), 0);
218222
return true;
219223
}
220224

@@ -237,6 +241,7 @@ export class Mover {
237241
);
238242

239243
info.updateTotalDelta();
244+
this.scrollCurrentBlockIntoView(workspace);
240245
return true;
241246
}
242247

@@ -258,6 +263,7 @@ export class Mover {
258263
info.totalDelta.y += y * UNCONSTRAINED_MOVE_DISTANCE * workspace.scale;
259264

260265
info.dragger.onDrag(info.fakePointerEvent('pointermove'), info.totalDelta);
266+
this.scrollCurrentBlockIntoView(workspace);
261267
return true;
262268
}
263269

@@ -388,6 +394,20 @@ export class Mover {
388394
this.currentMoveIndicator?.remove();
389395
this.currentMoveIndicator = null;
390396
}
397+
398+
/**
399+
* Scrolls the current block into view if exists one.
400+
*
401+
* @param workspace The workspace to get current block from.
402+
*/
403+
private scrollCurrentBlockIntoView(workspace: WorkspaceSvg) {
404+
const blockToView = this.getCurrentBlock(workspace);
405+
if (blockToView) {
406+
workspace.scrollBoundsIntoView(
407+
blockToView.getBoundingRectangleWithoutChildren(),
408+
);
409+
}
410+
}
391411
}
392412

393413
/**

0 commit comments

Comments
 (0)