Skip to content

Commit 4734bf9

Browse files
authored
feat: Add keyboard shortcut to clean up the workspace (#9728)
* feat: Add a keyboard shortcut to clean up the workspace * test: Add tests
1 parent 13459a2 commit 4734bf9

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

packages/blockly/core/shortcut_items.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export enum names {
6060
PREVIOUS_STACK = 'previous_stack',
6161
INFORMATION = 'information',
6262
PERFORM_ACTION = 'perform_action',
63+
CLEANUP = 'cleanup',
6364
}
6465

6566
/**
@@ -870,6 +871,25 @@ export function registerPerformAction() {
870871
ShortcutRegistry.registry.register(performActionShortcut);
871872
}
872873

874+
/**
875+
* Registers keyboard shortcut to clean up the workspace.
876+
*/
877+
export function registerCleanup() {
878+
const cleanupShortcut: KeyboardShortcut = {
879+
name: names.CLEANUP,
880+
preconditionFn: (workspace) =>
881+
!workspace.isDragging() && !workspace.isReadOnly(),
882+
callback: (workspace) => {
883+
keyboardNavigationController.setIsActive(true);
884+
workspace.cleanUp();
885+
return true;
886+
},
887+
keyCodes: [KeyCodes.C],
888+
allowCollision: true,
889+
};
890+
ShortcutRegistry.registry.register(cleanupShortcut);
891+
}
892+
873893
/**
874894
* Registers all default keyboard shortcut item. This should be called once per
875895
* instance of KeyboardShortcutRegistry.
@@ -899,6 +919,7 @@ export function registerKeyboardNavigationShortcuts() {
899919
registerDisconnectBlock();
900920
registerStackNavigation();
901921
registerPerformAction();
922+
registerCleanup();
902923
}
903924

904925
/**

packages/blockly/tests/mocha/shortcut_items_test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,4 +1231,43 @@ suite('Keyboard Shortcut Items', function () {
12311231
this.workspace.registerButtonCallback('CREATE_VARIABLE', oldCallback);
12321232
});
12331233
});
1234+
1235+
suite('Clean up workspace (C)', function () {
1236+
test('Arranges all blocks in a vertical column', function () {
1237+
this.workspace.newBlock('controls_if');
1238+
const block2 = this.workspace.newBlock('controls_if');
1239+
block2.moveBy(300, 20);
1240+
const block3 = this.workspace.newBlock('controls_if');
1241+
block3.moveBy(-75, -60);
1242+
1243+
const event = createKeyDownEvent(Blockly.utils.KeyCodes.C);
1244+
this.workspace.getInjectionDiv().dispatchEvent(event);
1245+
1246+
for (const block of this.workspace.getTopBlocks()) {
1247+
assert.equal(block.relativeCoords.x, 0);
1248+
}
1249+
});
1250+
1251+
test('Does nothing on a readonly workspace', function () {
1252+
this.workspace.newBlock('controls_if');
1253+
const block2 = this.workspace.newBlock('controls_if');
1254+
block2.moveBy(300, 20);
1255+
const block3 = this.workspace.newBlock('controls_if');
1256+
block3.moveBy(-75, -60);
1257+
1258+
this.workspace.setIsReadOnly(true);
1259+
1260+
const oldBounds = this.workspace
1261+
.getTopBlocks(true)
1262+
.map((b) => b.getBoundingRectangle());
1263+
1264+
const event = createKeyDownEvent(Blockly.utils.KeyCodes.C);
1265+
this.workspace.getInjectionDiv().dispatchEvent(event);
1266+
1267+
const newBounds = this.workspace
1268+
.getTopBlocks(true)
1269+
.map((b) => b.getBoundingRectangle());
1270+
assert.deepEqual(oldBounds, newBounds);
1271+
});
1272+
});
12341273
});

0 commit comments

Comments
 (0)