@ibiz-template-plugin/ibiz-file-previewer 0.0.11 → 0.0.13

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/dist/index.es.js CHANGED
@@ -151,7 +151,7 @@ function O(n2, u2, t2, i2, o2, r2, e2, f2, c2, s2) {
151
151
  else u2.__e = t2.__e, u2.__k = t2.__k, n3.then || z$1(u2);
152
152
  l$3.__e(n3, u2, t2);
153
153
  }
154
- else null == r2 && u2.__v == t2.__v ? (u2.__k = t2.__k, u2.__e = t2.__e) : f2 = u2.__e = q$2(t2.__e, u2, t2, i2, o2, r2, e2, c2, s2);
154
+ else null == r2 && u2.__v == t2.__v ? (u2.__k = t2.__k, u2.__e = t2.__e) : f2 = u2.__e = q$1(t2.__e, u2, t2, i2, o2, r2, e2, c2, s2);
155
155
  return (a2 = l$3.diffed) && a2(u2), 128 & u2.__u ? void 0 : f2;
156
156
  }
157
157
  function z$1(n2) {
@@ -172,7 +172,7 @@ function N$1(n2, u2, t2) {
172
172
  function V$1(n2) {
173
173
  return "object" != typeof n2 || null == n2 || n2.__b && n2.__b > 0 ? n2 : d$3(n2) ? n2.map(V$1) : w$2({}, n2);
174
174
  }
175
- function q$2(u2, t2, i2, o2, r2, e2, f2, c2, s2) {
175
+ function q$1(u2, t2, i2, o2, r2, e2, f2, c2, s2) {
176
176
  var a2, h2, v2, y2, w2, _2, m2, b2 = i2.props || p$3, k2 = t2.props, x2 = t2.type;
177
177
  if ("svg" == x2 ? r2 = "http://www.w3.org/2000/svg" : "math" == x2 ? r2 = "http://www.w3.org/1998/Math/MathML" : r2 || (r2 = "http://www.w3.org/1999/xhtml"), null != e2) {
178
178
  for (a2 = 0; a2 < e2.length; a2++) if ((w2 = e2[a2]) && "setAttribute" in w2 == !!x2 && (x2 ? w2.localName == x2 : 3 == w2.nodeType)) {
@@ -303,11 +303,6 @@ function T$1(n2, r2) {
303
303
  var u2 = p$2(t$1++, 7);
304
304
  return C(u2.__H, r2) && (u2.__ = n2(), u2.__H = r2, u2.__h = n2), u2.__;
305
305
  }
306
- function q$1(n2, t2) {
307
- return o$1 = 8, T$1(function() {
308
- return n2;
309
- }, t2);
310
- }
311
306
  function j$1() {
312
307
  for (var n2; n2 = f$2.shift(); ) if (n2.__P && n2.__H) try {
313
308
  n2.__H.__h.forEach(z), n2.__H.__h.forEach(B$1), n2.__H.__h = [];
@@ -1170,13 +1165,27 @@ const fileNameToFileType = (fileName) => {
1170
1165
  return fileType;
1171
1166
  };
1172
1167
  const throttle = (func, wait) => {
1173
- let timeout = null;
1168
+ let timeoutId = null;
1169
+ let lastCallTime = 0;
1174
1170
  return (...args) => {
1175
- if (!timeout) {
1176
- timeout = setTimeout(() => {
1177
- func(...args);
1178
- timeout = null;
1179
- }, wait);
1171
+ const now = Date.now();
1172
+ const remaining = wait - (now - lastCallTime);
1173
+ if (remaining <= 0) {
1174
+ lastCallTime = now;
1175
+ if (timeoutId) {
1176
+ clearTimeout(timeoutId);
1177
+ timeoutId = null;
1178
+ }
1179
+ func.apply(void 0, args);
1180
+ } else {
1181
+ if (timeoutId) {
1182
+ clearTimeout(timeoutId);
1183
+ }
1184
+ timeoutId = setTimeout(() => {
1185
+ lastCallTime = Date.now();
1186
+ timeoutId = null;
1187
+ func.apply(void 0, args);
1188
+ }, remaining);
1180
1189
  }
1181
1190
  };
1182
1191
  };
@@ -18295,27 +18304,35 @@ const ThumbnailPanel = (props) => {
18295
18304
  const [loadStates, setLoadStates] = d$2({});
18296
18305
  const ITEM_HEIGHT = 188;
18297
18306
  const BUFFER_COUNT = 1;
18298
- const [cachedImages, setCachedImages] = d$2(/* @__PURE__ */ new Set());
18307
+ const phantomHeight = props.pages.length * ITEM_HEIGHT;
18299
18308
  const [visibleRange, setVisibleRange] = d$2({ start: 0, end: 0 });
18300
- const calculateVisibleRange = q$1(() => {
18309
+ const [itemContainerOffsetY, setItemContainerOffsetY] = d$2(8);
18310
+ const calculateVisibleRange = (mode = "UI") => {
18301
18311
  if (!contentRef.current) return { start: 0, end: 0 };
18302
18312
  const container = contentRef.current;
18303
18313
  const scrollTop = container.scrollTop;
18304
18314
  const containerHeight = container.clientHeight;
18305
- const start = Math.max(
18306
- 0,
18307
- Math.floor(scrollTop / ITEM_HEIGHT) - BUFFER_COUNT
18308
- );
18315
+ let start = 0;
18316
+ if (mode === "UI") {
18317
+ start = Math.max(0, Math.floor(scrollTop / ITEM_HEIGHT) - BUFFER_COUNT);
18318
+ } else {
18319
+ start = Math.max(0, props.currentPage - BUFFER_COUNT);
18320
+ }
18309
18321
  const visibleCount = Math.ceil(containerHeight / ITEM_HEIGHT);
18310
18322
  const end = Math.min(
18311
18323
  props.pages.length,
18312
18324
  start + visibleCount + BUFFER_COUNT
18313
18325
  );
18314
18326
  return { start, end };
18315
- }, [props.pages.length]);
18316
- const updateVisibleRange = q$1(() => {
18317
- setVisibleRange(calculateVisibleRange());
18318
- }, [calculateVisibleRange]);
18327
+ };
18328
+ const updateVisibleRange = (mode) => {
18329
+ const range = calculateVisibleRange(mode);
18330
+ setItemContainerOffsetY(range.start * ITEM_HEIGHT + 8);
18331
+ setVisibleRange(range);
18332
+ };
18333
+ const throttleUpdateRange = throttle(() => {
18334
+ updateVisibleRange();
18335
+ }, 200);
18319
18336
  const handleClick = (index, _event) => {
18320
18337
  props.onPageClick(index);
18321
18338
  };
@@ -18332,85 +18349,117 @@ const ThumbnailPanel = (props) => {
18332
18349
  }
18333
18350
  }
18334
18351
  };
18335
- const handleImageLoad = (index) => {
18352
+ const handleImageLoad = (page) => {
18336
18353
  setLoadStates((prevState) => ({
18337
18354
  ...prevState,
18338
- [index]: true
18355
+ [page.image_url]: true
18339
18356
  }));
18340
- setCachedImages((prev) => new Set(prev).add(index));
18341
18357
  };
18342
- const handleImageError = (index, error2) => {
18358
+ const handleImageError = (page, error2) => {
18343
18359
  setLoadStates((prevState) => ({
18344
18360
  ...prevState,
18345
- [index]: true
18361
+ [page.image_url]: true
18346
18362
  }));
18347
- console.error("图片加载失败:", error2);
18363
+ console.error("路径为".concat(page.image_url, "的图片加载失败,"), error2);
18348
18364
  };
18349
- const getLoadState = (pageIndex) => {
18350
- return loadStates[pageIndex] || false;
18365
+ const getLoadState = (imageUrl) => {
18366
+ return loadStates[imageUrl] || false;
18351
18367
  };
18352
18368
  y$1(() => {
18353
18369
  const container = contentRef.current;
18354
18370
  if (container) {
18355
18371
  updateVisibleRange();
18356
- container.addEventListener("scroll", updateVisibleRange);
18372
+ container.addEventListener("scroll", throttleUpdateRange);
18357
18373
  return () => {
18358
- container.removeEventListener("scroll", updateVisibleRange);
18374
+ container.removeEventListener("scroll", throttleUpdateRange);
18359
18375
  };
18360
18376
  }
18361
- }, [updateVisibleRange]);
18377
+ });
18362
18378
  y$1(() => {
18363
- scrollToCurrentThumbnail();
18379
+ var _a;
18380
+ const activeThumbnail = (_a = contentRef.current) == null ? void 0 : _a.querySelector(
18381
+ ".thumbnail-item.active"
18382
+ );
18383
+ if (activeThumbnail) {
18384
+ scrollToCurrentThumbnail();
18385
+ } else {
18386
+ updateVisibleRange("DATA");
18387
+ setTimeout(() => {
18388
+ scrollToCurrentThumbnail();
18389
+ }, 0);
18390
+ }
18364
18391
  }, [props.currentPage]);
18365
18392
  return /* @__PURE__ */ u("div", { className: "thumbnail-panel", children: [
18366
18393
  /* @__PURE__ */ u("div", { className: "thumbnail-panel-header", children: /* @__PURE__ */ u("h3", { children: "缩略图" }) }),
18367
- /* @__PURE__ */ u("div", { className: "thumbnail-panel-content", ref: contentRef, children: props.pages && props.pages.length > 0 && props.pages.map((page, index) => {
18368
- const isLoaded = getLoadState(index);
18369
- const isCached = cachedImages.has(index);
18370
- const isVisible = visibleRange.start <= index && index <= visibleRange.end;
18371
- return /* @__PURE__ */ u(
18394
+ /* @__PURE__ */ u("div", { className: "thumbnail-panel-content", ref: contentRef, children: [
18395
+ /* @__PURE__ */ u(
18372
18396
  "div",
18373
18397
  {
18374
- className: "thumbnail-item ".concat(page.index === props.currentPage ? "active" : ""),
18375
- onClick: (event) => {
18376
- handleClick(index);
18398
+ class: "thumbnail-panel-phantom",
18399
+ style: { height: "".concat(phantomHeight, "px") }
18400
+ }
18401
+ ),
18402
+ /* @__PURE__ */ u(
18403
+ "div",
18404
+ {
18405
+ className: "thumbnail-item-container",
18406
+ style: {
18407
+ transform: "translate3d(0,".concat(itemContainerOffsetY, "px,0)")
18377
18408
  },
18378
- children: [
18379
- !isLoaded && /* @__PURE__ */ u(Loading, {}),
18380
- (isVisible || isCached) && /* @__PURE__ */ u(
18381
- "img",
18409
+ children: props.pages && props.pages.length > 0 && props.pages.map((page, index) => {
18410
+ const isVisible = visibleRange.start <= index && index <= visibleRange.end;
18411
+ if (!isVisible) {
18412
+ return null;
18413
+ }
18414
+ const isLoaded = getLoadState(page.image_url);
18415
+ return /* @__PURE__ */ u(
18416
+ "div",
18382
18417
  {
18383
- src: "".concat(imgBasePath).concat(page.image_url),
18384
- alt: page.description,
18385
- className: "thumbnail-image",
18386
- onLoad: () => {
18387
- handleImageLoad(index);
18418
+ className: "thumbnail-item ".concat(page.index === props.currentPage ? "active" : ""),
18419
+ onClick: (event) => {
18420
+ handleClick(index);
18388
18421
  },
18389
- onError: (error2) => {
18390
- handleImageError(index, error2);
18391
- }
18392
- }
18393
- ),
18394
- /* @__PURE__ */ u("div", { className: "thumbnail-page-number", children: index + 1 })
18395
- ]
18396
- },
18397
- index
18398
- );
18399
- }) })
18422
+ children: [
18423
+ !isLoaded && /* @__PURE__ */ u(Loading, {}),
18424
+ /* @__PURE__ */ u(
18425
+ "img",
18426
+ {
18427
+ src: "".concat(imgBasePath).concat(page.image_url),
18428
+ alt: page.description,
18429
+ className: "thumbnail-image",
18430
+ onLoad: () => {
18431
+ handleImageLoad(page);
18432
+ },
18433
+ onError: (error2) => {
18434
+ handleImageError(page, error2);
18435
+ }
18436
+ }
18437
+ ),
18438
+ /* @__PURE__ */ u("div", { className: "thumbnail-page-number", children: index + 1 })
18439
+ ]
18440
+ },
18441
+ page.image_url
18442
+ );
18443
+ })
18444
+ }
18445
+ )
18446
+ ] })
18400
18447
  ] });
18401
18448
  };
18402
18449
  const ImagePage = (props) => {
18403
18450
  const isLoading = useSignal(true);
18404
18451
  const imgRef = A(null);
18405
- const parentStyle = useSignal({
18452
+ const [parentDimensions, setParentDimensions] = d$2({
18406
18453
  width: 0,
18407
18454
  height: 0
18408
18455
  });
18456
+ const prevDimensions = A({ width: 0, height: 0 });
18409
18457
  const imgStyle = useComputed(() => {
18410
18458
  let result = {};
18459
+ const { width, height } = parentDimensions;
18411
18460
  if (isLoading.value) {
18412
18461
  result = {
18413
- height: "".concat(parentStyle.value.height * props.state.value.scale, "px"),
18462
+ height: "".concat(height * props.state.value.scale, "px"),
18414
18463
  flexShrink: 0
18415
18464
  };
18416
18465
  } else {
@@ -18420,11 +18469,11 @@ const ImagePage = (props) => {
18420
18469
  };
18421
18470
  if (props.state.value.mode === ImageMode.WIDTH) {
18422
18471
  Object.assign(result, {
18423
- width: "".concat((parentStyle.value.width - 24 - 6) * props.state.value.scale, "px")
18472
+ width: "".concat((width - 24 - 6) * props.state.value.scale, "px")
18424
18473
  });
18425
18474
  } else if (props.state.value.mode === ImageMode.PAGE) {
18426
18475
  Object.assign(result, {
18427
- height: "".concat(parentStyle.value.height * props.state.value.scale, "px")
18476
+ height: "".concat(height * props.state.value.scale, "px")
18428
18477
  });
18429
18478
  } else {
18430
18479
  Object.assign(result, {
@@ -18445,14 +18494,20 @@ const ImagePage = (props) => {
18445
18494
  var _a, _b;
18446
18495
  const rect = (_b = (_a = imgRef.current) == null ? void 0 : _a.parentElement) == null ? void 0 : _b.getBoundingClientRect();
18447
18496
  if (!rect) return;
18448
- parentStyle.value = {
18449
- height: rect.height,
18450
- width: rect.width
18451
- };
18497
+ const newWidth = Math.round(rect.width);
18498
+ const newHeight = Math.round(rect.height);
18499
+ if (prevDimensions.current.width !== newWidth || prevDimensions.current.height !== newHeight) {
18500
+ prevDimensions.current = { width: newWidth, height: newHeight };
18501
+ setParentDimensions({ width: newWidth, height: newHeight });
18502
+ }
18452
18503
  };
18453
18504
  y$1(() => {
18454
18505
  updateParentStyle();
18455
- const observer = new ResizeObserver(updateParentStyle);
18506
+ const observer = new ResizeObserver(() => {
18507
+ requestAnimationFrame(() => {
18508
+ updateParentStyle();
18509
+ });
18510
+ });
18456
18511
  if (imgRef.current) {
18457
18512
  if (imgRef.current.parentElement) {
18458
18513
  observer.observe(imgRef.current.parentElement);
@@ -18495,13 +18550,17 @@ const FilePreviewImageGroup = (props) => {
18495
18550
  const state = controller.state;
18496
18551
  const contentRef = A(null);
18497
18552
  const isScrolling = useSignal(false);
18498
- const [loadedPages, setLoadedPages] = d$2(/* @__PURE__ */ new Set());
18499
18553
  const computeClosestPage = () => {
18500
18554
  if (contentRef.current) {
18501
18555
  const containerRect = contentRef.current.getBoundingClientRect();
18502
18556
  let closestPage = state.value.currentPage;
18503
18557
  let minDistance = Infinity;
18504
- state.value.items.forEach((_2, index) => {
18558
+ const checkRangeStart = Math.max(0, closestPage - 200);
18559
+ const checkRangeEnd = Math.min(
18560
+ state.value.items.length - 1,
18561
+ closestPage + 200
18562
+ );
18563
+ for (let index = checkRangeStart; index <= checkRangeEnd; index++) {
18505
18564
  const pageElement = contentRef.current.querySelector(
18506
18565
  '[data-page-index="'.concat(index, '"]')
18507
18566
  );
@@ -18513,7 +18572,7 @@ const FilePreviewImageGroup = (props) => {
18513
18572
  closestPage = index;
18514
18573
  }
18515
18574
  }
18516
- });
18575
+ }
18517
18576
  return closestPage;
18518
18577
  }
18519
18578
  };
@@ -18524,7 +18583,7 @@ const FilePreviewImageGroup = (props) => {
18524
18583
  controller.updateState({ currentPage: closestPage });
18525
18584
  }
18526
18585
  }
18527
- }, 100);
18586
+ }, 200);
18528
18587
  const handleImageResize = () => {
18529
18588
  if (contentRef.current) {
18530
18589
  setTimeout(() => {
@@ -18546,8 +18605,8 @@ const FilePreviewImageGroup = (props) => {
18546
18605
  }
18547
18606
  };
18548
18607
  const preloadImages = () => {
18549
- const visiblePages = state.value.items.slice(0, 2);
18550
- visiblePages.forEach((page) => {
18608
+ const preloadPages = state.value.items.slice(0, 2);
18609
+ preloadPages.forEach((page) => {
18551
18610
  if (page) {
18552
18611
  const img = new Image();
18553
18612
  img.src = "".concat(imgBasePath).concat(page.image_url);
@@ -18604,6 +18663,7 @@ const FilePreviewImageGroup = (props) => {
18604
18663
  };
18605
18664
  const handlePageClick = (pageIndex) => {
18606
18665
  controller.updateState({ currentPage: pageIndex });
18666
+ updateIsScrolling();
18607
18667
  scrollToPageWhenStable(pageIndex);
18608
18668
  };
18609
18669
  const handleInput = (e2) => {
@@ -18677,7 +18737,7 @@ const FilePreviewImageGroup = (props) => {
18677
18737
  handleScroll();
18678
18738
  },
18679
18739
  children: state.value.items && state.value.items.length > 0 && state.value.items.map((page, index) => {
18680
- const bufferRange = 1;
18740
+ const bufferRange = 3;
18681
18741
  const startRange = Math.max(
18682
18742
  0,
18683
18743
  state.value.currentPage - bufferRange
@@ -18687,11 +18747,8 @@ const FilePreviewImageGroup = (props) => {
18687
18747
  state.value.currentPage + bufferRange
18688
18748
  );
18689
18749
  const shouldRender = index >= startRange && index <= endRange;
18690
- const shouldKeepLoaded = loadedPages.has(index);
18691
- if (shouldRender) {
18692
- if (!loadedPages.has(index)) {
18693
- setLoadedPages((prev) => new Set(prev).add(index));
18694
- }
18750
+ if (!shouldRender) {
18751
+ return null;
18695
18752
  }
18696
18753
  return /* @__PURE__ */ u(
18697
18754
  ImagePage,
@@ -18699,9 +18756,9 @@ const FilePreviewImageGroup = (props) => {
18699
18756
  page,
18700
18757
  index,
18701
18758
  state,
18702
- isShowImage: shouldRender || shouldKeepLoaded
18759
+ isShowImage: true
18703
18760
  },
18704
- index
18761
+ page.image_url
18705
18762
  );
18706
18763
  })
18707
18764
  }