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
|
<!--
- SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<div class="files-list-drag-image">
<span class="files-list-drag-image__icon">
<span ref="previewImg" />
<FolderIcon v-if="isSingleFolder" />
<FileMultipleIcon v-else />
</span>
<span class="files-list-drag-image__name">{{ name }}</span>
</div>
</template>
<script lang="ts">
import { FileType, Node, formatFileSize } from '@nextcloud/files'
import Vue from 'vue'
import FileMultipleIcon from 'vue-material-design-icons/FileMultiple.vue'
import FolderIcon from 'vue-material-design-icons/Folder.vue'
import { getSummaryFor } from '../utils/fileUtils.ts'
export default Vue.extend({
name: 'DragAndDropPreview',
components: {
FileMultipleIcon,
FolderIcon,
},
data() {
return {
nodes: [] as Node[],
}
},
computed: {
isSingleNode() {
return this.nodes.length === 1
},
isSingleFolder() {
return this.isSingleNode
&& this.nodes[0].type === FileType.Folder
},
name() {
if (!this.size) {
return this.summary
}
return `${this.summary} – ${this.size}`
},
size() {
const totalSize = this.nodes.reduce((total, node) => total + node.size || 0, 0)
const size = parseInt(totalSize, 10) || 0
if (typeof size !== 'number' || size < 0) {
return null
}
return formatFileSize(size, true)
},
summary(): string {
if (this.isSingleNode) {
const node = this.nodes[0]
return node.attributes?.displayname || node.basename
}
return getSummaryFor(this.nodes)
},
},
methods: {
update(nodes: Node[]) {
this.nodes = nodes
this.$refs.previewImg.replaceChildren()
// Clone icon node from the list
nodes.slice(0, 3).forEach(node => {
const preview = document.querySelector(`[data-cy-files-list-row-fileid="${node.fileid}"] .files-list__row-icon img`)
if (preview) {
const previewElmt = this.$refs.previewImg as HTMLElement
previewElmt.appendChild(preview.parentNode.cloneNode(true))
}
})
this.$nextTick(() => {
this.$emit('loaded', this.$el)
})
},
},
})
</script>
<style lang="scss">
$size: 32px;
$stack-shift: 6px;
.files-list-drag-image {
position: absolute;
top: -9999px;
left: -9999px;
display: flex;
overflow: hidden;
align-items: center;
height: 44px;
padding: 6px 12px;
background: var(--color-main-background);
&__icon,
.files-list__row-icon {
display: flex;
overflow: hidden;
align-items: center;
justify-content: center;
width: 32px;
height: 32px;
border-radius: var(--border-radius);
}
&__icon {
overflow: visible;
margin-right: 12px;
img {
max-width: 100%;
max-height: 100%;
}
.material-design-icon {
color: var(--color-text-maxcontrast);
&.folder-icon {
color: var(--color-primary-element);
}
}
// Previews container
> span {
display: flex;
// Stack effect if more than one element
.files-list__row-icon + .files-list__row-icon {
margin-top: $stack-shift;
margin-left: $stack-shift - $size;
& + .files-list__row-icon {
margin-top: $stack-shift * 2;
}
}
// If we have manually clone the preview,
// let's hide any fallback icons
&:not(:empty) + * {
display: none;
}
}
}
&__name {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
}
</style>
|