|
1 | | -/** |
2 | | - * Custom Overlays Implementation for SolidJS (JavaScript) |
3 | | - * |
4 | | - * Demonstrates interactive overlays that appear on page clicks |
5 | | - */ |
6 | | - |
7 | 1 | import { nutrientConfig } from "../nutrient-config.js"; |
8 | 2 |
|
9 | 3 | /** |
10 | | - * Load the viewer with custom overlay functionality |
11 | | - * @param nutrientViewer - The NutrientViewer object |
12 | | - * @param container - The container element |
| 4 | + * Load a PDF viewer with custom overlays functionality |
| 5 | + * @param nutrientViewer - The NutrientViewer object (from CDN or package) |
| 6 | + * @param container - The container element to mount the viewer |
13 | 7 | * @param document - URL to the PDF document |
| 8 | + * @returns Promise that resolves when the viewer is loaded |
14 | 9 | */ |
15 | 10 | export async function loadCustomOverlaysViewer( |
16 | 11 | nutrientViewer, |
17 | 12 | container, |
18 | 13 | document = nutrientConfig.documentUrl, |
19 | 14 | ) { |
| 15 | + if (!nutrientViewer) { |
| 16 | + throw new Error("NutrientViewer is required"); |
| 17 | + } |
| 18 | + |
| 19 | + // Ensure there's only one NutrientViewer instance |
| 20 | + nutrientViewer.unload(container); |
| 21 | + |
20 | 22 | const config = { |
21 | 23 | container, |
22 | 24 | document, |
23 | 25 | ...(nutrientConfig.baseUrl && { baseUrl: nutrientConfig.baseUrl }), |
24 | 26 | }; |
25 | 27 |
|
26 | | - const instance = await nutrientViewer.load(config); |
| 28 | + // Load the viewer with custom overlays configuration |
| 29 | + return load(nutrientViewer, config); |
| 30 | +} |
27 | 31 |
|
28 | | - // Add click event listener for custom overlays |
29 | | - instance.addEventListener("page.click", (event) => { |
30 | | - const { pageIndex, point } = event; |
| 32 | +/** |
| 33 | + * Internal load function with custom overlays configuration |
| 34 | + * @param nutrientViewer - The NutrientViewer object |
| 35 | + * @param defaultConfiguration - Base configuration object |
| 36 | + */ |
| 37 | +function load(nutrientViewer, defaultConfiguration) { |
| 38 | + return nutrientViewer.load(defaultConfiguration).then((instance) => { |
| 39 | + console.log("Nutrient Web SDK successfully loaded!!", instance); |
31 | 40 |
|
32 | | - // Create overlay content |
33 | | - const overlayElement = document.createElement("div"); |
34 | | - overlayElement.innerHTML = ` |
35 | | - <div style="background: white; padding: 15px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.2);"> |
36 | | - <h3 style="margin: 0 0 10px 0;">Page ${pageIndex + 1}</h3> |
37 | | - <p style="margin: 0;">Clicked at: (${Math.round(point.x)}, ${Math.round(point.y)})</p> |
38 | | - <button id="close-overlay" style="margin-top: 10px; padding: 5px 10px;">Close</button> |
39 | | - </div> |
40 | | - `; |
| 41 | + // Every time a user clicks on the page, we show a custom overlay item on |
| 42 | + // this page. |
| 43 | + instance.addEventListener("page.press", (event) => { |
| 44 | + if (event.pageIndex === 0) { |
| 45 | + instance.setCustomOverlayItem(getOverlayItemForPage1(nutrientViewer)); |
| 46 | + } |
41 | 47 |
|
42 | | - // Add overlay to the instance |
43 | | - const overlay = new nutrientViewer.Overlay({ |
44 | | - element: overlayElement, |
45 | | - pageIndex, |
46 | | - position: point, |
47 | | - onDisappear: () => overlay.remove(), |
| 48 | + if (event.pageIndex === 1) { |
| 49 | + instance.setCustomOverlayItem(getOverlayItemForPage2(nutrientViewer)); |
| 50 | + } |
48 | 51 | }); |
49 | 52 |
|
50 | | - // Close button handler |
51 | | - overlayElement |
52 | | - .querySelector("#close-overlay") |
53 | | - .addEventListener("click", () => { |
54 | | - overlay.remove(); |
55 | | - }); |
| 53 | + return instance; |
| 54 | + }); |
| 55 | +} |
| 56 | + |
| 57 | +function getOverlayItemForPage1(nutrientViewer) { |
| 58 | + // We create a div element with an emoji and a short text. |
| 59 | + const overlayElement = document.createElement("div"); |
| 60 | + |
| 61 | + overlayElement.style.cssText = |
| 62 | + "width: 300px;background: #FFF; border: 1px #333 solid; font-family: sans-serif; font-size: 14px; padding: 20px;"; |
| 63 | + overlayElement.innerHTML = |
| 64 | + "<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."; |
56 | 65 |
|
57 | | - instance.setCustomOverlayItem(overlay); |
| 66 | + // Then we return a NutrientViewer.CustomOverlayItem which uses the overlayElement |
| 67 | + // that we created above as a node, the pageIndex we get from our onPress |
| 68 | + // event and define the position on the page. |
| 69 | + return new nutrientViewer.CustomOverlayItem({ |
| 70 | + id: "overlay-item-first-page", |
| 71 | + node: overlayElement, |
| 72 | + pageIndex: 0, |
| 73 | + position: new nutrientViewer.Geometry.Point({ x: 300, y: 50 }), |
58 | 74 | }); |
| 75 | +} |
59 | 76 |
|
60 | | - return instance; |
| 77 | +function getOverlayItemForPage2(nutrientViewer) { |
| 78 | + const overlayElement = document.createElement("div"); |
| 79 | + |
| 80 | + // In this case we embed a video to the page |
| 81 | + overlayElement.innerHTML = |
| 82 | + '<iframe src="https://player.vimeo.com/video/227250697" width="500" height="360" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>'; |
| 83 | + |
| 84 | + // Then we return a NutrientViewer.CustomOverlayItem which uses the overlayElement |
| 85 | + // that we created above as a node, the pageIndex we get from our onPress |
| 86 | + // event and define the position on the page. |
| 87 | + return new nutrientViewer.CustomOverlayItem({ |
| 88 | + id: "overlay-item-second-page", |
| 89 | + node: overlayElement, |
| 90 | + pageIndex: 1, |
| 91 | + position: new nutrientViewer.Geometry.Point({ x: 55, y: 220 }), |
| 92 | + }); |
61 | 93 | } |
62 | 94 |
|
63 | 95 | /** |
64 | | - * Unload the viewer from a container |
65 | | - * @param nutrientViewer - The NutrientViewer object |
| 96 | + * Unload the custom overlays viewer from a container |
| 97 | + * @param nutrientViewer - The NutrientViewer object (from CDN) |
66 | 98 | * @param container - The container element |
67 | 99 | */ |
68 | | -export function unloadCustomOverlaysViewer(nutrientViewer, container) { |
69 | | - nutrientViewer.unload(container); |
| 100 | +export async function unloadCustomOverlaysViewer(nutrientViewer, container) { |
| 101 | + if (nutrientViewer) { |
| 102 | + nutrientViewer.unload(container); |
| 103 | + } |
70 | 104 | } |
0 commit comments