| author | ymh <ymh.work@gmail.com> |
| Tue, 13 Nov 2018 16:46:15 +0100 | |
| changeset 172 | 4b780ebbedc6 |
| parent 168 | ea92f4fe783d |
| 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 { |
| 172 | 6 |
onSelect(event, editor, next) { |
|
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 |
|
| 172 | 9 |
const { value } = editor |
| 157 | 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) { |
| 172 | 14 |
return next(); |
|
19
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 |
|
| 172 | 17 |
const nodes = []; |
|
19
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 |
// Concatenate the nodes text |
| 172 | 36 |
const text = nodes.map((node) => { |
37 |
let textStart = start.isInNode(node) ? start.offset : 0; |
|
38 |
let textEnd = end.isAtEndOfNode(node) ? end.offset : node.text.length; |
|
39 |
return node.text.substring(textStart,textEnd); |
|
40 |
}).join('\n'); |
|
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
41 |
|
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
42 |
if (onChange) { |
| 157 | 43 |
onChange(text, start.offset, end.offset); |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
44 |
} |
| 172 | 45 |
|
46 |
return next(); |
|
47 |
||
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
48 |
} |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff
changeset
|
49 |
|
|
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 |
export default AnnotationPlugin; |