blockly > Events

Events namespace

Classes

Class Description
BlockBase Abstract class for any event related to blocks.
BlockChange Notifies listeners when some element of a block has changed (e.g. field values, comments, etc).
BlockCreate Notifies listeners when a block (or connected stack of blocks) is created.
BlockDelete Notifies listeners when a block (or connected stack of blocks) is deleted.
BlockDrag Notifies listeners when a block is being manually dragged/dropped.
BlockFieldIntermediateChange Notifies listeners when the value of a block's field has changed but the change is not yet complete, and is expected to be followed by a block change event.
BlockMove Notifies listeners when a block is moved. This could be from one connection to another, or from one location on the workspace to another.
BubbleOpen Class for a bubble open event.
Click Notifies listeners that some blockly element was clicked.
CommentBase Abstract class for a comment event.
CommentChange Notifies listeners that the contents of a workspace comment has changed.
CommentCollapse
CommentCreate Notifies listeners that a workspace comment was created.
CommentDelete Notifies listeners that a workspace comment has been deleted.
CommentDrag Notifies listeners when a comment is being manually dragged/dropped.
CommentMove Notifies listeners that a workspace comment has moved.
CommentResize Notifies listeners that a workspace comment has resized.
FinishedLoading Notifies listeners when the workspace has finished deserializing from JSON/XML.
Selected Class for a selected event. Notifies listeners that a new element has been selected.
ThemeChange Notifies listeners that the workspace theme has changed.
ToolboxItemSelect Notifies listeners that a toolbox item has been selected.
TrashcanOpen Notifies listeners when the trashcan is opening or closing.
UiBase Base class for a UI event. UI events are events that don't need to be sent over the wire for multi-user editing to work (e.g. scrolling the workspace, zooming, opening toolbox categories). UI events do not undo or redo.
VarBase Abstract class for a variable event.
VarCreate Notifies listeners that a variable model has been created.
VarDelete Notifies listeners that a variable model has been deleted.
VarRename Notifies listeners that a variable model was renamed.
VarTypeChange Notifies listeners that a variable's type has changed.
ViewportChange

Notifies listeners that the workspace surface's position or scale has changed.

Does not notify when the workspace itself resizes.

Abstract Classes

Abstract Class Description
Abstract Abstract class for an event.

Enumerations

Enumeration Description
BubbleType
ClickTarget

Functions

Function Description
clearPendingUndo() Modify pending undo events so that when they are fired they don't land in the undo stack. Called by Workspace.clearUndo.
disable() Stop sending events. Every call to this function MUST also call enable.
disableOrphans(event) Set if a block is disabled depending on whether it is properly connected. Use this on applications where all blocks should be connected to a top block.
enable() Start sending events. Unless events were already disabled when the corresponding call to disable was made.
filter(queue)

Filter the queued events by merging duplicates, removing null events and reording BlockChange events.

History of this function:

This function was originally added in commit cf257ea5 with the intention of dramatically reduing the total number of dispatched events. Initialy it affected only BlockMove events but others were added over time.

Code was added to reorder BlockChange events added in commit 5578458, for uncertain reasons but most probably as part of an only-partially-successful attemp to fix problems with event ordering during block mutations. This code should probably have been added to the top of the function, before merging and null-removal, but was added at the bottom for now-forgotten reasons. See these bug investigations for a fuller discussion of the underlying issue and some of the failures that arose because of this incomplete/incorrect fix:

https://github.com/google/blockly/issues/8225#issuecomment-2195751783 https://github.com/google/blockly/issues/2037#issuecomment-2209696351

Later, in PR #1205 the original O(n^2) implementation was replaced by a linear-time implementation, though additonal fixes were made subsequently.

In August 2024 a number of significant simplifications were made:

This function was previously called from Workspace.prototype.undo, but the mutation of events by this function was the cause of issue #7026 (note that events would combine differently in reverse order vs. forward order). The originally-chosen fix for this was the addition (in PR #7069) of code to fireNow to post-filter the .undoStack_ and .redoStack_ of any workspace that had just been involved in dispatching events; this apparently resolved the issue but added considerable additional complexity and made it difficult to reason about how events are processed for undo/redo, so both the call from undo and the post-processing code was removed, and forward=true was made the default while calling the function with forward=false was deprecated.

At the same time, the buggy code to reorder BlockChange events was replaced by a less-buggy version of the same functionality in a new function, enqueueEvent, called from fireInternal, thus assuring that events will be in the correct order at the time filter is called.

Additionally, the event merging code was modified so that only immediately adjacent events would be merged. This simplified the implementation while ensuring that the merging of events cannot cause them to be reordered.

fire(event)

Enqueue an event to be dispatched to change listeners.

Notes:

- Events are enqueued until a timeout, generally after rendering is complete or at the end of the current microtask, if not running in a browser. - Queued events are subject to destructive modification by being combined with later-enqueued events, but only until they are fired. - Events are dispatched via the fireChangeListener method on the affected workspace.

fromJson(json, workspace) Decode the JSON into an event.
get(eventType) Gets the class for a specific event type from the registry.
getGroup() Current group.
getRecordUndo() Returns whether or not events will be added to the undo stack.
isEnabled() Returns whether events may be fired or not.
setGroup(state) Start or stop a group.
setRecordUndo(newValue) Sets whether events should be added to the undo stack.

Interfaces

Interface Description
AbstractEventJson
BlockBaseJson
BlockChangeJson
BlockCreateJson
BlockDeleteJson
BlockDragJson
BlockFieldIntermediateChangeJson
BlockMoveJson
BubbleOpenJson
ClickJson
CommentBaseJson
CommentChangeJson
CommentCollapseJson
CommentCreateJson
CommentDragJson
CommentMoveJson
CommentResizeJson
SelectedJson
ThemeChangeJson
ToolboxItemSelectJson
TrashcanOpenJson
VarBaseJson
VarCreateJson
VarDeleteJson
VarRenameJson
VarTypeChangeJson
ViewportChangeJson

Variables

Variable Description
BLOCK_CHANGE
BLOCK_CREATE
BLOCK_DELETE
BLOCK_DRAG
BLOCK_FIELD_INTERMEDIATE_CHANGE
BLOCK_MOVE
BUBBLE_OPEN
BUMP_EVENTS

List of events that cause objects to be bumped back into the visible portion of the workspace.

Not to be confused with bumping so that disconnected connections do not appear connected.

CHANGE
CLICK
COMMENT_CHANGE
COMMENT_CREATE
COMMENT_DELETE
COMMENT_DRAG
COMMENT_MOVE
COMMENT_RESIZE
CREATE
DELETE
FINISHED_LOADING
MOVE
SELECTED
THEME_CHANGE
TOOLBOX_ITEM_SELECT
TRASHCAN_OPEN
UI
VAR_CREATE
VAR_DELETE
VAR_RENAME
VIEWPORT_CHANGE

Type Aliases

Type Alias Description
BumpEvent

Type of events that cause objects to be bumped back into the visible portion of the workspace.

Not to be confused with bumping so that disconnected connections do not appear connected.