|
| 1 | +import type { |
| 2 | + AnnotationTooltipCallback, |
| 3 | + Instance, |
| 4 | + RedactionAnnotation, |
| 5 | +} from "@nutrient-sdk/viewer"; |
| 6 | +import { baseOptions } from "../../shared/base-options"; |
| 7 | + |
| 8 | +let instance: Instance | null = null; |
| 9 | + |
| 10 | +const getAllAnnotations = async () => { |
| 11 | + if (!instance) |
| 12 | + return window.NutrientViewer.Immutable.List<RedactionAnnotation>(); |
| 13 | + |
| 14 | + let annotationsList = |
| 15 | + window.NutrientViewer.Immutable.List<RedactionAnnotation>(); |
| 16 | + |
| 17 | + for (let i = 0; i < instance.totalPageCount; i++) { |
| 18 | + const anns = (await instance.getAnnotations(i)).filter( |
| 19 | + (a): a is RedactionAnnotation => |
| 20 | + a instanceof window.NutrientViewer.Annotations.RedactionAnnotation, |
| 21 | + ); |
| 22 | + annotationsList = annotationsList.concat(anns); |
| 23 | + } |
| 24 | + return annotationsList; |
| 25 | +}; |
| 26 | + |
| 27 | +const redactionAnnotationsHandlerCallback: AnnotationTooltipCallback = ( |
| 28 | + annotation, |
| 29 | +) => |
| 30 | + annotation instanceof window.NutrientViewer.Annotations.RedactionAnnotation |
| 31 | + ? [ |
| 32 | + { |
| 33 | + type: "custom", |
| 34 | + title: "Accept", |
| 35 | + id: "tooltip-accept-annotation", |
| 36 | + className: "TooltipItem-Duplication", |
| 37 | + onPress: async () => { |
| 38 | + if (!instance) return; |
| 39 | + |
| 40 | + const allRedactionAnnotations = (await getAllAnnotations()).filter( |
| 41 | + (a) => a.id !== annotation.id, |
| 42 | + ); |
| 43 | + |
| 44 | + await instance.delete(allRedactionAnnotations); |
| 45 | + await instance.applyRedactions(); |
| 46 | + await instance.create(allRedactionAnnotations); |
| 47 | + }, |
| 48 | + }, |
| 49 | + { |
| 50 | + type: "custom", |
| 51 | + title: "Reject", |
| 52 | + id: "tooltip-reject-annotation", |
| 53 | + className: "TooltipItem-Duplication", |
| 54 | + onPress: async () => { |
| 55 | + if (!instance) return; |
| 56 | + instance.delete(annotation); |
| 57 | + }, |
| 58 | + }, |
| 59 | + ] |
| 60 | + : []; |
| 61 | + |
| 62 | +window.NutrientViewer.load({ |
| 63 | + ...baseOptions, |
| 64 | + theme: window.NutrientViewer.Theme.DARK, |
| 65 | + enableAutomaticLinkExtraction: true, |
| 66 | + annotationTooltipCallback: redactionAnnotationsHandlerCallback, |
| 67 | + toolbarItems: [ |
| 68 | + ...window.NutrientViewer.defaultToolbarItems, |
| 69 | + { type: "redact-rectangle" }, |
| 70 | + { type: "redact-text-highlighter" }, |
| 71 | + ], |
| 72 | +}).then((_instance) => { |
| 73 | + instance = _instance; |
| 74 | +}); |
0 commit comments