createH5Bubble.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. *! 创建h5页面上拖动的气泡标签
  3. */
  4. function createH5Bubble(options, devTools) {
  5. let tagConfig = localStorage.getItem("devTools_tagConfig");
  6. if (!tagConfig) {
  7. tagConfig = {}
  8. } else {
  9. tagConfig = JSON.parse(tagConfig)
  10. }
  11. tagConfig = Object.assign({
  12. show: options.bubble.status,
  13. x: window.innerWidth - 90,
  14. y: window.innerHeight - 90,
  15. }, tagConfig);
  16. tagConfig.show = options.bubble.status;
  17. // 拖动范围限制
  18. let dragLimit = {
  19. min: { x: 0, y: 0, },
  20. max: {
  21. x: window.innerWidth - 70,
  22. y: window.innerHeight - 24,
  23. }
  24. }
  25. tagConfig.x = Math.min(Math.max(tagConfig.x, dragLimit.min.x), dragLimit.max.x)
  26. tagConfig.y = Math.min(Math.max(tagConfig.y, dragLimit.min.y), dragLimit.max.y)
  27. let tag = document.createElement("div");
  28. tag.style = `
  29. box-sizing: border-box;
  30. position: fixed;
  31. z-index: 9999999;
  32. left: ${tagConfig.x}px;
  33. top: ${tagConfig.y}px;
  34. width: 70px;
  35. height: 24px;
  36. display: flex;
  37. flex-direction: column;
  38. align-items: center;
  39. justify-content: center;
  40. padding: 4px;
  41. border-radius: 6px;
  42. background-color: ${options.bubble.bgColor};
  43. color: ${options.bubble.color};
  44. font-size: 10px;
  45. cursor: grab;
  46. box-shadow: 0px 0px 6px ${options.bubble.bgColor};
  47. backdrop-filter: blur(1px);
  48. `;
  49. tag.innerHTML = options.bubble.text;
  50. tag.setAttribute("id", "debugTag")
  51. if (tagConfig.show) {
  52. document.body.appendChild(tag)
  53. }
  54. /**
  55. * 标签单击事件
  56. */
  57. function tagClick() {
  58. let pages = getCurrentPages()
  59. let route = options.route.substring(1, options.route.length - 2);
  60. if (pages[pages.length - 1].route == route) {
  61. // 已经处于debug页面,不响应点击事件
  62. return;
  63. }
  64. devTools.show()
  65. }
  66. let isTouch = false;
  67. let touchStartPoint = {
  68. clientX: 0,
  69. clientY: 0,
  70. tagX: tagConfig.x,
  71. tagY: tagConfig.y,
  72. hasMove: false,
  73. }
  74. function touchStart(e) {
  75. if (isTouch) return;
  76. if (e.preventDefault) {
  77. e.preventDefault()
  78. }
  79. let clientX = e.clientX ? e.clientX : e.targetTouches[0].clientX;
  80. let clientY = e.clientX ? e.clientY : e.targetTouches[0].clientY;
  81. touchStartPoint.clientX = clientX;
  82. touchStartPoint.clientY = clientY;
  83. touchStartPoint.tagX = tagConfig.x;
  84. touchStartPoint.tagY = tagConfig.y;
  85. touchStartPoint.hasMove = false;
  86. isTouch = true;
  87. }
  88. function touchMove(e) {
  89. if (!isTouch) return;
  90. if (e.preventDefault) {
  91. e.preventDefault()
  92. }
  93. let clientX = e.clientX ? e.clientX : e.targetTouches[0].clientX;
  94. let clientY = e.clientX ? e.clientY : e.targetTouches[0].clientY;
  95. touchStartPoint.hasMove = true;
  96. let offsetX = touchStartPoint.clientX - clientX;
  97. let offsetY = touchStartPoint.clientY - clientY;
  98. let tx = touchStartPoint.tagX - offsetX;
  99. let ty = touchStartPoint.tagY - offsetY;
  100. tx = Math.min(Math.max(tx, dragLimit.min.x), dragLimit.max.x)
  101. ty = Math.min(Math.max(ty, dragLimit.min.y), dragLimit.max.y)
  102. tag.style.left = `${tx}px`;
  103. tag.style.top = `${ty}px`;
  104. tagConfig.x = tx;
  105. tagConfig.y = ty;
  106. }
  107. function touchEnd(e) {
  108. if (!isTouch) return;
  109. if (e.preventDefault) {
  110. e.preventDefault()
  111. }
  112. isTouch = false;
  113. localStorage.setItem("devTools_tagConfig", JSON.stringify(tagConfig))
  114. if (!touchStartPoint.hasMove) {
  115. tagClick()
  116. }
  117. }
  118. tag.addEventListener("touchstart", touchStart)
  119. tag.addEventListener("touchmove", touchMove)
  120. tag.addEventListener("touchend", touchEnd)
  121. tag.addEventListener("mousedown", touchStart)
  122. document.addEventListener("mousemove", touchMove)
  123. document.addEventListener("mouseup", touchEnd)
  124. localStorage.setItem("devTools_tagConfig", JSON.stringify(tagConfig))
  125. }
  126. export default createH5Bubble;