-
Notifications
You must be signed in to change notification settings - Fork 3.8k
feat: add top level save and load functions for JSO serialization
#5132
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
BeksOmega
merged 3 commits into
RaspberryPiFoundation:project-cereal
from
BeksOmega:cereal/top-level
Aug 6, 2021
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2021 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| /** | ||
| * @fileoverview Contains top-level functions for serializing workspaces to | ||
| * plain JavaScript objects. | ||
| */ | ||
| 'use strict'; | ||
|
|
||
| goog.module('Blockly.serialization.workspaces'); | ||
| goog.module.declareLegacyNamespace(); | ||
|
|
||
| // eslint-disable-next-line no-unused-vars | ||
| const Workspace = goog.require('Blockly.Workspace'); | ||
| const blocks = goog.require('Blockly.serialization.blocks'); | ||
| const variables = goog.require('Blockly.serialization.variables'); | ||
|
|
||
|
|
||
| /** | ||
| * Returns the state of the workspace as a plain JavaScript object. | ||
| * @param {!Workspace} workspace The workspace to serialize. | ||
| * @return {!Object<string, *>} The serialized state of the workspace. | ||
| */ | ||
| const save = function(workspace) { | ||
| const state = Object.create(null); | ||
|
|
||
| // TODO: Switch this to use plugin serialization system (once it is built). | ||
| const variableState = []; | ||
| const vars = workspace.getAllVariables(); | ||
| for (let i = 0; i < vars.length; i++) { | ||
| variableState.push(variables.save(vars[i])); | ||
| } | ||
| if (variableState.length) { | ||
| state['variables'] = variableState; | ||
| } | ||
|
|
||
| const blockState = []; | ||
| for (let block of workspace.getTopBlocks(false)) { | ||
| blockState.push( | ||
| blocks.save(block, {addCoordinates: true})); | ||
| } | ||
| if (blockState.length) { | ||
| // This is an object to support adding language version later. | ||
| state['blocks'] = { | ||
| 'blocks': blockState | ||
| }; | ||
| } | ||
|
|
||
| return state; | ||
| }; | ||
| exports.save = save; | ||
|
|
||
| /** | ||
| * Loads the variable represented by the given state into the given workspace. | ||
| * @param {!Object<string, *>} state The state of the workspace to deserialize | ||
| * into the workspace. | ||
| * @param {!Workspace} workspace The workspace to add the new state to. | ||
| */ | ||
| const load = function(state, workspace) { | ||
| // TODO: Switch this to use plugin serialization system (once it is built). | ||
| // TODO: Add something for clearing the state before deserializing. | ||
|
|
||
| if (state['variables']) { | ||
| const variableStates = state['variables']; | ||
| for (let i = 0; i < variableStates.length; i++) { | ||
| variables.load(variableStates[i], workspace); | ||
| } | ||
| } | ||
|
|
||
| if (state['blocks']) { | ||
| const blockStates = state['blocks']['blocks']; | ||
| for (let i = 0; i < blockStates.length; i++) { | ||
| blocks.load(blockStates[i], workspace); | ||
| } | ||
| } | ||
| }; | ||
| exports.load = load; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.