OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 "use strict"; | 5 "use strict"; |
6 | 6 |
7 class TextView extends View { | 7 class TextView extends View { |
8 constructor(id, broker, patterns, allowSpanSelection) { | 8 constructor(id, broker, patterns, allowSpanSelection) { |
9 super(id, broker); | 9 super(id, broker); |
10 let view = this; | 10 let view = this; |
11 view.sortedPositionList = []; | 11 view.hide(); |
12 view.nodePositionMap = []; | |
13 view.positionNodeMap = []; | |
14 view.textListNode = view.divNode.getElementsByTagName('ul')[0]; | 12 view.textListNode = view.divNode.getElementsByTagName('ul')[0]; |
15 view.fillerSvgElement = view.divElement.append("svg").attr('version','1.1').
attr("width", "0"); | 13 view.fillerSvgElement = view.divElement.append("svg").attr('version','1.1').
attr("width", "0"); |
16 view.patterns = patterns; | 14 view.patterns = patterns; |
17 view.allowSpanSelection = allowSpanSelection; | 15 view.allowSpanSelection = allowSpanSelection; |
18 view.nodeToLineMap = []; | 16 view.nodeToLineMap = []; |
19 var selectionHandler = { | 17 var selectionHandler = { |
20 clear: function() { | 18 clear: function() { |
21 broker.clear(selectionHandler); | 19 broker.clear(selectionHandler); |
22 }, | 20 }, |
23 select: function(items, selected) { | 21 select: function(items, selected) { |
24 for (let i of items) { | 22 for (let i of items) { |
25 if (selected) { | 23 if (selected) { |
26 i.classList.add("selected"); | 24 i.classList.add("selected"); |
27 } else { | 25 } else { |
28 i.classList.remove("selected"); | 26 i.classList.remove("selected"); |
29 } | 27 } |
30 } | 28 } |
31 broker.clear(selectionHandler); | 29 broker.clear(selectionHandler); |
32 broker.select(selectionHandler, view.getRanges(items), selected); | 30 broker.select(selectionHandler, view.getLocations(items), selected); |
33 }, | 31 }, |
34 selectionDifference: function(span1, inclusive1, span2, inclusive2) { | 32 selectionDifference: function(span1, inclusive1, span2, inclusive2) { |
35 return null; | 33 return null; |
36 }, | 34 }, |
37 brokeredSelect: function(ranges, selected) { | 35 brokeredSelect: function(locations, selected) { |
38 let locations = view.rangesToLocations(ranges); | |
39 view.selectLocations(locations, selected, true); | 36 view.selectLocations(locations, selected, true); |
40 }, | 37 }, |
41 brokeredClear: function() { | 38 brokeredClear: function() { |
42 view.selection.clear(); | 39 view.selection.clear(); |
43 } | 40 } |
44 }; | 41 }; |
45 view.selection = new Selection(selectionHandler); | 42 view.selection = new Selection(selectionHandler); |
46 broker.addSelectionHandler(selectionHandler); | 43 broker.addSelectionHandler(selectionHandler); |
47 } | 44 } |
48 | 45 |
49 setPatterns(patterns) { | 46 setPatterns(patterns) { |
50 let view = this; | 47 let view = this; |
51 view.patterns = patterns; | 48 view.patterns = patterns; |
52 } | 49 } |
53 | 50 |
54 clearText() { | 51 clearText() { |
55 let view = this; | 52 let view = this; |
56 while (view.textListNode.firstChild) { | 53 while (view.textListNode.firstChild) { |
57 view.textListNode.removeChild(view.textListNode.firstChild); | 54 view.textListNode.removeChild(view.textListNode.firstChild); |
58 } | 55 } |
59 } | 56 } |
60 | 57 |
61 rangeToLocation(range) { | |
62 return range; | |
63 } | |
64 | |
65 rangesToLocations(ranges) { | |
66 let view = this; | |
67 let nodes = new Set(); | |
68 let result = []; | |
69 for (let range of ranges) { | |
70 let start = range[0]; | |
71 let end = range[1]; | |
72 let block_id = range[3]; | |
73 let location = { pos_start: start, pos_end: end, block_id: block_id }; | |
74 if (range[2] !== null && range[2] != -1) { | |
75 location.node_id = range[2]; | |
76 if (range[0] == -1 && range[1] == -1) { | |
77 location.pos_start = view.nodePositionMap[location.node_id]; | |
78 location.pos_end = location.pos_start + 1; | |
79 } | |
80 } else { | |
81 if (range[0] != undefined) { | |
82 location.pos_start = range[0]; | |
83 location.pos_end = range[1]; | |
84 } | |
85 } | |
86 result.push(location); | |
87 } | |
88 return result; | |
89 } | |
90 | |
91 sameLocation(l1, l2) { | 58 sameLocation(l1, l2) { |
92 let view = this; | 59 let view = this; |
93 if (l1.block_id != undefined && l2.block_id != undefined && | 60 if (l1.block_id != undefined && l2.block_id != undefined && |
94 l1.block_id == l2.block_id && l1.node_id === undefined) { | 61 l1.block_id == l2.block_id && l1.node_id === undefined) { |
95 return true; | 62 return true; |
96 } | 63 } |
97 | 64 |
98 if (l1.address != undefined && l1.address == l2.address) { | 65 if (l1.address != undefined && l1.address == l2.address) { |
99 return true; | 66 return true; |
100 } | 67 } |
101 | 68 |
102 let node1 = l1.node_id; | 69 let node1 = l1.node_id; |
103 let node2 = l2.node_id; | 70 let node2 = l2.node_id; |
104 | 71 |
105 if (node1 === undefined && node2 == undefined) { | 72 if (node1 === undefined || node2 == undefined) { |
106 if (l1.pos_start === undefined || l2.pos_start == undefined) { | 73 if (l1.pos_start === undefined || l2.pos_start == undefined) { |
107 return false; | 74 return false; |
108 } | 75 } |
109 if (l1.pos_start == -1 || l2.pos_start == -1) { | 76 if (l1.pos_start == -1 || l2.pos_start == -1) { |
110 return false; | 77 return false; |
111 } | 78 } |
112 if (l1.pos_start < l2.pos_start) { | 79 if (l1.pos_start < l2.pos_start) { |
113 return l1.pos_end > l2.pos_start; | 80 return l1.pos_end > l2.pos_start; |
114 } { | 81 } { |
115 return l1.pos_start < l2.pos_end; | 82 return l1.pos_start < l2.pos_end; |
116 } | 83 } |
117 } | 84 } |
118 | 85 |
119 if (node1 === undefined) { | |
120 let lower = lowerBound(view.positionNodeMap, l1.pos_start, undefined, func
tion(a, b) { | |
121 var node = a[b]; | |
122 return view.nodePositionMap[node]; | |
123 } ); | |
124 while (++lower < view.positionNodeMap.length && | |
125 view.nodePositionMap[view.positionNodeMap[lower]] < l1.pos_end) { | |
126 if (view.positionNodeMap[lower] == node2) { | |
127 return true; | |
128 } | |
129 } | |
130 return false; | |
131 } | |
132 | |
133 if (node2 === undefined) { | |
134 let lower = lowerBound(view.positionNodeMap, l2.pos_start, undefined, func
tion(a, b) { | |
135 var node = a[b]; | |
136 return view.nodePositionMap[node]; | |
137 } ); | |
138 while (++lower < view.positionNodeMap.length && | |
139 view.nodePositionMap[view.positionNodeMap[lower]] < l2.pos_end) { | |
140 if (view.positionNodeMap[lower] == node1) { | |
141 return true; | |
142 } | |
143 } | |
144 return false; | |
145 } | |
146 | |
147 return l1.node_id == l2.node_id; | 86 return l1.node_id == l2.node_id; |
148 } | 87 } |
149 | 88 |
150 setNodePositionMap(map) { | |
151 let view = this; | |
152 view.nodePositionMap = map; | |
153 view.positionNodeMap = []; | |
154 view.sortedPositionList = []; | |
155 let next = 0; | |
156 for (let i in view.nodePositionMap) { | |
157 view.sortedPositionList[next] = Number(view.nodePositionMap[i]); | |
158 view.positionNodeMap[next++] = i; | |
159 } | |
160 view.sortedPositionList = sortUnique(view.sortedPositionList, | |
161 function(a,b) { return a - b; }); | |
162 this.positionNodeMap.sort(function(a,b) { | |
163 let result = view.nodePositionMap[a] - view.nodePositionMap[b]; | |
164 if (result != 0) return result; | |
165 return a - b; | |
166 }); | |
167 } | |
168 | |
169 selectLocations(locations, selected, makeVisible) { | 89 selectLocations(locations, selected, makeVisible) { |
170 let view = this; | 90 let view = this; |
171 let s = new Set(); | 91 let s = new Set(); |
172 for (let l of locations) { | 92 for (let l of locations) { |
173 for (let i = 0; i < view.textListNode.children.length; ++i) { | 93 for (let i = 0; i < view.textListNode.children.length; ++i) { |
174 let child = view.textListNode.children[i]; | 94 let child = view.textListNode.children[i]; |
175 if (child.location != undefined && view.sameLocation(l, child.location))
{ | 95 if (child.location != undefined && view.sameLocation(l, child.location))
{ |
176 s.add(child); | 96 s.add(child); |
177 } | 97 } |
178 } | 98 } |
179 } | 99 } |
180 view.selectCommon(s, selected, makeVisible); | 100 view.selectCommon(s, selected, makeVisible); |
181 } | 101 } |
182 | 102 |
183 getRanges(items) { | 103 getLocations(items) { |
184 let result = []; | 104 let result = []; |
185 let lastObject = null; | 105 let lastObject = null; |
186 for (let i of items) { | 106 for (let i of items) { |
187 if (i.location) { | 107 if (i.location) { |
188 let location = i.location; | 108 result.push(i.location); |
189 let start = -1; | |
190 let end = -1; | |
191 let node_id = -1; | |
192 let block_id = -1; | |
193 if (location.node_id !== undefined) { | |
194 node_id = location.node_id; | |
195 } | |
196 if (location.block_id !== undefined) { | |
197 block_id = location.block_id; | |
198 } | |
199 if (location.pos_start !== undefined) { | |
200 start = location.pos_start; | |
201 end = location.pos_end; | |
202 } else { | |
203 if (this.nodePositionMap && this.nodePositionMap[node_id]) { | |
204 start = this.nodePositionMap[node_id]; | |
205 end = start + 1; | |
206 } | |
207 } | |
208 if (lastObject == null || | |
209 (lastObject[2] != node_id || | |
210 lastObject[0] != start || | |
211 lastObject[1] != end || | |
212 lastObject[3] != block_id)) { | |
213 lastObject = [start, end, node_id, block_id]; | |
214 result.push(lastObject); | |
215 } | |
216 } | 109 } |
217 } | 110 } |
218 return result; | 111 return result; |
219 } | 112 } |
220 | 113 |
221 createFragment(text, style) { | 114 createFragment(text, style) { |
222 let view = this; | 115 let view = this; |
223 let span = document.createElement("SPAN"); | 116 let span = document.createElement("SPAN"); |
224 span.onmousedown = function(e) { | 117 span.onmousedown = function(e) { |
225 view.mouseDownSpan(span, e); | 118 view.mouseDownSpan(span, e); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 let child = view.textListNode.children[i]; | 190 let child = view.textListNode.children[i]; |
298 if (child.location && s(child.location)) { | 191 if (child.location && s(child.location)) { |
299 if (firstSelect) { | 192 if (firstSelect) { |
300 makeContainerPosVisible(view.parentNode, child.offsetTop); | 193 makeContainerPosVisible(view.parentNode, child.offsetTop); |
301 firstSelect = false; | 194 firstSelect = false; |
302 } | 195 } |
303 view.selection.select(child, selected); | 196 view.selection.select(child, selected); |
304 } | 197 } |
305 } | 198 } |
306 } else if (typeof s[Symbol.iterator] === 'function') { | 199 } else if (typeof s[Symbol.iterator] === 'function') { |
307 for (let i of s) { | 200 if (firstSelect) { |
308 if (firstSelect) { | 201 for (let i of s) { |
309 makeContainerPosVisible(view.parentNode, i.offsetTop); | 202 makeContainerPosVisible(view.parentNode, i.offsetTop); |
310 firstSelect = false; | 203 break; |
311 } | 204 } |
312 view.selection.select(i, selected); | |
313 } | 205 } |
| 206 view.selection.select(s, selected); |
314 } else { | 207 } else { |
315 if (firstSelect) { | 208 if (firstSelect) { |
316 makeContainerPosVisible(view.parentNode, s.offsetTop); | 209 makeContainerPosVisible(view.parentNode, s.offsetTop); |
317 } | 210 } |
318 view.selection.select(s, selected); | 211 view.selection.select(s, selected); |
319 } | 212 } |
320 } | 213 } |
321 | 214 |
322 mouseDownLine(li, e) { | 215 mouseDownLine(li, e) { |
323 let view = this; | 216 let view = this; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 lineLocation(li) { | 287 lineLocation(li) { |
395 let view = this; | 288 let view = this; |
396 for (let i = 0; i < li.children.length; ++i) { | 289 for (let i = 0; i < li.children.length; ++i) { |
397 let fragment = li.children[i]; | 290 let fragment = li.children[i]; |
398 if (fragment.location != undefined && !view.allowSpanSelection) { | 291 if (fragment.location != undefined && !view.allowSpanSelection) { |
399 return fragment.location; | 292 return fragment.location; |
400 } | 293 } |
401 } | 294 } |
402 } | 295 } |
403 } | 296 } |
OLD | NEW |