AvatarImageComponent.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <div
  3. class="avatar-wrapper relative-position"
  4. :class="{ 'drag-over': isDragOver }"
  5. @dragover.prevent="isDragOver = true"
  6. @dragleave="isDragOver = false"
  7. @drop.prevent="onDrop"
  8. >
  9. <div
  10. v-if="!imageUrl"
  11. class="full-width full-height flex flex-center column gap-xs no-image-state"
  12. @click="openChangeDialog"
  13. >
  14. <q-icon name="add_photo_alternate" size="32px" color="grey-5" />
  15. <span class="text-grey-6" style="font-size: 12px"
  16. >adicione uma imagem</span
  17. >
  18. </div>
  19. <template v-else>
  20. <img :src="imageUrl" class="avatar-image" />
  21. <div
  22. class="actions-overlay absolute row no-wrap"
  23. style="top: 6px; right: 6px; gap: 4px"
  24. >
  25. <q-btn
  26. round
  27. unelevated
  28. size="xs"
  29. icon="edit"
  30. color="grey-8"
  31. text-color="white"
  32. @click.stop="openChangeDialog"
  33. >
  34. <q-tooltip>Trocar imagem</q-tooltip>
  35. </q-btn>
  36. <q-btn
  37. round
  38. unelevated
  39. size="xs"
  40. icon="delete"
  41. color="negative"
  42. text-color="white"
  43. @click.stop="removeImage"
  44. >
  45. <q-tooltip>Remover imagem</q-tooltip>
  46. </q-btn>
  47. </div>
  48. </template>
  49. </div>
  50. </template>
  51. <script setup>
  52. import { ref } from "vue";
  53. import { useQuasar } from "quasar";
  54. import ChangeImageDialog from "src/components/shared/ChangeImageDialog.vue";
  55. const emit = defineEmits(["update:file"]);
  56. const $q = useQuasar();
  57. const imageUrl = ref(null);
  58. const isDragOver = ref(false);
  59. function openChangeDialog() {
  60. $q.dialog({ component: ChangeImageDialog }).onOk(({ file, previewUrl }) => {
  61. imageUrl.value = previewUrl;
  62. emit("update:file", file);
  63. });
  64. }
  65. function removeImage() {
  66. imageUrl.value = null;
  67. emit("update:file", null);
  68. }
  69. function onDrop(event) {
  70. isDragOver.value = false;
  71. const file = event.dataTransfer.files[0];
  72. if (file && file.type.startsWith("image/")) {
  73. const reader = new FileReader();
  74. reader.onload = (e) => {
  75. imageUrl.value = e.target.result;
  76. };
  77. reader.readAsDataURL(file);
  78. emit("update:file", file);
  79. }
  80. }
  81. defineExpose({
  82. setImageUrl(url) {
  83. imageUrl.value = url;
  84. },
  85. });
  86. </script>
  87. <style scoped>
  88. .avatar-wrapper {
  89. width: 174px;
  90. height: 149px;
  91. border-radius: 8px;
  92. border: 2px dashed #ccc;
  93. overflow: hidden;
  94. background-color: #f5f5f5;
  95. cursor: pointer;
  96. transition:
  97. border-color 0.2s,
  98. background-color 0.2s;
  99. }
  100. .avatar-wrapper:hover,
  101. .avatar-wrapper.drag-over {
  102. border-color: #ff8340;
  103. background-color: #fff5ef;
  104. }
  105. .no-image-state {
  106. height: 100%;
  107. }
  108. .avatar-image {
  109. width: 100%;
  110. height: 100%;
  111. object-fit: cover;
  112. display: block;
  113. }
  114. .actions-overlay {
  115. opacity: 0;
  116. transition: opacity 0.2s;
  117. }
  118. .avatar-wrapper:hover .actions-overlay {
  119. opacity: 1;
  120. }
  121. </style>