lk-text-select-highlight 1.0.6 → 1.0.7

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.
Files changed (4) hide show
  1. package/README-cn.md +15 -21
  2. package/README.md +15 -25
  3. package/index.js +30 -0
  4. package/package.json +1 -1
package/README-cn.md CHANGED
@@ -43,33 +43,27 @@ document.addEventListener('mouseup', () => {
43
43
 
44
44
  ## API
45
45
 
46
- ### new TextHighlighter(options)
46
+ ### 构造函数
47
47
 
48
- 使用以下选项初始化新的 TextHighlighter 实例:
48
+ `new TextHighlighter(options)`
49
49
 
50
- - `className`: 高亮元素的CSS类名 (默认: 'highlighted-text')
51
- - `color`: 高亮背景颜色 (默认: '#ffff00')
52
- - `container`: 限制文本选择高亮的DOM元素 (默认: document.body)
53
- - `strictMode`: 如果为true,则防止在包含目标className元素的选择上进行高亮 (默认: false)
54
- - `caseSensitive`: 搜索是否区分大小写 (默认: true)
50
+ 创建一个新的 TextHighlighter 实例。
55
51
 
56
- ### highlighter.highlightSelection()
52
+ #### 选项
57
53
 
58
- 高亮页面上当前选中的文本。
54
+ - `className`: 高亮元素的CSS类名 (默认值: `"highlighted-text"`)
55
+ - `color`: 高亮背景色 (默认值: `"#ffff00"`)
56
+ - `container`: 限制文本选择/高亮的DOM元素 (默认值: `document.body`)
57
+ - `strictMode`: 防止高亮包含目标className元素的选择 (默认值: `false`)
58
+ - `caseSensitive`: 搜索是否区分大小写 (默认值: `true`)
59
59
 
60
- ### highlighter.removeAllHighlights()
60
+ ### 方法
61
61
 
62
- 移除所有现有的高亮。
63
-
64
- ### highlighter.getHighlights()
65
-
66
- 返回所有当前高亮的数组。每个高亮对象包含:
67
- - `uuid`: 高亮元素的唯一标识符
68
- - `text`: 高亮的文本内容
69
-
70
- ### highlighter.removeHighlight(highlight)
71
-
72
- 移除指定的高亮对象。高亮对象可以从 getHighlights() 方法获取。该方法使用UUID来定位并移除相应的高亮元素。
62
+ - `highlightSelection()`: 高亮当前选中的文本
63
+ - `removeAllHighlights()`: 移除所有高亮
64
+ - `removeHighlight(highlight)`: 移除特定高亮
65
+ - `getHighlights()`: 获取所有高亮对象
66
+ - `setHighlights(highlights)`: 从高亮对象数组设置高亮数据
73
67
 
74
68
  ## 配置选项详解
75
69
 
package/README.md CHANGED
@@ -43,37 +43,27 @@ document.addEventListener('mouseup', () => {
43
43
 
44
44
  ## API
45
45
 
46
- ### new TextHighlighter(options)
46
+ ### Constructor
47
47
 
48
- Initialize a new TextHighlighter instance with the following options:
48
+ `new TextHighlighter(options)`
49
49
 
50
- - `className`: CSS class name for highlighted elements (default: 'highlighted-text')
51
- - `color`: Background color for highlights (default: '#ffff00')
52
- - `container`: DOM element that limits where text selection highlighting occurs (default: document.body)
53
- - `strictMode`: If true, prevents highlighting selections that contain elements with the target className (default: false)
54
- - `caseSensitive`: Whether search should be case sensitive (default: true)
50
+ Creates a new TextHighlighter instance.
55
51
 
56
- ### highlighter.highlightSelection()
52
+ #### Options
57
53
 
58
- Highlights the currently selected text on the page.
54
+ - `className`: CSS class name for highlighted elements (default: `"highlighted-text"`)
55
+ - `color`: Background color for highlights (default: `"#ffff00"`)
56
+ - `container`: DOM element to restrict text selection/highlighting (default: `document.body`)
57
+ - `strictMode`: Prevents highlighting selections that contain elements with the target className (default: `false`)
58
+ - `caseSensitive`: Whether search is case-sensitive (default: `true`)
59
59
 
60
- ### highlighter.removeAllHighlights()
60
+ ### Methods
61
61
 
62
- Removes all existing highlights.
63
-
64
- ### highlighter.getHighlights()
65
-
66
- Returns an array of all current highlights. Each highlight object contains:
67
- - `uuid`: Unique identifier for the highlight element
68
- - `text`: The highlighted text content
69
-
70
- ### highlighter.removeHighlight(highlight)
71
-
72
- Removes a specific highlight object. The highlight object can be obtained from the getHighlights() method. The method uses the UUID to locate and remove the corresponding highlight element.
73
-
74
- ### highlighter.removeAllHighlights()
75
-
76
- Removes all existing highlights.
62
+ - `highlightSelection()`: Highlights the currently selected text
63
+ - `removeAllHighlights()`: Removes all highlights
64
+ - `removeHighlight(highlight)`: Removes a specific highlight
65
+ - `getHighlights()`: Gets all highlight objects
66
+ - `setHighlights(highlights)`: Sets highlight data from an array of highlight objects
77
67
 
78
68
  ## Testing
79
69
 
package/index.js CHANGED
@@ -195,6 +195,36 @@ class TextHighlighter {
195
195
  }
196
196
  return false;
197
197
  }
198
+
199
+ /**
200
+ * Set highlights data
201
+ * @param {Array} highlights - Array of highlight objects with uuid and text properties
202
+ */
203
+ setHighlights(highlights) {
204
+ // Validate the input to ensure it's an array of objects with required properties
205
+ if (!Array.isArray(highlights)) {
206
+ throw new Error("Highlights must be an array");
207
+ }
208
+
209
+ // Validate each highlight object
210
+ for (const highlight of highlights) {
211
+ if (
212
+ typeof highlight !== "object" ||
213
+ !highlight.hasOwnProperty("uuid") ||
214
+ !highlight.hasOwnProperty("text")
215
+ ) {
216
+ throw new Error(
217
+ "Each highlight must be an object with uuid and text properties",
218
+ );
219
+ }
220
+ }
221
+
222
+ // Clear existing highlights
223
+ this.removeAllHighlights();
224
+
225
+ // Set the new highlights data
226
+ this.highlights = [...highlights];
227
+ }
198
228
  }
199
229
 
200
230
  // Export for both Node.js and browser environments
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lk-text-select-highlight",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
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",