|
| 1 | +/** |
| 2 | + * Custom Overlays Implementation (TypeScript) |
| 3 | + * |
| 4 | + * This example demonstrates advanced Nutrient Web SDK features: |
| 5 | + * - Custom overlay items that appear on page clicks |
| 6 | + * - Interactive overlays with HTML content |
| 7 | + * - Event handling for page interactions |
| 8 | + * - Dynamic overlay positioning |
| 9 | + * Framework implementations should import this and call loadCustomOverlaysViewer() with their container element. |
| 10 | + */ |
| 11 | + |
| 12 | +import type NutrientViewer from "@nutrient-sdk/viewer"; |
| 13 | +import type { Configuration } from "@nutrient-sdk/viewer"; |
| 14 | + |
| 15 | +/** |
| 16 | + * Load a PDF viewer with custom overlays functionality |
| 17 | + * @param nutrientViewer - The NutrientViewer object (from CDN) |
| 18 | + * @param container - The container element to mount the viewer |
| 19 | + * @param document - URL to the PDF document |
| 20 | + * @returns Promise that resolves when the viewer is loaded |
| 21 | + */ |
| 22 | +export async function loadCustomOverlaysViewer( |
| 23 | + nutrientViewer: typeof NutrientViewer, |
| 24 | + container: HTMLElement, |
| 25 | + document = "https://www.nutrient.io/downloads/nutrient-web-demo.pdf", |
| 26 | +) { |
| 27 | + if (!nutrientViewer) { |
| 28 | + throw new Error("NutrientViewer is required"); |
| 29 | + } |
| 30 | + |
| 31 | + // Ensure there's only one NutrientViewer instance |
| 32 | + nutrientViewer.unload(container); |
| 33 | + |
| 34 | + // Load the viewer with custom overlays configuration |
| 35 | + return load(nutrientViewer, { |
| 36 | + container, |
| 37 | + document, |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Internal load function with custom overlays configuration |
| 43 | + * @param nutrientViewer - The NutrientViewer object |
| 44 | + * @param defaultConfiguration - Base configuration object |
| 45 | + */ |
| 46 | +function load( |
| 47 | + nutrientViewer: NonNullable<typeof window.NutrientViewer>, |
| 48 | + defaultConfiguration: Configuration, |
| 49 | +) { |
| 50 | + return nutrientViewer.load(defaultConfiguration).then((instance) => { |
| 51 | + console.log("Nutrient Web SDK successfully loaded!!", instance); |
| 52 | + |
| 53 | + // Every time a user clicks on the page, we show a custom overlay item on |
| 54 | + // this page. |
| 55 | + instance.addEventListener("page.press", (event) => { |
| 56 | + if (event.pageIndex === 0) { |
| 57 | + instance.setCustomOverlayItem(getOverlayItemForPage1(nutrientViewer)); |
| 58 | + } |
| 59 | + |
| 60 | + if (event.pageIndex === 1) { |
| 61 | + instance.setCustomOverlayItem(getOverlayItemForPage2(nutrientViewer)); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + return instance; |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +function getOverlayItemForPage1( |
| 70 | + nutrientViewer: NonNullable<typeof window.NutrientViewer>, |
| 71 | +) { |
| 72 | + // We create a div element with an emoji and a short text. |
| 73 | + const overlayElement = document.createElement("div"); |
| 74 | + |
| 75 | + overlayElement.style.cssText = |
| 76 | + "width: 300px;background: #FFF; border: 1px #333 solid; font-family: sans-serif; font-size: 14px; padding: 20px;"; |
| 77 | + overlayElement.innerHTML = |
| 78 | + "<p>👋 This is an overlay item that appears when clicking on the first page. Find out what happens when you click on the second page."; |
| 79 | + |
| 80 | + // Then we return a NutrientViewer.CustomOverlayItem which uses the overlayElement |
| 81 | + // that we created above as a node, the pageIndex we get from our onPress |
| 82 | + // event and define the position on the page. |
| 83 | + return new nutrientViewer.CustomOverlayItem({ |
| 84 | + id: "overlay-item-first-page", |
| 85 | + node: overlayElement, |
| 86 | + pageIndex: 0, |
| 87 | + position: new nutrientViewer.Geometry.Point({ x: 300, y: 50 }), |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +function getOverlayItemForPage2( |
| 92 | + nutrientViewer: NonNullable<typeof window.NutrientViewer>, |
| 93 | +) { |
| 94 | + const overlayElement = document.createElement("div"); |
| 95 | + |
| 96 | + // In this case we embed a video to the page |
| 97 | + overlayElement.innerHTML = |
| 98 | + '<iframe src="https://player.vimeo.com/video/227250697" width="500" height="360" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>'; |
| 99 | + |
| 100 | + // Then we return a NutrientViewer.CustomOverlayItem which uses the overlayElement |
| 101 | + // that we created above as a node, the pageIndex we get from our onPress |
| 102 | + // event and define the position on the page. |
| 103 | + return new nutrientViewer.CustomOverlayItem({ |
| 104 | + id: "overlay-item-second-page", |
| 105 | + node: overlayElement, |
| 106 | + pageIndex: 1, |
| 107 | + position: new nutrientViewer.Geometry.Point({ x: 55, y: 220 }), |
| 108 | + }); |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Unload the custom overlays viewer from a container |
| 113 | + * @param nutrientViewer - The NutrientViewer object (from CDN) |
| 114 | + * @param container - The container element |
| 115 | + */ |
| 116 | +export async function unloadCustomOverlaysViewer( |
| 117 | + nutrientViewer: typeof NutrientViewer, |
| 118 | + container: HTMLElement, |
| 119 | +) { |
| 120 | + if (nutrientViewer) { |
| 121 | + nutrientViewer.unload(container); |
| 122 | + } |
| 123 | +} |
0 commit comments