Skip to content

Commit 86c831a

Browse files
authored
fix: use copyable interface for cut action, add tests (#8993)
* fix: use copyable for cut action * chore: rename keydown test * chore: add tests for shortcut items not acting on focused connections
1 parent eb5181e commit 86c831a

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

core/shortcut_items.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {Gesture} from './gesture.js';
1313
import {ICopyData, isCopyable} from './interfaces/i_copyable.js';
1414
import {isDeletable} from './interfaces/i_deletable.js';
1515
import {isDraggable} from './interfaces/i_draggable.js';
16-
import {isSelectable} from './interfaces/i_selectable.js';
1716
import {KeyboardShortcut, ShortcutRegistry} from './shortcut_registry.js';
1817
import {Coordinate} from './utils/coordinate.js';
1918
import {KeyCodes} from './utils/keycodes.js';
@@ -163,7 +162,7 @@ export function registerCut() {
163162
focused.isDeletable() &&
164163
isDraggable(focused) &&
165164
focused.isMovable() &&
166-
isSelectable(focused) &&
165+
isCopyable(focused) &&
167166
!focused.workspace.isFlyout
168167
);
169168
},

tests/mocha/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@
242242
import './jso_deserialization_test.js';
243243
import './jso_serialization_test.js';
244244
import './json_test.js';
245-
import './keydown_test.js';
246245
import './layering_test.js';
247246
import './blocks/lists_test.js';
248247
import './blocks/logic_ternary_test.js';
@@ -258,6 +257,7 @@
258257
import './registry_test.js';
259258
import './render_management_test.js';
260259
import './serializer_test.js';
260+
import './shortcut_items_test.js';
261261
import './shortcut_registry_test.js';
262262
import './touch_test.js';
263263
import './theme_test.js';

tests/mocha/keydown_test.js renamed to tests/mocha/shortcut_items_test.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from './test_helpers/setup_teardown.js';
1313
import {createKeyDownEvent} from './test_helpers/user_input.js';
1414

15-
suite('Key Down', function () {
15+
suite('Keyboard Shortcut Items', function () {
1616
setup(function () {
1717
sharedTestSetup.call(this);
1818
this.workspace = Blockly.inject('blocklyDiv', {});
@@ -35,6 +35,18 @@ suite('Key Down', function () {
3535
return block;
3636
}
3737

38+
/**
39+
* Creates a block and sets its nextConnection as the focused node.
40+
* @param {Blockly.Workspace} workspace The workspace to create a new block on.
41+
*/
42+
function setSelectedConnection(workspace) {
43+
defineStackBlock();
44+
const block = workspace.newBlock('stack_block');
45+
sinon
46+
.stub(Blockly.getFocusManager(), 'getFocusedNode')
47+
.returns(block.nextConnection);
48+
}
49+
3850
/**
3951
* Creates a test for not running keyDown events when the workspace is in read only mode.
4052
* @param {Object} keyEvent Mocked key down event. Use createKeyDownEvent.
@@ -73,9 +85,14 @@ suite('Key Down', function () {
7385
this.injectionDiv.dispatchEvent(this.event);
7486
sinon.assert.notCalled(this.hideChaffSpy);
7587
});
88+
test('Called when connection is focused', function () {
89+
setSelectedConnection(this.workspace);
90+
this.injectionDiv.dispatchEvent(this.event);
91+
sinon.assert.calledOnce(this.hideChaffSpy);
92+
});
7693
});
7794

78-
suite('Delete Block', function () {
95+
suite('Delete', function () {
7996
setup(function () {
8097
this.hideChaffSpy = sinon.spy(
8198
Blockly.WorkspaceSvg.prototype,
@@ -89,6 +106,7 @@ suite('Key Down', function () {
89106
['Backspace', createKeyDownEvent(Blockly.utils.KeyCodes.BACKSPACE)],
90107
];
91108
// Delete a block.
109+
// Note that chaff is hidden when a block is deleted.
92110
suite('Simple', function () {
93111
testCases.forEach(function (testCase) {
94112
const testCaseName = testCase[0];
@@ -108,6 +126,16 @@ suite('Key Down', function () {
108126
runReadOnlyTest(keyEvent, testCaseName);
109127
});
110128
});
129+
// Do not delete anything if a connection is focused.
130+
test('Not called when connection is focused', function () {
131+
// Restore the stub behavior called during setup
132+
Blockly.getFocusManager().getFocusedNode.restore();
133+
134+
setSelectedConnection(this.workspace);
135+
const event = createKeyDownEvent(Blockly.utils.KeyCodes.DELETE);
136+
this.injectionDiv.dispatchEvent(event);
137+
sinon.assert.notCalled(this.hideChaffSpy);
138+
});
111139
});
112140

113141
suite('Copy', function () {
@@ -194,6 +222,18 @@ suite('Key Down', function () {
194222
});
195223
});
196224
});
225+
test('Not called when connection is focused', function () {
226+
// Restore the stub behavior called during setup
227+
Blockly.getFocusManager().getFocusedNode.restore();
228+
229+
setSelectedConnection(this.workspace);
230+
const event = createKeyDownEvent(Blockly.utils.KeyCodes.C, [
231+
Blockly.utils.KeyCodes.CTRL,
232+
]);
233+
this.injectionDiv.dispatchEvent(event);
234+
sinon.assert.notCalled(this.copySpy);
235+
sinon.assert.notCalled(this.hideChaffSpy);
236+
});
197237
});
198238

199239
suite('Undo', function () {

0 commit comments

Comments
 (0)