Image.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <!--
  2. Assistant de création d'image
  3. https://norserium.github.io/vue-advanced-cropper/
  4. -->
  5. <template>
  6. <LazyLayoutDialog :show="true">
  7. <template #dialogType>{{ $t('image_assistant') }}</template>
  8. <template #dialogTitle>{{ $t('modif_picture') }}</template>
  9. <template #dialogText>
  10. <div class="upload">
  11. <v-row
  12. v-if="fetchState.pending"
  13. class="fill-height ma-0 loading"
  14. align="center"
  15. justify="center"
  16. >
  17. <v-progress-circular
  18. :indeterminate="true"
  19. color="grey lighten-1">
  20. </v-progress-circular>
  21. </v-row>
  22. <div v-else >
  23. <div class="upload__cropper-wrapper">
  24. <cropper
  25. ref="cropper"
  26. class="upload__cropper"
  27. check-orientation
  28. :src="image.src"
  29. :default-position="{left : coordinates.left, top : coordinates.top}"
  30. :default-size="coordinates.width ? {width : coordinates.width, height : coordinates.height}: defaultSize"
  31. @change="onChange"
  32. />
  33. <div v-if="image.src" class="upload__reset-button" title="Reset Image" @click="reset()">
  34. <v-icon>mdi-delete</v-icon>
  35. </div>
  36. </div>
  37. <div class="upload__buttons-wrapper">
  38. <button class="upload__button" @click="$refs.file.click()">
  39. <input ref="file" type="file" accept="image/*" @change="uploadImage($event)" />
  40. {{$t('upload_image')}}
  41. </button>
  42. </div>
  43. </div>
  44. </div>
  45. </template>
  46. <template #dialogBtn>
  47. <v-btn class="mr-4 submitBtn theme-neutral-strong" @click="$emit('close')">
  48. {{ $t('cancel') }}
  49. </v-btn>
  50. <v-btn class="mr-4 submitBtn theme-danger" @click="save">
  51. {{ $t('save') }}
  52. </v-btn>
  53. </template>
  54. </LazyLayoutDialog>
  55. </template>
  56. <script setup lang="ts">
  57. import {useNuxtApp} from "#app";
  58. import {ref, Ref} from "@vue/reactivity";
  59. import File from '~/models/Core/File'
  60. import {useAp2iRequestService} from "~/composables/data/useAp2iRequestService";
  61. import UrlUtils from "~/services/utils/urlUtils";
  62. import {useImageFetch} from "~/composables/data/useImageFetch";
  63. import {onUnmounted, watch, WatchStopHandle} from "@vue/runtime-core";
  64. import {useEntityManager} from "~/composables/data/useEntityManager";
  65. import {AnyJson} from "~/types/data";
  66. const props = defineProps({
  67. imageId: {
  68. type: Number,
  69. required: false
  70. },
  71. ownerId: {
  72. type: Number,
  73. required: false
  74. },
  75. field: {
  76. type: String,
  77. required: true
  78. }
  79. })
  80. const { emit } = useNuxtApp()
  81. const { em } = useEntityManager()
  82. const file = new File()
  83. const cropper: Ref<any> = ref(null)
  84. const image: Ref<AnyJson> = ref({
  85. id: null,
  86. src: null,
  87. file: null,
  88. name: null
  89. })
  90. const coordinates: Ref<AnyJson> = ref({})
  91. const defaultSize = ({ imageSize, visibleArea }: any) => {
  92. return {
  93. width: (visibleArea || imageSize).width,
  94. height: (visibleArea || imageSize).height,
  95. };
  96. }
  97. // Si l'id est renseigné, on récupère l'Item File afin d'avoir les informations de config, le nom, etc.
  98. if (props.imageId && props.imageId > 0) {
  99. const { apiRequestService } = useAp2iRequestService()
  100. const result: any = await apiRequestService.get(UrlUtils.join('api/files', '' + props.imageId))
  101. const config = JSON.parse(result.data.config)
  102. coordinates.value.left = config.x
  103. coordinates.value.top = config.y
  104. coordinates.value.height = config.height
  105. coordinates.value.width = config.width
  106. image.value.name = result.data.name
  107. image.value.id = result.data.id
  108. }
  109. //On récupère l'image...
  110. const { fetch } = useImageFetch()
  111. const { data: imageLoaded, pending } = fetch(props.imageId ?? null)
  112. const unwatch: WatchStopHandle = watch(
  113. imageLoaded,
  114. (newValue, oldValue) => {
  115. if (newValue === oldValue || typeof newValue === 'undefined') {
  116. return
  117. }
  118. image.value.src = newValue
  119. }
  120. )
  121. /**
  122. * Quand l'utilisateur choisit une image sur sa machine
  123. * @param event
  124. */
  125. const uploadImage = (event:any) => {
  126. const { files } = event.target
  127. if (files && files[0]) {
  128. reset()
  129. image.value.name = files[0].name
  130. image.value.src = URL.createObjectURL(files[0])
  131. image.value.file = files[0]
  132. }
  133. }
  134. /**
  135. * Lorsque le cropper change de position / taille, on met à jour les coordonnées
  136. * @param config
  137. */
  138. const onChange = ({ coordinates: config } : any) => {
  139. coordinates.value = config;
  140. }
  141. /**
  142. * Lorsque l'on sauvegarde l'image
  143. */
  144. // TODO: Voir si tout ou partie de cette fonction peut passer dans le useImageFetch, imageManager ou imageUtils
  145. const save = async () => {
  146. file.config = JSON.stringify({
  147. x: coordinates.value.left,
  148. y: coordinates.value.top,
  149. height: coordinates.value.height,
  150. width: coordinates.value.width
  151. })
  152. if (image.value.id > 0) {
  153. // Mise à jour d'une image existante : on bouge simplement le cropper
  154. file.id = image.value.id as number
  155. await em.persist(File, file) // TODO: à revoir
  156. // On émet un évent afin de mettre à jour le formulaire de départ
  157. emit('reload')
  158. } else {
  159. // Création d'une nouvelle image
  160. if (image.value.file) {
  161. // On créé l'objet File à sauvegarder
  162. file.name = image.value.name
  163. file.imgFieldName = props.field
  164. file.visibility = 'EVERYBODY'
  165. file.folder = 'IMAGES'
  166. file.status = 'READY'
  167. if (props.ownerId) {
  168. file.ownerId = props.ownerId
  169. }
  170. const returnedFile = await em.persist(File, file) // TODO: à revoir, il faudra pouvoir passer `image.value.file` avec la requête
  171. //On émet un évent afin de mettre à jour le formulaire de départ
  172. emit('update', returnedFile.data['@id'])
  173. } else {
  174. // On reset l'image : on a appuyé sur "poubelle" puis on enregistre
  175. emit('reset')
  176. }
  177. }
  178. }
  179. /**
  180. * On choisit de supprimer l'image présente
  181. */
  182. const reset = () => {
  183. image.value.src = null
  184. image.value.file = null
  185. image.value.name = null
  186. image.value.id = null
  187. URL.revokeObjectURL(image.value.src)
  188. }
  189. /**
  190. * Lorsqu'on démonte le component on supprime le watcher et on revoke l'objet URL
  191. */
  192. onUnmounted(() => {
  193. unwatch()
  194. if (image.value.src) {
  195. URL.revokeObjectURL(image.value.src)
  196. }
  197. })
  198. </script>
  199. <style lang="scss">
  200. .vue-advanced-cropper__stretcher{
  201. height: auto !important;
  202. width: auto !important;
  203. }
  204. .loading{
  205. height: 300px;
  206. }
  207. .upload {
  208. user-select: none;
  209. padding: 20px;
  210. display: block;
  211. &__cropper {
  212. border: solid 1px #eee; /* !color! */
  213. min-height: 500px;
  214. max-height: 500px;
  215. }
  216. &__cropper-wrapper {
  217. position: relative;
  218. }
  219. &__reset-button {
  220. position: absolute;
  221. right: 20px;
  222. bottom: 20px;
  223. cursor: pointer;
  224. display: flex;
  225. align-items: center;
  226. justify-content: center;
  227. height: 42px;
  228. width: 42px;
  229. background: rgba(#3fb37f, 0.7); /* !color! */
  230. transition: background 0.5s;
  231. &:hover {
  232. background: #3fb37f; /* !color! */
  233. }
  234. }
  235. &__buttons-wrapper {
  236. display: flex;
  237. justify-content: center;
  238. margin-top: 17px;
  239. }
  240. &__button {
  241. border: none;
  242. outline: solid transparent;
  243. color: white;
  244. font-size: 16px;
  245. padding: 10px 20px;
  246. background: #3fb37f; /* !color! */
  247. cursor: pointer;
  248. transition: background 0.5s;
  249. margin: 0 16px;
  250. &:hover,
  251. &:focus {
  252. background: #38d890; /* !color! */
  253. }
  254. input {
  255. display: none;
  256. }
  257. }
  258. }
  259. </style>