| author | salimr <riwad.salim@yahoo.fr> |
| Tue, 09 Oct 2018 19:07:47 +0200 | |
| changeset 167 | 1f340f3597a8 |
| parent 157 | 5c3af4f10e92 |
| permissions | -rw-r--r-- |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
1 |
function AnnotationPlugin(options) { |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
2 |
|
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
3 |
const { onChange } = options |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
4 |
|
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
5 |
return { |
| 157 | 6 |
onSelect(event, change) { |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
7 |
event.preventDefault() |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
8 |
|
| 157 | 9 |
const { value } = change |
10 |
const { selection } = value |
|
11 |
const { start, end} = selection |
|
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
12 |
|
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
13 |
if (selection.isCollapsed) { |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
14 |
return; |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
15 |
} |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
16 |
|
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
17 |
let nodes = []; |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
18 |
let hasStarted = false; |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
19 |
let hasEnded = false; |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
20 |
|
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
21 |
// Keep only the relevant nodes, |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
22 |
// i.e. nodes which are contained within selection |
| 157 | 23 |
value.document.nodes.forEach((node) => { |
24 |
if (start.isInNode(node)) { |
|
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
25 |
hasStarted = true; |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
26 |
} |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
27 |
if (hasStarted && !hasEnded) { |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
28 |
nodes.push(node); |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
29 |
} |
| 157 | 30 |
if (end.isAtEndOfNode(node)) { |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
31 |
hasEnded = true; |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
32 |
} |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
33 |
}); |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
34 |
|
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
35 |
let text = ''; |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
36 |
|
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
37 |
// Concatenate the nodes text |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
38 |
if (nodes.length === 1) { |
| 157 | 39 |
text = nodes[0].text.substring(start.offset, end.offset); |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
40 |
} else { |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
41 |
text = nodes.map((node) => { |
| 157 | 42 |
if (start.isInNode(node)) return node.text.substring(start.offset); |
43 |
if (end.isAtEndOfNode(node)) return node.text.substring(0, end.offset); |
|
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
44 |
return node.text; |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
45 |
}).join('\n'); |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
46 |
} |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
47 |
|
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
48 |
if (onChange) { |
| 157 | 49 |
onChange(text, start.offset, end.offset); |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
50 |
} |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
51 |
} |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
52 |
|
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
53 |
}; |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
54 |
} |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
55 |
|
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
56 |
export default AnnotationPlugin; |