-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNodesPicker.vue
313 lines (274 loc) · 7.8 KB
/
NodesPicker.vue
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
<template>
<fieldset class="node-picker__wrapper" :data-cy-conflict-picker-fieldset="existing.basename">
<legend>{{ existing.basename }}</legend>
<!-- Incoming file -->
<NcCheckboxRadioSwitch :checked="isChecked(incoming, newSelected)"
:required="!isEnoughSelected"
:data-cy-conflict-picker-input-incoming="existing.basename"
@update:checked="onUpdateIncomingChecked">
<span class="node-picker node-picker--incoming">
<!-- Icon or preview -->
<FileSvg v-if="!incomingPreview" class="node-picker__icon" :size="48" />
<img v-else
class="node-picker__preview"
:src="incomingPreview"
:alt="t('Preview image')"
loading="lazy">
<!-- Description -->
<span class="node-picker__desc">
<span class="node-picker__name">{{ t('New version') }}</span>
<span class="node-picker__mtime">{{ lastModified(incoming) }}</span>
<span class="node-picker__size">{{ size(incoming) }}</span>
</span>
</span>
</NcCheckboxRadioSwitch>
<!-- Existing file -->
<NcCheckboxRadioSwitch :checked="isChecked(existing, oldSelected)"
:required="!isEnoughSelected"
:data-cy-conflict-picker-input-existing="existing.basename"
@update:checked="onUpdateExistingChecked">
<span class="node-picker node-picker--existing">
<!-- Icon or preview -->
<FileSvg v-if="!existingPreview" class="node-picker__icon" :size="48" />
<img v-else
class="node-picker__preview"
:src="existingPreview"
:alt="t('Preview image')"
loading="lazy">
<!-- Description -->
<span class="node-picker__desc">
<span class="node-picker__name">{{ t('Existing version') }}</span>
<span class="node-picker__mtime">{{ lastModified(existing) }}</span>
<span class="node-picker__size">{{ size(existing) }}</span>
</span>
</span>
</NcCheckboxRadioSwitch>
</fieldset>
</template>
<script lang="ts">
import type { PropType } from 'vue'
import { File as NcFile, Folder, formatFileSize, FileType, Node } from '@nextcloud/files'
import { generateUrl } from '@nextcloud/router'
import moment from 'moment'
import Vue from 'vue'
import FileSvg from 'vue-material-design-icons/File.vue'
import NcCheckboxRadioSwitch from '@nextcloud/vue/dist/Components/NcCheckboxRadioSwitch.js'
import { t } from '../utils/l10n.ts'
const PREVIEW_SIZE = 64
const timings = [] as number[]
export default Vue.extend({
name: 'NodesPicker',
components: {
FileSvg,
NcCheckboxRadioSwitch,
},
props: {
incoming: {
type: [Node, File, NcFile, Folder],
required: true,
},
existing: {
type: [NcFile, Folder],
required: true,
},
newSelected: {
type: Array as PropType<(File|Node)[]>,
required: true,
},
oldSelected: {
type: Array as PropType<Node[]>,
required: true,
},
},
data() {
return {
asyncPreview: null,
}
},
computed: {
/**
* Whether the incoming or existing file is selected.
* This is used by the parent component to ensure
* that the user has selected at least one of the two files.
*/
isEnoughSelected(): boolean {
return this.isChecked(this.incoming, this.newSelected)
|| this.isChecked(this.existing, this.oldSelected)
},
incomingPreview() {
// If we generated a preview image, use it
if (this.asyncPreview) {
return this.asyncPreview
}
return this.previewUrl(this.incoming)
},
existingPreview() {
return this.previewUrl(this.existing)
},
},
methods: {
lastModified(node: File|Node): string {
const lastModified = node instanceof File
? new Date(node.lastModified)
: node.mtime
if (lastModified) {
return moment(lastModified).format('LLL')
}
return t('Last modified date unknown')
},
size(node: File|Node): string {
if (node.size) {
return formatFileSize(node.size, true)
}
return t('Unknown size')
},
previewUrl(node: File|Node) {
if (node instanceof File) {
this.previewImage(node).then((url: string) => {
const avg = timings.reduce((a, b) => a + b, 0) / timings.length
console.debug(`Generating previewImage took ${avg} milliseconds.`)
this.asyncPreview = url
})
return
}
if (node.type === FileType.Folder) {
return null
}
try {
const previewUrl = node.attributes.previewUrl
|| generateUrl('/core/preview?fileId={fileid}', {
fileid: node.fileid,
})
const url = new URL(window.location.origin + previewUrl)
// Request tiny previews
url.searchParams.set('x', PREVIEW_SIZE.toString())
url.searchParams.set('y', PREVIEW_SIZE.toString())
url.searchParams.set('mimeFallback', 'true')
return url.href
} catch (e) {
return null
}
},
isChecked(node: Node, selected: Node[]): boolean {
return selected.includes(node)
},
onUpdateChecked(node: Node, selection: string): void {
this.$emit(`update:${selection}`, ...args)
},
onUpdateIncomingChecked(checked) {
if (checked) {
this.$emit('update:newSelected', [this.incoming, ...this.newSelected])
} else {
this.$emit('update:newSelected', this.newSelected.filter((node) => node !== this.incoming))
}
},
onUpdateExistingChecked(checked) {
if (checked) {
this.$emit('update:oldSelected', [this.existing, ...this.oldSelected])
} else {
this.$emit('update:oldSelected', this.oldSelected.filter((node) => node !== this.existing))
}
},
/**
* Get the preview Image of a file
* @param file the soon-to-be-uploaded File
*/
async previewImage(file: File): Promise<string|null> {
return new Promise((resolve) => {
if (file.type.startsWith('image/')) {
const reader = new FileReader()
reader.onload = async (e) => {
const result = e?.target?.result
const t0 = performance.now()
if (typeof result === 'string') {
const img = document.createElement('img')
img.onload = async () => {
const url = await this.resizeImageWithCanvas(img)
const t1 = performance.now()
timings.push(t1 - t0)
resolve(url)
}
img.onerror = () => {
resolve(null)
}
img.src = result
return
}
if (result instanceof ArrayBuffer) {
const blob = new Blob([result], { type: file.type })
const url = URL.createObjectURL(blob)
const t1 = performance.now()
timings.push(t1 - t0)
resolve(url)
return
}
resolve(null)
}
reader.readAsArrayBuffer(file)
}
})
},
async resizeImageWithCanvas(image: HTMLImageElement): Promise<string|null> {
return new Promise((resolve) => {
const width = image.width
const height = image.height
// Calc scale up factor
const f = height < width ? PREVIEW_SIZE / height : PREVIEW_SIZE / width
try {
// Create your canvas
const canvas = document.createElement('canvas')
canvas.height = canvas.width = PREVIEW_SIZE
const ctx = canvas.getContext('2d')
const posX = (width * f - PREVIEW_SIZE) / 2 * -1
const posY = (height * f - PREVIEW_SIZE) / 2 * -1
ctx.drawImage(image, posX, posY, width * f, height * f)
canvas.toBlob((blob) => {
const url = URL.createObjectURL(blob)
resolve(url)
})
} catch (e) {
resolve(null)
}
})
},
t,
},
})
</script>
<style lang="scss" scoped>
$height: 64px;
.node-picker__wrapper {
// last fieldset does not have a border
&:not(:last-of-type) {
border-bottom: 1px solid var(--color-border);
}
}
.node-picker {
display: flex;
align-items: center;
height: $height;
&__icon, &__preview {
height: $height;
width: $height;
margin: 0 var(--secondary-margin);
display: block;
flex: 0 0 $height;
}
&__icon {
color: var(--color-text-maxcontrast);
}
&__preview {
overflow: hidden;
border-radius: calc(var(--border-radius) * 2);
background-position: center;
background-size: cover;
}
&__desc {
display: flex;
flex-direction: column;
span {
white-space: nowrap;
}
}
}
</style>