Image.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <template>
  2. <div>
  3. <div class="flex">
  4. <h3 class="mb-2 text-lg leading-6 font-medium text-gray-900 dark:text-gray-400" id="modal-title"
  5. :aria-label="ServiceContainer.modal.data.item.href" data-microtip-position="bottom-right" role="tooltip">{{ ServiceContainer.modal.data.item.name }}</h3>
  6. <div class="ml-auto mb-2">
  7. <button @click="crop" class="ml-1 px-2 py-1 rounded border border-transparent shadow-sm bg-blue-700/75 hover:bg-blue-700 dark:bg-gray-700 dark:hover:bg-gray-700/50 text-base font-medium text-white sm:ml-3 sm:w-auto sm:text-sm" v-if="showEdit">
  8. {{ t('Crop') }}</button>
  9. <button class="ml-1 px-2 py-1 text-blue-500" @click="editMode()" v-if="ServiceContainer.features.includes(FEATURES.EDIT)">{{ showEdit ? t('Cancel'): t('Edit') }}</button>
  10. </div>
  11. </div>
  12. <div class="w-full flex justify-center">
  13. <img ref="image" class="max-w-[50vh] max-h-[50vh]" :src="ServiceContainer.requester.getPreviewUrl(ServiceContainer.modal.data.adapter, ServiceContainer.modal.data.item)" alt="">
  14. </div>
  15. <message v-if="message.length" @hidden="message=''" :error="isError">{{ message }}</message>
  16. </div>
  17. </template>
  18. <script>
  19. import 'cropperjs/dist/cropper.css';
  20. import Cropper from 'cropperjs';
  21. import {FEATURES} from "../../features.js";
  22. import Message from '../Message.vue'
  23. export default {
  24. name:'ImagePreview',
  25. inject: ['ServiceContainer'],
  26. components:{
  27. Message
  28. },
  29. data(){
  30. return{
  31. cropper:null,
  32. showEdit:false,
  33. message:'',
  34. isError:false,
  35. FEATURES
  36. }
  37. },
  38. methods:{
  39. editMode(){
  40. this.showEdit = !this.showEdit;
  41. if (this.showEdit) {
  42. this.cropper = new Cropper(this.$refs.image, {
  43. crop(event) {
  44. },
  45. });
  46. } else {
  47. this.cropper.destroy();
  48. }
  49. },
  50. crop(){
  51. this.cropper.getCroppedCanvas({
  52. width: 795,
  53. height: 341
  54. })
  55. .toBlob(
  56. blob => {
  57. this.message = '';
  58. this.isError = false;
  59. let body = new FormData();
  60. body.append('file', blob);
  61. // console.log('blob',body.get('file'))
  62. this.ServiceContainer.requester.send({
  63. url: this.ServiceContainer.modal.data.item.href,
  64. method: 'PUT',
  65. responseType:'text',
  66. params: {
  67. // q: 'upload',
  68. // adapter: this.ServiceContainer.modal.data.adapter,
  69. // path: this.ServiceContainer.modal.data.item.path,
  70. },
  71. body
  72. })
  73. .then(data => {
  74. this.message = this.$t('Updated.');
  75. this.$refs.image.src = this.ServiceContainer.requester.getPreviewUrl(this.ServiceContainer.modal.data.adapter, this.ServiceContainer.modal.data.item);
  76. this.editMode();
  77. this.$emit('success');
  78. })
  79. .catch((e) => {
  80. this.message = this.$t(e.message);
  81. this.isError = true;
  82. });
  83. });
  84. },
  85. t(item){
  86. return this.$t(item)
  87. }
  88. },
  89. mounted() {
  90. this.$emit('success')
  91. }
  92. }
  93. </script>
  94. <!--<script setup>-->
  95. <!--import 'cropperjs/dist/cropper.css';-->
  96. <!--import Cropper from 'cropperjs';-->
  97. <!--import {inject, onMounted, ref} from 'vue';-->
  98. <!--import Message from '../Message.vue';-->
  99. <!--import {FEATURES} from "../features.js";-->
  100. <!--const emit = defineEmits(['success']);-->
  101. <!--const app = inject('ServiceContainer');-->
  102. <!--const {t} = app.i18n;-->
  103. <!--const image = ref(null);-->
  104. <!--const cropper = ref(null);-->
  105. <!--const showEdit = ref(false);-->
  106. <!--const message = ref('');-->
  107. <!--const isError = ref(false);-->
  108. <!--const editMode = () => {-->
  109. <!-- showEdit.value = !showEdit.value;-->
  110. <!-- if (showEdit.value) {-->
  111. <!-- cropper.value = new Cropper(image.value, {-->
  112. <!-- crop(event) {-->
  113. <!-- },-->
  114. <!-- });-->
  115. <!-- } else {-->
  116. <!-- cropper.value.destroy();-->
  117. <!-- }-->
  118. <!--};-->
  119. <!--const crop = () => {-->
  120. <!-- cropper.value-->
  121. <!-- .getCroppedCanvas({-->
  122. <!-- width: 795,-->
  123. <!-- height: 341-->
  124. <!-- })-->
  125. <!-- .toBlob(-->
  126. <!-- blob => {-->
  127. <!-- message.value = '';-->
  128. <!-- isError.value = false;-->
  129. <!-- const body = new FormData();-->
  130. <!-- body.set('file', blob);-->
  131. <!-- app.requester.send({-->
  132. <!-- url: '',-->
  133. <!-- method: 'post',-->
  134. <!-- params: {-->
  135. <!-- q: 'upload',-->
  136. <!-- adapter: app.modal.data.adapter,-->
  137. <!-- path: app.modal.data.item.path,-->
  138. <!-- },-->
  139. <!-- body,-->
  140. <!-- })-->
  141. <!-- .then(data => {-->
  142. <!-- message.value = t('Updated.');-->
  143. <!-- image.value.src = app.requester.getPreviewUrl(app.modal.data.adapter, app.modal.data.item);-->
  144. <!-- editMode();-->
  145. <!-- emit('success');-->
  146. <!-- })-->
  147. <!-- .catch((e) => {-->
  148. <!-- message.value = t(e.message);-->
  149. <!-- isError.value = true;-->
  150. <!-- });-->
  151. <!-- });-->
  152. <!--};-->
  153. <!--onMounted(() => {-->
  154. <!-- emit('success');-->
  155. <!--});-->
  156. <!--</script>-->