@embedpdf/plugin-annotation 1.0.5 → 1.0.7

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.
@@ -32,122 +32,170 @@ var import_plugin_annotation = require("@embedpdf/plugin-annotation");
32
32
  var useAnnotationPlugin = () => (0, import_preact.usePlugin)(import_plugin_annotation.AnnotationPlugin.id);
33
33
  var useAnnotationCapability = () => (0, import_preact.useCapability)(import_plugin_annotation.AnnotationPlugin.id);
34
34
 
35
- // src/preact/components/annotation-layer.tsx
36
- var import_hooks3 = require("preact/hooks");
37
-
38
- // ../models/dist/index.js
39
- var PdfAnnotationSubtype = /* @__PURE__ */ ((PdfAnnotationSubtype2) => {
40
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["UNKNOWN"] = 0] = "UNKNOWN";
41
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["TEXT"] = 1] = "TEXT";
42
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["LINK"] = 2] = "LINK";
43
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["FREETEXT"] = 3] = "FREETEXT";
44
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["LINE"] = 4] = "LINE";
45
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["SQUARE"] = 5] = "SQUARE";
46
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["CIRCLE"] = 6] = "CIRCLE";
47
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["POLYGON"] = 7] = "POLYGON";
48
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["POLYLINE"] = 8] = "POLYLINE";
49
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["HIGHLIGHT"] = 9] = "HIGHLIGHT";
50
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["UNDERLINE"] = 10] = "UNDERLINE";
51
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["SQUIGGLY"] = 11] = "SQUIGGLY";
52
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["STRIKEOUT"] = 12] = "STRIKEOUT";
53
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["STAMP"] = 13] = "STAMP";
54
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["CARET"] = 14] = "CARET";
55
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["INK"] = 15] = "INK";
56
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["POPUP"] = 16] = "POPUP";
57
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["FILEATTACHMENT"] = 17] = "FILEATTACHMENT";
58
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["SOUND"] = 18] = "SOUND";
59
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["MOVIE"] = 19] = "MOVIE";
60
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["WIDGET"] = 20] = "WIDGET";
61
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["SCREEN"] = 21] = "SCREEN";
62
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["PRINTERMARK"] = 22] = "PRINTERMARK";
63
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["TRAPNET"] = 23] = "TRAPNET";
64
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["WATERMARK"] = 24] = "WATERMARK";
65
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["THREED"] = 25] = "THREED";
66
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["RICHMEDIA"] = 26] = "RICHMEDIA";
67
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["XFAWIDGET"] = 27] = "XFAWIDGET";
68
- PdfAnnotationSubtype2[PdfAnnotationSubtype2["REDACT"] = 28] = "REDACT";
69
- return PdfAnnotationSubtype2;
70
- })(PdfAnnotationSubtype || {});
35
+ // src/preact/components/annotations.tsx
36
+ var import_models = require("@embedpdf/models");
37
+ var import_preact3 = require("@embedpdf/plugin-interaction-manager/preact");
38
+ var import_plugin_annotation2 = require("@embedpdf/plugin-annotation");
39
+ var import_hooks4 = require("preact/hooks");
71
40
 
72
- // src/lib/selectors.ts
73
- var getAnnotationsByPageIndex = (state, pageIndex) => {
74
- return state.annotations[pageIndex] || [];
75
- };
76
-
77
- // src/preact/components/annotations/highlight.tsx
41
+ // src/preact/components/selectable-container.tsx
78
42
  var import_hooks = require("preact/hooks");
43
+ var import_preact2 = require("@embedpdf/plugin-selection/preact");
79
44
  var import_jsx_runtime = require("preact/jsx-runtime");
80
- function HighlightAnnotation({
81
- annotation,
45
+ function SelectableAnnotationContainer({
46
+ trackedAnnotation,
82
47
  scale,
83
48
  isSelected = false,
84
- pageIndex
49
+ pageIndex,
50
+ children
85
51
  }) {
86
52
  const { provides: annotationProvides } = useAnnotationCapability();
87
- const highlightColor = annotation.color || { red: 255, green: 255, blue: 0, alpha: 76 };
88
- const rgbaColor = `rgba(${highlightColor.red}, ${highlightColor.green}, ${highlightColor.blue}, ${highlightColor.alpha / 255})`;
53
+ const { provides: selectionProvides } = (0, import_preact2.useSelectionCapability)();
89
54
  const handleClick = (0, import_hooks.useCallback)(
90
55
  (e) => {
91
56
  e.stopPropagation();
92
- if (annotationProvides) {
93
- annotationProvides.selectAnnotation(pageIndex, annotation.id);
57
+ if (annotationProvides && selectionProvides) {
58
+ annotationProvides.selectAnnotation(pageIndex, trackedAnnotation.localId);
59
+ selectionProvides.clear();
94
60
  }
95
61
  },
96
- [annotationProvides, isSelected, pageIndex, annotation.id]
62
+ [annotationProvides, selectionProvides, isSelected, pageIndex, trackedAnnotation.localId]
97
63
  );
98
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
64
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
65
+ children,
66
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
67
+ "div",
68
+ {
69
+ className: "markup-annotation",
70
+ style: {
71
+ position: "absolute",
72
+ mixBlendMode: "multiply",
73
+ cursor: "pointer",
74
+ outline: isSelected ? "2px solid #007ACC" : "none",
75
+ outlineOffset: isSelected ? "1px" : "0px",
76
+ left: `${trackedAnnotation.object.rect.origin.x * scale}px`,
77
+ top: `${trackedAnnotation.object.rect.origin.y * scale}px`,
78
+ width: `${trackedAnnotation.object.rect.size.width * scale}px`,
79
+ height: `${trackedAnnotation.object.rect.size.height * scale}px`,
80
+ zIndex: 1
81
+ },
82
+ onMouseDown: handleClick
83
+ }
84
+ )
85
+ ] });
86
+ }
87
+
88
+ // src/preact/components/text-markup/highlight.tsx
89
+ var import_jsx_runtime2 = require("preact/jsx-runtime");
90
+ function Highlight({ color = "#FFFF00", opacity = 0.5, rects, scale }) {
91
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: rects.map((b, i) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
99
92
  "div",
100
93
  {
101
- className: "highlight-annotation",
102
94
  style: {
103
95
  position: "absolute",
104
- mixBlendMode: "multiply",
105
- cursor: "pointer",
106
- outline: isSelected ? "2px solid #007ACC" : "none",
107
- left: `${annotation.rect.origin.x * scale}px`,
108
- top: `${annotation.rect.origin.y * scale}px`,
109
- width: `${annotation.rect.size.width * scale}px`,
110
- height: `${annotation.rect.size.height * scale}px`
111
- },
112
- onMouseDown: handleClick,
113
- children: annotation.segmentRects.map((rect, index) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
114
- "div",
115
- {
116
- style: {
117
- position: "absolute",
118
- left: `${(rect.origin.x - annotation.rect.origin.x) * scale}px`,
119
- top: `${(rect.origin.y - annotation.rect.origin.y) * scale}px`,
120
- width: `${rect.size.width * scale}px`,
121
- height: `${rect.size.height * scale}px`,
122
- backgroundColor: rgbaColor
123
- }
124
- },
125
- index
126
- ))
127
- }
128
- ) });
96
+ left: b.origin.x * scale,
97
+ top: b.origin.y * scale,
98
+ width: b.size.width * scale,
99
+ height: b.size.height * scale,
100
+ background: color,
101
+ opacity,
102
+ pointerEvents: "none"
103
+ }
104
+ },
105
+ i
106
+ )) });
129
107
  }
130
108
 
131
- // src/preact/components/annotation-layer.tsx
132
- var import_preact2 = require("@embedpdf/plugin-interaction-manager/preact");
133
- var import_jsx_runtime2 = require("preact/jsx-runtime");
134
- function AnnotationLayer({ pageIndex, scale, style, ...props }) {
109
+ // src/preact/components/text-markup/underline.tsx
110
+ var import_jsx_runtime3 = require("preact/jsx-runtime");
111
+ function Underline({ color = "#FFFF00", opacity = 0.5, rects, scale }) {
112
+ const thickness = 2 * scale;
113
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children: rects.map((r, i) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
114
+ "div",
115
+ {
116
+ style: {
117
+ position: "absolute",
118
+ left: r.origin.x * scale,
119
+ top: (r.origin.y + r.size.height) * scale - thickness,
120
+ width: r.size.width * scale,
121
+ height: thickness,
122
+ background: color,
123
+ opacity,
124
+ pointerEvents: "none"
125
+ }
126
+ },
127
+ i
128
+ )) });
129
+ }
130
+
131
+ // src/preact/components/text-markup/strikeout.tsx
132
+ var import_jsx_runtime4 = require("preact/jsx-runtime");
133
+ function Strikeout({ color = "#FFFF00", opacity = 0.5, rects, scale }) {
134
+ const thickness = 2 * scale;
135
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_jsx_runtime4.Fragment, { children: rects.map((r, i) => /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
136
+ "div",
137
+ {
138
+ style: {
139
+ position: "absolute",
140
+ left: r.origin.x * scale,
141
+ top: (r.origin.y + r.size.height / 2) * scale - thickness / 2,
142
+ width: r.size.width * scale,
143
+ height: thickness,
144
+ background: color,
145
+ opacity,
146
+ pointerEvents: "none"
147
+ }
148
+ },
149
+ i
150
+ )) });
151
+ }
152
+
153
+ // src/preact/components/text-markup/squiggly.tsx
154
+ var import_jsx_runtime5 = require("preact/jsx-runtime");
155
+ function Squiggly({ color = "#FFFF00", opacity = 0.5, rects, scale }) {
156
+ const amplitude = 2 * scale;
157
+ const period = 6 * scale;
158
+ const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${period}" height="${amplitude * 2}" viewBox="0 0 ${period} ${amplitude * 2}">
159
+ <path d="M0 ${amplitude} Q ${period / 4} 0 ${period / 2} ${amplitude} T ${period} ${amplitude}"
160
+ fill="none" stroke="${color}" stroke-width="${amplitude}" stroke-linecap="round"/>
161
+ </svg>`;
162
+ const svgDataUri = `url("data:image/svg+xml;utf8,${encodeURIComponent(svg)}")`;
163
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(import_jsx_runtime5.Fragment, { children: rects.map((r, i) => /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
164
+ "div",
165
+ {
166
+ style: {
167
+ position: "absolute",
168
+ left: r.origin.x * scale,
169
+ top: (r.origin.y + r.size.height) * scale - amplitude,
170
+ width: r.size.width * scale,
171
+ height: amplitude * 2,
172
+ backgroundImage: svgDataUri,
173
+ backgroundRepeat: "repeat-x",
174
+ backgroundSize: `${period}px ${amplitude * 2}px`,
175
+ opacity,
176
+ pointerEvents: "none"
177
+ }
178
+ },
179
+ i
180
+ )) });
181
+ }
182
+
183
+ // src/preact/components/annotations.tsx
184
+ var import_jsx_runtime6 = require("preact/jsx-runtime");
185
+ function Annotations({ pageIndex, scale }) {
135
186
  const { provides: annotationProvides } = useAnnotationCapability();
136
- const [annotations, setAnnotations] = (0, import_hooks3.useState)([]);
137
- const { register } = (0, import_preact2.usePointerHandlers)({ pageIndex });
138
- const [selectionState, setSelectionState] = (0, import_hooks3.useState)({ selectedPageIndex: void 0, selectedAnnotationId: void 0 });
139
- (0, import_hooks3.useEffect)(() => {
187
+ const [annotations, setAnnotations] = (0, import_hooks4.useState)([]);
188
+ const { register } = (0, import_preact3.usePointerHandlers)({ pageIndex });
189
+ const [selectionState, setSelectionState] = (0, import_hooks4.useState)(null);
190
+ (0, import_hooks4.useEffect)(() => {
140
191
  if (annotationProvides) {
141
192
  annotationProvides.onStateChange((state) => {
142
- setAnnotations(getAnnotationsByPageIndex(state, pageIndex));
143
- setSelectionState({
144
- selectedPageIndex: state.selectedAnnotation?.pageIndex,
145
- selectedAnnotationId: state.selectedAnnotation?.annotationId
146
- });
193
+ setAnnotations((0, import_plugin_annotation2.getAnnotationsByPageIndex)(state, pageIndex));
194
+ setSelectionState((0, import_plugin_annotation2.getSelectedAnnotationByPageIndex)(state, pageIndex));
147
195
  });
148
196
  }
149
197
  }, [annotationProvides]);
150
- const handlers = (0, import_hooks3.useMemo)(
198
+ const handlers = (0, import_hooks4.useMemo)(
151
199
  () => ({
152
200
  onPointerDown: (_, pe) => {
153
201
  if (pe.target === pe.currentTarget && annotationProvides) {
@@ -157,34 +205,180 @@ function AnnotationLayer({ pageIndex, scale, style, ...props }) {
157
205
  }),
158
206
  [annotationProvides]
159
207
  );
160
- (0, import_hooks3.useEffect)(() => {
208
+ (0, import_hooks4.useEffect)(() => {
161
209
  return register(handlers);
162
210
  }, [register, handlers]);
163
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
211
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_jsx_runtime6.Fragment, { children: annotations.map((annotation) => {
212
+ const isSelected = selectionState?.localId === annotation.localId;
213
+ switch (annotation.object.type) {
214
+ case import_models.PdfAnnotationSubtype.UNDERLINE:
215
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
216
+ SelectableAnnotationContainer,
217
+ {
218
+ trackedAnnotation: annotation,
219
+ scale,
220
+ isSelected,
221
+ pageIndex,
222
+ children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
223
+ Underline,
224
+ {
225
+ color: annotation.object.color,
226
+ opacity: annotation.object.opacity,
227
+ rects: annotation.object.segmentRects,
228
+ scale
229
+ }
230
+ )
231
+ },
232
+ annotation.localId
233
+ );
234
+ case import_models.PdfAnnotationSubtype.STRIKEOUT:
235
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
236
+ SelectableAnnotationContainer,
237
+ {
238
+ trackedAnnotation: annotation,
239
+ scale,
240
+ isSelected,
241
+ pageIndex,
242
+ children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
243
+ Strikeout,
244
+ {
245
+ color: annotation.object.color,
246
+ opacity: annotation.object.opacity,
247
+ rects: annotation.object.segmentRects,
248
+ scale
249
+ }
250
+ )
251
+ },
252
+ annotation.localId
253
+ );
254
+ case import_models.PdfAnnotationSubtype.SQUIGGLY:
255
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
256
+ SelectableAnnotationContainer,
257
+ {
258
+ trackedAnnotation: annotation,
259
+ scale,
260
+ isSelected,
261
+ pageIndex,
262
+ children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
263
+ Squiggly,
264
+ {
265
+ color: annotation.object.color,
266
+ opacity: annotation.object.opacity,
267
+ rects: annotation.object.segmentRects,
268
+ scale
269
+ }
270
+ )
271
+ },
272
+ annotation.localId
273
+ );
274
+ case import_models.PdfAnnotationSubtype.HIGHLIGHT:
275
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
276
+ SelectableAnnotationContainer,
277
+ {
278
+ trackedAnnotation: annotation,
279
+ scale,
280
+ isSelected,
281
+ pageIndex,
282
+ children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
283
+ Highlight,
284
+ {
285
+ color: annotation.object.color,
286
+ opacity: annotation.object.opacity,
287
+ rects: annotation.object.segmentRects,
288
+ scale
289
+ }
290
+ )
291
+ },
292
+ annotation.localId
293
+ );
294
+ default:
295
+ return null;
296
+ }
297
+ }) });
298
+ }
299
+
300
+ // src/preact/components/text-markup.tsx
301
+ var import_models2 = require("@embedpdf/models");
302
+ var import_preact4 = require("@embedpdf/plugin-selection/preact");
303
+ var import_hooks5 = require("preact/hooks");
304
+ var import_jsx_runtime7 = require("preact/jsx-runtime");
305
+ function TextMarkup({ pageIndex, scale }) {
306
+ const { provides: selectionProvides } = (0, import_preact4.useSelectionCapability)();
307
+ const { provides: annotationProvides } = useAnnotationCapability();
308
+ const [rects, setRects] = (0, import_hooks5.useState)([]);
309
+ const [activeTool, setActiveTool] = (0, import_hooks5.useState)({ mode: null, defaults: null });
310
+ (0, import_hooks5.useEffect)(() => {
311
+ if (!selectionProvides) return;
312
+ const off = selectionProvides.onSelectionChange(() => {
313
+ setRects(selectionProvides.getHighlightRectsForPage(pageIndex));
314
+ });
315
+ return off;
316
+ }, [selectionProvides, pageIndex]);
317
+ (0, import_hooks5.useEffect)(() => {
318
+ if (!annotationProvides) return;
319
+ const off = annotationProvides.onActiveToolChange(setActiveTool);
320
+ return off;
321
+ }, [annotationProvides]);
322
+ switch (activeTool.mode) {
323
+ case import_models2.PdfAnnotationSubtype.UNDERLINE:
324
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
325
+ Underline,
326
+ {
327
+ color: activeTool.defaults?.color,
328
+ opacity: activeTool.defaults?.opacity,
329
+ rects,
330
+ scale
331
+ }
332
+ );
333
+ case import_models2.PdfAnnotationSubtype.HIGHLIGHT:
334
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
335
+ Highlight,
336
+ {
337
+ color: activeTool.defaults?.color,
338
+ opacity: activeTool.defaults?.opacity,
339
+ rects,
340
+ scale
341
+ }
342
+ );
343
+ case import_models2.PdfAnnotationSubtype.STRIKEOUT:
344
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
345
+ Strikeout,
346
+ {
347
+ color: activeTool.defaults?.color,
348
+ opacity: activeTool.defaults?.opacity,
349
+ rects,
350
+ scale
351
+ }
352
+ );
353
+ case import_models2.PdfAnnotationSubtype.SQUIGGLY:
354
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
355
+ Squiggly,
356
+ {
357
+ color: activeTool.defaults?.color,
358
+ opacity: activeTool.defaults?.opacity,
359
+ rects,
360
+ scale
361
+ }
362
+ );
363
+ default:
364
+ return null;
365
+ }
366
+ }
367
+
368
+ // src/preact/components/annotation-layer.tsx
369
+ var import_jsx_runtime8 = require("preact/jsx-runtime");
370
+ function AnnotationLayer({ pageIndex, scale, style, ...props }) {
371
+ return /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(
164
372
  "div",
165
373
  {
166
374
  style: {
167
375
  ...style
168
376
  },
169
377
  ...props,
170
- children: annotations.map((annotation) => {
171
- const isSelected = selectionState.selectedPageIndex === pageIndex && selectionState.selectedAnnotationId === annotation.id;
172
- switch (annotation.type) {
173
- case PdfAnnotationSubtype.HIGHLIGHT:
174
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
175
- HighlightAnnotation,
176
- {
177
- annotation,
178
- scale,
179
- isSelected,
180
- pageIndex
181
- },
182
- annotation.id
183
- );
184
- default:
185
- return null;
186
- }
187
- })
378
+ children: [
379
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(Annotations, { pageIndex, scale }),
380
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(TextMarkup, { pageIndex, scale })
381
+ ]
188
382
  }
189
383
  );
190
384
  }