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