lk-text-select-highlight 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +35 -15
- package/package.json +6 -5
- package/types/index.d.ts +2 -2
package/index.js
CHANGED
|
@@ -20,6 +20,18 @@ class TextHighlighter {
|
|
|
20
20
|
this.highlights = [];
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Generate a simple UUID
|
|
25
|
+
*/
|
|
26
|
+
generateUUID() {
|
|
27
|
+
return (
|
|
28
|
+
"highlight-" +
|
|
29
|
+
Date.now() +
|
|
30
|
+
"-" +
|
|
31
|
+
Math.random().toString(36).substr(2, 9)
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
23
35
|
/**
|
|
24
36
|
* Highlight selected text
|
|
25
37
|
*/
|
|
@@ -54,10 +66,11 @@ class TextHighlighter {
|
|
|
54
66
|
}
|
|
55
67
|
|
|
56
68
|
try {
|
|
57
|
-
const
|
|
69
|
+
const uuid = this.generateUUID();
|
|
70
|
+
const highlightedElement = this.wrapRange(range, uuid);
|
|
58
71
|
|
|
59
72
|
this.highlights.push({
|
|
60
|
-
|
|
73
|
+
uuid: uuid,
|
|
61
74
|
text: selection.toString(),
|
|
62
75
|
});
|
|
63
76
|
|
|
@@ -114,14 +127,18 @@ class TextHighlighter {
|
|
|
114
127
|
/**
|
|
115
128
|
* Wrap a range with highlight element
|
|
116
129
|
* @param {Range} range - The range to wrap
|
|
130
|
+
* @param {string} uuid - Unique identifier for the highlight
|
|
117
131
|
*/
|
|
118
|
-
wrapRange(range) {
|
|
132
|
+
wrapRange(range, uuid) {
|
|
119
133
|
const highlightEl = document.createElement("span");
|
|
120
134
|
highlightEl.className = this.options.className;
|
|
121
135
|
highlightEl.style.backgroundColor = this.options.color;
|
|
122
136
|
highlightEl.style.padding = "0";
|
|
123
137
|
highlightEl.style.margin = "0";
|
|
124
138
|
|
|
139
|
+
// Add UUID as data attribute
|
|
140
|
+
highlightEl.setAttribute("data-uuid", uuid);
|
|
141
|
+
|
|
125
142
|
// Clone the range and insert the highlight element
|
|
126
143
|
range.surroundContents(highlightEl);
|
|
127
144
|
|
|
@@ -133,12 +150,12 @@ class TextHighlighter {
|
|
|
133
150
|
*/
|
|
134
151
|
removeAllHighlights() {
|
|
135
152
|
this.highlights.forEach((highlight) => {
|
|
136
|
-
|
|
153
|
+
const element = document.querySelector(
|
|
154
|
+
`[data-uuid="${highlight.uuid}"]`,
|
|
155
|
+
);
|
|
156
|
+
if (element && element.parentNode) {
|
|
137
157
|
const textNode = document.createTextNode(highlight.text);
|
|
138
|
-
|
|
139
|
-
textNode,
|
|
140
|
-
highlight.element,
|
|
141
|
-
);
|
|
158
|
+
element.parentNode.replaceChild(textNode, element);
|
|
142
159
|
}
|
|
143
160
|
});
|
|
144
161
|
|
|
@@ -149,23 +166,26 @@ class TextHighlighter {
|
|
|
149
166
|
* Get all highlighted elements
|
|
150
167
|
*/
|
|
151
168
|
getHighlights() {
|
|
169
|
+
// Return highlights with uuid and text
|
|
152
170
|
return [...this.highlights];
|
|
153
171
|
}
|
|
154
172
|
|
|
155
173
|
/**
|
|
156
174
|
* Remove a specific highlight
|
|
157
|
-
* @param {Object} highlight - The highlight object to remove
|
|
175
|
+
* @param {Object} highlight - The highlight object to remove (containing uuid)
|
|
158
176
|
*/
|
|
159
177
|
removeHighlight(highlight) {
|
|
160
|
-
const index = this.highlights.
|
|
178
|
+
const index = this.highlights.findIndex(
|
|
179
|
+
(h) => h.uuid === highlight.uuid,
|
|
180
|
+
);
|
|
161
181
|
if (index !== -1) {
|
|
162
182
|
// Restore the original text
|
|
163
|
-
|
|
183
|
+
const element = document.querySelector(
|
|
184
|
+
`[data-uuid="${highlight.uuid}"]`,
|
|
185
|
+
);
|
|
186
|
+
if (element && element.parentNode) {
|
|
164
187
|
const textNode = document.createTextNode(highlight.text);
|
|
165
|
-
|
|
166
|
-
textNode,
|
|
167
|
-
highlight.element,
|
|
168
|
-
);
|
|
188
|
+
element.parentNode.replaceChild(textNode, element);
|
|
169
189
|
}
|
|
170
190
|
|
|
171
191
|
// Remove from the highlights array
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lk-text-select-highlight",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "A lightweight JavaScript library for highlighting selected text on web pages",
|
|
5
5
|
"main": "dist/lk-text-select-highlight.cjs.js",
|
|
6
6
|
"module": "dist/lk-text-select-highlight.esm.js",
|
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
".": {
|
|
14
14
|
"import": "./dist/lk-text-select-highlight.esm.js",
|
|
15
15
|
"require": "./dist/lk-text-select-highlight.cjs.js",
|
|
16
|
-
"umd": "./dist/lk-text-select-highlight.umd.js"
|
|
16
|
+
"umd": "./dist/lk-text-select-highlight.umd.js",
|
|
17
|
+
"types": "./types/index.d.ts"
|
|
17
18
|
}
|
|
18
19
|
},
|
|
19
20
|
"scripts": {
|
|
@@ -33,12 +34,12 @@
|
|
|
33
34
|
"license": "MIT",
|
|
34
35
|
"repository": {
|
|
35
36
|
"type": "git",
|
|
36
|
-
"url": "https://
|
|
37
|
+
"url": "https://gitee.com/liu_k41/lk-text-select-highlight.git"
|
|
37
38
|
},
|
|
38
39
|
"bugs": {
|
|
39
|
-
"url": "https://
|
|
40
|
+
"url": "https://gitee.com/liu_k41/lk-text-select-highlight/issues"
|
|
40
41
|
},
|
|
41
|
-
"homepage": "https://
|
|
42
|
+
"homepage": "https://gitee.com/liu_k41/lk-text-select-highlight#readme",
|
|
42
43
|
"devDependencies": {
|
|
43
44
|
"@rollup/plugin-commonjs": "^25.0.7",
|
|
44
45
|
"@rollup/plugin-node-resolve": "^15.2.3",
|