Every word within a div

3

I am implementing a spellcheker (corrector of words), I have a container where each new written word is placed in a new div, instead of placing it as ordinary text.

I am putting every word on a div, so that word suggestions are displayed, is this necessary? or can I put it in span? or on any label?

With div I managed the display of suggestions, when I try to span it does NOT work .

To see the functionality, type any word and press the space bar

let written = '';
document.getElementById('parent').addEventListener('keypress', function(e) {

  written += String.fromCharCode(e.which);

  if (e.which === 32) {
    let newDivWord = document.createElement('div');

    newDivWord.appendChild(document.createTextNode(written));
    newDivWord.classList.add('root');
    newDivWord.id = document.getElementById('parent').childElementCount + 1;
    document.getElementById('parent').appendChild(newDivWord);
    console.log(document.getElementById('parent'));
    written = "";
  }
})
#parent>div {
  border-color: yellow;
  float: left;
}

.root {
  padding-right: 2px;
}
<div id="parent" contenteditable=true style="border: black 2px solid; height:200px">
  <div class="root" id="1">Qué</div>
  <div class="root" id="2">es</div>
  <div class="root" id="3">Lorem</div>
  <div class="root" id="4">Ipsum</div>
</div>

Should the written text that is not within the new div be erased?

The future functionality of my problem would be: , but if you help me, I would thank you infinitely.

The text What is Lorem Ipsum if I type 'hello' before lorem should be 'What is hello Lorem Ipsum', in a div of its own.

The text What is Lorem Ipsum if I type 'hello' after Ipsum should be 'What is Lorem Ipsum hello', on a div.

The text What is Lorem Ipsum if I type 'hello' before 'QUE' should be 'hello What is Lorem Ipsum hello', on a div of its own.

The text What is Lorem Ipsum if I type 'hello' before 'sum' should be 'hello What is Lorem Iphola sum hello', a div would be created for Iphola and sum would be another div.

What I need now in the present: I want the new word not to be repeated, one of them is the writing of the normal text, and the other repetition is the creation of the new div, and should add the new div at the end.

    
asked by x-rw 25.06.2018 в 02:55
source

2 answers

2

One thing you can do is use getSelection() to get the active text node, and from there edit its content (with getSelection().focusNode().textContent ).

For example, the following demo removes the word that was inserted, but it has a couple of problems: 1) leave the space blank (I think it's because of the type of event, but I've done tests with keyup and it fails , I have to see why it is); and 2) move the cursor to the beginning of the text node (it may not be a problem if the word is inserted at the beginning, but it is somewhat inconvenient if it is at the end).

I leave it in case it serves as your base while I try to improve it:

let written = '';
document.getElementById('parent').addEventListener('keypress', function(e) {

  // guardamos la palabra
  var palabra = written;
  written += String.fromCharCode(e.which);

  if (e.which === 32) {
  
    // seleccionamos el nodo activo
    var sel = window.getSelection();
    // eliminamos la palabra recien escrita
    sel.focusNode.textContent = sel.focusNode.textContent.replace(palabra, "");
    
    let newDivWord = document.createElement('div');

    newDivWord.appendChild(document.createTextNode(written));
    newDivWord.classList.add('root');
    newDivWord.id = document.getElementById('parent').childElementCount + 1;
    document.getElementById('parent').appendChild(newDivWord);
    console.log(document.getElementById('parent'));
    written = "";
  }
})
#parent>div {
  border-color: yellow;
  float: left;
}

.root {
  padding-right: 2px;
}
<div id="parent" contenteditable=true style="border: black 2px solid; height:200px">
  <div class="root" id="1">Qué</div>
  <div class="root" id="2">es</div>
  <div class="root" id="3">Lorem</div>
  <div class="root" id="4">Ipsum</div>
</div>
    
answered by 25.06.2018 / 05:30
source
0

I just made a validation by executing your code, because I have a similar need to create an element that allows me to dynamically add families of keywords.
I noticed that if I try to edit an existing word, it is replaced and additionally the changes are added at the end. I guess it's not the desired behavior .

  

Initial text: What is Loren Ipsum
  edited as: Qu eda como Loren Ipsum
  The container shows: Quda as it is Loren Ipsum gives as
  And the cursor stays inside the first container,

With dev tools I inspected the elements and found that the Quda como chain remains on the first div and two new ones are added at the end < div class="root" > da < / div > and < div class="root" > as < / div > So your code needs to address the possibility that existing containers will be edited and eventually be edited between two of them.

As for your question, I think that between containers there can only be spaces and punctuation characters (separators) since your interest, I suppose, should be to catch the words. So that if any free text were to remain, it should be inserted in a container instead of being deleted.

    
answered by 25.06.2018 в 04:22