內部區塊
內部區塊是指附加至值和陳述式輸入內容的區塊。
個別區塊程式碼產生器必須處理自身內部的串連
區塊,系統才會將程式碼加進正確位置
import {javascriptGenerator, Order} from 'blockly/javascript';
javascriptGenerator.forBlock['my_custom_block'] = function(block, generator) {
// Generate innner block code.
const statement = generator.statementToCode(block, 'MY_STATEMENT_INPUT');
const value = generator.valueToCode(block, 'MY_VALUE_INPUT', Order.ATOMIC);
// Concatenate the string.
const code = `some code ${statement} ${value} some more code`;
// Return the code.
return code;
}
附加至陳述式輸入的內部區塊程式碼可使用
statementToCode
。這會呼叫
陳述式區塊的區塊程式碼產生器
縮排程式碼
const statement = generator.statementToCode(block, 'MY_STATEMENT_INPUT');
您只需要針對直接連線的內部區塊呼叫 statementToCode
陳述式輸入內容
附加至值輸入的內部區塊程式碼可使用以下程式碼產生:
valueTocode
。這會呼叫
值區塊的區塊程式碼產生器
在內部區塊周圍加上括號在需要時執行程式碼
如要進一步瞭解做法,請參閱括號說明文件
即可。
const value = generator.valueToCode(block, 'MY_VALUE_INPUT', Order.ATOMIC);
串連程式碼
取得內部區塊的程式碼字串後,
輸入正確的程式碼字串
const code = `some code ${statement} ${value} some more code`;
傳回碼
不同類型的區塊需要在不同的區塊中傳回程式碼字串
因此,建議您個別查看他們的網頁瞭解詳情:
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2024-10-15 (世界標準時間)。
[null,null,["上次更新時間:2024-10-15 (世界標準時間)。"],[[["Inner blocks attached to custom blocks need to have their generated code properly concatenated within the custom block's code."],["`statementToCode` is used to generate and properly indent code for inner blocks in statement inputs, while `valueToCode` handles code generation and parenthesization for inner blocks in value inputs."],["The code for inner blocks is retrieved as strings which are then concatenated with the main block's code to produce the final output."],["Different block types have different return requirements, so refer to specific documentation for blocks with or without outputs to ensure correct code generation."]]],["Inner blocks' code is generated using `statementToCode` for statement inputs and `valueToCode` for value inputs. `statementToCode` handles indenting, while `valueToCode` manages parentheses. Individual generators are needed to concatenate these inner block codes into the correct placement within the custom code string. The final step is returning this complete code string, with different block types needing distinct return methods, which depends on if they have outputs or not.\n"]]