OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "core/editing/suggestion/TextSuggestionController.h" | |
6 | |
7 #include "core/editing/Editor.h" | |
8 #include "core/editing/FrameSelection.h" | |
9 #include "core/editing/PlainTextRange.h" | |
10 #include "core/editing/markers/DocumentMarkerController.h" | |
11 #include "core/editing/markers/SpellCheckMarker.h" | |
12 #include "core/editing/spellcheck/SpellChecker.h" | |
13 #include "core/frame/FrameView.h" | |
14 #include "core/frame/LocalFrame.h" | |
15 #include "public/platform/InterfaceProvider.h" | |
16 | |
17 namespace blink { | |
18 | |
19 namespace { | |
20 | |
21 const double kSuggestionUnderlineAlphaMultiplier = 0.4; | |
22 | |
23 } // anonymous namespace | |
24 | |
25 TextSuggestionController::TextSuggestionController(LocalFrame& frame) | |
26 : suggestion_menu_is_open_(false), frame_(&frame) {} | |
27 | |
28 bool TextSuggestionController::IsMenuOpen() const { | |
29 return suggestion_menu_is_open_; | |
30 } | |
31 | |
32 void TextSuggestionController::HandlePotentialMisspelledWordTap() { | |
33 Optional<std::pair<Node*, SpellCheckMarker*>> node_and_marker = | |
34 GetFrame().GetSpellChecker().GetSpellCheckMarkerTouchingSelection(); | |
35 if (!node_and_marker) | |
36 return; | |
37 | |
38 if (!text_suggestion_host_) { | |
39 GetFrame().GetInterfaceProvider()->GetInterface( | |
40 mojo::MakeRequest(&text_suggestion_host_)); | |
41 } | |
42 | |
43 text_suggestion_host_->StartSpellCheckMenuTimer(); | |
44 } | |
45 | |
46 DEFINE_TRACE(TextSuggestionController) { | |
47 visitor->Trace(frame_); | |
48 } | |
49 | |
50 void TextSuggestionController::ApplySpellCheckSuggestion( | |
51 const String& suggestion) { | |
52 GetFrame().GetSpellChecker().ReplaceMisspelledRange(suggestion); | |
53 SuggestionMenuClosed(); | |
54 } | |
55 | |
56 namespace { | |
57 | |
58 bool ShouldDeleteNextCharacter(const Node* marker_text_node, | |
59 const DocumentMarker* marker) { | |
60 // If the character immediately following the range to be deleted is a space, | |
61 // delete it if either of these conditions holds: | |
62 // - We're deleting at the beginning of the editable text (to avoid ending up | |
63 // with a space at the beginning) | |
64 // - The character immediately before the range being deleted is also a space | |
65 // (to avoid ending up with two adjacent spaces) | |
66 const EphemeralRange next_character_range = | |
67 PlainTextRange(marker->EndOffset(), marker->EndOffset() + 1) | |
68 .CreateRange(*marker_text_node->parentNode()); | |
69 // No character immediately following the range (so it can't be a space) | |
70 if (next_character_range.IsNull()) | |
71 return false; | |
72 | |
73 const String next_character_str = PlainText( | |
74 next_character_range, | |
75 TextIteratorBehavior::Builder().SetEmitsSpaceForNbsp(true).Build()); | |
76 // Character immediately following the range is not a space | |
77 if (!WTF::IsASCIISpace(next_character_str[0])) | |
78 return false; | |
79 | |
80 // First case: we're deleting at the beginning of the editable text | |
81 if (marker->StartOffset() == 0) | |
82 return true; | |
83 | |
84 const EphemeralRange prev_character_range = | |
85 PlainTextRange(marker->StartOffset() - 1, marker->StartOffset()) | |
86 .CreateRange(*marker_text_node->parentNode()); | |
87 // Not at beginning, but there's no character immediately before the range | |
88 // being deleted (so it can't be a space) | |
89 if (prev_character_range.IsNull()) | |
90 return false; | |
91 | |
92 const String prev_character_str = PlainText( | |
93 prev_character_range, | |
94 TextIteratorBehavior::Builder().SetEmitsSpaceForNbsp(true).Build()); | |
95 // Return true if the character immediately before the range is space, false | |
96 // otherwise | |
97 return WTF::IsASCIISpace(prev_character_str[0]); | |
98 } | |
99 | |
100 } // namespace | |
101 | |
102 void TextSuggestionController::DeleteActiveSuggestionRange() { | |
103 Optional<std::pair<Node*, SpellCheckMarker*>> node_and_marker = | |
104 GetFrame().GetSpellChecker().GetSpellCheckMarkerTouchingSelection(); | |
105 if (!node_and_marker) | |
106 return; | |
107 | |
108 Node* const marker_text_node = node_and_marker.value().first; | |
109 const DocumentMarker* const marker = node_and_marker.value().second; | |
110 | |
111 const bool delete_next_char = | |
112 ShouldDeleteNextCharacter(marker_text_node, marker); | |
113 | |
114 const EphemeralRange range_to_delete = EphemeralRange( | |
115 Position(marker_text_node, marker->StartOffset()), | |
116 Position(marker_text_node, marker->EndOffset() + delete_next_char)); | |
117 | |
118 GetFrame().Selection().SetSelection( | |
119 SelectionInDOMTree::Builder().SetBaseAndExtent(range_to_delete).Build()); | |
120 | |
121 GetFrame().GetEditor().ReplaceSelectionWithText( | |
122 "", false, false, InputEvent::InputType::kDeleteByComposition); | |
yosin_UTC9
2017/06/08 01:50:09
Can we use DeleteSelectionCommand instead of Repla
chongz
2017/06/08 15:49:16
JS can always figure it out from the empty |data|
rlanday
2017/06/08 18:35:41
Do you want me to use DeleteSelectionCommand direc
| |
123 | |
124 SuggestionMenuClosed(); | |
125 } | |
126 | |
127 void TextSuggestionController::NewWordAddedToDictionary(const String& word) { | |
128 // Android pops up a dialog to let the user confirm they actually want to add | |
129 // the word to the dictionary; this method gets called as soon as the dialog | |
130 // is shown. So the word isn't actually in the dictionary here, even if the | |
131 // user will end up confirming the dialog, and we shouldn't try to re-run | |
132 // spellcheck here. | |
133 | |
134 // Note: this actually matches the behavior in native Android text boxes | |
135 GetDocument().Markers().RemoveSpellingMarkersUnderWords( | |
136 Vector<String>({word})); | |
137 SuggestionMenuClosed(); | |
138 } | |
139 | |
140 void TextSuggestionController::SpellCheckMenuTimeoutCallback() { | |
141 Optional<std::pair<Node*, SpellCheckMarker*>> const node_and_marker = | |
142 GetFrame().GetSpellChecker().GetSpellCheckMarkerTouchingSelection(); | |
143 if (!node_and_marker) | |
144 return; | |
145 | |
146 Node* const container_node = node_and_marker.value().first; | |
147 SpellCheckMarker* const marker = node_and_marker.value().second; | |
148 | |
149 Range* marker_range = | |
150 Range::Create(GetDocument(), container_node, marker->StartOffset(), | |
151 container_node, marker->EndOffset()); | |
152 | |
153 const String& misspelled_word = marker_range->GetText(); | |
yosin_UTC9
2017/06/08 05:06:50
Please use PlaintText() instead of using temporary
| |
154 const String& description = marker->Description(); | |
155 | |
156 suggestion_menu_is_open_ = true; | |
157 GetFrame().Selection().SetCaretVisible(false); | |
158 GetDocument().Markers().AddActiveSuggestionMarker( | |
159 EphemeralRange(marker_range), SK_ColorTRANSPARENT, | |
160 StyleableMarker::Thickness::kThin, | |
161 Color(SK_ColorRED).CombineWithAlpha(kSuggestionUnderlineAlphaMultiplier)); | |
yosin_UTC9
2017/06/08 01:50:09
Could you move color of marker to InlineTextBoxPai
| |
162 | |
163 Vector<String> suggestions; | |
164 description.Split('\n', suggestions); | |
165 | |
166 Vector<mojom::blink::SpellCheckSuggestionPtr> suggestion_ptrs; | |
167 for (const String& suggestion : suggestions) { | |
168 mojom::blink::SpellCheckSuggestionPtr info_ptr( | |
169 mojom::blink::SpellCheckSuggestion::New()); | |
170 info_ptr->suggestion = suggestion; | |
171 suggestion_ptrs.push_back(std::move(info_ptr)); | |
172 } | |
173 | |
174 const IntRect& absolute_bounds = GetFrame().Selection().AbsoluteCaretBounds(); | |
175 const IntRect& viewport_bounds = | |
176 GetFrame().View()->ContentsToViewport(absolute_bounds); | |
177 | |
178 text_suggestion_host_->ShowSpellCheckSuggestionMenu( | |
179 viewport_bounds.X(), viewport_bounds.MaxY(), std::move(misspelled_word), | |
180 std::move(suggestion_ptrs)); | |
181 } | |
182 | |
183 void TextSuggestionController::SuggestionMenuClosed() { | |
184 GetDocument().Markers().RemoveMarkersOfTypes( | |
185 DocumentMarker::kActiveSuggestion); | |
186 GetFrame().Selection().SetCaretVisible(true); | |
187 suggestion_menu_is_open_ = false; | |
188 } | |
189 | |
190 Document& TextSuggestionController::GetDocument() const { | |
191 DCHECK(IsAvailable()); | |
192 return *GetFrame().GetDocument(); | |
193 } | |
194 | |
195 bool TextSuggestionController::IsAvailable() const { | |
196 return GetFrame().GetDocument(); | |
197 } | |
198 | |
199 } // namespace blink | |
OLD | NEW |