@aranzatech/diagrams-bpmn 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -3289,7 +3289,13 @@ function isManual(nodeType) {
3289
3289
  return TASK_TYPES2.has(nodeType) || nodeType === "IntermediateCatchEvent" || nodeType === "BoundaryEvent";
3290
3290
  }
3291
3291
  function isSubProcess(nodeType) {
3292
- return nodeType === "SubProcess" || nodeType === "Transaction" || nodeType === "AdHocSubProcess";
3292
+ return nodeType === "SubProcess" || nodeType === "Transaction" || nodeType === "EventSubProcess" || nodeType === "AdHocSubProcess";
3293
+ }
3294
+ function isRootProcessStart(diagram, node) {
3295
+ if (node.type !== "StartEvent") return false;
3296
+ if (!node.parentId) return true;
3297
+ const parent = nodeById(diagram, node.parentId);
3298
+ return !parent || !isSubProcess(parent.type);
3293
3299
  }
3294
3300
  function getScopeId(diagram, nodeId) {
3295
3301
  const node = nodeById(diagram, nodeId);
@@ -3525,9 +3531,7 @@ function fireAutomatic(diagram, state, token) {
3525
3531
  }
3526
3532
  function createSimulation(diagram, initialVariables = {}) {
3527
3533
  _tokenCounter = 0;
3528
- const startEvents = diagram.nodes.filter(
3529
- (n) => n.type === "StartEvent" && !n.parentId
3530
- );
3534
+ const startEvents = diagram.nodes.filter((n) => isRootProcessStart(diagram, n));
3531
3535
  const tokens = startEvents.map((n) => ({
3532
3536
  id: nextTokenId(),
3533
3537
  elementId: n.id,
@@ -3701,14 +3705,30 @@ function createBpmnNode(options) {
3701
3705
  },
3702
3706
  width: options.width ?? size.width,
3703
3707
  height: options.height ?? size.height,
3708
+ zIndex: getBpmnNodeZIndex(options.elementType),
3704
3709
  ...options.parentId ? { parentId: options.parentId } : {},
3710
+ ...options.elementType === "Lane" && options.parentId ? { extent: "parent" } : {},
3705
3711
  ...dragHandle ? { dragHandle } : {}
3706
3712
  };
3707
3713
  }
3714
+ function getBpmnNodeZIndex(elementType) {
3715
+ if (elementType === "Pool") return 0;
3716
+ if (elementType === "Lane") return 1;
3717
+ if (elementType === "BoundaryEvent") return 4;
3718
+ return 3;
3719
+ }
3720
+ function withBpmnNodeZIndexes(nodes) {
3721
+ return nodes.map((node) => {
3722
+ const zIndex = getBpmnNodeZIndex(node.data.elementType);
3723
+ return node.zIndex === zIndex ? node : { ...node, zIndex };
3724
+ });
3725
+ }
3708
3726
  var BPMN_POOL_LANE_LAYOUT = {
3709
3727
  poolHeaderSize: 30,
3710
3728
  laneHeaderSize: 24,
3711
- laneGap: 0
3729
+ laneGap: 0,
3730
+ verticalPoolHeaderSize: 28,
3731
+ minLaneSize: 96
3712
3732
  };
3713
3733
  function getBpmnDragHandleSelector(elementType) {
3714
3734
  if (elementType === "Pool") return ".pool-drag-handle";
@@ -3829,6 +3849,116 @@ function getBpmnPoolLanes(state, poolId) {
3829
3849
  function getNodeDimension(node, axis) {
3830
3850
  return node[axis] ?? node.measured?.[axis] ?? getBpmnElementSize(node.data.elementType)[axis];
3831
3851
  }
3852
+ function getBpmnLaneOrderPosition(lane, orientation) {
3853
+ const size = getBpmnNodeSize(lane);
3854
+ return orientation === "vertical" ? lane.position.x + size.width / 2 : lane.position.y + size.height / 2;
3855
+ }
3856
+ function resizeHorizontalBpmnLanes(lanes, pool) {
3857
+ const poolSize = getBpmnNodeSize(pool);
3858
+ const laneWidth = Math.max(BPMN_POOL_LANE_LAYOUT.minLaneSize, poolSize.width - BPMN_POOL_LANE_LAYOUT.poolHeaderSize);
3859
+ const laneHeight = Math.max(BPMN_POOL_LANE_LAYOUT.minLaneSize, poolSize.height / Math.max(1, lanes.length));
3860
+ return lanes.map((lane, index) => ({
3861
+ ...lane,
3862
+ position: { x: BPMN_POOL_LANE_LAYOUT.poolHeaderSize, y: index * laneHeight },
3863
+ width: laneWidth,
3864
+ height: laneHeight,
3865
+ parentId: pool.id,
3866
+ extent: "parent",
3867
+ zIndex: getBpmnNodeZIndex("Lane"),
3868
+ data: { ...lane.data, orientation: "horizontal", laneIndex: index }
3869
+ }));
3870
+ }
3871
+ function resizeVerticalBpmnLanes(lanes, pool) {
3872
+ const poolSize = getBpmnNodeSize(pool);
3873
+ const laneWidth = Math.max(BPMN_POOL_LANE_LAYOUT.minLaneSize, poolSize.width / Math.max(1, lanes.length));
3874
+ const laneHeight = Math.max(BPMN_POOL_LANE_LAYOUT.minLaneSize, poolSize.height - BPMN_POOL_LANE_LAYOUT.verticalPoolHeaderSize);
3875
+ return lanes.map((lane, index) => ({
3876
+ ...lane,
3877
+ position: { x: index * laneWidth, y: BPMN_POOL_LANE_LAYOUT.verticalPoolHeaderSize },
3878
+ width: laneWidth,
3879
+ height: laneHeight,
3880
+ parentId: pool.id,
3881
+ extent: "parent",
3882
+ zIndex: getBpmnNodeZIndex("Lane"),
3883
+ data: { ...lane.data, orientation: "vertical", laneIndex: index }
3884
+ }));
3885
+ }
3886
+ function layoutBpmnPoolLaneNodes(nodes, poolId) {
3887
+ const pool = nodes.find((node) => node.id === poolId && node.data.elementType === "Pool");
3888
+ if (!pool) return nodes;
3889
+ const orientation = pool.data.orientation === "vertical" ? "vertical" : "horizontal";
3890
+ const lanes = nodes.filter((node) => node.parentId === pool.id && node.data.elementType === "Lane").sort((a, b) => {
3891
+ const aIndex = typeof a.data.laneIndex === "number" ? a.data.laneIndex : void 0;
3892
+ const bIndex = typeof b.data.laneIndex === "number" ? b.data.laneIndex : void 0;
3893
+ if (aIndex !== void 0 || bIndex !== void 0) return (aIndex ?? 0) - (bIndex ?? 0);
3894
+ return getBpmnLaneOrderPosition(a, orientation) - getBpmnLaneOrderPosition(b, orientation);
3895
+ });
3896
+ if (lanes.length === 0) return withBpmnNodeZIndexes(nodes);
3897
+ const laneMap = new Map(
3898
+ (orientation === "vertical" ? resizeVerticalBpmnLanes(lanes, pool) : resizeHorizontalBpmnLanes(lanes, pool)).map((lane) => [lane.id, lane])
3899
+ );
3900
+ return withBpmnNodeZIndexes(nodes.map((node) => laneMap.get(node.id) ?? node));
3901
+ }
3902
+ function reorderBpmnLaneAfterDrop(nodes, laneId) {
3903
+ const lane = nodes.find((node) => node.id === laneId && node.data.elementType === "Lane");
3904
+ if (!lane?.parentId) return nodes;
3905
+ const pool = nodes.find((node) => node.id === lane.parentId && node.data.elementType === "Pool");
3906
+ if (!pool) return nodes;
3907
+ const orientation = pool.data.orientation === "vertical" ? "vertical" : "horizontal";
3908
+ const lanes = nodes.filter((node) => node.parentId === pool.id && node.data.elementType === "Lane").sort((a, b) => getBpmnLaneOrderPosition(a, orientation) - getBpmnLaneOrderPosition(b, orientation));
3909
+ const moved = lanes.find((candidate) => candidate.id === lane.id);
3910
+ if (!moved) return layoutBpmnPoolLaneNodes(nodes, pool.id);
3911
+ const withoutMoved = lanes.filter((candidate) => candidate.id !== lane.id);
3912
+ const movedCenter = getBpmnLaneOrderPosition(lane, orientation);
3913
+ const insertIndex = withoutMoved.findIndex(
3914
+ (candidate) => movedCenter < getBpmnLaneOrderPosition(candidate, orientation)
3915
+ );
3916
+ const ordered = [...withoutMoved];
3917
+ ordered.splice(insertIndex === -1 ? ordered.length : insertIndex, 0, moved);
3918
+ const orderedMap = new Map(ordered.map((candidate, index) => [candidate.id, index]));
3919
+ const sortedByDrop = nodes.map((node) => {
3920
+ if (node.parentId !== pool.id || node.data.elementType !== "Lane") return node;
3921
+ return { ...node, data: { ...node.data, laneIndex: orderedMap.get(node.id) ?? 0 } };
3922
+ });
3923
+ return layoutBpmnPoolLaneNodes(sortedByDrop, pool.id);
3924
+ }
3925
+ function getBpmnNodeSize(node) {
3926
+ return diagramsCore.getNodeSize(node, getBpmnElementSize(node.data.elementType));
3927
+ }
3928
+ function getBpmnNodeAbsolutePosition(state, nodeOrId) {
3929
+ const nodeId = typeof nodeOrId === "string" ? nodeOrId : nodeOrId.id;
3930
+ return diagramsCore.getNodeAbsolutePosition(state, nodeId);
3931
+ }
3932
+ function toBpmnRelativePosition(state, absolutePosition, parentOrId) {
3933
+ return diagramsCore.toRelativeNodePosition(state, absolutePosition, parentOrId);
3934
+ }
3935
+ var BPMN_CONTAINER_PRIORITY = [
3936
+ "Lane",
3937
+ "SubProcess",
3938
+ "EventSubProcess",
3939
+ "Transaction",
3940
+ "AdHocSubProcess",
3941
+ "Pool"
3942
+ ];
3943
+ function findBpmnContainerAt(state, options) {
3944
+ return diagramsCore.findContainingNode(state, {
3945
+ point: options.position,
3946
+ excludeId: options.excludeId,
3947
+ fallbackSize: (node) => getBpmnElementSize(node.data.elementType),
3948
+ predicate: (node) => BPMN_CONTAINER_PRIORITY.includes(node.data.elementType),
3949
+ sort: (a, b) => {
3950
+ const aPriority = BPMN_CONTAINER_PRIORITY.indexOf(a.data.elementType);
3951
+ const bPriority = BPMN_CONTAINER_PRIORITY.indexOf(b.data.elementType);
3952
+ if (aPriority !== bPriority) return aPriority - bPriority;
3953
+ const aSize = getBpmnNodeSize(a);
3954
+ const bSize = getBpmnNodeSize(b);
3955
+ return aSize.width * aSize.height - bSize.width * bSize.height;
3956
+ }
3957
+ });
3958
+ }
3959
+ function getBpmnNodeCenter(node, absolutePosition) {
3960
+ return diagramsCore.getNodeCenterPosition(absolutePosition, getBpmnNodeSize(node));
3961
+ }
3832
3962
  function resolvePoolLaneDirection(pool) {
3833
3963
  return pool.data.orientation === "vertical" ? "horizontal" : "vertical";
3834
3964
  }
@@ -4091,6 +4221,19 @@ function reparentBpmnNodeCommand(options) {
4091
4221
  }
4092
4222
  };
4093
4223
  }
4224
+ function reparentBpmnNodeAtPosition(state, options) {
4225
+ const node = diagramsCore.getNode(state, options.id);
4226
+ if (!node) throw new Error(`Element "${options.id}" does not exist.`);
4227
+ const hitPoint = getBpmnNodeCenter(node, options.position);
4228
+ const parent = isBpmnProcessNode(node.data.elementType) ? findBpmnContainerAt(state, { position: hitPoint, excludeId: node.id }) : void 0;
4229
+ const containment = canContainBpmnElement(parent?.data.elementType, node.data.elementType);
4230
+ if (containment !== true) throw new Error(containment);
4231
+ const nextPosition = parent ? toBpmnRelativePosition(state, options.position, parent) : options.position;
4232
+ return diagramsCore.reparentNode(state, node.id, {
4233
+ ...parent ? { parentId: parent.id } : { parentId: void 0 },
4234
+ position: nextPosition
4235
+ });
4236
+ }
4094
4237
  function resizeBpmnNodeCommand(options) {
4095
4238
  return {
4096
4239
  id: `bpmn.resize.${options.id}`,
@@ -4478,10 +4621,15 @@ exports.createBpmnNodeCommand = createBpmnNodeCommand;
4478
4621
  exports.createSimulation = createSimulation;
4479
4622
  exports.deleteBpmnElementsCommand = deleteBpmnElementsCommand;
4480
4623
  exports.deserializeBpmnDiagram = deserializeBpmnDiagram;
4624
+ exports.findBpmnContainerAt = findBpmnContainerAt;
4481
4625
  exports.fire = fire;
4482
4626
  exports.getBpmnDragHandleSelector = getBpmnDragHandleSelector;
4483
4627
  exports.getBpmnElementSize = getBpmnElementSize;
4484
4628
  exports.getBpmnLaneIndexAtPosition = getBpmnLaneIndexAtPosition;
4629
+ exports.getBpmnNodeAbsolutePosition = getBpmnNodeAbsolutePosition;
4630
+ exports.getBpmnNodeCenter = getBpmnNodeCenter;
4631
+ exports.getBpmnNodeSize = getBpmnNodeSize;
4632
+ exports.getBpmnNodeZIndex = getBpmnNodeZIndex;
4485
4633
  exports.getBpmnPoolLanes = getBpmnPoolLanes;
4486
4634
  exports.getElementMeta = getElementMeta;
4487
4635
  exports.getFireable = getFireable;
@@ -4499,13 +4647,16 @@ exports.isDataType = isDataType;
4499
4647
  exports.isEventType = isEventType;
4500
4648
  exports.isGatewayType = isGatewayType;
4501
4649
  exports.isTaskType = isTaskType;
4650
+ exports.layoutBpmnPoolLaneNodes = layoutBpmnPoolLaneNodes;
4502
4651
  exports.layoutBpmnPoolLanes = layoutBpmnPoolLanes;
4503
4652
  exports.moveBpmnLaneCommand = moveBpmnLaneCommand;
4504
4653
  exports.parseBpmnDiagramDocument = parseBpmnDiagramDocument;
4505
4654
  exports.parseBpmnXml = parseBpmnXml;
4506
4655
  exports.pasteBpmnElementsCommand = pasteBpmnElementsCommand;
4507
4656
  exports.reorderBpmnLane = reorderBpmnLane;
4657
+ exports.reorderBpmnLaneAfterDrop = reorderBpmnLaneAfterDrop;
4508
4658
  exports.reorderBpmnLaneCommand = reorderBpmnLaneCommand;
4659
+ exports.reparentBpmnNodeAtPosition = reparentBpmnNodeAtPosition;
4509
4660
  exports.reparentBpmnNodeCommand = reparentBpmnNodeCommand;
4510
4661
  exports.replaceBpmnNodeCommand = replaceBpmnNodeCommand;
4511
4662
  exports.resizeBpmnNodeCommand = resizeBpmnNodeCommand;
@@ -4519,7 +4670,9 @@ exports.setVariable = setVariable;
4519
4670
  exports.supportsCollapse = supportsCollapse;
4520
4671
  exports.supportsMarkers = supportsMarkers;
4521
4672
  exports.tick = tick;
4673
+ exports.toBpmnRelativePosition = toBpmnRelativePosition;
4522
4674
  exports.validateBpmnConnectionForEdgeType = validateBpmnConnectionForEdgeType;
4523
4675
  exports.validateBpmnDiagram = validateBpmnDiagram;
4676
+ exports.withBpmnNodeZIndexes = withBpmnNodeZIndexes;
4524
4677
  //# sourceMappingURL=index.cjs.map
4525
4678
  //# sourceMappingURL=index.cjs.map