使用vue实现鼠标框选文件的功能
# 示例代码
<template>
<div
ref="fileTable"
tabindex="0"
class="file-table"
@click.stop="handleClickWarp(data)"
@contextmenu.prevent.stop="
handleContextmenu({ event: $event, data: data, type: 'empty' })
"
@mousedown.left="onmousedownClick"
@mouseup="handMouseUp"
@mousemove="onmouseMove"
>
<!-- @click.stop="handleBoxClick(data)" -->
<div class="list-inner" v-if="tableData && tableData.length">
<ul class="block-box" v-if="type === 'block'">
<li
v-for="item in tableData"
:key="item.hash + '_' + item.fileId + item.isSelect"
:id="item.hash"
:class="[
{ cursor: item.mime === 'directory' },
{ active: item.isSelect },
{ isVirtualization: item.isVirtualization },
{ mousehover: !mouseState.isMove },
]"
@click.stop="handleItemClick(item, true)"
@dblclick.stop="handleDblClick(item)"
@contextmenu.prevent.stop="
handleContextmenu({ event: $event, data: item, type: 'file' })
"
:title="item.name"
name="file"
>
<div class="icon-warp">
<img :src="item.icon" />
</div>
<div v-if="item.isInput" class="edit">
<input
type="text"
:id="'input_' + item.hash"
autofocus="autofocus"
v-model="item.reName"
@click.stop
@keyup="handleKeyUp($event, item.reName)"
@blur="handleOnBlur(item.reName)"
/>
</div>
<p v-else class="name">{{ item.name }}</p>
</li>
</ul>
<Table :columns="columns" :data="tableData" v-if="type === 'table'">
<template slot-scope="{ row }" slot="name">
<div
class="name-warp"
@click.stop="handleItemClick(row, true)"
@dblclick.stop="handleDblClick(row)"
@contextmenu.prevent.stop="
handleContextmenu({ event: $event, data: row, type: 'file' })
"
>
<div class="icon-warp">
<img :src="row.icon" />
</div>
<div v-if="row.isInput" class="edit">
<input
type="text"
:id="'input_' + row.hash"
autofocus="autofocus"
v-model="row.reName"
@keyup="handleKeyUp($event, row.reName)"
@blur="handleOnBlur(row.reName)"
/>
</div>
<p v-else class="name">{{ row.name }}</p>
</div>
</template>
<template slot-scope="{ row }" slot="power">
<div class="power-warp">
<p>
{{ row.read == 1 ? "可读取" : "不可读" }} 和
{{ row.write == 1 ? "可写入" : "不可写" }}
</p>
</div>
</template>
<template slot-scope="{ row }" slot="changeTime">
<div><Time :time="row.ts" /></div>
</template>
<template slot-scope="{ row }" slot="size">
<span>{{ row.formatSize }}</span>
</template>
<template slot-scope="{ row }" slot="type">
<span v-if="row.mime == 'directory'">{{ fileMapTip["folder"] }}</span>
<span v-else>{{
fileMapTip[row.suffix] || fileMapTip["unknown"]
}}</span>
</template>
</Table>
<div id="rectangular"></div>
</div>
<NotDataLoading :loading="loading" :data="tableData">
暂无任何文件
</NotDataLoading>
</div>
</template>
<script>
import { formatBytes } from "@/utils/index";
import { fileImgMapSuffix, getRequestId } from "@/utils/files.js";
import { cloneDeep } from "lodash";
export default {
name: "FileTable",
data() {
return {
tableData: [],
content: {},
isReName: false,
type: "block", // table block
sort: "size",
mouseState: {
isDown: false,
isMove: false,
},
columns: [
{
title: "名称",
slot: "name",
// sortable: true,
},
{
title: "权限",
slot: "power",
width: 240,
},
{
title: "修改于",
slot: "changeTime",
width: 160,
},
{
title: "大小",
slot: "size",
// sortable: true,
width: 160,
},
{
title: "类别",
slot: "type",
width: 200,
},
],
};
},
props: ["data", "loading", "copyInfo"],
watch: {
copyInfo: {
handler() {
this.setData(this.data);
},
},
data: {
handler(newVal) {
// console.log(newVal);
if (newVal) {
this.setData(newVal);
} else {
this.setData({ children: [{}] });
}
},
// deep: true,
immediate: true,
},
},
created() {
this.isDownESC = false;
this.keyEvent = { ctrlKey: "", keyCode: 0 };
},
mounted() {
// 键盘操作
this.keyFiles = [];
this.$refs.fileTable.onkeydown = (e) => {
// console.log(e);
// this.$parent.copyInfo = {};
e = e || window.event;
// e.preventDefault(); //关闭浏览器快捷键
// e.keyCode = e.charCode || e.keyCode;
this.keyEvent = e;
// console.log(this.keyEvent);
// console.log({ ...this.keyState });
if (this.keyEvent.ctrlKey && this.keyEvent.keyCode == 65) {
// ctr+A
this.handleIsSelect(this.data.children, true);
this.$store.commit("setMouseClickType", "keydown");
this.$store.commit("setKeyFiles", this.data.children);
e.preventDefault();
} else if (this.keyEvent.ctrlKey && this.keyEvent.keyCode == 67) {
// ctr+C
console.log("ctr+C");
if (!this.isReName) {
this.$parent.clickMenuDropdown({ type: "duplicate" });
}
} else if (this.keyEvent.ctrlKey && this.keyEvent.keyCode == 86) {
// ctr+V
console.log("ctr+V");
this.$parent.clickMenuDropdown({ type: "paste" });
} else if (this.keyEvent.ctrlKey && this.keyEvent.keyCode == 88) {
// ctr+X
console.log("ctr+X");
this.$parent.clickMenuDropdown({ type: "shear" });
} else if (this.keyEvent.keyCode == 27) {
// ESC 退出重命名
console.log("ESC");
this.isDownESC = true;
this.tableData.forEach((item) => {
item.reName = "";
item.isInput = false;
});
} else if (
this.$store.state.keyFiles.length &&
this.keyEvent.keyCode == 46
) {
// Delete
console.log("Delete");
// 判断正在重命名
let file = this.$store.state.keyFiles[0];
if (file.hash == this.data.hash) return;
if (file && !document.querySelector(`#input_${file.hash}`)) {
this.$parent.clickMenuDropdown({ type: "remove" });
}
}
};
this.$refs.fileTable.onkeyup = (e) => {
// e.preventDefault(); //关闭浏览器快捷键
this.keyEvent = { ctrlKey: "", keyCode: 0 };
};
},
methods: {
setData(data) {
this.itemObj = {};
if (data.hash) {
let tableData = fileImgMapSuffix(data.children || []);
let keyFiles = this.$store.state.keyFiles;
tableData.forEach((item) => {
// 文件剪切时是否虚化
item.isVirtualization = false;
if (keyFiles.length > 0) {
keyFiles.forEach((each) => {
if (
this.copyInfo.menuType === "shear" &&
each.hash == item.hash
) {
item.isVirtualization = true;
}
});
}
item.formatSize = formatBytes(item.size);
item.fileId = getRequestId(8);
item.isSelect = false;
});
this.tableData = this.handleSortFiles({
type: window.sessionStorage.getItem("file-order-type") || "date",
sort: window.sessionStorage.getItem("file-order-sort") || "asc",
content: tableData,
});
}
this.$parent.childrenInfo = data;
this.handleItemClick(data);
},
handleSortFiles({ type, sort, content }) {
let data = cloneDeep(content || this.tableData);
// console.log(type);
let files = [];
let folder = [];
data.forEach((item) => {
if (item.mime == "directory") {
folder.push(item);
} else {
files.push(item);
}
});
// 按时间排序
if (type == "date") {
files.sort(function (a, b) {
if (sort == "asc") {
return a.ts - b.ts;
} else {
return b.ts - a.ts;
}
});
folder.sort(function (a, b) {
if (sort == "asc") {
return a.ts - b.ts;
} else {
return b.ts - a.ts;
}
});
if (sort == "asc") {
return [...folder, ...files];
} else {
return [...files, ...folder];
}
} else if (type == "name") {
let sortFile = (data) => {
let chineseChars = [],
chars = [];
data.forEach((item) => {
// 判断是否为中文
if (/^[\u4e00-\u9fa5]*$/.test(item.name.charAt(0))) {
chineseChars.push(item); // 姓名首字符为中文的
} else {
chars.push(item); // 姓名首字符非中文的(字母,数字)
}
});
chars.sort((a, b) => {
if (sort == "asc") {
return a.name.charCodeAt(0) - b.name.charCodeAt(0);
} else {
return b.name.charCodeAt(0) - a.name.charCodeAt(0);
}
});
chineseChars.sort((a, b) => {
if (sort == "asc") {
return a.name.localeCompare(b.name);
} else {
return b.name.localeCompare(a.name);
}
});
return chars.concat(chineseChars); // list为最终想要的数组
};
// return [...sortFile(folder), ...sortFile(files)];
if (sort == "asc") {
return [...sortFile(folder), ...sortFile(files)];
} else {
return [...sortFile(files), ...sortFile(folder)];
}
} else if (type == "size") {
files.sort(function (a, b) {
if (sort == "asc") {
return a.size - b.size;
} else {
return b.size - a.size;
}
});
folder.sort(function (a, b) {
if (sort == "asc") {
return a.size - b.size;
} else {
return b.size - a.size;
}
});
if (sort == "asc") {
return [...folder, ...files];
} else {
return [...files, ...folder];
}
}
return content;
},
handleIsSelect(file = [], isSelect = true) {
// if (file) {
let arrHash = file.map((item) => item.hash) || [];
this.tableData.forEach((item) => {
item.isSelect = false;
if (arrHash.indexOf(item.hash) > -1) {
item.isSelect = isSelect;
}
});
},
// 监听键盘事件
handleKeyUp(e, name) {
if (this.isDownESC) {
this.isDownESC = false;
return;
}
if (e.keyCode == 13) {
this.handleOnBlur(name);
this.handleRename({});
}
},
handleOnBlur(rename) {
// console.log(rename);
this.isReName = false;
if (!rename) {
return;
}
if (rename.slice(0, 1) == ".") {
this.$mitt.emit("confirm-modal", {
type: "info",
title: "系统提示",
content: "暂不支持以”.“开头的文件名称!",
});
return;
}
if (this.isDownESC) {
this.isDownESC = false;
return;
}
if (this.createType == "mkdir" || this.createType == "mkfile") {
this.$emit("created-file-folder", {
name: rename,
type: this.createType,
});
} else if (this.createType == "rename" && this.itemObj.name !== rename) {
this.$emit("rename-file-folder", rename);
} else {
this.handleRename({});
}
},
createFolderFile(row, type) {
this.handleRename(row, type);
},
handleClickWarp(row) {
if (!this.mouseState.isMove) {
// this.$store.commit("setIsShowFileMetadata", false);
this.$parent.childrenInfo = row;
this.handleItemClick(row);
}
},
handleItemClick(row = {}, mouseClickType) {
if (mouseClickType) {
this.$store.commit("setMouseClickType", "click");
}
this.$store.commit(
"setIsShowFileMetadata",
row && row.mime !== "directory"
);
if (
!(
this.copyInfo.menuType == "shear" ||
this.copyInfo.menuType == "duplicate"
)
) {
if (
this.keyEvent &&
this.keyEvent.ctrlKey &&
this.keyEvent.keyCode == 17
) {
this.keyFiles.push(row);
} else {
this.keyFiles = [row];
this.$parent.childrenInfo = row;
if (row.hash !== this.itemObj.hash) {
this.handleRename();
}
}
this.handleIsSelect(this.keyFiles, true);
this.$store.commit("setKeyFiles", this.keyFiles);
}
},
handleRename(row = {}, type) {
this.isReName = true;
if (row.hash) {
this.$parent.childrenInfo = row;
// this.active = row.hash;
this.handleIsSelect([row], true);
}
this.itemObj = row;
this.createType = type;
this.tableData.forEach((item) => {
if (item.hash === row.hash) {
item.reName = row.name;
item.isInput = true;
this.$nextTick(() => {
let dom = document.querySelector(`#input_${item.hash}`);
dom.focus();
let lastIndexOf = row.name.length;
if (row.mime != "directory") {
lastIndexOf = row.name.lastIndexOf(".");
}
this.setInputSelection(dom, 0, lastIndexOf);
}, 500);
} else {
item.reName = "";
item.isInput = false;
}
});
},
handleDblClick(row) {
if (row.mime === "directory") {
this.$emit("on-dblclick", { data: row, isDblClick: true });
}
},
handleContextmenu({ event, data, type }) {
this.$store.commit("setMouseClickType", "contextmenu");
this.$store.commit("setIsShowFileMetadata", false);
let keyFiles = this.$store.state.keyFiles;
if (keyFiles.length <= 1) {
this.handleItemClick(data);
}
this.$emit("on-contextmenu", { event, data, type });
// this.active = data.hash;
},
// 选中input中部分文字
setInputSelection(input, startPos, endPos) {
input.focus();
if (typeof input.selectionStart != "undefined") {
input.selectionStart = startPos;
input.selectionEnd = endPos;
} else if (document.selection && document.selection.createRange) {
// IE branch
input.select();
var range = document.selection.createRange();
range.collapse(true);
range.moveEnd("character", endPos);
range.moveStart("character", startPos);
range.select();
}
},
/********* 鼠标框选事件*********************/
//鼠标左键按下方法
onmousedownClick(event) {
this.selectIdList = [];
this.mouseState.isDown = true;
this.start_x = event.clientX;
this.start_y = event.clientY;
},
onmouseMove(event) {
if (!this.mouseState.isDown) return;
this.mouseState.isMove = true;
let end_x = event.clientX;
let end_y = event.clientY;
let that = this;
let divElement = document.getElementById("rectangular");
divElement.style.display = "block";
divElement.className = "rectangular";
if (that.start_x < end_x) {
divElement.style.width = end_x - that.start_x + "px";
divElement.style.height =
(that.start_y > end_y > 0
? that.start_y - end_y
: end_y - that.start_y) + "px";
divElement.style.left = that.start_x + "px";
divElement.style.right = end_x + "px";
//从下往上
if (that.start_y > end_y) {
divElement.style.top = end_y + "px";
divElement.style.bottom = that.start_y + "px";
} else {
divElement.style.top = that.start_y + "px";
divElement.style.bottom = end_y + "px";
}
} else {
divElement.style.width = that.start_x - end_x + "px";
divElement.style.height =
(that.start_y > end_y > 0
? that.start_y - end_y
: end_y - that.start_y) + "px";
divElement.style.left = end_x + "px";
divElement.style.right = that.start_x + "px";
//从下往上
if (that.start_y > end_y) {
divElement.style.top = end_y + "px";
divElement.style.bottom = that.start_y + "px";
} else {
divElement.style.top = that.start_y + "px";
divElement.style.bottom = end_y + "px";
}
}
},
//鼠标左键抬起方法
handMouseUp(event) {
let { isMove, isDown } = this.mouseState;
if (isDown && !isMove) {
this.mouseState.isDown = false;
}
// 移动样式清空
let divElement = document.getElementById("rectangular");
divElement.style.display = "none";
if (isMove && isDown) {
this.mouseState.isDown = false;
setTimeout(() => {
this.mouseState.isMove = false;
}, 300);
this.end_x = event.clientX;
this.end_y = event.clientY;
let files = document.getElementsByName("file");
let a = 0;
for (let i = 0; i < files.length; i++) {
const file = files[i];
let id = file.id;
// console.log(id);
let el = file.getBoundingClientRect();
let top = el.top; //上边界
let left = el.left; //左边界
let right = el.right; //右边界
let bottom = el.bottom; //下边界
//原地点击
if (this.start_x == this.end_x) {
if (
!(
this.end_x >= left &&
this.end_x <= right &&
this.end_y > top &&
this.end_y < bottom
)
) {
a += 1;
}
} else {
//从左往右
if (this.start_x < this.end_x) {
//从下到上
if (this.start_y > this.end_y) {
if (top < this.start_y && bottom > this.end_y) {
if (left < this.end_x && right > this.start_x) {
if (this.selectIdList.indexOf(id) === -1) {
this.selectIdList.push(id);
// file.className = file.className + " ctrlClick";
// file.style = "border: 1px solid rgba(24, 135, 243, 0.5)";
}
}
}
}
//从上到下
else {
if (top < this.end_y && bottom > this.start_y) {
if (left < this.end_x && right > this.start_x) {
if (this.selectIdList.indexOf(id) === -1) {
this.selectIdList.push(id);
// file.className = file.className + " ctrlClick";
// file.style = "border: 1px solid rgba(24, 135, 243, 0.5)";
}
}
}
}
//从右往左
} else {
//从下到上
if (this.start_y > this.end_y) {
if (top < this.start_y && bottom > this.end_y) {
if (left < this.start_x && right > this.end_x) {
if (this.selectIdList.indexOf(id) === -1) {
this.selectIdList.push(id);
// file.className = file.className + " ctrlClick";
// file.style = "border: 1px solid rgba(24, 135, 243, 0.5)";
}
}
}
}
//从上到下
else {
if (top < this.end_y && bottom > this.start_y) {
if (left < this.start_x && right > this.end_x) {
if (this.selectIdList.indexOf(id) === -1) {
this.selectIdList.push(id);
// file.className = file.className + " ctrlClick";
// file.style = "border: 1px solid rgba(24, 135, 243, 0.5)";
}
}
}
}
}
}
}
this.keyFiles = this.tableData.filter(
(item) => this.selectIdList.indexOf(item.hash) > -1
);
this.handleIsSelect(this.keyFiles, true);
this.$store.commit("setKeyFiles", this.keyFiles);
}
},
},
};
</script>
<style lang="scss" scoped>
.file-table {
height: 100%;
outline: none;
#rectangular {
background-color: rgba(235, 239, 243, 0.45);
position: fixed;
border: 1px solid rgba(24, 135, 243, 1);
z-index: 20;
}
ul {
display: flex;
// padding: 14px 0 0 14px;
padding: 20px;
flex-wrap: wrap;
li {
width: 128px;
// max-height: 150px;
position: relative;
vertical-align: top;
display: inline-block;
text-align: center;
word-wrap: break-word;
overflow: hidden;
border: 1px solid transparent;
padding: 15px;
margin-right: 5px;
margin-bottom: 5px;
border-radius: 6px;
font-size: 12px;
user-select: none;
&.cursor {
cursor: pointer;
}
&.mousehover {
&:hover {
background-color: #f1f4f9;
// border: 1px solid rgba(0, 140, 255, 0.3);
}
}
// &:hover,
&.active {
background-color: #f1f4f9;
// border: 1px solid rgba(0, 140, 255, 0.3);
}
&.isVirtualization {
opacity: 0.5;
}
.name {
max-width: 90px;
line-height: 20px;
text-align: center;
max-height: 58px;
text-overflow: -o-ellipsis-lastline;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 3;
line-clamp: 3;
-webkit-box-orient: vertical;
}
}
}
.edit {
max-width: 90px;
input {
width: 100%;
padding: 2px 3px;
line-height: 12px;
border: 1px solid #ccc;
border-radius: 3px;
&:focus {
outline: none;
}
}
}
.no-file-warp {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
p {
font-size: 16px;
color: #afb6bc;
}
}
.icon-warp {
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
.img-viewer {
max-width: 85px;
}
img {
display: block;
}
}
.block-box {
.icon-warp {
width: 85px;
height: 62px;
}
img {
width: 40px;
}
}
// table
.name-warp {
display: flex;
.icon-warp {
margin-right: 5px;
}
img {
width: 16px;
}
}
}
</style>
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
# 效果图
# 核心代码
# html代码
<div @mousedown.left="onmousedownClick" @mouseup="handMouseUp">
<ul class="block-box">
<li
v-for="item in tableData"
:key="item.hash + '_' + item.fileId + item.isSelect"
:id="item.hash"
:title="item.name"
name="file"
>
<p class="name">{{ item.name }}</p>
</li>
</ul>
<div id="rectangular"></div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# css代码
.rectangular{
background-color: rgba(235, 239, 243,0.45);
position: fixed;
border: 1px solid rgba(24, 135, 243,1);
z-index: 20;
}
1
2
3
4
5
6
2
3
4
5
6
# js代码
//以下四个方法都是关于多选的
//按住ctrl多选
keyDownCtrl(event, item) {
//获取点击对象
let div = event.currentTarget;
let orginClassName = div.className;
this.isRightClickCtrl = true;
if (this.selectIdList.indexOf(item.fileId) == -1) {
this.selectIdList.push(item.fileId);
div.className = orginClassName + " " + "ctrlClick"
div.style = 'border: 1px solid rgba(24, 135, 243, 0.5)'
}
},
//还原样式
setFileDivClass() {
let files = document.getElementsByName("file");
for (let i = 0; i < files.length; i++) {
const file = files[i];
file.style="";
file.className = this.selectDivClassName;
}
},
//鼠标左键按下方法
onmousedownClick(event) {
this.isRightClick = true;
this.start_x = event.clientX;
this.start_y = event.clientY;
console.log(" this.start_x="+ this.start_x);
console.log(" this.start_y="+ this.start_y);
let that=this;
document.onmousemove=function (event) {
let end_x=event.clientX;
let end_y=event.clientY;
let divElement=document.getElementById("rectangular");
divElement.style.display='block'
divElement.className='rectangular';
//从左往右
// 画矩形
if(that.start_x<end_x){
divElement.style.width=(end_x-that.start_x)+"px";
divElement.style.height=(that.start_y>end_y>0?(that.start_y-end_y):(end_y-that.start_y))+"px";
divElement.style.left=that.start_x+"px";
divElement.style.right=end_x+"px";
//从下往上
if(that.start_y>end_y){
divElement.style.top=end_y+"px";
divElement.style.bottom=that.start_y+"px";
}else{
divElement.style.top=that.start_y+"px";
divElement.style.bottom=end_y+"px";
}
}else{
divElement.style.width=(that.start_x-end_x)+"px";
divElement.style.height=(that.start_y>end_y>0?(that.start_y-end_y):(end_y-that.start_y))+"px";
divElement.style.left=end_x+"px";
divElement.style.right=that.start_x+"px";
//从下往上
if(that.start_y>end_y){
divElement.style.top=end_y+"px";
divElement.style.bottom=that.start_y+"px";
}else{
divElement.style.top=that.start_y+"px";
divElement.style.bottom=end_y+"px";
}
}
}
document.onmouseup=function () {
//禁用鼠标移动时间
document.onmousemove=function () {
return false;
}
// 移动样式清空
let divElement=document.getElementById("rectangular");
divElement.style.display='none';
}
},
//鼠标左键抬起方法
handMouseUp: function (event) {
this.end_x = event.clientX;
this.end_y = event.clientY;
console.log(" this.end_x=" + this.end_x);
console.log(" this.end_y=" + this.end_y);
//核心内容,根据你的鼠标移动矩形区域来判断div是否在里面
this.isRightClick = false;
let files = document.getElementsByName("file");
let a = 0;
for (let i = 0; i < files.length; i++) {
const file = files[i];
let id = file.id;
let el = file.getBoundingClientRect();
let top = el.top //上边界
let left = el.left;//左边界
let right = el.right;//右边界
let bottom = el.bottom;//下边界
//原地点击
if (this.start_x == this.end_x) {
if (!(this.end_x >= left && this.end_x <= right && this.end_y > top && this.end_y < bottom)) {
a += 1;
}
} else {
//从左往右
if (this.start_x < this.end_x) {
//从下到上
if (this.start_y > this.end_y) {
if (top < this.start_y && bottom > this.end_y) {
if(left<this.end_x&&right>this.start_x){
if (this.selectIdList.indexOf(id) === -1) {
this.selectIdList.push(id);
file.className = file.className + " ctrlClick";
file.style = 'border: 1px solid rgba(24, 135, 243, 0.5)'
}
}
}
}
//从上到下
else{
if (top < this.end_y && bottom > this.start_y) {
if(left<this.end_x&&right>this.start_x){
if (this.selectIdList.indexOf(id) === -1) {
this.selectIdList.push(id);
file.className = file.className + " ctrlClick";
file.style = 'border: 1px solid rgba(24, 135, 243, 0.5)'
}
}
}
}
//从右往左
}else{
//从下到上
if (this.start_y > this.end_y) {
if (top < this.start_y && bottom > this.end_y) {
if(left<this.start_x&&right>this.end_x){
if (this.selectIdList.indexOf(id) === -1) {
this.selectIdList.push(id);
file.className = file.className + " ctrlClick";
file.style = 'border: 1px solid rgba(24, 135, 243, 0.5)'
}
}
}
}
//从上到下
else{
if (top < this.end_y && bottom > this.start_y) {
if(left<this.start_x&&right>this.end_x){
if (this.selectIdList.indexOf(id) === -1) {
this.selectIdList.push(id);
file.className = file.className + " ctrlClick";
file.style = 'border: 1px solid rgba(24, 135, 243, 0.5)'
}
}
}
}
}
}
}
//如果点击空白处
if (a == files.length) {
this.selectIdList = [];
this.setFileDivClass();
}
}
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
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
上次更新: 2024/01/30, 00:35:17
- 02
- Node与GLIBC_2.27不兼容解决方案08-19
- 03
- Git清空本地文件跟踪缓存08-13