image-preview.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import type { FC } from 'react'
  2. import React, { useCallback, useEffect, useRef, useState } from 'react'
  3. import { t } from 'i18next'
  4. import { createPortal } from 'react-dom'
  5. import { RiAddBoxLine, RiCloseLine, RiDownloadCloud2Line, RiFileCopyLine, RiZoomInLine, RiZoomOutLine } from '@remixicon/react'
  6. import Tooltip from '@/app/components/base/tooltip'
  7. import Toast from '@/app/components/base/toast'
  8. type ImagePreviewProps = {
  9. url: string
  10. title: string
  11. onCancel: () => void
  12. }
  13. const isBase64 = (str: string): boolean => {
  14. try {
  15. return btoa(atob(str)) === str
  16. }
  17. catch (err) {
  18. return false
  19. }
  20. }
  21. const ImagePreview: FC<ImagePreviewProps> = ({
  22. url,
  23. title,
  24. onCancel,
  25. }) => {
  26. const [scale, setScale] = useState(1)
  27. const [position, setPosition] = useState({ x: 0, y: 0 })
  28. const [isDragging, setIsDragging] = useState(false)
  29. const imgRef = useRef<HTMLImageElement>(null)
  30. const dragStartRef = useRef({ x: 0, y: 0 })
  31. const [isCopied, setIsCopied] = useState(false)
  32. const containerRef = useRef<HTMLDivElement>(null)
  33. const openInNewTab = () => {
  34. // Open in a new window, considering the case when the page is inside an iframe
  35. if (url.startsWith('http') || url.startsWith('https')) {
  36. window.open(url, '_blank')
  37. }
  38. else if (url.startsWith('data:image')) {
  39. // Base64 image
  40. const win = window.open()
  41. win?.document.write(`<img src="${url}" alt="${title}" />`)
  42. }
  43. else {
  44. Toast.notify({
  45. type: 'error',
  46. message: `Unable to open image: ${url}`,
  47. })
  48. }
  49. }
  50. const downloadImage = () => {
  51. // Open in a new window, considering the case when the page is inside an iframe
  52. if (url.startsWith('http') || url.startsWith('https')) {
  53. const a = document.createElement('a')
  54. a.href = url
  55. a.download = title
  56. a.click()
  57. }
  58. else if (url.startsWith('data:image')) {
  59. // Base64 image
  60. const a = document.createElement('a')
  61. a.href = url
  62. a.download = title
  63. a.click()
  64. }
  65. else {
  66. Toast.notify({
  67. type: 'error',
  68. message: `Unable to open image: ${url}`,
  69. })
  70. }
  71. }
  72. const zoomIn = () => {
  73. setScale(prevScale => Math.min(prevScale * 1.2, 15))
  74. }
  75. const zoomOut = () => {
  76. setScale((prevScale) => {
  77. const newScale = Math.max(prevScale / 1.2, 0.5)
  78. if (newScale === 1)
  79. setPosition({ x: 0, y: 0 }) // Reset position when fully zoomed out
  80. return newScale
  81. })
  82. }
  83. const imageBase64ToBlob = (base64: string, type = 'image/png'): Blob => {
  84. const byteCharacters = atob(base64)
  85. const byteArrays = []
  86. for (let offset = 0; offset < byteCharacters.length; offset += 512) {
  87. const slice = byteCharacters.slice(offset, offset + 512)
  88. const byteNumbers = new Array(slice.length)
  89. for (let i = 0; i < slice.length; i++)
  90. byteNumbers[i] = slice.charCodeAt(i)
  91. const byteArray = new Uint8Array(byteNumbers)
  92. byteArrays.push(byteArray)
  93. }
  94. return new Blob(byteArrays, { type })
  95. }
  96. const imageCopy = useCallback(() => {
  97. const shareImage = async () => {
  98. try {
  99. const base64Data = url.split(',')[1]
  100. const blob = imageBase64ToBlob(base64Data, 'image/png')
  101. await navigator.clipboard.write([
  102. new ClipboardItem({
  103. [blob.type]: blob,
  104. }),
  105. ])
  106. setIsCopied(true)
  107. Toast.notify({
  108. type: 'success',
  109. message: t('common.operation.imageCopied'),
  110. })
  111. }
  112. catch (err) {
  113. console.error('Failed to copy image:', err)
  114. const link = document.createElement('a')
  115. link.href = url
  116. link.download = `${title}.png`
  117. document.body.appendChild(link)
  118. link.click()
  119. document.body.removeChild(link)
  120. Toast.notify({
  121. type: 'info',
  122. message: t('common.operation.imageDownloaded'),
  123. })
  124. }
  125. }
  126. shareImage()
  127. }, [title, url])
  128. const handleWheel = useCallback((e: React.WheelEvent<HTMLDivElement>) => {
  129. if (e.deltaY < 0)
  130. zoomIn()
  131. else
  132. zoomOut()
  133. }, [])
  134. const handleMouseDown = useCallback((e: React.MouseEvent<HTMLDivElement>) => {
  135. if (scale > 1) {
  136. setIsDragging(true)
  137. dragStartRef.current = { x: e.clientX - position.x, y: e.clientY - position.y }
  138. }
  139. }, [scale, position])
  140. const handleMouseMove = useCallback((e: React.MouseEvent<HTMLDivElement>) => {
  141. if (isDragging && scale > 1) {
  142. const deltaX = e.clientX - dragStartRef.current.x
  143. const deltaY = e.clientY - dragStartRef.current.y
  144. // Calculate boundaries
  145. const imgRect = imgRef.current?.getBoundingClientRect()
  146. const containerRect = imgRef.current?.parentElement?.getBoundingClientRect()
  147. if (imgRect && containerRect) {
  148. const maxX = (imgRect.width * scale - containerRect.width) / 2
  149. const maxY = (imgRect.height * scale - containerRect.height) / 2
  150. setPosition({
  151. x: Math.max(-maxX, Math.min(maxX, deltaX)),
  152. y: Math.max(-maxY, Math.min(maxY, deltaY)),
  153. })
  154. }
  155. }
  156. }, [isDragging, scale])
  157. const handleMouseUp = useCallback(() => {
  158. setIsDragging(false)
  159. }, [])
  160. useEffect(() => {
  161. document.addEventListener('mouseup', handleMouseUp)
  162. return () => {
  163. document.removeEventListener('mouseup', handleMouseUp)
  164. }
  165. }, [handleMouseUp])
  166. useEffect(() => {
  167. const handleKeyDown = (event: KeyboardEvent) => {
  168. if (event.key === 'Escape')
  169. onCancel()
  170. }
  171. window.addEventListener('keydown', handleKeyDown)
  172. // Set focus to the container element
  173. if (containerRef.current)
  174. containerRef.current.focus()
  175. // Cleanup function
  176. return () => {
  177. window.removeEventListener('keydown', handleKeyDown)
  178. }
  179. }, [onCancel])
  180. return createPortal(
  181. <div className='fixed inset-0 p-8 flex items-center justify-center bg-black/80 z-[1000] image-preview-container'
  182. onClick={e => e.stopPropagation()}
  183. onWheel={handleWheel}
  184. onMouseDown={handleMouseDown}
  185. onMouseMove={handleMouseMove}
  186. onMouseUp={handleMouseUp}
  187. style={{ cursor: scale > 1 ? 'move' : 'default' }}
  188. tabIndex={-1}>
  189. {/* eslint-disable-next-line @next/next/no-img-element */}
  190. <img
  191. ref={imgRef}
  192. alt={title}
  193. src={isBase64(url) ? `data:image/png;base64,${url}` : url}
  194. className='max-w-full max-h-full'
  195. style={{
  196. transform: `scale(${scale}) translate(${position.x}px, ${position.y}px)`,
  197. transition: isDragging ? 'none' : 'transform 0.2s ease-in-out',
  198. }}
  199. />
  200. <Tooltip popupContent={t('common.operation.copyImage')}>
  201. <div className='absolute top-6 right-48 flex items-center justify-center w-8 h-8 rounded-lg cursor-pointer'
  202. onClick={imageCopy}>
  203. {isCopied
  204. ? <RiFileCopyLine className='w-4 h-4 text-green-500'/>
  205. : <RiFileCopyLine className='w-4 h-4 text-gray-500'/>}
  206. </div>
  207. </Tooltip>
  208. <Tooltip popupContent={t('common.operation.zoomOut')}>
  209. <div className='absolute top-6 right-40 flex items-center justify-center w-8 h-8 rounded-lg cursor-pointer'
  210. onClick={zoomOut}>
  211. <RiZoomOutLine className='w-4 h-4 text-gray-500'/>
  212. </div>
  213. </Tooltip>
  214. <Tooltip popupContent={t('common.operation.zoomIn')}>
  215. <div className='absolute top-6 right-32 flex items-center justify-center w-8 h-8 rounded-lg cursor-pointer'
  216. onClick={zoomIn}>
  217. <RiZoomInLine className='w-4 h-4 text-gray-500'/>
  218. </div>
  219. </Tooltip>
  220. <Tooltip popupContent={t('common.operation.download')}>
  221. <div className='absolute top-6 right-24 flex items-center justify-center w-8 h-8 rounded-lg cursor-pointer'
  222. onClick={downloadImage}>
  223. <RiDownloadCloud2Line className='w-4 h-4 text-gray-500'/>
  224. </div>
  225. </Tooltip>
  226. <Tooltip popupContent={t('common.operation.openInNewTab')}>
  227. <div className='absolute top-6 right-16 flex items-center justify-center w-8 h-8 rounded-lg cursor-pointer'
  228. onClick={openInNewTab}>
  229. <RiAddBoxLine className='w-4 h-4 text-gray-500'/>
  230. </div>
  231. </Tooltip>
  232. <Tooltip popupContent={t('common.operation.cancel')}>
  233. <div
  234. className='absolute top-6 right-6 flex items-center justify-center w-8 h-8 bg-white/8 rounded-lg backdrop-blur-[2px] cursor-pointer'
  235. onClick={onCancel}>
  236. <RiCloseLine className='w-4 h-4 text-gray-500'/>
  237. </div>
  238. </Tooltip>
  239. </div>,
  240. document.body,
  241. )
  242. }
  243. export default ImagePreview