-
Notifications
You must be signed in to change notification settings - Fork 3.8k
feat: Add a JSON Block definition interface #9613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,7 +38,6 @@ export const blocks = createBlockDefinitionsFromJsonArray([ | |
| 'variable': '%{BKY_VARIABLES_DEFAULT_NAME}', | ||
| }, | ||
| ], | ||
| 'output': null, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The change to this file should also be reverted. You've removed an output from one block and added it to a different one unexpectedly. This will change the function and shape of the blocks. |
||
| 'style': 'variable_dynamic_blocks', | ||
| 'helpUrl': '%{BKY_VARIABLES_GET_HELPURL}', | ||
| 'tooltip': '%{BKY_VARIABLES_GET_TOOLTIP}', | ||
|
|
@@ -59,6 +58,7 @@ export const blocks = createBlockDefinitionsFromJsonArray([ | |
| 'name': 'VALUE', | ||
| }, | ||
| ], | ||
| 'output': null, | ||
| 'previousStatement': null, | ||
| 'nextStatement': null, | ||
| 'style': 'variable_dynamic_blocks', | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import {FieldCheckboxFromJsonConfig} from '../field_checkbox.js'; | ||
| import {FieldDropdownFromJsonConfig} from '../field_dropdown'; | ||
| import {FieldImageFromJsonConfig} from '../field_image'; | ||
| import {FieldNumberFromJsonConfig} from '../field_number'; | ||
| import {FieldTextInputFromJsonConfig} from '../field_textinput'; | ||
| import {FieldVariableFromJsonConfig} from '../field_variable'; | ||
|
|
||
| /** | ||
| * Defines the JSON structure for a block definition. | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * const blockDef: JsonBlockDefinition = { | ||
| * type: 'custom_block', | ||
| * message0: 'move %1 steps', | ||
| * args0: [ | ||
| * { | ||
| * 'type': 'field_number', | ||
| * 'name': 'INPUT', | ||
| * }, | ||
| * ], | ||
| * previousStatement: null, | ||
| * nextStatement: null, | ||
| * }; | ||
| * ``` | ||
| */ | ||
| export interface JsonBlockDefinition { | ||
| type: string; | ||
| style?: string | null; | ||
| colour?: string | number; | ||
| output?: string | string[] | null; | ||
| previousStatement?: string | string[] | null; | ||
| nextStatement?: string | string[] | null; | ||
| outputShape?: number; | ||
| inputsInline?: boolean; | ||
| tooltip?: string; | ||
| helpUrl?: string; | ||
| extensions?: string[]; | ||
| mutator?: string; | ||
| enableContextMenu?: boolean; | ||
| suppressPrefixSuffix?: boolean; | ||
|
|
||
| [key: `message${number}`]: string | undefined; | ||
| [key: `args${number}`]: JsonBlockArg[] | undefined; | ||
| [key: `implicitAlign${number}`]: string | undefined; | ||
| } | ||
|
|
||
| /** | ||
| * A map for custom args. | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * declare module 'blockly/core/interfaces/i_json_block_definition' { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an interesting idea, but I don't think it's a pattern we want to add here. Using I think for now, it would be best to have custom fields/inputs/args use |
||
| * interface JsonBlockCustomArgMap { | ||
| * field_mitosis: { | ||
| * type: 'field_mitosis'; | ||
| * name?: string; | ||
| * cellId: string; | ||
| * }; | ||
| * } | ||
| * } | ||
| * ``` | ||
| */ | ||
| export interface JsonBlockCustomArgMap {} | ||
|
|
||
| export type BuiltinJsonBlockArg = | ||
| | InputValueArg | ||
| | InputStatementArg | ||
| | InputDummyArg | ||
| | InputEndRowArg | ||
| | FieldInputArg | ||
| | FieldNumberArg | ||
| | FieldDropdownArg | ||
| | FieldCheckboxArg | ||
| | FieldImageArg | ||
| | FieldVariableArg; | ||
|
|
||
| export type CustomJsonBlockArg = | ||
| JsonBlockCustomArgMap[keyof JsonBlockCustomArgMap]; | ||
|
|
||
| export type JsonBlockArg = BuiltinJsonBlockArg | CustomJsonBlockArg; | ||
|
|
||
| /** Input args */ | ||
| interface InputValueArg { | ||
| type: 'input_value'; | ||
| name?: string; | ||
| check?: string | string[]; | ||
| align?: FieldsAlign; | ||
| } | ||
|
|
||
| interface InputStatementArg { | ||
| type: 'input_statement'; | ||
| name?: string; | ||
| check?: string | string[]; | ||
| } | ||
|
|
||
| interface InputDummyArg { | ||
| type: 'input_dummy'; | ||
| name?: string; | ||
| } | ||
|
|
||
| interface InputEndRowArg { | ||
| type: 'input_end_row'; | ||
| name?: string; | ||
| } | ||
|
|
||
| /** Field args */ | ||
| interface FieldInputArg extends FieldTextInputFromJsonConfig { | ||
| type: 'field_input'; | ||
| name?: string; | ||
| } | ||
|
|
||
| interface FieldNumberArg extends FieldNumberFromJsonConfig { | ||
| type: 'field_number'; | ||
| name?: string; | ||
| } | ||
|
|
||
| interface FieldDropdownArg extends FieldDropdownFromJsonConfig { | ||
| type: 'field_dropdown'; | ||
| name?: string; | ||
| } | ||
|
|
||
| interface FieldCheckboxArg extends FieldCheckboxFromJsonConfig { | ||
| type: 'field_checkbox'; | ||
| name?: string; | ||
| } | ||
|
|
||
| interface FieldImageArg extends FieldImageFromJsonConfig { | ||
| type: 'field_image'; | ||
| name?: string; | ||
| } | ||
|
|
||
| interface FieldVariableArg extends FieldVariableFromJsonConfig { | ||
| type: 'field_variable'; | ||
| name?: string; | ||
| } | ||
|
|
||
| export type FieldsAlign = 'LEFT' | 'RIGHT' | 'CENTRE'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might want to use |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import {defineBlocksWithJsonArray} from 'blockly-test/core'; | ||
| import type {JsonBlockDefinition} from 'blockly-test/core/interfaces/i_json_block_definition'; | ||
|
|
||
| import './different_user_input'; | ||
|
|
||
| declare module 'blockly-test/core/interfaces/i_json_block_definition' { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed above, if you have the internal Blockly types use |
||
| interface JsonBlockCustomArgMap { | ||
| field_mitosis: { | ||
| type: 'field_mitosis'; | ||
| name?: string; | ||
| cellId: string; | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| const mitosisBlockDefinition: JsonBlockDefinition = { | ||
| type: 'mitosis_block', | ||
| message0: 'split cell %1', | ||
| args0: [ | ||
| { | ||
| type: 'field_mitosis', | ||
| name: 'CELL', | ||
| cellId: 'cell-A', | ||
| }, | ||
| ], | ||
| previousStatement: null, | ||
| nextStatement: null, | ||
| }; | ||
|
|
||
| defineBlocksWithJsonArray([mitosisBlockDefinition]); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change to this file should not be made. You are changing the block definitions in unexpected ways.