Skip to content

Commit b9549f3

Browse files
authored
Content Editor example (#167)
1 parent a3ed23f commit b9549f3

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
category: editor
3+
title: Detect Content Editor Button Clicks
4+
description: Monitor when the content editor is opened or closed, and detect clicks on the Cancel and Save & Close buttons using view state changes and DOM event listeners.
5+
keywords: [content editor, interaction mode, viewState, button detection, event listener, CONTENT_EDITOR, toolbar]
6+
---

0 commit comments

Comments
 (0)