สร้างบล็อกขั้นตอนที่กำหนดเอง

คุณจำเป็นต้องดำเนินการต่อไปนี้เพื่อสร้างการบล็อกขั้นตอนที่กำหนดเอง

  1. ติดตั้งปลั๊กอิน @blockly/block-shareable-procedures ตามที่อธิบายไว้ในหน้าการใช้ขั้นตอน
  2. ใช้ระบบการทำให้เป็นอนุกรม JSON ตามที่อธิบายไว้ในหน้าภาพรวม

เพิ่มโมเดลข้อมูลลงในพื้นที่ทำงาน

ทั้งการกำหนดขั้นตอนและการบล็อกการเรียกใช้กระบวนการจะอ้างอิงโมเดลข้อมูลสนับสนุนซึ่งกำหนดลายเซ็นของกระบวนการ (ชื่อ พารามิเตอร์ และการคืนสินค้า) วิธีนี้จะทำให้คุณออกแบบแอปพลิเคชันได้อย่างยืดหยุ่นมากขึ้น (เช่น คุณสามารถอนุญาตให้กำหนดขั้นตอนในพื้นที่ทำงานหนึ่ง และอ้างอิงในอีกพื้นที่ทำงานหนึ่งได้)

ซึ่งหมายความว่า คุณจะต้องเพิ่มโมเดลข้อมูลกระบวนการลงในพื้นที่ทำงานเพื่อให้การบล็อกทำงานได้ ซึ่งทำได้หลายวิธี (เช่น UI ที่กำหนดเอง)

โดย @blockly/block-shareable-procedures จะดำเนินการนี้ได้ด้วยการมีบล็อก schema-definition ในการสร้างโมเดลข้อมูลสนับสนุนแบบไดนามิก เมื่ออินสแตนซ์ดังกล่าวสร้างขึ้นในพื้นที่ทำงาน หากต้องการติดตั้งใช้งานด้วยตนเอง ให้สร้างโมเดลใน init และลบออกใน destroy

import {ObservableProcedureModel} from '@blockly/block-shareable-procedures';

Blockly.Blocks['my_procedure_def'] = {
  init: function() {
    this.model = new ObservableProcedureModel('default name');
    this.workspace.getProcedureMap().add(model);
    // etc...
  }

  destroy: function() {
    // (Optionally) Destroy the model when the definition block is deleted.

    // Insertion markers reference the model of the original block.
    if (this.isInsertionMarker()) return;
    this.workpace.getProcedureMap().delete(model.getId());
  }
}

แสดงข้อมูลเกี่ยวกับการบล็อก

การกำหนดขั้นตอนและการบล็อกการเรียกใช้กระบวนการต้องใช้เมธอด getProcedureModel, isProcedureDef และ getVarModels ตัวอย่างการแทรกโค้ดของ Blockly ที่ใช้สำหรับรับข้อมูลเกี่ยวกับบล็อกขั้นตอนของคุณ

Blockly.Blocks['my_procedure_def'] = {
  getProcedureModel() {
    return this.model;
  },

  isProcedureDef() {
    return true;
  },

  getVarModels() {
    // If your procedure references variables
    // then you should return those models here.
    return [];
  },
};

Blockly.Blocks['my_procedure_call'] = {
  getProcedureModel() {
    return this.model;
  },

  isProcedureDef() {
    return false;
  },

  getVarModels() {
    // If your procedure references variables
    // then you should return those models here.
    return [];
  },
};

ทริกเกอร์การแสดงผลอีกครั้งเมื่อมีการอัปเดต

การกำหนดขั้นตอนและการบล็อกการเรียกใช้กระบวนการต้องใช้เมธอด doProcedureUpdate นี่คือฮุกที่โมเดลข้อมูลเรียกใช้เพื่อบอกให้บล็อกขั้นตอนแสดงผลอีกครั้งเอง

Blockly.Blocks['my_procedure_def'] = {
  doProcedureUpdate() {
    this.setFieldValue('NAME', this.model.getName());
    this.setFieldValue(
        'PARAMS',
        this.model.getParameters()
            .map((p) => p.getName())
            .join(','));
    this.setFieldValue(
        'RETURN', this.model.getReturnTypes().join(',');
  }
};

Blockly.Blocks['my_procedure_call'] = {
  doProcedureUpdate() {
    // Similar to the def block above...
  }
};

เพิ่มการซีเรียลไลซ์ที่กำหนดเอง

การเรียงอันดับสำหรับบล็อกกระบวนการต้องทำ 2 สิ่งแยกกัน

  1. เมื่อโหลดจาก JSON บล็อกจะต้องจับการอ้างอิงไปยังโมเดลข้อมูลสํารอง เนื่องจากบล็อกและโมเดลต่างๆ จะเรียงลำดับแยกกัน
  2. เมื่อคัดลอกและวางบล็อกกระบวนการ บล็อกดังกล่าวจะต้องทำให้สถานะของโมเดลขั้นตอนเป็นอนุกรมเพื่อให้จำลองได้

ทั้ง 2 อย่างนี้จัดการผ่าน saveExtraState และ loadExtraState ขอย้ำอีกครั้งว่าการบล็อกขั้นตอนที่กำหนดเองจะรองรับเฉพาะเมื่อใช้ระบบการเรียงอันดับ JSON เท่านั้น เราจึงต้องกำหนดเฉพาะฮุกอนุกรม JSON

import {
    ObservableProcedureModel,
    ObservableParameterModel,
    isProcedureBlock
} from '@blockly/block-shareable-procedures';

Blockly.Blocks['my_procedure_def'] = {
  // When doFullSerialization is true, we should serialize the full state of
  // the model.
  saveExtraState(doFullSerialization) {
    const state = Object.create(null);
    state['procedureId']: this.model.getId();

    if (doFullSerialization) {
      state['name'] = this.model.getName();
      state['parameters'] = this.model.getParameters().map((p) => {
        return {name: p.getName(), p.getId()};
      });
      state['returnTypes'] = this.model.getReturnTypes();

      // Flag for deserialization.
      state['createNewModel'] = true;
    }

    return state;
  },

  loadExtraState(state) {
    const id = state['procedureId']
    const map = this.workspace.getProcedureMap();

    if (map.has(id) && !state['createNewModel']) {
      // Delete the existing model (created in init).
      map.delete(this.model.getId());
      // Grab a reference to the model we're supposed to reference.
      this.model = map.get(id);
      this.doProcedureUpdate();
      return;
    }

    // There is no existing procedure model (we are likely pasting), so
    // generate it from JSON.
    this.model
        .setName(state['name'])
        .setReturnTypes(state['returnTypes']);
    for (const [i, param] of state['parameters'].entries()) {
      this.model.insertParameter(
          i,
          new ObservableParameterModel(
              this.workspace, param['name'], param['id']));
    }
    this.doProcedureUpdate();
  },
};

Blockly.Blocks['my_procedure_call'] = {
  saveExtraState() {
    return {
      'procedureId': this.model.getId(),
    };
  },

  loadExtraState(state) {
    // Delete our existing model (created in init).
    this.workspace.getProcedureMap().delete(model.getId());
    // Grab a reference to the new model.
    this.model = this.workspace.getProcedureMap()
        .get(state['procedureId']);
    if (this.model) this.doProcedureUpdate();
  },

  // Handle pasting after the procedure definition has been deleted.
  onchange(event) {
    if (event.type === Blockly.Events.BLOCK_CREATE &&
        event.blockId === this.id) {
      if(!this.model) { // Our procedure definition doesn't exist =(
        this.dispose();
      }
    }
  }
};

เลือกแก้ไขโมเดลขั้นตอนได้

นอกจากนี้คุณยังเพิ่มความสามารถให้ผู้ใช้แก้ไขโมเดลกระบวนการได้อีกด้วย การเรียกใช้เมธอด insertParameter, deleteParameter หรือ setReturnTypes จะทริกเกอร์ให้บล็อกแสดงผลอีกครั้ง (ผ่าน doProcedureUpdate) โดยอัตโนมัติ

ตัวเลือกสำหรับการสร้าง UI เพื่อแก้ไขโมเดลขั้นตอน ได้แก่ การใช้ตัวเปลี่ยนแปลง (ซึ่งกระบวนการที่มีในตัวบล็อกใช้) ช่องรูปภาพที่มีตัวแฮนเดิลคลิก หรือบางอย่างที่อยู่นอก Blockly อย่างสิ้นเชิง เป็นต้น

เพิ่มบล็อกในกล่องเครื่องมือ

หมวดหมู่ขั้นตอนแบบไดนามิกที่มีมาให้ Blockly ใช้กับการบล็อกขั้นตอนแบบบิวท์อินใน Blockly โดยเฉพาะ ในการเข้าถึงการบล็อก คุณจะต้องกำหนดหมวดหมู่แบบไดนามิกที่กำหนดเองของคุณเอง และเพิ่มลงในกล่องเครื่องมือ

const proceduresFlyoutCallback = function(workspace) {
  const blockList = [];
  blockList.push({
    'kind': 'block',
    'type': 'my_procedure_def',
  });
  for (const model of
        workspace.getProcedureMap().getProcedures()) {
    blockList.push({
      'kind': 'block',
      'type': 'my_procedure_call',
      'extraState': {
        'procedureId': model.getId(),
      },
    });
  }
  return blockList;
};

myWorkspace.registerToolboxCategoryCallback(
    'MY_PROCEDURES', proceduresFlyoutCallback);