3328350766
6 天以前 761eb03d6b3bebd0b197179564c84d89d3d12a0d
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
<template>
  <div
    class="mouse-select-wrap"
    style="position: relative; user-select: none;"
    @mousedown="handleMouseDown"
    @mousemove="handleMouseMove"
    @mouseup="handleMouseUp"
    @mouseleave="handleMouseLeave"
  >
    <!-- 这里是需要进行框选的内容 -->
    <div :class="{ 'notSelect': isSelecting }">
      <slot />
    </div>
    <!-- 在鼠标框选移动时生成虚线框 -->
    <div
      v-if="isSelecting"
      :style="getSelectionBoxStyle"
    />
  </div>
</template>
 
<script>
import { mapMutations, mapState } from 'vuex'
let time = 0
let newTime = 0
export default {
  props: {
    offsetX: {
      type: Number,
      default: 340 + 50
    },
    offsetY: {
      type: Number,
      default: 73 + 50
    }
  },
  data () {
    return {
      isSelecting: false, // 是否正在进行框选
      isSelectDown: false, // 是否按下鼠标左键
      startX: 0, // 框选起始点的横坐标
      startY: 0, // 框选起始点的纵坐标
      endX: 0, // 框选结束点的横坐标
      endY: 0 // 框选结束点的纵坐标
    }
  },
  computed: {
    ...mapState('bigScreen', {
      shiftKeyDown: state => state.shiftKeyDown,
      scale: state => state.zoom / 100
    }),
    getSelectionBoxStyle () {
      // 计算虚线框的样式
      const left = Math.min(this.startX, this.endX) + 'px'
      const top = Math.min(this.startY, this.endY) + 'px'
      const width = Math.abs(this.endX - this.startX) + 'px'
      const height = Math.abs(this.endY - this.startY) + 'px'
      if (!this.isSelecting) {
        return {
          display: 'none'
        }
      }
      return {
        position: 'absolute',
        left,
        top,
        width,
        height,
        border: '1px dashed #007aff',
        background: '#2a2e3380'
      }
    }
  },
  methods: {
    ...mapMutations('bigScreen',
      [
        'changeActiveCodes',
        'changeActiveCode'
      ]
    ),
    handleMouseDown (event) {
      // 点击在底部背景上
      if (event.button === 0) {
        time = new Date()
        // 避免和shift + 点击多选组件冲突
        if (this.shiftKeyDown) {
          return
        }
        this.isSelectDown = true
        // 点击在底部背景上
        if (typeof event.target.className === 'string' && event.target.className.indexOf('mouse-select-wrap') !== -1) {
          this.startX = event.offsetX
          this.endX = event.offsetX
          this.startY = event.offsetY
          this.endY = event.offsetY
        } else if (typeof event.target.className === 'string' && event.target.className.indexOf('design-drag-wrap') !== -1) {
          this.startX = event.offsetX + 50
          this.endX = event.offsetX + 50
          this.startY = event.offsetY + 50
          this.endY = event.offsetY + 50
        } else if (event.target.className === '') {
          this.startX = event.offsetX + 50
          this.endX = event.offsetX + 50
          this.startY = event.offsetY + 50
          this.endY = event.offsetY + 50
        }
      }
    },
    handleMouseMove (event) {
      if (!this.isSelectDown) {
        return
      }
      newTime = new Date()
      // if (newTime - time > 300) {
      this.isSelecting = true
      // }
      if (this.isSelecting) {
        if (typeof event.target.className === 'string' && event.target.className.indexOf('mouse-select-wrap') !== -1) {
          this.endX = event.offsetX
          this.endY = event.offsetY
        } else if (typeof event.target.className === 'string' && (event.target.className.indexOf('design-drag-wrap') !== -1)) {
          this.endX = event.offsetX + 50
          this.endY = event.offsetY + 50
        }
      }
    },
    handleMouseUp (event) {
      this.isSelectDown = false
 
      //  按下鼠标和抬起鼠标时间间隔小表示单击,否则表示框选结束
      // 避免和shift + 点击多选组件冲突
      // 避免和右键点击冲突
      if (
        newTime - time < 300 &&
        !this.shiftKeyDown &&
        event.button !== 2
      ) {
        // 按下鼠标和抬起鼠标时间间隔小表示单击,否则表示框选结束
        // this.changeActiveCodes([])
        // this.changeActiveCode('')
      }
      // 鼠标按下,并开始选择
      if (event.button === 0 && this.isSelecting) {
        this.isSelecting = false
        if (newTime - time < 300) {
          // newTime = 0
          // time = 0
          // return
        }
        newTime = 0
        time = 0
        // 在这里可以根据起始点和结束点的坐标计算选中的区域,进行相应的操作
        this.$emit('selectArea', {
          startX: Math.min(this.startX, this.endX),
          startY: Math.min(this.startY, this.endY),
          endX: Math.max(this.startX, this.endX),
          endY: Math.max(this.startY, this.endY)
        })
        // 重置起始点和结束点的坐标
        this.startX = 0
        this.startY = 0
        this.endX = 0
        this.endY = 0
      } else if (!this.shiftKeyDown && !this.isSelecting) {
        // 重置起始点和结束点的坐标
        this.startX = 0
        this.startY = 0
        this.endX = 0
        this.endY = 0
        // this.$emit('selectArea', {
        //   startX: Math.min(this.startX, this.endX),
        //   startY: Math.min(this.startY, this.endY),
        //   endX: Math.max(this.startX, this.endX),
        //   endY: Math.max(this.startY, this.endY)
        // })
        // this.changeActiveCodes([])
      }
    },
    handleMouseLeave () {
      this.isSelecting = false
      this.isSelectDown = false
      this.startX = 0
      this.startY = 0
      this.endX = 0
      this.endY = 0
    }
  }
}
</script>
<style scoped lang="scss">
.mouse-select-wrap{
  width: 200%;
  height: 200%;
  margin: -50px;
  padding: 50px;
}
.notSelect {
  user-select: none;
  pointer-events: none;
}
</style>