anchor.mjs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Utilities
  2. import { includes } from "./helpers.mjs";
  3. const block = ['top', 'bottom'];
  4. const inline = ['start', 'end', 'left', 'right'];
  5. /** Parse a raw anchor string into an object */
  6. export function parseAnchor(anchor, isRtl) {
  7. let [side, align] = anchor.split(' ');
  8. if (!align) {
  9. align = includes(block, side) ? 'start' : includes(inline, side) ? 'top' : 'center';
  10. }
  11. return {
  12. side: toPhysical(side, isRtl),
  13. align: toPhysical(align, isRtl)
  14. };
  15. }
  16. export function toPhysical(str, isRtl) {
  17. if (str === 'start') return isRtl ? 'right' : 'left';
  18. if (str === 'end') return isRtl ? 'left' : 'right';
  19. return str;
  20. }
  21. export function flipSide(anchor) {
  22. return {
  23. side: {
  24. center: 'center',
  25. top: 'bottom',
  26. bottom: 'top',
  27. left: 'right',
  28. right: 'left'
  29. }[anchor.side],
  30. align: anchor.align
  31. };
  32. }
  33. export function flipAlign(anchor) {
  34. return {
  35. side: anchor.side,
  36. align: {
  37. center: 'center',
  38. top: 'bottom',
  39. bottom: 'top',
  40. left: 'right',
  41. right: 'left'
  42. }[anchor.align]
  43. };
  44. }
  45. export function flipCorner(anchor) {
  46. return {
  47. side: anchor.align,
  48. align: anchor.side
  49. };
  50. }
  51. export function getAxis(anchor) {
  52. return includes(block, anchor.side) ? 'y' : 'x';
  53. }
  54. //# sourceMappingURL=anchor.mjs.map