I am attepting to make a text area where I can manually mark words, or sets of words, as either a spelling or grammatical error. in order to do this I switched from a plain <textarea /> to a <div contenteditable /> so that I have full control on how to render the text inside. Now my question is, the way I mark spelling and grammer errrors in with an array of index ranges ([number, number][]).
right now I do the 'rendering' with this code
const html = createMemo(() => {
return value().split('').map((letter, index) => {
const spellingOpen = spellingErrors().some(([start]) => start === index) ? `<span class="${css.spellingError}">` : '';
const spellingClose = spellingErrors().some(([, end]) => end === index) ? `</span>` : '';
const grammarOpen = grammarErrors().some(([start]) => start === index) ? `<span class="${css.grammarError}">` : '';
const grammarClose = grammarErrors().some(([, end]) => end === index) ? `</span>` : '';
return `${grammarOpen}${spellingOpen}${letter}${spellingClose}${grammarClose}`;
}).join('');
});
return <div
contentEditable
innerHTML={html()}
/>;
I however really dislike having to do manual string manipulation combined with the innterHTML. So the advice I would like to get. or otherwise brainstorming I would like to do. Is, how can I do this is a more solid-esc / jsx-esc way, so that I don't need to do manual string manipulation (and hopefully then also no longer having to rerender the content on every edit which resets the cursor to the start of the input).