pdf-tsx 0.17.2 → 0.17.3

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.
@@ -1 +1 @@
1
- const e=require("../../_virtual/_rolldown/runtime.cjs"),t=require("../../hooks/useLabels.cjs"),n=require("./ToolbarButton.cjs"),r=require("./PDFToolbar.module.cjs"),i=require("./PDFToolbarDropdownContext.cjs");let a=require("react");a=e.__toESM(a,1);let o=require("react/jsx-runtime");var s=40;function c({children:e,overflowIcon:c=`···`}){let l=t.useLabels(),u=(0,a.useRef)(null),d=(0,a.useRef)([]),f=(0,a.useRef)([]),[p,m]=(0,a.useState)(null),[h,g]=(0,a.useState)(!1),_=(0,a.useRef)(null),v=a.default.Children.toArray(e),y=(0,a.useCallback)(()=>{let e=u.current;if(!e)return;d.current.forEach((e,t)=>{if(e){let n=e.offsetWidth;n>0&&(f.current[t]=n)}});let t=f.current;if(t.length===0)return;let n=e.offsetWidth;if(t.reduce((e,t)=>e+t,0)<=n){m(null);return}let r=0,i=0;for(let e=0;e<t.length&&r+t[e]+s<=n;e++)r+=t[e],i++;m(i)},[]);(0,a.useLayoutEffect)(()=>{y()},[y]),(0,a.useEffect)(()=>{let e=u.current;if(!e)return;let t=new ResizeObserver(()=>y());return t.observe(e),()=>t.disconnect()},[y]),(0,a.useEffect)(()=>{if(!h)return;let e=e=>{_.current?.contains(e.target)||g(!1)};return document.addEventListener(`mousedown`,e),()=>document.removeEventListener(`mousedown`,e)},[h]);let b=p!==null&&p<v.length,x=b?v.slice(p):[];return(0,o.jsxs)(`div`,{ref:u,className:r.default.toolbar,children:[v.map((e,t)=>(0,o.jsx)(`div`,{ref:e=>{d.current[t]=e},className:r.default.item,style:p!==null&&t>=p?{display:`none`}:void 0,children:e},t)),b&&(0,o.jsxs)(`div`,{ref:_,className:r.default.overflowWrapper,children:[(0,o.jsx)(n.ToolbarButton,{onClick:()=>g(e=>!e),"aria-label":l.toolbarMoreOptions,"aria-expanded":h,"aria-haspopup":`true`,title:l.toolbarMoreOptions,className:h?r.default.overflowBtnActive:void 0,children:c}),h&&(0,o.jsx)(`div`,{className:r.default.dropdown,role:`menu`,children:x.map((e,t)=>{if(a.default.isValidElement(e)&&e.type.isSeparator)return(0,o.jsx)(i.PDFToolbarDropdownContext.Provider,{value:!0,children:e},t);let n=a.default.isValidElement(e)?e.type.toolbarLabelKey:void 0,s=n?l[n]:void 0;return(0,o.jsx)(`div`,{className:r.default.dropdownItem,role:`menuitem`,children:s?(0,o.jsxs)(`div`,{className:r.default.labeledItem,children:[(0,o.jsx)(`span`,{className:r.default.itemLabel,children:s}),(0,o.jsx)(`div`,{className:r.default.itemContent,children:e})]}):(0,o.jsx)(i.PDFToolbarDropdownContext.Provider,{value:!0,children:e})},t)})})]})]})}exports.PDFToolbar=c;
1
+ const e=require("../../_virtual/_rolldown/runtime.cjs"),t=require("../../hooks/useLabels.cjs"),n=require("./ToolbarButton.cjs"),r=require("./PDFToolbar.module.cjs"),i=require("./PDFToolbarDropdownContext.cjs");let a=require("react");a=e.__toESM(a,1);let o=require("react/jsx-runtime");var s=40;function c({children:e,overflowIcon:c=`···`}){let l=t.useLabels(),u=(0,a.useRef)(null),d=(0,a.useRef)([]),f=(0,a.useRef)([]),[p,m]=(0,a.useState)(null),[h,g]=(0,a.useState)(!1),_=(0,a.useRef)(null),v=a.default.Children.toArray(e),y=(0,a.useCallback)(()=>{let e=u.current;if(!e)return;d.current.forEach((e,t)=>{if(e){let n=e.offsetWidth;n>0&&(f.current[t]=n)}});let t=f.current;if(t.length===0)return;let n=e.offsetWidth;if(t.reduce((e,t)=>e+t,0)<=n){m(null);return}let r=0,i=0;for(let e=0;e<t.length&&r+t[e]+s<=n;e++)r+=t[e],i++;m(i)},[]);(0,a.useLayoutEffect)(()=>{y()},[y]),(0,a.useEffect)(()=>{let e=u.current;if(!e)return;let t=new ResizeObserver(()=>y());return t.observe(e),d.current.forEach(e=>{e&&t.observe(e)}),()=>t.disconnect()},[y,v.length]),(0,a.useEffect)(()=>{if(!h)return;let e=e=>{_.current?.contains(e.target)||g(!1)};return document.addEventListener(`mousedown`,e),()=>document.removeEventListener(`mousedown`,e)},[h]);let b=p!==null&&p<v.length,x=b?v.slice(p):[];return(0,o.jsxs)(`div`,{ref:u,className:r.default.toolbar,children:[v.map((e,t)=>(0,o.jsx)(`div`,{ref:e=>{d.current[t]=e},className:r.default.item,style:p!==null&&t>=p?{display:`none`}:void 0,children:e},t)),b&&(0,o.jsxs)(`div`,{ref:_,className:r.default.overflowWrapper,children:[(0,o.jsx)(n.ToolbarButton,{onClick:()=>g(e=>!e),"aria-label":l.toolbarMoreOptions,"aria-expanded":h,"aria-haspopup":`true`,title:l.toolbarMoreOptions,className:h?r.default.overflowBtnActive:void 0,children:c}),h&&(0,o.jsx)(`div`,{className:r.default.dropdown,role:`menu`,children:x.map((e,t)=>{if(a.default.isValidElement(e)&&e.type.isSeparator)return(0,o.jsx)(i.PDFToolbarDropdownContext.Provider,{value:!0,children:e},t);let n=a.default.isValidElement(e)?e.type.toolbarLabelKey:void 0,s=n?l[n]:void 0;return(0,o.jsx)(`div`,{className:r.default.dropdownItem,role:`menuitem`,children:s?(0,o.jsxs)(`div`,{className:r.default.labeledItem,children:[(0,o.jsx)(`span`,{className:r.default.itemLabel,children:s}),(0,o.jsx)(`div`,{className:r.default.itemContent,children:e})]}):(0,o.jsx)(i.PDFToolbarDropdownContext.Provider,{value:!0,children:e})},t)})})]})]})}exports.PDFToolbar=c;
@@ -1 +1 @@
1
- const e=require("../_virtual/_rolldown/runtime.cjs");let t=require("react"),n=require("pdfjs-dist");n=e.__toESM(n,1);function r({pdf:e,zoom:n,rotation:r,visiblePages:i,canvasRefs:a}){let[o,s]=(0,t.useState)([]),[c,l]=(0,t.useState)(new Set),u=(0,t.useRef)([]),d=(0,t.useRef)(new Set),f=(0,t.useRef)(!1);return(0,t.useEffect)(()=>{if(!e)return;f.current=!1,d.current=new Set,l(new Set);let t=!1;return(async()=>{let i=u.current.map(e=>e?(e.cancel(),e.promise.catch(()=>{})):Promise.resolve());if(await Promise.all(i),u.current=Array(e.numPages).fill(null),t)return;let a=await e.getPage(1);if(t)return;let o=a.getViewport({scale:n,rotation:r}),c={width:o.width,height:o.height};s(Array(e.numPages).fill(c));let l=await Promise.all(Array.from({length:e.numPages},async(t,i)=>{let o=(i===0?a:await e.getPage(i+1)).getViewport({scale:n,rotation:r});return{width:o.width,height:o.height}}));t||(f.current=!0,s(l))})(),()=>{t=!0,u.current.forEach(e=>e?.cancel())}},[e,n,r]),(0,t.useEffect)(()=>{if(!e||o.length!==e.numPages||!f.current)return;let t=!1;return(async()=>{for(let o of i){if(t||d.current.has(o))continue;let i=await e.getPage(o);if(t)return;let s=window.devicePixelRatio||1,c=i.getViewport({scale:n,rotation:r}),f=i.getViewport({scale:n*s,rotation:r}),p=a.current[o-1];if(!p)continue;let m=p.getContext(`2d`);if(!m)continue;let h=u.current[o-1];if(h&&(h.cancel(),await h.promise.catch(()=>{}),t))return;p.width=f.width,p.height=f.height,p.style.width=`${c.width}px`,p.style.height=`${c.height}px`;let g=i.render({canvasContext:m,viewport:f,canvas:p});u.current[o-1]=g;try{await g.promise,t||(d.current=new Set(d.current).add(o),l(e=>{if(e.has(o))return e;let t=new Set(e);return t.add(o),t}))}catch(e){e&&typeof e==`object`&&`name`in e&&e.name!==`RenderingCancelledException`&&console.error(`Render error:`,e)}finally{u.current[o-1]===g&&(u.current[o-1]=null)}}})(),()=>{t=!0}},[e,o,r,i,n,a]),{pageSizes:o,renderedPages:c}}exports.usePDFRenderer=r;
1
+ const e=require("../_virtual/_rolldown/runtime.cjs");let t=require("react"),n=require("pdfjs-dist");n=e.__toESM(n,1);function r(e,t,n,r){let i=e.getContext(`2d`);if(!i)return null;let a=t.getViewport({scale:n,rotation:r}),o=window.devicePixelRatio||1,s=Math.min(1,4096/Math.max(a.width,a.height)),c=Math.sqrt(Math.min(1,16777216/(a.width*a.height))),l=Math.max(.1,Math.min(o,s,c));l<o&&console.warn(`[pdf-tsx] canvas clamped to avoid blank render: page=${t.pageNumber} zoom=${n} dpr=${o} requested=${Math.round(a.width*o)}x${Math.round(a.height*o)}px clamped=${Math.round(a.width*l)}x${Math.round(a.height*l)}px`);let u=t.getViewport({scale:n*l,rotation:r});return e.width=u.width,e.height=u.height,e.style.width=`${a.width}px`,e.style.height=`${a.height}px`,t.render({canvasContext:i,viewport:u,canvas:e})}function i({pdf:e,zoom:n,rotation:i,visiblePages:a,canvasRefs:o}){let[s,c]=(0,t.useState)([]),[l,u]=(0,t.useState)(new Set),d=(0,t.useRef)([]),f=(0,t.useRef)(new Set),p=(0,t.useRef)(!1);return(0,t.useEffect)(()=>{if(!e)return;p.current=!1,f.current=new Set,u(new Set);let t=!1;return(async()=>{let r=d.current.map(e=>e?(e.cancel(),e.promise.catch(()=>{})):Promise.resolve());if(await Promise.all(r),d.current=Array(e.numPages).fill(null),t)return;let a=await e.getPage(1);if(t)return;let o=a.getViewport({scale:n,rotation:i}),s={width:o.width,height:o.height};c(Array(e.numPages).fill(s));let l=await Promise.all(Array.from({length:e.numPages},async(t,r)=>{let o=(r===0?a:await e.getPage(r+1)).getViewport({scale:n,rotation:i});return{width:o.width,height:o.height}}));t||(p.current=!0,c(l))})(),()=>{t=!0,d.current.forEach(e=>e?.cancel())}},[e,n,i]),(0,t.useEffect)(()=>{if(!e||s.length!==e.numPages||!p.current)return;let t=!1;return(async()=>{for(let s of a){if(t||f.current.has(s))continue;let a=await e.getPage(s);if(t)return;let c=o.current[s-1];if(!c)continue;let l=d.current[s-1];if(l&&(l.cancel(),await l.promise.catch(()=>{}),t))return;let p=r(c,a,n,i);if(p){d.current[s-1]=p;try{await p.promise,t||(f.current=new Set(f.current).add(s),u(e=>{if(e.has(s))return e;let t=new Set(e);return t.add(s),t}))}catch(e){e&&typeof e==`object`&&`name`in e&&e.name!==`RenderingCancelledException`&&console.error(`Render error:`,e)}finally{d.current[s-1]===p&&(d.current[s-1]=null)}}}})(),()=>{t=!0}},[e,s,i,a,n,o]),{pageSizes:s,renderedPages:l}}exports.usePDFRenderer=i;
@@ -1 +1 @@
1
- var e=60,t=10;async function n(n,r,i,a){let o=(await r.getPage(1)).getViewport({scale:1,rotation:a}),s=i.clientWidth-e;if(n===`width`)return s/o.width;let c=i.clientHeight-t*2;return Math.min(s/o.width,c/o.height)}exports.computeFit=n;
1
+ const e=require("./zoom.cjs");var t=60,n=10;async function r(r,i,a,o){if(a.clientWidth<=t)return 1;let s=(await i.getPage(1)).getViewport({scale:1,rotation:o}),c=a.clientWidth-t,l;if(r===`width`)l=c/s.width;else{let e=a.clientHeight-n*2;l=Math.min(c/s.width,e/s.height)}return Math.max(e.MIN_ZOOM,Math.min(5,l))}exports.computeFit=r;
@@ -33,8 +33,10 @@ function p({ children: p, overflowIcon: m = "···" }) {
33
33
  let e = g.current;
34
34
  if (!e) return;
35
35
  let t = new ResizeObserver(() => T());
36
- return t.observe(e), () => t.disconnect();
37
- }, [T]), o(() => {
36
+ return t.observe(e), _.current.forEach((e) => {
37
+ e && t.observe(e);
38
+ }), () => t.disconnect();
39
+ }, [T, w.length]), o(() => {
38
40
  if (!x) return;
39
41
  let e = (e) => {
40
42
  C.current?.contains(e.target) || S(!1);
@@ -1,96 +1,103 @@
1
1
  import { useEffect as e, useRef as t, useState as n } from "react";
2
2
  import "pdfjs-dist";
3
3
  //#region src/hooks/usePDFRenderer.ts
4
- function r({ pdf: r, zoom: i, rotation: a, visiblePages: o, canvasRefs: s }) {
5
- let [c, l] = n([]), [u, d] = n(/* @__PURE__ */ new Set()), f = t([]), p = t(/* @__PURE__ */ new Set()), m = t(!1);
4
+ function r(e, t, n, r) {
5
+ let i = e.getContext("2d");
6
+ if (!i) return null;
7
+ let a = t.getViewport({
8
+ scale: n,
9
+ rotation: r
10
+ }), o = window.devicePixelRatio || 1, s = Math.min(1, 4096 / Math.max(a.width, a.height)), c = Math.sqrt(Math.min(1, 16777216 / (a.width * a.height))), l = Math.max(.1, Math.min(o, s, c));
11
+ l < o && console.warn(`[pdf-tsx] canvas clamped to avoid blank render: page=${t.pageNumber} zoom=${n} dpr=${o} requested=${Math.round(a.width * o)}x${Math.round(a.height * o)}px clamped=${Math.round(a.width * l)}x${Math.round(a.height * l)}px`);
12
+ let u = t.getViewport({
13
+ scale: n * l,
14
+ rotation: r
15
+ });
16
+ return e.width = u.width, e.height = u.height, e.style.width = `${a.width}px`, e.style.height = `${a.height}px`, t.render({
17
+ canvasContext: i,
18
+ viewport: u,
19
+ canvas: e
20
+ });
21
+ }
22
+ function i({ pdf: i, zoom: a, rotation: o, visiblePages: s, canvasRefs: c }) {
23
+ let [l, u] = n([]), [d, f] = n(/* @__PURE__ */ new Set()), p = t([]), m = t(/* @__PURE__ */ new Set()), h = t(!1);
6
24
  return e(() => {
7
- if (!r) return;
8
- m.current = !1, p.current = /* @__PURE__ */ new Set(), d(/* @__PURE__ */ new Set());
25
+ if (!i) return;
26
+ h.current = !1, m.current = /* @__PURE__ */ new Set(), f(/* @__PURE__ */ new Set());
9
27
  let e = !1;
10
28
  return (async () => {
11
- let t = f.current.map((e) => e ? (e.cancel(), e.promise.catch(() => {})) : Promise.resolve());
12
- if (await Promise.all(t), f.current = Array(r.numPages).fill(null), e) return;
13
- let n = await r.getPage(1);
29
+ let t = p.current.map((e) => e ? (e.cancel(), e.promise.catch(() => {})) : Promise.resolve());
30
+ if (await Promise.all(t), p.current = Array(i.numPages).fill(null), e) return;
31
+ let n = await i.getPage(1);
14
32
  if (e) return;
15
- let o = n.getViewport({
16
- scale: i,
17
- rotation: a
33
+ let r = n.getViewport({
34
+ scale: a,
35
+ rotation: o
18
36
  }), s = {
19
- width: o.width,
20
- height: o.height
37
+ width: r.width,
38
+ height: r.height
21
39
  };
22
- l(Array(r.numPages).fill(s));
23
- let c = await Promise.all(Array.from({ length: r.numPages }, async (e, t) => {
24
- let o = (t === 0 ? n : await r.getPage(t + 1)).getViewport({
25
- scale: i,
26
- rotation: a
40
+ u(Array(i.numPages).fill(s));
41
+ let c = await Promise.all(Array.from({ length: i.numPages }, async (e, t) => {
42
+ let r = (t === 0 ? n : await i.getPage(t + 1)).getViewport({
43
+ scale: a,
44
+ rotation: o
27
45
  });
28
46
  return {
29
- width: o.width,
30
- height: o.height
47
+ width: r.width,
48
+ height: r.height
31
49
  };
32
50
  }));
33
- e || (m.current = !0, l(c));
51
+ e || (h.current = !0, u(c));
34
52
  })(), () => {
35
- e = !0, f.current.forEach((e) => e?.cancel());
53
+ e = !0, p.current.forEach((e) => e?.cancel());
36
54
  };
37
55
  }, [
38
- r,
39
56
  i,
40
- a
57
+ a,
58
+ o
41
59
  ]), e(() => {
42
- if (!r || c.length !== r.numPages || !m.current) return;
60
+ if (!i || l.length !== i.numPages || !h.current) return;
43
61
  let e = !1;
44
62
  return (async () => {
45
- for (let t of o) {
46
- if (e || p.current.has(t)) continue;
47
- let n = await r.getPage(t);
63
+ for (let t of s) {
64
+ if (e || m.current.has(t)) continue;
65
+ let n = await i.getPage(t);
48
66
  if (e) return;
49
- let o = window.devicePixelRatio || 1, c = n.getViewport({
50
- scale: i,
51
- rotation: a
52
- }), l = n.getViewport({
53
- scale: i * o,
54
- rotation: a
55
- }), u = s.current[t - 1];
56
- if (!u) continue;
57
- let m = u.getContext("2d");
58
- if (!m) continue;
59
- let h = f.current[t - 1];
60
- if (h && (h.cancel(), await h.promise.catch(() => {}), e)) return;
61
- u.width = l.width, u.height = l.height, u.style.width = `${c.width}px`, u.style.height = `${c.height}px`;
62
- let g = n.render({
63
- canvasContext: m,
64
- viewport: l,
65
- canvas: u
66
- });
67
- f.current[t - 1] = g;
68
- try {
69
- await g.promise, e || (p.current = new Set(p.current).add(t), d((e) => {
70
- if (e.has(t)) return e;
71
- let n = new Set(e);
72
- return n.add(t), n;
73
- }));
74
- } catch (e) {
75
- e && typeof e == "object" && "name" in e && e.name !== "RenderingCancelledException" && console.error("Render error:", e);
76
- } finally {
77
- f.current[t - 1] === g && (f.current[t - 1] = null);
67
+ let s = c.current[t - 1];
68
+ if (!s) continue;
69
+ let l = p.current[t - 1];
70
+ if (l && (l.cancel(), await l.promise.catch(() => {}), e)) return;
71
+ let u = r(s, n, a, o);
72
+ if (u) {
73
+ p.current[t - 1] = u;
74
+ try {
75
+ await u.promise, e || (m.current = new Set(m.current).add(t), f((e) => {
76
+ if (e.has(t)) return e;
77
+ let n = new Set(e);
78
+ return n.add(t), n;
79
+ }));
80
+ } catch (e) {
81
+ e && typeof e == "object" && "name" in e && e.name !== "RenderingCancelledException" && console.error("Render error:", e);
82
+ } finally {
83
+ p.current[t - 1] === u && (p.current[t - 1] = null);
84
+ }
78
85
  }
79
86
  }
80
87
  })(), () => {
81
88
  e = !0;
82
89
  };
83
90
  }, [
84
- r,
85
- c,
86
- a,
87
- o,
88
91
  i,
89
- s
92
+ l,
93
+ o,
94
+ s,
95
+ a,
96
+ c
90
97
  ]), {
91
- pageSizes: c,
92
- renderedPages: u
98
+ pageSizes: l,
99
+ renderedPages: d
93
100
  };
94
101
  }
95
102
  //#endregion
96
- export { r as usePDFRenderer };
103
+ export { i as usePDFRenderer };
@@ -1,13 +1,18 @@
1
+ import { MIN_ZOOM as e } from "./zoom.js";
1
2
  //#region src/utils/pdfFit.ts
2
- var e = 60, t = 10;
3
- async function n(n, r, i, a) {
4
- let o = (await r.getPage(1)).getViewport({
3
+ var t = 60, n = 10;
4
+ async function r(r, i, a, o) {
5
+ if (a.clientWidth <= t) return 1;
6
+ let s = (await i.getPage(1)).getViewport({
5
7
  scale: 1,
6
- rotation: a
7
- }), s = i.clientWidth - e;
8
- if (n === "width") return s / o.width;
9
- let c = i.clientHeight - t * 2;
10
- return Math.min(s / o.width, c / o.height);
8
+ rotation: o
9
+ }), c = a.clientWidth - t, l;
10
+ if (r === "width") l = c / s.width;
11
+ else {
12
+ let e = a.clientHeight - n * 2;
13
+ l = Math.min(c / s.width, e / s.height);
14
+ }
15
+ return Math.max(e, Math.min(5, l));
11
16
  }
12
17
  //#endregion
13
- export { n as computeFit };
18
+ export { r as computeFit };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pdf-tsx",
3
- "version": "0.17.2",
3
+ "version": "0.17.3",
4
4
  "description": "A fully-featured, composable PDF viewer component for React",
5
5
  "type": "module",
6
6
  "main": "./dist/cjs/index.cjs",