/
Images.vue
309 lines (276 loc) · 7.38 KB
/
Images.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
<!--
- @copyright Copyright (c) 2019 John Molakvoæ <skjnldsv@protonmail.com>
-
- @author John Molakvoæ <skjnldsv@protonmail.com>
-
- @license AGPL-3.0-or-later
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-->
<template>
<ImageEditor v-if="editing"
:mime="mime"
:src="src"
:fileid="fileid"
@close="onClose" />
<img v-else-if="data !== null"
:alt="alt"
:class="{
dragging,
loaded,
zoomed: zoomRatio !== 1
}"
:src="data"
:style="{
marginTop: Math.round(shiftY * 2) + 'px',
marginLeft: Math.round(shiftX * 2) + 'px',
height: zoomHeight + 'px',
width: zoomWidth + 'px',
}"
@error.capture.prevent.stop.once="onFail"
@load="updateImgSize"
@wheel="updateZoom"
@dblclick.prevent="onDblclick"
@mousedown.prevent="dragStart">
</template>
<script>
import axios from '@nextcloud/axios'
import Vue from 'vue'
import AsyncComputed from 'vue-async-computed'
import ImageEditor from './ImageEditor.vue'
import { basename } from '@nextcloud/paths'
Vue.use(AsyncComputed)
export default {
name: 'Images',
components: {
ImageEditor,
},
props: {
canZoom: {
type: Boolean,
default: false,
},
editing: {
type: Boolean,
default: false,
},
},
data() {
return {
dragging: false,
shiftX: 0,
shiftY: 0,
zoomRatio: 1,
fallback: false,
}
},
computed: {
src() {
return this.source ?? this.davPath
},
zoomHeight() {
return Math.round(this.height * this.zoomRatio)
},
zoomWidth() {
return Math.round(this.width * this.zoomRatio)
},
alt() {
return this.basename
},
},
asyncComputed: {
data() {
// Avoid svg xss attack vector
if (this.mime === 'image/svg+xml') {
return this.getBase64FromImage()
}
// Load the raw gif instead of the static preview
if (this.mime === 'image/gif') {
return this.src
}
// If there is no preview and we have a direct source
// load it instead
if (this.source && !this.hasPreview && !this.previewUrl) {
return this.source
}
// If loading the preview failed once, let's load the original file
if (this.fallback) {
return this.src
}
return this.previewPath
},
},
watch: {
active(val, old) {
// the item was hidden before and is now the current view
if (val === true && old === false) {
this.resetZoom()
// end the dragging if your mouse go out of the content
window.addEventListener('mouseout', this.dragEnd)
// the item is not displayed
} else if (val === false) {
window.removeEventListener('mouseout', this.dragEnd)
}
},
},
methods: {
// Updates the dimensions of the modal
updateImgSize() {
this.naturalHeight = this.$el.naturalHeight
this.naturalWidth = this.$el.naturalWidth
this.updateHeightWidth()
this.doneLoading()
},
/**
* Manually retrieve the path and return its base64
*
* @return {string}
*/
async getBase64FromImage() {
const file = await axios.get(this.src)
return `data:${this.mime};base64,${btoa(file.data)}`
},
/**
* Handle zooming
*
* @param {Event} event the scroll event
* @return {null}
*/
updateZoom(event) {
if (!this.canZoom) {
return
}
event.stopPropagation()
event.preventDefault()
// scrolling position relative to the image
const scrollX = event.clientX - this.$el.x - (this.width * this.zoomRatio / 2)
const scrollY = event.clientY - this.$el.y - (this.height * this.zoomRatio / 2)
const scrollPercX = scrollX / (this.width * this.zoomRatio)
const scrollPercY = scrollY / (this.height * this.zoomRatio)
const isZoomIn = event.deltaY < 0
const newZoomRatio = isZoomIn
? Math.min(this.zoomRatio + 0.1, 5) // prevent too big zoom
: Math.max(this.zoomRatio - 0.1, 1) // prevent too small zoom
// do not continue, img is back to its original state
if (newZoomRatio === 1) {
return this.resetZoom()
}
// calc how much the img grow from its current size
// and adjust the margin accordingly
const growX = this.width * newZoomRatio - this.width * this.zoomRatio
const growY = this.height * newZoomRatio - this.height * this.zoomRatio
// compensate for existing margins
this.disableSwipe()
this.shiftX = this.shiftX - scrollPercX * growX
this.shiftY = this.shiftY - scrollPercY * growY
this.zoomRatio = newZoomRatio
},
resetZoom() {
this.enableSwipe()
this.zoomRatio = 1
this.shiftX = 0
this.shiftY = 0
},
/**
* Dragging handlers
*
* @param {Event} event the event
*/
dragStart(event) {
const { pageX, pageY } = event
this.dragX = pageX
this.dragY = pageY
this.dragging = true
this.$el.onmouseup = this.dragEnd
this.$el.onmousemove = this.dragHandler
},
dragEnd(event) {
event.preventDefault()
this.dragging = false
this.$el.onmouseup = null
this.$el.onmousemove = null
},
dragHandler(event) {
event.preventDefault()
const { pageX, pageY } = event
if (this.dragging && this.zoomRatio > 1 && pageX > 0 && pageY > 0) {
const moveX = this.shiftX + (pageX - this.dragX)
const moveY = this.shiftY + (pageY - this.dragY)
const growX = this.zoomWidth - this.width
const growY = this.zoomHeight - this.height
this.shiftX = Math.min(Math.max(moveX, -growX / 2), growX / 2)
this.shiftY = Math.min(Math.max(moveY, -growY / 2), growY / 2)
this.dragX = pageX
this.dragY = pageY
}
},
onDblclick() {
if (this.zoomRatio > 1) {
this.resetZoom()
} else {
this.zoomRatio = 1.3
}
},
onClose() {
this.$emit('update:editing', false)
},
// Fallback to the original image if not already done
onFail(event) {
if (!this.fallback) {
console.error(`Loading of file preview ${basename(this.src)} failed, falling back to original file`)
this.fallback = true
}
},
},
}
</script>
<style scoped lang="scss">
$checkered-size: 8px;
$checkered-color: #efefef;
img {
max-width: 100%;
max-height: 100%;
align-self: center;
justify-self: center;
// black while loading
background-color: #000;
// disable animations during zooming/resize
transition: none !important;
// show checkered bg on hover if not currently zooming (but ok if zoomed)
&:hover {
background-image: linear-gradient(45deg, #{$checkered-color} 25%, transparent 25%),
linear-gradient(45deg, transparent 75%, #{$checkered-color} 75%),
linear-gradient(45deg, transparent 75%, #{$checkered-color} 75%),
linear-gradient(45deg, #{$checkered-color} 25%, #fff 25%);
background-size: 2 * $checkered-size 2 * $checkered-size;
background-position: 0 0, 0 0, -#{$checkered-size} -#{$checkered-size}, $checkered-size $checkered-size;
}
&.loaded {
// white once done loading
background-color: #fff;
}
&.zoomed {
position: absolute;
max-height: none;
max-width: none;
z-index: 10010;
cursor: move;
}
&.dragging {
transition: none !important;
cursor: move;
}
}
</style>