# Block 类
用于创建一个块。通常不直接调用,推荐使用 workspace.newBlock()。
签名:
export declare class Block implements IASTNodeLocation, IDeletable
实现: IASTNodeLocation, IDeletable
# 构造函数
构造函数 | 修饰符 | 描述 |
---|---|---|
(constructor)(workspace, prototypeName, opt_id) | 构造 Block 类的新实例 |
# Properties
属性 | 修饰符 | 类型 | 描述 |
---|---|---|---|
childBlocks_ | protected | this[] | |
collapsed_ | protected | boolean | |
COLLAPSED_FIELD_NAME | static readonly | string | The language-neutral ID given to the collapsed field. |
COLLAPSED_INPUT_NAME | static readonly | string | The language-neutral ID given to the collapsed input. |
colour_ | protected | string | Colour of the block in '#RRGGBB' format. |
compose? | (p1: Block) => void | (Optional) An optional function that reconfigures the block based on the contents of the mutator dialog. | |
contextMenu | boolean | ||
data | string | null | |
decompose? | (p1: Workspace) => Block | (Optional) An optional function that populates the mutator's dialog with this block's components. | |
destroy? | () => void | (Optional) An optional method called during disposal. | |
domToMutation? | (p1: Element) => void | (Optional) An optional deserialization method for defining how to deserialize the mutation state from XML. This must be coupled with defining mutationToDom . | |
getDeveloperVariables? | () => string[] | (Optional) An optional property for declaring developer variables. Return a list of variable names for use by generators. Developer variables are never shown to the user, but are declared as global variables in the generated code. | |
hat? | string | (Optional) Name of the type of hat. | |
helpUrl | string | Function | |
icons | IIcon[] | ||
id | string | ||
init? | () => void | (Optional) An optional method called during initialization. | |
inputList | Input[] | ||
inputsInline? | boolean | (Optional) | |
inputsInlineDefault? | boolean | (Optional) | |
isInFlyout | boolean | ||
isInMutator | boolean | ||
isInsertionMarker_ | protected | boolean | True if this block is an insertion marker. |
loadExtraState? | (p1: any) => void | (Optional) An optional serialization method for defining how to deserialize the block's extra state (eg mutation state) from something JSON compatible. This must be coupled with defining saveExtraState . | |
mutationToDom? | (...p1: any[]) => Element | (Optional) An optional serialization method for defining how to serialize the mutation state to XML. This must be coupled with defining domToMutation . | |
nextConnection | Connection | null | |
onchange? | ((p1: Abstract) => void) | null | |
outputConnection | Connection | null | |
outputShape_ | protected | number | null |
parentBlock_ | protected | this | null |
previousConnection | Connection | null | |
rendered | boolean | null | |
RTL | boolean | ||
saveExtraState? | (doFullSerialization?: boolean) => any | (Optional) An optional serialization method for defining how to serialize the block's extra state (eg mutation state) to something JSON compatible. This must be coupled with defining loadExtraState . | |
styleName_ | protected | string | Name of the block style. |
suppressPrefixSuffix | boolean | null | |
tooltip | Tooltip.TipInfo | ||
type | string | ||
workspace | Workspace |
# Methods
Method | Modifiers | Description |
---|---|---|
addIcon(icon) | Adds the given icon to the block. | |
allInputsFilled(opt_shadowBlocksAreFilled) | Recursively checks whether all statement and value inputs are filled with blocks. Also checks all following statement blocks in this stack. | |
appendDummyInput(name) | Appends a dummy input row. | |
appendEndRowInput(name) | Appends an input that ends the row. | |
appendInput(input) | Appends the given input row. Allows for custom inputs to be appended to the block. | |
appendStatementInput(name) | Appends a statement input row. | |
appendValueInput(name) | Appends a value input row. | |
bumpNeighbours() | Bump unconnected blocks out of alignment. Two blocks which aren't actually connected should not coincidentally line up on screen. | |
dispose(healStack) | Dispose of this block. | |
disposeInternal() | protected | Disposes of this block without doing things required by the top block. E.g. does not fire events, unplug the block, etc. |
doInit_() | protected | Calls the init() function and handles associated event firing, etc. |
getChildren(ordered) | Find all the blocks that are directly nested inside this one. Includes value and statement inputs, as well as any following statement. Excludes any connection on an output tab or any preceding statement. Blocks are optionally sorted by position; top to bottom. | |
getColour() | Get the colour of a block. | |
getCommentText() | Returns the comment on this block (or null if there is no comment). | |
getDescendants(ordered) | Find all the blocks that are directly or indirectly nested inside this one. Includes this block in the list. Includes value and statement inputs, as well as any following statements. Excludes any connection on an output tab or any preceding statements. Blocks are optionally sorted by position; top to bottom. | |
getField(name) | Returns the named field from a block. | |
getFieldValue(name) | Returns the language-neutral value of the given field. | |
getHue() | Get the HSV hue value of a block. Null if hue not set. | |
getIcon(type) | ||
getIcons() | ||
getInheritedDisabled() | Get whether the block is disabled or not due to parents. The block's own disabled property is not considered. | |
getInput(name) | Fetches the named input object. | |
getInputsInline() | Get whether value inputs are arranged horizontally or vertically. | |
getInputTargetBlock(name) | Fetches the block attached to the named input. | |
getInputWithBlock(block) | Return the input that connects to the specified block. | |
getNextBlock() | Return the next statement block directly connected to this block. | |
getOutputShape() | Get the block's output shape. | |
getParent() | Return the parent block or null if this block is at the top level. The parent block is either the block connected to the previous connection (for a statement block) or the block connected to the output connection (for a value block). | |
getPreviousBlock() | Returns the block connected to the previous connection. | |
getRelativeToSurfaceXY() | Return the coordinates of the top-left corner of this block relative to the drawing surface's origin (0,0), in workspace units. | |
getRootBlock() | Return the top-most block in this block's tree. This will return itself if this block is at the top level. | |
getStyleName() | Get the name of the block style. | |
getSurroundParent() | Return the parent block that surrounds the current block, or null if this block has no surrounding block. A parent block might just be the previous statement, whereas the surrounding block is an if statement, while loop, etc. | |
getTooltip() | Returns the tooltip text for this block. | |
getVars() | Return all variables referenced by this block. | |
hasIcon(type) | ||
initModel() | Call initModel on all fields on the block. May be called more than once. Either initModel or initSvg must be called after creating a block and before the first interaction with it. Interactions include UI actions (e.g. clicking and dragging) and firing events (e.g. create, delete, and change). | |
isCollapsed() | Get whether the block is collapsed or not. | |
isDeletable() | Get whether this block is deletable or not. | |
isDisposed() | Returns if this block has been disposed of / deleted. | |
isDuplicatable() | Get whether is block is duplicatable or not. If duplicating this block and descendants will put this block over the workspace's capacity this block is not duplicatable. If duplicating this block and descendants will put any type over their maxInstances this block is not duplicatable. | |
isEnabled() | Get whether this block is enabled or not. | |
isInsertionMarker() | Get whether this block is an insertion marker block or not. | |
isOwnDeletable() | Return whether this block's own deletable property is true or false. | |
isOwnEditable() | Return whether this block's own editable property is true or false. | |
isShadow() | Get whether this block is a shadow block or not. | |
jsonInit(json) | Initialize this block using a cross-platform, internationalization-friendly JSON description. | |
mixin(mixinObj, opt_disableCheck) | Add key/values from mixinObj to this block object. By default, this method will check that the keys in mixinObj will not overwrite existing values in the block, including prototype values. This provides some insurance against mixin / extension incompatibilities with future block features. This check can be disabled by passing true as the second argument. | |
moveBy(dx, dy, reason) | Move a block by a relative offset. | |
moveInputBefore(name, refName) | Move a named input to a different location on this block. | |
moveNumberedInputBefore(inputIndex, refIndex) | Move a numbered input to a different location on this block. | |
removeIcon(type) | Removes the icon whose getType matches the given type iconType from the block. | |
removeInput(name, opt_quiet) | Remove an input from this block. | |
renameVarById(oldId, newId) | Notification that a variable is renaming. If the ID matches one of this block's variables, rename it. | |
setCollapsed(collapsed) | Set whether the block is collapsed or not. | |
setColour(colour) | Change the colour of a block. | |
setCommentText(text) | Set this block's comment text. | |
setDeletable(deletable) | Set whether this block is deletable or not. | |
setEditable(editable) | Set whether this block is editable or not. | |
setEnabled(enabled) | Set whether the block is enabled or not. | |
setFieldValue(newValue, name) | Sets the value of the given field for this block. | |
setHelpUrl(url) | Set the URL of this block's help page. | |
setInputsInline(newBoolean) | Set whether value inputs are arranged horizontally or vertically. | |
setMovable(movable) | Set whether this block is movable or not. | |
setMutator(_mutator) | Give this block a mutator dialog. | |
setNextStatement(newBoolean, opt_check) | Set whether another block can chain onto the bottom of this block. | |
setOnChange(onchangeFn) | Sets a callback function to use whenever the block's parent workspace changes, replacing any prior onchange handler. This is usually only called from the constructor, the block type initializer function, or an extension initializer function. | |
setOutput(newBoolean, opt_check) | Set whether this block returns a value. | |
setOutputShape(outputShape) | Set the block's output shape. | |
setPreviousStatement(newBoolean, opt_check) | Set whether this block can chain onto the bottom of another block. | |
setStyle(blockStyleName) | Set the style and colour values of a block. | |
setTooltip(newTip) | Sets the tooltip for this block. | |
setWarningText(_text, _opt_id) | Set this block's warning text. | |
toDevString() | This method returns a string describing this Block in developer terms (type name and ID; English only). Intended to on be used in console logs and errors. If you need a string that uses the user's native language (including block text, field values, and child blocks), use [toString()]. | |
toString(opt_maxLength, opt_emptyToken) | Create a human-readable text representation of this block and any children. | |
unplug(opt_healStack) | Unplug this block from its superior block. If this block is a statement, optionally reconnect the block underneath with the block on top. |