index.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <template>
  2. <div class="go-sketch-rule">
  3. <sketch-rule v-if="sketchRuleReDraw" :thick="thick" :scale="scale" :width="canvasBox().width"
  4. :height="canvasBox().height" :startX="startX" :startY="startY" :lines="lines" :palette="paletteStyle">
  5. </sketch-rule>
  6. <div ref="$app" class="edit-screens" @scroll="handleScroll">
  7. <div ref="$container" class="edit-screen-container" :style="{ width: width * 2 + 'px' }">
  8. <div ref="refSketchRuleBox" class="canvas" @mousedown="dragCanvas"
  9. :style="{ marginLeft: '-' + (canvasBox().width / 2 - 25) + 'px' }">
  10. <div :style="{ pointerEvents: isPressSpace ? 'none' : 'auto' }">
  11. <slot></slot>
  12. </div>
  13. </div>
  14. </div>
  15. </div>
  16. <!-- 修复右下角白点用的 -->
  17. <div v-if="designStore.getDarkTheme" class="fix-edit-screens-block"></div>
  18. </div>
  19. </template>
  20. <script setup lang="ts">
  21. import { ref, reactive, onMounted, toRefs, watch, onUnmounted, computed } from 'vue'
  22. import { listen } from 'dom-helpers'
  23. import { useDesignStore } from '@/store/modules/designStore/designStore'
  24. import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
  25. const chartEditStore = useChartEditStore()
  26. const designStore = useDesignStore()
  27. const thick = 20
  28. let prevMoveXVallue = [0, 0]
  29. let prevMoveYVallue = [0, 0]
  30. const $app = ref()
  31. const sketchRuleReDraw = ref(true)
  32. const refSketchRuleBox = ref()
  33. const $container = ref()
  34. const isPressSpace = ref(false)
  35. const cursorStyle = ref('auto')
  36. const { width, height } = toRefs(chartEditStore.getEditCanvasConfig)
  37. const startX = ref(0)
  38. const startY = ref(0)
  39. const lines = reactive({ h: [], v: [] })
  40. const scale = computed(() => {
  41. return chartEditStore.getEditCanvas.scale
  42. })
  43. // 滚动条拖动的高度
  44. const containerWidth = computed(() => {
  45. return `${height.value * 2}px`
  46. })
  47. // 主题
  48. const paletteStyle = computed(() => {
  49. const isDarkTheme = designStore.getDarkTheme
  50. return isDarkTheme
  51. ? {
  52. bgColor: '#18181c',
  53. longfgColor: '#4d4d4d',
  54. shortfgColor: '#4d4d4d',
  55. fontColor: '#4d4d4d',
  56. shadowColor: '#18181c',
  57. borderColor: '#18181c',
  58. cornerActiveColor: '#18181c'
  59. }
  60. : {}
  61. })
  62. // 颜色
  63. const themeColor = computed(() => {
  64. return designStore.getAppTheme
  65. })
  66. const handleWheel = (e: any) => {
  67. e.preventDefault()
  68. if (e.ctrlKey || e.metaKey) {
  69. let resScale = scale.value
  70. // 放大(200%)
  71. if (e.wheelDelta >= 0 && scale.value < 2) {
  72. resScale = scale.value + 0.05
  73. chartEditStore.setScale(resScale)
  74. return
  75. }
  76. // 缩小(10%)
  77. if (e.wheelDelta < 0 && scale.value > 0.1) {
  78. resScale = scale.value - 0.05
  79. chartEditStore.setScale(resScale)
  80. }
  81. }
  82. }
  83. const handleScroll = () => {
  84. if (!$app.value) return
  85. const screensRect = $app.value.getBoundingClientRect()
  86. const canvasRect = refSketchRuleBox.value.getBoundingClientRect()
  87. // 标尺开始的刻度
  88. startX.value = (screensRect.left + thick - canvasRect.left) / scale.value
  89. startY.value = (screensRect.top + thick - canvasRect.top) / scale.value
  90. }
  91. const dragCanvas = (e: any) => {
  92. e.preventDefault()
  93. e.stopPropagation()
  94. if (e.which == 2) isPressSpace.value = true
  95. else if (!window.$KeyboardActive?.space) return
  96. // @ts-ignore
  97. document.activeElement?.blur()
  98. const startX = e.pageX
  99. const startY = e.pageY
  100. const listenMousemove = listen(window, 'mousemove', (e: any) => {
  101. const nx = e.pageX - startX
  102. const ny = e.pageY - startY
  103. const [prevMoveX1, prevMoveX2] = prevMoveXVallue
  104. const [prevMoveY1, prevMoveY2] = prevMoveYVallue
  105. prevMoveXVallue = [prevMoveX2, nx]
  106. prevMoveYVallue = [prevMoveY2, ny]
  107. $app.value.scrollLeft -=
  108. prevMoveX2 > prevMoveX1 ? Math.abs(prevMoveX2 - prevMoveX1) : -Math.abs(prevMoveX2 - prevMoveX1)
  109. $app.value.scrollTop -=
  110. prevMoveY2 > prevMoveY1 ? Math.abs(prevMoveY2 - prevMoveY1) : -Math.abs(prevMoveY2 - prevMoveY1)
  111. })
  112. const listenMouseup = listen(window, 'mouseup', () => {
  113. listenMousemove()
  114. listenMouseup()
  115. prevMoveXVallue = [0, 0]
  116. prevMoveYVallue = [0, 0]
  117. isPressSpace.value = false
  118. })
  119. }
  120. const canvasBox = () => {
  121. const layoutDom = document.getElementById('go-chart-edit-layout')
  122. if (layoutDom) {
  123. return {
  124. height: layoutDom.clientHeight - 25,
  125. width: layoutDom.clientWidth
  126. }
  127. }
  128. return {
  129. width: width.value,
  130. height: height.value
  131. }
  132. }
  133. // 在位置不动的情况下重绘标尺
  134. const reDraw = () => {
  135. sketchRuleReDraw.value = false
  136. setTimeout(() => {
  137. sketchRuleReDraw.value = true
  138. }, 10)
  139. }
  140. watch(
  141. () => designStore.getDarkTheme,
  142. () => {
  143. reDraw()
  144. }
  145. )
  146. // 滚动居中
  147. const canvasPosCenter = () => {
  148. const { width: containerWidth, height: containerHeight } = $container.value.getBoundingClientRect()
  149. const { width, height } = canvasBox()
  150. $app.value.scrollLeft = containerWidth / 2 - width / 2
  151. $app.value.scrollTop = containerHeight / 2 - height / 2
  152. }
  153. // 处理标尺重制大小
  154. watch(
  155. () => scale.value,
  156. (newValue, oldValue) => {
  157. if (oldValue !== newValue) {
  158. handleScroll()
  159. chartEditStore.setScale(newValue)
  160. setTimeout(() => {
  161. canvasPosCenter()
  162. }, 500);
  163. }
  164. }
  165. )
  166. watch(
  167. () => isPressSpace.value,
  168. newValue => {
  169. cursorStyle.value = newValue ? 'grab' : 'auto'
  170. }
  171. )
  172. onMounted(() => {
  173. if ($app.value) {
  174. $app.value.addEventListener('wheel', handleWheel, { passive: false })
  175. canvasPosCenter()
  176. }
  177. })
  178. onUnmounted(() => {
  179. if ($app.value) {
  180. $app.value.removeEventListener('wheel', handleWheel)
  181. }
  182. })
  183. window.onKeySpacePressHold = (isHold: boolean) => {
  184. isPressSpace.value = isHold
  185. }
  186. </script>
  187. <style>
  188. /* 使用 SCSS 会报错,直接使用最基础的 CSS 进行修改,
  189. 此库有计划 Vue3 版本,但是开发的时候还没发布 */
  190. #mb-ruler {
  191. top: 0;
  192. left: 0;
  193. }
  194. /* 横线 */
  195. #mb-ruler .v-container .lines .line {
  196. /* 最大缩放 200% */
  197. width: 200vw !important;
  198. border-top: 1px dashed v-bind('themeColor') !important;
  199. }
  200. #mb-ruler .v-container .indicator {
  201. border-bottom: 1px dashed v-bind('themeColor') !important;
  202. }
  203. /* 竖线 */
  204. #mb-ruler .h-container .lines .line {
  205. /* 最大缩放 200% */
  206. height: 200vh !important;
  207. border-left: 1px dashed v-bind('themeColor') !important;
  208. }
  209. #mb-ruler .h-container .indicator {
  210. border-left: 1px dashed v-bind('themeColor') !important;
  211. }
  212. /* 坐标数值背景颜色 */
  213. #mb-ruler .indicator .value {
  214. background-color: rgba(0, 0, 0, 0);
  215. }
  216. /* 删除按钮 */
  217. #mb-ruler .line .del {
  218. padding: 0;
  219. color: v-bind('themeColor');
  220. font-size: 26px;
  221. font-weight: bolder;
  222. }
  223. #mb-ruler .corner {
  224. border-width: 0 !important;
  225. }
  226. </style>
  227. <style lang="scss" scoped>
  228. @include go('sketch-rule') {
  229. position: relative;
  230. overflow: hidden;
  231. width: 100%;
  232. height: 100%;
  233. .edit-screens {
  234. position: absolute;
  235. width: 100%;
  236. height: 100%;
  237. overflow: auto;
  238. user-select: none;
  239. padding-bottom: 0px;
  240. /* firefox */
  241. scrollbar-color: rgba(144, 146, 152, 0.3) transparent;
  242. scrollbar-width: thin;
  243. /* chrome */
  244. &::-webkit-scrollbar,
  245. &::-webkit-scrollbar-track-piece {
  246. background-color: transparent;
  247. }
  248. &::-webkit-scrollbar {
  249. width: 7px;
  250. }
  251. &::-webkit-scrollbar-thumb {
  252. border-radius: 5px;
  253. background-color: rgba(144, 146, 152, 0.3);
  254. }
  255. }
  256. .fix-edit-screens-block {
  257. position: absolute;
  258. bottom: 0;
  259. right: 0;
  260. width: 10px;
  261. height: 10px;
  262. background-color: $--color-dark-bg-1;
  263. }
  264. .edit-screen-container {
  265. position: absolute;
  266. height: v-bind('containerWidth');
  267. top: 0;
  268. left: 0;
  269. }
  270. .canvas {
  271. position: absolute;
  272. top: 50%;
  273. left: 50%;
  274. transform-origin: 50% 0;
  275. transform: translateY(-50%);
  276. &:hover {
  277. cursor: v-bind('cursorStyle');
  278. }
  279. &:active {
  280. cursor: crosshair;
  281. }
  282. }
  283. }
  284. </style>