OLD | NEW |
---|---|
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library trydart.selection; | 5 library trydart.selection; |
6 | 6 |
7 import 'dart:html' show | 7 import 'dart:html' show |
8 CharacterData, | 8 CharacterData, |
9 Node, | 9 Node, |
10 NodeFilter, | 10 NodeFilter, |
(...skipping 17 matching lines...) Expand all Loading... | |
28 isCollapsed(selection) ? selection.anchorOffset : -1; | 28 isCollapsed(selection) ? selection.anchorOffset : -1; |
29 | 29 |
30 Text addNodeFromSubstring(int start, | 30 Text addNodeFromSubstring(int start, |
31 int end, | 31 int end, |
32 List<Node> nodes, | 32 List<Node> nodes, |
33 [Decoration decoration]) { | 33 [Decoration decoration]) { |
34 if (start == end) return null; | 34 if (start == end) return null; |
35 | 35 |
36 Text textNode = new Text(text.substring(start, end)); | 36 Text textNode = new Text(text.substring(start, end)); |
37 | 37 |
38 if (start <= globalOffset && globalOffset < end) { | 38 if (start <= globalOffset && globalOffset <= end) { |
39 anchorNode = textNode; | 39 anchorNode = textNode; |
40 anchorOffset = globalOffset - start; | 40 anchorOffset = globalOffset - start; |
41 } | 41 } |
42 | 42 |
43 nodes.add(decoration == null ? textNode : decoration.applyTo(textNode)); | 43 nodes.add(decoration == null ? textNode : decoration.applyTo(textNode)); |
44 | 44 |
45 return textNode; | 45 return textNode; |
46 } | 46 } |
47 | 47 |
48 void adjust(Selection selection) { | 48 void adjust(Selection selection) { |
49 if (anchorOffset >= 0) { | 49 if (anchorOffset >= 0) { |
50 Stopwatch sw = new Stopwatch()..start(); | |
50 selection.collapse(anchorNode, anchorOffset); | 51 selection.collapse(anchorNode, anchorOffset); |
52 sw.stop(); | |
53 print('selection.collapse took ${sw.elapsedMilliseconds}ms'); | |
kasperl
2014/05/06 04:51:06
Sure you want to keep this in?
ahe
2014/05/06 12:54:02
Removed.
| |
51 } | 54 } |
52 } | 55 } |
53 | 56 |
54 void updateText(String newText) { | 57 void updateText(String newText) { |
55 text = newText; | 58 text = newText; |
56 globalOffset = computeGlobalOffset(root, anchorNode, anchorOffset); | 59 globalOffset = computeGlobalOffset(root, anchorNode, anchorOffset); |
57 } | 60 } |
58 | 61 |
59 /// Computes the global offset, that is, the offset from [root]. | 62 /// Computes the global offset, that is, the offset from [root]. |
60 static int computeGlobalOffset(Node root, Node anchorNode, int anchorOffset) { | 63 static int computeGlobalOffset(Node root, Node anchorNode, int anchorOffset) { |
(...skipping 13 matching lines...) Expand all Loading... | |
74 | 77 |
75 return -1; | 78 return -1; |
76 } | 79 } |
77 } | 80 } |
78 | 81 |
79 bool isCollapsed(Selection selection) { | 82 bool isCollapsed(Selection selection) { |
80 // Firefox and Chrome don't agree on if the selection is collapsed if there | 83 // Firefox and Chrome don't agree on if the selection is collapsed if there |
81 // is no node selected. | 84 // is no node selected. |
82 return selection.isCollapsed && selection.anchorNode != null; | 85 return selection.isCollapsed && selection.anchorNode != null; |
83 } | 86 } |
OLD | NEW |