/// <summary> /// Return the generator code of each block type in an array in a given language. /// </summary> /// <param name="blockXmlMap"></param> Map of block type to XML. /// <param name="generatorLanguage"> E.g. "JavaScript", "Python", "PHP", "Lua", /// "Dart"</param> /// <returns>The concatenation of each block's generator code in the /// desired format.</returns> public string getGeneratorCode(Dictionary <string, Element> blockXmlMap, string generatorLanguage) { var multiblockCode = new JsArray <string>(); // Define the custom blocks in order to be able to create instances of // them in the exporter workspace. this.addBlockDefinitions(blockXmlMap); foreach (var blockType in blockXmlMap.Keys) { string blockGenCode; var xml = blockXmlMap[blockType]; if (xml != null) { // Render the preview block in the hidden workspace. var tempBlock = FactoryUtils.getDefinedBlock(blockType, this.hiddenWorkspace); // Get generator stub for the given block and add to generator code. blockGenCode = FactoryUtils.getGeneratorStub(tempBlock, generatorLanguage); } else { // Append warning comment and write to console. blockGenCode = "// No generator stub generated for " + blockType + ". Block was not found in Block Library Storage."; Console.WriteLine("No block generator stub generated for " + blockType + ". Block was not found in Block Library Storage."); } multiblockCode.Push(blockGenCode); } return(multiblockCode.Join("\n\n")); }
/// <summary> /// Given a set of block types, gets the Blockly.Block objects for each block /// type. /// </summary> /// <param name="blockTypes">Array of blocks that have been defined.</param> /// <returns>Array of Blockly.Block objects corresponding /// to the array of blockTypes.</returns> public JsArray <Block> getDefinedBlocks(JsArray <string> blockTypes) { var blocks = new JsArray <Blockly.Block>(); for (var i = 0; i < blockTypes.Length; i++) { blocks.Push(FactoryUtils.getDefinedBlock(blockTypes[i], this.hiddenWorkspace)); } return(blocks); }
/// <summary> /// Generate XML for the workspace factory's category from imported block /// definitions. /// </summary> /// <param name="blockLibStorage">Block Library Storage object.</param> /// <returns>XML representation of a category.</returns> public Element generateCategoryFromBlockLib(BlockLibraryStorage blockLibStorage) { var allBlockTypes = blockLibStorage.getBlockTypes(); // Object mapping block type to XML. var blockXmlMap = blockLibStorage.getBlockXmlMap(allBlockTypes); // Define the custom blocks in order to be able to create instances of // them in the exporter workspace. this.addBlockDefinitions(blockXmlMap); // Get array of defined blocks. var blocks = new JsArray <Blockly.Block>(); foreach (var blockType in blockXmlMap.Keys) { var block = FactoryUtils.getDefinedBlock(blockType, this.hiddenWorkspace); blocks.Push(block); } return(FactoryUtils.generateCategoryXml(blocks, "Block Library")); }
/// <summary> /// Generate selector dom from block library storage. For each block in the /// library, it has a block option, which consists of a checkbox, a label, /// and a fixed size preview workspace. /// </summary> /// <param name="blockLibStorage"> Block Library Storage object.</param> /// <param name="blockSelectorId"> ID of the div element that will contain /// the block options.</param> /// <returns>Map of block type to Block Option object.</returns> public Dictionary <string, BlockOption> createBlockSelectorFromLib(BlockLibraryStorage blockLibStorage, string blockSelectorId) { // Object mapping each stored block type to XML. var allBlockTypes = blockLibStorage.getBlockTypes(); var blockXmlMap = blockLibStorage.getBlockXmlMap(allBlockTypes); // Define the custom blocks in order to be able to create instances of // them in the exporter workspace. this.addBlockDefinitions(blockXmlMap); var blockSelector = Document.GetElementById(blockSelectorId); // Clear the block selector. Node child; while ((child = blockSelector.FirstChild) != null) { blockSelector.RemoveChild(child); } // Append each block option's dom to the selector. var blockOptions = new Dictionary <string, BlockOption>(); foreach (var blockType in blockXmlMap.Keys) { // Get preview block's XML. var block = FactoryUtils.getDefinedBlock(blockType, this.hiddenWorkspace); var previewBlockXml = Blockly.Xml.workspaceToDom(this.hiddenWorkspace); // Create block option, inject block into preview workspace, and append // option to block selector. var blockOpt = new BlockOption(blockSelector, blockType, previewBlockXml); blockOpt.createDom(); blockSelector.AppendChild(blockOpt.dom); blockOpt.showPreviewBlock(); blockOptions[blockType] = blockOpt; } return(blockOptions); }
/// <summary> /// Pulls information about all blocks in the block library to generate XML /// for the selector workpace's toolbox. /// </summary> /// <param name="blockLibStorage"> Block Library Storage object.</param> /// <returns>XML representation of the toolbox.</returns> Element generateToolboxFromLibrary(BlockLibraryStorage blockLibStorage) { // Create DOM for XML. var xmlDom = goog.dom.createDom("xml", new Dictionary <string, string> { { "id", "blockExporterTools_toolbox" }, { "style", "display:none" } }); var allBlockTypes = blockLibStorage.getBlockTypes(); // Object mapping block type to XML. var blockXmlMap = blockLibStorage.getBlockXmlMap(allBlockTypes); // Define the custom blocks in order to be able to create instances of // them in the exporter workspace. this.addBlockDefinitions(blockXmlMap); foreach (var blockType in blockXmlMap.Keys) { // Get block. var block = FactoryUtils.getDefinedBlock(blockType, this.hiddenWorkspace); var category = FactoryUtils.generateCategoryXml(new JsArray <Blockly.Block> { block }, blockType); xmlDom.AppendChild(category); } // If there are no blocks in library and the map is empty, append dummy // category. if (blockXmlMap.Count == 0) { var category = goog.dom.createDom("category"); category.SetAttribute("name", "Next Saved Block"); xmlDom.AppendChild(category); } return(xmlDom); }