8000 fix: use copyable interface for cut action, add tests by maribethb · Pull Request #8993 · google/blockly · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: use copyable interface for cut action, add tests #8993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions core/shortcut_items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {Gesture} from './gesture.js';
import {ICopyData, isCopyable} from './interfaces/i_copyable.js';
import {isDeletable} from './interfaces/i_deletable.js';
import {isDraggable} from './interfaces/i_draggable.js';
import {isSelectable} from './interfaces/i_selectable.js';
import {KeyboardShortcut, ShortcutRegistry} from './shortcut_registry.js';
import {Coordinate} from './utils/coordinate.js';
import {KeyCodes} from './utils/keycodes.js';
Expand Down Expand Up @@ -163,7 +162,7 @@ export function registerCut() {
focused.isDeletable() &&
isDraggable(focused) &&
focused.isMovable() &&
isSelectable(focused) &&
isCopyable(focused) &&
!focused.workspace.isFlyout
);
},
Expand Down
2 changes: 1 addition & 1 deletion tests/mocha/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@
import './jso_deserialization_test.js';
import './jso_serialization_test.js';
import './json_test.js';
import './keydown_test.js';
import './layering_test.js';
import './blocks/lists_test.js';
import './blocks/logic_ternary_test.js';
Expand All @@ -258,6 +257,7 @@
import './registry_test.js';
import './render_management_test.js';
import './serializer_test.js';
import './shortcut_items_test.js';
import './shortcut_registry_test.js';
import './touch_test.js';
import './theme_test.js';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from './test_helpers/setup_teardown.js';
import {createKeyDownEvent} from './test_helpers/user_input.js';

suite('Key Down', function () {
suite('Keyboard Shortcut Items', function () {
setup(function () {
sharedTestSetup.call(this);
this.workspace = Blockly.inject('blocklyDiv', {});
Expand All @@ -35,6 +35,18 @@ suite('Key Down', function () {
return block;
}

/**
* Creates a block and sets its nextConnection as the focused node.
* @param {Blockly.Workspace} workspace The workspace to create a new block on.
*/
function setSelectedConnection(workspace) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be ideal to set up a situation with a real focusable node rather than spying, if possible (just for better integration verification with the rest of Blockly and to reduce the risk of functional disparity over time).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed #8996 as a follow up

defineStackBlock();
const block = workspace.newBlock('stack_block');
sinon
.stub(Blockly.getFocusManager(), 'getFocusedNode')
.returns(block.nextConnection);
}

/**
* Creates a test for not running keyDown events when the workspace is in read only mode.
* @param {Object} keyEvent Mocked key down event. Use createKeyDownEvent.
Expand Down Expand Up @@ -73,9 +85,14 @@ suite('Key Down', function () {
this.injectionDiv.dispatchEvent(this.event);
sinon.assert.notCalled(this.hideChaffSpy);
});
test('Called when connection is focused', function () {
setSelectedConnection(this.workspace);
this.injectionDiv.dispatchEvent(this.event);
sinon.assert.calledOnce(this.hideChaffSpy);
});
});

suite('Delete Block', function () {
suite('Delete', function () {
setup(function () {
this.hideChaffSpy = sinon.spy(
Blockly.WorkspaceSvg.prototype,
Expand All @@ -89,6 +106,7 @@ suite('Key Down', function () {
['Backspace', createKeyDownEvent(Blockly.utils.KeyCodes.BACKSPACE)],
];
// Delete a block.
// Note that chaff is hidden when a block is deleted.
suite('Simple', function () {
testCases.forEach(function (testCase) {
const testCaseName = testCase[0];
Expand All @@ -108,6 +126,16 @@ suite('Key Down', function () {
runReadOnlyTest(keyEvent, testCaseName);
});
});
// Do not delete anything if a connection is focused.
test('Not called when connection is focused', function () {
// Restore the stub behavior called during setup
Blockly.getFocusManager().getFocusedNode.restore();

setSelectedConnection(this.workspace);
const event = createKeyDownEvent(Blockly.utils.KeyCodes.DELETE);
this.injectionDiv.dispatchEvent(event);
sinon.assert.notCalled(this.hideChaffSpy);
});
});

suite('Copy', function () {
Expand Down Expand Up @@ -194,6 +222,18 @@ suite('Key Down', function () {
});
});
});
test('Not called when connection is focused', function () {
// Restore the stub behavior called during setup
Blockly.getFocusManager().getFocusedNode.restore();

setSelectedConnection(this.workspace);
const event = createKeyDownEvent(Blockly.utils.KeyCodes.C, [
Blockly.utils.KeyCodes.CTRL,
]);
this.injectionDiv.dispatchEvent(event);
sinon.assert.notCalled(this.copySpy);
sinon.assert.notCalled(this.hideChaffSpy);
});
});

suite('Undo', function () {
Expand Down
0