lk-text-select-highlight 1.0.1 → 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 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 highlightedElement = this.wrapRange(range);
69
+ const uuid = this.generateUUID();
70
+ const highlightedElement = this.wrapRange(range, uuid);
58
71
 
59
72
  this.highlights.push({
60
- element: highlightedElement,
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
- if (highlight.element.parentNode) {
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
- highlight.element.parentNode.replaceChild(
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.indexOf(highlight);
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
- if (highlight.element.parentNode) {
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
- highlight.element.parentNode.replaceChild(
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.1",
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",
package/types/index.d.ts CHANGED
@@ -31,8 +31,8 @@ declare interface TextHighlighterOptions {
31
31
  }
32
32
 
33
33
  declare interface HighlightObject {
34
- /** 高亮的DOM元素 */
35
- element: HTMLElement;
34
+ /** 高亮的唯一标识符 */
35
+ uuid: string;
36
36
  /** 高亮的文本内容 */
37
37
  text: string;
38
38
  }