Skip to content

Commit 6a006fb

Browse files
authored
Merge pull request #40 from alienzhou/test
add unit tests and fix some bugs
2 parents d910c4c + 261d81f commit 6a006fb

26 files changed

+1634
-110
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ example/index.html
55
dist
66
docs/TODO.md
77
package-lock.json
8-
typings
8+
typings
9+
.nyc_output
10+
coverage

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ install:
1111
- npm install
1212

1313
script:
14-
- npm run build
14+
- npm run build
15+
- npm run coverage
16+
17+
after_script: "cat coverage/lcov.info | node_modules/coveralls/bin/coveralls.js"

README.md

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,32 @@
1-
![Web Highlighter](https://raw.githubusercontent.com/alienzhou/web-highlighter/master/docs/img/logo.png)
1+
<div>
2+
<h1 align="center"><code>Web Highlighter</code>&nbsp;&nbsp;🖍️</h1>
3+
<p align="center">
4+
<strong>✨A no-runtime dependency lib for highlighting-note & persistence on any website ✨🖍️</strong>
5+
</p>
6+
<img src="https://raw.githubusercontent.com/alienzhou/web-highlighter/master/docs/img/logo.png">
7+
<p align="center">
8+
<a href="https://travis-ci.org/alienzhou/web-highlighter" target="_blank">
9+
<img src="https://api.travis-ci.org/alienzhou/web-highlighter.svg?branch=master" alt="Build status" />
10+
</a>
11+
<a href="https://www.npmjs.com/package/web-highlighter" target="_blank">
12+
<img src="https://img.shields.io/npm/v/web-highlighter.svg" alt="NPM version" />
13+
</a>
14+
<a href='https://coveralls.io/github/alienzhou/web-highlighter?branch=master'>
15+
<img src='https://coveralls.io/repos/github/alienzhou/web-highlighter/badge.svg?branch=master' alt='Coverage Status' />
16+
</a>
17+
<a href="https://unpkg.com/web-highlighter" target="_blank">
18+
<img src="https://img.badgesize.io/https://unpkg.com/web-highlighter/dist/web-highlighter.min.js?compression=gzip" alt="Gzip size" />
19+
</a>
20+
<a href="https://codebeat.co/projects/github-com-alienzhou-web-highlighter-master" target="_blank">
21+
<img src="https://codebeat.co/badges/f5a18a9b-9765-420e-a17f-fa0b54b3a125" alt="Codebeat" />
22+
</a>
23+
<a href="https://opensource.org/licenses/mit-license.php" target="_blank">
24+
<img src="https://img.shields.io/github/license/alienzhou/web-highlighter" alt="MIT Licence" />
25+
</a>
26+
</p>
27+
</div>
228

3-
✨A no-runtime dependency lib for highlighting-note & persistence on any website ✨🖍️
4-
5-
[![NPM version](https://img.shields.io/npm/v/web-highlighter.svg)](https://www.npmjs.com/package/web-highlighter) [![](https://api.travis-ci.org/alienzhou/web-highlighter.svg?branch=master)](https://travis-ci.org/alienzhou/web-highlighter) [![gzip size](https://img.badgesize.io/https://unpkg.com/web-highlighter/dist/web-highlighter.min.js?compression=gzip)](https://unpkg.com/web-highlighter) [![codebeat badge](https://codebeat.co/badges/f5a18a9b-9765-420e-a17f-fa0b54b3a125)](https://codebeat.co/projects/github-com-alienzhou-web-highlighter-master) [![install size](https://packagephobia.now.sh/badge?p=web-highlighter)](https://packagephobia.now.sh/result?p=web-highlighter) [![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php)
29+
---
630

731
English | [简体中文](https://github.com/alienzhou/web-highlighter/blob/master/README.zh_CN.md)
832

@@ -117,9 +141,13 @@ It will read the selected range by [`Selection API`](https://caniuse.com/#search
117141

118142
For more details, please read [this article (in Chinese)](https://www.alienzhou.com/2019/04/21/web-note-highlight-in-js/).
119143

120-
## API
144+
## APIs
121145

122-
### `highlighter = new Highlighter([opts])`
146+
### Options
147+
148+
```JavaScript
149+
const highlighter = new Highlighter([opts])
150+
```
123151

124152
Create a new `highlighter` instance.
125153

@@ -143,6 +171,7 @@ All options:
143171
| $root | `Document | HTMLElement` | the container to enable highlighting | No | `document` |
144172
| exceptSelectors | `Array<string>` | if an element matches the selector, it won't be highlighted | No | `null` |
145173
| 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` |
146175
| style | `Object` | control highlighted areas style | No | details below |
147176

148177
`style` field options:
@@ -159,19 +188,31 @@ var highlighter = new Highlighter({
159188
});
160189
```
161190

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()`
163204

164205
Start auto-highlighting. When the user select a text segment, a highlighting will be added to the text automatically.
165206

166-
### `highlighter.stop()`
207+
#### `highlighter.stop()`
167208

168209
It will stop the auto-highlighting.
169210

170-
### `highlighter.dispose()`
211+
#### `highlighter.dispose()`
171212

172213
When you don't want the highlighter anymore, remember to call it first. It will remove some listeners and do some cleanup.
173214

174-
### `highlighter.fromRange(range)`
215+
#### `highlighter.fromRange(range)`
175216

176217
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.
177218

@@ -184,7 +225,7 @@ if (selection.isCollapsed) {
184225
}
185226
```
186227

187-
### `highlighter.fromStore(start, end, text, id)`
228+
#### `highlighter.fromStore(start, end, text, id)`
188229

189230
Mostly, you use this api to highlight text by the persisted information stored from backend.
190231

@@ -197,31 +238,31 @@ Four attributes' meanings:
197238
- text `string`: text content
198239
- id `string`: unique id
199240

200-
### `highlighter.remove(id)`
241+
#### `highlighter.remove(id)`
201242

202243
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).
203244

204-
### `highlighter.removeAll()`
245+
#### `highlighter.removeAll()`
205246

206247
Remove all highlighted areas belonging to the root.
207248

208-
### `highlighter.addClass(className, id)`
249+
#### `highlighter.addClass(className, id)`
209250

210251
Add a className for highlighted areas (wrap elements) by unique id. You can change a highlighted area's style by using this api.
211252

212253

213-
### `highlighter.removeClass(className, id)`
254+
#### `highlighter.removeClass(className, id)`
214255

215256
Remove the className by unique id. It's `highlighter.addClass`'s inverse operation.
216257

217-
### `highlighter.getDoms([id])`
258+
#### `highlighter.getDoms([id])`
218259

219260
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.
220261

221262
If the `id` is not passed, it will return all the areas' wrap nodes.
222263

223264

224-
### `highlighter.getIdByDom(node)`
265+
#### `highlighter.getIdByDom(node)`
225266

226267
If you have a wrap node, it can return the unique highlight id for you.
227268

@@ -294,6 +335,10 @@ Different event has different `data`. Attributes below:
294335
|---|---|---|
295336
|`ids`|a list of the highlight id|Array<string>|
296337

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+
297342
## Compatibility
298343

299344
> It depends on [Selection API](https://caniuse.com/#search=selection%20api).

0 commit comments

Comments
 (0)