Kod oluşturma ve çalıştırma

Uygulamanız istediğiniz zaman kod oluşturabilir. Örneğin, son kullanıcı bir düğmeyi tıkladığında veya her değişiklik yaptığında kod oluşturabilir.

Kod oluşturmaya genel bakış için Kod oluşturma bölümüne bakın.

Dil kodu oluşturucu içe aktarma

Kod oluşturmak için bir dil kodu oluşturucuya ihtiyacınız vardır. Dil kodu oluşturucuları aşağıdaki yöntemlerden herhangi birini kullanarak 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 jeneratörü 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 jeneratörü 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 işlemi hızlı olduğundan bu işlem performansı çok az etkiler. Bu işlem bir 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 son ek 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;

Giriş kodu genellikle kodun başına harici tanımlar eklemek için kullanılır. Son not kodu genellikle kodun sonunda davranışı başlatmak için işlevleri çağırmak amacıyla kullanılır.

Oluşturulan kodunuz olduğu gibi çalıştırılabilir durumdaysa önsöz veya son ek eklemeniz gerekmez.

Kodu yürütme

Kodu oluşturduktan sonra nasıl çalıştıracağınızı belirlemeniz gerekir. Bu işlemin nasıl yürütüleceğine karar vermek uygulamaya özgüdür ve Blockly'nin kapsamı dışındadır.

JavaScript kodu yürütmek istiyorsanız JavaScript oluşturma ve çalıştırma başlıklı makaleyi inceleyin. Bu makalede, JavaScript kod oluşturucunun bazı özel özellikleri ve Blockly ekibinin JavaScript'i güvenli bir şekilde yürütmek için önerdiği JSInterpreter hakkında bilgi verilmektedir.

Diğer diller için farklı araçlar gerekir.