@koi-br/ocr-web-sdk 1.0.40 → 1.0.41

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koi-br/ocr-web-sdk",
3
- "version": "1.0.40",
3
+ "version": "1.0.41",
4
4
  "description": "一个支持多种Office文件格式预览的Vue3组件SDK,包括PDF、Word、Excel、图片、OFD、TIF等格式",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",
@@ -444,7 +444,10 @@ const isScrollPaging = ref(false); // 标记是否正在进行滚动翻页
444
444
 
445
445
  // ResizeObserver 相关
446
446
  let resizeObserver: ResizeObserver | null = null;
447
- let resizeTimer: any = null; // 防抖定时器
447
+ let resizeTimer: any = null; // handleContainerResize 内部的定时器
448
+ let resizeDebounceTimer: any = null; // ResizeObserver 的防抖定时器
449
+ let isResizing = false; // 标记是否正在处理 resize
450
+ let lastContainerWidth = 0; // 记录上次容器宽度,用于判断是否真的变化了
448
451
 
449
452
  // 图片和容器引用
450
453
  const containerRef = ref<HTMLElement>();
@@ -1047,6 +1050,10 @@ const onImageLoad = (event: Event, pageNum: number) => {
1047
1050
  if (autoScale > 0) {
1048
1051
  scale.value = autoScale;
1049
1052
  initialAutoFitScale.value = autoScale; // 记录初始自适应缩放比例
1053
+ // 记录当前容器宽度,用于后续 resize 检查
1054
+ if (containerRef.value) {
1055
+ lastContainerWidth = containerRef.value.getBoundingClientRect().width;
1056
+ }
1050
1057
  console.log('[ImagePreview] onImageLoad: 缩放比例已设置:', autoScale);
1051
1058
  } else {
1052
1059
  console.warn('[ImagePreview] onImageLoad: 计算出的缩放比例无效:', autoScale);
@@ -2384,6 +2391,33 @@ const handleContainerResize = () => {
2384
2391
  return;
2385
2392
  }
2386
2393
 
2394
+ // 如果正在计算中或正在处理 resize,跳过(避免重复计算)
2395
+ if (isCalculatingAutoFit.value || isResizing) {
2396
+ return;
2397
+ }
2398
+
2399
+ // 检查容器尺寸是否真的变化了(避免无意义的重复计算)
2400
+ if (containerRef.value) {
2401
+ const currentWidth = containerRef.value.getBoundingClientRect().width;
2402
+ // 如果宽度变化小于 5px,认为是渲染抖动,不处理
2403
+ if (Math.abs(currentWidth - lastContainerWidth) < 5) {
2404
+ return;
2405
+ }
2406
+ lastContainerWidth = currentWidth;
2407
+ }
2408
+
2409
+ console.log('[ImagePreview] handleContainerResize 被调用:', {
2410
+ autoFitWidth: props.autoFitWidth,
2411
+ isUserZooming: isUserZooming.value,
2412
+ isImageReady: isImageReady.value,
2413
+ isCalculatingAutoFit: isCalculatingAutoFit.value,
2414
+ hasFirstPageSize: !!imageSizes.get(1),
2415
+ containerWidth: containerRef.value?.getBoundingClientRect()?.width,
2416
+ });
2417
+
2418
+ // 标记正在处理 resize
2419
+ isResizing = true;
2420
+
2387
2421
  // 清除之前的定时器
2388
2422
  if (resizeTimer) {
2389
2423
  clearTimeout(resizeTimer);
@@ -2391,31 +2425,46 @@ const handleContainerResize = () => {
2391
2425
  }
2392
2426
 
2393
2427
  // 隐藏图片,显示 loading
2428
+ console.log('[ImagePreview] handleContainerResize: 开始重新计算');
2394
2429
  isImageReady.value = false;
2395
2430
  isCalculatingAutoFit.value = true;
2396
2431
 
2397
2432
  // 立即计算并应用新的缩放比例,避免过渡期间露出底色
2398
2433
  // 使用 requestAnimationFrame 确保在浏览器重绘前更新
2399
2434
  requestAnimationFrame(() => {
2400
- const newScale = calculateAutoFitScale();
2401
- if (newScale > 0) {
2402
- // 即使变化很小也立即更新,确保过渡期间图片始终填满容器
2403
- scale.value = newScale;
2404
- initialAutoFitScale.value = newScale;
2435
+ try {
2436
+ console.log('[ImagePreview] handleContainerResize: 开始计算自适应宽度...');
2437
+ const newScale = calculateAutoFitScale();
2438
+ console.log('[ImagePreview] handleContainerResize: 计算结果:', newScale);
2439
+
2440
+ if (newScale > 0) {
2441
+ // 即使变化很小也立即更新,确保过渡期间图片始终填满容器
2442
+ scale.value = newScale;
2443
+ initialAutoFitScale.value = newScale;
2444
+ }
2445
+ } catch (error) {
2446
+ console.error('[ImagePreview] handleContainerResize: 计算失败:', error);
2405
2447
  }
2406
2448
 
2407
2449
  // 在过渡动画完成后再次检查,确保最终状态正确(处理过渡动画期间的连续变化)
2408
2450
  resizeTimer = setTimeout(() => {
2409
- const finalScale = calculateAutoFitScale();
2410
- if (finalScale > 0 && Math.abs(finalScale - scale.value) > 0.01) {
2411
- scale.value = finalScale;
2412
- initialAutoFitScale.value = finalScale;
2451
+ try {
2452
+ const finalScale = calculateAutoFitScale();
2453
+ if (finalScale > 0 && Math.abs(finalScale - scale.value) > 0.01) {
2454
+ scale.value = finalScale;
2455
+ initialAutoFitScale.value = finalScale;
2456
+ }
2457
+ } catch (error) {
2458
+ console.error('[ImagePreview] handleContainerResize: 最终计算失败:', error);
2459
+ } finally {
2460
+ // 计算完成后,显示图片并隐藏 loading
2461
+ console.log('[ImagePreview] handleContainerResize: 更新状态完成');
2462
+ isCalculatingAutoFit.value = false;
2463
+ isResizing = false; // 重置标记
2464
+ requestAnimationFrame(() => {
2465
+ isImageReady.value = true;
2466
+ });
2413
2467
  }
2414
- // 计算完成后,显示图片并隐藏 loading
2415
- isCalculatingAutoFit.value = false;
2416
- requestAnimationFrame(() => {
2417
- isImageReady.value = true;
2418
- });
2419
2468
  }, 350); // 350ms 延迟,略大于过渡动画时间(300ms),确保过渡完成后稳定
2420
2469
  });
2421
2470
  };
@@ -2473,11 +2522,17 @@ onMounted(() => {
2473
2522
  nextTick(() => {
2474
2523
  if (containerRef.value && typeof ResizeObserver !== 'undefined') {
2475
2524
  resizeObserver = new ResizeObserver((entries) => {
2476
- // 使用 entries 参数立即获取新尺寸,避免延迟
2477
- for (const entry of entries) {
2478
- // 立即响应尺寸变化,避免过渡期间露出底色
2479
- handleContainerResize();
2525
+ // 使用防抖,避免频繁触发
2526
+ if (resizeDebounceTimer) {
2527
+ clearTimeout(resizeDebounceTimer);
2480
2528
  }
2529
+ resizeDebounceTimer = setTimeout(() => {
2530
+ // 使用 entries 参数立即获取新尺寸,避免延迟
2531
+ for (const entry of entries) {
2532
+ // 立即响应尺寸变化,避免过渡期间露出底色
2533
+ handleContainerResize();
2534
+ }
2535
+ }, 100); // 100ms 防抖,避免频繁触发
2481
2536
  });
2482
2537
  resizeObserver.observe(containerRef.value);
2483
2538
  } else {
@@ -2503,6 +2558,10 @@ onBeforeUnmount(() => {
2503
2558
  clearTimeout(resizeTimer);
2504
2559
  resizeTimer = null;
2505
2560
  }
2561
+ if (resizeDebounceTimer) {
2562
+ clearTimeout(resizeDebounceTimer);
2563
+ resizeDebounceTimer = null;
2564
+ }
2506
2565
  if (resizeObserver) {
2507
2566
  resizeObserver.disconnect();
2508
2567
  resizeObserver = null;