previewPaneImageContent.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import type { CSSProperties, SyntheticEvent } from "react";
  2. import { useCallback, useMemo, useState } from "react";
  3. interface ImagePreviewContentProps {
  4. title: string;
  5. imageSource: string;
  6. fitToContainer?: boolean;
  7. sourcePath?: string;
  8. }
  9. interface ImageNaturalSize {
  10. source: string;
  11. width: number;
  12. height: number;
  13. }
  14. const IMAGE_PREVIEW_CHECKERBOARD_CLASS =
  15. "[background-color:var(--color-background)] [background-image:linear-gradient(45deg,var(--color-surface)_25%,transparent_25%),linear-gradient(-45deg,var(--color-surface)_25%,transparent_25%),linear-gradient(45deg,transparent_75%,var(--color-surface)_75%),linear-gradient(-45deg,transparent_75%,var(--color-surface)_75%)] [background-position:0_0,0_4px,4px_-4px,-4px_0] [background-size:8px_8px]";
  16. export function getImagePreviewPixelRatio(sourceName: string): number {
  17. const fileName = sourceName.replace(/\\/g, "/").split("/").at(-1) ?? "";
  18. const match = /@(\d+(?:\.\d+)?)x(?=(?:\.[^./\\]+)?$)/i.exec(fileName);
  19. const ratio = match ? Number(match[1]) : 1;
  20. return Number.isFinite(ratio) && ratio > 0 ? ratio : 1;
  21. }
  22. export function ImagePreviewContent({
  23. title,
  24. fitToContainer = false,
  25. imageSource,
  26. sourcePath,
  27. }: ImagePreviewContentProps) {
  28. const [naturalSize, setNaturalSize] = useState<ImageNaturalSize | null>(null);
  29. const pixelRatio = useMemo(
  30. () => getImagePreviewPixelRatio(sourcePath ?? title),
  31. [sourcePath, title],
  32. );
  33. const imageStyle = useMemo<CSSProperties | undefined>(() => {
  34. if (
  35. fitToContainer ||
  36. pixelRatio <= 1 ||
  37. naturalSize?.source !== imageSource ||
  38. naturalSize.width <= 0 ||
  39. naturalSize.height <= 0
  40. ) {
  41. return undefined;
  42. }
  43. return {
  44. // Retina 文件名里的 @2x/@3x 表示物理像素倍率,预览时折算为 CSS 像素,避免素材被放大显示。
  45. height: naturalSize.height / pixelRatio,
  46. width: naturalSize.width / pixelRatio,
  47. };
  48. }, [fitToContainer, imageSource, naturalSize, pixelRatio]);
  49. const handleLoad = useCallback(
  50. (event: SyntheticEvent<HTMLImageElement>) => {
  51. if (fitToContainer) {
  52. return;
  53. }
  54. const image = event.currentTarget;
  55. setNaturalSize({
  56. source: imageSource,
  57. width: image.naturalWidth,
  58. height: image.naturalHeight,
  59. });
  60. },
  61. [fitToContainer, imageSource],
  62. );
  63. const imageClassName = fitToContainer
  64. ? "h-full max-h-full w-full max-w-full object-contain"
  65. : "h-auto max-h-full w-auto max-w-full object-contain";
  66. return (
  67. <div
  68. className={`flex h-full min-h-0 items-center justify-center overflow-hidden p-10 ${IMAGE_PREVIEW_CHECKERBOARD_CLASS}`}
  69. >
  70. <img
  71. alt={title}
  72. className={imageClassName}
  73. onLoad={handleLoad}
  74. src={imageSource}
  75. style={imageStyle}
  76. />
  77. </div>
  78. );
  79. }
  80. export function SvgPreviewContent({
  81. title,
  82. svgContent,
  83. }: {
  84. title: string;
  85. svgContent: string;
  86. }) {
  87. return (
  88. <ImagePreviewContent
  89. fitToContainer
  90. title={title}
  91. imageSource={`data:image/svg+xml;charset=utf-8,${encodeURIComponent(svgContent)}`}
  92. />
  93. );
  94. }