如何在Draft.js中使用策略功能Decorator?

时间:2018-09-29 01:54:17

标签: javascript arrays reactjs draftjs draft-js-plugins

我一直在Draft.js中与Decorators一起玩,但是当我无法构建自定义的 我正在将文本发送到API,我将得到一个响应,其中包含在编辑器中键入的不正确单词数组。因此,我试图构建一个将样式应用于该数组中所有项目的Strategory函数

 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);
    }  
  }

我想念它无法正常工作吗?

1 个答案:

答案 0 :(得分:1)

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);
      }
    })
  }

您想做这样的事情吗?