align.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.resetOffset = exports.yAlign = exports.xAlign = void 0;
  4. const x6_common_1 = require("@antv/x6-common");
  5. const x6_geometry_1 = require("@antv/x6-geometry");
  6. // `x-align` when set to `middle` causes centering of the subelement around its new x coordinate.
  7. // `x-align` when set to `right` uses the x coordinate as referenced to the right of the bbox.
  8. exports.xAlign = {
  9. offset: offsetWrapper('x', 'width', 'right'),
  10. };
  11. // `y-align` when set to `middle` causes centering of the subelement around its new y coordinate.
  12. // `y-align` when set to `bottom` uses the y coordinate as referenced to the bottom of the bbox.
  13. exports.yAlign = {
  14. offset: offsetWrapper('y', 'height', 'bottom'),
  15. };
  16. exports.resetOffset = {
  17. offset(val, { refBBox }) {
  18. return val ? { x: -refBBox.x, y: -refBBox.y } : { x: 0, y: 0 };
  19. },
  20. };
  21. function offsetWrapper(axis, dimension, corner) {
  22. return (value, { refBBox }) => {
  23. const point = new x6_geometry_1.Point();
  24. let delta;
  25. if (value === 'middle') {
  26. delta = refBBox[dimension] / 2;
  27. }
  28. else if (value === corner) {
  29. delta = refBBox[dimension];
  30. }
  31. else if (typeof value === 'number' && Number.isFinite(value)) {
  32. delta = value > -1 && value < 1 ? -refBBox[dimension] * value : -value;
  33. }
  34. else if (x6_common_1.NumberExt.isPercentage(value)) {
  35. delta = (refBBox[dimension] * parseFloat(value)) / 100;
  36. }
  37. else {
  38. delta = 0;
  39. }
  40. point[axis] = -(refBBox[axis] + delta);
  41. return point;
  42. };
  43. }
  44. //# sourceMappingURL=align.js.map