pleroma-fe/src/components/still-image/still-image.js

75 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-01-29 10:47:26 +03:00
const StillImage = {
props: [
'src',
'referrerpolicy',
'mimetype',
'imageLoadError',
2019-02-18 05:03:26 +00:00
'imageLoadHandler',
2022-02-03 22:50:32 +02:00
'alt',
'height',
'width',
2022-12-24 12:43:30 -05:00
'dataSrc',
2026-01-06 16:22:52 +02:00
'loading',
2018-01-29 10:47:26 +03:00
],
2026-01-06 16:22:52 +02:00
data() {
return {
// for lazy loading, see loadLazy()
realSrc: this.src,
2026-01-06 16:22:52 +02:00
stopGifs: this.$store.getters.mergedConfig.stopGifs,
}
},
2018-01-29 10:47:26 +03:00
computed: {
2026-01-06 16:22:52 +02:00
animated() {
if (!this.realSrc) {
return false
}
2026-01-06 16:22:52 +02:00
return (
this.stopGifs &&
(this.mimetype === 'image/gif' || this.realSrc.endsWith('.gif'))
)
2022-02-03 22:50:32 +02:00
},
2026-01-06 16:22:52 +02:00
style() {
const appendPx = (str) => (/\d$/.test(str) ? str + 'px' : str)
2022-02-03 22:50:32 +02:00
return {
height: this.height ? appendPx(this.height) : null,
2026-01-06 16:22:52 +02:00
width: this.width ? appendPx(this.width) : null,
2022-02-03 22:50:32 +02:00
}
2026-01-06 16:22:52 +02:00
},
2018-01-29 10:47:26 +03:00
},
methods: {
2026-01-06 16:22:52 +02:00
loadLazy() {
if (this.dataSrc) {
this.realSrc = this.dataSrc
}
},
2026-01-06 16:22:52 +02:00
onLoad() {
if (!this.realSrc) {
return
}
2020-09-29 10:18:37 +00:00
const image = this.$refs.src
if (!image) return
this.imageLoadHandler && this.imageLoadHandler(image)
2018-01-29 10:47:26 +03:00
const canvas = this.$refs.canvas
if (!canvas) return
2020-09-29 10:18:37 +00:00
const width = image.naturalWidth
const height = image.naturalHeight
canvas.width = width
canvas.height = height
2020-09-29 10:18:37 +00:00
canvas.getContext('2d').drawImage(image, 0, 0, width, height)
2019-02-05 17:17:50 +02:00
},
2026-01-06 16:22:52 +02:00
onError() {
2019-02-05 17:17:50 +02:00
this.imageLoadError && this.imageLoadError()
2026-01-06 16:22:52 +02:00
},
2022-01-17 23:41:11 -05:00
},
watch: {
2026-01-06 16:22:52 +02:00
src() {
2022-01-17 23:41:11 -05:00
this.realSrc = this.src
},
2026-01-06 16:22:52 +02:00
dataSrc() {
2022-01-17 23:41:11 -05:00
this.$el.removeAttribute('data-loaded')
2026-01-06 16:22:52 +02:00
},
},
2018-01-29 10:47:26 +03:00
}
export default StillImage