|
| 1 | +import type { Instance, ViewState } from "@nutrient-sdk/viewer"; |
| 2 | +import { baseOptions } from "../../shared/base-options"; |
| 3 | + |
| 4 | +window.NutrientViewer.load({ |
| 5 | + ...baseOptions, |
| 6 | + theme: window.NutrientViewer.Theme.DARK, |
| 7 | + toolbarItems: [ |
| 8 | + ...window.NutrientViewer.defaultToolbarItems, |
| 9 | + { type: "content-editor" }, |
| 10 | + ], |
| 11 | +}).then((instance: Instance) => { |
| 12 | + let isContentEditorActive = false; |
| 13 | + let clickListenerAdded = false; |
| 14 | + |
| 15 | + // Function to add button click detection |
| 16 | + function addButtonClickDetection() { |
| 17 | + if (!instance.contentDocument || clickListenerAdded) return; |
| 18 | + |
| 19 | + // Add event listener to detect button clicks in content editor |
| 20 | + instance.contentDocument.addEventListener( |
| 21 | + "click", |
| 22 | + (event: Event) => { |
| 23 | + if (!isContentEditorActive) return; |
| 24 | + |
| 25 | + const target = event.target as HTMLElement | null; |
| 26 | + if (!target) return; |
| 27 | + |
| 28 | + const button = target.closest("button"); |
| 29 | + if (!button) return; |
| 30 | + |
| 31 | + const buttonText = button.textContent || button.innerText || ""; |
| 32 | + |
| 33 | + // Check for Cancel button |
| 34 | + if (buttonText.trim() === "Cancel") { |
| 35 | + console.log("Cancel button clicked"); |
| 36 | + } |
| 37 | + |
| 38 | + // Check for Save & Close button |
| 39 | + if (buttonText.includes("Save") && buttonText.includes("Close")) { |
| 40 | + console.log("Save & Close button clicked"); |
| 41 | + } |
| 42 | + }, |
| 43 | + true, |
| 44 | + ); |
| 45 | + |
| 46 | + clickListenerAdded = true; |
| 47 | + } |
| 48 | + |
| 49 | + // Monitor view state changes |
| 50 | + instance.addEventListener( |
| 51 | + "viewState.change", |
| 52 | + (viewState: ViewState, previousViewState: ViewState) => { |
| 53 | + const currentMode = viewState.toJS().interactionMode; |
| 54 | + const previousMode = previousViewState.toJS().interactionMode; |
| 55 | + |
| 56 | + if (currentMode === "CONTENT_EDITOR") { |
| 57 | + console.log("Content Editor opened"); |
| 58 | + isContentEditorActive = true; |
| 59 | + |
| 60 | + // Add button click detection |
| 61 | + setTimeout(addButtonClickDetection, 500); |
| 62 | + } else if (previousMode === "CONTENT_EDITOR") { |
| 63 | + console.log("Content Editor closed"); |
| 64 | + isContentEditorActive = false; |
| 65 | + } |
| 66 | + }, |
| 67 | + ); |
| 68 | +}); |
0 commit comments