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
|
<!--
- @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
-
- @author Julius Härtl <jus@bitgrid.net>
-
- @license GNU AGPL version 3 or any later version
-
- 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>
<div class="image" :class="{'icon-loading': !loaded}" :data-src="src">
<div v-if="imageLoaded && isSupportedImage"
v-click-outside="() => showIcons = false"
class="image__view"
@click="showIcons = true"
@mouseover="showIcons = true"
@mouseleave="showIcons = false">
<transition name="fade">
<img v-show="loaded"
:src="imageUrl"
class="image__main"
@load="onLoaded">
</transition>
<transition name="fade">
<div v-show="loaded" class="image__caption">
<input ref="altInput"
type="text"
:value="alt"
@keyup.enter="updateAlt()">
<div
v-if="showIcons"
class="trash-icon"
title="Delete this image"
@click="deleteImage">
<TrashCanIcon />
</div>
</div>
</transition>
</div>
<div v-else>
<transition name="fade">
<div v-show="loaded">
<a :href="internalLinkOrImage" target="_blank">
<span v-if="!isSupportedImage">{{ alt }}</span>
</a>
</div>
</transition>
<transition v-if="isSupportedImage" name="fade">
<div v-show="loaded" class="image__caption">
<input ref="altInput"
type="text"
:value="alt"
@keyup.enter="updateAlt()">
</div>
</transition>
</div>
</div>
</template>
<script>
import path from 'path'
import { generateUrl, generateRemoteUrl } from '@nextcloud/router'
import { getCurrentUser } from '@nextcloud/auth'
import ClickOutside from 'vue-click-outside'
import TrashCanIcon from 'vue-material-design-icons/TrashCan.vue'
import store from './../mixins/store'
const imageMimes = [
'image/png',
'image/jpeg',
'image/jpg',
'image/gif',
'image/x-xbitmap',
'image/x-ms-bmp',
'image/bmp',
'image/svg+xml',
'image/webp',
]
const getQueryVariable = (src, variable) => {
const query = src.split('?')[1]
if (typeof query === 'undefined') {
return
}
const vars = query.split(/[&#]/)
if (typeof vars === 'undefined') {
return
}
for (let i = 0; i < vars.length; i++) {
const pair = vars[i].split('=')
if (decodeURIComponent(pair[0]) === variable) {
return decodeURIComponent(pair[1])
}
}
}
export default {
name: 'ImageView',
components: {
TrashCanIcon,
},
directives: {
ClickOutside,
},
mixins: [
store,
],
props: ['node', 'options', 'updateAttrs', 'view', 'getPos'], // eslint-disable-line
data() {
return {
imageLoaded: false,
loaded: false,
failed: false,
showIcons: false,
}
},
computed: {
currentSession() {
return this.$store.state.currentSession
},
davUrl() {
if (getCurrentUser()) {
const uid = getCurrentUser().uid
const encoded = encodeURI(this.filePath)
return generateRemoteUrl(`dav/files/${uid}${encoded}`)
} else {
return generateUrl('/s/{token}/download?path={dirname}&files={basename}',
{
token: this.token,
dirname: this.options.currentDirectory,
basename: this.basename,
})
}
},
imageUrl() {
if (this.src.startsWith('text://')) {
const documentId = this.currentSession?.documentId
const sessionId = this.currentSession?.id
const sessionToken = this.currentSession?.token
const imageFileName = getQueryVariable(this.src, 'imageFileName')
if (getCurrentUser() || !this.token) {
return generateUrl('/apps/text/image?documentId={documentId}&sessionId={sessionId}&sessionToken={sessionToken}&imageFileName={imageFileName}',
{
documentId,
sessionId,
sessionToken,
imageFileName,
})
} else {
return generateUrl('/apps/text/image?documentId={documentId}&sessionId={sessionId}&sessionToken={sessionToken}&imageFileName={imageFileName}&shareToken={shareToken}',
{
documentId,
sessionId,
sessionToken,
imageFileName,
shareToken: this.token,
})
}
}
if (this.isRemoteUrl || this.isPreviewUrl) {
return this.src
}
if (this.hasPreview && this.mime !== 'image/gif') {
return this.previewUrl
}
return this.davUrl
},
isRemoteUrl() {
return this.src.startsWith('http://')
|| this.src.startsWith('https://')
},
isPreviewUrl() {
return this.src.match(/^(\/index.php)?\/core\/preview/)
|| this.src.match(/^(\/index.php)?\/apps\/files_sharing\/publicpreview\//)
},
basename() {
return decodeURI(this.src.split('?')[0])
},
fileId() {
return getQueryVariable(this.src, 'fileId')
},
filePath() {
const f = [
this.options.currentDirectory,
this.basename,
].join('/')
return path.normalize(f)
},
hasPreview() {
return getQueryVariable(this.src, 'hasPreview') === 'true'
},
previewUrl() {
const fileQuery = (this.fileId)
? `?fileId=${this.fileId}&file=${encodeURIComponent(this.filePath)}`
: `?file=${encodeURIComponent(this.filePath)}`
const query = fileQuery + '&x=1024&y=1024&a=true'
if (getCurrentUser()) {
return generateUrl('/core/preview') + query
} else {
return generateUrl(`/apps/files_sharing/publicpreview/${this.token}${query}`)
}
},
mime() {
return getQueryVariable(this.src, 'mimetype')
},
mimeIcon() {
if (this.mime) {
const mimeIconUrl = window.OC.MimeType.getIconUrl(this.mime)
return { backgroundImage: `url(${mimeIconUrl})` }
}
return {}
},
isSupportedImage() {
return typeof this.mime === 'undefined'
|| imageMimes.indexOf(this.mime) !== -1
},
internalLinkOrImage() {
const fileId = getQueryVariable(this.src, 'fileId')
if (fileId) {
return generateUrl('/f/' + fileId)
}
return this.src
},
src: {
get() {
return this.node.attrs.src || ''
},
set(src) {
this.updateAttrs({
src,
})
},
},
alt: {
get() {
return this.node.attrs.alt ? this.node.attrs.alt : ''
},
set(alt) {
this.updateAttrs({
alt,
})
},
},
t() {
return (a, s) => window.t(a, s)
},
token() {
return document.getElementById('sharingToken')
&& document.getElementById('sharingToken').value
},
},
beforeMount() {
if (!this.isSupportedImage) {
// TODO check if hasPreview and render a file preview if available
this.failed = true
this.imageLoaded = false
this.loaded = true
return
}
const img = new Image()
img.src = this.imageUrl
img.onload = () => {
this.imageLoaded = true
}
img.onerror = () => {
this.failed = true
this.imageLoaded = false
this.loaded = true
}
},
methods: {
updateAlt() {
this.alt = this.$refs.altInput.value
},
onLoaded() {
this.loaded = true
},
deleteImage() {
const tr = this.view.state.tr
const pos = this.getPos()
tr.delete(pos, pos + this.node.nodeSize)
this.view.dispatch(tr)
},
},
}
</script>
<style scoped lang="scss">
.image {
margin: 0;
padding: 0;
}
.image__caption {
text-align: center;
color: var(--color-text-lighter);
display: flex;
align-items: center;
justify-content: center;
input[type='text'] {
max-width: 80%;
border: none;
text-align: center;
}
}
.image__loading {
height: 100px;
}
.image__view {
text-align: center;
position: relative;
img {
max-width: 100%;
}
}
.image__main {
max-height: calc(100vh - 50px - 50px);
}
.fade-enter-active {
transition: opacity .3s ease-in-out;
}
.fade-enter-to {
opacity: 1;
}
.fade-enter {
opacity: 0;
}
.trash-icon {
position: absolute;
right: 0;
display: flex;
justify-content: flex-end;
align-items: center;
svg {
cursor: pointer;
}
}
</style>
|