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

142 lines
3.8 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
2020-10-20 21:18:23 +03:00
import { library } from '@fortawesome/fontawesome-svg-core'
import {
2020-10-21 00:01:28 +03:00
faCircleNotch
2020-10-20 21:18:23 +03:00
} from '@fortawesome/free-solid-svg-icons'
library.add(
2020-10-21 00:01:28 +03:00
faCircleNotch
2020-10-20 21:18:23 +03:00
)
2019-02-07 03:05:59 -05:00
const ImageCropper = {
props: {
trigger: {
2019-02-08 12:13:23 -05:00
type: [String, window.Element],
2019-02-07 03:05:59 -05:00
required: true
},
2019-02-08 21:59:33 -05:00
submitHandler: {
type: Function,
required: true
},
2019-02-07 03:05:59 -05:00
mimes: {
type: String,
default: 'image/png, image/gif, image/jpeg, image/bmp, image/x-icon'
},
2019-02-08 21:59:33 -05:00
saveButtonLabel: {
2019-02-08 11:42:02 -05:00
type: String
2019-02-07 03:05:59 -05:00
},
2019-03-18 18:19:42 -07:00
saveWithoutCroppingButtonlabel: {
type: String
},
2019-02-08 21:59:33 -05:00
cancelButtonLabel: {
2019-02-08 11:42:02 -05:00
type: String
2019-02-07 03:05:59 -05:00
}
},
data () {
return {
dataUrl: undefined,
2019-02-08 21:59:33 -05:00
filename: undefined,
2020-12-02 12:46:31 +02:00
submitting: false
2019-02-07 03:05:59 -05:00
}
},
2019-02-08 11:42:02 -05:00
computed: {
2019-02-08 21:59:33 -05:00
saveText () {
2019-02-08 11:42:02 -05:00
return this.saveButtonLabel || this.$t('image_cropper.save')
2019-02-08 21:59:33 -05:00
},
2019-03-18 18:19:42 -07:00
saveWithoutCroppingText () {
return this.saveWithoutCroppingButtonlabel || this.$t('image_cropper.save_without_cropping')
},
2019-02-08 21:59:33 -05:00
cancelText () {
return this.cancelButtonLabel || this.$t('image_cropper.cancel')
2019-02-08 11:42:02 -05:00
}
},
2019-02-07 03:05:59 -05:00
methods: {
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
},
2019-03-22 13:00:58 -04:00
submit (cropping = true) {
2019-02-08 21:59:33 -05:00
this.submitting = true
2025-03-30 17:06:19 +03:00
let cropperPromise
if (cropping) {
cropperPromise = this.$refs.cropperSelection.$toCanvas()
} else {
cropperPromise = Promise.resolve()
}
cropperPromise.then(canvas => {
this.submitHandler(canvas, this.file)
.then(() => this.destroy())
.finally(() => {
this.submitting = false
})
})
2019-03-18 18:19:42 -07:00
},
2019-02-07 03:05:59 -05:00
pickImage () {
this.$refs.input.click()
},
getTriggerDOM () {
return typeof this.trigger === 'object' ? this.trigger : document.querySelector(this.trigger)
},
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 (
selection.x >= maxSelection.x
&& selection.y >= maxSelection.y
&& (selection.x + selection.width) <= (maxSelection.x + maxSelection.width)
&& (selection.y + selection.height) <= (maxSelection.y + maxSelection.height)
)
},
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)) {
event.preventDefault();
}
2019-02-07 03:05:59 -05:00
}
},
mounted () {
// listen for click event on trigger
const trigger = this.getTriggerDOM()
2019-02-07 03:05:59 -05:00
if (!trigger) {
this.$emit('error', 'No image make trigger found.', 'user')
} else {
trigger.addEventListener('click', this.pickImage)
}
// 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 () {
// remove the event listeners
const trigger = this.getTriggerDOM()
if (trigger) {
trigger.removeEventListener('click', this.pickImage)
}
const fileInput = this.$refs.input
fileInput.removeEventListener('change', this.readFile)
2019-02-07 03:05:59 -05:00
}
}
export default ImageCropper