ใช้บับเบิลป๊อปอัป

ไอคอนในตัวจะใช้ UI สไตล์ป๊อปอัปแบบฟองอากาศเพื่อแสดงข้อมูลเพิ่มเติมหรือเครื่องมือแก้ไข ไอคอนที่กำหนดเองยังทำซ้ำลักษณะการทำงานนี้ได้ เพื่อคงรูปลักษณ์ที่สม่ำเสมอ

หากไอคอนควรแสดงฟอง คุณต้องใช้อินเทอร์เฟซ IHasBubble

แสดงหรือซ่อนฟีเจอร์นี้

ไอคอนที่ใช้ลูกโป่งควรใช้เมธอด setBubbleVisible เพื่อแสดงหรือซ่อนลูกโป่ง

// Implement the setBubbleVisible method of the IHasBubble interface.
async setBubbleVisible(visible) {
  // State is already correct.
  if (!!this.myBubble === visible) return;

  // Wait for queued renders to finish so that the icon will be correctly
  // positioned before displaying the bubble.
  await Blockly.renderManagement.finishQueuedRenders();

  if (visible) {
    this.myBubble = new MyBubble(this.getAnchorLocation(), this.getOwnerRect());
  } else {
    this.myBubble?.dispose();
  }
}

// Implement helper methods for getting the anchor location and bounds.

// Returns the location of the middle of this icon in workspace coordinates.
getAnchorLocation() {
  const size = this.getSize();
  const midIcon = new Blockly.utils.Coordinate(size.width / 2, size.height / 2);
  return Blockly.utils.Coordinate.sum(this.workspaceLocation, midIcon);
}

// Returns the rect the bubble should avoid overlapping, i.e. the block this
// icon is appended to.
getOwnerRect() {
  const bbox = this.sourceBlock.getSvgRoot().getBBox();
  return new Blockly.utils.Rect(
      bbox.y, bbox.y + bbox.height, bbox.x, bbox.x + bbox.width);
}

จัดการกับการลากบล็อก

เมื่อไอคอนเปลี่ยนตำแหน่ง ฟองจะไม่ย้ายตามโดยอัตโนมัติ คุณต้องอัปเดตตำแหน่งของฟองข้อความหรือซ่อนฟองข้อความ ซึ่งทําได้ภายในเมธอด onLocationChange ของอินเทอร์เฟซ IIcon

onLocationChange(blockOrigin) {
  super.onLocationChange(blockOrigin);
  this.myBubble?.setAnchorLocation(this.getAnchorLocation());
}

แสดงลูกโป่งอีกครั้ง

อินเทอร์เฟซ IHasBubble ยังกำหนดให้คุณใช้เมธอด bubbleIsVisible ที่แสดงผลว่าฟองข้อความจะแสดงหรือไม่

isBubbleVisible() {
  return !!this.myBubble;
}