|
| 1 | +import type { Instance, ViewState } from "@nutrient-sdk/viewer"; |
| 2 | +import { baseOptions } from "../../shared/base-options"; |
| 3 | + |
| 4 | +// Custom floating menu for Form Creator with quick access to all widget types |
| 5 | + |
| 6 | +window.NutrientViewer.load({ |
| 7 | + ...baseOptions, |
| 8 | + theme: window.NutrientViewer.Theme.DARK, |
| 9 | + toolbarItems: [ |
| 10 | + ...window.NutrientViewer.defaultToolbarItems, |
| 11 | + { type: "form-creator" }, |
| 12 | + ], |
| 13 | +}).then((instance: Instance) => { |
| 14 | + attachFormCreatorListener(instance); |
| 15 | +}); |
| 16 | + |
| 17 | +function attachFormCreatorListener(instance: Instance) { |
| 18 | + const doc = instance.contentDocument; |
| 19 | + const button = doc.querySelector( |
| 20 | + ".NutrientViewer-Toolbar-Button-Form-Creator", |
| 21 | + ); |
| 22 | + if (!button) return; |
| 23 | + |
| 24 | + const OVERLAY_ID = "form-creator-floating"; |
| 25 | + |
| 26 | + function renderFloating() { |
| 27 | + const old = document.getElementById(OVERLAY_ID); |
| 28 | + if (old) old.remove(); |
| 29 | + |
| 30 | + if (!button || button.getAttribute("aria-pressed") !== "true") return; |
| 31 | + |
| 32 | + const overlay = document.createElement("div"); |
| 33 | + overlay.id = OVERLAY_ID; |
| 34 | + overlay.style.cssText = |
| 35 | + "position:fixed;display:flex;flex-wrap:wrap;gap:4px;" + |
| 36 | + "background:rgba(0,0,0,0.7);padding:8px;border-radius:4px;" + |
| 37 | + "font-family:sans-serif;color:white;z-index:10000;"; |
| 38 | + |
| 39 | + const modes = [ |
| 40 | + { |
| 41 | + label: "Button", |
| 42 | + mode: window.NutrientViewer.InteractionMode.BUTTON_WIDGET, |
| 43 | + }, |
| 44 | + { |
| 45 | + label: "Text", |
| 46 | + mode: window.NutrientViewer.InteractionMode.TEXT_WIDGET, |
| 47 | + }, |
| 48 | + { |
| 49 | + label: "Checkbox", |
| 50 | + mode: window.NutrientViewer.InteractionMode.CHECKBOX_WIDGET, |
| 51 | + }, |
| 52 | + { |
| 53 | + label: "Radio", |
| 54 | + mode: window.NutrientViewer.InteractionMode.RADIO_BUTTON_WIDGET, |
| 55 | + }, |
| 56 | + { |
| 57 | + label: "Combo", |
| 58 | + mode: window.NutrientViewer.InteractionMode.COMBO_BOX_WIDGET, |
| 59 | + }, |
| 60 | + { |
| 61 | + label: "List", |
| 62 | + mode: window.NutrientViewer.InteractionMode.LIST_BOX_WIDGET, |
| 63 | + }, |
| 64 | + { |
| 65 | + label: "Signature", |
| 66 | + mode: window.NutrientViewer.InteractionMode.SIGNATURE_WIDGET, |
| 67 | + }, |
| 68 | + { |
| 69 | + label: "Date", |
| 70 | + mode: window.NutrientViewer.InteractionMode.DATE_WIDGET, |
| 71 | + }, |
| 72 | + ]; |
| 73 | + |
| 74 | + modes.forEach((item) => { |
| 75 | + const btn = document.createElement("button"); |
| 76 | + btn.textContent = item.label; |
| 77 | + btn.style.padding = "4px 8px"; |
| 78 | + btn.addEventListener("click", () => { |
| 79 | + instance.setViewState((vs: ViewState) => |
| 80 | + vs.set("interactionMode", item.mode), |
| 81 | + ); |
| 82 | + }); |
| 83 | + overlay.appendChild(btn); |
| 84 | + }); |
| 85 | + |
| 86 | + const toolbar = doc.querySelector(".NutrientViewer-Toolbar"); |
| 87 | + if (!toolbar) return; |
| 88 | + const r = toolbar.getBoundingClientRect(); |
| 89 | + overlay.style.top = r.bottom + window.scrollY + "px"; |
| 90 | + overlay.style.left = r.left + window.scrollX + "px"; |
| 91 | + |
| 92 | + document.body.appendChild(overlay); |
| 93 | + } |
| 94 | + |
| 95 | + button.addEventListener("click", () => { |
| 96 | + setTimeout(renderFloating, 0); |
| 97 | + }); |
| 98 | + instance.addEventListener("viewState.currentPageIndex.change", () => { |
| 99 | + setTimeout(renderFloating, 0); |
| 100 | + }); |
| 101 | +} |
0 commit comments