drawView.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * 绘制调试工具
  3. */
  4. /**
  5. * 入口文件
  6. */
  7. function init(options, devTools) {
  8. let sysInfo = uni.getSystemInfoSync()
  9. let tagConfig = uni.getStorageSync("devTools_tagConfig");
  10. if (!tagConfig) {
  11. tagConfig = {}
  12. }
  13. tagConfig = Object.assign({
  14. show: options.bubble.status,
  15. x: sysInfo.screenWidth - 90,
  16. y: sysInfo.screenHeight - 90,
  17. }, tagConfig)
  18. tagConfig.show = options.bubble.status;
  19. // 拖动范围限制
  20. let dragLimit = {
  21. min: { x: 0, y: 0, },
  22. max: {
  23. x: sysInfo.screenWidth - 70,
  24. y: sysInfo.screenHeight - 24,
  25. }
  26. }
  27. let view = new plus.nativeObj.View('debugTag', {
  28. top: tagConfig.y + 'px',
  29. left: tagConfig.x + 'px',
  30. height: '24px',
  31. width: '70px',
  32. backgroundColor: options.bubble.bgColor,
  33. });
  34. view.drawText(options.bubble.text, {}, {
  35. size: '12px',
  36. color: options.bubble.color,
  37. weight: 'bold'
  38. });
  39. if (tagConfig.show) {
  40. view.show();
  41. }
  42. let isTouch = false;
  43. let touchStart = {
  44. l: 0,
  45. t: 0,
  46. x: 0,
  47. y: 0,
  48. time: 0,
  49. hasMove: false,
  50. }
  51. view.addEventListener("touchstart", (e) => {
  52. isTouch = true;
  53. touchStart.l = e.clientX;
  54. touchStart.t = e.clientY;
  55. touchStart.time = new Date().getTime();
  56. touchStart.hasMove = false;
  57. })
  58. view.addEventListener("touchmove", (e) => {
  59. if (!isTouch) return;
  60. if (!touchStart.hasMove) {
  61. touchStart.hasMove = true;
  62. }
  63. let x = e.screenX - touchStart.l;
  64. let y = e.screenY - touchStart.t;
  65. x = Math.min(Math.max(x, dragLimit.min.x), dragLimit.max.x)
  66. y = Math.min(Math.max(y, dragLimit.min.y), dragLimit.max.y)
  67. view.setStyle({
  68. top: y + 'px',
  69. left: x + 'px',
  70. })
  71. touchStart.x = x;
  72. touchStart.y = y;
  73. })
  74. view.addEventListener("touchend", (e) => {
  75. isTouch = false;
  76. if (
  77. !touchStart.hasMove
  78. || touchStart.time > (new Date().getTime() - 300)
  79. ) {// 单击事件
  80. let pages = getCurrentPages()
  81. let route = options.route.substring(1, options.route.length - 2);
  82. if (pages[pages.length - 1].route == route) {
  83. // 已经处于debug页面,不响应点击事件
  84. return;
  85. }
  86. devTools.show()
  87. } else { //拖拽结束事件
  88. tagConfig.x = touchStart.x;
  89. tagConfig.y = touchStart.y;
  90. uni.setStorageSync("devTools_tagConfig", tagConfig)
  91. }
  92. })
  93. uni.setStorageSync("devTools_tagConfig", tagConfig)
  94. }
  95. export default init;