Skip to content
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

[Google Blockly] Unregister context menu items #50672

Merged
merged 5 commits into from
Mar 15, 2023

Conversation

mikeharv
Copy link
Contributor

@mikeharv mikeharv commented Mar 9, 2023

The workspace context menu option "Clean up blocks" calls a method that currently has a bug in which immovable blocks are ignored:

Initial block layout and context menu options Result when selecting "clean up blocks"
Screen Shot 2023-03-08 at 10 09 36 AM (1) Screen Shot 2023-03-08 at 10 09 43 AM (1)

https://codedotorg.slack.com/archives/C03DBDN67B7/p1678288305437829
google/blockly#6889

The Blockly team has agreed that this is a bug they are interested in fixing. This bug affects nearly all Google Blockly levels because of our convention to make when run immovable. Fortunately, we have never broadcast that this option exists; in fact it was only recently discovered by the curriculum team. In the meantime, we can remove the context menu option.

While looking into this, I discovered three other context menu options that I believe should also be unregistered. The options to expand and collapse blocks "work", but not a behavior that we've ever surfaced to users before. Similarly, the option to delete all blocks from the workspace is probably more aggressive than we want. (Users are already trained to click "Start Over" if they want to get back to an initial state for their code.) This last option also produces a confirmation modal dialog that we haven't customized.

Collapsed blocks:
image

Delete all:
image

Links

Slack thread: https://codedotorg.slack.com/archives/C03DBDN67B7/p1678288305437829
Blockly bug report: google/blockly#6889

Follow-up work

Communicate with Blockly about the status of this report and consider reregistering the menu option once fixed.

PR Checklist:

  • Tests provide adequate coverage
  • Privacy and Security impacts have been assessed
  • Code is well-commented
  • New features are translatable or updates will not break translations
  • Relevant documentation has been added or updated
  • User impact is well-understood and desirable
  • Pull Request is labeled appropriately
  • Follow-up work items (including potential tech debt) are tracked and linked

@mikeharv mikeharv requested a review from a team March 9, 2023 13:59
Copy link
Contributor

@bencodeorg bencodeorg left a comment

Choose a reason for hiding this comment

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

LGTM -- nit on PR description, could you use different titles in the "Before" / "After" screenshots you included that clarify that those screenshots aren't before/after implementing this PR (as I understand it)?

@@ -270,6 +270,19 @@ function initializeBlocklyWrapper(blocklyInstance) {
CdoRendererZelos,
true /* opt_allowOverrides */
);
// cleanUp() doesn't currently account for immovable blocks.
blocklyWrapper.blockly_.ContextMenuRegistry.registry.unregister(
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: might make sense organizationally to put these in contextMenu.js and add a function like unregisterContextMenuItems()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good suggestion. Done!

GoogleBlockly.ContextMenuRegistry.registry.unregister('collapseWorkspace');
GoogleBlockly.ContextMenuRegistry.registry.unregister('expandWorkspace');
GoogleBlockly.ContextMenuRegistry.registry.unregister('workspaceDelete');
} catch (error) {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this here to catch if a context menu option isn't in the list? Could we just use optional chaining?

Copy link
Contributor Author

@mikeharv mikeharv Mar 13, 2023

Choose a reason for hiding this comment

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

It's actually to make sure the wrapper's unit test keeps passing.
I think that we're not really cleaning up its state properly since the same instance of GoogleBlockly is used and fed into initializeBlocklyWrapper multiple times. With the try/catch, the test doesn't crash if we've already unregistered things.
So I guess this is admittedly more of a bandaid than reworking the entire test file. That being said, if core Blockly did ever change their default registry out from under us, this code wouldn't break. I'd suggest we leave it regardless of how soon we refactor the test.

Drone log, if you're interested: https://drone.cdn-code.org/code-dot-org/code-dot-org/26802/1/4


FAILED TESTS:
--
5508 | Google Blockly Wrapper
5509 | ✖ "before each" hook for "getGenerator returns the JS Generator"
5510 | HeadlessChrome 0.0.0 (Linux 0.0.0)
5511 | Error: Menu item with ID "cleanWorkspace" not found.
5512 | at ContextMenuRegistry$module$build$src$core$contextmenu_registry.unregister (webpack://blockly-mooc/./node_modules/blockly/blockly_compressed.js?:11098:41)
5513 | at initializeBlocklyWrapper (webpack://blockly-mooc/./src/blockly/googleBlocklyWrapper.js?:297:56)
5514 | at Context.eval (webpack://blockly-mooc/./test/unit/blockly/googleBlocklyWrapperTest.js?:21:52)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah ok, a couple thoughts then -- the last line of this section is confusing then, seems like that's what it's intended to do?

afterEach(() => {
// Reset Blockly for other tests.
Blockly = cdoBlockly; // eslint-disable-line no-global-assign
// Reset context menu for other tests.
GoogleBlockly.ContextMenuRegistry.registry.reset();
});

If you can't figure out why that isn't doing what we'd expect/don't want to figure it out now, could you catch a more specific error (maybe not, looks generic from the log you shared)? At the very least, a comment noting why we're catching all errors here that more explicitly notes that it is for tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So all the code in that init method is run multiple times on the same GoogleBlockly instance without that instance ever being reset. Resetting the context menu here had been working because previously we were only registering new options to the context menu, not unregistering existing ones from it. I can look at this again a little later on. Thanks!

@mikeharv mikeharv merged commit ab097e0 into staging Mar 15, 2023
@mikeharv mikeharv deleted the mike/remove-cleanup-option branch March 15, 2023 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants