index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. var __assign = (this && this.__assign) || function () {
  2. __assign = Object.assign || function(t) {
  3. for (var s, i = 1, n = arguments.length; i < n; i++) {
  4. s = arguments[i];
  5. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  6. t[p] = s[p];
  7. }
  8. return t;
  9. };
  10. return __assign.apply(this, arguments);
  11. };
  12. var __read = (this && this.__read) || function (o, n) {
  13. var m = typeof Symbol === "function" && o[Symbol.iterator];
  14. if (!m) return o;
  15. var i = m.call(o), r, ar = [], e;
  16. try {
  17. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  18. }
  19. catch (error) { e = { error: error }; }
  20. finally {
  21. try {
  22. if (r && !r.done && (m = i["return"])) m.call(i);
  23. }
  24. finally { if (e) throw e.error; }
  25. }
  26. return ar;
  27. };
  28. var TurboType = {
  29. SEQUENCE_FLOW: 1,
  30. START_EVENT: 2,
  31. END_EVENT: 3,
  32. USER_TASK: 4,
  33. // SERVICE_TASK = 5, 暂不支持
  34. EXCLUSIVE_GATEWAY: 6,
  35. };
  36. var TurboTypeMap = {
  37. 1: 'bpmn:sequenceFlow',
  38. 2: 'bpmn:startEvent',
  39. 3: 'bpmn:endEvent',
  40. 4: 'bpmn:userTask',
  41. 6: 'bpmn:exclusiveGateway',
  42. };
  43. // 转换Turbo识别的类型
  44. function getTurboType(type) {
  45. switch (type) {
  46. case 'bpmn:sequenceFlow':
  47. return TurboType.SEQUENCE_FLOW;
  48. case 'bpmn:startEvent':
  49. return TurboType.START_EVENT;
  50. case 'bpmn:endEvent':
  51. return TurboType.END_EVENT;
  52. case 'bpmn:userTask':
  53. return TurboType.USER_TASK;
  54. // case 'bpmn:serviceTask':
  55. // return TurboType.SERVICE_TASK;
  56. case 'bpmn:exclusiveGateway':
  57. return TurboType.EXCLUSIVE_GATEWAY;
  58. default:
  59. return type;
  60. }
  61. }
  62. // 将LogicFlow中的Node数据转换为Turbo元素数据
  63. function convertNodeToTurboElement(node) {
  64. var id = node.id, type = node.type, x = node.x, y = node.y, _a = node.text, text = _a === void 0 ? '' : _a, properties = node.properties;
  65. return {
  66. incoming: [],
  67. outgoing: [],
  68. dockers: [],
  69. type: getTurboType(node.type),
  70. properties: __assign(__assign({}, properties), { name: (text && text.value) || '', x: x,
  71. y: y,
  72. text: text }),
  73. key: id,
  74. };
  75. }
  76. // 将LogicFlow中的Edge数据转换为Turbo元素数据
  77. function convertEdgeToTurboElement(edge) {
  78. var id = edge.id, type = edge.type, sourceNodeId = edge.sourceNodeId, targetNodeId = edge.targetNodeId, startPoint = edge.startPoint, endPoint = edge.endPoint, pointsList = edge.pointsList, _a = edge.text, text = _a === void 0 ? '' : _a, properties = edge.properties;
  79. return {
  80. incoming: [sourceNodeId],
  81. outgoing: [targetNodeId],
  82. type: getTurboType(type),
  83. dockers: [],
  84. properties: __assign(__assign({}, properties), { name: (text && text.value) || '', text: text, startPoint: JSON.stringify(startPoint), endPoint: JSON.stringify(endPoint), pointsList: JSON.stringify(pointsList) }),
  85. key: id,
  86. };
  87. }
  88. // 将LogicFlow中数据转换为Turbo数据
  89. export function toTurboData(data) {
  90. var nodeMap = new Map();
  91. var turboData = {
  92. flowElementList: [],
  93. };
  94. data.nodes.forEach(function (node) {
  95. var flowElement = convertNodeToTurboElement(node);
  96. turboData.flowElementList.push(flowElement);
  97. nodeMap.set(node.id, flowElement);
  98. });
  99. data.edges.forEach(function (edge) {
  100. var flowElement = convertEdgeToTurboElement(edge);
  101. var sourceElement = nodeMap.get(edge.sourceNodeId);
  102. sourceElement.outgoing.push(flowElement.key);
  103. var targetElement = nodeMap.get(edge.targetNodeId);
  104. targetElement.incoming.push(flowElement.key);
  105. turboData.flowElementList.push(flowElement);
  106. });
  107. return turboData;
  108. }
  109. // 将Turbo元素数据转换为LogicFlow中的Edge数据
  110. function convertFlowElementToEdge(element) {
  111. var incoming = element.incoming, outgoing = element.outgoing, properties = element.properties, key = element.key, type = element.type;
  112. var text = properties.text, name = properties.name, startPoint = properties.startPoint, endPoint = properties.endPoint, pointsList = properties.pointsList;
  113. var edge = {
  114. id: key,
  115. type: TurboTypeMap[type],
  116. sourceNodeId: incoming[0],
  117. targetNodeId: outgoing[0],
  118. text: text || name,
  119. properties: {},
  120. };
  121. if (startPoint) {
  122. edge.startPoint = JSON.parse(startPoint);
  123. }
  124. if (endPoint) {
  125. edge.endPoint = JSON.parse(endPoint);
  126. }
  127. if (pointsList) {
  128. edge.pointsList = JSON.parse(pointsList);
  129. }
  130. // 这种转换方式,在自定义属性中不能与excludeProperties中的属性重名,否则将在转换过程中丢失
  131. var excludeProperties = ['startPoint', 'endPoint', 'pointsList', 'text'];
  132. Object.keys(element.properties).forEach(function (property) {
  133. if (excludeProperties.indexOf(property) === -1) {
  134. edge.properties[property] = element.properties[property];
  135. }
  136. });
  137. return edge;
  138. }
  139. // 将Turbo元素数据转换为LogicFlow中的Node数据
  140. function convertFlowElementToNode(element) {
  141. var properties = element.properties, key = element.key, type = element.type, bounds = element.bounds;
  142. var x = properties.x, y = properties.y;
  143. var text = properties.text;
  144. if (x === undefined) {
  145. var _a = __read(bounds, 2), _b = _a[0], x1 = _b.x, y1 = _b.y, _c = _a[1], x2 = _c.x, y2 = _c.y;
  146. x = (x1 + x2) / 2;
  147. y = (y1 + y2) / 2;
  148. }
  149. var node = {
  150. id: key,
  151. type: TurboTypeMap[type],
  152. x: x,
  153. y: y,
  154. text: text,
  155. properties: {},
  156. };
  157. // 这种转换方式,在自定义属性中不能与excludeProperties中的属性重名,否则将在转换过程中丢失
  158. var excludeProperties = ['x', 'y', 'text'];
  159. Object.keys(element.properties).forEach(function (property) {
  160. if (excludeProperties.indexOf(property) === -1) {
  161. node.properties[property] = element.properties[property];
  162. }
  163. });
  164. return node;
  165. }
  166. // 将Turbo元素数据转换为LogicFlow数据
  167. export function toLogicflowData(data) {
  168. var lfData = {
  169. nodes: [],
  170. edges: [],
  171. };
  172. var list = data.flowElementList;
  173. list && list.length > 0 && list.forEach(function (element) {
  174. if (element.type === TurboType.SEQUENCE_FLOW) {
  175. var edge = convertFlowElementToEdge(element);
  176. lfData.edges.push(edge);
  177. }
  178. else {
  179. var node = convertFlowElementToNode(element);
  180. lfData.nodes.push(node);
  181. }
  182. });
  183. return lfData;
  184. }
  185. var TurboAdapter = /** @class */ (function () {
  186. function TurboAdapter(_a) {
  187. var lf = _a.lf;
  188. lf.adapterIn = this.adapterIn;
  189. lf.adapterOut = this.adapterOut;
  190. }
  191. TurboAdapter.prototype.adapterOut = function (logicflowData) {
  192. if (logicflowData) {
  193. return toTurboData(logicflowData);
  194. }
  195. };
  196. TurboAdapter.prototype.adapterIn = function (turboData) {
  197. if (turboData) {
  198. return toLogicflowData(turboData);
  199. }
  200. };
  201. TurboAdapter.pluginName = 'turboAdapter';
  202. return TurboAdapter;
  203. }());
  204. export default TurboAdapter;