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

feat: add block factory export in json #8051

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions demos/blockfactory/app_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ AppController.prototype.exportBlockLibraryToFile = function() {
}
};

AppController.prototype.exportBlockLibraryAsJson = function() {
const blockJson = this.blockLibraryController.getBlockLibraryAsJson();
if (blockJson.length === 0) {
alert('No blocks in library to export');
return;
}
const filename = 'legacy_block_factory_export.txt';
FactoryUtils.createAndDownloadFile(JSON.stringify(blockJson), filename, 'plain');
};

/**
* Converts an object mapping block type to XML to text file for output.
* @param {!Object} blockXmlMap Object mapping block type to XML.
Expand Down Expand Up @@ -491,6 +501,10 @@ AppController.prototype.assignBlockFactoryClickHandlers = function() {
self.exportBlockLibraryToFile();
});

document.getElementById('exportAsJson').addEventListener('click', function() {
self.exportBlockLibraryAsJson();
});

document.getElementById('helpButton').addEventListener('click',
function() {
open('https://developers.google.com/blockly/custom-blocks/block-factory',
Expand Down
23 changes: 23 additions & 0 deletions demos/blockfactory/block_library_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,29 @@ BlockLibraryController.prototype.getBlockLibrary = function() {
return this.storage.getBlockXmlTextMap();
};

/**
* @return {Object[]} Array of JSON data, where each item is the data for one block type.
*/
BlockLibraryController.prototype.getBlockLibraryAsJson = function() {
const xmlBlocks = this.storage.getBlockXmlMap(this.storage.getBlockTypes());
const jsonBlocks = [];
const headlessWorkspace = new Blockly.Workspace();

for (const blockName in xmlBlocks) {
// Load the block XML into a workspace so we can save it as JSON
headlessWorkspace.clear();
const blockXml = xmlBlocks[blockName];
Blockly.Xml.domToWorkspace(blockXml, headlessWorkspace);
const block = headlessWorkspace.getBlocksByType('factory_base', false)[0];

if (!block) continue;

const json = Blockly.serialization.blocks.save(block, {addCoordinates: false, saveIds: false});
jsonBlocks.push(json);
}
return jsonBlocks;
}

/**
* Return stored XML of a given block type.
* @param {string} blockType The type of block.
Expand Down
3 changes: 3 additions & 0 deletions demos/blockfactory/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ <h3>Preview:
<button id="localSaveButton" title="Save block library XML to a local file.">
<span>Download Block Library</span>
</button>
<button id="exportAsJson" title="Export block library for import into the new Block Factory.">
<span>Export Block Library</span>
</button>
</td>
</tr>
</table>
Expand Down
Loading