简体   繁体   English

如何从块中的代码生成块?

[英]How to generate blocks from code in blockly?

I have a blockly application which generates some output code.我有一个块状应用程序,它生成一些 output 代码。 Now, is it possible to write some function which will take my output code and will put corresponding blocks on workspace.现在,是否可以编写一些 function 将使用我的 output 代码并将相应的块放在工作区中。 For example, on this page, https://developers.google.com/blockly/例如,在这个页面上, https://developers.google.com/blockly/

Blocks are connected to generate javascript code, But is there any way, I will give javascript code and blocks will appear on workspace.连接块以生成 javascript 代码,但是有什么办法,我会给 javascript 代码,并且块会出现在工作区上。

You can only create javascript from blocks, not blocks from javascript.您只能从块中创建 javascript,而不能从 javascript 中创建块。 However, You can export blocks to xml, and import back the xml to blocks.但是,您可以将块导出到 xml,然后将 xml 导入回块。 So you can always save your blocks anywhere you wish in xml format, and load those from xml back to your blockly workspace.因此,您始终可以将您的块以 xml 格式保存在您希望的任何位置,并将它们从 xml 加载回您的块工作区。

function saveBlocks() {
    var xmlDom = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace);
    var xmlText = Blockly.Xml.domToPrettyText(xmlDom);
    // do whatever you want to this xml
}
function loadBlock(xml) { // xml is the same block xml you stored
    if (typeof xml != "string" || xml.length < 5) {
        return false;
    }
    try {
        var dom = Blockly.Xml.textToDom(xml);
        Blockly.mainWorkspace.clear();
        Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, dom);
        return true;
    } catch (e) {
        return false;
    }
}

It can be done - see https://makecode.microbit.org/ .可以做到 - 请参阅https://makecode.microbit.org/ If you create a block then edit as Javascript, you can edit and (if it's valid code) then it will show the correct blocks when you switch back to the block view.如果您创建一个块然后编辑为 Javascript,您可以编辑并且(如果它是有效代码)然后当您切换回块视图时它将显示正确的块。 The javascript is a bit odd that it's generates - so don't expect it to turn any javascript into blocks...它生成的 javascript 有点奇怪 - 所以不要指望它把任何 javascript 变成块......

This isn't part of Blockly - and I'm not sure how this has been done - just that it exists - hope this helps.这不是 Blockly 的一部分 - 我不确定这是如何完成的 - 只是它存在 - 希望这会有所帮助。

I don't remember seeing this feature in blockly, but it is definitely possible.我不记得在blockly 中看到过这个功能,但它绝对是可能的。 You will have to write custom parsers for your language code and then build the blocks accordingly.您必须为您的语言代码编写自定义解析器,然后相应地构建块。

This was somewhat explored in this answer .这在这个答案中有所探讨。

Basically as and when you parse the code, you need to programatically create the blocks and attach them together to create the program in blockly.基本上,当您解析代码时,您需要以编程方式创建块并将它们附加在一起以分块创建程序。

We are working on a python-to-blocks system here https://www.npmjs.com/package/@pi-top/blockly it's a work in progress and is being built to work with the pi-top project website https://further.pi-top.com/ but you should be able to extract something useful, the main code->blocks functionality is in the src/piBlocks folder.我们正在这里开发一个 python-to-blocks 系统https://www.npmjs.com/package/@pi-top/blockly这是一项正在进行的工作,正在构建以与 pi-top 项目网站https 一起使用: //further.pi-top.com/但你应该能够提取一些有用的东西,主要的代码->块功能在 src/piBlocks 文件夹中。 The code is based on this project https://github.com/blockpy-edu/BlockMirror I mainly converted it to typescript, reorganised the code and linted it, with a few additions and bugfixes.代码基于这个项目https://github.com/blockpy-edu/BlockMirror我主要将其转换为 typescript,重新组织代码并对其进行 linted,并进行了一些添加和错误修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM