@embedpdf/plugin-annotation 1.0.13 → 1.0.15

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 (32) hide show
  1. package/dist/index.cjs +1 -1
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.js +103 -49
  4. package/dist/index.js.map +1 -1
  5. package/dist/lib/annotation-plugin.d.ts +1 -0
  6. package/dist/lib/helpers.d.ts +9 -2
  7. package/dist/lib/types.d.ts +48 -7
  8. package/dist/preact/adapter.d.ts +6 -0
  9. package/dist/preact/index.cjs +1 -1
  10. package/dist/preact/index.cjs.map +1 -1
  11. package/dist/preact/index.js +500 -9
  12. package/dist/preact/index.js.map +1 -1
  13. package/dist/react/adapter.d.ts +6 -1
  14. package/dist/react/index.cjs +1 -1
  15. package/dist/react/index.cjs.map +1 -1
  16. package/dist/react/index.js +500 -9
  17. package/dist/react/index.js.map +1 -1
  18. package/dist/shared-preact/components/annotation-container.d.ts +4 -2
  19. package/dist/shared-preact/components/annotations/free-text-paint.d.ts +10 -0
  20. package/dist/shared-preact/components/annotations/free-text.d.ts +13 -0
  21. package/dist/shared-preact/components/annotations/stamp-paint.d.ts +8 -0
  22. package/dist/shared-preact/components/annotations/stamp.d.ts +12 -0
  23. package/dist/shared-preact/components/render-annotation.d.ts +1 -1
  24. package/dist/shared-preact/hooks/use-drag-resize.d.ts +5 -2
  25. package/dist/shared-react/components/annotation-container.d.ts +4 -2
  26. package/dist/shared-react/components/annotations/free-text-paint.d.ts +10 -0
  27. package/dist/shared-react/components/annotations/free-text.d.ts +13 -0
  28. package/dist/shared-react/components/annotations/stamp-paint.d.ts +8 -0
  29. package/dist/shared-react/components/annotations/stamp.d.ts +12 -0
  30. package/dist/shared-react/components/render-annotation.d.ts +1 -1
  31. package/dist/shared-react/hooks/use-drag-resize.d.ts +5 -2
  32. package/package.json +9 -9
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { BasePlugin, createBehaviorEmitter, SET_DOCUMENT } from "@embedpdf/core";
2
- import { PdfAnnotationLineEnding, rectFromPoints, expandRect, rotateAndTranslatePoint, PdfAnnotationSubtype, PdfBlendMode, ignore, PdfTaskHelper, PdfErrorCode, Rotation, AppearanceMode, Task, PdfAnnotationBorderStyle } from "@embedpdf/models";
2
+ import { PdfAnnotationLineEnding, rectFromPoints, expandRect, rotateAndTranslatePoint, PdfAnnotationSubtype, PdfBlendMode, ignore, PdfTaskHelper, PdfErrorCode, Rotation, AppearanceMode, Task, PdfVerticalAlignment, PdfTextAlignment, PdfStandardFont, PdfAnnotationBorderStyle } from "@embedpdf/models";
3
3
  const ANNOTATION_PLUGIN_ID = "annotation";
4
4
  const manifest = {
5
5
  id: ANNOTATION_PLUGIN_ID,
@@ -315,6 +315,60 @@ const index = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.definePropert
315
315
  deriveRect,
316
316
  lineRectWithEndings
317
317
  }, Symbol.toStringTag, { value: "Module" }));
318
+ function isInk(a) {
319
+ return a.object.type === PdfAnnotationSubtype.INK;
320
+ }
321
+ function isCircle(a) {
322
+ return a.object.type === PdfAnnotationSubtype.CIRCLE;
323
+ }
324
+ function isPolygon(a) {
325
+ return a.object.type === PdfAnnotationSubtype.POLYGON;
326
+ }
327
+ function isSquare(a) {
328
+ return a.object.type === PdfAnnotationSubtype.SQUARE;
329
+ }
330
+ function isLine(a) {
331
+ return a.object.type === PdfAnnotationSubtype.LINE;
332
+ }
333
+ function isPolyline(a) {
334
+ return a.object.type === PdfAnnotationSubtype.POLYLINE;
335
+ }
336
+ function isHighlight(a) {
337
+ return a.object.type === PdfAnnotationSubtype.HIGHLIGHT;
338
+ }
339
+ function isUnderline(a) {
340
+ return a.object.type === PdfAnnotationSubtype.UNDERLINE;
341
+ }
342
+ function isStrikeout(a) {
343
+ return a.object.type === PdfAnnotationSubtype.STRIKEOUT;
344
+ }
345
+ function isSquiggly(a) {
346
+ return a.object.type === PdfAnnotationSubtype.SQUIGGLY;
347
+ }
348
+ function isTextMarkup(a) {
349
+ return isHighlight(a) || isUnderline(a) || isStrikeout(a) || isSquiggly(a);
350
+ }
351
+ function isFreeText(a) {
352
+ return a.object.type === PdfAnnotationSubtype.FREETEXT;
353
+ }
354
+ function isStamp(a) {
355
+ return a.object.type === PdfAnnotationSubtype.STAMP;
356
+ }
357
+ function isHighlightDefaults(defaults) {
358
+ return defaults.subtype === PdfAnnotationSubtype.HIGHLIGHT;
359
+ }
360
+ function isUnderlineDefaults(defaults) {
361
+ return defaults.subtype === PdfAnnotationSubtype.UNDERLINE;
362
+ }
363
+ function isStrikeoutDefaults(defaults) {
364
+ return defaults.subtype === PdfAnnotationSubtype.STRIKEOUT;
365
+ }
366
+ function isSquigglyDefaults(defaults) {
367
+ return defaults.subtype === PdfAnnotationSubtype.SQUIGGLY;
368
+ }
369
+ function isTextMarkupDefaults(defaults) {
370
+ return isHighlightDefaults(defaults) || isUnderlineDefaults(defaults) || isStrikeoutDefaults(defaults) || isSquigglyDefaults(defaults);
371
+ }
318
372
  const _AnnotationPlugin = class _AnnotationPlugin extends BasePlugin {
319
373
  constructor(id, registry, engine, config) {
320
374
  super(id, registry);
@@ -322,6 +376,7 @@ const _AnnotationPlugin = class _AnnotationPlugin extends BasePlugin {
322
376
  this.state$ = createBehaviorEmitter();
323
377
  this.modeByVariant = /* @__PURE__ */ new Map();
324
378
  this.variantByMode = /* @__PURE__ */ new Map();
379
+ this.pendingContexts = /* @__PURE__ */ new Map();
325
380
  this.activeVariantChange$ = createBehaviorEmitter();
326
381
  this.activeTool$ = createBehaviorEmitter({
327
382
  variantKey: null,
@@ -354,7 +409,6 @@ const _AnnotationPlugin = class _AnnotationPlugin extends BasePlugin {
354
409
  });
355
410
  (_b = this.interactionManager) == null ? void 0 : _b.onModeChange((s) => {
356
411
  const newVariant = this.variantByMode.get(s.activeMode) ?? null;
357
- console.log(newVariant, this.state.activeVariant);
358
412
  if (newVariant !== this.state.activeVariant) {
359
413
  this.dispatch(setActiveVariant(newVariant));
360
414
  this.activeVariantChange$.emit(newVariant);
@@ -363,19 +417,17 @@ const _AnnotationPlugin = class _AnnotationPlugin extends BasePlugin {
363
417
  (_c = this.selection) == null ? void 0 : _c.onEndSelection(() => {
364
418
  var _a2, _b2;
365
419
  if (!this.state.activeVariant) return;
366
- if (!(this.state.activeVariant === makeVariantKey(PdfAnnotationSubtype.HIGHLIGHT) || this.state.activeVariant === makeVariantKey(PdfAnnotationSubtype.UNDERLINE) || this.state.activeVariant === makeVariantKey(PdfAnnotationSubtype.STRIKEOUT) || this.state.activeVariant === makeVariantKey(PdfAnnotationSubtype.SQUIGGLY))) {
367
- return;
368
- }
420
+ const defaults = this.state.toolDefaults[this.state.activeVariant];
421
+ if (!defaults || !isTextMarkupDefaults(defaults)) return;
369
422
  const formattedSelection = (_a2 = this.selection) == null ? void 0 : _a2.getFormattedSelection();
370
423
  if (!formattedSelection) return;
371
424
  for (const selection of formattedSelection) {
372
425
  const rect = selection.rect;
373
426
  const segmentRects = selection.segmentRects;
374
- const type = this.state.activeVariant;
375
- const subtype = this.state.toolDefaults[type].subtype;
376
- const color = this.state.toolDefaults[type].color;
377
- const opacity = this.state.toolDefaults[type].opacity;
378
- const blendMode = this.state.toolDefaults[type].blendMode ?? PdfBlendMode.Normal;
427
+ const subtype = defaults.subtype;
428
+ const color = defaults.color;
429
+ const opacity = defaults.opacity;
430
+ const blendMode = defaults.blendMode ?? PdfBlendMode.Normal;
379
431
  this.createAnnotation(selection.pageIndex, {
380
432
  type: subtype,
381
433
  rect,
@@ -455,7 +507,7 @@ const _AnnotationPlugin = class _AnnotationPlugin extends BasePlugin {
455
507
  },
456
508
  getColorPresets: () => [...this.state.colorPresets],
457
509
  addColorPreset: (color) => this.dispatch(addColorPreset(color)),
458
- createAnnotation: (pageIndex, annotation) => this.createAnnotation(pageIndex, annotation),
510
+ createAnnotation: (pageIndex, annotation, ctx) => this.createAnnotation(pageIndex, annotation, ctx),
459
511
  updateAnnotation: (pageIndex, localId, patch) => this.updateAnnotation(pageIndex, localId, patch),
460
512
  deleteAnnotation: (pageIndex, localId) => this.deleteAnnotation(pageIndex, localId),
461
513
  renderAnnotation: (options) => this.renderAnnotation(options),
@@ -528,9 +580,12 @@ const _AnnotationPlugin = class _AnnotationPlugin extends BasePlugin {
528
580
  selectAnnotation(pageIndex, annotationId) {
529
581
  this.dispatch(selectAnnotation(pageIndex, annotationId));
530
582
  }
531
- createAnnotation(pageIndex, annotation) {
583
+ createAnnotation(pageIndex, annotation, ctx) {
532
584
  const localId = annotation.id;
533
- const execute = () => this.dispatch(createAnnotation(pageIndex, localId, annotation));
585
+ const execute = () => {
586
+ this.dispatch(createAnnotation(pageIndex, localId, annotation));
587
+ if (ctx) this.pendingContexts.set(localId, ctx);
588
+ };
534
589
  if (!this.history) {
535
590
  execute();
536
591
  if (this.config.autoCommit) this.commit();
@@ -539,6 +594,7 @@ const _AnnotationPlugin = class _AnnotationPlugin extends BasePlugin {
539
594
  const command = {
540
595
  execute,
541
596
  undo: () => {
597
+ this.pendingContexts.delete(localId);
542
598
  this.dispatch(deselectAnnotation());
543
599
  this.dispatch(deleteAnnotation(pageIndex, localId));
544
600
  }
@@ -606,8 +662,12 @@ const _AnnotationPlugin = class _AnnotationPlugin extends BasePlugin {
606
662
  affectedPages.add(pageIndex);
607
663
  switch (ta.commitState) {
608
664
  case "new":
609
- const task2 = this.engine.createPageAnnotation(doc, page, ta.object);
610
- task2.wait((annoId) => this.dispatch(storePdfId(uid, annoId)), ignore);
665
+ const ctx = this.pendingContexts.get(ta.localId);
666
+ const task2 = this.engine.createPageAnnotation(doc, page, ta.object, ctx);
667
+ task2.wait((annoId) => {
668
+ this.dispatch(storePdfId(uid, annoId));
669
+ this.pendingContexts.delete(ta.localId);
670
+ }, ignore);
611
671
  creations.push(task2);
612
672
  break;
613
673
  case "dirty":
@@ -665,7 +725,9 @@ const DEFAULT_COLORS = [
665
725
  "#25D2D1",
666
726
  "#597CE2",
667
727
  "#C544CE",
668
- "#7D2E25"
728
+ "#7D2E25",
729
+ "#000000",
730
+ "#FFFFFF"
669
731
  ];
670
732
  const patchAnno = (state, uid, patch) => {
671
733
  const prev = state.byUid[uid];
@@ -809,6 +871,24 @@ const initialState = (cfg) => ({
809
871
  strokeColor: "#E44234",
810
872
  strokeStyle: PdfAnnotationBorderStyle.SOLID
811
873
  },
874
+ [makeVariantKey(PdfAnnotationSubtype.FREETEXT)]: {
875
+ name: "Free Text",
876
+ subtype: PdfAnnotationSubtype.FREETEXT,
877
+ interaction: { mode: "freeText", exclusive: true, cursor: "crosshair" },
878
+ backgroundColor: "transparent",
879
+ opacity: 1,
880
+ fontSize: 14,
881
+ fontColor: "#E44234",
882
+ content: "Insert text here",
883
+ fontFamily: PdfStandardFont.Helvetica,
884
+ textAlign: PdfTextAlignment.Left,
885
+ verticalAlign: PdfVerticalAlignment.Top
886
+ },
887
+ [makeVariantKey(PdfAnnotationSubtype.STAMP)]: {
888
+ name: "Photo",
889
+ subtype: PdfAnnotationSubtype.STAMP,
890
+ interaction: { mode: "stamp", exclusive: true, cursor: "crosshair" }
891
+ },
812
892
  ...cfg.toolDefaults
813
893
  },
814
894
  colorPresets: cfg.colorPresets ?? DEFAULT_COLORS,
@@ -941,39 +1021,6 @@ const reducer = (state, action) => {
941
1021
  return state;
942
1022
  }
943
1023
  };
944
- function isInk(a) {
945
- return a.object.type === PdfAnnotationSubtype.INK;
946
- }
947
- function isCircle(a) {
948
- return a.object.type === PdfAnnotationSubtype.CIRCLE;
949
- }
950
- function isPolygon(a) {
951
- return a.object.type === PdfAnnotationSubtype.POLYGON;
952
- }
953
- function isSquare(a) {
954
- return a.object.type === PdfAnnotationSubtype.SQUARE;
955
- }
956
- function isLine(a) {
957
- return a.object.type === PdfAnnotationSubtype.LINE;
958
- }
959
- function isPolyline(a) {
960
- return a.object.type === PdfAnnotationSubtype.POLYLINE;
961
- }
962
- function isTextMarkup(a) {
963
- return a.object.type === PdfAnnotationSubtype.HIGHLIGHT || a.object.type === PdfAnnotationSubtype.UNDERLINE || a.object.type === PdfAnnotationSubtype.STRIKEOUT || a.object.type === PdfAnnotationSubtype.SQUIGGLY;
964
- }
965
- function isHighlight(a) {
966
- return a.object.type === PdfAnnotationSubtype.HIGHLIGHT;
967
- }
968
- function isUnderline(a) {
969
- return a.object.type === PdfAnnotationSubtype.UNDERLINE;
970
- }
971
- function isStrikeout(a) {
972
- return a.object.type === PdfAnnotationSubtype.STRIKEOUT;
973
- }
974
- function isSquiggly(a) {
975
- return a.object.type === PdfAnnotationSubtype.SQUIGGLY;
976
- }
977
1024
  const AnnotationPluginPackage = {
978
1025
  manifest,
979
1026
  create: (registry, engine, config) => new AnnotationPlugin(ANNOTATION_PLUGIN_ID, registry, engine, config),
@@ -993,7 +1040,9 @@ export {
993
1040
  getToolDefaultsBySubtypeAndIntent,
994
1041
  isAnnotationSelected,
995
1042
  isCircle,
1043
+ isFreeText,
996
1044
  isHighlight,
1045
+ isHighlightDefaults,
997
1046
  isInAnnotationVariant,
998
1047
  isInk,
999
1048
  isLine,
@@ -1001,9 +1050,14 @@ export {
1001
1050
  isPolyline,
1002
1051
  isSquare,
1003
1052
  isSquiggly,
1053
+ isSquigglyDefaults,
1054
+ isStamp,
1004
1055
  isStrikeout,
1056
+ isStrikeoutDefaults,
1005
1057
  isTextMarkup,
1058
+ isTextMarkupDefaults,
1006
1059
  isUnderline,
1060
+ isUnderlineDefaults,
1007
1061
  makeVariantKey,
1008
1062
  manifest,
1009
1063
  parseVariantKey,