fix: Drag and Resize events for workspace comments (#8217)

* feat: Added a comment_drag event.

* Add workspace comment resize events.

* Addressing PR feedback.

* Fixed chai imports in new test files.

* Addressing more PR feedback.
This commit is contained in:
John Nesky
2024-06-26 12:16:56 -07:00
committed by GitHub
parent be268e3bb4
commit 9a0619aa2a
14 changed files with 469 additions and 14 deletions

View File

@@ -0,0 +1,35 @@
/**
* @license
* Copyright 2024 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {assert} from '../../node_modules/chai/chai.js';
import {
sharedTestSetup,
sharedTestTeardown,
} from './test_helpers/setup_teardown.js';
suite('Comment Drag Event', function () {
setup(function () {
sharedTestSetup.call(this);
this.workspace = new Blockly.Workspace();
});
teardown(function () {
sharedTestTeardown.call(this);
});
suite('Serialization', function () {
test('events round-trip through JSON', function () {
const comment = new Blockly.comments.WorkspaceComment(this.workspace);
comment.setText('test text');
const origEvent = new Blockly.Events.CommentDrag(comment, true);
const json = origEvent.toJson();
const newEvent = new Blockly.Events.fromJson(json, this.workspace);
assert.deepEqual(newEvent, origEvent);
});
});
});

View File

@@ -0,0 +1,38 @@
/**
* @license
* Copyright 2024 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {assert} from '../../node_modules/chai/chai.js';
import {
sharedTestSetup,
sharedTestTeardown,
} from './test_helpers/setup_teardown.js';
suite('Comment Resize Event', function () {
setup(function () {
sharedTestSetup.call(this);
this.workspace = new Blockly.Workspace();
});
teardown(function () {
sharedTestTeardown.call(this);
});
suite('Serialization', function () {
test('events round-trip through JSON', function () {
const comment = new Blockly.comments.WorkspaceComment(this.workspace);
comment.setText('test text');
comment.setSize(new Blockly.utils.Size(100, 100));
const origEvent = new Blockly.Events.CommentResize(comment);
comment.setSize(new Blockly.utils.Size(200, 200));
origEvent.recordCurrentSizeAsNewSize();
const json = origEvent.toJson();
const newEvent = new Blockly.Events.fromJson(json, this.workspace);
assert.deepEqual(newEvent, origEvent);
});
});
});

View File

@@ -862,7 +862,30 @@ suite('Events', function () {
},
}),
},
{
title: 'Comment drag start',
class: Blockly.Events.CommentDrag,
getArgs: (thisObj) => [thisObj.comment, true],
getExpectedJson: (thisObj) => ({
type: 'comment_drag',
group: '',
isStart: true,
commentId: thisObj.comment.id,
}),
},
{
title: 'Comment drag end',
class: Blockly.Events.CommentDrag,
getArgs: (thisObj) => [thisObj.comment, false],
getExpectedJson: (thisObj) => ({
type: 'comment_drag',
group: '',
isStart: false,
commentId: thisObj.comment.id,
}),
},
// TODO(#4577) Test serialization of move event coordinate properties.
// TODO(#4577) Test serialization of comment resize event properties.
];
const testSuites = [
{

View File

@@ -66,6 +66,8 @@
import './event_comment_create_test.js';
import './event_comment_delete_test.js';
import './event_comment_move_test.js';
import './event_comment_drag_test.js';
import './event_comment_resize_test.js';
import './event_marker_move_test.js';
import './event_selected_test.js';
import './event_theme_change_test.js';

View File

@@ -81,6 +81,28 @@ suite('Workspace comment', function () {
);
});
test('resize events are fired when a comment is resized', function () {
this.renderedComment = new Blockly.comments.RenderedWorkspaceComment(
this.workspace,
);
const spy = createChangeListenerSpy(this.workspace);
this.renderedComment.setSize(new Blockly.utils.Size(300, 200));
this.clock.runAll();
assertEventFired(
spy,
Blockly.Events.CommentResize,
{
commentId: this.renderedComment.id,
oldSize: {width: 120, height: 100},
newSize: {width: 300, height: 200},
},
this.workspace.id,
);
});
test('change events are fired when a comments text is edited', function () {
this.renderedComment = new Blockly.comments.RenderedWorkspaceComment(
this.workspace,