@react-pdf-kit/viewer 0.0.0-experimental.3 → 0.0.0-experimental.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.
Files changed (48) hide show
  1. package/dist/RPLayout.module-7766e0b4.js +14 -0
  2. package/dist/assets/style.css +1 -1
  3. package/dist/assets/style.js +15 -15
  4. package/dist/components/RPController.js +24 -20
  5. package/dist/components/RPPages.js +322 -321
  6. package/dist/components/RPProvider.js +22 -19
  7. package/dist/components/layout/LayoutContainer.js +1 -1
  8. package/dist/components/layout/LayoutWrapper.js +1 -1
  9. package/dist/components/layout/RPDefaultLayout.js +5 -3
  10. package/dist/components/layout/RPLayout.js +5 -3
  11. package/dist/components/layout/toolbar/RPToolbar.js +4 -2
  12. package/dist/components/layout/toolbar/SearchResultNavigator.js +4 -2
  13. package/dist/components/layout/toolbar/SearchTool.js +4 -2
  14. package/dist/components/layout/toolbar/ToolbarDefault.js +4 -2
  15. package/dist/components/layout/toolbar/ToolbarLayout.js +4 -2
  16. package/dist/components/layout/toolbar/tools/DualPageWithCoverTool.js +7 -4
  17. package/dist/components/layout/toolbar/tools/FirstPageTool.js +7 -4
  18. package/dist/components/layout/toolbar/tools/LastPageTool.js +7 -4
  19. package/dist/components/layout/toolbar/tools/defaults/RPHorizontalBar.js +4 -2
  20. package/dist/components/page/AnnotationLayer.js +52 -52
  21. package/dist/components/page/DualPage.js +9 -4
  22. package/dist/components/page/DualPageWithCover.js +9 -4
  23. package/dist/components/page/RPPage.js +9 -4
  24. package/dist/components/page/SinglePage.js +9 -4
  25. package/dist/components/page/TextHighlightLayer.js +84 -177
  26. package/dist/components/page/TextLayer.js +124 -170
  27. package/dist/contexts/HighlightContext.js +8 -6
  28. package/dist/contexts/SearchContext.js +6 -4
  29. package/dist/contexts/TextSelectionContext.js +191 -0
  30. package/dist/main.js +114 -112
  31. package/dist/types/contexts/TextSelectionContext.d.ts +14 -0
  32. package/dist/types/utils/geometryCache.d.ts +15 -0
  33. package/dist/types/utils/glyphHitTest.d.ts +12 -0
  34. package/dist/types/utils/hooks/useTextSelection.d.ts +12 -3
  35. package/dist/types/utils/selectionUtils.d.ts +30 -0
  36. package/dist/utils/geometryCache.js +32 -0
  37. package/dist/utils/glyphHitTest.js +29 -0
  38. package/dist/utils/highlight.js +184 -158
  39. package/dist/utils/hooks/useCopyText.js +60 -22
  40. package/dist/utils/hooks/useHighlight.js +35 -33
  41. package/dist/utils/hooks/useLicense.js +1 -1
  42. package/dist/utils/hooks/useSearch.js +4 -2
  43. package/dist/utils/hooks/useTextSelection.js +2 -73
  44. package/dist/utils/selectionUtils.js +96 -0
  45. package/package.json +2 -2
  46. package/dist/RPLayout.module-b4b23e29.js +0 -14
  47. package/dist/types/utils/hooks/useFlickerSelectText.d.ts +0 -1
  48. package/dist/utils/hooks/useFlickerSelectText.js +0 -25
@@ -14,6 +14,8 @@ import { getWordPositionInPage as ft } from "../getWordPositionInPage.js";
14
14
  import { usePagesRotateContext as gt } from "../../contexts/PagesRotateContext.js";
15
15
  import "../../de_DE-a553b162.js";
16
16
  import "../charators.js";
17
+ import "../selectionUtils.js";
18
+ import "../geometryCache.js";
17
19
  import "react/jsx-runtime";
18
20
  import "../appConsole.js";
19
21
  import "../../contexts/InitialStateContext.js";
@@ -36,7 +38,7 @@ import "../../contexts/VirtualGridContext.js";
36
38
  import "../smoothScrollTo.js";
37
39
  import "../../contexts/SmoothScrollContext.js";
38
40
  import "../calculatePage.js";
39
- const $t = (O = {}, U = "") => {
41
+ const ee = (O = {}, U = "") => {
40
42
  const [p, q] = C(U), [B, y] = C(!1), { pdf: _, pages: S } = it(), { widths: f, heights: R } = ut(), w = b(!p), { currentZoom: A } = at(), [s, g] = C([]), { setFocusedPage: D } = mt(), { columnCount: G, viewMode: V } = pt(), { pageRotate: v } = gt(), [c, i] = C(0), { pageScrollElementRef: W, virtualScrollableElementRef: Z } = st(), { scrollMode: h } = ct(), { pagesRef: L } = lt(), J = b({}), m = b(0), K = H(() => c === 0 ? null : s[c - 1], [c, s]), a = H(() => s.length, [s]), u = I(
41
43
  (e) => {
42
44
  if (e === 0)
@@ -159,5 +161,5 @@ const $t = (O = {}, U = "") => {
159
161
  };
160
162
  };
161
163
  export {
162
- $t as useSearch
164
+ ee as useSearch
163
165
  };
@@ -1,76 +1,5 @@
1
- import { useCallback as p, useEffect as b } from "react";
2
- const D = ({ onTextSelect: s, pagesRef: n }) => {
3
- const f = p(() => {
4
- const e = n == null ? void 0 : n.querySelectorAll('[data-rp$="-textLayer"]');
5
- return e ? Array.from(e) : [];
6
- }, [n]), N = p(() => {
7
- const e = document.getSelection();
8
- if (!e || e.rangeCount === 0 || e.toString().trim() === "")
9
- return null;
10
- const E = e.toString(), a = /* @__PURE__ */ new Map(), O = f();
11
- for (let i = 0; i < e.rangeCount; i++) {
12
- const t = e.getRangeAt(i);
13
- O.forEach((d) => {
14
- if (t.intersectsNode(d)) {
15
- const g = d.getAttribute("data-rp"), T = g == null ? void 0 : g.match(/page-(\d+)-textLayer/);
16
- if (T) {
17
- const h = parseInt(T[1]), C = d.closest('[data-rp*="page-"]');
18
- if (!C)
19
- return;
20
- const c = C.getBoundingClientRect();
21
- a.has(h) || a.set(h, {
22
- rects: [],
23
- pageRect: c
24
- });
25
- const y = a.get(h), _ = document.createTreeWalker(d, NodeFilter.SHOW_TEXT, null);
26
- let u;
27
- for (; u = _.nextNode(); )
28
- if (t.intersectsNode(u) && u.parentElement) {
29
- const o = document.createRange();
30
- o.selectNodeContents(u);
31
- const A = t.compareBoundaryPoints(Range.START_TO_START, o) > 0 ? t.startContainer : o.startContainer, B = t.compareBoundaryPoints(Range.START_TO_START, o) > 0 ? t.startOffset : o.startOffset, R = t.compareBoundaryPoints(Range.END_TO_END, o) < 0 ? t.endContainer : o.endContainer, S = t.compareBoundaryPoints(Range.END_TO_END, o) < 0 ? t.endOffset : o.endOffset, m = document.createRange();
32
- m.setStart(A, B), m.setEnd(R, S);
33
- const r = m.getBoundingClientRect();
34
- if (r.width === 0 || r.height === 0)
35
- continue;
36
- const P = {
37
- left: r.left - c.left,
38
- top: r.top - c.top,
39
- right: r.right - c.left,
40
- bottom: r.bottom - c.top,
41
- width: r.width,
42
- height: r.height
43
- };
44
- y.rects.push(P);
45
- }
46
- }
47
- }
48
- });
49
- }
50
- const w = Array.from(a.entries()).sort(([i], [t]) => i - t).map(([i, t]) => ({
51
- pageNumber: i,
52
- pageBasedBoundingRects: t.rects,
53
- pageDimension: {
54
- width: t.pageRect.width,
55
- height: t.pageRect.height
56
- },
57
- pagePositionInWindow: {
58
- x: t.pageRect.left,
59
- y: t.pageRect.top
60
- }
61
- }));
62
- return {
63
- text: E,
64
- pageSelections: w
65
- };
66
- }, [f]), l = p(() => {
67
- const e = N();
68
- e && (s == null || s(e));
69
- }, [N, s]);
70
- b(() => (n == null || n.addEventListener("pointerup", l), () => {
71
- n == null || n.removeEventListener("pointerup", l);
72
- }), [n, l]);
1
+ const o = (e) => {
73
2
  };
74
3
  export {
75
- D as useTextSelection
4
+ o as useTextSelection
76
5
  };
@@ -0,0 +1,96 @@
1
+ function M(l) {
2
+ const e = [];
3
+ for (const h of l.runs)
4
+ for (const i of h.glyphs)
5
+ e.push(i);
6
+ return e;
7
+ }
8
+ function w(l, e) {
9
+ const h = M(l);
10
+ if (h.length === 0)
11
+ return { start: e, end: e };
12
+ const i = Math.max(0, Math.min(e, h.length - 1));
13
+ let o = i;
14
+ for (; o > 0 && h[o - 1].flags === 0; )
15
+ o--;
16
+ let r = i;
17
+ for (; r < h.length - 1 && h[r + 1].flags === 0; )
18
+ r++;
19
+ return { start: o, end: r };
20
+ }
21
+ function R(l, e) {
22
+ let h = -1, i = 0;
23
+ for (let t = 0; t < l.runs.length; t++) {
24
+ const a = l.runs[t].glyphs.length;
25
+ if (e < i + a) {
26
+ h = t;
27
+ break;
28
+ }
29
+ i += a;
30
+ }
31
+ if (h === -1)
32
+ return { start: e, end: e };
33
+ const o = l.runs[h].rect, r = o.height;
34
+ let s = 1 / 0, n = -1 / 0, c = 0;
35
+ for (let t = 0; t < l.runs.length; t++) {
36
+ const a = l.runs[t], g = a.rect, x = Math.max(g.y, o.y), y = Math.min(g.y + g.height, o.y + o.height), p = Math.max(0, y - x), u = Math.min(g.height, r);
37
+ if (u > 0 && p / u >= 0.5) {
38
+ const f = c, m = c + a.glyphs.length - 1;
39
+ f < s && (s = f), m > n && (n = m);
40
+ }
41
+ c += a.glyphs.length;
42
+ }
43
+ return s === 1 / 0 ? { start: e, end: e } : { start: s, end: n };
44
+ }
45
+ function v(l, e, h) {
46
+ const i = M(l);
47
+ if (i.length === 0)
48
+ return [];
49
+ const o = Math.max(0, Math.min(e, h)), r = Math.min(i.length - 1, Math.max(e, h)), s = [];
50
+ let n = null;
51
+ for (let c = o; c <= r; c++) {
52
+ const t = i[c];
53
+ if (t.flags === 2)
54
+ continue;
55
+ if (!n) {
56
+ n = { x: t.x, y: t.y, width: t.width, height: t.height };
57
+ continue;
58
+ }
59
+ const a = Math.max(n.y, t.y), g = Math.min(
60
+ n.y + n.height,
61
+ t.y + t.height
62
+ ), x = Math.max(0, g - a), y = Math.min(n.height, t.height);
63
+ if (y > 0 && x / y >= 0.5) {
64
+ const u = Math.min(n.x, t.x), f = Math.min(n.y, t.y), m = Math.max(n.x + n.width, t.x + t.width), d = Math.max(
65
+ n.y + n.height,
66
+ t.y + t.height
67
+ );
68
+ n = {
69
+ x: u,
70
+ y: f,
71
+ width: m - u,
72
+ height: d - f
73
+ };
74
+ } else
75
+ s.push(n), n = { x: t.x, y: t.y, width: t.width, height: t.height };
76
+ }
77
+ return n && s.push(n), s;
78
+ }
79
+ function B(l, e, h) {
80
+ const i = Math.min(e, h), o = Math.max(e, h);
81
+ let r = "", s = 0;
82
+ for (const n of l.runs) {
83
+ if (s + n.text.length - 1 >= i && s <= o) {
84
+ const t = Math.max(0, i - s), a = Math.min(n.text.length, o - s + 1);
85
+ r += n.text.slice(t, a);
86
+ }
87
+ s += n.glyphs.length;
88
+ }
89
+ return r;
90
+ }
91
+ export {
92
+ R as expandToLineBoundary,
93
+ w as expandToWordBoundary,
94
+ v as rectsForRange,
95
+ B as textForRange
96
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-pdf-kit/viewer",
3
- "version": "0.0.0-experimental.3",
3
+ "version": "0.0.0-experimental.4",
4
4
  "private": false,
5
5
  "description": "A react-pdf-viewer component for React and Next.js. Suitable for react-pdf document.",
6
6
  "keywords": [
@@ -39,7 +39,7 @@
39
39
  "prepare": "husky"
40
40
  },
41
41
  "dependencies": {
42
- "@pdf-viewer/pdfium": "0.0.0-experimental.1",
42
+ "@pdf-viewer/pdfium": "0.0.0-experimental.4",
43
43
  "@radix-ui/react-checkbox": "^1.3.3",
44
44
  "@radix-ui/react-popover": "^1.1.15",
45
45
  "@radix-ui/react-tooltip": "^1.2.8",
@@ -1,14 +0,0 @@
1
- const r = {
2
- "rp-layout": "_rp-layout_dptrm_1",
3
- "rp-layout-wrapper": "_rp-layout-wrapper_dptrm_5",
4
- "rp-content": "_rp-content_dptrm_11",
5
- "rp-sidebar": "_rp-sidebar_dptrm_18",
6
- "rp-pages": "_rp-pages_dptrm_23",
7
- "rp-container": "_rp-container_dptrm_29",
8
- "rp-loader": "_rp-loader_dptrm_33",
9
- "rp-theme-variables": "_rp-theme-variables_dptrm_37",
10
- "rp-dark-mode": "_rp-dark-mode_dptrm_153"
11
- };
12
- export {
13
- r as c
14
- };
@@ -1 +0,0 @@
1
- export declare const useFlickerSelectText: () => void;
@@ -1,25 +0,0 @@
1
- import { useRef as u, useCallback as g, useEffect as f } from "react";
2
- const m = () => {
3
- const i = u(null), a = u(/firefox/i.test(navigator.userAgent)), o = g(() => {
4
- var c, d;
5
- const s = document.getSelection();
6
- if (!s || s.rangeCount === 0 || a.current)
7
- return;
8
- const t = s.getRangeAt(0), l = i.current && (t.compareBoundaryPoints(Range.END_TO_END, i.current) === 0 || t.compareBoundaryPoints(Range.START_TO_END, i.current) === 0);
9
- let e = l ? t.startContainer : t.endContainer;
10
- if ((e == null ? void 0 : e.nodeType) === Node.TEXT_NODE && (e = e.parentNode), !l && t.endOffset === 0)
11
- do {
12
- for (; !(e != null && e.previousSibling); )
13
- e = e == null ? void 0 : e.parentNode;
14
- e = e.previousSibling;
15
- } while (!e.childNodes.length);
16
- const n = (c = e == null ? void 0 : e.parentElement) == null ? void 0 : c.closest("[data-rp-text-layer]"), r = n == null ? void 0 : n.querySelector(".endOfContent");
17
- r && (r.style.width = n.style.width, r.style.height = n.style.height, (d = e == null ? void 0 : e.parentElement) == null || d.insertBefore(r, l ? e : e.nextSibling)), i.current = t.cloneRange();
18
- }, []);
19
- f(() => (document == null || document.addEventListener("selectionchange", o), () => {
20
- document == null || document.removeEventListener("selectionchange", o);
21
- }), [o]);
22
- };
23
- export {
24
- m as useFlickerSelectText
25
- };