boundary.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.boundary = void 0;
  4. const x6_common_1 = require("@antv/x6-common");
  5. const x6_geometry_1 = require("@antv/x6-geometry");
  6. const util_1 = require("./util");
  7. const util_2 = require("../../util");
  8. /**
  9. * Places the connection point at the intersection between the
  10. * edge path end segment and the actual shape of the target magnet.
  11. */
  12. const boundary = function (line, view, magnet, options) {
  13. let node;
  14. let intersection;
  15. const anchor = line.end;
  16. const selector = options.selector;
  17. if (typeof selector === 'string') {
  18. node = view.findOne(selector);
  19. }
  20. else if (Array.isArray(selector)) {
  21. node = x6_common_1.ObjectExt.getByPath(magnet, selector);
  22. }
  23. else {
  24. node = (0, util_1.findShapeNode)(magnet);
  25. }
  26. if (!x6_common_1.Dom.isSVGGraphicsElement(node)) {
  27. if (node === magnet || !x6_common_1.Dom.isSVGGraphicsElement(magnet)) {
  28. return anchor;
  29. }
  30. node = magnet;
  31. }
  32. const localShape = view.getShapeOfElement(node);
  33. const magnetMatrix = view.getMatrixOfElement(node);
  34. const translateMatrix = view.getRootTranslatedMatrix();
  35. const rotateMatrix = view.getRootRotatedMatrix();
  36. const targetMatrix = translateMatrix
  37. .multiply(rotateMatrix)
  38. .multiply(magnetMatrix);
  39. const localMatrix = targetMatrix.inverse();
  40. const localLine = util_2.Util.transformLine(line, localMatrix);
  41. const localRef = localLine.start.clone();
  42. const data = view.getDataOfElement(node);
  43. if (options.insideout === false) {
  44. if (data.shapeBBox == null) {
  45. data.shapeBBox = localShape.bbox();
  46. }
  47. const localBBox = data.shapeBBox;
  48. if (localBBox != null && localBBox.containsPoint(localRef)) {
  49. return anchor;
  50. }
  51. }
  52. if (options.extrapolate === true) {
  53. localLine.setLength(1e6);
  54. }
  55. // Caching segment subdivisions for paths
  56. let pathOptions;
  57. if (x6_geometry_1.Path.isPath(localShape)) {
  58. const precision = options.precision || 2;
  59. if (data.segmentSubdivisions == null) {
  60. data.segmentSubdivisions = localShape.getSegmentSubdivisions({
  61. precision,
  62. });
  63. }
  64. pathOptions = {
  65. precision,
  66. segmentSubdivisions: data.segmentSubdivisions,
  67. };
  68. intersection = localLine.intersect(localShape, pathOptions);
  69. }
  70. else {
  71. intersection = localLine.intersect(localShape);
  72. }
  73. if (intersection) {
  74. if (Array.isArray(intersection)) {
  75. intersection = localRef.closest(intersection);
  76. }
  77. }
  78. else if (options.sticky === true) {
  79. // No intersection, find the closest point instead
  80. if (x6_geometry_1.Rectangle.isRectangle(localShape)) {
  81. intersection = localShape.getNearestPointToPoint(localRef);
  82. }
  83. else if (x6_geometry_1.Ellipse.isEllipse(localShape)) {
  84. intersection = localShape.intersectsWithLineFromCenterToPoint(localRef);
  85. }
  86. else {
  87. intersection = localShape.closestPoint(localRef, pathOptions);
  88. }
  89. }
  90. const cp = intersection
  91. ? util_2.Util.transformPoint(intersection, targetMatrix)
  92. : anchor;
  93. let cpOffset = options.offset || 0;
  94. if (options.stroked !== false) {
  95. if (typeof cpOffset === 'object') {
  96. cpOffset = Object.assign({}, cpOffset);
  97. if (cpOffset.x == null) {
  98. cpOffset.x = 0;
  99. }
  100. cpOffset.x += (0, util_1.getStrokeWidth)(node) / 2;
  101. }
  102. else {
  103. cpOffset += (0, util_1.getStrokeWidth)(node) / 2;
  104. }
  105. }
  106. return (0, util_1.offset)(cp, line.start, cpOffset);
  107. };
  108. exports.boundary = boundary;
  109. //# sourceMappingURL=boundary.js.map