@embedpdf/plugin-selection 1.0.5 → 1.0.6

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.
@@ -34,10 +34,11 @@ var useSelectionPlugin = () => (0, import_preact.usePlugin)(import_plugin_select
34
34
 
35
35
  // src/preact/components/selection-layer.tsx
36
36
  var import_hooks = require("preact/hooks");
37
- var import_plugin_selection2 = require("@embedpdf/plugin-selection");
37
+ var import_models = require("@embedpdf/models");
38
38
  var import_preact2 = require("@embedpdf/plugin-interaction-manager/preact");
39
+ var import_plugin_selection2 = require("@embedpdf/plugin-selection");
39
40
  var import_jsx_runtime = require("preact/jsx-runtime");
40
- function SelectionLayer({ pageIndex, scale }) {
41
+ function SelectionLayer({ pageIndex, scale, background = "rgba(33,150,243)" }) {
41
42
  const { provides: sel } = useSelectionCapability();
42
43
  const { register } = (0, import_preact2.usePointerHandlers)({ modeId: "default", pageIndex });
43
44
  const [rects, setRects] = (0, import_hooks.useState)([]);
@@ -45,7 +46,7 @@ function SelectionLayer({ pageIndex, scale }) {
45
46
  (0, import_hooks.useEffect)(() => {
46
47
  if (!sel) return;
47
48
  return sel.onSelectionChange(() => {
48
- setRects(sel.getHighlightRects(pageIndex));
49
+ setRects(sel.getHighlightRectsForPage(pageIndex));
49
50
  });
50
51
  }, [sel, pageIndex]);
51
52
  let geoCache;
@@ -55,17 +56,25 @@ function SelectionLayer({ pageIndex, scale }) {
55
56
  }, []);
56
57
  (0, import_hooks.useEffect)(() => {
57
58
  if (!sel) return;
58
- sel.getGeometry(pageIndex).then((g) => geoCache = g);
59
+ const task = sel.getGeometry(pageIndex);
60
+ task.wait((g) => geoCache = g, import_models.ignore);
61
+ return () => {
62
+ task.abort({
63
+ code: import_models.PdfErrorCode.Cancelled,
64
+ message: "Cancelled"
65
+ });
66
+ };
59
67
  }, [sel, pageIndex]);
60
68
  const handlers = (0, import_hooks.useMemo)(
61
69
  () => ({
62
70
  onPointerDown: (point) => {
63
71
  if (!sel) return;
64
72
  sel.clear();
65
- sel.getGeometry(pageIndex).then((geo) => {
73
+ const task = sel.getGeometry(pageIndex);
74
+ task.wait((geo) => {
66
75
  const g = (0, import_plugin_selection2.glyphAt)(geo, point);
67
76
  if (g !== -1) sel.begin(pageIndex, g);
68
- });
77
+ }, import_models.ignore);
69
78
  },
70
79
  onPointerMove: (point) => {
71
80
  if (!sel) return;
@@ -97,7 +106,7 @@ function SelectionLayer({ pageIndex, scale }) {
97
106
  top: b.origin.y * scale,
98
107
  width: b.size.width * scale,
99
108
  height: b.size.height * scale,
100
- background: "rgba(33,150,243)",
109
+ background,
101
110
  pointerEvents: "none"
102
111
  }
103
112
  },
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/preact/index.ts","../../src/preact/hooks/use-selection.ts","../../src/preact/components/selection-layer.tsx"],"sourcesContent":["export * from './hooks';\nexport * from './components';\n","import { useCapability, usePlugin } from '@embedpdf/core/preact';\nimport { SelectionPlugin } from '@embedpdf/plugin-selection';\n\nexport const useSelectionCapability = () => useCapability<SelectionPlugin>(SelectionPlugin.id);\nexport const useSelectionPlugin = () => usePlugin<SelectionPlugin>(SelectionPlugin.id);\n","/** @jsxImportSource preact */\nimport { useCallback, useEffect, useMemo, useRef, useState } from 'preact/hooks';\nimport { useSelectionCapability } from '../hooks';\nimport { glyphAt } from '@embedpdf/plugin-selection';\nimport { PdfPageGeometry, Rect } from '@embedpdf/models';\nimport { useCursor, usePointerHandlers } from '@embedpdf/plugin-interaction-manager/preact';\nimport { PointerEventHandlers } from '@embedpdf/plugin-interaction-manager';\n\ntype Props = { pageIndex: number; scale: number };\n\nexport function SelectionLayer({ pageIndex, scale }: Props) {\n const { provides: sel } = useSelectionCapability();\n const { register } = usePointerHandlers({ modeId: 'default', pageIndex });\n const [rects, setRects] = useState<Array<Rect>>([]);\n const { setCursor, removeCursor } = useCursor();\n\n /* subscribe to rect updates */\n useEffect(() => {\n if (!sel) return;\n return sel.onSelectionChange(() => {\n setRects(sel.getHighlightRects(pageIndex));\n });\n }, [sel, pageIndex]);\n\n /* cheap glyphAt cache for the active page */\n let geoCache: PdfPageGeometry | undefined;\n const cachedGlyphAt = useCallback((pt: { x: number; y: number }) => {\n if (!geoCache) return -1;\n return glyphAt(geoCache, pt);\n }, []);\n\n // Initialize geometry cache\n useEffect(() => {\n if (!sel) return;\n sel.getGeometry(pageIndex).then((g) => (geoCache = g));\n }, [sel, pageIndex]);\n\n const handlers = useMemo(\n (): PointerEventHandlers => ({\n onPointerDown: (point) => {\n if (!sel) return;\n\n // clear the selection\n sel.clear();\n sel.getGeometry(pageIndex).then((geo) => {\n const g = glyphAt(geo, point);\n if (g !== -1) sel.begin(pageIndex, g);\n });\n },\n onPointerMove: (point) => {\n if (!sel) return;\n const g = cachedGlyphAt(point);\n if (g !== -1) {\n setCursor('selection-text', 'text', 10);\n } else {\n removeCursor('selection-text');\n }\n if (g !== -1) sel.update(pageIndex, g);\n },\n onPointerUp: () => {\n if (!sel) return;\n sel.end();\n },\n }),\n [sel, pageIndex, cachedGlyphAt],\n );\n\n useEffect(() => {\n if (!register) return;\n return register(handlers);\n }, [register, handlers]);\n\n return (\n <>\n {rects.map((b, i) => (\n <div\n key={i}\n style={{\n position: 'absolute',\n left: b.origin.x * scale,\n top: b.origin.y * scale,\n width: b.size.width * scale,\n height: b.size.height * scale,\n background: 'rgba(33,150,243)',\n pointerEvents: 'none',\n }}\n />\n ))}\n </>\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAyC;AACzC,8BAAgC;AAEzB,IAAM,yBAAyB,UAAM,6BAA+B,wCAAgB,EAAE;AACtF,IAAM,qBAAqB,UAAM,yBAA2B,wCAAgB,EAAE;;;ACHrF,mBAAkE;AAElE,IAAAA,2BAAwB;AAExB,IAAAC,iBAA8C;AAoE1C;AA/DG,SAAS,eAAe,EAAE,WAAW,MAAM,GAAU;AAC1D,QAAM,EAAE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,IAAK;AACV,YAAI,IAAI;AAAA,MACV;AAAA,IACF;AAAA,IACA,CAAC,KAAK,WAAW,aAAa;AAAA,EAChC;AAEA,8BAAU,MAAM;AACd,QAAI,CAAC,SAAU;AACf,WAAO,SAAS,QAAQ;AAAA,EAC1B,GAAG,CAAC,UAAU,QAAQ,CAAC;AAEvB,SACE,2EACG,gBAAM,IAAI,CAAC,GAAG,MACb;AAAA,IAAC;AAAA;AAAA,MAEC,OAAO;AAAA,QACL,UAAU;AAAA,QACV,MAAM,EAAE,OAAO,IAAI;AAAA,QACnB,KAAK,EAAE,OAAO,IAAI;AAAA,QAClB,OAAO,EAAE,KAAK,QAAQ;AAAA,QACtB,QAAQ,EAAE,KAAK,SAAS;AAAA,QACxB,YAAY;AAAA,QACZ,eAAe;AAAA,MACjB;AAAA;AAAA,IATK;AAAA,EAUP,CACD,GACH;AAEJ;","names":["import_plugin_selection","import_preact"]}
1
+ {"version":3,"sources":["../../src/preact/index.ts","../../src/preact/hooks/use-selection.ts","../../src/preact/components/selection-layer.tsx"],"sourcesContent":["export * from './hooks';\nexport * from './components';\n","import { useCapability, usePlugin } from '@embedpdf/core/preact';\nimport { SelectionPlugin } from '@embedpdf/plugin-selection';\n\nexport const useSelectionCapability = () => useCapability<SelectionPlugin>(SelectionPlugin.id);\nexport const useSelectionPlugin = () => usePlugin<SelectionPlugin>(SelectionPlugin.id);\n","/** @jsxImportSource preact */\nimport { useCallback, useEffect, useMemo, useState } from 'preact/hooks';\nimport { ignore, PdfErrorCode, PdfPageGeometry, Rect } from '@embedpdf/models';\nimport { useCursor, usePointerHandlers } from '@embedpdf/plugin-interaction-manager/preact';\nimport { PointerEventHandlers } from '@embedpdf/plugin-interaction-manager';\nimport { glyphAt } from '@embedpdf/plugin-selection';\n\nimport { useSelectionCapability } from '../hooks';\n\ntype Props = {\n pageIndex: number;\n scale: number;\n background?: string;\n};\n\nexport function SelectionLayer({ pageIndex, scale, background = 'rgba(33,150,243)' }: Props) {\n const { provides: sel } = useSelectionCapability();\n const { register } = usePointerHandlers({ modeId: 'default', pageIndex });\n const [rects, setRects] = useState<Array<Rect>>([]);\n const { setCursor, removeCursor } = useCursor();\n\n /* subscribe to rect updates */\n useEffect(() => {\n if (!sel) return;\n return sel.onSelectionChange(() => {\n setRects(sel.getHighlightRectsForPage(pageIndex));\n });\n }, [sel, pageIndex]);\n\n /* cheap glyphAt cache for the active page */\n let geoCache: PdfPageGeometry | undefined;\n const cachedGlyphAt = useCallback((pt: { x: number; y: number }) => {\n if (!geoCache) return -1;\n return glyphAt(geoCache, pt);\n }, []);\n\n // Initialize geometry cache\n useEffect(() => {\n if (!sel) return;\n const task = sel.getGeometry(pageIndex);\n task.wait((g) => (geoCache = g), ignore);\n\n return () => {\n task.abort({\n code: PdfErrorCode.Cancelled,\n message: 'Cancelled',\n });\n };\n }, [sel, pageIndex]);\n\n const handlers = useMemo(\n (): PointerEventHandlers => ({\n onPointerDown: (point) => {\n if (!sel) return;\n\n // clear the selection\n sel.clear();\n const task = sel.getGeometry(pageIndex);\n task.wait((geo) => {\n const g = glyphAt(geo, point);\n if (g !== -1) sel.begin(pageIndex, g);\n }, ignore);\n },\n onPointerMove: (point) => {\n if (!sel) return;\n const g = cachedGlyphAt(point);\n if (g !== -1) {\n setCursor('selection-text', 'text', 10);\n } else {\n removeCursor('selection-text');\n }\n if (g !== -1) sel.update(pageIndex, g);\n },\n onPointerUp: () => {\n if (!sel) return;\n sel.end();\n },\n }),\n [sel, pageIndex, cachedGlyphAt],\n );\n\n useEffect(() => {\n if (!register) return;\n return register(handlers);\n }, [register, handlers]);\n\n return (\n <>\n {rects.map((b, i) => (\n <div\n key={i}\n style={{\n position: 'absolute',\n left: b.origin.x * scale,\n top: b.origin.y * scale,\n width: b.size.width * scale,\n height: b.size.height * scale,\n background,\n pointerEvents: 'none',\n }}\n />\n ))}\n </>\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA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;AACtC,aAAK,KAAK,CAAC,QAAQ;AACjB,gBAAM,QAAI,kCAAQ,KAAK,KAAK;AAC5B,cAAI,MAAM,GAAI,KAAI,MAAM,WAAW,CAAC;AAAA,QACtC,GAAG,oBAAM;AAAA,MACX;AAAA,MACA,eAAe,CAAC,UAAU;AACxB,YAAI,CAAC,IAAK;AACV,cAAM,IAAI,cAAc,KAAK;AAC7B,YAAI,MAAM,IAAI;AACZ,oBAAU,kBAAkB,QAAQ,EAAE;AAAA,QACxC,OAAO;AACL,uBAAa,gBAAgB;AAAA,QAC/B;AACA,YAAI,MAAM,GAAI,KAAI,OAAO,WAAW,CAAC;AAAA,MACvC;AAAA,MACA,aAAa,MAAM;AACjB,YAAI,CAAC,IAAK;AACV,YAAI,IAAI;AAAA,MACV;AAAA,IACF;AAAA,IACA,CAAC,KAAK,WAAW,aAAa;AAAA,EAChC;AAEA,8BAAU,MAAM;AACd,QAAI,CAAC,SAAU;AACf,WAAO,SAAS,QAAQ;AAAA,EAC1B,GAAG,CAAC,UAAU,QAAQ,CAAC;AAEvB,SACE,2EACG,gBAAM,IAAI,CAAC,GAAG,MACb;AAAA,IAAC;AAAA;AAAA,MAEC,OAAO;AAAA,QACL,UAAU;AAAA,QACV,MAAM,EAAE,OAAO,IAAI;AAAA,QACnB,KAAK,EAAE,OAAO,IAAI;AAAA,QAClB,OAAO,EAAE,KAAK,QAAQ;AAAA,QACtB,QAAQ,EAAE,KAAK,SAAS;AAAA,QACxB;AAAA,QACA,eAAe;AAAA,MACjB;AAAA;AAAA,IATK;AAAA,EAUP,CACD,GACH;AAEJ;","names":["import_preact","import_plugin_selection"]}
@@ -16,7 +16,8 @@ declare const useSelectionPlugin: () => {
16
16
  type Props = {
17
17
  pageIndex: number;
18
18
  scale: number;
19
+ background?: string;
19
20
  };
20
- declare function SelectionLayer({ pageIndex, scale }: Props): preact.JSX.Element;
21
+ declare function SelectionLayer({ pageIndex, scale, background }: Props): preact.JSX.Element;
21
22
 
22
23
  export { SelectionLayer, useSelectionCapability, useSelectionPlugin };
@@ -16,7 +16,8 @@ declare const useSelectionPlugin: () => {
16
16
  type Props = {
17
17
  pageIndex: number;
18
18
  scale: number;
19
+ background?: string;
19
20
  };
20
- declare function SelectionLayer({ pageIndex, scale }: Props): preact.JSX.Element;
21
+ declare function SelectionLayer({ pageIndex, scale, background }: Props): preact.JSX.Element;
21
22
 
22
23
  export { SelectionLayer, useSelectionCapability, useSelectionPlugin };
@@ -6,10 +6,11 @@ var useSelectionPlugin = () => usePlugin(SelectionPlugin.id);
6
6
 
7
7
  // src/preact/components/selection-layer.tsx
8
8
  import { useCallback, useEffect, useMemo, useState } from "preact/hooks";
9
- import { glyphAt } from "@embedpdf/plugin-selection";
9
+ import { ignore, PdfErrorCode } from "@embedpdf/models";
10
10
  import { useCursor, usePointerHandlers } from "@embedpdf/plugin-interaction-manager/preact";
11
+ import { glyphAt } from "@embedpdf/plugin-selection";
11
12
  import { Fragment, jsx } from "preact/jsx-runtime";
12
- function SelectionLayer({ pageIndex, scale }) {
13
+ function SelectionLayer({ pageIndex, scale, background = "rgba(33,150,243)" }) {
13
14
  const { provides: sel } = useSelectionCapability();
14
15
  const { register } = usePointerHandlers({ modeId: "default", pageIndex });
15
16
  const [rects, setRects] = useState([]);
@@ -17,7 +18,7 @@ function SelectionLayer({ pageIndex, scale }) {
17
18
  useEffect(() => {
18
19
  if (!sel) return;
19
20
  return sel.onSelectionChange(() => {
20
- setRects(sel.getHighlightRects(pageIndex));
21
+ setRects(sel.getHighlightRectsForPage(pageIndex));
21
22
  });
22
23
  }, [sel, pageIndex]);
23
24
  let geoCache;
@@ -27,17 +28,25 @@ function SelectionLayer({ pageIndex, scale }) {
27
28
  }, []);
28
29
  useEffect(() => {
29
30
  if (!sel) return;
30
- sel.getGeometry(pageIndex).then((g) => geoCache = g);
31
+ const task = sel.getGeometry(pageIndex);
32
+ task.wait((g) => geoCache = g, ignore);
33
+ return () => {
34
+ task.abort({
35
+ code: PdfErrorCode.Cancelled,
36
+ message: "Cancelled"
37
+ });
38
+ };
31
39
  }, [sel, pageIndex]);
32
40
  const handlers = useMemo(
33
41
  () => ({
34
42
  onPointerDown: (point) => {
35
43
  if (!sel) return;
36
44
  sel.clear();
37
- sel.getGeometry(pageIndex).then((geo) => {
45
+ const task = sel.getGeometry(pageIndex);
46
+ task.wait((geo) => {
38
47
  const g = glyphAt(geo, point);
39
48
  if (g !== -1) sel.begin(pageIndex, g);
40
- });
49
+ }, ignore);
41
50
  },
42
51
  onPointerMove: (point) => {
43
52
  if (!sel) return;
@@ -69,7 +78,7 @@ function SelectionLayer({ pageIndex, scale }) {
69
78
  top: b.origin.y * scale,
70
79
  width: b.size.width * scale,
71
80
  height: b.size.height * scale,
72
- background: "rgba(33,150,243)",
81
+ background,
73
82
  pointerEvents: "none"
74
83
  }
75
84
  },
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/preact/hooks/use-selection.ts","../../src/preact/components/selection-layer.tsx"],"sourcesContent":["import { useCapability, usePlugin } from '@embedpdf/core/preact';\nimport { SelectionPlugin } from '@embedpdf/plugin-selection';\n\nexport const useSelectionCapability = () => useCapability<SelectionPlugin>(SelectionPlugin.id);\nexport const useSelectionPlugin = () => usePlugin<SelectionPlugin>(SelectionPlugin.id);\n","/** @jsxImportSource preact */\nimport { useCallback, useEffect, useMemo, useRef, useState } from 'preact/hooks';\nimport { useSelectionCapability } from '../hooks';\nimport { glyphAt } from '@embedpdf/plugin-selection';\nimport { PdfPageGeometry, Rect } from '@embedpdf/models';\nimport { useCursor, usePointerHandlers } from '@embedpdf/plugin-interaction-manager/preact';\nimport { PointerEventHandlers } from '@embedpdf/plugin-interaction-manager';\n\ntype Props = { pageIndex: number; scale: number };\n\nexport function SelectionLayer({ pageIndex, scale }: Props) {\n const { provides: sel } = useSelectionCapability();\n const { register } = usePointerHandlers({ modeId: 'default', pageIndex });\n const [rects, setRects] = useState<Array<Rect>>([]);\n const { setCursor, removeCursor } = useCursor();\n\n /* subscribe to rect updates */\n useEffect(() => {\n if (!sel) return;\n return sel.onSelectionChange(() => {\n setRects(sel.getHighlightRects(pageIndex));\n });\n }, [sel, pageIndex]);\n\n /* cheap glyphAt cache for the active page */\n let geoCache: PdfPageGeometry | undefined;\n const cachedGlyphAt = useCallback((pt: { x: number; y: number }) => {\n if (!geoCache) return -1;\n return glyphAt(geoCache, pt);\n }, []);\n\n // Initialize geometry cache\n useEffect(() => {\n if (!sel) return;\n sel.getGeometry(pageIndex).then((g) => (geoCache = g));\n }, [sel, pageIndex]);\n\n const handlers = useMemo(\n (): PointerEventHandlers => ({\n onPointerDown: (point) => {\n if (!sel) return;\n\n // clear the selection\n sel.clear();\n sel.getGeometry(pageIndex).then((geo) => {\n const g = glyphAt(geo, point);\n if (g !== -1) sel.begin(pageIndex, g);\n });\n },\n onPointerMove: (point) => {\n if (!sel) return;\n const g = cachedGlyphAt(point);\n if (g !== -1) {\n setCursor('selection-text', 'text', 10);\n } else {\n removeCursor('selection-text');\n }\n if (g !== -1) sel.update(pageIndex, g);\n },\n onPointerUp: () => {\n if (!sel) return;\n sel.end();\n },\n }),\n [sel, pageIndex, cachedGlyphAt],\n );\n\n useEffect(() => {\n if (!register) return;\n return register(handlers);\n }, [register, handlers]);\n\n return (\n <>\n {rects.map((b, i) => (\n <div\n key={i}\n style={{\n position: 'absolute',\n left: b.origin.x * scale,\n top: b.origin.y * scale,\n width: b.size.width * scale,\n height: b.size.height * scale,\n background: 'rgba(33,150,243)',\n pointerEvents: 'none',\n }}\n />\n ))}\n </>\n );\n}\n"],"mappings":";AAAA,SAAS,eAAe,iBAAiB;AACzC,SAAS,uBAAuB;AAEzB,IAAM,yBAAyB,MAAM,cAA+B,gBAAgB,EAAE;AACtF,IAAM,qBAAqB,MAAM,UAA2B,gBAAgB,EAAE;;;ACHrF,SAAS,aAAa,WAAW,SAAiB,gBAAgB;AAElE,SAAS,eAAe;AAExB,SAAS,WAAW,0BAA0B;AAoE1C,mBAEI,WAFJ;AA/DG,SAAS,eAA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A,MACA,aAAa,MAAM;AACjB,YAAI,CAAC,IAAK;AACV,YAAI,IAAI;AAAA,MACV;AAAA,IACF;AAAA,IACA,CAAC,KAAK,WAAW,aAAa;AAAA,EAChC;AAEA,YAAU,MAAM;AACd,QAAI,CAAC,SAAU;AACf,WAAO,SAAS,QAAQ;AAAA,EAC1B,GAAG,CAAC,UAAU,QAAQ,CAAC;AAEvB,SACE,gCACG,gBAAM,IAAI,CAAC,GAAG,MACb;AAAA,IAAC;AAAA;AAAA,MAEC,OAAO;AAAA,QACL,UAAU;AAAA,QACV,MAAM,EAAE,OAAO,IAAI;AAAA,QACnB,KAAK,EAAE,OAAO,IAAI;AAAA,QAClB,OAAO,EAAE,KAAK,QAAQ;AAAA,QACtB,QAAQ,EAAE,KAAK,SAAS;AAAA,QACxB,YAAY;AAAA,QACZ,eAAe;AAAA,MACjB;AAAA;AAAA,IATK;AAAA,EAUP,CACD,GACH;AAEJ;","names":[]}
1
+ {"version":3,"sources":["../../src/preact/hooks/use-selection.ts","../../src/preact/components/selection-layer.tsx"],"sourcesContent":["import { useCapability, usePlugin } from '@embedpdf/core/preact';\nimport { SelectionPlugin } from '@embedpdf/plugin-selection';\n\nexport const useSelectionCapability = () => useCapability<SelectionPlugin>(SelectionPlugin.id);\nexport const useSelectionPlugin = () => usePlugin<SelectionPlugin>(SelectionPlugin.id);\n","/** @jsxImportSource preact */\nimport { useCallback, useEffect, useMemo, useState } from 'preact/hooks';\nimport { ignore, PdfErrorCode, PdfPageGeometry, Rect } from '@embedpdf/models';\nimport { useCursor, usePointerHandlers } from '@embedpdf/plugin-interaction-manager/preact';\nimport { PointerEventHandlers } from '@embedpdf/plugin-interaction-manager';\nimport { glyphAt } from '@embedpdf/plugin-selection';\n\nimport { useSelectionCapability } from '../hooks';\n\ntype Props = {\n pageIndex: number;\n scale: number;\n background?: string;\n};\n\nexport function SelectionLayer({ pageIndex, scale, background = 'rgba(33,150,243)' }: Props) {\n const { provides: sel } = useSelectionCapability();\n const { register } = usePointerHandlers({ modeId: 'default', pageIndex });\n const [rects, setRects] = useState<Array<Rect>>([]);\n const { setCursor, removeCursor } = useCursor();\n\n /* subscribe to rect updates */\n useEffect(() => {\n if (!sel) return;\n return sel.onSelectionChange(() => {\n setRects(sel.getHighlightRectsForPage(pageIndex));\n });\n }, [sel, pageIndex]);\n\n /* cheap glyphAt cache for the active page */\n let geoCache: PdfPageGeometry | undefined;\n const cachedGlyphAt = useCallback((pt: { x: number; y: number }) => {\n if (!geoCache) return -1;\n return glyphAt(geoCache, pt);\n }, []);\n\n // Initialize geometry cache\n useEffect(() => {\n if (!sel) return;\n const task = sel.getGeometry(pageIndex);\n task.wait((g) => (geoCache = g), ignore);\n\n return () => {\n task.abort({\n code: PdfErrorCode.Cancelled,\n message: 'Cancelled',\n });\n };\n }, [sel, pageIndex]);\n\n const handlers = useMemo(\n (): PointerEventHandlers => ({\n onPointerDown: (point) => {\n if (!sel) return;\n\n // clear the selection\n sel.clear();\n const task = sel.getGeometry(pageIndex);\n task.wait((geo) => {\n const g = glyphAt(geo, point);\n if (g !== -1) sel.begin(pageIndex, g);\n }, ignore);\n },\n onPointerMove: (point) => {\n if (!sel) return;\n const g = cachedGlyphAt(point);\n if (g !== -1) {\n setCursor('selection-text', 'text', 10);\n } else {\n removeCursor('selection-text');\n }\n if (g !== -1) sel.update(pageIndex, g);\n },\n onPointerUp: () => {\n if (!sel) return;\n sel.end();\n },\n }),\n [sel, pageIndex, cachedGlyphAt],\n );\n\n useEffect(() => {\n if (!register) return;\n return register(handlers);\n }, [register, handlers]);\n\n return (\n <>\n {rects.map((b, i) => (\n <div\n key={i}\n style={{\n position: 'absolute',\n left: b.origin.x * scale,\n top: b.origin.y * scale,\n width: b.size.width * scale,\n height: b.size.height * scale,\n background,\n pointerEvents: 'none',\n }}\n />\n ))}\n </>\n );\n}\n"],"mappings":";AAAA,SAAS,eAAe,iBAAiB;AACzC,SAAS,uBAAuB;AAEzB,IAAM,yBAAyB,MAAM,cAA+B,gB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Q;AACjB,gBAAM,IAAI,QAAQ,KAAK,KAAK;AAC5B,cAAI,MAAM,GAAI,KAAI,MAAM,WAAW,CAAC;AAAA,QACtC,GAAG,MAAM;AAAA,MACX;AAAA,MACA,eAAe,CAAC,UAAU;AACxB,YAAI,CAAC,IAAK;AACV,cAAM,IAAI,cAAc,KAAK;AAC7B,YAAI,MAAM,IAAI;AACZ,oBAAU,kBAAkB,QAAQ,EAAE;AAAA,QACxC,OAAO;AACL,uBAAa,gBAAgB;AAAA,QAC/B;AACA,YAAI,MAAM,GAAI,KAAI,OAAO,WAAW,CAAC;AAAA,MACvC;AAAA,MACA,aAAa,MAAM;AACjB,YAAI,CAAC,IAAK;AACV,YAAI,IAAI;AAAA,MACV;AAAA,IACF;AAAA,IACA,CAAC,KAAK,WAAW,aAAa;AAAA,EAChC;AAEA,YAAU,MAAM;AACd,QAAI,CAAC,SAAU;AACf,WAAO,SAAS,QAAQ;AAAA,EAC1B,GAAG,CAAC,UAAU,QAAQ,CAAC;AAEvB,SACE,gCACG,gBAAM,IAAI,CAAC,GAAG,MACb;AAAA,IAAC;AAAA;AAAA,MAEC,OAAO;AAAA,QACL,UAAU;AAAA,QACV,MAAM,EAAE,OAAO,IAAI;AAAA,QACnB,KAAK,EAAE,OAAO,IAAI;AAAA,QAClB,OAAO,EAAE,KAAK,QAAQ;AAAA,QACtB,QAAQ,EAAE,KAAK,SAAS;AAAA,QACxB;AAAA,QACA,eAAe;AAAA,MACjB;AAAA;AAAA,IATK;AAAA,EAUP,CACD,GACH;AAEJ;","names":[]}
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/react/index.ts
21
+ var react_exports = {};
22
+ __export(react_exports, {
23
+ SelectionLayer: () => SelectionLayer,
24
+ useSelectionCapability: () => useSelectionCapability,
25
+ useSelectionPlugin: () => useSelectionPlugin
26
+ });
27
+ module.exports = __toCommonJS(react_exports);
28
+
29
+ // src/react/hooks/use-selection.ts
30
+ var import_react = require("@embedpdf/core/react");
31
+ var import_plugin_selection = require("@embedpdf/plugin-selection");
32
+ var useSelectionCapability = () => (0, import_react.useCapability)(import_plugin_selection.SelectionPlugin.id);
33
+ var useSelectionPlugin = () => (0, import_react.usePlugin)(import_plugin_selection.SelectionPlugin.id);
34
+
35
+ // src/react/components/selection-layer.tsx
36
+ var import_react2 = require("react");
37
+ var import_models = require("@embedpdf/models");
38
+ var import_react3 = require("@embedpdf/plugin-interaction-manager/react");
39
+ var import_plugin_selection2 = require("@embedpdf/plugin-selection");
40
+ var import_jsx_runtime = require("react/jsx-runtime");
41
+ function SelectionLayer({ pageIndex, scale, background = "rgba(33,150,243)" }) {
42
+ const { provides: sel } = useSelectionCapability();
43
+ const { register } = (0, import_react3.usePointerHandlers)({ modeId: "default", pageIndex });
44
+ const [rects, setRects] = (0, import_react2.useState)([]);
45
+ const { setCursor, removeCursor } = (0, import_react3.useCursor)();
46
+ (0, import_react2.useEffect)(() => {
47
+ if (!sel) return;
48
+ return sel.onSelectionChange(() => {
49
+ setRects(sel.getHighlightRectsForPage(pageIndex));
50
+ });
51
+ }, [sel, pageIndex]);
52
+ let geoCache;
53
+ const cachedGlyphAt = (0, import_react2.useCallback)((pt) => {
54
+ if (!geoCache) return -1;
55
+ return (0, import_plugin_selection2.glyphAt)(geoCache, pt);
56
+ }, []);
57
+ (0, import_react2.useEffect)(() => {
58
+ if (!sel) return;
59
+ const task = sel.getGeometry(pageIndex);
60
+ task.wait((g) => geoCache = g, import_models.ignore);
61
+ return () => {
62
+ task.abort({
63
+ code: import_models.PdfErrorCode.Cancelled,
64
+ message: "Cancelled"
65
+ });
66
+ };
67
+ }, [sel, pageIndex]);
68
+ const handlers = (0, import_react2.useMemo)(
69
+ () => ({
70
+ onPointerDown: (point) => {
71
+ if (!sel) return;
72
+ sel.clear();
73
+ const task = sel.getGeometry(pageIndex);
74
+ task.wait((geo) => {
75
+ const g = (0, import_plugin_selection2.glyphAt)(geo, point);
76
+ if (g !== -1) sel.begin(pageIndex, g);
77
+ }, import_models.ignore);
78
+ },
79
+ onPointerMove: (point) => {
80
+ if (!sel) return;
81
+ const g = cachedGlyphAt(point);
82
+ if (g !== -1) {
83
+ setCursor("selection-text", "text", 10);
84
+ } else {
85
+ removeCursor("selection-text");
86
+ }
87
+ if (g !== -1) sel.update(pageIndex, g);
88
+ },
89
+ onPointerUp: () => {
90
+ if (!sel) return;
91
+ sel.end();
92
+ }
93
+ }),
94
+ [sel, pageIndex, cachedGlyphAt]
95
+ );
96
+ (0, import_react2.useEffect)(() => {
97
+ if (!register) return;
98
+ return register(handlers);
99
+ }, [register, handlers]);
100
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: rects.map((b, i) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
101
+ "div",
102
+ {
103
+ style: {
104
+ position: "absolute",
105
+ left: b.origin.x * scale,
106
+ top: b.origin.y * scale,
107
+ width: b.size.width * scale,
108
+ height: b.size.height * scale,
109
+ background,
110
+ pointerEvents: "none"
111
+ }
112
+ },
113
+ i
114
+ )) });
115
+ }
116
+ // Annotate the CommonJS export names for ESM import in node:
117
+ 0 && (module.exports = {
118
+ SelectionLayer,
119
+ useSelectionCapability,
120
+ useSelectionPlugin
121
+ });
122
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/react/index.ts","../../src/react/hooks/use-selection.ts","../../src/react/components/selection-layer.tsx"],"sourcesContent":["export * from './hooks';\nexport * from './components';\n","import { useCapability, usePlugin } from '@embedpdf/core/react';\nimport { SelectionPlugin } from '@embedpdf/plugin-selection';\n\nexport const useSelectionCapability = () => useCapability<SelectionPlugin>(SelectionPlugin.id);\nexport const useSelectionPlugin = () => usePlugin<SelectionPlugin>(SelectionPlugin.id);\n","import { useCallback, useEffect, useMemo, useState } from 'react';\nimport { ignore, PdfErrorCode, PdfPageGeometry, Rect } from '@embedpdf/models';\nimport { useCursor, usePointerHandlers } from '@embedpdf/plugin-interaction-manager/react';\nimport { PointerEventHandlers } from '@embedpdf/plugin-interaction-manager';\nimport { glyphAt } from '@embedpdf/plugin-selection';\n\nimport { useSelectionCapability } from '../hooks';\n\ntype Props = {\n pageIndex: number;\n scale: number;\n background?: string;\n};\n\nexport function SelectionLayer({ pageIndex, scale, background = 'rgba(33,150,243)' }: Props) {\n const { provides: sel } = useSelectionCapability();\n const { register } = usePointerHandlers({ modeId: 'default', pageIndex });\n const [rects, setRects] = useState<Array<Rect>>([]);\n const { setCursor, removeCursor } = useCursor();\n\n /* subscribe to rect updates */\n useEffect(() => {\n if (!sel) return;\n return sel.onSelectionChange(() => {\n setRects(sel.getHighlightRectsForPage(pageIndex));\n });\n }, [sel, pageIndex]);\n\n /* cheap glyphAt cache for the active page */\n let geoCache: PdfPageGeometry | undefined;\n const cachedGlyphAt = useCallback((pt: { x: number; y: number }) => {\n if (!geoCache) return -1;\n return glyphAt(geoCache, pt);\n }, []);\n\n // Initialize geometry cache\n useEffect(() => {\n if (!sel) return;\n const task = sel.getGeometry(pageIndex);\n task.wait((g) => (geoCache = g), ignore);\n\n return () => {\n task.abort({\n code: PdfErrorCode.Cancelled,\n message: 'Cancelled',\n });\n };\n }, [sel, pageIndex]);\n\n const handlers = useMemo(\n (): PointerEventHandlers => ({\n onPointerDown: (point) => {\n if (!sel) return;\n\n // clear the selection\n sel.clear();\n const task = sel.getGeometry(pageIndex);\n task.wait((geo) => {\n const g = glyphAt(geo, point);\n if (g !== -1) sel.begin(pageIndex, g);\n }, ignore);\n },\n onPointerMove: (point) => {\n if (!sel) return;\n const g = cachedGlyphAt(point);\n if (g !== -1) {\n setCursor('selection-text', 'text', 10);\n } else {\n removeCursor('selection-text');\n }\n if (g !== -1) sel.update(pageIndex, g);\n },\n onPointerUp: () => {\n if (!sel) return;\n sel.end();\n },\n }),\n [sel, pageIndex, cachedGlyphAt],\n );\n\n useEffect(() => {\n if (!register) return;\n return register(handlers);\n }, [register, handlers]);\n\n return (\n <>\n {rects.map((b, i) => (\n <div\n key={i}\n style={{\n position: 'absolute',\n left: b.origin.x * scale,\n top: b.origin.y * scale,\n width: b.size.width * scale,\n height: b.size.height * scale,\n background,\n pointerEvents: 'none',\n }}\n />\n ))}\n </>\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAyC;AACzC,8BAAgC;AAEzB,IAAM,yBAAyB,UAAM,4BAA+B,wCAAgB,EAAE;AACtF,IAAM,qBAAqB,UAAM,wBAA2B,wCAAgB,EAAE;;;ACJrF,IAAAA,gBAA0D;AAC1D,oBAA4D;AAC5D,IAAAA,gBAA8C;AAE9C,IAAAC,2BAAwB;AAkFpB;AAxEG,SAAS,eAAe,EAAE,WAAW,OAAO,aAAa,mBAAmB,GAAU;AAC3F,QAAM,EAAE,UAAU,IAAI,IAAI,uBAAuB;AACjD,QAAM,EAAE,SAAS,QAAI,kCAAmB,EAAE,QAAQ,WAAW,UAAU,CAAC;AACxE,QAAM,CAAC,OAAO,QAAQ,QAAI,wBAAsB,CAAC,CAAC;AAClD,QAAM,EAAE,WAAW,aAAa,QAAI,yBAAU;AAG9C,+BAAU,MAAM;AACd,QAAI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aAAa;AAAA,EAChC;AAEA,+BAAU,MAAM;AACd,QAAI,CAAC,SAAU;AACf,WAAO,SAAS,QAAQ;AAAA,EAC1B,GAAG,CAAC,UAAU,QAAQ,CAAC;AAEvB,SACE,2EACG,gBAAM,IAAI,CAAC,GAAG,MACb;AAAA,IAAC;AAAA;AAAA,MAEC,OAAO;AAAA,QACL,UAAU;AAAA,QACV,MAAM,EAAE,OAAO,IAAI;AAAA,QACnB,KAAK,EAAE,OAAO,IAAI;AAAA,QAClB,OAAO,EAAE,KAAK,QAAQ;AAAA,QACtB,QAAQ,EAAE,KAAK,SAAS;AAAA,QACxB;AAAA,QACA,eAAe;AAAA,MACjB;AAAA;AAAA,IATK;AAAA,EAUP,CACD,GACH;AAEJ;","names":["import_react","import_plugin_selection"]}
@@ -0,0 +1,23 @@
1
+ import * as _embedpdf_plugin_selection from '@embedpdf/plugin-selection';
2
+ import { SelectionPlugin } from '@embedpdf/plugin-selection';
3
+ import * as react_jsx_runtime from 'react/jsx-runtime';
4
+
5
+ declare const useSelectionCapability: () => {
6
+ provides: Readonly<_embedpdf_plugin_selection.SelectionCapability> | null;
7
+ isLoading: boolean;
8
+ ready: Promise<void>;
9
+ };
10
+ declare const useSelectionPlugin: () => {
11
+ plugin: SelectionPlugin | null;
12
+ isLoading: boolean;
13
+ ready: Promise<void>;
14
+ };
15
+
16
+ type Props = {
17
+ pageIndex: number;
18
+ scale: number;
19
+ background?: string;
20
+ };
21
+ declare function SelectionLayer({ pageIndex, scale, background }: Props): react_jsx_runtime.JSX.Element;
22
+
23
+ export { SelectionLayer, useSelectionCapability, useSelectionPlugin };
@@ -0,0 +1,23 @@
1
+ import * as _embedpdf_plugin_selection from '@embedpdf/plugin-selection';
2
+ import { SelectionPlugin } from '@embedpdf/plugin-selection';
3
+ import * as react_jsx_runtime from 'react/jsx-runtime';
4
+
5
+ declare const useSelectionCapability: () => {
6
+ provides: Readonly<_embedpdf_plugin_selection.SelectionCapability> | null;
7
+ isLoading: boolean;
8
+ ready: Promise<void>;
9
+ };
10
+ declare const useSelectionPlugin: () => {
11
+ plugin: SelectionPlugin | null;
12
+ isLoading: boolean;
13
+ ready: Promise<void>;
14
+ };
15
+
16
+ type Props = {
17
+ pageIndex: number;
18
+ scale: number;
19
+ background?: string;
20
+ };
21
+ declare function SelectionLayer({ pageIndex, scale, background }: Props): react_jsx_runtime.JSX.Element;
22
+
23
+ export { SelectionLayer, useSelectionCapability, useSelectionPlugin };
@@ -0,0 +1,93 @@
1
+ // src/react/hooks/use-selection.ts
2
+ import { useCapability, usePlugin } from "@embedpdf/core/react";
3
+ import { SelectionPlugin } from "@embedpdf/plugin-selection";
4
+ var useSelectionCapability = () => useCapability(SelectionPlugin.id);
5
+ var useSelectionPlugin = () => usePlugin(SelectionPlugin.id);
6
+
7
+ // src/react/components/selection-layer.tsx
8
+ import { useCallback, useEffect, useMemo, useState } from "react";
9
+ import { ignore, PdfErrorCode } from "@embedpdf/models";
10
+ import { useCursor, usePointerHandlers } from "@embedpdf/plugin-interaction-manager/react";
11
+ import { glyphAt } from "@embedpdf/plugin-selection";
12
+ import { Fragment, jsx } from "react/jsx-runtime";
13
+ function SelectionLayer({ pageIndex, scale, background = "rgba(33,150,243)" }) {
14
+ const { provides: sel } = useSelectionCapability();
15
+ const { register } = usePointerHandlers({ modeId: "default", pageIndex });
16
+ const [rects, setRects] = useState([]);
17
+ const { setCursor, removeCursor } = useCursor();
18
+ useEffect(() => {
19
+ if (!sel) return;
20
+ return sel.onSelectionChange(() => {
21
+ setRects(sel.getHighlightRectsForPage(pageIndex));
22
+ });
23
+ }, [sel, pageIndex]);
24
+ let geoCache;
25
+ const cachedGlyphAt = useCallback((pt) => {
26
+ if (!geoCache) return -1;
27
+ return glyphAt(geoCache, pt);
28
+ }, []);
29
+ useEffect(() => {
30
+ if (!sel) return;
31
+ const task = sel.getGeometry(pageIndex);
32
+ task.wait((g) => geoCache = g, ignore);
33
+ return () => {
34
+ task.abort({
35
+ code: PdfErrorCode.Cancelled,
36
+ message: "Cancelled"
37
+ });
38
+ };
39
+ }, [sel, pageIndex]);
40
+ const handlers = useMemo(
41
+ () => ({
42
+ onPointerDown: (point) => {
43
+ if (!sel) return;
44
+ sel.clear();
45
+ const task = sel.getGeometry(pageIndex);
46
+ task.wait((geo) => {
47
+ const g = glyphAt(geo, point);
48
+ if (g !== -1) sel.begin(pageIndex, g);
49
+ }, ignore);
50
+ },
51
+ onPointerMove: (point) => {
52
+ if (!sel) return;
53
+ const g = cachedGlyphAt(point);
54
+ if (g !== -1) {
55
+ setCursor("selection-text", "text", 10);
56
+ } else {
57
+ removeCursor("selection-text");
58
+ }
59
+ if (g !== -1) sel.update(pageIndex, g);
60
+ },
61
+ onPointerUp: () => {
62
+ if (!sel) return;
63
+ sel.end();
64
+ }
65
+ }),
66
+ [sel, pageIndex, cachedGlyphAt]
67
+ );
68
+ useEffect(() => {
69
+ if (!register) return;
70
+ return register(handlers);
71
+ }, [register, handlers]);
72
+ return /* @__PURE__ */ jsx(Fragment, { children: rects.map((b, i) => /* @__PURE__ */ jsx(
73
+ "div",
74
+ {
75
+ style: {
76
+ position: "absolute",
77
+ left: b.origin.x * scale,
78
+ top: b.origin.y * scale,
79
+ width: b.size.width * scale,
80
+ height: b.size.height * scale,
81
+ background,
82
+ pointerEvents: "none"
83
+ }
84
+ },
85
+ i
86
+ )) });
87
+ }
88
+ export {
89
+ SelectionLayer,
90
+ useSelectionCapability,
91
+ useSelectionPlugin
92
+ };
93
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/react/hooks/use-selection.ts","../../src/react/components/selection-layer.tsx"],"sourcesContent":["import { useCapability, usePlugin } from '@embedpdf/core/react';\nimport { SelectionPlugin } from '@embedpdf/plugin-selection';\n\nexport const useSelectionCapability = () => useCapability<SelectionPlugin>(SelectionPlugin.id);\nexport const useSelectionPlugin = () => usePlugin<SelectionPlugin>(SelectionPlugin.id);\n","import { useCallback, useEffect, useMemo, useState } from 'react';\nimport { ignore, PdfErrorCode, PdfPageGeometry, Rect } from '@embedpdf/models';\nimport { useCursor, usePointerHandlers } from '@embedpdf/plugin-interaction-manager/react';\nimport { PointerEventHandlers } from '@embedpdf/plugin-interaction-manager';\nimport { glyphAt } from '@embedpdf/plugin-selection';\n\nimport { useSelectionCapability } from '../hooks';\n\ntype Props = {\n pageIndex: number;\n scale: number;\n background?: string;\n};\n\nexport function SelectionLayer({ pageIndex, scale, background = 'rgba(33,150,243)' }: Props) {\n const { provides: sel } = useSelectionCapability();\n const { register } = usePointerHandlers({ modeId: 'default', pageIndex });\n const [rects, setRects] = useState<Array<Rect>>([]);\n const { setCursor, removeCursor } = useCursor();\n\n /* subscribe to rect updates */\n useEffect(() => {\n if (!sel) return;\n return sel.onSelectionChange(() => {\n setRects(sel.getHighlightRectsForPage(pageIndex));\n });\n }, [sel, pageIndex]);\n\n /* cheap glyphAt cache for the active page */\n let geoCache: PdfPageGeometry | undefined;\n const cachedGlyphAt = useCallback((pt: { x: number; y: number }) => {\n if (!geoCache) return -1;\n return glyphAt(geoCache, pt);\n }, []);\n\n // Initialize geometry cache\n useEffect(() => {\n if (!sel) return;\n const task = sel.getGeometry(pageIndex);\n task.wait((g) => (geoCache = g), ignore);\n\n return () => {\n task.abort({\n code: PdfErrorCode.Cancelled,\n message: 'Cancelled',\n });\n };\n }, [sel, pageIndex]);\n\n const handlers = useMemo(\n (): PointerEventHandlers => ({\n onPointerDown: (point) => {\n if (!sel) return;\n\n // clear the selection\n sel.clear();\n const task = sel.getGeometry(pageIndex);\n task.wait((geo) => {\n const g = glyphAt(geo, point);\n if (g !== -1) sel.begin(pageIndex, g);\n }, ignore);\n },\n onPointerMove: (point) => {\n if (!sel) return;\n const g = cachedGlyphAt(point);\n if (g !== -1) {\n setCursor('selection-text', 'text', 10);\n } else {\n removeCursor('selection-text');\n }\n if (g !== -1) sel.update(pageIndex, g);\n },\n onPointerUp: () => {\n if (!sel) return;\n sel.end();\n },\n }),\n [sel, pageIndex, cachedGlyphAt],\n );\n\n useEffect(() => {\n if (!register) return;\n return register(handlers);\n }, [register, handlers]);\n\n return (\n <>\n {rects.map((b, i) => (\n <div\n key={i}\n style={{\n position: 'absolute',\n left: b.origin.x * scale,\n top: b.origin.y * scale,\n width: b.size.width * scale,\n height: b.size.height * scale,\n background,\n pointerEvents: 'none',\n }}\n />\n ))}\n </>\n );\n}\n"],"mappings":";AAAA,SAAS,eAAe,iBAAiB;AACzC,SAAS,uBAAuB;AAEzB,IAAM,yBAAyB,MAAM,cAA+B,gBAAgB,EAAE;AACtF,IAAM,qBAAqB,MAAM,UAA2B,gBAAgB,EAAE;;;ACJrF,SAAS,aAAa,WAAW,SAAS,gBAAgB;AAC1D,SAAS,QAAQ,oBAA2C;AAC5D,SAAS,WAAW,0BAA0B;AAE9C,SAAS,eAAe;AAkFpB,mBAEI,WAFJ;AAxEG,SAAS,eAAe,EAAE,WAAW,OAAO,aAAa,mBAAmB,GAAU;AAC3F,QAAM,EAAE,UAAU,IAAI,IAAI,uBAAuB;AACjD,QAAM,EAAE,SAAS,IAAI,mBAAmB,EAAE,QAAQ,WAAW,UAAU,CAAC;AACxE,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAsB,CAAC,CAAC;AAClD,QAAM,EAAE,WAAW,aAAa,IAAI,UAAU;AAG9C,YAAU,MAAM;AACd,QAAI,CAAC,IAAK;AACV,WAAO,IAAI,kBAAkB,MAAM;AACjC,eAAS,IAAI,yBAAyB,SAAS,CAAC;AAAA,IAClD,CAAC;AAAA,EACH,GAAG,CAAC,KAAK,SAAS,CAAC;AAGnB,MAAI;AACJ,QAAM,gBAAgB,YAAY,CAAC,OAAiC;AAClE,QAAI,CAAC,SAAU,QAAO;AACtB,WAAO,QAAQ,UAAU,EAAE;AAAA,EAC7B,GAAG,CAAC,CAAC;AAGL,YAAU,MAAM;AACd,QAAI,CAAC,IAAK;AACV,UAAM,OAAO,IAAI,YAAY,SAAS;AACtC,SAAK,KAAK,CAAC,MAAO,WA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AAA,MACjB;AAAA;AAAA,IATK;AAAA,EAUP,CACD,GACH;AAEJ;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@embedpdf/plugin-selection",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -15,25 +15,30 @@
15
15
  "types": "./dist/preact/index.d.ts",
16
16
  "import": "./dist/preact/index.js",
17
17
  "require": "./dist/preact/index.cjs"
18
+ },
19
+ "./react": {
20
+ "types": "./dist/react/index.d.ts",
21
+ "import": "./dist/react/index.js",
22
+ "require": "./dist/react/index.cjs"
18
23
  }
19
24
  },
20
25
  "dependencies": {
21
- "@embedpdf/models": "1.0.5"
26
+ "@embedpdf/models": "1.0.6"
22
27
  },
23
28
  "devDependencies": {
24
29
  "@types/react": "^18.2.0",
25
30
  "tsup": "^8.0.0",
26
31
  "typescript": "^5.0.0",
27
- "@embedpdf/plugin-viewport": "1.0.5",
28
- "@embedpdf/plugin-interaction-manager": "1.0.5"
32
+ "@embedpdf/plugin-viewport": "1.0.6",
33
+ "@embedpdf/plugin-interaction-manager": "1.0.6"
29
34
  },
30
35
  "peerDependencies": {
31
36
  "react": ">=16.8.0",
32
37
  "react-dom": ">=16.8.0",
33
38
  "preact": "^10.26.4",
34
- "@embedpdf/plugin-viewport": "1.0.5",
35
- "@embedpdf/core": "1.0.5",
36
- "@embedpdf/plugin-interaction-manager": "1.0.5"
39
+ "@embedpdf/plugin-viewport": "1.0.6",
40
+ "@embedpdf/plugin-interaction-manager": "1.0.6",
41
+ "@embedpdf/core": "1.0.6"
37
42
  },
38
43
  "files": [
39
44
  "dist",