blockly-samples icon indicating copy to clipboard operation
blockly-samples copied to clipboard

feat: created plugin to reify shadow blocks.

Open johnnesky opened this issue 3 years ago • 2 comments

Fixes #878. This plugin exports a function shadowBlockConversionChangeListener that, when added to a workspace as a change listener, detects BlockChange events on shadow blocks and responds by converting the block (and any parent shadow blocks via output or previous connections) into real blocks.

It also exports a new undoable BlockShadowChange event class. If this plugin is ever merged into the main repository, the new event type can probably be merged with BlocklyChange events, and the new change listener function logic can probably be mostly moved into Blockly.Field.prototype.setValue.

The change listener attempts to group the new shadow change events with the initiating block change event. ~~As a hack, it retroactively overwrites the group property of the initiating event if it doesn't already have a group name, in order to be able to group events with it. We can delete this hack, but then changes to~~ boolean fields of blocks will not be grouped with the shadow change events for undo purposes, because those events do not have group names by default. Other field types such as text and number will still be grouped with shadow change events.

As a subtlety, the change listener ignores changes that occur while undoing or redoing. Only new user events should result in shadow blocks getting converted to real blocks.

See also the closed PR https://github.com/google/blockly/pull/319 to add this feature to the main Blockly repository. It came with a feature flag to enable the feature, which may be useful if this plugin is ever merged into the main repository.

johnnesky avatar Sep 23 '22 21:09 johnnesky

Obviously I'm wrestling with test failures in my PR, although I don't know how to reproduce the failure locally. It seems to have something to do with me using async functions in the test.mocha.js file, resulting in the error message: "ReferenceError: regeneratorRuntime is not defined". I see some articles online suggesting additions to the webpack configuration. Should I try to mess with that?

I noticed the existing test file https://github.com/google/blockly-samples/blob/master/plugins/migration/test/rename.mocha.js uses async successfully, although I think it is configured somewhat differently.

Uh I guess worst case scenario I could just return explicit promises?

The reason why I'm using async functions is because I'm testing how a workspace change listener responds to events, and events are broadcast asynchronously. Is there a way to broadcast them synchronously? I saw that there was a "fireNow" function in event utils but I couldn't figure out a way to call it from the test file.

johnnesky avatar Sep 23 '22 23:09 johnnesky

At the suggestion of @rachel-fenichel and @BeksOmega I used sinon fake time instead of async functions, and that seems to resolve test failures, thanks!

johnnesky avatar Sep 28 '22 00:09 johnnesky