Class: Workspace
Defined in: packages/blockly/core/workspace.ts:42
Class for a workspace. This is a data structure that contains blocks. There is no UI, and can be created headlessly.
Extended by
Properties index
| Property | Description |
|---|---|
| SCAN_ANGLE | Angle away from the horizontal to sweep for blocks. Order of execution is generally top to bottom, but a small angle changes the scan to give a bit of a left to right bias (reversed in RTL). Units are in degrees. See: https://tvtropes.org/pmwiki/pmwiki.php/Main/DiagonalBilling |
| id | - |
| options | - |
| RTL | - |
| horizontalLayout | - |
| toolboxPosition | - |
| rendered | Returns true if the workspace is visible and false if it's headless. |
| MAX_UNDO | Maximum number of undo events in stack. 0 turns off undo, Infinity sets it to unlimited. |
| connectionDBList | Set of databases for rapid lookup of connection locations. |
| connectionChecker | - |
| undoStack_ | - |
| redoStack_ | - |
Methods index
| Method | Description |
|---|---|
| dispose | Dispose of this workspace. Unlink from all DOM elements to prevent memory leaks. |
| sortByOrigin | Sorts bounded elements on the workspace by their relative position, top to bottom (with slight LTR or RTL bias). |
| addTopBlock | Adds a block to the list of top blocks. |
| removeTopBlock | Removes a block from the list of top blocks. |
| getTopBlocks | Finds the top-level blocks and returns them. Blocks are optionally sorted by position; top to bottom (with slight LTR or RTL bias). |
| addTypedBlock | Add a block to the list of blocks keyed by type. |
| removeTypedBlock | Remove a block from the list of blocks keyed by type. |
| getBlocksByType | Finds the blocks with the associated type and returns them. Blocks are optionally sorted by position; top to bottom (with slight LTR or RTL bias). |
| getAllBlocks | Find all blocks in workspace. Blocks are optionally sorted by position; top to bottom (with slight LTR or RTL bias). |
| clear | Dispose of all blocks and comments in workspace. |
| getWidth | Returns the horizontal offset of the workspace. Intended for LTR/RTL compatibility in XML. Not relevant for a headless workspace. |
| newBlock | Obtain a newly created block. |
| newComment | Obtain a newly created comment. |
| remainingCapacity | The number of blocks that may be added to the workspace before reaching the maxBlocks. |
| remainingCapacityOfType | The number of blocks of the given type that may be added to the workspace before reaching the maxInstances allowed for that type. |
| isCapacityAvailable | Check if there is remaining capacity for blocks of the given counts to be created. If the total number of blocks represented by the map is more than the total remaining capacity, it returns false. If a type count is more than the remaining capacity for that type, it returns false. |
| hasBlockLimits | Checks if the workspace has any limits on the maximum number of blocks, or the maximum number of blocks of specific types. |
| undo | Undo or redo the previous action. |
| redo | Redoes the previous action. |
| clearUndo | Clear the undo/redo stacks. |
| addChangeListener | When something in this workspace changes, call a function. Note that there may be a few recent events already on the stack. Thus the new change listener might be called with events that occurred a few milliseconds before the change listener was added. |
| removeChangeListener | Stop listening for this workspace's changes. |
| fireChangeListener | Fire a change event. |
| getBlockById | Find the block on this workspace with the specified ID. |
| getCommentById | Find the comment on this workspace with the specified ID. |
| allInputsFilled | Checks whether all value and statement inputs in the workspace are filled with blocks. |
| getPotentialVariableMap | Return the variable map that contains "potential" variables. These exist in the flyout but not in the workspace. |
| getVariableMap | Return the map of all variables on the workspace. |
| getProcedureMap | Returns the map of all procedures on the workpace. |
| getRootWorkspace | Returns the root workspace of this workspace if the workspace has parent(s). |
| getById | Find the workspace with the specified ID. |
| getAll | Find all workspaces. |
| getVariableMapClass | - |
| isReadOnly | Returns whether or not this workspace is in readonly mode. |
| setIsReadOnly | Sets whether or not this workspace is in readonly mode. |
Constructors
Constructor
new Workspace(opt_options?): Workspace;
Defined in: packages/blockly/core/workspace.ts:128
Parameters
| Parameter | Type | Description |
|---|---|---|
opt_options? | Options | Dictionary of options. |
Returns
Workspace
Properties
SCAN_ANGLE
static SCAN_ANGLE: number = 3;
Defined in: packages/blockly/core/workspace.ts:49
Angle away from the horizontal to sweep for blocks. Order of execution is generally top to bottom, but a small angle changes the scan to give a bit of a left to right bias (reversed in RTL). Units are in degrees. See: https://tvtropes.org/pmwiki/pmwiki.php/Main/DiagonalBilling
id
id: string;
Defined in: packages/blockly/core/workspace.ts:50
options
options: Options;
Defined in: packages/blockly/core/workspace.ts:51
RTL
RTL: boolean;
Defined in: packages/blockly/core/workspace.ts:52
horizontalLayout
horizontalLayout: boolean;
Defined in: packages/blockly/core/workspace.ts:53
toolboxPosition
toolboxPosition: Position;
Defined in: packages/blockly/core/workspace.ts:54
rendered
rendered: boolean = false;
Defined in: packages/blockly/core/workspace.ts:59
Returns true if the workspace is visible and false if it's headless.
MAX_UNDO
MAX_UNDO: number = 1024;
Defined in: packages/blockly/core/workspace.ts:97
Maximum number of undo events in stack. 0 turns off undo, Infinity
sets it to unlimited.
connectionDBList
connectionDBList: ConnectionDB[] = [];
Defined in: packages/blockly/core/workspace.ts:100
Set of databases for rapid lookup of connection locations.
connectionChecker
connectionChecker: IConnectionChecker;
Defined in: packages/blockly/core/workspace.ts:101
undoStack_
protected undoStack_: Abstract[] = [];
Defined in: packages/blockly/core/workspace.ts:107
redoStack_
protected redoStack_: Abstract[] = [];
Defined in: packages/blockly/core/workspace.ts:108
Accessors
isFlyout
Get Signature
get isFlyout(): boolean;
Defined in: packages/blockly/core/workspace.ts:69
Is this workspace the surface for a flyout?
Returns
boolean
isMutator
Get Signature
get isMutator(): boolean;
Defined in: packages/blockly/core/workspace.ts:81
Is this workspace the surface for a mutator?
Returns
boolean
Methods
dispose()
dispose(): void;
Defined in: packages/blockly/core/workspace.ts:161
Dispose of this workspace. Unlink from all DOM elements to prevent memory leaks.
Returns
void
sortByOrigin()
protected sortByOrigin(a, b): number;
Defined in: packages/blockly/core/workspace.ts:201
Sorts bounded elements on the workspace by their relative position, top to bottom (with slight LTR or RTL bias).
Parameters
| Parameter | Type | Description |
|---|---|---|
a | IBoundedElement | The first element to sort. |
b | IBoundedElement | The second elment to sort. |
Returns
number
-1, 0 or 1 depending on the sort order.
addTopBlock()
addTopBlock(block): void;
Defined in: packages/blockly/core/workspace.ts:214
Adds a block to the list of top blocks.
Parameters
| Parameter | Type | Description |
|---|---|---|
block | Block | Block to add. |
Returns
void
removeTopBlock()
removeTopBlock(block): void;
Defined in: packages/blockly/core/workspace.ts:223
Removes a block from the list of top blocks.
Parameters
| Parameter | Type | Description |
|---|---|---|
block | Block | Block to remove. |
Returns
void
getTopBlocks()
getTopBlocks(ordered?): Block[];
Defined in: packages/blockly/core/workspace.ts:236
Finds the top-level blocks and returns them. Blocks are optionally sorted by position; top to bottom (with slight LTR or RTL bias).
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
ordered | boolean | false | Sort the list if true. |
Returns
Block[]
The top-level block objects.
addTypedBlock()
addTypedBlock(block): void;
Defined in: packages/blockly/core/workspace.ts:250
Add a block to the list of blocks keyed by type.
Parameters
| Parameter | Type | Description |
|---|---|---|
block | Block | Block to add. |
Returns
void
removeTypedBlock()
removeTypedBlock(block): void;
Defined in: packages/blockly/core/workspace.ts:262
Remove a block from the list of blocks keyed by type.
Parameters
| Parameter | Type | Description |
|---|---|---|
block | Block | Block to remove. |
Returns
void
getBlocksByType()
getBlocksByType(type, ordered?): Block[];
Defined in: packages/blockly/core/workspace.ts:277
Finds the blocks with the associated type and returns them. Blocks are optionally sorted by position; top to bottom (with slight LTR or RTL bias).
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
type | string | undefined | The type of block to search for. |
ordered | boolean | false | Sort the list if true. |
Returns
Block[]
The blocks of the given type.
getAllBlocks()
getAllBlocks(ordered?): Block[];
Defined in: packages/blockly/core/workspace.ts:351
Find all blocks in workspace. Blocks are optionally sorted by position; top to bottom (with slight LTR or RTL bias).
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
ordered | boolean | false | Sort the list if true. |
Returns
Block[]
Array of blocks.
clear()
clear(): void;
Defined in: packages/blockly/core/workspace.ts:374
Dispose of all blocks and comments in workspace.
Returns
void
getWidth()
getWidth(): number;
Defined in: packages/blockly/core/workspace.ts:411
Returns the horizontal offset of the workspace. Intended for LTR/RTL compatibility in XML. Not relevant for a headless workspace.
Returns
number
Width.
newBlock()
newBlock(prototypeName, opt_id?): Block;
Defined in: packages/blockly/core/workspace.ts:426
Obtain a newly created block.
Parameters
| Parameter | Type | Description |
|---|---|---|
prototypeName | string | Name of the language object containing type-specific functions for this block. |
opt_id? | string | Optional ID. Use this ID if provided, otherwise create a new ID. |
Returns
The created block.
newComment()
newComment(id?): WorkspaceComment;
Defined in: packages/blockly/core/workspace.ts:440
Obtain a newly created comment.
Parameters
| Parameter | Type | Description |
|---|---|---|
id? | string | Optional ID. Use this ID if provided, otherwise create a new ID. |
Returns
The created comment.
remainingCapacity()
remainingCapacity(): number;
Defined in: packages/blockly/core/workspace.ts:454
The number of blocks that may be added to the workspace before reaching the maxBlocks.
Returns
number
Number of blocks left.
remainingCapacityOfType()
remainingCapacityOfType(type): number;
Defined in: packages/blockly/core/workspace.ts:469
The number of blocks of the given type that may be added to the workspace before reaching the maxInstances allowed for that type.
Parameters
| Parameter | Type | Description |
|---|---|---|
type | string | Type of block to return capacity for. |
Returns
number
Number of blocks of type left.
isCapacityAvailable()
isCapacityAvailable(typeCountsMap): boolean;
Defined in: packages/blockly/core/workspace.ts:492
Check if there is remaining capacity for blocks of the given counts to be created. If the total number of blocks represented by the map is more than the total remaining capacity, it returns false. If a type count is more than the remaining capacity for that type, it returns false.
Parameters
| Parameter | Type | Description |
|---|---|---|
typeCountsMap | { [key: string]: number; } | A map of types to counts (usually representing blocks to be created). |
Returns
boolean
True if there is capacity for the given map, false otherwise.
hasBlockLimits()
hasBlockLimits(): boolean;
Defined in: packages/blockly/core/workspace.ts:515
Checks if the workspace has any limits on the maximum number of blocks, or the maximum number of blocks of specific types.
Returns
boolean
True if it has block limits, false otherwise.
undo()
undo(redo?): void;
Defined in: packages/blockly/core/workspace.ts:544
Undo or redo the previous action.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
redo | boolean | false | False if undo, true if redo. |
Returns
void
redo()
redo(): void;
Defined in: packages/blockly/core/workspace.ts:581
Redoes the previous action.
Returns
void
clearUndo()
clearUndo(): void;
Defined in: packages/blockly/core/workspace.ts:586
Clear the undo/redo stacks.
Returns
void
addChangeListener()
addChangeListener(func): (e) => void;
Defined in: packages/blockly/core/workspace.ts:602
When something in this workspace changes, call a function. Note that there may be a few recent events already on the stack. Thus the new change listener might be called with events that occurred a few milliseconds before the change listener was added.
Parameters
| Parameter | Type | Description |
|---|---|---|
func | (e) => void | Function to call. |
Returns
Obsolete return value, ignore.
(e) => void
removeChangeListener()
removeChangeListener(func): void;
Defined in: packages/blockly/core/workspace.ts:612
Stop listening for this workspace's changes.
Parameters
| Parameter | Type | Description |
|---|---|---|
func | (e) => void | Function to stop calling. |
Returns
void
fireChangeListener()
fireChangeListener(event): void;
Defined in: packages/blockly/core/workspace.ts:621
Fire a change event.
Parameters
| Parameter | Type | Description |
|---|---|---|
event | Abstract | Event to fire. |
Returns
void
getBlockById()
getBlockById(id): Block | null;
Defined in: packages/blockly/core/workspace.ts:641
Find the block on this workspace with the specified ID.
Parameters
| Parameter | Type | Description |
|---|---|---|
id | string | ID of block to find. |
Returns
Block | null
The sought after block, or null if not found.
getCommentById()
getCommentById(id):
| WorkspaceComment
| null;
Defined in: packages/blockly/core/workspace.ts:672
Find the comment on this workspace with the specified ID.
Parameters
| Parameter | Type | Description |
|---|---|---|
id | string | ID of comment to find. |
Returns
| WorkspaceComment
| null
The sought after comment, or null if not found.
allInputsFilled()
allInputsFilled(opt_shadowBlocksAreFilled?): boolean;
Defined in: packages/blockly/core/workspace.ts:684
Checks whether all value and statement inputs in the workspace are filled with blocks.
Parameters
| Parameter | Type | Description |
|---|---|---|
opt_shadowBlocksAreFilled? | boolean | An optional argument controlling whether shadow blocks are counted as filled. Defaults to true. |
Returns
boolean
True if all inputs are filled, false otherwise.
getPotentialVariableMap()
getPotentialVariableMap():
| IVariableMap<IVariableModel<IVariableState>>
| null;
Defined in: packages/blockly/core/workspace.ts:701
Return the variable map that contains "potential" variables. These exist in the flyout but not in the workspace.
Returns
| IVariableMap<IVariableModel<IVariableState>>
| null
The potential variable map.
getVariableMap()
getVariableMap(): IVariableMap<IVariableModel<IVariableState>>;
Defined in: packages/blockly/core/workspace.ts:722
Return the map of all variables on the workspace.
Returns
IVariableMap<IVariableModel<IVariableState>>
The variable map.
getProcedureMap()
getProcedureMap(): IProcedureMap;
Defined in: packages/blockly/core/workspace.ts:737
Returns the map of all procedures on the workpace.
Returns
getRootWorkspace()
getRootWorkspace(): Workspace | null;
Defined in: packages/blockly/core/workspace.ts:748
Returns the root workspace of this workspace if the workspace has parent(s).
E.g. workspaces in flyouts and mini workspace bubbles have parent workspaces.
Returns
Workspace | null
getById()
static getById(id): Workspace | null;
Defined in: packages/blockly/core/workspace.ts:769
Find the workspace with the specified ID.
Parameters
| Parameter | Type | Description |
|---|---|---|
id | string | ID of workspace to find. |
Returns
Workspace | null
The sought after workspace or null if not found.
getAll()
static getAll(): Workspace[];
Defined in: packages/blockly/core/workspace.ts:778
Find all workspaces.
Returns
Workspace[]
Array of workspaces.
getVariableMapClass()
protected getVariableMapClass(): (...p1) => IVariableMap<IVariableModel<IVariableState>>;
Defined in: packages/blockly/core/workspace.ts:782
Returns
(...p1) => IVariableMap<IVariableModel<IVariableState>>
isReadOnly()
isReadOnly(): boolean;
Defined in: packages/blockly/core/workspace.ts:801
Returns whether or not this workspace is in readonly mode.
Returns
boolean
True if the workspace is readonly, otherwise false.
setIsReadOnly()
setIsReadOnly(readOnly): void;
Defined in: packages/blockly/core/workspace.ts:810
Sets whether or not this workspace is in readonly mode.
Parameters
| Parameter | Type | Description |
|---|---|---|
readOnly | boolean | True to make the workspace readonly, otherwise false. |
Returns
void