ImagePreview.svelte 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <script lang="ts">
  2. import { onDestroy, onMount } from 'svelte';
  3. export let show = false;
  4. export let src = '';
  5. export let alt = '';
  6. let mounted = false;
  7. let previewElement = null;
  8. const downloadImage = (url, filename, prefixName = '') => {
  9. fetch(url)
  10. .then((response) => response.blob())
  11. .then((blob) => {
  12. const objectUrl = window.URL.createObjectURL(blob);
  13. const link = document.createElement('a');
  14. link.href = objectUrl;
  15. link.download = `${prefixName}${filename}`;
  16. document.body.appendChild(link);
  17. link.click();
  18. document.body.removeChild(link);
  19. window.URL.revokeObjectURL(objectUrl);
  20. })
  21. .catch((error) => console.error('Error downloading image:', error));
  22. };
  23. const handleKeyDown = (event: KeyboardEvent) => {
  24. if (event.key === 'Escape') {
  25. console.log('Escape');
  26. show = false;
  27. }
  28. };
  29. onMount(() => {
  30. mounted = true;
  31. });
  32. $: if (show && previewElement) {
  33. document.body.appendChild(previewElement);
  34. window.addEventListener('keydown', handleKeyDown);
  35. document.body.style.overflow = 'hidden';
  36. } else if (previewElement) {
  37. window.removeEventListener('keydown', handleKeyDown);
  38. document.body.removeChild(previewElement);
  39. document.body.style.overflow = 'unset';
  40. }
  41. onDestroy(() => {
  42. show = false;
  43. if (previewElement) {
  44. document.body.removeChild(previewElement);
  45. }
  46. });
  47. </script>
  48. {#if show}
  49. <!-- svelte-ignore a11y-click-events-have-key-events -->
  50. <!-- svelte-ignore a11y-no-static-element-interactions -->
  51. <div
  52. bind:this={previewElement}
  53. class="modal fixed top-0 right-0 left-0 bottom-0 bg-black text-white w-full min-h-screen h-screen flex justify-center z-9999 overflow-hidden overscroll-contain"
  54. >
  55. <div class=" absolute left-0 w-full flex justify-between select-none">
  56. <div>
  57. <button
  58. class=" p-5"
  59. on:click={() => {
  60. show = false;
  61. }}
  62. >
  63. <svg
  64. xmlns="http://www.w3.org/2000/svg"
  65. fill="none"
  66. viewBox="0 0 24 24"
  67. stroke-width="2"
  68. stroke="currentColor"
  69. class="w-6 h-6"
  70. >
  71. <path stroke-linecap="round" stroke-linejoin="round" d="M6 18 18 6M6 6l12 12" />
  72. </svg>
  73. </button>
  74. </div>
  75. <div>
  76. <button
  77. class=" p-5"
  78. on:click={() => {
  79. downloadImage(src, src.substring(src.lastIndexOf('/') + 1), alt);
  80. }}
  81. >
  82. <svg
  83. xmlns="http://www.w3.org/2000/svg"
  84. viewBox="0 0 20 20"
  85. fill="currentColor"
  86. class="w-6 h-6"
  87. >
  88. <path
  89. 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"
  90. />
  91. <path
  92. 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"
  93. />
  94. </svg>
  95. </button>
  96. </div>
  97. </div>
  98. <img {src} {alt} class=" mx-auto h-full object-scale-down select-none" draggable="false" />
  99. </div>
  100. {/if}