-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Add JSO hooks for blocks and fields. #5052
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
Add JSO hooks for blocks and fields. #5052
Conversation
| Blockly.Field.prototype.saveState = function() { | ||
| // Default backwards-compatible implementation. | ||
| var container = Blockly.utils.xml.createElement('field'); | ||
| container.setAttribute('name', this.name || ''); | ||
| return Blockly.Xml.domToText(this.toXml(container)); | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this doesn't solve for the case where someone is extending a field other than Field and adding custom serialization. For example, pretend FieldVariable is external so new hooks aren't being added to it. FieldVariable extends FieldDropdown and adds custom serialization with toXml. When we go to serialize it using saveState FieldDropdown's saveState function is hit instead of this one on Fiield. Therefore FieldVariable gets serialized as a dropdown field rather than stringifying its XML implementation.
Sadly I don't think this is solvable :/ We can't do any fancy undefined checking like we do with blocks without requiring devs to explicitly implement serialization on every field (even if they want to use the default implementation).
This is why I hate allowing people to depend on the internal class hierarchy lol.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm so what does this mean for external developers? Any custom field that adds logic to the toXml method will need to add a saveState method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only custom fields with custom toXml method that extend classes other than Blockly.Field. So if you extend Blockly.TextInput, Blockly.FieldDropdown etc, then yeah you have to add a saveState method :/
I think it may be better to rip the backwards compatibility out for now, and explore providing a python script that will add wrapper implementations to developers' block and field files. That will make make it so that our internal code doesn't rely on Blockly.Xml (making it possible to delete later) and it fixes the above problem. I also mention that option here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, that sounds fine with me. If we run into any unknown problems with the script we can add this back before releasing.
e7b373a to
06b58f2
Compare
9e73bed to
ea2f60e
Compare
core/field.js
Outdated
| /** | ||
| * Sets the field's state based on the given state value. Should only be called | ||
| * by the serialization system. | ||
| * Default implementation calls the XLM versions for backwards compatibility. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: XML
| Blockly.Field.prototype.saveState = function() { | ||
| // Default backwards-compatible implementation. | ||
| var container = Blockly.utils.xml.createElement('field'); | ||
| container.setAttribute('name', this.name || ''); | ||
| return Blockly.Xml.domToText(this.toXml(container)); | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm so what does this mean for external developers? Any custom field that adds logic to the toXml method will need to add a saveState method?
|
Note to self:
|
* Add JSON serialiation hooks for fields * Add checking for JSON hooks * Fix other checks and move checks to function * Remove error for both serialization hooks being defined * Fixup comments and errors * Add tests * Add json hooks to block properties * Cleanup * Rip out fragile backwards compatibility
* Add JSON serialiation hooks for fields * Add checking for JSON hooks * Fix other checks and move checks to function * Remove error for both serialization hooks being defined * Fixup comments and errors * Add tests * Add json hooks to block properties * Cleanup * Rip out fragile backwards compatibility
* Add JSON serialiation hooks for fields * Add checking for JSON hooks * Fix other checks and move checks to function * Remove error for both serialization hooks being defined * Fixup comments and errors * Add tests * Add json hooks to block properties * Cleanup * Rip out fragile backwards compatibility
* Add JSON serialiation hooks for fields * Add checking for JSON hooks * Fix other checks and move checks to function * Remove error for both serialization hooks being defined * Fixup comments and errors * Add tests * Add json hooks to block properties * Cleanup * Rip out fragile backwards compatibility
The basics
Link for Diff: BeksOmega/blockly@tests/serialier...BeksOmega:cereal/basic-hooks
The details
Resolves
Work on project cereal.
Dependent on #5034
Proposed Changes
Adds
saveStateandloadStateto fields. These functions wrap the oldtoXmlandfromXmlfunctions for backwards compatibility (as suggested by Sam).Adds
saveExtraStateandloadExtraStateto blocks. These functions do not wrapmutationToDomanddomToMutationbecause mixins don't allow that.As such handling backwards compatibility will need to be done in the JSO serializer itself.
Reason for Changes
We want hooks specific to the JSO system.
Test Coverage
Added tests to make sure the extension system handles the new mutator mixins correctly. I'll add serialization testing when I actually write the serializer.
Documentation
I have a running docs task outside github.
Additional Information
I didn't add hooks for icons yet, because I think that will run into problems with icons not being added in headless mode. I'm going to worry about that after the rest of the serializer is built, because serializing icons is a comparatively small part of serialization.