Skip to main content

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

PropertyDescription
SCAN_ANGLEAngle 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-
renderedReturns true if the workspace is visible and false if it's headless.
MAX_UNDOMaximum number of undo events in stack. 0 turns off undo, Infinity sets it to unlimited.
connectionDBListSet of databases for rapid lookup of connection locations.
connectionChecker-
undoStack_-
redoStack_-

Methods index

MethodDescription
disposeDispose of this workspace. Unlink from all DOM elements to prevent memory leaks.
sortByOriginSorts bounded elements on the workspace by their relative position, top to bottom (with slight LTR or RTL bias).
addTopBlockAdds a block to the list of top blocks.
removeTopBlockRemoves a block from the list of top blocks.
getTopBlocksFinds the top-level blocks and returns them. Blocks are optionally sorted by position; top to bottom (with slight LTR or RTL bias).
addTypedBlockAdd a block to the list of blocks keyed by type.
removeTypedBlockRemove a block from the list of blocks keyed by type.
getBlocksByTypeFinds the blocks with the associated type and returns them. Blocks are optionally sorted by position; top to bottom (with slight LTR or RTL bias).
getAllBlocksFind all blocks in workspace. Blocks are optionally sorted by position; top to bottom (with slight LTR or RTL bias).
clearDispose of all blocks and comments in workspace.
getWidthReturns the horizontal offset of the workspace. Intended for LTR/RTL compatibility in XML. Not relevant for a headless workspace.
newBlockObtain a newly created block.
newCommentObtain a newly created comment.
remainingCapacityThe number of blocks that may be added to the workspace before reaching the maxBlocks.
remainingCapacityOfTypeThe number of blocks of the given type that may be added to the workspace before reaching the maxInstances allowed for that type.
isCapacityAvailableCheck 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.
hasBlockLimitsChecks if the workspace has any limits on the maximum number of blocks, or the maximum number of blocks of specific types.
undoUndo or redo the previous action.
redoRedoes the previous action.
clearUndoClear the undo/redo stacks.
addChangeListenerWhen 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.
removeChangeListenerStop listening for this workspace's changes.
fireChangeListenerFire a change event.
getBlockByIdFind the block on this workspace with the specified ID.
getCommentByIdFind the comment on this workspace with the specified ID.
allInputsFilledChecks whether all value and statement inputs in the workspace are filled with blocks.
getPotentialVariableMapReturn the variable map that contains "potential" variables. These exist in the flyout but not in the workspace.
getVariableMapReturn the map of all variables on the workspace.
getProcedureMapReturns the map of all procedures on the workpace.
getRootWorkspaceReturns the root workspace of this workspace if the workspace has parent(s).
getByIdFind the workspace with the specified ID.
getAllFind all workspaces.
getVariableMapClass-
isReadOnlyReturns whether or not this workspace is in readonly mode.
setIsReadOnlySets 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

ParameterTypeDescription
opt_options?OptionsDictionary 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

ParameterTypeDescription
aIBoundedElementThe first element to sort.
bIBoundedElementThe 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

ParameterTypeDescription
blockBlockBlock 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

ParameterTypeDescription
blockBlockBlock 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

ParameterTypeDefault valueDescription
orderedbooleanfalseSort 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

ParameterTypeDescription
blockBlockBlock 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

ParameterTypeDescription
blockBlockBlock 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

ParameterTypeDefault valueDescription
typestringundefinedThe type of block to search for.
orderedbooleanfalseSort 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

ParameterTypeDefault valueDescription
orderedbooleanfalseSort 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

ParameterTypeDescription
prototypeNamestringName of the language object containing type-specific functions for this block.
opt_id?stringOptional ID. Use this ID if provided, otherwise create a new ID.

Returns

Block

The created block.


newComment()

newComment(id?): WorkspaceComment;

Defined in: packages/blockly/core/workspace.ts:440

Obtain a newly created comment.

Parameters

ParameterTypeDescription
id?stringOptional ID. Use this ID if provided, otherwise create a new ID.

Returns

WorkspaceComment

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

ParameterTypeDescription
typestringType 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

ParameterTypeDescription
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

ParameterTypeDefault valueDescription
redobooleanfalseFalse 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

ParameterTypeDescription
func(e) => voidFunction 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

ParameterTypeDescription
func(e) => voidFunction to stop calling.

Returns

void


fireChangeListener()

fireChangeListener(event): void;

Defined in: packages/blockly/core/workspace.ts:621

Fire a change event.

Parameters

ParameterTypeDescription
eventAbstractEvent 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

ParameterTypeDescription
idstringID 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

ParameterTypeDescription
idstringID 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

ParameterTypeDescription
opt_shadowBlocksAreFilled?booleanAn 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

IProcedureMap


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

ParameterTypeDescription
idstringID 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

ParameterTypeDescription
readOnlybooleanTrue to make the workspace readonly, otherwise false.

Returns

void