-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from N-J-Martin/blockDataInput
block data input block
- Loading branch information
Showing
7 changed files
with
220 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// A variation of code from https://blocklycodelabs.dev/codelabs/custom-generator/index.html?index=..%2F..index#0 | ||
|
||
import * as Blockly from 'blockly'; | ||
|
||
export const jsonBlocks = Blockly.common.createBlockDefinitionsFromJsonArray([{ | ||
"type": "object", | ||
"message0": "{ %1 %2 }", | ||
"args0": [ | ||
{ | ||
"type": "input_dummy" | ||
}, | ||
{ | ||
"type": "input_statement", | ||
"name": "MEMBERS" | ||
} | ||
], | ||
"output": null, | ||
"colour": 230, | ||
}, | ||
{ | ||
"type": "member", | ||
"message0": "%1 %2 %3", | ||
"args0": [ | ||
{ | ||
"type": "input_value", | ||
"name": "MEMBER_NAME", | ||
}, | ||
{ | ||
"type": "field_label", | ||
"name": "COLON", | ||
"text": ":" | ||
}, | ||
{ | ||
"type": "input_value", | ||
"name": "MEMBER_VALUE" | ||
} | ||
], | ||
"previousStatement": null, | ||
"nextStatement": null, | ||
"colour": 230, | ||
"inputsInline": true | ||
}]); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// A variation of code from https://blocklycodelabs.dev/codelabs/custom-generator/index.html?index=..%2F..index#0 | ||
|
||
|
||
import * as Blockly from 'blockly'; | ||
|
||
export const jsonGenerator = new Blockly.Generator('JSON'); | ||
const Order = { | ||
ATOMIC: 0, | ||
}; | ||
|
||
jsonGenerator.forBlock['logic_null'] = function(block) { | ||
return ['null', Order.ATOMIC]; | ||
}; | ||
|
||
|
||
jsonGenerator.forBlock['text'] = function(block) { | ||
const textValue = block.getFieldValue('TEXT'); | ||
const code = `"${textValue}"`; | ||
return [code, Order.ATOMIC]; | ||
|
||
}; | ||
|
||
jsonGenerator.forBlock['math_number'] = function(block) { | ||
const code = String(block.getFieldValue('NUM')); | ||
return [code, Order.ATOMIC]; | ||
}; | ||
|
||
jsonGenerator.forBlock['logic_boolean'] = function(block) { | ||
const code = (block.getFieldValue('BOOL') === 'TRUE') ? 'true' : 'false'; | ||
return [code, Order.ATOMIC]; | ||
}; | ||
|
||
jsonGenerator.forBlock['member'] = function(block, generator) { | ||
const name = generator.valueToCode(block, 'MEMBER_NAME', Order.ATOMIC); | ||
const value = generator.valueToCode(block, 'MEMBER_VALUE', Order.ATOMIC); | ||
const code = `"${name}": ${value}`; | ||
return code; | ||
}; | ||
|
||
jsonGenerator.forBlock['lists_create_with'] = function(block, generator) { | ||
const values = []; | ||
for (let i = 0; i < block.itemCount_; i++) { | ||
const valueCode = generator.valueToCode(block, 'ADD' + i, | ||
Order.ATOMIC); | ||
if (valueCode) { | ||
values.push(valueCode); | ||
} | ||
} | ||
const valueString = values.join(',\n'); | ||
const indentedValueString = | ||
generator.prefixLines(valueString, generator.INDENT); | ||
const codeString = '[\n' + indentedValueString + '\n]'; | ||
return [codeString, Order.ATOMIC]; | ||
}; | ||
|
||
jsonGenerator.forBlock['object'] = function(block, generator) { | ||
const statementMembers = | ||
generator.statementToCode(block, 'MEMBERS'); | ||
const code = '{\n' + statementMembers + '\n}'; | ||
return [code, Order.ATOMIC]; | ||
}; | ||
|
||
jsonGenerator.scrub_ = function(block, code, thisOnly) { | ||
const nextBlock = | ||
block.nextConnection && block.nextConnection.targetBlock(); | ||
if (nextBlock && !thisOnly) { | ||
return code + ',\n' + jsonGenerator.blockToCode(nextBlock); | ||
} | ||
return code; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// A variation of code from https://blocklycodelabs.dev/codelabs/custom-generator/index.html?index=..%2F..index#0 | ||
|
||
export const jsonToolbox = { | ||
'kind': 'categoryToolbox', | ||
'contents': [ { | ||
'kind' : 'category', | ||
'name' : 'Values', | ||
'contents': [ | ||
{ | ||
'kind': 'block', | ||
'type': 'object' | ||
}, | ||
{ | ||
'kind': 'block', | ||
'type': 'member' | ||
}, | ||
{ | ||
'kind': 'block', | ||
'type': 'math_number' | ||
}, | ||
{ | ||
'kind': 'block', | ||
'type': 'text' | ||
}, | ||
{ | ||
'kind': 'block', | ||
'type': 'logic_boolean' | ||
}, | ||
{ | ||
'kind': 'block', | ||
'type': 'logic_null' | ||
}, | ||
{ | ||
'kind': 'block', | ||
'type': 'lists_create_with' | ||
}, | ||
]}, | ||
{ | ||
'kind': 'category', | ||
'name': 'Variables', | ||
'custom': 'GET_VARIABLE' | ||
} | ||
] | ||
} |