logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://hacktivis.me/git/pleroma-fe.git

image_cropper.js (3306B)


  1. import Cropper from 'cropperjs'
  2. import 'cropperjs/dist/cropper.css'
  3. import { library } from '@fortawesome/fontawesome-svg-core'
  4. import {
  5. faCircleNotch
  6. } from '@fortawesome/free-solid-svg-icons'
  7. library.add(
  8. faCircleNotch
  9. )
  10. const ImageCropper = {
  11. props: {
  12. trigger: {
  13. type: [String, window.Element],
  14. required: true
  15. },
  16. submitHandler: {
  17. type: Function,
  18. required: true
  19. },
  20. cropperOptions: {
  21. type: Object,
  22. default () {
  23. return {
  24. aspectRatio: 1,
  25. autoCropArea: 1,
  26. viewMode: 1,
  27. movable: false,
  28. zoomable: false,
  29. guides: false
  30. }
  31. }
  32. },
  33. mimes: {
  34. type: String,
  35. default: 'image/png, image/gif, image/jpeg, image/bmp, image/x-icon'
  36. },
  37. saveButtonLabel: {
  38. type: String
  39. },
  40. saveWithoutCroppingButtonlabel: {
  41. type: String
  42. },
  43. cancelButtonLabel: {
  44. type: String
  45. }
  46. },
  47. data () {
  48. return {
  49. cropper: undefined,
  50. dataUrl: undefined,
  51. filename: undefined,
  52. submitting: false
  53. }
  54. },
  55. computed: {
  56. saveText () {
  57. return this.saveButtonLabel || this.$t('image_cropper.save')
  58. },
  59. saveWithoutCroppingText () {
  60. return this.saveWithoutCroppingButtonlabel || this.$t('image_cropper.save_without_cropping')
  61. },
  62. cancelText () {
  63. return this.cancelButtonLabel || this.$t('image_cropper.cancel')
  64. }
  65. },
  66. methods: {
  67. destroy () {
  68. if (this.cropper) {
  69. this.cropper.destroy()
  70. }
  71. this.$refs.input.value = ''
  72. this.dataUrl = undefined
  73. this.$emit('close')
  74. },
  75. submit (cropping = true) {
  76. this.submitting = true
  77. this.submitHandler(cropping && this.cropper, this.file)
  78. .then(() => this.destroy())
  79. .finally(() => {
  80. this.submitting = false
  81. })
  82. },
  83. pickImage () {
  84. this.$refs.input.click()
  85. },
  86. createCropper () {
  87. this.cropper = new Cropper(this.$refs.img, this.cropperOptions)
  88. },
  89. getTriggerDOM () {
  90. return typeof this.trigger === 'object' ? this.trigger : document.querySelector(this.trigger)
  91. },
  92. readFile () {
  93. const fileInput = this.$refs.input
  94. if (fileInput.files != null && fileInput.files[0] != null) {
  95. this.file = fileInput.files[0]
  96. const reader = new window.FileReader()
  97. reader.onload = (e) => {
  98. this.dataUrl = e.target.result
  99. this.$emit('open')
  100. }
  101. reader.readAsDataURL(this.file)
  102. this.$emit('changed', this.file, reader)
  103. }
  104. }
  105. },
  106. mounted () {
  107. // listen for click event on trigger
  108. const trigger = this.getTriggerDOM()
  109. if (!trigger) {
  110. this.$emit('error', 'No image make trigger found.', 'user')
  111. } else {
  112. trigger.addEventListener('click', this.pickImage)
  113. }
  114. // listen for input file changes
  115. const fileInput = this.$refs.input
  116. fileInput.addEventListener('change', this.readFile)
  117. },
  118. beforeUnmount: function () {
  119. // remove the event listeners
  120. const trigger = this.getTriggerDOM()
  121. if (trigger) {
  122. trigger.removeEventListener('click', this.pickImage)
  123. }
  124. const fileInput = this.$refs.input
  125. fileInput.removeEventListener('change', this.readFile)
  126. }
  127. }
  128. export default ImageCropper