pin.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.pinAbsolute = exports.pinRelative = void 0;
  4. const x6_geometry_1 = require("@antv/x6-geometry");
  5. function toPercentage(value, max) {
  6. if (max === 0) {
  7. return '0%';
  8. }
  9. return `${Math.round((value / max) * 100)}%`;
  10. }
  11. function pin(relative) {
  12. const strategy = (terminal, view, magnet, coords) => {
  13. return view.isEdgeElement(magnet)
  14. ? pinEdgeTerminal(relative, terminal, view, magnet, coords)
  15. : pinNodeTerminal(relative, terminal, view, magnet, coords);
  16. };
  17. return strategy;
  18. }
  19. function pinNodeTerminal(relative, data, view, magnet, coords) {
  20. const node = view.cell;
  21. const angle = node.getAngle();
  22. const bbox = view.getUnrotatedBBoxOfElement(magnet);
  23. const center = node.getBBox().getCenter();
  24. const pos = x6_geometry_1.Point.create(coords).rotate(angle, center);
  25. let dx = pos.x - bbox.x;
  26. let dy = pos.y - bbox.y;
  27. if (relative) {
  28. dx = toPercentage(dx, bbox.width);
  29. dy = toPercentage(dy, bbox.height);
  30. }
  31. data.anchor = {
  32. name: 'topLeft',
  33. args: {
  34. dx,
  35. dy,
  36. rotate: true,
  37. },
  38. };
  39. return data;
  40. }
  41. function pinEdgeTerminal(relative, end, view, magnet, coords) {
  42. const connection = view.getConnection();
  43. if (!connection) {
  44. return end;
  45. }
  46. const length = connection.closestPointLength(coords);
  47. if (relative) {
  48. const totalLength = connection.length();
  49. end.anchor = {
  50. name: 'ratio',
  51. args: {
  52. ratio: length / totalLength,
  53. },
  54. };
  55. }
  56. else {
  57. end.anchor = {
  58. name: 'length',
  59. args: {
  60. length,
  61. },
  62. };
  63. }
  64. return end;
  65. }
  66. exports.pinRelative = pin(true);
  67. exports.pinAbsolute = pin(false);
  68. //# sourceMappingURL=pin.js.map