1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | var _require = require("./draftKeyUtils"),
|
14 | notEmptyKey = _require.notEmptyKey;
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function getEntityKeyForSelection(contentState, targetSelection) {
|
23 | var entityKey;
|
24 |
|
25 | if (targetSelection.isCollapsed()) {
|
26 | var key = targetSelection.getAnchorKey();
|
27 | var offset = targetSelection.getAnchorOffset();
|
28 |
|
29 | if (offset > 0) {
|
30 | entityKey = contentState.getBlockForKey(key).getEntityAt(offset - 1);
|
31 |
|
32 | if (entityKey !== contentState.getBlockForKey(key).getEntityAt(offset)) {
|
33 | return null;
|
34 | }
|
35 |
|
36 | return filterKey(contentState.getEntityMap(), entityKey);
|
37 | }
|
38 |
|
39 | return null;
|
40 | }
|
41 |
|
42 | var startKey = targetSelection.getStartKey();
|
43 | var startOffset = targetSelection.getStartOffset();
|
44 | var startBlock = contentState.getBlockForKey(startKey);
|
45 | entityKey = startOffset === startBlock.getLength() ? null : startBlock.getEntityAt(startOffset);
|
46 | return filterKey(contentState.getEntityMap(), entityKey);
|
47 | }
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | function filterKey(entityMap, entityKey) {
|
55 | if (notEmptyKey(entityKey)) {
|
56 | var entity = entityMap.__get(entityKey);
|
57 |
|
58 | return entity.getMutability() === 'MUTABLE' ? entityKey : null;
|
59 | }
|
60 |
|
61 | return null;
|
62 | }
|
63 |
|
64 | module.exports = getEntityKeyForSelection; |
\ | No newline at end of file |