@plannotator/web-highlighter 0.8.0

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 (88) hide show
  1. package/.cursor/environment.json +6 -0
  2. package/.eslintrc.js +250 -0
  3. package/.prettierrc +9 -0
  4. package/.travis.yml +17 -0
  5. package/CHANGELOG.md +220 -0
  6. package/LICENSE +21 -0
  7. package/README.md +371 -0
  8. package/README.zh_CN.md +367 -0
  9. package/config/base.config.js +25 -0
  10. package/config/base.example.config.js +38 -0
  11. package/config/paths.js +22 -0
  12. package/config/server.config.js +17 -0
  13. package/config/webpack.config.dev.js +18 -0
  14. package/config/webpack.config.example.js +20 -0
  15. package/config/webpack.config.prod.js +28 -0
  16. package/dist/data/cache.d.ts +13 -0
  17. package/dist/index.d.ts +58 -0
  18. package/dist/model/range/dom.d.ts +6 -0
  19. package/dist/model/range/index.d.ts +20 -0
  20. package/dist/model/range/selection.d.ts +14 -0
  21. package/dist/model/source/dom.d.ts +23 -0
  22. package/dist/model/source/index.d.ts +18 -0
  23. package/dist/painter/dom.d.ts +22 -0
  24. package/dist/painter/index.d.ts +19 -0
  25. package/dist/painter/style.d.ts +1 -0
  26. package/dist/types/index.d.ts +102 -0
  27. package/dist/util/camel.d.ts +5 -0
  28. package/dist/util/const.d.ts +41 -0
  29. package/dist/util/deferred.d.ts +9 -0
  30. package/dist/util/dom.d.ts +32 -0
  31. package/dist/util/event.emitter.d.ts +13 -0
  32. package/dist/util/hook.d.ts +15 -0
  33. package/dist/util/interaction.d.ts +6 -0
  34. package/dist/util/is.mobile.d.ts +5 -0
  35. package/dist/util/tool.d.ts +4 -0
  36. package/dist/util/uuid.d.ts +4 -0
  37. package/dist/web-highlighter.min.js +3 -0
  38. package/dist/web-highlighter.min.js.map +1 -0
  39. package/docs/ADVANCE.md +113 -0
  40. package/docs/ADVANCE.zh_CN.md +111 -0
  41. package/docs/img/create-flow.jpg +0 -0
  42. package/docs/img/create-flow.zh_CN.jpg +0 -0
  43. package/docs/img/logo.png +0 -0
  44. package/docs/img/remove-flow.jpg +0 -0
  45. package/docs/img/remove-flow.zh_CN.jpg +0 -0
  46. package/docs/img/sample.gif +0 -0
  47. package/example/index.css +2 -0
  48. package/example/index.js +214 -0
  49. package/example/local.store.js +72 -0
  50. package/example/my.css +119 -0
  51. package/example/tpl.html +59 -0
  52. package/package.json +103 -0
  53. package/script/build.js +17 -0
  54. package/script/convet-md.js +25 -0
  55. package/script/dev.js +22 -0
  56. package/src/data/cache.ts +57 -0
  57. package/src/index.ts +285 -0
  58. package/src/model/range/dom.ts +94 -0
  59. package/src/model/range/index.ts +88 -0
  60. package/src/model/range/selection.ts +55 -0
  61. package/src/model/source/dom.ts +66 -0
  62. package/src/model/source/index.ts +54 -0
  63. package/src/painter/dom.ts +345 -0
  64. package/src/painter/index.ts +199 -0
  65. package/src/painter/style.ts +21 -0
  66. package/src/types/index.ts +118 -0
  67. package/src/util/camel.ts +6 -0
  68. package/src/util/const.ts +54 -0
  69. package/src/util/deferred.ts +37 -0
  70. package/src/util/dom.ts +155 -0
  71. package/src/util/event.emitter.ts +45 -0
  72. package/src/util/hook.ts +52 -0
  73. package/src/util/interaction.ts +20 -0
  74. package/src/util/is.mobile.ts +7 -0
  75. package/src/util/tool.ts +14 -0
  76. package/src/util/uuid.ts +10 -0
  77. package/test/api.spec.ts +555 -0
  78. package/test/event.spec.ts +284 -0
  79. package/test/fixtures/broken.json +32 -0
  80. package/test/fixtures/index.html +11 -0
  81. package/test/fixtures/source.json +47 -0
  82. package/test/hook.spec.ts +244 -0
  83. package/test/integrate.spec.ts +48 -0
  84. package/test/mobile.spec.ts +87 -0
  85. package/test/option.spec.ts +212 -0
  86. package/test/util.spec.ts +244 -0
  87. package/test-newlines.html +226 -0
  88. package/tsconfig.json +23 -0
package/README.md ADDED
@@ -0,0 +1,371 @@
1
+ <div>
2
+ <h1 align="center"><code>Web Highlighter</code>&nbsp;&nbsp;🖍️</h1>
3
+ <p align="center">
4
+ <strong>✨ A no-dependency lib for text highlighting & 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>
28
+
29
+ ---
30
+
31
+ English | [简体中文](https://github.com/alienzhou/web-highlighter/blob/master/README.zh_CN.md)
32
+
33
+ ## Background
34
+
35
+ It's from an idea: highlight texts on the website and save the highlighted areas just like what you do in PDF.
36
+
37
+ If you have ever visited [medium.com](http://medium.com), you must know the feature of highlighting notes: users select a text segment and click the 'highlight' button. Then the text will be highlighted with a shining background color. Besides, the highlighted areas will be saved and recovered when you visit it next time. It's like the simple demo bellow.
38
+
39
+ ![](https://raw.githubusercontent.com/alienzhou/web-highlighter/master/docs/img/sample.gif)
40
+
41
+ This is a useful feature for readers. If you're a developer, you may want your website support it and attract more visits. If you're a user (like me), you may want a browser-plugin to do this.
42
+
43
+ For this reason, the repo (web-highlighter) aims to help you implement highlighting-note on any website quickly (e.g. blogs, document viewers, online books and so on). It contains the core abilities for note highlighting and persistence. And you can implement your own product by some easy-to-use APIs. It has been used for our sites in production.
44
+
45
+ ## Install
46
+
47
+ ```bash
48
+ npm i web-highlighter
49
+ ```
50
+
51
+ ## Usage
52
+
53
+ Only two lines, highlighted when texts are selected.
54
+
55
+ ```JavaScript
56
+ import Highlighter from 'web-highlighter';
57
+ (new Highlighter()).run();
58
+ ```
59
+
60
+ If you need persistence, four lines make it.
61
+
62
+ ```JavaScript
63
+ import Highlighter from 'web-highlighter';
64
+
65
+ // 1. initialize
66
+ const highlighter = new Highlighter();
67
+
68
+ // 2. retrieve data from backend, then highlight it on the page
69
+ getRemoteData().then(s => highlighter.fromStore(s.startMeta, s.endMeta, s.id, s.text));
70
+
71
+ // 3. listen for highlight creating, then save to backend
72
+ highlighter.on(Highlighter.event.CREATE, ({sources}) => save(sources));
73
+
74
+ // 4. auto highlight
75
+ highlighter.run();
76
+ ```
77
+
78
+ ## Example
79
+
80
+ A more complex example
81
+
82
+ ```JavaScript
83
+ import Highlighter from 'web-highlighter';
84
+
85
+ // won't highlight pre&code elements
86
+ const highlighter = new Highlighter({
87
+ exceptSelectors: ['pre', 'code']
88
+ });
89
+
90
+ // add some listeners to handle interaction, such as hover
91
+ highlighter
92
+ .on('selection:hover', ({id}) => {
93
+ // display different bg color when hover
94
+ highlighter.addClass('highlight-wrap-hover', id);
95
+ })
96
+ .on('selection:hover-out', ({id}) => {
97
+ // remove the hover effect when leaving
98
+ highlighter.removeClass('highlight-wrap-hover', id);
99
+ })
100
+ .on('selection:create', ({sources}) => {
101
+ sources = sources.map(hs => ({hs}));
102
+ // save to backend
103
+ store.save(sources);
104
+ });
105
+
106
+ // retrieve data from store, and display highlights on the website
107
+ store.getAll().forEach(
108
+ // hs is the same data saved by 'store.save(sources)'
109
+ ({hs}) => highlighter.fromStore(hs.startMeta, hs.endMeta, hs.text, hs.id)
110
+ );
111
+
112
+ // auto-highlight selections
113
+ highlighter.run()
114
+ ```
115
+
116
+ Besides, there is an example in this repo (in `example` folder). To play with it, you just need ——
117
+
118
+ Firstly enter the repository and run
119
+
120
+ ```bash
121
+ npm i
122
+ ```
123
+
124
+ Then start the example
125
+
126
+ ```
127
+ npm start
128
+ ```
129
+
130
+ Finally visit http://127.0.0.1:8085/
131
+
132
+ ---
133
+
134
+ Another real product built with web-highlighter (for the highlighting area on the left):
135
+
136
+ ![product sample](https://user-images.githubusercontent.com/9822789/64678049-632e8500-d4ab-11e9-99d6-f960bc90d17b.gif)
137
+
138
+ ## How it works
139
+
140
+ It will read the selected range by [`Selection API`](https://caniuse.com/#search=selection%20api). Then the information of the range will be converted to a serializable data structure so that it can be store in backend. When users visit your page next time, these data will be returned and deserialized in your page. The data structure is tech stack independent. So you can use on any 'static' pages made with React / Vue / Angular / jQuery and others.
141
+
142
+ For more details, please read [this article (in Chinese)](https://www.alienzhou.com/2019/04/21/web-note-highlight-in-js/).
143
+
144
+ ## APIs
145
+
146
+ ### 1. Options
147
+
148
+ ```JavaScript
149
+ const highlighter = new Highlighter([opts])
150
+ ```
151
+
152
+ Create a new `highlighter` instance.
153
+
154
+ `opts` will be merged into the default options (shown bellow).
155
+
156
+ ```JavaScript
157
+ {
158
+ $root: document.documentElement,
159
+ exceptSelectors: null,
160
+ wrapTag: 'span',
161
+ style: {
162
+ className: 'highlight-mengshou-wrap'
163
+ }
164
+ }
165
+ ```
166
+
167
+ All options:
168
+
169
+ | name | type | description | required | default |
170
+ |---|---|---|---|---|
171
+ | $root | `Document | HTMLElement` | the container to enable highlighting | No | `document` |
172
+ | exceptSelectors | `Array<string>` | if an element matches the selector, it won't be highlighted | No | `null` |
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` |
175
+ | style | `Object` | control highlighted areas style | No | details below |
176
+
177
+ `style` field options:
178
+
179
+ | name | type | description | required | default |
180
+ |---|---|---|---|---|
181
+ | className | `string` | the className for wrap element | No | `highlight-mengshou-wrap` |
182
+
183
+ `exceptSelectors` needs `null` or `Array<string>`. It supports id selectors, class selectors and tag selectors. For example, to skip h1 and `.title` elements:
184
+
185
+ ```JavaScript
186
+ var highlighter = new Highlighter({
187
+ exceptSelectors: ['h1', '.title']
188
+ });
189
+ ```
190
+
191
+ ### 2. 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
+ ### 3. Instance Methods
202
+
203
+ #### `highlighter.run()`
204
+
205
+ Start auto-highlighting. When the user select a text segment, a highlighting will be added to the text automatically.
206
+
207
+ #### `highlighter.stop()`
208
+
209
+ It will stop the auto-highlighting.
210
+
211
+ #### `highlighter.dispose()`
212
+
213
+ When you don't want the highlighter anymore, remember to call it first. It will remove some listeners and do some cleanup.
214
+
215
+ #### `highlighter.fromRange(range)`
216
+
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.
218
+
219
+ Use it as bellow:
220
+
221
+ ```JavaScript
222
+ const selection = window.getSelection();
223
+ if (!selection.isCollapsed) {
224
+ highlighter.fromRange(selection.getRangeAt(0));
225
+ }
226
+ ```
227
+
228
+ #### `highlighter.fromStore(start, end, text, id)`
229
+
230
+ Mostly, you use this api to highlight text by the persisted information stored from backend.
231
+
232
+ These four values are from the `HighlightSource` object. `HighlightSource` object is a special object created by web-highlighter when highlighted area created. For persistence in backend (database), it's necessary to find a data structure to represent a dom node. This structure is called `HighlightSource` in web-highlighter.
233
+
234
+ Four attributes' meanings:
235
+
236
+ - start `Object`: meta info about the beginning element
237
+ - end `Object`: meta info about then end element
238
+ - text `string`: text content
239
+ - id `string`: unique id
240
+
241
+ #### `highlighter.remove(id)`
242
+
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).
244
+
245
+ #### `highlighter.removeAll()`
246
+
247
+ Remove all highlighted areas belonging to the root.
248
+
249
+ #### `highlighter.addClass(className, id)`
250
+
251
+ Add a className for highlighted areas (wrap elements) by unique id. You can change a highlighted area's style by using this api.
252
+
253
+
254
+ #### `highlighter.removeClass(className, id)`
255
+
256
+ Remove the className by unique id. It's `highlighter.addClass`'s inverse operation.
257
+
258
+ #### `highlighter.getDoms([id])`
259
+
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.
261
+
262
+ If the `id` is not passed, it will return all the areas' wrap nodes.
263
+
264
+
265
+ #### `highlighter.getIdByDom(node)`
266
+
267
+ If you have a DOM node, it can return the unique highlight id for you. When passing a non-wrapper element, it will find the nearest ancestor wrapper node.
268
+
269
+ #### `highlighter.getExtraIdByDom(node)`
270
+
271
+ If you have a DOM node, it can return the extra unique highlight id for you. When passing a non-wrapper element, it will find the nearest ancestor wrapper node.
272
+
273
+ #### `highlighter.setOption(opt)`
274
+
275
+ You can use this API to change the highlighter's options. The parameters' structure is the same as the constructor's. You can pass partial options.
276
+
277
+ ### 4. Event Listener
278
+
279
+ web-highlighter use listeners to handle the events.
280
+
281
+ e.g.
282
+
283
+ ```JavaScript
284
+ var highlighter = new Highlighter();
285
+ highlighter.on(Highlighter.event.CREATE, function (data, inst, e) {
286
+ // ...
287
+ });
288
+ ```
289
+
290
+ The callback function will receive three parameters:
291
+
292
+ - data `any`: event data
293
+ - inst `Highlighter`: current Highlighter instance
294
+ - e `Event`: some event is triggered by the browser (such as click), web-highlighter will expose it
295
+
296
+ `Highlighter.event` is `EventType` type. It contains:
297
+
298
+ - `EventType.CLICK`: click the highlighted area
299
+ - `EventType.HOVER`: mouse enter the highlighted area
300
+ - `EventType.HOVER_OUT`: mouse leave the highlighted area
301
+ - `EventType.CREATE`: a highlighted area is created
302
+ - `EventType.REMOVE`: a highlighted area is removed
303
+
304
+
305
+ Different event has different `data`. Attributes below:
306
+
307
+ #### `EventType.CLICK`
308
+
309
+ |name|description|type|
310
+ |---|---|---|
311
+ |`id`|the highlight id|string|
312
+
313
+ #### `EventType.HOVER`
314
+
315
+ |name|description|type|
316
+ |---|---|---|
317
+ |`id`|the highlight id|string|
318
+
319
+ #### `EventType.HOVER_OUT`
320
+
321
+ |name|description|type|
322
+ |---|---|---|
323
+ |`id`|the highlight id|string|
324
+
325
+ #### `EventType.CREATE`
326
+
327
+ > no parameter `e`
328
+
329
+ |name|description|type|
330
+ |---|---|---|
331
+ |`source`|`HighlightSource` object|Array<HighlightSource>|
332
+ |`type`|the reason for creating|string|
333
+
334
+ `source` is a `HighlightSource` object. It is an object created by web-highlighter when highlighted area created. For persistence in backend (database), it's necessary to use a data structure which can be serialized (`JSON.stringify()`) to represent a dom node in browsers. `HighlightSource` is the data structure designed for this.
335
+
336
+ `type` explains why a highlighted area is be created. Now `type` has two possible values: `from-input` and `from-store`. `from-input` shows that a highlighted area is created because of user's selection. `from-store` means it from a storage.
337
+
338
+ #### `EventType.REMOVE`
339
+
340
+ > no parameter `e`
341
+
342
+ |name|description|type|
343
+ |---|---|---|
344
+ |`ids`|a list of the highlight id|Array<string>|
345
+
346
+ ### 5. Hooks
347
+
348
+ Hooks let you control the highlighting flow powerfully. You can almost customize any logic by hooks. See more in ['Advance' part](#Advance).
349
+
350
+ ## Compatibility
351
+
352
+ > It depends on [Selection API](https://caniuse.com/#search=selection%20api).
353
+
354
+ - IE 11
355
+ - Edge
356
+ - Firefox 52+
357
+ - Chrome 15+
358
+ - Safari 5.1+
359
+ - Opera 15+
360
+
361
+ _**Mobile supports:**_ automatically detect whether mobile devices and use touch events when on mobile devices.
362
+
363
+ ## Advance
364
+
365
+ It provides some hooks for you so that the highlighting behaviour can be controlled better by your own.
366
+
367
+ To learn more about the hooks, read [this doc](https://github.com/alienzhou/web-highlighter/blob/master/docs/ADVANCE.md).
368
+
369
+ ## License
370
+
371
+ [MIT](./LICENCE)