util.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.findShapeNode = exports.getStrokeWidth = exports.offset = void 0;
  4. const x6_geometry_1 = require("@antv/x6-geometry");
  5. function offset(p1, p2, offset) {
  6. let tx;
  7. if (typeof offset === 'object') {
  8. if (Number.isFinite(offset.y)) {
  9. const line = new x6_geometry_1.Line(p2, p1);
  10. const { start, end } = line.parallel(offset.y);
  11. p2 = start; // eslint-disable-line
  12. p1 = end; // eslint-disable-line
  13. }
  14. tx = offset.x;
  15. }
  16. else {
  17. tx = offset;
  18. }
  19. if (tx == null || !Number.isFinite(tx)) {
  20. return p1;
  21. }
  22. const length = p1.distance(p2);
  23. if (tx === 0 && length > 0) {
  24. return p1;
  25. }
  26. return p1.move(p2, -Math.min(tx, length - 1));
  27. }
  28. exports.offset = offset;
  29. function getStrokeWidth(magnet) {
  30. const stroke = magnet.getAttribute('stroke-width');
  31. if (stroke === null) {
  32. return 0;
  33. }
  34. return parseFloat(stroke) || 0;
  35. }
  36. exports.getStrokeWidth = getStrokeWidth;
  37. function findShapeNode(magnet) {
  38. if (magnet == null) {
  39. return null;
  40. }
  41. let node = magnet;
  42. do {
  43. let tagName = node.tagName;
  44. if (typeof tagName !== 'string')
  45. return null;
  46. tagName = tagName.toUpperCase();
  47. if (tagName === 'G') {
  48. node = node.firstElementChild;
  49. }
  50. else if (tagName === 'TITLE') {
  51. node = node.nextElementSibling;
  52. }
  53. else
  54. break;
  55. } while (node);
  56. return node;
  57. }
  58. exports.findShapeNode = findShapeNode;
  59. //# sourceMappingURL=util.js.map