@@ -19,7 +19,7 @@ import { useLocalization } from '../../util/localization';
19
19
interface Suggestion {
20
20
token : string ;
21
21
known : boolean ;
22
- sugg : Array < [ string , number ] > ;
22
+ sugg : Array < string > ;
23
23
}
24
24
25
25
// eslint-disable-next-line
@@ -64,7 +64,8 @@ const SpellCheckForm = ({
64
64
} , [ history , lang , text ] ) ;
65
65
66
66
const handleInput = ( e : React . FormEvent < HTMLDivElement > ) => {
67
- setText ( e . currentTarget . innerText ) ;
67
+ const plainText = e . currentTarget . innerText ;
68
+ setText ( plainText . replace ( / < \/ ? [ ^ > ] + ( > | $ ) / g, '' ) ) ; // Strip away any HTML tags
68
69
} ;
69
70
70
71
const handleSubmit = ( ) => {
@@ -78,13 +79,13 @@ const SpellCheckForm = ({
78
79
void ( async ( ) => {
79
80
try {
80
81
trackEvent ( { category : 'spellchecker' , action : 'spellcheck' , name : lang , value : text . length } ) ;
81
- const [ ref , request ] = apyFetch ( 'speller' , { lang, q : text } ) ;
82
+ const [ ref , request ] = apyFetch ( 'speller/voikko ' , { lang, q : text } ) ;
82
83
spellcheckResult . current = ref ;
83
- setSuggestions ( ( await request ) . data as Array < Suggestion > ) ;
84
+ const data = ( await request ) . data as Array < Suggestion > ;
85
+ setSuggestions ( data ) ;
86
+ renderHighlightedText ( text , data ) ;
84
87
setError ( null ) ;
85
88
spellcheckResult . current = null ;
86
-
87
- renderHighlightedText ( text ) ;
88
89
setLoading ( false ) ;
89
90
} catch ( error ) {
90
91
if ( ! axios . isCancel ( error ) ) {
@@ -183,40 +184,38 @@ const SpellCheckForm = ({
183
184
initialRender . current = false ;
184
185
}
185
186
186
- const renderHighlightedText = React . useCallback (
187
- ( text : string ) => {
188
- if ( text . trim ( ) . length === 0 ) {
189
- return ;
190
- }
187
+ const renderHighlightedText = ( text : string , suggestions : Suggestion [ ] ) => {
188
+ if ( text . trim ( ) . length === 0 ) {
189
+ return ;
190
+ }
191
191
192
- const contentElement = spellcheckRef . current ;
193
- if ( contentElement instanceof HTMLElement ) {
194
- const savedSelection = saveCaretPosition ( contentElement ) ;
195
- const parts = text
196
- . split ( / ( \s + ) / )
197
- . map ( ( word , index ) => {
198
- const suggestion = suggestions . find ( ( s ) => s . token === word && ! s . known ) ;
199
- if ( suggestion ) {
200
- return `<span key=${ index } class="misspelled">${ word } </span>` ;
201
- } else {
202
- return `<span key=${ index } class="correct">${ word } </span>` ;
203
- }
204
- } )
205
- . join ( '' ) ;
206
-
207
- contentElement . innerHTML = parts ;
208
- const misspelledElements = contentElement . querySelectorAll ( '.misspelled' ) ;
209
- misspelledElements . forEach ( ( element ) => {
210
- const word = element . textContent || '' ;
211
- const eventHandler = ( e : Event ) => handleWordClick ( word , e as MouseEvent | TouchEvent ) ;
212
- element . addEventListener ( 'click' , eventHandler ) ;
213
- element . addEventListener ( 'touchstart' , eventHandler ) ;
214
- } ) ;
215
- restoreCaretPosition ( contentElement , savedSelection ) ;
216
- }
217
- } ,
218
- [ suggestions , handleWordClick ] ,
219
- ) ;
192
+ const contentElement = spellcheckRef . current ;
193
+ if ( contentElement instanceof HTMLElement ) {
194
+ const savedSelection = saveCaretPosition ( contentElement ) ;
195
+
196
+ const parts = text
197
+ . split ( / ( \s + ) / )
198
+ . map ( ( word , index ) => {
199
+ const suggestion = suggestions . find ( ( s ) => s . token === word && ! s . known ) ;
200
+ if ( suggestion ) {
201
+ return `<span key=${ index } class="misspelled">${ word } </span>` ;
202
+ } else {
203
+ return `<span key=${ index } class="correct">${ word } </span>` ;
204
+ }
205
+ } )
206
+ . join ( '' ) ;
207
+
208
+ contentElement . innerHTML = parts ;
209
+ const misspelledElements = contentElement . querySelectorAll ( '.misspelled' ) ;
210
+ misspelledElements . forEach ( ( element ) => {
211
+ const word = element . textContent || '' ;
212
+ const eventHandler = ( e : Event ) => handleWordClick ( word , e as MouseEvent | TouchEvent ) ;
213
+ element . addEventListener ( 'click' , eventHandler ) ;
214
+ element . addEventListener ( 'touchstart' , eventHandler ) ;
215
+ } ) ;
216
+ restoreCaretPosition ( contentElement , savedSelection ) ;
217
+ }
218
+ } ;
220
219
221
220
const applySuggestion = ( suggestion : string ) => {
222
221
if ( ! selectedWord ) return ;
@@ -227,8 +226,8 @@ const SpellCheckForm = ({
227
226
228
227
setText ( updatedText ) ;
229
228
setSelectedWord ( null ) ;
230
- console . log ( updatedText ) ;
231
- renderHighlightedText ( updatedText ) ;
229
+ setSuggestionPosition ( null ) ;
230
+ renderHighlightedText ( updatedText , suggestions ) ;
232
231
} ;
233
232
234
233
return (
@@ -252,19 +251,16 @@ const SpellCheckForm = ({
252
251
< Form . Label className = "col-md-2 col-lg-1 col-form-label text-md-right" > { t ( 'Input_Text' ) } </ Form . Label >
253
252
< Col md = "10" >
254
253
< div
254
+ aria-label = "Enter text for spell checking"
255
255
className = "content-editable"
256
256
contentEditable
257
257
onInput = { handleInput }
258
258
onKeyDown = { ( event : React . KeyboardEvent < HTMLDivElement > ) => {
259
259
if ( event . code === 'Enter' && ! event . shiftKey ) {
260
260
event . preventDefault ( ) ;
261
261
handleSubmit ( ) ;
262
- }
263
- else if ( event . code === 'Tab' || event . code === ' ' ) {
264
- event . preventDefault ( ) ;
265
262
}
266
263
} }
267
- placeholder = { t ( 'Spell_Checking_Help' ) }
268
264
ref = { spellcheckRef }
269
265
role = "textbox"
270
266
tabIndex = { 0 }
@@ -285,18 +281,19 @@ const SpellCheckForm = ({
285
281
>
286
282
{ suggestions
287
283
. find ( ( s ) => s . token === selectedWord )
288
- ?. sugg . map ( ( [ sugg ] , index ) => (
284
+ ?. sugg . map ( ( suggestion , index ) => (
289
285
< div
286
+ className = "suggestion"
290
287
key = { index }
291
- onClick = { ( ) => applySuggestion ( sugg ) }
288
+ onClick = { ( ) => applySuggestion ( suggestion ) }
292
289
onKeyDown = { ( event ) => {
293
290
if ( event . key === 'Enter' ) {
294
- applySuggestion ( sugg ) ;
291
+ applySuggestion ( suggestion ) ;
295
292
}
296
293
} }
297
294
role = "presentation"
298
295
>
299
- { sugg }
296
+ { suggestion }
300
297
</ div >
301
298
) ) }
302
299
</ div >
0 commit comments