您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

functions.web.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // @flow
  2. const LEFT_RIGHT_OFFSET = 25;
  3. const TOP_BOTTOM_OFFSET = 20;
  4. const getLeftAlignedStyle = bounds => {
  5. return {
  6. position: 'fixed',
  7. right: `${window.innerWidth - bounds.x + LEFT_RIGHT_OFFSET}px`
  8. };
  9. };
  10. const getRightAlignedStyle = bounds => {
  11. return {
  12. position: 'fixed',
  13. left: `${bounds.x + bounds.width + LEFT_RIGHT_OFFSET}px`
  14. };
  15. };
  16. const getTopAlignedStyle = bounds => {
  17. return {
  18. position: 'fixed',
  19. bottom: `${window.innerHeight - bounds.y + TOP_BOTTOM_OFFSET}px`
  20. };
  21. };
  22. const getBottomAlignedStyle = bounds => {
  23. return {
  24. position: 'fixed',
  25. top: `${bounds.y + bounds.height + TOP_BOTTOM_OFFSET}px`
  26. };
  27. };
  28. const getLeftRightStartAlign = (bounds, size) => {
  29. return {
  30. top: `${Math.min(bounds.y + 15, window.innerHeight - size.height - 20)}px`
  31. };
  32. };
  33. const getLeftRightMidAlign = (bounds, size) => {
  34. return {
  35. bottom: `${window.innerHeight - bounds.y - bounds.height - (size.height / 2)}px`
  36. };
  37. };
  38. const getLeftRightEndAlign = (bounds, size) => {
  39. return {
  40. bottom: `${Math.min(window.innerHeight - bounds.y - bounds.height, window.innerHeight - size.height)}px`
  41. };
  42. };
  43. const getTopBotStartAlign = bounds => {
  44. return {
  45. right: `${window.innerWidth - bounds.x + 10}px`
  46. };
  47. };
  48. const getTopBotMidAlign = (bounds, size) => {
  49. return {
  50. right: `${window.innerWidth - bounds.x - (size.width / 2)}px`
  51. };
  52. };
  53. const getTopBotEndAlign = bounds => {
  54. return {
  55. left: `${bounds.x + bounds.width + 10}px`
  56. };
  57. };
  58. /**
  59. * Gets the trigger element's and the context menu's bounds/size info and
  60. * computes the style to apply to the context menu to positioning it correctly
  61. * in regards to the given position info.
  62. *
  63. * @param {DOMRect} triggerBounds -The bounds info of the trigger html element.
  64. * @param {DOMRectReadOnly} dialogSize - The size info of the context menu.
  65. * @param {string} position - The position of the context menu in regards to the trigger element.
  66. *
  67. * @returns {Object} = The style to apply to context menu for positioning it correctly.
  68. */
  69. export const getContextMenuStyle = (triggerBounds: DOMRect,
  70. dialogSize: DOMRectReadOnly,
  71. position: string) => {
  72. const parsed = position.split('-');
  73. switch (parsed[0]) {
  74. case 'top': {
  75. let alignmentStyle = {};
  76. if (parsed[1]) {
  77. alignmentStyle = parsed[1] === 'start'
  78. ? getTopBotStartAlign(triggerBounds)
  79. : getTopBotEndAlign(triggerBounds);
  80. } else {
  81. alignmentStyle = getTopBotMidAlign(triggerBounds, dialogSize);
  82. }
  83. return {
  84. ...getTopAlignedStyle(triggerBounds),
  85. ...alignmentStyle
  86. };
  87. }
  88. case 'bottom': {
  89. let alignmentStyle = {};
  90. if (parsed[1]) {
  91. alignmentStyle = parsed[1] === 'start'
  92. ? getTopBotStartAlign(triggerBounds)
  93. : getTopBotEndAlign(triggerBounds);
  94. } else {
  95. alignmentStyle = getTopBotMidAlign(triggerBounds, dialogSize);
  96. }
  97. return {
  98. ...getBottomAlignedStyle(triggerBounds),
  99. ...alignmentStyle
  100. };
  101. }
  102. case 'left': {
  103. let alignmentStyle = {};
  104. if (parsed[1]) {
  105. alignmentStyle = parsed[1] === 'start'
  106. ? getLeftRightStartAlign(triggerBounds, dialogSize)
  107. : getLeftRightEndAlign(triggerBounds, dialogSize);
  108. } else {
  109. alignmentStyle = getLeftRightMidAlign(triggerBounds, dialogSize);
  110. }
  111. return {
  112. ...getLeftAlignedStyle(triggerBounds),
  113. ...alignmentStyle
  114. };
  115. }
  116. case 'right': {
  117. let alignmentStyle = {};
  118. if (parsed[1]) {
  119. alignmentStyle = parsed[1] === 'start'
  120. ? getLeftRightStartAlign(triggerBounds, dialogSize)
  121. : getLeftRightEndAlign(triggerBounds, dialogSize);
  122. } else {
  123. alignmentStyle = getLeftRightMidAlign(triggerBounds, dialogSize);
  124. }
  125. return {
  126. ...getRightAlignedStyle(triggerBounds),
  127. ...alignmentStyle
  128. };
  129. }
  130. default: {
  131. return {
  132. ...getLeftAlignedStyle(triggerBounds),
  133. ...getLeftRightEndAlign(triggerBounds, dialogSize)
  134. };
  135. }
  136. }
  137. };