pleroma-fe/src/components/image_cropper/image_cropper.js

99 lines
2.7 KiB
JavaScript
Raw Normal View History

2025-03-30 17:06:19 +03:00
import 'cropperjs' // This adds all of the cropperjs's components into DOM
2026-01-08 17:26:52 +02:00
2020-10-20 21:18:23 +03:00
import { library } from '@fortawesome/fontawesome-svg-core'
2026-01-06 16:22:52 +02:00
import { faCircleNotch } from '@fortawesome/free-solid-svg-icons'
2020-10-20 21:18:23 +03:00
2026-01-06 16:22:52 +02:00
library.add(faCircleNotch)
2019-02-07 03:05:59 -05:00
const ImageCropper = {
props: {
2025-08-05 17:00:21 +03:00
// Mime-types to accept, i.e. which filetypes to accept (.gif, .png, etc.)
2019-02-07 03:05:59 -05:00
mimes: {
type: String,
2026-01-06 16:22:52 +02:00
default: 'image/png, image/gif, image/jpeg, image/bmp, image/x-icon',
2019-02-07 03:05:59 -05:00
},
2025-08-05 17:00:21 +03:00
// Fixed aspect-ratio for selection box
2025-08-04 11:10:43 +03:00
aspectRatio: {
2026-01-06 16:22:52 +02:00
type: Number,
},
2019-02-07 03:05:59 -05:00
},
2026-01-06 16:22:52 +02:00
data() {
2019-02-07 03:05:59 -05:00
return {
dataUrl: undefined,
2026-01-06 16:22:52 +02:00
filename: undefined,
2019-02-07 03:05:59 -05:00
}
},
2025-08-05 17:00:21 +03:00
emits: [
'submit', // cropping complete or uncropped image returned
'close', // cropper is closed
],
2019-02-07 03:05:59 -05:00
methods: {
2026-01-06 16:22:52 +02:00
destroy() {
2019-02-07 03:05:59 -05:00
this.$refs.input.value = ''
this.dataUrl = undefined
2019-02-08 21:59:33 -05:00
this.$emit('close')
2019-02-07 03:05:59 -05:00
},
2026-01-06 16:22:52 +02:00
submit(cropping = true) {
2025-03-30 17:06:19 +03:00
let cropperPromise
if (cropping) {
cropperPromise = this.$refs.cropperSelection.$toCanvas()
} else {
cropperPromise = Promise.resolve()
}
2025-08-04 03:35:09 +03:00
2026-01-06 16:22:52 +02:00
cropperPromise.then((canvas) => {
2025-08-04 03:35:09 +03:00
this.$emit('submit', { canvas, file: this.file })
2025-03-30 17:06:19 +03:00
})
2019-03-18 18:19:42 -07:00
},
2026-01-06 16:22:52 +02:00
pickImage() {
2019-02-07 03:05:59 -05:00
this.$refs.input.click()
},
2026-01-06 16:22:52 +02:00
readFile() {
const fileInput = this.$refs.input
if (fileInput.files != null && fileInput.files[0] != null) {
this.file = fileInput.files[0]
2022-07-31 12:35:48 +03:00
const reader = new window.FileReader()
reader.onload = (e) => {
this.dataUrl = e.target.result
2019-02-08 21:59:33 -05:00
this.$emit('open')
}
reader.readAsDataURL(this.file)
this.$emit('changed', this.file, reader)
}
2025-03-30 17:06:19 +03:00
},
inSelection(selection, maxSelection) {
return (
2026-01-06 16:22:52 +02:00
selection.x >= maxSelection.x &&
selection.y >= maxSelection.y &&
selection.x + selection.width <= maxSelection.x + maxSelection.width &&
selection.y + selection.height <= maxSelection.y + maxSelection.height
2025-03-30 17:06:19 +03:00
)
},
onCropperSelectionChange(event) {
const cropperCanvas = this.$refs.cropperCanvas
const cropperCanvasRect = cropperCanvas.getBoundingClientRect()
const selection = event.detail
const maxSelection = {
x: 0,
y: 0,
width: cropperCanvasRect.width,
height: cropperCanvasRect.height,
}
if (!this.inSelection(selection, maxSelection)) {
2026-01-06 16:22:52 +02:00
event.preventDefault()
2025-03-30 17:06:19 +03:00
}
2026-01-06 16:22:52 +02:00
},
2019-02-07 03:05:59 -05:00
},
2026-01-06 16:22:52 +02:00
mounted() {
2019-02-07 03:05:59 -05:00
// listen for input file changes
const fileInput = this.$refs.input
fileInput.addEventListener('change', this.readFile)
},
2022-03-24 13:50:22 +02:00
beforeUnmount: function () {
const fileInput = this.$refs.input
fileInput.removeEventListener('change', this.readFile)
2026-01-06 16:22:52 +02:00
},
2019-02-07 03:05:59 -05:00
}
export default ImageCropper