Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Side by Side Diff: dart/site/try/src/selection.dart

Issue 225893002: Refactor code to prepare for line-based tokenization. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « dart/site/try/src/interaction_manager.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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,
11 Selection, 11 Selection,
12 Text, 12 Text,
13 TreeWalker; 13 TreeWalker;
14 14
15 import 'decoration.dart'; 15 import 'decoration.dart';
16 16
17 class TrySelection { 17 class TrySelection {
18 final Node root; 18 final Node root;
19 final String text;
20 final int globalOffset;
21
22 Node anchorNode; 19 Node anchorNode;
23 int anchorOffset; 20 int anchorOffset;
24 21
25 TrySelection.internal( 22 String text;
26 this.root, this.text, this.globalOffset, 23 int globalOffset = -1;
27 this.anchorNode, this.anchorOffset);
28 24
29 factory TrySelection(Node root, Selection selection, String text) { 25 TrySelection(this.root, Selection selection)
30 if (selection.isCollapsed) { 26 : this.anchorNode = selection.isCollapsed ? selection.anchorNode : null,
31 Node anchorNode = selection.anchorNode; 27 this.anchorOffset = selection.isCollapsed ? selection.anchorOffset : -1;
32 int anchorOffset = selection.anchorOffset;
33 return new TrySelection.internal(
34 root, text, computeGlobalOffset(root, anchorNode, anchorOffset),
35 anchorNode, anchorOffset);
36 } else {
37 return new TrySelection.internal(root, text, -1, null, -1);
38 }
39 }
40 28
41 Text addNodeFromSubstring(int start, 29 Text addNodeFromSubstring(int start,
42 int end, 30 int end,
43 List<Node> nodes, 31 List<Node> nodes,
44 [Decoration decoration]) { 32 [Decoration decoration]) {
45 if (start == end) return null; 33 if (start == end) return null;
46 34
47 Text textNode = new Text(text.substring(start, end)); 35 Text textNode = new Text(text.substring(start, end));
48 36
49 if (start <= globalOffset && globalOffset < end) { 37 if (start <= globalOffset && globalOffset < end) {
50 anchorNode = textNode; 38 anchorNode = textNode;
51 anchorOffset = globalOffset - start; 39 anchorOffset = globalOffset - start;
52 } 40 }
53 41
54 nodes.add(decoration == null ? textNode : decoration.applyTo(textNode)); 42 nodes.add(decoration == null ? textNode : decoration.applyTo(textNode));
55 43
56 return textNode; 44 return textNode;
57 } 45 }
58 46
59 void adjust(Selection selection) { 47 void adjust(Selection selection) {
60 if (anchorOffset >= 0) { 48 if (anchorOffset >= 0) {
61 selection.collapse(anchorNode, anchorOffset); 49 selection.collapse(anchorNode, anchorOffset);
62 } 50 }
63 } 51 }
64 52
53 void updateText(String newText) {
54 text = newText;
55 globalOffset = computeGlobalOffset(root, anchorNode, anchorOffset);
56 }
57
65 /// Computes the global offset, that is, the offset from [root]. 58 /// Computes the global offset, that is, the offset from [root].
66 static int computeGlobalOffset(Node root, Node anchorNode, int anchorOffset) { 59 static int computeGlobalOffset(Node root, Node anchorNode, int anchorOffset) {
67 if (anchorOffset == -1) return -1; 60 if (anchorOffset == -1) return -1;
68 61
69 int offset = 0; 62 int offset = 0;
70 TreeWalker walker = new TreeWalker(root, NodeFilter.SHOW_TEXT); 63 TreeWalker walker = new TreeWalker(root, NodeFilter.SHOW_TEXT);
71 for (Node node = walker.nextNode(); 64 for (Node node = walker.nextNode();
72 node != null; 65 node != null;
73 node = walker.nextNode()) { 66 node = walker.nextNode()) {
74 CharacterData text = node; 67 CharacterData text = node;
75 if (anchorNode == text) { 68 if (anchorNode == text) {
76 return anchorOffset + offset; 69 return anchorOffset + offset;
77 } 70 }
78 offset += text.data.length; 71 offset += text.data.length;
79 } 72 }
80 73
81 return -1; 74 return -1;
82 } 75 }
83 } 76 }
OLDNEW
« no previous file with comments | « dart/site/try/src/interaction_manager.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698