Creating a new Category
Create a new folder inside blocks/js
import { OutputType, BlockShape, InputShape } from '$lib/utils/blockRegistryTool';
class XXXBlocks {
getRegistry () {
return {
id: "Name",
color: 60,
blocks: [
]
};
}
//place for functions
}
export default XXXBlocks;
Creating a new block
Inside the blocks array you will be defining the blocks.
{
func: "teststatement",
text: "statement block",
shape: BlockShape.STATEMENT
},
Every block needs a function to work
teststatement () {
return 'void;';
}
Defining the block
func
: function name of the blocktext
: Text that is shown on the block, using brackets you can make[ARGUMENTS]
,/n
will makes a new line and if you use branches it will be on another branch.shape
: For blocks that won´t return anything, but make functions
- STATEMENT - Not actually required, but keeps code consistent.
- EVENT - floating block with an input inside.Can be replaced with FLOATING, but keeps code consistent.
- TERMINAL - block with no blocks allowed to attach after.
- FLOATING - block that cannot have any parent blocks
- TOPPER - cannot have any blocks attached before it
- CUSTOM - can be used if “manual” is used for the block.
output
: Blocks that return values.
- STRING - returns text
- NUMBER - returns number
- BOOLEAN - returns true/false
- ARRY - returns a list / array
- OBJECT - returns a JSON object.
- ANY - can be put inside any block.
- DISCORD - contains multiple values of discord objects
- SERVER
- CHANNEL
- MESSAGE
- MEMBER
branches
: allows you create multiple branches (even is 1 branch)inline
: true/false if the block should be inline or not default - falsearguments
: if you used any arguments you have to define them here. Mostly you will use type of value with checktype
: defines which block can be put here.- VALUE - allows other blocks
- DUMMY - Can be used for seperating content on a block by a new line.
- SPACE - Similar to DUMMY.Can be used for seperating content on a block.
- IMAGE
- ANGLE - Angle field for directional inputs.
- CHECKBOX - Checkbox field usually for toggles.
- COLOR - Color field.
- MENU - Dropdown menu field with options.
- SERIALIZABLE_LABEL - Label that serializes to the project.
- NUMBER - Number field. Used for restricting to certain numbers.
- TEXT - Text field. Used if blocks shouldnt be used here,but text can still be input here.
- MULTILINE_TEXT - Multi-line text field.Similar to TEXT, but new line characters are allowed.
- VARIABLE - Variable field. Similar to MENU, but the options are all variables.
- DISCORD
- SERVER
- CHANNEL
- MESSAGE
check
: check is used to check if the block that user puts has the same values. Same as shapeoptions
: used for menu. as array of inputs where first value is shown and second is returned in the code.
options: [
['shown', 'codeshown'],
['shown2', 'codeshown2']
]
Advanced block
This block changes a variable to another value.
{
func: "change_variable",
text: "change [NAME] [CHANGE] [VALUE]",
shape: BlockShape.STATEMENT,
inline: true,
arguments: {
CHANGE: {
type: InputShape.MENU,
options: [
['add', 'add'],
['subtract', 'subtract'],
['multiply by', 'multiply'],
['divide by', 'divide'],
['set to', 'set']
]
},
NAME: {
type: InputShape.VALUE,
check: OutputType.STRING
},
VALUE: {
type: InputShape.VALUE,
},
}
...
change_variable(args: any) {
let newName = args.NAME.replace(/'/g, "");
switch (args.CHANGE) {
case 'add':
return `${newName} += ${args.VALUE}\n`;
case 'subtract':
return `${newName} -= ${args.VALUE}\n`;
case 'multiply':
return `${newName} *= ${args.VALUE}\n`;
case 'divide':
return `${newName} /= ${args.VALUE}\n`;
case 'set':
return `${newName} = ${args.VALUE}\n`;
default:
return '';
}
}
Warnings
WarningType.
- RequiredParent - adds warning when blocks root block isn’t correct
- EmptyInput - when field is empty
fields to define warnings
- ‘warnings’ - list of warnings
- type: WarningType
- message: string
- inputName: string | string[] - a string or a list of strings that are checked whether they are empty
- parentType: string | string[] - a string or a list of strings that check whether root block is the right type
Example
{
func: "test_warning",
text: "Warning parent\n input: [INPUT]",
shape: BlockShape.STATEMENT,
warnings: [
{
type: WarningType.RequiredParent,
parentType: "coretest_testevent",
message: "this block belongs under test event block!"
},
{
type: WarningType.EmptyInput,
inputName: "INPUT",
message: "Input field is empty"
},
],
arguments: {
INPUT: {
type: InputShape.VALUE
}
}
},
Mutators
Defining it
*required fields for mutator to work
- *
mutator
- string field for defining mutator id. - *
mutatorData
- *
type
:- checkbox
- *
inputs
: MutatorInput[]- *
text
: string of the field name(use single word for it, this will be removed when inputName field will be added!!!) - *
type
: OutputType or string[] of types - *
defaultValue
: Determines if input on start is shown, boolean(this field will be modified for multiple types or kept for only the checkbox mutator!!!) inputName
: is used to get input value when generating js, if left empty will use *text
field for input name and then its used to get the value.(used when *text
field is with multiple words.)
- *
blockType
: id of a block as base for block when opening the mutator menu.color
: color of the block that is inside the mutator menu
- *
Example of mutator block
//other blocks...
{
func: "test_mainblock",
text: "statement blockwdwdwdw",
mutator: "test_mainblock_mutator",
mutatorData: {
type: MutatorType.CheckBox,
inputs: [
{
text: "Title", // text for input text
type: OutputType.STRING, // type for input added to the main block
defaultValue: true, // whether the checkbox is checked also will affect if input is showed on start
},
{
text: "Description",
type: OutputType.STRING,
defaultValue: false,
}
]
},
shape: BlockShape.EVENT
}
//other blocks...
Getting input data
test_mainblock(args: any) {
return `console.log(${args.Title}, ${args.Description})`
}