Ho un pulsante contrassegno sull'interfaccia utente, facendo clic su, ogni selezione dell'utente è contrassegnata in rosso. Nessun problema qui. Lo raggiungo entrodocument.execCommand("insertHTML")
Ma ho un requisito aggiuntivo che se viene creata la nuova selezione che è l'intersezione dei segni di selezione vecchi, il segno rosso di selezione vecchia dovrebbe scomparire.
Come esempio:
Nell'immagine seguente: questo e i test sono contrassegnati. Ora, se seleziono il suo is tes dal segno di inizio e clic, i vecchi segni di questo e il test dovrebbero andare via e solo il suo è tes dovrebbe essere segnato perché c'è un incrocio.
codice:
const button = document.getElementById("button");
button.addEventListener('click', ()=>{
const s = window.getSelection();
const selectionStr = s.toString();
document.execCommand("insertHTML", false, `<span class="bg-red">${selectionStr}<span>`);
})
.bg-red {
background: red;
}
<div contenteditable="true">
this is testing this is testing this is testing
</div>
<button id="button">mark</button>
