打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Events  |  Blockly  |  Google Developers

     Events

Every change on the workspace triggers an event. These events fully describethe before and after state of each change.

Listening to Events

Workspaces have addChangeListener and removeChangeListener methods that canbe used to listen to the event stream. One example is therealtime generation of code.Another example is themaximum block limit demo.As is often the case, neither of these two examples care what the triggeringevent was. They simply look at the current state of the workspace.

A more sophisticated event listener would look at the triggering event. Thefollowing example detects when the user creates his or her first comment,issues an alert, then stops listening so that no further alerts are triggered.

function onFirstComment(event) {
  if (event.type == Blockly.Events.CHANGE &&
      event.element == 'comment' &&
      !event.oldValue && event.newValue) {
    alert('Congratulations on creating your first comment!')
    workspace.removeChangeListener(onFirstComment);
  }
}
workspace.addChangeListener(onFirstComment);

Blocks have another method of listening to the event stream. A block candefine anonchangefunction which will get called whenever a change occurs on the block'sworkspace.

Event Types

All events share the following common properties.

NameTypeDescription
typestringOne of Blockly.Events.CREATE, Blockly.Events.DELETE, Blockly.Events.CHANGE, Blockly.Events.MOVE, Blockly.Events.UI.
workspaceIdstringUUID of workspace. The workspace can be found with Blockly.Workspace.getById(event.workspaceId)
blockIdstringUUID of block. The block can be found with workspace.getBlockById(event.blockId)
groupstringUUID of group. Some events are part of an indivisible group, such as inserting a statement in a stack.

Blockly.Events.BLOCK_CREATE

Block create events have two additional properties.

NameTypeDescription
xmlobjectAn XML tree defining the new block and any connected child blocks.
idsarrayAn array containing the UUIDs of the new block and any connected child blocks.

Blockly.Events.BLOCK_DELETE

Block delete events have two additional properties.

NameTypeDescription
oldXmlobjectAn XML tree defining the deleted block and any connected child blocks.
idsarrayAn array containing the UUIDs of the deleted block and any connected child blocks.

Blockly.Events.BLOCK_CHANGE

Block change events have four additional properties.

NameTypeDescription
elementstringOne of 'field', 'comment', 'collapsed', 'disabled', 'inline', 'mutate'
namestringName of the field if this is a change to a field.
oldValuevalueOriginal value.
newValuevalueChanged value.

Blockly.Events.BLOCK_MOVE

Block move events have six additional properties.

NameTypeDescription
oldParentIdstringUUID of old parent block. Undefined if it was a top level block.
oldInputNamestringName of input on old parent. Undefined if it was a top level block or parent's next block.
oldCoordinateobjectX and Y coordinates if it was a top level block. Undefined if it had a parent.
newParentIdstringUUID of new parent block. Undefined if it is a top level block.
newInputNamestringName of input on new parent. Undefined if it is a top level block or parent's next block.
newCoordinateobjectX and Y coordinates if it is a top level block. Undefined if it has a parent.

Blockly.Events.VAR_CREATE

Variable create events have two additional properties.

NameTypeDescription
varTypestringThe type of the variable like 'int' or 'string'. Does not need to be unique. This will default to "" which is a specific type.
varNamestringThe name of the variable. This is unique across variables and procedures.
varIdstringThe unique id of the variable.

Blockly.Events.VAR_DELETE

Variable delete events have two additional properties.

NameTypeDescription
varTypestringThe type of the variable like 'int' or 'string'. Does not need to be unique. This will default to "" which is a specific type.
varNamestringThe name of the variable. This is unique across variables and procedures.
varIdstringThe unique id of the variable.

Blockly.Events.VAR_RENAME

Variable rename events have two additional properties.

NameTypeDescription
oldNamestringThe current name of the variable. This is unique across variables and procedures.
newNamestringThe new name of the variable. This is unique across variables and procedures.
varIdstringThe unique id of the variable.

Blockly.Events.UI

UI events have three additional properties.

NameTypeDescription
elementstringOne of 'selected', 'category', 'click', 'commentOpen', 'mutatorOpen', 'warningOpen'
oldValuevalueOriginal value.
newValuevalueChanged value.

It is expected that the list of UI actions represented by UI events will becomemore comprehensive over time.For instance events for scrolling, zooming, dragging bubbles, etc.

Here isa live demoof two Blockly instances using events to synchronize between them.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
ITeye Java编程 Spring框架 Ajax技术 agile敏捷软件开发 ruby on r...
openwrt 挂载overlay到U盘 成功
生成UUID
objective
美国Parkmerced Block 6 Apartments公寓
java使用SecureRandom类生成32位的uiid唯一主键(代码)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服