/// <summary> /// Evaluates block definition code of each block in given object mapping /// block type to XML. Called in order to be able to create instances of the /// blocks in the exporter workspace. /// </summary> /// <param name="blockXmlMap"> Map of block type to XML.</param> public void addBlockDefinitions(Dictionary <string, Element> blockXmlMap) { //var blockDefs = this.getBlockDefinitions(blockXmlMap, "JavaScript"); //Script.Eval(blockDefs); var definitionFormat = "JSON"; foreach (var blockXml in blockXmlMap) { var xml = blockXml.Value; if (xml == null) { continue; } var rootBlock = this.getRootBlockFromXml_(xml); if (rootBlock != null) { // Generate the block's definition. var code = FactoryUtils.getBlockDefinition(blockXml.Key, rootBlock, definitionFormat, this.hiddenWorkspace); // Add block's definition to the definitions to return. Blockly.Core.Blocks[blockXml.Key] = code; } } }
/// <summary> /// Return the given language code of each block type in an array. /// </summary> /// <param name="blockXmlMap"> Map of block type to XML.</param> /// <param name="definitionFormat"> "JSON" or "JavaScript"</param> /// <returns>The concatenation of each block's language code in the /// desired format.</returns> public string getBlockDefinitions(Dictionary <string, Element> blockXmlMap, string definitionFormat) { var blockCode = new JsArray <string>(); foreach (var blockType in blockXmlMap.Keys) { string code; var xml = blockXmlMap[blockType]; if (xml != null) { // Render and get block from hidden workspace. var rootBlock = this.getRootBlockFromXml_(xml); if (rootBlock != null) { // Generate the block's definition. code = FactoryUtils.getBlockDefinition(blockType, rootBlock, definitionFormat, this.hiddenWorkspace); // Add block's definition to the definitions to return. } else { // Append warning comment and write to console. code = "// No block definition generated for " + blockType + ". Could not find root block in XML stored for this block."; Console.WriteLine("No block definition generated for " + blockType + ". Could not find root block in XML stored for this block."); } } else { // Append warning comment and write to console. code = "// No block definition generated for " + blockType + ". Block was not found in Block Library Storage."; Console.WriteLine("No block definition generated for " + blockType + ". Block was not found in Block Library Storage."); } blockCode.Push(code); } // Surround json with [] and comma separate items. if (definitionFormat == "JSON") { return("[" + blockCode.Join(",\n") + "]"); } return(blockCode.Join("\n\n")); }
/// <summary> /// Update the language code based on constructs made in Blockly. /// </summary> public static void updateLanguage(Events.Abstract e) { var rootBlock = FactoryUtils.getRootBlock(BlockFactory.mainWorkspace); if (rootBlock == null) { return; } var blockType = rootBlock.getFieldValue("NAME").Trim().ToLowerCase(); if (blockType == null) { blockType = BlockFactory.UNNAMED; } var format = ((HTMLSelectElement)Document.GetElementById("format")).Value; var code = FactoryUtils.getBlockDefinition(blockType, rootBlock, format, BlockFactory.mainWorkspace); FactoryUtils.injectCode(code, "languagePre"); BlockFactory.updatePreview(); }