-
Notifications
You must be signed in to change notification settings - Fork 91
/
ImageView.vue
458 lines (428 loc) Β· 10.6 KB
/
ImageView.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
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
<!--
- SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<NodeViewWrapper :contenteditable="isEditable">
<figure class="image image-view"
data-component="image-view"
:data-attachment-type="attachmentType"
:class="{'icon-loading': !loaded, 'image-view--failed': failed}"
:data-src="src">
<div v-if="canDisplayImage"
v-click-outside="() => showIcons = false"
class="image__view"
@mouseover="showIcons = true"
@mouseleave="showIcons = false">
<transition name="fade">
<template v-if="!failed">
<div v-if="isMediaAttachment"
contenteditable="false"
class="media"
@click="handleAttachmentClick">
<div class="media__wrapper">
<img v-show="loaded"
:src="imageUrl"
:alt="alt"
class="image__main"
@load="onLoaded">
<div class="metadata">
<span class="name">{{ alt }}</span>
<span class="size">{{ attachmentSize }}</span>
</div>
</div>
<div v-if="showDeleteIcon"
class="buttons">
<NcButton :aria-label="t('text', 'Delete this attachment')"
:title="t('text', 'Delete this attachment')"
@click="onDelete">
<template #icon>
<DeleteIcon />
</template>
</NcButton>
</div>
</div>
<div v-else class="media" contenteditable="false">
<img v-show="loaded"
:src="imageUrl"
:alt="alt"
class="image__main"
@click="handleImageClick"
@load="onLoaded">
</div>
</template>
<template v-else>
<ImageIcon class="image__main image__main--broken-icon" :size="100" />
</template>
</transition>
<transition name="fade">
<div v-if="!isMediaAttachment"
v-show="loaded"
class="image__caption"
:title="alt">
<figcaption v-if="!isEditable">
{{ alt }}
</figcaption>
<div v-else class="image__caption__wrapper">
<input v-show="!isMediaAttachment"
ref="altInput"
type="text"
class="image__caption__input"
:value="alt"
@blur="updateAlt"
@keyup="updateAlt">
<div v-if="showImageDeleteIcon"
contenteditable="false"
class="image__caption__delete">
<NcButton :aria-label="t('text', 'Delete this image')"
:title="t('text', 'Delete this image')"
@click="onDelete">
<template #icon>
<DeleteIcon />
</template>
</NcButton>
</div>
</div>
</div>
</transition>
<div class="image__modal">
<ShowImageModal :images="embeddedImageList"
:start-index="imageIndex"
:show="showImageModal"
@close="showImageModal=false" />
</div>
</div>
<div v-else class="image-view__cant_display">
<transition name="fade">
<div v-show="loaded" class="image__caption">
<input ref="altInput"
type="text"
:value="alt"
:disabled="!isEditable"
@blur="updateAlt"
@keyup.enter="updateAlt">
</div>
</transition>
</div>
<small v-if="errorMessage" class="image__error-message">
{{ errorMessage }}
</small>
</figure>
</NodeViewWrapper>
</template>
<script>
import ClickOutside from 'vue-click-outside'
import { mapGetters } from 'vuex'
import { NcButton } from '@nextcloud/vue'
import { showError } from '@nextcloud/dialogs'
import ShowImageModal from '../components/ImageView/ShowImageModal.vue'
import store from '../mixins/store.js'
import { useAttachmentResolver } from '../components/Editor.provider.js'
import { emit } from '@nextcloud/event-bus'
import { NodeViewWrapper } from '@tiptap/vue-2'
import { Image as ImageIcon, Delete as DeleteIcon } from '../components/icons.js'
class LoadImageError extends Error {
constructor(reason, imageUrl) {
super(reason?.message || t('text', 'Failed to load'))
this.reason = reason
this.imageUrl = imageUrl
}
}
export default {
name: 'ImageView',
components: {
ImageIcon,
DeleteIcon,
NcButton,
ShowImageModal,
NodeViewWrapper,
},
directives: {
ClickOutside,
},
mixins: [
store,
useAttachmentResolver,
],
props: ['editor', 'node', 'extension', 'updateAttributes', 'deleteNode'], // eslint-disable-line
data() {
return {
attachment: null,
imageLoaded: false,
loaded: false,
failed: false,
showIcons: false,
imageUrl: null,
errorMessage: null,
attachmentSize: null,
showImageModal: false,
imageIndex: null,
isEditable: false,
embeddedImageList: [],
}
},
computed: {
...mapGetters({
imageAttachments: 'text/imageAttachments',
}),
attachmentType() {
if (this.attachment) {
return this.attachment.isImage ? 'image' : 'media'
} else {
return null
}
},
isMediaAttachment() {
return this.attachmentType === 'media'
},
showDeleteIcon() {
return this.isEditable && this.showIcons
},
showImageDeleteIcon() {
return this.showDeleteIcon && !this.isMediaAttachment
},
canDisplayImage() {
if (this.failed && this.loaded) {
return true
}
return this.loaded && this.imageLoaded
},
src: {
get() {
return this.node.attrs.src || ''
},
set(src) {
this.updateAttributes({
src,
})
},
},
alt: {
get() {
return this.node.attrs.alt ? this.node.attrs.alt : ''
},
set(alt) {
this.updateAttributes({
alt,
})
},
},
},
beforeMount() {
this.isEditable = this.editor.isEditable
this.editor.on('update', ({ editor }) => {
this.isEditable = editor.isEditable
})
this.loadPreview()
.catch(this.onImageLoadFailure)
},
methods: {
async loadPreview() {
this.attachment = await this.$attachmentResolver.resolve(this.src)
if (!this.attachment.previewUrl) {
throw new Error('Attachment source was not resolved')
}
return new Promise((resolve, reject) => {
const img = new Image()
img.onload = async () => {
this.imageUrl = this.attachment.previewUrl
this.imageLoaded = true
this.loaded = true
this.attachmentSize = this.attachment.size
}
img.onerror = (e) => {
reject(new LoadImageError(e, this.attachment.previewUrl))
}
img.src = this.attachment.previewUrl
})
},
onImageLoadFailure(err) {
this.failed = true
this.imageLoaded = false
this.loaded = true
this.errorMessage = err.message
if (err instanceof LoadImageError) {
this.errorMessage = `${this.errorMessage} ${this.src}`
}
this.$emit('error', { error: err, src: this.src })
},
updateAlt(event) {
this.updateAttributes({
alt: event.target.value,
})
},
onLoaded() {
this.loaded = true
this.$nextTick(() => {
this.$refs.altInput?.focus()
})
},
async updateEmbeddedImageList() {
this.embeddedImageList = []
// Get all images that succeeded to load
const imageViews = Array.from(document.querySelectorAll('figure[data-component="image-view"][data-attachment-type="image"]:not(.image-view--failed).image-view'))
for (const imgv of imageViews) {
const src = imgv.getAttribute('data-src')
if (!this.embeddedImageList.find(i => i.src === src)) {
// Don't add duplicates (e.g. when several editors are loaded in HTML document)
const attachment = await this.$attachmentResolver.resolve(imgv.getAttribute('data-src'))
this.embeddedImageList.push({
src,
...attachment,
})
}
}
},
handleAttachmentClick() {
// Open in viewer if possible
if (OCA.Viewer
// Viewer is not in use
&& !OCA.Viewer.file
// Viewer supports mimetype
&& OCA.Viewer.mimetypes.indexOf(this.attachment.mimetype) !== -1
// Attachment has davPath, i.e. is native attachment and not in public share
// (in public share we probably don't have DAV access)
&& this.attachment.davPath) {
// Viewer exists, is not in use and supports mimetype
OCA.Viewer.open({ path: this.attachment.davPath })
return
}
// Download file
window.location.assign(this.attachment.fullUrl)
},
async handleImageClick() {
await this.updateEmbeddedImageList()
this.imageIndex = this.embeddedImageList.findIndex(i => (i.src === this.src))
if (this.imageIndex !== -1) {
this.showImageModal = true
} else {
console.error('Could not find image in attachments list', this.attachment)
showError(t('text', 'Could not find image in attachments list.'))
}
},
onDelete() {
emit('text:image-node:delete', this.imageUrl)
this.deleteNode()
},
},
}
</script>
<style scoped lang="scss">
.image {
margin: 0;
padding: 0;
&, * {
-webkit-user-modify: read-only !important;
}
}
.image__caption {
text-align: center;
color: var(--color-text-lighter);
display: flex;
align-items: center;
justify-content: center;
&__wrapper {
position: relative;
}
&__delete {
display: flex;
flex-basis: 20%;
align-items: center;
width: 20px !important;
height: 20px;
position: absolute;
bottom: -1px;
right: -3px;
&, svg {
cursor: pointer;
}
}
.image__caption__wrapper {
flex-basis: 80%;
}
input[type='text'] {
width: 85%;
text-align: center;
background-color: transparent;
border: none !important;
color: var(--color-text-maxcontrast) !important;
&:focus {
border: 2px solid var(--color-border-dark) !important;
color: var(--color-main-text) !important;
}
}
figcaption {
color: var(--color-text-maxcontrast) !important;
max-width: 80%;
text-align: center;
width: fit-content;
}
}
.image__loading {
height: 100px;
}
.image__main {
max-height: calc(100vh - 50px - 50px);
}
.image__main--broken-icon, .image__error-message {
color: var(--color-text-maxcontrast);
}
.image__error-message {
display: block;
text-align: center;
}
.image__view {
text-align: center;
position: relative;
img {
max-width: 100%;
}
&:hover {
input[type='text'] {
border: 2px solid var(--color-border-dark) !important;
color: var(--color-main-text) !important;
}
}
}
.media {
display: flex;
align-items: center;
justify-content: left;
padding-bottom: 0;
.media__wrapper {
display: flex;
border: 2px solid var(--color-border);
border-radius: var(--border-radius-large);
padding: 8px;
img {
width: var(--default-clickable-area);
height: var(--default-clickable-area);
}
.metadata {
margin-left: 8px;
display: flex;
flex-direction: column;
align-items: start;
span {
line-height: 20px;
font-weight: normal;
&.size {
color: var(--color-text-maxcontrast);
}
}
}
}
.buttons {
margin-left: 8px;
}
}
.fade-enter-active {
transition: opacity .3s ease-in-out;
}
.fade-enter-to {
opacity: 1;
}
.fade-enter {
opacity: 0;
}
</style>