@embedpdf/plugin-redaction 2.9.0 → 2.10.0

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.
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { BasePlugin, createBehaviorEmitter, refreshPages } from "@embedpdf/core";
2
2
  import { PdfAnnotationSubtype, PdfPermissionFlag, uuidV4, PdfTaskHelper, PdfErrorCode, Task } from "@embedpdf/models";
3
+ import { defineAnnotationTool } from "@embedpdf/plugin-annotation";
3
4
  var RedactionMode = /* @__PURE__ */ ((RedactionMode2) => {
4
5
  RedactionMode2["Redact"] = "redact";
5
6
  RedactionMode2["MarqueeRedact"] = "marqueeRedact";
@@ -257,9 +258,10 @@ const redactionReducer = (state = initialState, action) => {
257
258
  return state;
258
259
  }
259
260
  };
260
- const redactTool = {
261
+ const redactTool = defineAnnotationTool({
261
262
  id: "redact",
262
263
  name: "Redact",
264
+ categories: ["annotation", "redaction"],
263
265
  matchScore: (a) => a.type === PdfAnnotationSubtype.REDACT ? 10 : 0,
264
266
  interaction: {
265
267
  mode: RedactionMode.Redact,
@@ -293,7 +295,7 @@ const redactTool = {
293
295
  behavior: {
294
296
  useAppearanceStream: false
295
297
  }
296
- };
298
+ });
297
299
  const redactTools = [redactTool];
298
300
  const _RedactionPlugin = class _RedactionPlugin extends BasePlugin {
299
301
  constructor(id, registry, config) {
@@ -325,7 +327,7 @@ const _RedactionPlugin = class _RedactionPlugin extends BasePlugin {
325
327
  } else {
326
328
  this.useAnnotationMode = false;
327
329
  }
328
- if (this.useAnnotationMode) {
330
+ if (this.useAnnotationMode && this.annotationCapability) {
329
331
  for (const tool of redactTools) {
330
332
  this.annotationCapability.addTool(tool);
331
333
  }
@@ -680,6 +682,14 @@ const _RedactionPlugin = class _RedactionPlugin extends BasePlugin {
680
682
  }
681
683
  return state;
682
684
  }
685
+ isRedactTool(tool) {
686
+ return (tool == null ? void 0 : tool.id) === redactTool.id && tool.defaults.type === PdfAnnotationSubtype.REDACT;
687
+ }
688
+ getRedactTool() {
689
+ var _a;
690
+ const tool = (_a = this.annotationCapability) == null ? void 0 : _a.getTool("redact");
691
+ return this.isRedactTool(tool) ? tool : void 0;
692
+ }
683
693
  // ─────────────────────────────────────────────────────────
684
694
  // Annotation Mode State Sync
685
695
  // ─────────────────────────────────────────────────────────
@@ -763,6 +773,7 @@ const _RedactionPlugin = class _RedactionPlugin extends BasePlugin {
763
773
  return;
764
774
  }
765
775
  if (this.useAnnotationMode) {
776
+ if (!this.annotationCapability) return;
766
777
  const annoScope = this.annotationCapability.forDocument(id);
767
778
  for (const item of items) {
768
779
  const annotation = this.redactionItemToAnnotation(item);
@@ -962,9 +973,9 @@ const _RedactionPlugin = class _RedactionPlugin extends BasePlugin {
962
973
  * In legacy mode: returns hardcoded red
963
974
  */
964
975
  getPreviewStrokeColor() {
965
- if (this.useAnnotationMode && this.annotationCapability) {
966
- const tool = this.annotationCapability.getTool("redact");
967
- return (tool == null ? void 0 : tool.defaults.strokeColor) ?? "#FF0000";
976
+ var _a;
977
+ if (this.useAnnotationMode) {
978
+ return ((_a = this.getRedactTool()) == null ? void 0 : _a.defaults.strokeColor) ?? "#FF0000";
968
979
  }
969
980
  return "#FF0000";
970
981
  }
@@ -1222,7 +1233,14 @@ const _RedactionPlugin = class _RedactionPlugin extends BasePlugin {
1222
1233
  * Annotation mode: Apply all redactions using engine.applyAllRedactions per page
1223
1234
  */
1224
1235
  applyAllRedactionsAnnotationMode(docId, doc) {
1225
- const annoState = this.annotationCapability.forDocument(docId).getState();
1236
+ const annotationCapability = this.annotationCapability;
1237
+ if (!annotationCapability) {
1238
+ return PdfTaskHelper.reject({
1239
+ code: PdfErrorCode.NotFound,
1240
+ message: "Annotation capability not found"
1241
+ });
1242
+ }
1243
+ const annoState = annotationCapability.forDocument(docId).getState();
1226
1244
  const redactAnnotationsByPage = /* @__PURE__ */ new Map();
1227
1245
  for (const ta of Object.values(annoState.byUid)) {
1228
1246
  if (ta.object.type === PdfAnnotationSubtype.REDACT) {
@@ -1244,18 +1262,18 @@ const _RedactionPlugin = class _RedactionPlugin extends BasePlugin {
1244
1262
  const task = new Task();
1245
1263
  Task.all(tasks).wait(
1246
1264
  () => {
1247
- var _a, _b;
1248
- const annoScope = (_a = this.annotationCapability) == null ? void 0 : _a.forDocument(docId);
1265
+ var _a;
1266
+ const annoScope = annotationCapability.forDocument(docId);
1249
1267
  const allPurgedIds = [];
1250
1268
  for (const [pageIndex, ids] of redactAnnotationsByPage) {
1251
1269
  for (const id of ids) {
1252
- annoScope == null ? void 0 : annoScope.purgeAnnotation(pageIndex, id);
1270
+ annoScope.purgeAnnotation(pageIndex, id);
1253
1271
  this.dispatch(removePending(docId, pageIndex, id));
1254
1272
  allPurgedIds.push(id);
1255
1273
  }
1256
1274
  }
1257
1275
  if (allPurgedIds.length > 0) {
1258
- (_b = this.historyCapability) == null ? void 0 : _b.forDocument(docId).purgeByMetadata(
1276
+ (_a = this.historyCapability) == null ? void 0 : _a.forDocument(docId).purgeByMetadata(
1259
1277
  (meta) => {
1260
1278
  var _a2;
1261
1279
  return ((_a2 = meta == null ? void 0 : meta.annotationIds) == null ? void 0 : _a2.some((id) => allPurgedIds.includes(id))) ?? false;
@@ -1287,10 +1305,10 @@ const _RedactionPlugin = class _RedactionPlugin extends BasePlugin {
1287
1305
  * @returns Array of annotation IDs that were created
1288
1306
  */
1289
1307
  createRedactAnnotationsFromSelection(documentId, formattedSelection, text) {
1308
+ var _a;
1290
1309
  if (!this.annotationCapability) return [];
1291
1310
  const annoScope = this.annotationCapability.forDocument(documentId);
1292
- const tool = this.annotationCapability.getTool("redact");
1293
- const defaults = tool == null ? void 0 : tool.defaults;
1311
+ const defaults = (_a = this.getRedactTool()) == null ? void 0 : _a.defaults;
1294
1312
  const annotationIds = [];
1295
1313
  for (const selection of formattedSelection) {
1296
1314
  const annotationId = uuidV4();
@@ -1354,10 +1372,10 @@ const _RedactionPlugin = class _RedactionPlugin extends BasePlugin {
1354
1372
  * Create a REDACT annotation from an area/marquee selection (annotation mode only)
1355
1373
  */
1356
1374
  createRedactAnnotationFromArea(documentId, pageIndex, rect) {
1375
+ var _a;
1357
1376
  if (!this.annotationCapability) return;
1358
1377
  const annoScope = this.annotationCapability.forDocument(documentId);
1359
- const tool = this.annotationCapability.getTool("redact");
1360
- const defaults = tool == null ? void 0 : tool.defaults;
1378
+ const defaults = (_a = this.getRedactTool()) == null ? void 0 : _a.defaults;
1361
1379
  const annotationId = uuidV4();
1362
1380
  const annotation = {
1363
1381
  ...defaults,
@@ -1377,8 +1395,7 @@ const _RedactionPlugin = class _RedactionPlugin extends BasePlugin {
1377
1395
  */
1378
1396
  redactionItemToAnnotation(item) {
1379
1397
  var _a;
1380
- const tool = (_a = this.annotationCapability) == null ? void 0 : _a.getTool("redact");
1381
- const defaults = (tool == null ? void 0 : tool.defaults) ?? {};
1398
+ const defaults = ((_a = this.getRedactTool()) == null ? void 0 : _a.defaults) ?? {};
1382
1399
  return {
1383
1400
  ...defaults,
1384
1401
  id: item.id,