Skip to content

Commit 5d8ec49

Browse files
committed
feat: add text selection toolbar custom button example
1 parent 01dfd27 commit 5d8ec49

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import type {
2+
InlineTextSelectionToolbarItem,
3+
Instance,
4+
Rect,
5+
} from "@nutrient-sdk/viewer";
6+
import { baseOptions } from "../../shared/base-options";
7+
8+
window.NutrientViewer.load({
9+
...baseOptions,
10+
}).then((instance: Instance) => {
11+
const buttonNode = document.createElement("div");
12+
buttonNode.innerHTML = `
13+
<button
14+
type="button"
15+
title="Link"
16+
aria-label="Link"
17+
aria-pressed="false"
18+
style="background: none; border: none; cursor: pointer; padding: 4px;">
19+
<svg width="25" height="24" viewBox="0 0 25 24" fill="none" xmlns="http://www.w3.org/2000/svg">
20+
<path d="M14.1219 10.59C14.4482 10.9143 14.7071 11.2999 14.8837 11.7247C15.0604 12.1494 15.1513 12.6049 15.1513 13.065C15.1513 13.525 15.0604 13.9805 14.8837 14.4052C14.7071 14.83 14.4482 15.2156 14.1219 15.54L10.5919 19.07C10.2668 19.4038 9.87801 19.6691 9.44864 19.8503C9.01927 20.0315 8.55796 20.1248 8.09193 20.1248C7.6259 20.1248 7.16459 20.0315 6.73522 19.8503C6.30585 19.6691 5.91711 19.4038 5.59193 19.07C5.2581 18.7448 4.99277 18.356 4.81161 17.9267C4.63045 17.4973 4.53711 17.036 4.53711 16.57C4.53711 16.1039 4.63045 15.6426 4.81161 15.2133C4.99277 14.7839 5.2581 14.3951 5.59193 14.07L7.41193 12.35" stroke="currentColor" stroke-width="1.25" stroke-miterlimit="10" stroke-linecap="round"></path>
21+
<path d="M11.3019 13.4099C10.9756 13.0856 10.7167 12.7 10.5401 12.2752C10.3634 11.8505 10.2725 11.395 10.2725 10.9349C10.2725 10.4749 10.3634 10.0194 10.5401 9.59467C10.7167 9.16992 10.9756 8.78428 11.3019 8.45994L14.8319 4.92994C15.1571 4.59611 15.5458 4.33079 15.9752 4.14962C16.4045 3.96846 16.8658 3.87512 17.3319 3.87512C17.7979 3.87512 18.2592 3.96846 18.6886 4.14962C19.118 4.33079 19.5067 4.59611 19.8319 4.92994C20.1657 5.25512 20.431 5.64386 20.6122 6.07323C20.7934 6.50261 20.8867 6.96391 20.8867 7.42994C20.8867 7.89597 20.7934 8.35728 20.6122 8.78665C20.431 9.21603 20.1657 9.60477 19.8319 9.92994L18.0119 11.6499" stroke="currentColor" stroke-width="1.25" stroke-miterlimit="10" stroke-linecap="round"></path>
22+
</svg>
23+
</button>`;
24+
25+
const highlightWithLink: InlineTextSelectionToolbarItem = {
26+
type: "custom",
27+
id: "highlight-link",
28+
title: "Link",
29+
node: buttonNode,
30+
onPress: async () => {
31+
const selectedText = instance.getTextSelection();
32+
if (!selectedText) return;
33+
34+
const rectsPerPage = await selectedText.getSelectedRectsPerPage();
35+
const firstPageSelection = rectsPerPage.get(0);
36+
if (!firstPageSelection) return;
37+
38+
const { rects, pageIndex } = firstPageSelection;
39+
40+
const highlightRects = rects.map((rect: Rect) => {
41+
const adjustedRect = rect.translateY(-5);
42+
return adjustedRect
43+
.set("height", rect.height - 10)
44+
.set("top", rect.top + 8);
45+
});
46+
47+
const underlineRects = rects.map((rect: Rect) => {
48+
return rect.set("height", rect.height);
49+
});
50+
51+
const highlight =
52+
new window.NutrientViewer.Annotations.HighlightAnnotation({
53+
pageIndex: pageIndex,
54+
rects: highlightRects,
55+
boundingBox: window.NutrientViewer.Geometry.Rect.union(rects),
56+
color: window.NutrientViewer.Color.YELLOW,
57+
blendMode: "multiply",
58+
opacity: 0.5,
59+
});
60+
61+
const underline =
62+
new window.NutrientViewer.Annotations.UnderlineAnnotation({
63+
pageIndex: pageIndex,
64+
rects: underlineRects,
65+
boundingBox: window.NutrientViewer.Geometry.Rect.union(rects),
66+
color: window.NutrientViewer.Color.BLUE,
67+
blendMode: "multiply",
68+
opacity: 0.9,
69+
});
70+
71+
const linkAnnotation =
72+
new window.NutrientViewer.Annotations.LinkAnnotation({
73+
pageIndex: pageIndex,
74+
boundingBox: window.NutrientViewer.Geometry.Rect.union(rects),
75+
action: new window.NutrientViewer.Actions.URIAction({
76+
uri: "https://nutrient.io",
77+
}),
78+
borderColor: window.NutrientViewer.Color.TRANSPARENT,
79+
blendMode: "multiply",
80+
});
81+
82+
await instance.create([linkAnnotation, underline, highlight]);
83+
},
84+
};
85+
86+
instance.setInlineTextSelectionToolbarItems((toolbarOptions) => {
87+
const { defaultItems } = toolbarOptions;
88+
return [...defaultItems, highlightWithLink];
89+
});
90+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
category: toolbar-menus
3+
title: Text Selection Toolbar Custom Button
4+
description: Adds a custom link button to the text selection toolbar that creates a highlighted, underlined, and clickable link from selected text.
5+
keywords: [text selection, inline toolbar, link annotation, highlight, underline, custom button]
6+
---

0 commit comments

Comments
 (0)