1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | var UnicodeUtils = require("fbjs/lib/UnicodeUtils");
|
14 |
|
15 | var getCorrectDocumentFromNode = require("./getCorrectDocumentFromNode");
|
16 |
|
17 | var getRangeClientRects = require("./getRangeClientRects");
|
18 |
|
19 | var invariant = require("fbjs/lib/invariant");
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | function getLineHeightPx(element) {
|
26 | var computed = getComputedStyle(element);
|
27 | var correctDocument = getCorrectDocumentFromNode(element);
|
28 | var div = correctDocument.createElement('div');
|
29 | div.style.fontFamily = computed.fontFamily;
|
30 | div.style.fontSize = computed.fontSize;
|
31 | div.style.fontStyle = computed.fontStyle;
|
32 | div.style.fontWeight = computed.fontWeight;
|
33 | div.style.lineHeight = computed.lineHeight;
|
34 | div.style.position = 'absolute';
|
35 | div.textContent = 'M';
|
36 | var documentBody = correctDocument.body;
|
37 | !documentBody ? process.env.NODE_ENV !== "production" ? invariant(false, 'Missing document.body') : invariant(false) : void 0;
|
38 |
|
39 | documentBody.appendChild(div);
|
40 | var rect = div.getBoundingClientRect();
|
41 | documentBody.removeChild(div);
|
42 | return rect.height;
|
43 | }
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | function areRectsOnOneLine(rects, lineHeight) {
|
59 | var minTop = Infinity;
|
60 | var minBottom = Infinity;
|
61 | var maxTop = -Infinity;
|
62 | var maxBottom = -Infinity;
|
63 |
|
64 | for (var ii = 0; ii < rects.length; ii++) {
|
65 | var rect = rects[ii];
|
66 |
|
67 | if (rect.width === 0 || rect.width === 1) {
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | continue;
|
76 | }
|
77 |
|
78 | minTop = Math.min(minTop, rect.top);
|
79 | minBottom = Math.min(minBottom, rect.bottom);
|
80 | maxTop = Math.max(maxTop, rect.top);
|
81 | maxBottom = Math.max(maxBottom, rect.bottom);
|
82 | }
|
83 |
|
84 | return maxTop <= minBottom && maxTop - minTop < lineHeight && maxBottom - minBottom < lineHeight;
|
85 | }
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | function getNodeLength(node) {
|
92 |
|
93 | switch (node.nodeType) {
|
94 | case Node.DOCUMENT_TYPE_NODE:
|
95 | return 0;
|
96 |
|
97 | case Node.TEXT_NODE:
|
98 | case Node.PROCESSING_INSTRUCTION_NODE:
|
99 | case Node.COMMENT_NODE:
|
100 | return node.length;
|
101 |
|
102 | default:
|
103 | return node.childNodes.length;
|
104 | }
|
105 | }
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | function expandRangeToStartOfLine(range) {
|
113 | !range.collapsed ? process.env.NODE_ENV !== "production" ? invariant(false, 'expandRangeToStartOfLine: Provided range is not collapsed.') : invariant(false) : void 0;
|
114 | range = range.cloneRange();
|
115 | var containingElement = range.startContainer;
|
116 |
|
117 | if (containingElement.nodeType !== 1) {
|
118 | containingElement = containingElement.parentNode;
|
119 | }
|
120 |
|
121 | var lineHeight = getLineHeightPx(containingElement);
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | var bestContainer = range.endContainer;
|
133 | var bestOffset = range.endOffset;
|
134 | range.setStart(range.startContainer, 0);
|
135 |
|
136 | while (areRectsOnOneLine(getRangeClientRects(range), lineHeight)) {
|
137 | bestContainer = range.startContainer;
|
138 | bestOffset = range.startOffset;
|
139 | !bestContainer.parentNode ? process.env.NODE_ENV !== "production" ? invariant(false, 'Found unexpected detached subtree when traversing.') : invariant(false) : void 0;
|
140 | range.setStartBefore(bestContainer);
|
141 |
|
142 | if (bestContainer.nodeType === 1 && getComputedStyle(bestContainer).display !== 'inline') {
|
143 |
|
144 | break;
|
145 | }
|
146 | }
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 | var currentContainer = bestContainer;
|
157 | var maxIndexToConsider = bestOffset - 1;
|
158 |
|
159 | do {
|
160 | var nodeValue = currentContainer.nodeValue;
|
161 | var ii = maxIndexToConsider;
|
162 |
|
163 | for (; ii >= 0; ii--) {
|
164 | if (nodeValue != null && ii > 0 && UnicodeUtils.isSurrogatePair(nodeValue, ii - 1)) {
|
165 |
|
166 |
|
167 | continue;
|
168 | }
|
169 |
|
170 | range.setStart(currentContainer, ii);
|
171 |
|
172 | if (areRectsOnOneLine(getRangeClientRects(range), lineHeight)) {
|
173 | bestContainer = currentContainer;
|
174 | bestOffset = ii;
|
175 | } else {
|
176 | break;
|
177 | }
|
178 | }
|
179 |
|
180 | if (ii === -1 || currentContainer.childNodes.length === 0) {
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | break;
|
189 | }
|
190 |
|
191 | currentContainer = currentContainer.childNodes[ii];
|
192 | maxIndexToConsider = getNodeLength(currentContainer);
|
193 | } while (true);
|
194 |
|
195 | range.setStart(bestContainer, bestOffset);
|
196 | return range;
|
197 | }
|
198 |
|
199 | module.exports = expandRangeToStartOfLine; |
\ | No newline at end of file |