You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
English | [简体中文](https://github.com/alienzhou/web-highlighter/blob/master/README.zh_CN.md)
8
32
@@ -117,9 +141,13 @@ It will read the selected range by [`Selection API`](https://caniuse.com/#search
117
141
118
142
For more details, please read [this article (in Chinese)](https://www.alienzhou.com/2019/04/21/web-note-highlight-in-js/).
119
143
120
-
## API
144
+
## APIs
121
145
122
-
### `highlighter = new Highlighter([opts])`
146
+
### Options
147
+
148
+
```JavaScript
149
+
consthighlighter=newHighlighter([opts])
150
+
```
123
151
124
152
Create a new `highlighter` instance.
125
153
@@ -143,6 +171,7 @@ All options:
143
171
| $root | `Document | HTMLElement` | the container to enable highlighting | No |`document`|
144
172
| exceptSelectors |`Array<string>`| if an element matches the selector, it won't be highlighted | No |`null`|
145
173
| wrapTag |`string`| the html tag used to wrap highlighted texts | No |`span`|
174
+
| verbose |`boolean`| dose it need to output (print) some warning and error message | No |`false`|
146
175
| style |`Object`| control highlighted areas style | No | details below |
147
176
148
177
`style` field options:
@@ -159,19 +188,31 @@ var highlighter = new Highlighter({
159
188
});
160
189
```
161
190
162
-
### `highlighter.run()`
191
+
### Static Methods
192
+
193
+
#### `Highlighter.isHighlightSource(source)`
194
+
195
+
If the `source` is a highlight source object, it will return `true`, vice verse.
196
+
197
+
#### `Highlighter.isHighlightWrapNode($node)`
198
+
199
+
If the `$node` is a highlight wrapper dom node, it will return `true`, vice verse.
200
+
201
+
### Instance Methods
202
+
203
+
#### `highlighter.run()`
163
204
164
205
Start auto-highlighting. When the user select a text segment, a highlighting will be added to the text automatically.
165
206
166
-
### `highlighter.stop()`
207
+
####`highlighter.stop()`
167
208
168
209
It will stop the auto-highlighting.
169
210
170
-
### `highlighter.dispose()`
211
+
####`highlighter.dispose()`
171
212
172
213
When you don't want the highlighter anymore, remember to call it first. It will remove some listeners and do some cleanup.
173
214
174
-
### `highlighter.fromRange(range)`
215
+
####`highlighter.fromRange(range)`
175
216
176
217
You can pass a [`Range`](https://developer.mozilla.org/en-US/docs/Web/API/Range) object to it and then it will be highlighted. You can use `window.getSelection().getRangeAt(0)` to get a range object or use `document.createRange()` to create a new range.
177
218
@@ -184,7 +225,7 @@ if (selection.isCollapsed) {
184
225
}
185
226
```
186
227
187
-
### `highlighter.fromStore(start, end, text, id)`
228
+
####`highlighter.fromStore(start, end, text, id)`
188
229
189
230
Mostly, you use this api to highlight text by the persisted information stored from backend.
190
231
@@ -197,31 +238,31 @@ Four attributes' meanings:
197
238
- text `string`: text content
198
239
- id `string`: unique id
199
240
200
-
### `highlighter.remove(id)`
241
+
####`highlighter.remove(id)`
201
242
202
243
Remove (clean) a highlighted area by it's unique id. The id will be generated by web-highlighter by default. You can also add a hook for your own rule. [Hooks doc here](https://github.com/alienzhou/web-highlighter/blob/master/docs/ADVANCE.md).
203
244
204
-
### `highlighter.removeAll()`
245
+
####`highlighter.removeAll()`
205
246
206
247
Remove all highlighted areas belonging to the root.
207
248
208
-
### `highlighter.addClass(className, id)`
249
+
####`highlighter.addClass(className, id)`
209
250
210
251
Add a className for highlighted areas (wrap elements) by unique id. You can change a highlighted area's style by using this api.
211
252
212
253
213
-
### `highlighter.removeClass(className, id)`
254
+
####`highlighter.removeClass(className, id)`
214
255
215
256
Remove the className by unique id. It's `highlighter.addClass`'s inverse operation.
216
257
217
-
### `highlighter.getDoms([id])`
258
+
####`highlighter.getDoms([id])`
218
259
219
260
Get all the wrap nodes in a highlighted area. A highlighted area may contain many segments. It will return all the dom nodes wrapping these segments.
220
261
221
262
If the `id` is not passed, it will return all the areas' wrap nodes.
222
263
223
264
224
-
### `highlighter.getIdByDom(node)`
265
+
####`highlighter.getIdByDom(node)`
225
266
226
267
If you have a wrap node, it can return the unique highlight id for you.
227
268
@@ -294,6 +335,10 @@ Different event has different `data`. Attributes below:
294
335
|---|---|---|
295
336
|`ids`|a list of the highlight id|Array<string>|
296
337
338
+
### Hooks
339
+
340
+
Hooks let you control the highlighting flow powerfully. You can almost customize any logic by hooks. See more in ['Advance' part](#Advance).
341
+
297
342
## Compatibility
298
343
299
344
> It depends on [Selection API](https://caniuse.com/#search=selection%20api).
0 commit comments