1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 | var ReactDOMSelection = require('./ReactDOMSelection');
|
12 |
|
13 | var containsNode = require('fbjs/lib/containsNode');
|
14 | var focusNode = require('fbjs/lib/focusNode');
|
15 | var getActiveElement = require('fbjs/lib/getActiveElement');
|
16 |
|
17 | function isInDocument(node) {
|
18 | return containsNode(document.documentElement, node);
|
19 | }
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | var ReactInputSelection = {
|
28 | hasSelectionCapabilities: function (elem) {
|
29 | var nodeName = elem && elem.nodeName && elem.nodeName.toLowerCase();
|
30 | return nodeName && (nodeName === 'input' && elem.type === 'text' || nodeName === 'textarea' || elem.contentEditable === 'true');
|
31 | },
|
32 |
|
33 | getSelectionInformation: function () {
|
34 | var focusedElem = getActiveElement();
|
35 | return {
|
36 | focusedElem: focusedElem,
|
37 | selectionRange: ReactInputSelection.hasSelectionCapabilities(focusedElem) ? ReactInputSelection.getSelection(focusedElem) : null
|
38 | };
|
39 | },
|
40 |
|
41 | |
42 |
|
43 |
|
44 |
|
45 |
|
46 | restoreSelection: function (priorSelectionInformation) {
|
47 | var curFocusedElem = getActiveElement();
|
48 | var priorFocusedElem = priorSelectionInformation.focusedElem;
|
49 | var priorSelectionRange = priorSelectionInformation.selectionRange;
|
50 | if (curFocusedElem !== priorFocusedElem && isInDocument(priorFocusedElem)) {
|
51 | if (ReactInputSelection.hasSelectionCapabilities(priorFocusedElem)) {
|
52 | ReactInputSelection.setSelection(priorFocusedElem, priorSelectionRange);
|
53 | }
|
54 | focusNode(priorFocusedElem);
|
55 | }
|
56 | },
|
57 |
|
58 | |
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | getSelection: function (input) {
|
65 | var selection;
|
66 |
|
67 | if ('selectionStart' in input) {
|
68 |
|
69 | selection = {
|
70 | start: input.selectionStart,
|
71 | end: input.selectionEnd
|
72 | };
|
73 | } else if (document.selection && input.nodeName && input.nodeName.toLowerCase() === 'input') {
|
74 |
|
75 | var range = document.selection.createRange();
|
76 |
|
77 |
|
78 | if (range.parentElement() === input) {
|
79 | selection = {
|
80 | start: -range.moveStart('character', -input.value.length),
|
81 | end: -range.moveEnd('character', -input.value.length)
|
82 | };
|
83 | }
|
84 | } else {
|
85 |
|
86 | selection = ReactDOMSelection.getOffsets(input);
|
87 | }
|
88 |
|
89 | return selection || { start: 0, end: 0 };
|
90 | },
|
91 |
|
92 | |
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | setSelection: function (input, offsets) {
|
99 | var start = offsets.start;
|
100 | var end = offsets.end;
|
101 | if (end === undefined) {
|
102 | end = start;
|
103 | }
|
104 |
|
105 | if ('selectionStart' in input) {
|
106 | input.selectionStart = start;
|
107 | input.selectionEnd = Math.min(end, input.value.length);
|
108 | } else if (document.selection && input.nodeName && input.nodeName.toLowerCase() === 'input') {
|
109 | var range = input.createTextRange();
|
110 | range.collapse(true);
|
111 | range.moveStart('character', start);
|
112 | range.moveEnd('character', end - start);
|
113 | range.select();
|
114 | } else {
|
115 | ReactDOMSelection.setOffsets(input, offsets);
|
116 | }
|
117 | }
|
118 | };
|
119 |
|
120 | module.exports = ReactInputSelection; |
\ | No newline at end of file |