globalTools.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <!--
  2. * @FileDescription: 公用工具 - 【白板/批注】
  3. * @Author: 王新雷
  4. * @Date:2024年10月14日10:03:11
  5. -->
  6. <template>
  7. <div class="globalTools" :class="isPlay ? 'isPlay' : ''">
  8. <div :class="['iconTools', toolOpen ? 'hideTools' : '']" ref="iconToolsDom">
  9. <img @click="openTool" src="@/img/layout/icon-tool.png" />
  10. </div>
  11. <div :class="['expendTools', toolOpen ? 'showTools' : '']" ref="expendToolsDom">
  12. <img @click="openType('note')" src="@/img/layout/icon-note.png" />
  13. <img @click="openType('whiteboard')" class="iconWhiteboard" src="@/img/layout/icon-whiteboard.png" />
  14. <img @click="openTool" class="iconArrow" src="@/img/layout/g-arrow-right.png" />
  15. </div>
  16. </div>
  17. <pen
  18. :close="
  19. () => {
  20. penShow = false
  21. }
  22. "
  23. v-model="penShow"
  24. />
  25. <pen
  26. :is-white="true"
  27. :close="
  28. () => {
  29. whitePenShow = false
  30. }
  31. "
  32. v-model="whitePenShow"
  33. />
  34. </template>
  35. <script setup lang="ts">
  36. import { baseSize, baseWidth, size } from "@/libs/rem"
  37. import pen from "@/views/coursewarePlay/components/pen"
  38. import { toolOpen, whitePenShow, penShow, isPlay } from "./globalTools"
  39. import { onMounted, ref } from "vue"
  40. const iconToolsDom = ref<HTMLDivElement>()
  41. const expendToolsDom = ref<HTMLDivElement>()
  42. function openTool() {
  43. if (isLock) return
  44. toolOpen.value = !toolOpen.value
  45. }
  46. function openType(type: "note" | "whiteboard") {
  47. console.log(type, "type")
  48. if (isLock) return
  49. if (type === "note") {
  50. penShow.value = true
  51. } else if (type === "whiteboard") {
  52. whitePenShow.value = true
  53. }
  54. }
  55. function computePos(type: "width" | "height", value: number | string) {
  56. const clientNum = type == "width" ? baseWidth : document.documentElement.clientHeight
  57. return typeof value === "string"
  58. ? {
  59. pos:
  60. type == "width"
  61. ? ((clientNum - (parseInt(value) / 100) * clientNum) / 2 / baseSize).toFixed(5)
  62. : (clientNum - (parseInt(value) / 100) * clientNum) / 2,
  63. unit: value
  64. }
  65. : {
  66. pos: type == "width" ? ((clientNum - value) / 2 / baseSize).toFixed(5) : (clientNum - value * (size / baseSize)) / 2,
  67. unit: (value / baseSize).toFixed(5) + "rem"
  68. }
  69. }
  70. /* 拖拽还没有兼容rem */
  71. let isLock = false
  72. function drag(el: HTMLElement) {
  73. function mousedown(e: MouseEvent) {
  74. e.stopPropagation()
  75. e.preventDefault()
  76. isLock = false
  77. const parentElement = el
  78. const parentElementRect = parentElement.getBoundingClientRect()
  79. // const downX = e.clientX
  80. const downY = e.clientY
  81. // const clientWidth = document.documentElement.clientWidth
  82. const clientHeight = document.documentElement.clientHeight
  83. // const minLeft = 0
  84. const minTop = 0
  85. // const maxLeft = clientWidth - parentElementRect.width
  86. const maxTop = clientHeight - parentElementRect.height
  87. function onMousemove(e: MouseEvent) {
  88. // let moveX = parentElementRect.left + (e.clientX - downX)
  89. let moveY = parentElementRect.top + (e.clientY - downY)
  90. // let moveY = e.clientY - downY
  91. // moveX = moveX < minLeft ? minLeft : moveX > maxLeft ? maxLeft : moveX
  92. moveY = moveY < minTop ? minTop : moveY > maxTop ? maxTop : moveY
  93. document.documentElement.style.setProperty("--toolTranslateY", `${moveY}px`)
  94. isLock = true
  95. }
  96. function onMouseup() {
  97. document.removeEventListener("mousemove", onMousemove)
  98. document.removeEventListener("mouseup", onMouseup)
  99. }
  100. document.addEventListener("mousemove", onMousemove)
  101. document.addEventListener("mouseup", onMouseup)
  102. }
  103. el.addEventListener("mousedown", mousedown, true)
  104. }
  105. //重新计算位置 居中
  106. function refreshPos() {
  107. const posHeight = computePos("height", iconToolsDom.value?.clientHeight || 0)
  108. if (iconToolsDom.value) {
  109. document.documentElement.style.setProperty("--toolTranslateY", `${posHeight.pos}px`)
  110. }
  111. }
  112. onMounted(() => {
  113. drag(iconToolsDom.value!)
  114. drag(expendToolsDom.value!)
  115. refreshPos()
  116. iconToolsDom.value!.onclick = (e: any) => {
  117. e.stopPropagation()
  118. }
  119. })
  120. </script>
  121. <style lang="scss" scoped>
  122. .globalTools {
  123. &.isPlay {
  124. .iconTools,
  125. .expendTools {
  126. opacity: $opacity-disabled;
  127. }
  128. }
  129. .iconTools,
  130. .expendTools {
  131. position: fixed;
  132. right: 0;
  133. top: 0;
  134. transform: translateY(var(--toolTranslateY));
  135. // margin-top: -29px;
  136. z-index: 2999;
  137. padding: 8px 14px 8px 16px;
  138. background: rgba(0, 0, 0, 0.4);
  139. border-radius: 200px 0px 0px 200px;
  140. border: 2px solid rgba(255, 255, 255, 0.3);
  141. border-right-width: 0;
  142. cursor: pointer;
  143. // transition: transform 0.2s ease;
  144. img {
  145. width: 34px;
  146. height: 34px;
  147. -moz-user-select: none;
  148. /* 火狐浏览器 */
  149. -webkit-user-drag: none;
  150. /* 谷歌、Safari和Opera浏览器 */
  151. -webkit-user-select: none;
  152. /* 谷歌、Safari和Opera浏览器 */
  153. -ms-user-select: none;
  154. /* IE10+浏览器 */
  155. user-select: none;
  156. /* 通用 */
  157. -webkit-touch-callout: none;
  158. /* iOS Safari */
  159. }
  160. }
  161. .iconTools {
  162. // transition-delay: 0.2s;
  163. }
  164. .expendTools {
  165. // transform: translateX(100%);
  166. display: none;
  167. img {
  168. cursor: pointer;
  169. }
  170. .iconWhiteboard {
  171. margin: 0 30px;
  172. }
  173. .iconArrow {
  174. width: 28px;
  175. height: 28px;
  176. }
  177. }
  178. .hideTools {
  179. // transition: transform 0.2s ease;
  180. transform: translateY(var(--toolTranslateY));
  181. display: none;
  182. }
  183. .showTools {
  184. // transition: transform 0.2s ease;
  185. // transition-delay: 0.2s;
  186. transform: translateY(var(--toolTranslateY));
  187. display: block;
  188. }
  189. }
  190. </style>