@blocklet/editor 2.5.46 → 2.5.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -34,6 +34,7 @@ import { useMediumZoom } from '../hooks/medium-zoom';
34
34
  import { FrameMockup, isDeviceFrame } from '../ui/FrameMockup';
35
35
  import { useSafeArea } from '../../ext/SafeAreaPlugin';
36
36
  import { UPDATE_BUSY_COMMAND } from '../../ext/BusyPlugin';
37
+ import { getDevicePixelRatio } from '../utils/images';
37
38
  const ImageAnnotation = lazy(() => import('../ui/ImageEnhancer').then((module) => ({ default: module.ImageAnnotation })));
38
39
  const ImageAnnotationView = lazy(() => import('../ui/ImageEnhancer').then((module) => ({ default: module.ImageAnnotationView })));
39
40
  const ImageEnhancer = lazy(() => import('../ui/ImageEnhancer').then((module) => ({ default: module.ImageEnhancer })));
@@ -362,6 +363,7 @@ function formatFileSize(size) {
362
363
  function SimpleImageComponent({ src, altText = '', width, height, }) {
363
364
  const [loaded, setLoaded] = useState(false);
364
365
  const imageRef = useRef(null);
366
+ const containerRef = useRef(null);
365
367
  const hasFixedSize = typeof width === 'number' && typeof height === 'number' && width > 0 && height > 0;
366
368
  const { maxContentWidth } = useEditorConfig();
367
369
  const displaySrc = useMemo(() => {
@@ -369,18 +371,39 @@ function SimpleImageComponent({ src, altText = '', width, height, }) {
369
371
  return src;
370
372
  try {
371
373
  const qs = getQuery(src);
374
+ const dpr = getDevicePixelRatio();
372
375
  if (qs?.imageFilter)
373
376
  return src;
374
377
  // 图片剪裁,限制最大宽度
375
378
  return withQuery(src, {
376
379
  imageFilter: 'resize',
377
- w: maxContentWidth,
380
+ w: Math.round(maxContentWidth * dpr),
378
381
  });
379
382
  }
380
383
  catch {
381
384
  return src;
382
385
  }
383
386
  }, [src, maxContentWidth]);
387
+ // 当图片出现在 viewport 中时,预加载原图
388
+ useEffect(() => {
389
+ if (!containerRef.current || displaySrc === src)
390
+ return;
391
+ const observer = new IntersectionObserver((entries) => {
392
+ entries.forEach((entry) => {
393
+ if (entry.isIntersecting) {
394
+ const preloadImg = new Image();
395
+ preloadImg.src = src;
396
+ observer.disconnect();
397
+ }
398
+ });
399
+ }, {
400
+ rootMargin: '50px',
401
+ });
402
+ observer.observe(containerRef.current);
403
+ return () => {
404
+ observer.disconnect();
405
+ };
406
+ }, [src, displaySrc]);
384
407
  useMediumZoom(imageRef);
385
408
  const renderSkeleton = () => {
386
409
  if (hasFixedSize) {
@@ -388,7 +411,7 @@ function SimpleImageComponent({ src, altText = '', width, height, }) {
388
411
  }
389
412
  return _jsx(Skeleton, { variant: "rectangular", animation: "wave", sx: { width: 300, height: 200, borderRadius: 1 } });
390
413
  };
391
- return (_jsxs(_Fragment, { children: [!loaded && renderSkeleton(), _jsx(Box, { sx: {
414
+ return (_jsxs(_Fragment, { children: [!loaded && renderSkeleton(), _jsx(Box, { ref: containerRef, sx: {
392
415
  p: 1,
393
416
  border: 1,
394
417
  borderColor: 'divider',
@@ -7,3 +7,4 @@ export declare const scaleImage: ({ width, height, maxSize }: {
7
7
  height: number;
8
8
  scaleFactor: number;
9
9
  };
10
+ export declare function getDevicePixelRatio(): number;
@@ -11,3 +11,6 @@ export const scaleImage = ({ width, height, maxSize }) => {
11
11
  }
12
12
  return { width: newWidth, height: newHeight, scaleFactor };
13
13
  };
14
+ export function getDevicePixelRatio() {
15
+ return window?.devicePixelRatio || 1;
16
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/editor",
3
- "version": "2.5.46",
3
+ "version": "2.5.48",
4
4
  "main": "lib/index.js",
5
5
  "sideEffects": false,
6
6
  "publishConfig": {
@@ -73,7 +73,7 @@
73
73
  "ufo": "^1.5.4",
74
74
  "url-join": "^4.0.1",
75
75
  "zustand": "^4.5.5",
76
- "@blocklet/pdf": "2.5.46"
76
+ "@blocklet/pdf": "2.5.48"
77
77
  },
78
78
  "devDependencies": {
79
79
  "@babel/core": "^7.25.2",