Uygulamanız istediği zaman kod oluşturabilir. Örneğin, son kullanıcı bir düğmeyi tıkladığında veya kullanıcı her değişiklik yaptığında kod oluşturabilir.
Kod üretimine genel bakış için Kod üretimi başlıklı makaleyi inceleyin.
Dil kod üreten model içe aktarma
Kod oluşturmak için bir dil kodu üreten modele ihtiyacınız vardır. Aşağıdaki yöntemlerden herhangi biriyle dil kodu oluşturucuları içe aktarabilirsiniz.
Modüller
import {javascriptGenerator} from 'blockly/javascript';
import {pythonGenerator} from 'blockly/python';
import {phpGenerator} from 'blockly/php';
import {luaGenerator} from 'blockly/lua';
import {dartGenerator} from 'blockly/dart';
// Generate code for all the blocks in the workspace.
const jsCode = javascriptGenerator.workspaceToCode(workspace);
const pythonCode = pythonGenerator.workspaceToCode(workspace);
const phpCode = phpGenerator.workspaceToCode(workspace);
const luaCode = luaGenerator.workspaceToCode(workspace);
const dartCode = dartGenerator.workspaceToCode(workspace);
Unpkg
Blockly'yi ekledikten sonra oluşturma aracını eklemeniz gerekir.
<script src="https://unpkg.com/blockly"></script>
<script src="https://unpkg.com/blockly/javascript_compressed"></script>
<script src="https://unpkg.com/blockly/python_compressed"></script>
<script src="https://unpkg.com/blockly/php_compressed"></script>
<script src="https://unpkg.com/blockly/lua_compressed"></script>
<script src="https://unpkg.com/blockly/dart_compressed"></script>
// Generate code for all the blocks in the workspace.
const jsCode = javascript.javascriptGenerator.workspaceToCode(workspace);
const pythonCode = python.pythonGenerator.workspaceToCode(workspace);
const phpCode = php.phpGenerator.workspaceToCode(workspace);
const luaCode = lua.luaGenerator.workspaceToCode(workspace);
const dartCode = dart.dartGenerator.workspaceToCode(workspace);
Yerel komut dosyaları
Blockly'yi ekledikten sonra oluşturma aracını eklemeniz gerekir.
<script src="blockly_compressed.js"></script>
<script src="javascript_compressed.js"></script>
<script src="python_compressed.js"></script>
<script src="php_compressed.js"></script>
<script src="lua_compressed.js"></script>
<script src="dart_compressed.js"></script>
// Generate code for all the blocks in the workspace.
const jsCode = javascript.javascriptGenerator.workspaceToCode(workspace);
const pythonCode = python.pythonGenerator.workspaceToCode(workspace);
const phpCode = php.phpGenerator.workspaceToCode(workspace);
const luaCode = lua.luaGenerator.workspaceToCode(workspace);
const dartCode = dart.dartGenerator.workspaceToCode(workspace);
Kod oluşturma
Kod oluşturmak için oluşturucunun workspaceToCode işlevini kullanın.
const code = javascriptGenerator.workspaceToCode(workspace);
Sürekli güncellemeler
Sürekli güncellemeler, kullanıcı bir değişiklik yaptığında kodu göstermenize veya çalıştırmanıza olanak tanır. Kod oluşturma hızlı bir işlem olduğundan bu işlemin performans üzerinde çok az etkisi olur. Bu işlem, etkinlik işleyici kullanılarak yapılır.
const supportedEvents = new Set([
Blockly.Events.BLOCK_CHANGE,
Blockly.Events.BLOCK_CREATE,
Blockly.Events.BLOCK_DELETE,
Blockly.Events.BLOCK_MOVE,
]);
function updateCode(event) {
if (workspace.isDragging()) return; // Don't update while changes are happening.
if (!supportedEvents.has(event.type)) return;
const code = javascriptGenerator.workspaceToCode(workspace);
document.getElementById('textarea').value = code;
}
workspace.addChangeListener(updateCode);
Önsöz veya sonsöz kodu ekleme
Kodunuzu oluşturduktan sonra, isteğe bağlı olarak oluşturulan kodun başlangıcından önce veya sonundan sonra kod ekleyebilirsiniz.
let code = javascriptGenerator.workspaceToCode(workspace);
// Add a preamble and a postscript to the code.
const preamble = 'import {MyLibrary} from \'/path/to/my/library\';\n'
const postscript = 'MyLibrary.myKickoffFunction();\n';
code = preamble + code + postscript;
Ön kod genellikle kodun başına harici tanımlar eklemek için kullanılır. Postscript kodu genellikle kodun sonunda davranış başlatmak için işlevleri çağırmak üzere kullanılır.
Oluşturulan kodunuz olduğu gibi çalıştırılabiliyorsa önsöz veya sonsöz eklemenize gerek yoktur.
Kodu yürütme
Kodu oluşturduktan sonra nasıl çalıştıracağınızı bulmanız gerekir. Bunun nasıl yürütüleceğine karar vermek uygulamaya özel bir durumdur ve Blockly'nin kapsamı dışındadır.
JavaScript kodu yürütmek istiyorsanız JavaScript oluşturma ve çalıştırma başlıklı makaleye bakın. Bu dokümanda, JavaScript kodu üreten modelin bazı özel özelliklerinin yanı sıra Blockly ekibinin JavaScript'i güvenli bir şekilde yürütmek için önerdiği JSInterpreter ele alınmaktadır.
Diğer diller için başka araçlar gerekir.