index.js 7.3 KB

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