index.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. import inherits from 'inherits-browser';
  2. import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
  3. import { pointsAligned } from 'diagram-js/lib/util/Geometry';
  4. import { assign } from 'min-dash';
  5. import { DEFAULT_CONNECT } from '../../config/constants';
  6. import { NodeUtils } from '../../utils/nodeUtil';
  7. /**
  8. * @typedef {import('diagram-js/lib/core/EventBus').default} EventBus
  9. * @typedef {import('diagram-js/lib/features/grid-snapping/GridSnapping').default} GridSnapping
  10. * @typedef {import('../../modeling/Modeling').default} Modeling
  11. *
  12. * @typedef {import('diagram-js/lib/util/Types').Point} Point
  13. */
  14. const HIGH_PRIORITY = 3000;
  15. export default function GridSnappingLayoutConnectionBehavior(this: any, eventBus: any, gridSnapping: any, modeling: any, jnpfData: any, injector: any) {
  16. CommandInterceptor.call(this, eventBus);
  17. this._gridSnapping = gridSnapping;
  18. this._injector = injector;
  19. const self = this;
  20. this.postExecuted(['connection.create', 'connection.layout'], HIGH_PRIORITY, function (event: any) {
  21. let context = event.context,
  22. connection = context.connection,
  23. waypoints = connection.waypoints;
  24. let target = connection.target,
  25. source = connection.source;
  26. // 横向美化
  27. if (jnpfData.data?.layout?.value === 'horizontal') {
  28. let x = source.x;
  29. let y = source.y + source.height / 2;
  30. let targetX = target.x + target.width / 2;
  31. if (source.x + source.width <= target.x) {
  32. // 右侧
  33. x = source.x + source.width;
  34. if (y === target.y + target.height / 2) {
  35. waypoints = [
  36. { x: x, y: y },
  37. { x: target.x, y: target.y + target.height / 2 },
  38. ];
  39. } else if (y >= target.y + target.height && x + 30 > target.x) {
  40. waypoints = [
  41. { x: x, y: y },
  42. { x: targetX, y: y },
  43. { x: targetX, y: target.y + target.height },
  44. ];
  45. } else if (y - 10 < target.y && x + 30 > target.x) {
  46. waypoints = [
  47. { x: x, y: y },
  48. { x: targetX, y: y },
  49. { x: targetX, y: target.y },
  50. ];
  51. } else {
  52. if (target.x - source.x - source.width - DEFAULT_CONNECT / 2 > 0) {
  53. waypoints = [
  54. { x: x, y: y },
  55. { x: target.x - DEFAULT_CONNECT / 2, y: y },
  56. { x: target.x - DEFAULT_CONNECT / 2, y: target.y + target.height / 2 },
  57. { x: target.x, y: target.y + target.height / 2 },
  58. ];
  59. } else {
  60. waypoints = [
  61. { x: x, y: y },
  62. { x: source.x + source.width + (target.x - source.x - source.width) / 2, y: y },
  63. { x: source.x + source.width + (target.x - source.x - source.width) / 2, y: target.y + target.height / 2 },
  64. { x: target.x, y: target.y + target.height / 2 },
  65. ];
  66. }
  67. }
  68. } else if (source.x > target.x + target.width) {
  69. // 左侧
  70. x = target.x + target.width;
  71. if (x + 30 > source.x) {
  72. if (y - 10 > target.y + target.height) {
  73. waypoints = [
  74. { x: source.x, y: source.y + source.height / 2 },
  75. { x: target.x + target.width / 2, y: source.y + source.height / 2 },
  76. { x: target.x + target.width / 2, y: target.y + target.height },
  77. ];
  78. } else if (y - 10 < target.y + target.height) {
  79. waypoints = [
  80. { x: source.x, y: source.y + source.height / 2 },
  81. { x: target.x + target.width / 2, y: source.y + source.height / 2 },
  82. { x: target.x + target.width / 2, y: target.y },
  83. ];
  84. }
  85. } else {
  86. waypoints = [
  87. { x: source.x, y: source.y + source.height / 2 },
  88. { x: (source.x - x) / 2 + x, y: source.y + source.height / 2 },
  89. { x: (source.x - x) / 2 + x, y: target.y + target.height / 2 },
  90. { x: x, y: target.y + target.height / 2 },
  91. ];
  92. }
  93. } else {
  94. // 上侧
  95. if (target.y < source.y) {
  96. if (source.x + source.width / 2 === target.x + target.width / 2) {
  97. waypoints = [
  98. { x: source.x + source.width / 2, y: source.y },
  99. { x: target.x + target.width / 2, y: target.y + target.height },
  100. ];
  101. } else if (target.y + target.height > source.y - 20) {
  102. if (source.x + source.width / 2 >= target.x + target.width) {
  103. // 左上
  104. waypoints = [
  105. { x: source.x + source.width / 2, y: source.y },
  106. { x: source.x + source.width / 2, y: target.y + target.height / 2 },
  107. { x: target.x + target.width, y: target.y + target.height / 2 },
  108. ];
  109. } else if (source.x + source.width / 2 <= target.x) {
  110. // 右上
  111. waypoints = [
  112. { x: source.x + source.width / 2, y: source.y },
  113. { x: source.x + source.width / 2, y: target.y + target.height / 2 },
  114. { x: target.x, y: target.y + target.height / 2 },
  115. ];
  116. } else {
  117. waypoints = [
  118. { x: source.x + source.width / 2, y: source.y },
  119. { x: source.x + source.width / 2, y: source.y - (source.y - target.y - target.height) / 2 },
  120. { x: target.x + target.width / 2, y: target.y + target.height },
  121. ];
  122. }
  123. } else {
  124. waypoints = [
  125. { x: source.x + source.width / 2, y: source.y },
  126. { x: source.x + source.width / 2, y: source.y - (source.y - target.y - target.height) / 2 },
  127. { x: target.x + target.width / 2, y: source.y - (source.y - target.y - target.height) / 2 },
  128. { x: target.x + target.width / 2, y: target.y + target.height },
  129. ];
  130. }
  131. } else if (target.y + target.height > source.y + source.height) {
  132. // 下侧
  133. if (source.x + source.width / 2 === target.x + target.width / 2) {
  134. waypoints = [
  135. { x: source.x + source.width / 2, y: source.y + source.height },
  136. { x: target.x + target.width / 2, y: target.y },
  137. ];
  138. } else if (target.y - 20 < source.y + source.height) {
  139. if (source.x + source.width / 2 >= target.x + target.width) {
  140. // 左下
  141. waypoints = [
  142. { x: source.x + source.width / 2, y: source.y + source.height },
  143. { x: source.x + source.width / 2, y: target.y + target.height / 2 },
  144. { x: target.x + target.width, y: target.y + target.height / 2 },
  145. ];
  146. } else if (source.x + source.width / 2 <= target.x) {
  147. // 右下
  148. waypoints = [
  149. { x: source.x + source.width / 2, y: source.y + source.height },
  150. { x: source.x + source.width / 2, y: target.y + target.height / 2 },
  151. { x: target.x, y: target.y + target.height / 2 },
  152. ];
  153. } else {
  154. waypoints = [
  155. { x: source.x + source.width / 2, y: source.y + source.height },
  156. { x: source.x + source.width / 2, y: target.y - (target.y - source.y - source.height) / 2 },
  157. { x: target.x + target.width / 2, y: target.y - (target.y - source.y - source.height) / 2 },
  158. { x: target.x + target.width / 2, y: target.y },
  159. ];
  160. }
  161. } else {
  162. waypoints = [
  163. { x: source.x + source.width / 2, y: source.y + source.height },
  164. { x: source.x + source.width / 2, y: target.y - (target.y - source.y - source.height) / 2 },
  165. { x: target.x + target.width / 2, y: target.y - (target.y - source.y - source.height) / 2 },
  166. { x: target.x + target.width / 2, y: target.y },
  167. ];
  168. }
  169. }
  170. }
  171. }
  172. if (jnpfData.data?.layout?.value === 'vertical') {
  173. if (source.y + source.height < target.y) {
  174. if (source.x + source.width / 2 === target.x + target.width / 2) {
  175. waypoints = [
  176. { x: source.x + source.width / 2, y: source.y + source.height },
  177. { x: target.x + target.width / 2, y: target.y },
  178. ];
  179. } else {
  180. if (source.y + source.height + 20 > target.y && source.x + source.width / 2 < target.x) {
  181. waypoints = [
  182. { x: source.x + source.width / 2, y: source.y + source.height },
  183. { x: source.x + source.width / 2, y: target.y + target.height / 2 },
  184. { x: target.x, y: target.y + target.height / 2 },
  185. ];
  186. } else if (source.y + source.height + 20 > target.y && source.x + source.width / 2 > target.x + target.width + 10) {
  187. waypoints = [
  188. { x: source.x + source.width / 2, y: source.y + source.height },
  189. { x: source.x + source.width / 2, y: target.y + target.height / 2 },
  190. { x: target.x + target.width, y: target.y + target.height / 2 },
  191. ];
  192. } else
  193. waypoints = [
  194. { x: source.x + source.width / 2, y: source.y + source.height },
  195. { x: source.x + source.width / 2, y: source.y + source.height + (target.y - source.y - source.height) / 2 },
  196. { x: target.x + target.width / 2, y: source.y + source.height + (target.y - source.y - source.height) / 2 },
  197. { x: target.x + target.width / 2, y: target.y },
  198. ];
  199. }
  200. } else if (source.y > target.y + target.height) {
  201. if (source.x + source.width / 2 === target.x + target.width / 2) {
  202. waypoints = [
  203. { x: source.x + source.width / 2, y: source.y },
  204. { x: target.x + target.width / 2, y: target.y + target.height },
  205. ];
  206. } else {
  207. if (source.y - 20 < target.y + target.height && source.x + source.width / 2 < target.x) {
  208. waypoints = [
  209. { x: source.x + source.width / 2, y: source.y },
  210. { x: source.x + source.width / 2, y: target.y + target.height / 2 },
  211. { x: target.x, y: target.y + target.height / 2 },
  212. ];
  213. } else if (source.y - 20 < target.y + target.height && source.x + source.width / 2 > target.x + target.width + 10) {
  214. waypoints = [
  215. { x: source.x + source.width / 2, y: source.y },
  216. { x: source.x + source.width / 2, y: target.y + target.height / 2 },
  217. { x: target.x + target.width, y: target.y + target.height / 2 },
  218. ];
  219. } else
  220. waypoints = [
  221. { x: source.x + source.width / 2, y: source.y },
  222. { x: source.x + source.width / 2, y: source.y - (source.y - target.y - target.height) / 2 },
  223. { x: target.x + target.width / 2, y: source.y - (source.y - target.y - target.height) / 2 },
  224. { x: target.x + target.width / 2, y: target.y + target.height },
  225. ];
  226. }
  227. } else if (source.x + source.width / 2 < target.x - 10) {
  228. // 右侧
  229. if (source.x + source.width >= target.x - 10 && source.y + source.height / 2 >= target.y + target.height - 10) {
  230. waypoints = [
  231. { x: source.x + source.width, y: source.y + source.height / 2 },
  232. { x: target.x + target.width / 2, y: source.y + source.height / 2 },
  233. { x: target.x + target.width / 2, y: target.y + target.height },
  234. ];
  235. } else if (source.x + source.width >= target.x - 10 && source.y + source.height / 2 + 10 <= target.y) {
  236. waypoints = [
  237. { x: source.x + source.width, y: source.y + source.height / 2 },
  238. { x: target.x + target.width / 2, y: source.y + source.height / 2 },
  239. { x: target.x + target.width / 2, y: target.y },
  240. ];
  241. } else {
  242. waypoints = [
  243. { x: source.x + source.width, y: source.y + source.height / 2 },
  244. { x: source.x + source.width + (target.x - source.x - source.width) / 2, y: source.y + source.height / 2 },
  245. { x: source.x + source.width + (target.x - source.x - source.width) / 2, y: target.y + target.height / 2 },
  246. { x: target.x, y: target.y + target.height / 2 },
  247. ];
  248. }
  249. } else if (source.x + source.width / 2 > target.x + target.width - 10) {
  250. // 左侧
  251. if (source.x < target.x + target.width + 20 && source.y + source.height / 2 >= target.y + target.height - 10) {
  252. waypoints = [
  253. { x: source.x, y: source.y + source.height / 2 },
  254. { x: target.x + target.width / 2, y: source.y + source.height / 2 },
  255. { x: target.x + target.width / 2, y: target.y + target.height },
  256. ];
  257. } else if (source.x < target.x + target.width + 20 && source.y + source.height / 2 + 10 <= target.y) {
  258. waypoints = [
  259. { x: source.x, y: source.y + source.height / 2 },
  260. { x: target.x + target.width / 2, y: source.y + source.height / 2 },
  261. { x: target.x + target.width / 2, y: target.y },
  262. ];
  263. } else
  264. waypoints = [
  265. { x: source.x, y: source.y + source.height / 2 },
  266. { x: target.x + target.width + (source.x - target.x - target.width) / 2, y: source.y + source.height / 2 },
  267. { x: target.x + target.width + (source.x - target.x - target.width) / 2, y: target.y + target.height / 2 },
  268. { x: target.x + target.width, y: target.y + target.height / 2 },
  269. ];
  270. }
  271. }
  272. // 判断是横向还是纵向 如果是纵向 需要重新修改坐标
  273. modeling.updateWaypoints(connection, self.snapMiddleSegments(waypoints));
  274. let elementRegistry = injector.get('elementRegistry');
  275. let labelCenter = NodeUtils.updateLabelWaypoints(connection, elementRegistry, jnpfData);
  276. let connectLabel = connection.label;
  277. if (connectLabel) {
  278. let label = elementRegistry.get(connectLabel.id);
  279. label.x = labelCenter.x;
  280. label.y = labelCenter.y;
  281. modeling.updateProperties(label, {});
  282. }
  283. });
  284. }
  285. GridSnappingLayoutConnectionBehavior.$inject = ['eventBus', 'gridSnapping', 'modeling', 'jnpfData', 'injector'];
  286. inherits(GridSnappingLayoutConnectionBehavior, CommandInterceptor);
  287. /**
  288. * Snap middle segments of a given connection.
  289. *
  290. * @param {Point[]} waypoints
  291. *
  292. * @return {Point[]}
  293. */
  294. GridSnappingLayoutConnectionBehavior.prototype.snapMiddleSegments = function (this: any, waypoints: any[]) {
  295. const gridSnapping = this._gridSnapping;
  296. waypoints = waypoints.slice();
  297. for (let i = 1; i < waypoints.length - 2; i++) {
  298. const snapped = snapSegment(gridSnapping, waypoints[i], waypoints[i + 1]);
  299. waypoints[i] = snapped[0];
  300. waypoints[i + 1] = snapped[1];
  301. }
  302. return waypoints;
  303. };
  304. /**
  305. * Check whether a connection has middle segments.
  306. *
  307. * @param {Point[]} waypoints
  308. *
  309. * @return {boolean}
  310. */
  311. function hasMiddleSegments(waypoints: any[]) {
  312. return waypoints.length > 3;
  313. }
  314. /**
  315. * Check whether an alignment is horizontal.
  316. *
  317. * @param {string} aligned
  318. *
  319. * @return {boolean}
  320. */
  321. function horizontallyAligned(aligned: string) {
  322. return aligned === 'h';
  323. }
  324. /**
  325. * Check whether an alignment is vertical.
  326. *
  327. * @param {string} aligned
  328. *
  329. * @return {boolean}
  330. */
  331. function verticallyAligned(aligned: string) {
  332. return aligned === 'v';
  333. }
  334. /**
  335. * Get middle segments from a given connection.
  336. *
  337. * @param {Point[]} waypoints
  338. *
  339. * @return {Point[]}
  340. */
  341. function snapSegment(gridSnapping: any, segmentStart: any, segmentEnd: any) {
  342. const aligned: any = pointsAligned(segmentStart, segmentEnd);
  343. const snapped: any = {};
  344. if (horizontallyAligned(aligned)) {
  345. snapped.y = gridSnapping.snapValue(segmentStart.y);
  346. }
  347. if (verticallyAligned(aligned)) {
  348. snapped.x = gridSnapping.snapValue(segmentStart.x);
  349. }
  350. if ('x' in snapped || 'y' in snapped) {
  351. segmentStart = assign({}, segmentStart, snapped);
  352. segmentEnd = assign({}, segmentEnd, snapped);
  353. }
  354. return [segmentStart, segmentEnd];
  355. }