| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017 |
- import * as Blockly from "blockly";
- import 'blockly/msg/zh-hans';
- import {javascriptGenerator} from "blockly/javascript";
- import {pythonGenerator} from "blockly/python";
- // 导入图片资源
- import about_turnImage from '@/assets/images/blockly/component/about_turn.png';
- import move_backwardImage from '@/assets/images/blockly/component/move_backward.png';
- import move_forwardImage from '@/assets/images/blockly/component/move_forward.png';
- import pick_upImage from '@/assets/images/blockly/component/pick_up.png';
- import turn_leftImage from '@/assets/images/blockly/component/turn_left.png';
- import turn_rightImage from '@/assets/images/blockly/component/turn_right.png';
- import useImage from '@/assets/images/blockly/component/use.png';
- import pauseImage from '@/assets/images/blockly/component/pause.png';
- import play_soundImage from '@/assets/images/blockly/component/sound.png';
- import constructImage from '@/assets/images/blockly/component/construct.png';
- import lightImage from '@/assets/images/blockly/component/light.png';
- import repeatImage from '@/assets/images/blockly/component/repeat.png';
- import ifImage from '@/assets/images/blockly/component/if.png';
- import ifElseImage from '@/assets/images/blockly/component/if_else.png';
- import lightRedImage from '@/assets/images/blockly/light/red.png';
- import lightYellowImage from '@/assets/images/blockly/light/yellow.png';
- import lightGreenImage from '@/assets/images/blockly/light/green.png';
- import {DICT_TYPE} from '@/utils/dictUtils.js';
- // 安全地获取和解析字典数据
- const getDictDataFromStorage = (dictType) => {
- try {
- const data = localStorage.getItem(dictType);
- return data ? JSON.parse(data) : [];
- } catch (error) {
- console.error(`解析${dictType}字典数据失败:`, error);
- return [];
- }
- };
- // 地图元素类型字典
- const savedMapTypeDict = getDictDataFromStorage(DICT_TYPE.AI_BLOCKLY_MAP_TYPE);
- const BLOCKLY_MAP_TYPE_DICT = savedMapTypeDict.reduce((acc, item) => {
- acc[item.value.toUpperCase()] = item.value;
- return acc;
- }, {});
- // 特殊积木字典
- const blocklyMapSpecialDict = getDictDataFromStorage(DICT_TYPE.BLOCKLY_MAP_SPECIAL);
- const BLOCKLY_MAP_SPECIAL_DICT = blocklyMapSpecialDict.reduce((acc, item) => {
- acc[item.value.toUpperCase()] = item.value;
- return acc;
- }, {});
- // 地图标记字典
- const blocklyMapMarkDict = getDictDataFromStorage(DICT_TYPE.BLOCKLY_MAP_MARK);
- const BLOCKLY_MAP_MARK_DICT = blocklyMapMarkDict.reduce((acc, item) => {
- acc[item.value.toUpperCase()] = {
- value: item.value,
- label: item.label,
- img: item.remark
- };
- return acc;
- }, {});
- // 灯光颜色字典
- const BLOCKLY_LIGHT_COLOR_DICT = [
- {
- label: '红色',
- color: [233, 77, 88],
- img: lightRedImage,
- },
- {
- label: '黄色',
- color: [233, 211, 36],
- img: lightYellowImage,
- },
- {
- label: '绿色',
- color: [118, 187, 45],
- img: lightGreenImage,
- },
- ];
- /**
- * 定义所有可用的自定义积木配置
- */
- const availableBlocks = {
- // 通用积木 - 始终可用(移动控制)
- move_forward: {
- jsonConfig: {
- "type": "move_forward",
- "message0": "%1 向前移动",
- "args0": [
- {
- "type": "field_image",
- "src": move_forwardImage,
- "width": 20,
- "height": 20,
- "alt": "向前移动"
- }
- ],
- "previousStatement": null,
- "nextStatement": null,
- "colour": 230,
- "tooltip": "控制角色向前移动一格",
- "helpUrl": ""
- },
- isGeneral: true // 标记为通用积木
- },
- move_forward_param: {
- jsonConfig: {
- "type": "move_forward_param",
- "message0": "%1 向前移动 %2 次",
- "args0": [
- {
- "type": "field_image",
- "src": move_forwardImage,
- "width": 20,
- "height": 20,
- "alt": "向前移动多次"
- },
- {
- "type": "field_number",
- "name": "PARAM",
- "value": 1,
- "min": 1,
- "max": 10,
- "precision": 1
- }
- ],
- "previousStatement": null,
- "nextStatement": null,
- "colour": 230,
- "tooltip": "控制角色向前移动指定次数",
- "helpUrl": ""
- },
- isGeneral: true // 标记为通用积木
- },
- move_backward: {
- jsonConfig: {
- "type": "move_backward",
- "message0": "%1 向后移动",
- "args0": [
- {
- "type": "field_image",
- "src": move_backwardImage,
- "width": 20,
- "height": 20,
- "alt": "向后移动"
- }
- ],
- "previousStatement": null,
- "nextStatement": null,
- "colour": 230,
- "tooltip": "控制角色向后移动一格",
- "helpUrl": ""
- },
- isGeneral: true
- },
- move_backward_param: {
- jsonConfig: {
- "type": "move_backward_param",
- "message0": "%1 向后移动 %2 次",
- "args0": [
- {
- "type": "field_image",
- "src": move_backwardImage,
- "width": 20,
- "height": 20,
- "alt": "向后移动多次"
- },
- {
- "type": "field_number",
- "name": "PARAM",
- "value": 1,
- "min": 1,
- "max": 10,
- "precision": 1
- }
- ],
- "previousStatement": null,
- "nextStatement": null,
- "colour": 230,
- "tooltip": "控制角色向后移动指定次数",
- "helpUrl": ""
- },
- isGeneral: true
- },
- turn_left: {
- jsonConfig: {
- "type": "turn_left",
- "message0": "%1 向左转",
- "args0": [
- {
- "type": "field_image",
- "src": turn_leftImage,
- "width": 20,
- "height": 20,
- "alt": "向左转"
- }
- ],
- "previousStatement": null,
- "nextStatement": null,
- "colour": 230,
- "tooltip": "控制角色向左转",
- "helpUrl": ""
- },
- isGeneral: true
- },
- turn_right: {
- jsonConfig: {
- "type": "turn_right",
- "message0": "%1 向右转",
- "args0": [
- {
- "type": "field_image",
- "src": turn_rightImage,
- "width": 20,
- "height": 20,
- "alt": "向右转"
- }
- ],
- "previousStatement": null,
- "nextStatement": null,
- "colour": 230,
- "tooltip": "控制角色向右转",
- "helpUrl": ""
- },
- isGeneral: true
- },
- turn_around: {
- jsonConfig: {
- "type": "turn_around",
- "message0": "%1 向后转",
- "args0": [
- {
- "type": "field_image",
- "src": about_turnImage,
- "width": 20,
- "height": 20,
- "alt": "向后转"
- }
- ],
- "previousStatement": null,
- "nextStatement": null,
- "colour": 230,
- "tooltip": "控制角色向后转",
- "helpUrl": ""
- },
- isGeneral: true
- },
- // 通用积木 - 始终可用(功能积木)
- pickup_item: {
- jsonConfig: {
- "type": "pickup_item",
- "message0": "%1 拾取物品",
- "args0": [
- {
- "type": "field_image",
- "src": pick_upImage,
- "width": 20,
- "height": 20,
- "alt": "拾取物品"
- }
- ],
- "previousStatement": null,
- "nextStatement": null,
- "colour": 30,
- "tooltip": "尝试拾取当前位置的物品",
- "helpUrl": ""
- },
- isGeneral: true
- },
- use_item: {
- jsonConfig: {
- "type": "use_item",
- "message0": "%1 使用物品",
- "args0": [
- {
- "type": "field_image",
- "src": useImage,
- "width": 20,
- "height": 20,
- "alt": "使用物品"
- }
- ],
- "previousStatement": null,
- "nextStatement": null,
- "colour": 30,
- "tooltip": "在当前位置使用物品",
- "helpUrl": ""
- },
- isGeneral: true
- },
- // 逻辑积木 - 自定义如果否则带图标
- controls_if: {
- jsonConfig: {
- "type": "controls_if",
- "message0": "%1 如果 %2",
- "args0": [
- {
- "type": "field_image",
- "src": ifImage,
- "width": 20,
- "height": 20,
- "alt": "条件"
- },
- {
- "type": "input_value",
- "name": "IF0",
- "check": "Boolean"
- }
- ],
- "message1": "执行%1",
- "args1": [
- {
- "type": "input_statement",
- "name": "DO0"
- }
- ],
- "previousStatement": null,
- "nextStatement": null,
- "colour": 210,
- "tooltip": "如果条件为真,则执行相应的代码。",
- "helpUrl": "",
- "mutator": "controls_if_mutator"
- },
- isGeneral: true // 标记为通用积木
- },
- // 循环积木 - 自定义重复执行带图标
- controls_repeat_ext: {
- jsonConfig: {
- "type": "controls_repeat_ext",
- "message0": "%1 重复 %2 次",
- "args0": [
- {
- "type": "field_image",
- "src": repeatImage,
- "width": 25,
- "height": 25,
- "alt": "循环"
- },
- {
- "type": "field_number",
- "name": "TIMES",
- "value": 1,
- "min": 1,
- "max": 100
- }
- ],
- "message1": "执行 %1",
- "args1": [
- {
- "type": "input_statement",
- "name": "DO"
- }
- ],
- "previousStatement": null,
- "nextStatement": null,
- "colour": "#5BA55B",
- "tooltip": "重复执行内部代码指定次数",
- "helpUrl": ""
- },
- isGeneral: true // 标记为通用积木
- },
- // 循环积木 - while循环
- controls_whileUntil: {
- jsonConfig: {
- "type": "controls_whileUntil",
- "message0": "%1 %2 %3",
- "args0": [
- {
- "type": "field_image",
- "src": repeatImage,
- "width": 25,
- "height": 25,
- "alt": "循环"
- },
- {
- "type": "field_dropdown",
- "name": "MODE",
- "options": [
- ["当", "WHILE"],
- ["直到", "UNTIL"]
- ]
- },
- {
- "type": "input_value",
- "name": "CONDITION",
- "check": "Boolean"
- }
- ],
- "message1": "执行 %1",
- "args1": [
- {
- "type": "input_statement",
- "name": "DO"
- }
- ],
- "previousStatement": null,
- "nextStatement": null,
- "colour": "#5BA55B",
- "tooltip": "当条件为真时重复执行内部代码",
- "helpUrl": ""
- },
- isGeneral: true // 标记为通用积木
- },
- // 当经过某个形状的积木(可作为参数使用的value类型)
- when_passed: {
- jsonConfig: {
- "type": "when_passed",
- "message0": "当经过 : %1",
- "args0": [
- {
- "type": "field_dropdown",
- "name": "SHAPE",
- "options": Object.values(BLOCKLY_MAP_MARK_DICT).map(shape => [
- {
- "src": shape.img,
- "width": 30,
- "height": 30,
- "alt": shape.label
- },
- shape.value
- ])
- }
- ],
- "output": "Boolean",
- "colour": 30,
- "tooltip": "检查角色是否经过指定形状,可作为参数使用",
- "helpUrl": ""
- },
- isGeneral: true
- },
- // 是否到达终点积木
- is_at_end: {
- jsonConfig: {
- "type": "is_at_end",
- "message0": "到达终点",
- "output": "Boolean",
- "colour": 10,
- "tooltip": "检查角色是否到达终点位置",
- "helpUrl": ""
- },
- isGeneral: true
- },
- // 特殊积木 - 可根据配置动态加载
- pause: {
- jsonConfig: {
- "type": "pause",
- "message0": "%1 暂停 %2 秒",
- "args0": [
- {
- "type": "field_image",
- "src": pauseImage,
- "width": 20,
- "height": 20,
- "alt": "暂停"
- },
- {
- "type": "field_number",
- "name": "SECONDS",
- "value": 1,
- "min": 1,
- "max": 10,
- "precision": 1
- }
- ],
- "previousStatement": null,
- "nextStatement": null,
- "colour": 0,
- "tooltip": "让角色在原地停留指定的秒数",
- "helpUrl": ""
- },
- isGeneral: false
- },
- play_sound: {
- jsonConfig: {
- "type": "play_sound",
- "message0": "%1 声音: %2",
- "args0": [
- {
- "type": "field_image",
- "src": play_soundImage,
- "width": 20,
- "height": 20,
- "alt": "声音"
- },
- {
- "type": "field_dropdown",
- "name": "SOUND",
- "options": [
- ["打招呼", "打招呼"],
- ["铃铛", "铃铛"],
- ]
- }
- ],
- "previousStatement": null,
- "nextStatement": null,
- "colour": 160,
- "tooltip": "播放指定的声音特效",
- "helpUrl": ""
- },
- isGeneral: false
- },
- construct: {
- jsonConfig: {
- "type": "construct",
- "message0": "%1 修建",
- "args0": [
- {
- "type": "field_image",
- "src": constructImage,
- "width": 20,
- "height": 20,
- "alt": "修建"
- }
- ],
- "previousStatement": null,
- "nextStatement": null,
- "colour": 160,
- "tooltip": "修建处理",
- "helpUrl": ""
- },
- isGeneral: false
- },
- light: {
- jsonConfig: {
- "type": "light",
- "message0": "%1 灯光 %2",
- "args0": [
- {
- "type": "field_image",
- "src": lightImage,
- "width": 25,
- "height": 25,
- "alt": "灯光"
- },
- {
- "type": "field_dropdown",
- "name": "COLOR",
- "options": BLOCKLY_LIGHT_COLOR_DICT.map(item => [
- {
- "src": item.img,
- "width": 20,
- "height": 20,
- "alt": item.label
- },
- item.label
- ])
- }
- ],
- "previousStatement": null,
- "nextStatement": null,
- "colour": 30,
- "tooltip": "在当前位置产生灯光效果",
- "helpUrl": ""
- },
- isGeneral: false
- }
- };
- /**
- * 定义所有可用的JavaScript生成器配置
- */
- const availableGenerators = {
- // 通用生成器 - 始终可用
- move_forward: function(block) {
- return 'await moveForward();\n';
- },
- move_forward_param: function(block) {
- const stepCount = block.getFieldValue('PARAM');
- return `await moveForward(${stepCount});\n`;
- },
- move_backward: function(block) {
- return 'await moveBackward();\n';
- },
- move_backward_param: function(block) {
- const stepCount = block.getFieldValue('PARAM');
- return `await moveBackward(${stepCount});\n`;
- },
- turn_left: function(block) {
- return 'await turnLeft();\n';
- },
- turn_right: function(block) {
- return 'await turnRight();\n';
- },
- turn_around: function(block) {
- return 'await turnAround();\n';
- },
- pickup_item: function(block) {
- return 'await pickupItem();\n';
- },
- use_item: function(block) {
- return 'await useItem();\n';
- },
- when_passed: function(block) {
- const shape = block.getFieldValue('SHAPE');
- return [`whenPassed('${shape}')`, javascriptGenerator.ORDER_FUNCTION_CALL];
- },
- is_at_end: function(block) {
- return [`isAtEnd()`, javascriptGenerator.ORDER_FUNCTION_CALL];
- },
- // 条件判断生成器 - 使用Blockly默认实现方式确保兼容性
- controls_if: function(block) {
- let code = '';
- let branchCode, conditionCode;
- let n = 0;
- // 生成if条件
- if (block.getInput('IF0')) {
- conditionCode = javascriptGenerator.valueToCode(block, 'IF0', javascriptGenerator.ORDER_NONE) || 'false';
- branchCode = javascriptGenerator.statementToCode(block, 'DO0');
- code += `if (${conditionCode}) {
- ${javascriptGenerator.prefixLines(branchCode, javascriptGenerator.INDENT)}}
- `;
- n = 1;
- }
- // 生成else if条件
- for (; block.getInput('IF' + n); n++) {
- conditionCode = javascriptGenerator.valueToCode(block, 'IF' + n, javascriptGenerator.ORDER_NONE) || 'false';
- branchCode = javascriptGenerator.statementToCode(block, 'DO' + n);
- code += `else if (${conditionCode}) {
- ${javascriptGenerator.prefixLines(branchCode, javascriptGenerator.INDENT)}}
- `;
- }
- // 生成else条件
- if (block.getInput('ELSE')) {
- const elseCode = javascriptGenerator.statementToCode(block, 'ELSE');
- code += `else {
- ${javascriptGenerator.prefixLines(elseCode, javascriptGenerator.INDENT)}}
- `;
- }
- return code;
- },
- controls_repeat_ext: function(block) {
- const repeats = block.getFieldValue('TIMES');
- const branch = javascriptGenerator.statementToCode(block, 'DO');
- const code = `for (let i = 0; i < ${repeats}; i++) {\n${javascriptGenerator.prefixLines(branch, javascriptGenerator.INDENT)}}\n`;
- return code;
- },
- controls_whileUntil: function(block) {
- const until = block.getFieldValue('MODE') === 'UNTIL';
- const condition = javascriptGenerator.valueToCode(block, 'CONDITION', javascriptGenerator.ORDER_NONE) || 'false';
- const branch = javascriptGenerator.statementToCode(block, 'DO');
- const code = until ? `while (!((${condition})) && loopCount < 100) {\n${javascriptGenerator.prefixLines(branch, javascriptGenerator.INDENT)}loopCount++;\n}` : `while ((${condition}) && loopCount < 100) {\n${javascriptGenerator.prefixLines(branch, javascriptGenerator.INDENT)}loopCount++;\n}`;
- return code;
- },
- // 特殊生成器 - 可根据配置动态加载
- pause: function(block) {
- const seconds = block.getFieldValue('SECONDS');
- return `await pause(${seconds});\n`;
- },
- play_sound: function(block) {
- const sound = block.getFieldValue('SOUND');
- return `await playSound('${sound}');
- `;
- },
- construct: function(block) {
- return 'await construct();\n';
- },
- light: function(block) {
- const color = block.getFieldValue('COLOR');
- return `await light('${color}');
- `;
- }
- };
- /**
- * 定义所有可用的Python生成器配置
- */
- const availablePythonGenerators = {
- // 通用生成器 - 始终可用
- move_forward: function(block) {
- return 'move_forward()\n';
- },
- move_forward_param: function(block) {
- const stepCount = block.getFieldValue('PARAM');
- return `move_forward(${stepCount})\n`;
- },
- move_backward: function(block) {
- return 'move_backward()\n';
- },
- move_backward_param: function(block) {
- const stepCount = block.getFieldValue('PARAM');
- return `move_backward(${stepCount})\n`;
- },
- turn_left: function(block) {
- return 'turn_left()\n';
- },
- turn_right: function(block) {
- return 'turn_right()\n';
- },
- turn_around: function(block) {
- return 'turn_around()\n';
- },
- pickup_item: function(block) {
- return 'pickup_item()\n';
- },
- use_item: function(block) {
- return 'use_item()\n';
- },
- when_passed: function(block) {
- const shape = block.getFieldValue('SHAPE');
- return [`when_passed('${shape}')`, pythonGenerator.ORDER_FUNCTION_CALL];
- },
- is_at_end: function(block) {
- return [`is_at_end()`, pythonGenerator.ORDER_FUNCTION_CALL];
- },
- // 条件判断生成器
- controls_if: function(block) {
- const n = block.elseifCount_;
- let code = '';
- let branchCode = '';
- let branchCondition = '';
- // 生成if条件
- branchCondition = pythonGenerator.valueToCode(block, 'IF0', pythonGenerator.ORDER_NONE) || 'False';
- branchCode = pythonGenerator.statementToCode(block, 'DO0');
- code += `if ${branchCondition}:
- ${pythonGenerator.prefixLines(branchCode, pythonGenerator.INDENT)}
- `;
- // 生成else if条件
- for (let i = 1; i <= n; i++) {
- branchCondition = pythonGenerator.valueToCode(block, 'IF' + i, pythonGenerator.ORDER_NONE) || 'False';
- branchCode = pythonGenerator.statementToCode(block, 'DO' + i);
- code += `elif ${branchCondition}:
- ${pythonGenerator.prefixLines(branchCode, pythonGenerator.INDENT)}
- `;
- }
- debugger
- // 生成else条件
- const elseCode = pythonGenerator.statementToCode(block, 'ELSE');
- if (elseCode) {
- code += `else:
- ${pythonGenerator.prefixLines(elseCode, pythonGenerator.INDENT)}
- `;
- }
- return code;
- },
- controls_repeat_ext: function(block) {
- const repeats = block.getFieldValue('TIMES');
- const branch = pythonGenerator.statementToCode(block, 'DO');
- const code = `for i in range(${repeats}):\n${pythonGenerator.prefixLines(branch, pythonGenerator.INDENT)}\n`;
- return code;
- },
- controls_whileUntil: function(block) {
- const until = block.getFieldValue('MODE') === 'UNTIL';
- const condition = pythonGenerator.valueToCode(block, 'CONDITION', pythonGenerator.ORDER_NONE) || 'False';
- const branch = pythonGenerator.statementToCode(block, 'DO');
- const code = until ? `while not (${condition}) and loop_count < 100:\n${pythonGenerator.prefixLines(branch, pythonGenerator.INDENT)}loop_count += 1\n` : `while (${condition}) and loop_count < 100:\n${pythonGenerator.prefixLines(branch, pythonGenerator.INDENT)}loop_count += 1\n`;
- return code;
- },
- // 特殊生成器 - 可根据配置动态加载
- pause: function(block) {
- const seconds = block.getFieldValue('SECONDS');
- return `pause(${seconds})\n`;
- },
- play_sound: function(block) {
- const sound = block.getFieldValue('SOUND');
- return `play_sound('${sound}')
- `;
- },
- construct: function(block) {
- return 'construct()\n';
- },
- light: function(block) {
- const color = block.getFieldValue('COLOR');
- return `light('${color}')
- `;
- }
- };
- /**
- * 注册自定义Blockly积木
- * @param {Array<string>} blocklySpecialBlocks - 特殊积木方法标识集合
- */
- export function registerCustomBlocks(blocklySpecialBlocks = []) {
- // 确保blocklySpecialBlocks始终是数组
- blocklySpecialBlocks = Array.isArray(blocklySpecialBlocks) ? blocklySpecialBlocks : [];
- // 遍历所有可用积木
- for (const [blockName, blockConfig] of Object.entries(availableBlocks)) {
- // 如果是通用积木,或者是允许的特殊积木,则注册
- if (blockConfig.isGeneral || blocklySpecialBlocks.includes(blockName)) {
- Blockly.Blocks[blockName] = {
- init: function() {
- this.jsonInit(blockConfig.jsonConfig);
- }
- };
- }
- }
- }
- /**
- * 设置Blockly中文本地化
- */
- export function setupBlocklyChineseLocale() {
- // 使用扩展方式覆盖默认的英文文本为中文
- const locale = {
- // 逻辑积木中文配置
- CONTROLS_IF_MSG_IF: "如果",
- CONTROLS_IF_MSG_ELSEIF: "否则如果",
- CONTROLS_IF_MSG_ELSE: "否则",
- CONTROLS_IF_MSG_THEN: "执行",
- CONTROLS_IF_MSG_DO: "执行",
- CONTROLS_IF_TOOLTIP_1: "如果条件为真,则执行相应的代码。",
- CONTROLS_IF_TOOLTIP_2: "如果条件为真,则执行相应的代码;否则执行其他代码。",
- CONTROLS_IF_TOOLTIP_3: "如果条件为真,则执行相应的代码;否则检查其他条件。",
- CONTROLS_IF_TOOLTIP_4: "如果条件为真,则执行相应的代码;否则检查其他条件,如果都不满足则执行最后代码。",
- // 控制块的弹出菜单文本
- CONTROLS_IF_IF: "如果",
- CONTROLS_IF_ELSEIF: "否则如果",
- CONTROLS_IF_ELSE: "否则",
- CONTROLS_IF_IF_TITLE: "如果",
- CONTROLS_IF_IF_TITLE_IF: "如果",
- CONTROLS_IF_ELSEIF_TITLE_ELSEIF: "否则如果",
- CONTROLS_IF_ELSE_TITLE: "否则",
- CONTROLS_IF_ELSE_TITLE_ELSE: "否则",
- // 条件块的提示文本和帮助URL
- CONTROLS_IF_HELPURL: "条件语句帮助",
- CONTROLS_IF_TOOLTIP_IF: "添加或删除条件。",
- CONTROLS_IF_TOOLTIP_ELSE: "添加或删除否则块。",
- // 添加设置按钮相关的中文配置
- CONTROLS_IF_ELSEIF_TOOLTIP: "否则如果条件为真,则执行相应的代码。",
- CONTROLS_IF_ELSE_TOOLTIP: "否则执行相应的代码。",
- // 比较运算符中文配置
- LOGIC_COMPARE_EQ: "等于",
- LOGIC_COMPARE_NEQ: "不等于",
- LOGIC_COMPARE_LT: "小于",
- LOGIC_COMPARE_LTE: "小于等于",
- LOGIC_COMPARE_GT: "大于",
- LOGIC_COMPARE_GTE: "大于等于",
- LOGIC_COMPARE_TOOLTIP_EQ: "比较两个值是否相等。",
- LOGIC_COMPARE_TOOLTIP_NEQ: "比较两个值是否不相等。",
- LOGIC_COMPARE_TOOLTIP_LT: "比较第一个值是否小于第二个值。",
- LOGIC_COMPARE_TOOLTIP_LTE: "比较第一个值是否小于或等于第二个值。",
- LOGIC_COMPARE_TOOLTIP_GT: "比较第一个值是否大于第二个值。",
- LOGIC_COMPARE_TOOLTIP_GTE: "比较第一个值是否大于或等于第二个值。",
- // 逻辑运算中文配置
- LOGIC_OPERATION_AND: "且",
- LOGIC_OPERATION_OR: "或",
- LOGIC_OPERATION_TOOLTIP_AND: "如果两个条件都为真,则结果为真。",
- LOGIC_OPERATION_TOOLTIP_OR: "如果任一条件为真,则结果为真。",
- // 布尔值配置
- LOGIC_BOOLEAN_TRUE: "真",
- LOGIC_BOOLEAN_FALSE: "假",
- LOGIC_BOOLEAN_TOOLTIP: "返回一个布尔值:真或假。",
- // 循环积木中文配置
- CONTROLS_REPEAT_TITLE: "重复执行%1次",
- CONTROLS_REPEAT_TOOLTIP: "重复执行内部代码指定次数。",
- CONTROLS_REPEAT_MSG_DO: "执行",
- CONTROLS_REPEAT_INPUT_DO: "执行",
- CONTROLS_WHILEUNTIL_MSG_DO: "执行",
- CONTROLS_WHILEUNTIL_INPUT_DO: "执行",
- CONTROLS_FOR_MSG_DO: "执行",
- CONTROLS_FOR_INPUT_DO: "执行",
- CONTROLS_FOR_EACH_MSG_DO: "执行",
- CONTROLS_FOR_EACH_INPUT_DO: "执行",
- // 数学积木中文配置
- MATH_NUMBER_TOOLTIP: "一个数字。在编辑器中双击以更改。",
- MATH_ADDITION_SYMBOL: "加",
- MATH_SUBTRACTION_SYMBOL: "减",
- MATH_MULTIPLICATION_SYMBOL: "乘",
- MATH_DIVISION_SYMBOL: "除",
- MATH_ARITHMETIC_TOOLTIP_ADD: "返回两个数的和。",
- MATH_ARITHMETIC_TOOLTIP_SUBTRACT: "返回第一个数减去第二个数的差。",
- MATH_ARITHMETIC_TOOLTIP_MULTIPLY: "返回两个数的积。",
- MATH_ARITHMETIC_TOOLTIP_DIVIDE: "返回第一个数除以第二个数的商。"
- };
- // 使用Object.assign来合并配置,避免直接修改导入对象
- Object.assign(Blockly.Msg, locale);
- }
- /**
- * 注册JavaScript生成器
- * @param {Array<string>} blocklySpecialBlocks - 特殊积木方法标识集合
- */
- export function registerJavaScriptGenerators(blocklySpecialBlocks = []) {
- // 确保blocklySpecialBlocks始终是数组
- blocklySpecialBlocks = Array.isArray(blocklySpecialBlocks) ? blocklySpecialBlocks : [];
- // 遍历所有可用生成器
- for (const [blockName, generatorFunc] of Object.entries(availableGenerators)) {
- // 如果是通用生成器,或者是允许的特殊生成器对应的生成器,则注册
- if (availableBlocks[blockName].isGeneral || blocklySpecialBlocks.includes(blockName)) {
- javascriptGenerator.forBlock[blockName] = generatorFunc;
- }
- }
- // 为text_print块添加生成器,用于调试(始终可用)
- javascriptGenerator.forBlock['text_print'] = function(block) {
- const msg = javascriptGenerator.valueToCode(block, 'TEXT', javascriptGenerator.ORDER_NONE) || '';
- return msg;
- };
- }
- /**
- * 注册Python生成器
- * @param {Array<string>} blocklySpecialBlocks - 特殊积木方法标识集合
- */
- export function registerPythonGenerators(blocklySpecialBlocks = []) {
- // 确保blocklySpecialBlocks始终是数组
- blocklySpecialBlocks = Array.isArray(blocklySpecialBlocks) ? blocklySpecialBlocks : [];
- // 遍历所有可用生成器
- for (const [blockName, generatorFunc] of Object.entries(availablePythonGenerators)) {
- // 如果是通用生成器,或者是允许的特殊生成器对应的生成器,则注册
- if (availableBlocks[blockName].isGeneral || blocklySpecialBlocks.includes(blockName)) {
- pythonGenerator.forBlock[blockName] = generatorFunc;
- }
- }
- // 为text_print块添加生成器,用于调试(始终可用)
- pythonGenerator.forBlock['text_print'] = function(block) {
- const msg = pythonGenerator.valueToCode(block, 'TEXT', pythonGenerator.ORDER_NONE) || '';
- return msg;
- };
- }
- /**
- * 初始化Blockly工作区
- * @param {HTMLElement} blocklyDiv - Blockly容器元素
- * @param {HTMLElement} toolbox - 工具箱元素
- * @returns {Blockly.WorkspaceSvg} - Blockly工作区实例
- */
- export function initBlockly(options = {}) {
- // 应用中文配置
- setupBlocklyChineseLocale();
- const defaultOptions = {
- toolbox: document.getElementById('toolbox'),
- collapse: false,
- comments: true,
- disable: false, // 设为false以允许编辑
- maxBlocks: Infinity,
- trashcan: true,
- horizontalLayout: false,
- toolboxPosition: 'start',
- toolboxCollapse: false, // 设置工具箱默认展开
- toolboxAlwaysExpanded: true, // 确保点击类别后保持展开状态
- css: true,
- media: 'https://unpkg.com/blockly/media/',
- rtl: false,
- scrollbars: true,
- sounds: false, // 禁用声音以提高性能
- oneBasedIndex: true,
- grid: {
- spacing: 20,
- length: 3,
- colour: "#ccc",
- snap: true
- },
- zoom: {
- controls: true,
- wheel: true,
- startScale: 1.0,
- maxScale: 3,
- minScale: 0.3,
- scaleSpeed: 1.2
- }
- };
- // 合并默认选项和用户提供的选项
- const finalOptions = { ...defaultOptions, ...options };
- return Blockly.inject('blocklyDiv', finalOptions);
- }
- /**
- * 获取所有可用的积木配置
- * @returns {Object} 所有可用积木的配置对象
- */
- export function getAllBlocksConfig() {
- return {...availableBlocks};
- }
- /**
- * 获取所有可用的JavaScript生成器配置
- * @returns {Object} 所有可用JavaScript生成器的配置对象
- */
- export function getAllGeneratorsConfig() {
- return {...availableGenerators};
- }
- /**
- * 获取所有可用的Python生成器配置
- * @returns {Object} 所有可用Python生成器的配置对象
- */
- export function getAllPythonGeneratorsConfig() {
- return {...availablePythonGenerators};
- }
- // 导出字典常量
- export { BLOCKLY_MAP_TYPE_DICT, BLOCKLY_MAP_MARK_DICT, BLOCKLY_MAP_SPECIAL_DICT,
- BLOCKLY_LIGHT_COLOR_DICT };
- export default {
- registerCustomBlocks,
- registerJavaScriptGenerators,
- registerPythonGenerators,
- setupBlocklyChineseLocale,
- initBlockly,
- getAllBlocksConfig,
- getAllGeneratorsConfig,
- BLOCKLY_MAP_TYPE_DICT,
- BLOCKLY_MAP_MARK_DICT,
- BLOCKLY_MAP_SPECIAL_DICT
- };
|