Revision | 14d693b8a8ad77af24f48f1aa371b7efb35a3356 (tree) |
---|---|
Time | 2020-10-04 06:27:48 |
Author | dyknon <dyknon@user...> |
Commiter | dyknon |
simplify
@@ -133,10 +133,11 @@ function connect_signals(candidate_popup){ | ||
133 | 133 | _preedit_handle_id = candidate_popup._panelService.connect( |
134 | 134 | "update-preedit-text", |
135 | 135 | (ps, text, cursorPosition, visible) => { |
136 | - // cursorPosition: index start from 0; counting Unicode characters. | |
137 | - // XXX: How about combining characters? | |
138 | - // text.get_text(): Javascript string object. | |
139 | - // Its indexes are based on UTF-16 code units. | |
136 | + // Text length and position representations | |
137 | + // iBus: index start from 0; counting Unicode characters. | |
138 | + // XXX: How about combining characters? | |
139 | + // Javascript: Counts UTF-16 code units. | |
140 | + // Pango: Counts UTF-8 bytes. | |
140 | 141 | const text_string = text.get_text(); |
141 | 142 | let newstring = text_string; |
142 | 143 | const attrs = text.get_attributes(); |
@@ -145,16 +146,23 @@ function connect_signals(candidate_popup){ | ||
145 | 146 | let unicode_count = 0; |
146 | 147 | let utf16_count = 0; |
147 | 148 | let utf8_count = 0; |
148 | - while(utf16_count < text_string.length){ | |
149 | - if(_cursor_letter !== void 0 && cursorPosition == unicode_count){ | |
150 | - if(!_cursor_combining || unicode_count){ | |
149 | + let cursor = () => {}; | |
150 | + if(_cursor_letter !== void 0){ | |
151 | + cursor = () => { | |
152 | + if( | |
153 | + cursorPosition == unicode_count | |
154 | + && (!_cursor_combining || unicode_count) | |
155 | + ){ | |
151 | 156 | newstring = ""; |
152 | 157 | newstring += text_string.substring(0, utf16_count); |
153 | 158 | newstring += _cursor_letter; |
154 | 159 | newstring += text_string.substring(utf16_count); |
155 | 160 | utf8_count += _cursor_utf8_cnt; |
156 | 161 | } |
157 | - } | |
162 | + }; | |
163 | + } | |
164 | + while(utf16_count < text_string.length){ | |
165 | + cursor(); | |
158 | 166 | |
159 | 167 | const code = text_string.codePointAt(utf16_count); |
160 | 168 | if(code > 0xffff){ |
@@ -168,15 +176,7 @@ function connect_signals(candidate_popup){ | ||
168 | 176 | |
169 | 177 | utf8i.push(utf8_count); |
170 | 178 | } |
171 | - if(_cursor_letter !== void 0 && cursorPosition == unicode_count){ | |
172 | - if(!_cursor_combining || unicode_count){ | |
173 | - newstring = ""; | |
174 | - newstring += text_string.substring(0, utf16_count); | |
175 | - newstring += _cursor_letter; | |
176 | - newstring += text_string.substring(utf16_count); | |
177 | - utf8_count += _cursor_utf8_cnt; | |
178 | - } | |
179 | - } | |
179 | + cursor(); | |
180 | 180 | } |
181 | 181 | |
182 | 182 | candidate_popup._preeditText.text = newstring; |