@files-preview-app/preview-file 1.3.3 → 1.3.4

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.js CHANGED
@@ -5465,6 +5465,24 @@ function renderRtfTableElement(table) {
5465
5465
  tableEl.appendChild(tbody);
5466
5466
  return tableEl;
5467
5467
  }
5468
+ function extractMediaFingerprint(el) {
5469
+ const img = el.tagName.toLowerCase() === "img" ? el : el.querySelector("img");
5470
+ if (img && img.src) {
5471
+ const s = img.src;
5472
+ return s.length > 300 ? `img_${s.slice(0, 80)}_${s.length}_${s.slice(-80)}` : `img_${s}`;
5473
+ }
5474
+ const innerImg = el.querySelector("image");
5475
+ if (innerImg) {
5476
+ const h = innerImg.getAttribute("xlink:href") || innerImg.getAttribute("href") || "";
5477
+ if (h) return h.length > 300 ? `svgimg_${h.slice(0, 80)}_${h.length}_${h.slice(-80)}` : `svgimg_${h}`;
5478
+ }
5479
+ const svg = el.tagName.toLowerCase() === "svg" ? el : el.querySelector("svg");
5480
+ if (svg) {
5481
+ const vb = svg.getAttribute("viewBox") || "";
5482
+ return `svg_${vb}_${svg.innerHTML.length}`;
5483
+ }
5484
+ return null;
5485
+ }
5468
5486
  var RtfPlugin = class {
5469
5487
  id = "rtf";
5470
5488
  name = "Rich Text Format (RTF)";
@@ -5637,9 +5655,27 @@ var RtfPlugin = class {
5637
5655
  }
5638
5656
  const rawText = new TextDecoder("latin1").decode(ctx.buffer);
5639
5657
  const tables = parseRtfTables(rawText);
5640
- const doc = new RTFJS.Document(ctx.buffer, {});
5658
+ const seenMediaSignatures = /* @__PURE__ */ new Set();
5659
+ const doc = new RTFJS.Document(ctx.buffer, {
5660
+ onPicture: (isLegacy, createPic) => {
5661
+ if (isLegacy === true) {
5662
+ return null;
5663
+ }
5664
+ const pic = createPic();
5665
+ if (!pic) return null;
5666
+ const fp = extractMediaFingerprint(pic);
5667
+ if (fp) {
5668
+ if (seenMediaSignatures.has(fp)) {
5669
+ return null;
5670
+ }
5671
+ seenMediaSignatures.add(fp);
5672
+ }
5673
+ return pic;
5674
+ }
5675
+ });
5641
5676
  const htmlElements = await doc.render();
5642
5677
  const contentNodes = [];
5678
+ const seenInContentBlocks = /* @__PURE__ */ new Set();
5643
5679
  const extractBlocks = (nodes) => {
5644
5680
  for (const item of nodes) {
5645
5681
  if (!item || !(item instanceof HTMLElement)) continue;
@@ -5651,6 +5687,18 @@ var RtfPlugin = class {
5651
5687
  if (hasChildBlocks && !["table", "tr", "td", "th", "ul", "ol", "li"].includes(tag)) {
5652
5688
  extractBlocks(Array.from(item.children));
5653
5689
  } else {
5690
+ const fp = extractMediaFingerprint(item);
5691
+ if (fp) {
5692
+ if (seenInContentBlocks.has(fp)) {
5693
+ continue;
5694
+ }
5695
+ seenInContentBlocks.add(fp);
5696
+ }
5697
+ const hasMedia = item.querySelector("img, svg, canvas, table") !== null || ["img", "svg", "table"].includes(tag);
5698
+ const textContent = (item.textContent || "").trim();
5699
+ if (!hasMedia && textContent.length === 0) {
5700
+ continue;
5701
+ }
5654
5702
  const svgs = item.querySelectorAll("svg, img");
5655
5703
  svgs.forEach((s) => {
5656
5704
  const el = s;
@@ -5706,38 +5754,59 @@ var RtfPlugin = class {
5706
5754
  }
5707
5755
  }
5708
5756
  });
5709
- wrapper.innerHTML = "";
5710
- contentNodes.forEach((node) => wrapper.appendChild(node));
5711
- const childHeights = contentNodes.map((c) => {
5712
- const rectH = c.getBoundingClientRect ? c.getBoundingClientRect().height : 0;
5713
- const offH = c.offsetHeight || 0;
5714
- const realH = Math.max(rectH, offH);
5715
- if (realH > 0) return realH;
5716
- const textLen = c.textContent?.trim().length || 0;
5717
- return Math.max(24, Math.ceil(textLen / 95) * 22 + 12);
5718
- });
5719
- wrapper.innerHTML = "";
5720
- const createRtfCard = () => {
5757
+ const createRtfCard = (pageNum) => {
5721
5758
  const card = document.createElement("div");
5722
5759
  card.className = "fp-rtf-page-card";
5760
+ card.setAttribute("data-page-number", String(pageNum));
5723
5761
  card.style.backgroundColor = "#ffffff";
5724
- card.style.boxShadow = "0 4px 24px rgba(0,0,0,0.08)";
5762
+ card.style.boxShadow = "0 4px 24px rgba(0,0,0,0.08), 0 1px 3px rgba(0,0,0,0.04)";
5725
5763
  card.style.borderRadius = "4px";
5726
5764
  card.style.padding = "64px 56px";
5727
5765
  card.style.width = "816px";
5728
5766
  card.style.minHeight = "1056px";
5767
+ card.style.maxHeight = "1056px";
5768
+ card.style.overflow = "hidden";
5729
5769
  card.style.boxSizing = "border-box";
5730
5770
  card.style.marginBottom = "24px";
5731
5771
  card.style.fontFamily = 'Calibri, "Segoe UI", Arial, sans-serif';
5732
5772
  card.style.lineHeight = "1.6";
5733
5773
  card.style.color = "#1e293b";
5774
+ card.style.position = "relative";
5734
5775
  return card;
5735
5776
  };
5736
- let curCard = createRtfCard();
5777
+ wrapper.innerHTML = "";
5778
+ const measureCard = createRtfCard(0);
5779
+ measureCard.style.maxHeight = "none";
5780
+ measureCard.style.height = "auto";
5781
+ wrapper.appendChild(measureCard);
5782
+ contentNodes.forEach((node) => measureCard.appendChild(node));
5783
+ const childHeights = contentNodes.map((c) => {
5784
+ const rect = c.getBoundingClientRect ? c.getBoundingClientRect() : null;
5785
+ const rectH = rect ? rect.height : 0;
5786
+ const offH = c.offsetHeight || 0;
5787
+ let realH = Math.max(rectH, offH);
5788
+ try {
5789
+ const cs = window.getComputedStyle(c);
5790
+ realH += (parseFloat(cs.marginTop) || 0) + (parseFloat(cs.marginBottom) || 0);
5791
+ } catch {
5792
+ }
5793
+ const hasMedia = c.querySelector("img, svg") !== null || ["img", "svg"].includes(c.tagName.toLowerCase());
5794
+ const hasTable = c.querySelector("table") !== null || c.tagName.toLowerCase() === "table";
5795
+ if (hasMedia) {
5796
+ realH = Math.max(realH, 300);
5797
+ } else if (hasTable) {
5798
+ realH = Math.max(realH, 240);
5799
+ }
5800
+ if (realH > 0) return realH;
5801
+ const textLen = c.textContent?.trim().length || 0;
5802
+ return Math.max(28, Math.ceil(textLen / 75) * 26 + 16);
5803
+ });
5804
+ wrapper.innerHTML = "";
5805
+ let curCard = createRtfCard(1);
5737
5806
  wrapper.appendChild(curCard);
5738
5807
  pageElements = [curCard];
5739
5808
  let curH = 0;
5740
- const maxH = 928;
5809
+ const maxH = 860;
5741
5810
  for (let i = 0; i < contentNodes.length; i++) {
5742
5811
  const child = contentNodes[i];
5743
5812
  const chH = childHeights[i];
@@ -5745,8 +5814,10 @@ var RtfPlugin = class {
5745
5814
  if (curH === 0 && isChildEmpty) {
5746
5815
  continue;
5747
5816
  }
5748
- if (curH + chH > maxH && curCard.childNodes.length > 0) {
5749
- curCard = createRtfCard();
5817
+ const isTable = child.querySelector("table") !== null || child.tagName.toLowerCase() === "table";
5818
+ const shouldBreakBeforeTable = isTable && curH > 400 && curCard.childNodes.length > 0;
5819
+ if ((curH + chH > maxH || shouldBreakBeforeTable) && curCard.childNodes.length > 0) {
5820
+ curCard = createRtfCard(pageElements.length + 1);
5750
5821
  wrapper.appendChild(curCard);
5751
5822
  pageElements.push(curCard);
5752
5823
  curH = 0;