Search code examples
javascriptarraysreactjsdraftjsdraft-js-plugins

How to use strategy function Decorator in Draft.js?


I've been playing with the Decorators in Draft.js but when I could not build a custom one I am sending the text to an API which I will get a response with an array of incorrect words that typed in the editor So I am trying to build a Strategory function that applies a style to all the items in that array

 function highlightWorngWords(contentBlock, callback) {
    let text = contentBlock.getText();
    let worngWords = ['word', 'word1' , 'word2'];
    while ( worngWords  !== null ) {
      start = worngWords.index;
      callback(start, start + worngWords[0].length);
    }  
  }

What did I miss it won't work?


Solution

  • function highlightWorngWords(contentBlock, callback) {
        let text = contentBlock.getText();
        let worngWords = ['word', 'word1' , 'word2'];
        worngWords.forEach(word => {
          start = text.indexOf(word);
          if (start !== -1) {
             callback(start, start + word.length);
          }
        })
      }
    

    Are you wanting to do something like this?