ImagePreview.svelte 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <script lang="ts">
  2. export let show = false;
  3. export let src = '';
  4. export let alt = '';
  5. const downloadImage = (url, filename) => {
  6. fetch(url)
  7. .then((response) => response.blob())
  8. .then((blob) => {
  9. const objectUrl = window.URL.createObjectURL(blob);
  10. const link = document.createElement('a');
  11. link.href = objectUrl;
  12. link.download = filename;
  13. document.body.appendChild(link);
  14. link.click();
  15. document.body.removeChild(link);
  16. window.URL.revokeObjectURL(objectUrl);
  17. })
  18. .catch((error) => console.error('Error downloading image:', error));
  19. };
  20. </script>
  21. {#if show}
  22. <!-- svelte-ignore a11y-click-events-have-key-events -->
  23. <!-- svelte-ignore a11y-no-static-element-interactions -->
  24. <div
  25. class="fixed top-0 right-0 left-0 bottom-0 bg-black text-white w-full min-h-screen h-screen flex justify-center z-50 overflow-hidden overscroll-contain"
  26. >
  27. <div class=" absolute left-0 w-full flex justify-between">
  28. <div>
  29. <button
  30. class=" p-5"
  31. on:click={() => {
  32. show = false;
  33. }}
  34. >
  35. <svg
  36. xmlns="http://www.w3.org/2000/svg"
  37. fill="none"
  38. viewBox="0 0 24 24"
  39. stroke-width="2"
  40. stroke="currentColor"
  41. class="w-6 h-6"
  42. >
  43. <path stroke-linecap="round" stroke-linejoin="round" d="M6 18 18 6M6 6l12 12" />
  44. </svg>
  45. </button>
  46. </div>
  47. <div>
  48. <button
  49. class=" p-5"
  50. on:click={() => {
  51. downloadImage(src, src.substring(src.lastIndexOf('/') + 1));
  52. }}
  53. >
  54. <svg
  55. xmlns="http://www.w3.org/2000/svg"
  56. viewBox="0 0 20 20"
  57. fill="currentColor"
  58. class="w-6 h-6"
  59. >
  60. <path
  61. d="M10.75 2.75a.75.75 0 0 0-1.5 0v8.614L6.295 8.235a.75.75 0 1 0-1.09 1.03l4.25 4.5a.75.75 0 0 0 1.09 0l4.25-4.5a.75.75 0 0 0-1.09-1.03l-2.955 3.129V2.75Z"
  62. />
  63. <path
  64. d="M3.5 12.75a.75.75 0 0 0-1.5 0v2.5A2.75 2.75 0 0 0 4.75 18h10.5A2.75 2.75 0 0 0 18 15.25v-2.5a.75.75 0 0 0-1.5 0v2.5c0 .69-.56 1.25-1.25 1.25H4.75c-.69 0-1.25-.56-1.25-1.25v-2.5Z"
  65. />
  66. </svg>
  67. </button>
  68. </div>
  69. </div>
  70. <img {src} {alt} class=" mx-auto h-full object-scale-down" />
  71. </div>
  72. {/if}