Modeling
建模服务,提供图表元素的创建、修改、删除等核心建模操作。
模块说明
Modeling 是 diagram-js 最核心的服务,它封装了所有的建模操作(如移动、调整大小、创建、删除元素等),并通过 CommandStack 执行这些操作,确保所有变更都可撤销和重做。
核心特性
- 可撤销操作: 所有操作通过 CommandStack 执行,支持撤销/重做
- 丰富的 API: 提供 20+ 个建模方法
- 命令处理器: 为每种操作注册专门的命令处理器
- 事件触发: 操作过程中触发相应的生命周期事件
- 提示支持: 所有方法支持 hints 参数传递额外信息
模块依赖
javascript
Modeling.$inject = ["eventBus", "elementFactory", "commandStack"];eventBus{EventBus}: 事件总线elementFactory{ElementFactory}: 元素工厂commandStack{CommandStack}: 命令栈
公共方法概览
图形操作
createShape()- 创建图形moveShape()- 移动图形resizeShape()- 调整图形大小removeShape()- 删除图形replaceShape()- 替换图形appendShape()- 创建并连接图形toggleCollapse()- 折叠/展开图形
连接线操作
createConnection()/connect()- 创建连接线moveConnection()- 移动连接线removeConnection()- 删除连接线updateWaypoints()- 更新路径点reconnect()/reconnectStart()/reconnectEnd()- 重新连接layoutConnection()- 布局连接线
批量操作
createElements()- 创建多个元素moveElements()- 移动多个元素removeElements()- 删除多个元素alignElements()- 对齐元素distributeElements()- 分布元素
其他操作
createLabel()- 创建标签updateAttachment()- 更新附着createSpace()- 创建空间
详细方法文档
createShape()
作用: 创建一个新图形。
参数:
shape{Partial<Shape>}: 图形属性position{Point}: 位置{x, y}target{Parent}: 父元素parentIndex{number}(可选): 插入位置hints{Object}(可选): 提示信息
返回值: {Shape} - 创建的图形
示例:
javascript
const modeling = diagram.get("modeling");
// 创建图形
const shape = modeling.createShape(
{ type: "custom:Task", width: 100, height: 80 },
{ x: 200, y: 200 },
rootElement,
);
// 创建附着元素
const boundaryEvent = modeling.createShape(
{ type: "custom:BoundaryEvent", width: 36, height: 36 },
{ x: task.x + task.width, y: task.y + task.height / 2 },
task,
{ attach: true },
);moveShape()
作用: 移动图形。
参数:
shape{Shape}: 要移动的图形delta{Point}: 移动偏移量{x, y}newParent{Parent}(可选): 新的父元素newParentIndex{number}(可选): 新位置索引hints{Object}(可选): 提示信息
示例:
javascript
// 移动图形
modeling.moveShape(shape, { x: 50, y: 30 });
// 移动到新父元素
modeling.moveShape(shape, { x: 0, y: 0 }, newParent);resizeShape()
作用: 调整图形大小。
参数:
shape{Shape}: 要调整的图形newBounds{Rect}: 新的边界{x, y, width, height}minBounds{Dimensions}(可选): 最小尺寸hints{Object}(可选): 提示信息
示例:
javascript
modeling.resizeShape(shape, {
x: shape.x,
y: shape.y,
width: 150,
height: 100,
});removeShape() / removeElements()
作用: 删除图形或多个元素。
示例:
javascript
// 删除单个图形
modeling.removeShape(shape);
// 删除多个元素
modeling.removeElements([shape1, shape2, connection1]);createConnection() / connect()
作用: 创建连接线。
参数:
source{Element}: 源元素target{Element}: 目标元素attrs{Partial<Connection>}: 连接线属性parent{Parent}: 父元素hints{Object}(可选): 提示信息
返回值: {Connection} - 创建的连接线
示例:
javascript
// 简单连接
const connection = modeling.connect(source, target);
// 带属性的连接
const connection = modeling.createConnection(
source,
target,
{ type: "custom:SequenceFlow" },
rootElement,
);updateWaypoints()
作用: 更新连接线的路径点。
参数:
connection{Connection}: 连接线newWaypoints{Point[]}: 新的路径点hints{Object}(可选): 提示信息
示例:
javascript
const newWaypoints = [
{ x: 100, y: 100 },
{ x: 200, y: 100 },
{ x: 200, y: 200 },
{ x: 300, y: 200 },
];
modeling.updateWaypoints(connection, newWaypoints);reconnect() / reconnectStart() / reconnectEnd()
作用: 重新连接连接线的源或目标。
示例:
javascript
// 重新连接源
modeling.reconnectStart(connection, newSource, { x: 100, y: 100 });
// 重新连接目标
modeling.reconnectEnd(connection, newTarget, { x: 300, y: 200 });
// 同时重新连接源和目标
modeling.reconnect(connection, newSource, newTarget, [
{ x: 100, y: 100 },
{ x: 200, y: 150 },
{ x: 300, y: 200 },
]);appendShape()
作用: 创建图形并连接到源元素。
参数:
source{Element}: 源元素shape{Partial<Shape>}: 新图形属性position{Point}: 位置target{Parent}: 父元素hints{Object}(可选): 提示信息(可包含connection和connectionParent)
返回值: {Shape} - 创建的图形
示例:
javascript
const newTask = modeling.appendShape(
currentTask,
{ type: "custom:Task", width: 100, height: 80 },
{ x: 300, y: 200 },
rootElement,
{
connection: { type: "custom:SequenceFlow" },
},
);moveElements()
作用: 移动多个元素。
参数:
elements{Element[]}: 要移动的元素delta{Point}: 移动偏移量target{Parent}(可选): 新的父元素hints{Object}(可选): 提示信息
示例:
javascript
// 批量移动
modeling.moveElements(selectedElements, { x: 50, y: 30 });
// 移动到新父元素
modeling.moveElements(elements, { x: 0, y: 0 }, newParent);alignElements()
作用: 对齐多个元素。
参数:
elements{Element[]}: 要对齐的元素alignment{Object}: 对齐方式
对齐选项:
left: 左对齐center: 水平居中right: 右对齐top: 顶部对齐middle: 垂直居中bottom: 底部对齐
示例:
javascript
// 左对齐
modeling.alignElements(elements, { left: true });
// 水平和垂直居中
modeling.alignElements(elements, { center: true, middle: true });distributeElements()
作用: 均匀分布元素。
参数:
groups{Array}: 分布组axis{'x' | 'y'}: 分布轴dimension{'width' | 'height'}: 分布维度
示例:
javascript
const groups = [
{
elements: elements,
range: { min: 0, max: 500 },
},
];
// 水平均匀分布
modeling.distributeElements(groups, "x", "width");replaceShape()
作用: 替换图形。
参数:
oldShape{Shape}: 要替换的图形newShape{Partial<Shape>}: 新图形属性hints{Object}(可选): 提示信息
返回值: {Shape} - 新图形
示例:
javascript
const newShape = modeling.replaceShape(oldTask, {
type: "custom:ServiceTask",
width: 100,
height: 80,
});createLabel()
作用: 创建标签。
参数:
labelTarget{Element}: 标签的目标元素position{Point}: 位置label{Partial<Label>}: 标签属性parent{Parent}(可选): 父元素
返回值: {Label} - 创建的标签
layoutConnection()
作用: 重新布局连接线。
参数:
connection{Connection}: 连接线hints{Object}(可选): 提示信息
示例:
javascript
// 自动布局连接线
modeling.layoutConnection(connection);createSpace()
作用: 创建空间(用于 Space Tool)。
参数:
movingShapes{Shape[]}: 要移动的图形resizingShapes{Shape[]}: 要调整大小的图形delta{Point}: 偏移量direction{Direction}: 方向start{number}: 起始位置
命令处理器
Modeling 注册了以下命令处理器:
shape.create- CreateShapeHandlershape.move- MoveShapeHandlershape.resize- ResizeShapeHandlershape.delete- DeleteShapeHandlershape.replace- ReplaceShapeHandlershape.append- AppendShapeHandlershape.toggleCollapse- ToggleShapeCollapseHandlerconnection.create- CreateConnectionHandlerconnection.move- MoveConnectionHandlerconnection.delete- DeleteConnectionHandlerconnection.layout- LayoutConnectionHandlerconnection.updateWaypoints- UpdateWaypointsHandlerconnection.reconnect- ReconnectConnectionHandlerelements.create- CreateElementsHandlerelements.move- MoveElementsHandlerelements.delete- DeleteElementsHandlerelements.align- AlignElementsHandlerelements.distribute- DistributeElementsHandlerlabel.create- CreateLabelHandlerelement.updateAttachment- UpdateAttachmentHandlerspaceTool- SpaceToolHandler
Hints 参数
大多数方法支持 hints 参数来传递额外信息:
javascript
{
attach: true, // 是否附着到目标
connection: {...}, // 连接线配置
connectionParent: parent, // 连接线父元素
// ... 其他自定义提示
}使用建议
- 始终使用 Modeling: 不要直接修改元素属性,使用 Modeling 方法
- 检查返回值: 某些方法返回创建的元素,可以继续操作
- 使用 hints: 通过 hints 传递特定行为标志
- 监听事件: 通过 CommandStack 事件监听操作
相关模块
CommandStack: Modeling 底层使用命令栈ElementFactory: 创建元素时使用Rules: 验证操作是否允许CommandInterceptor: 扩展命令执行逻辑