过程会阻止所有引用定义过程签名(名称、参数和返回)的后备数据模型。@blockly/block-shareable-procedures 插件提供的数据模型旨在复制 Blockly 的旧版内置程序块的行为。
这包括您可能不希望在自定义程序块中出现的某些行为,例如:
- 不支持退回类型
- 所有形参都与一个全局变量相关联
如果您需要不同的行为,可以创建自己的自定义程序数据模型。
程序模型实现
您的程序数据模型需要实现 IProcedureModel
接口。
class MyProcedureModel {
constructor(workspace, name, id) {
this.workspace = workspace;
this.name = name;
this.id = id;
// Note: construction should not add the model to the procedure map.
},
// Other methods are omitted for brevity...
}
参数模型实现
您的参数数据模型需要实现 IParameterModel
接口。
class MyParameterModel {
constructor(workspace, name, id) {
this.workspace = workspace;
this.name = name;
this.id = id;
},
setProcedureModel(model) {
this.model = model;
return this;
}
// Other methods are omitted for brevity...
}
触发器更改
任何会触发程序模型更改的方法都应从 @blockly/block-shareable-procedures 插件中调用 triggerProceduresUpdate
。这会针对所有程序块调用 doProcedureUpdate
,从而导致它们重新渲染。
import {triggerProceduresUpdate} from '@blockly/block-shareable-procedures';
class MyProcedureModel {
setName(name) {
this.name = name;
triggerProcedureUpdate();
return this;
}
// Other methods are omitted for brevity...
}
class MyParameterModel {
setName(name) {
this.name = name;
triggerProcedureUpdate();
return this;
}
// Other methods are omitted for brevity...
}
事件
@blockly/block-shareable-procedures 插件中的程序模型也会在程序被修改时触发事件。这样一来,多个工作区就可以保持同步,并且可以在这些工作区之间共享程序模型。您也可以选择是否要触发事件。
反序列化
您的每个类还需要一个 static loadState
方法来支持反序列化。
class MyProcedureModel {
static loadState(state, workspace) {
// Note that the procedure model should not deserialize parameters.
// The deserializer will handle that.
return new MyProcedureModel(workspace, state.name, state.id);
}
// Other methods are omitted for brevity...
}
class MyParameterModel {
static loadState(state, workspace) {
return new MyParameterModel(workspace, state.name, state.id);
}
// Other methods are omitted for brevity...
}