首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Draft.js -如何修剪内容

Draft.js -如何修剪内容
EN

Stack Overflow用户
提问于 2017-10-18 04:30:20
回答 3查看 2.7K关注 0票数 2

如何从用Draft.js生成的内容的两端修剪空格?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-10-18 09:04:49

也许存在一个更优雅的解决方案,但当我面临同样的问题时,我用以下代码解决了这个问题:

代码语言:javascript
复制
if(typeof String.prototype.trimLeft !== 'function') {
    String.prototype.trimLeft = function() {
        return this.replace(/^\s+/,"");
    }
}

if(typeof String.prototype.trimRight !== 'function') {
    String.prototype.trimRight = function() {
        return this.replace(/\s+$/,"");
    }
}

这里,我为没有这些方法的浏览器添加了trimLefttrimRight方法。

代码语言:javascript
复制
trimContent = () => {
  const editorState = this.state.editorState;
  let currentContent = this.state.editorState.getCurrentContent();
  const firstBlock = currentContent.getBlockMap().first();
  const lastBlock = currentContent.getBlockMap().last();
  const firstBlockKey = firstBlock.getKey();
  const lastBlockKey = lastBlock.getKey();
  const firstAndLastBlockIsTheSame = firstBlockKey === lastBlockKey;

  const textStart = firstBlock.getText()
  const trimmedTextStart = textStart.trimLeft();
  const lengthOfTrimmedCharsStart = textStart.length - trimmedTextStart.length;

  let newSelection = new SelectionState({
    anchorKey: firstBlockKey,
    anchorOffset: 0,
    focusKey: firstBlockKey,
    focusOffset: lengthOfTrimmedCharsStart
  });

  currentContent = Modifier.replaceText(
    currentContent,
    newSelection,
    '',
  )

  let newEditorState = EditorState.push(
    editorState,
    currentContent,
  )

  let offset = 0;

  if (firstAndLastBlockIsTheSame) {
    offset = lengthOfTrimmedCharsStart
  }

  const textEnd = lastBlock.getText()
  const trimmedTextEnd = textEnd.trimRight();
  const lengthOfTrimmedCharsEnd = textEnd.length - trimmedTextEnd.length

  newSelection = new SelectionState({
    anchorKey: lastBlockKey,
    anchorOffset: trimmedTextEnd.length - offset,
    focusKey: lastBlockKey,
    focusOffset: textEnd.length - offset
  });

  currentContent = Modifier.replaceText(
    currentContent,
    newSelection,
    '',
  )

  newEditorState = EditorState.push(
    editorState,
    currentContent,
  )

  this._handleChange(newEditorState);
}

trimContent方法--在这里,我使用Modifier.replaceText util删除空格字符。工作示例- https://jsfiddle.net/p5532ddw/

票数 1
EN

Stack Overflow用户

发布于 2019-11-15 08:59:33

我有一个多行的Draft.js输入,我想从每一行的两端修剪空格。

我在上面修改了Mikhail的解决方案,并创建了这样一个函数:

代码语言:javascript
复制
const trimEditorState = (currentEditorState) => {
  const currentContent = currentEditorState.getCurrentContent()
  const newContent = currentContent.getBlockMap().reduce((accumulator, block) => {
    const key = block.getKey()
    const text = block.getText()
    const trimmedLeft = text.trimLeft()
    const trimmedRight = text.trimRight()
    const offset = text.length - trimmedLeft.length

    const textToReplaceLeft = new SelectionState({
      anchorKey: key,
      focusKey: key,
      anchorOffset: 0,
      focusOffset: offset
    })

    const leftTrimmedContent = Modifier.replaceText(accumulator, textToReplaceLeft, '')

    const textToReplacRight = new SelectionState({
      anchorKey: key,
      focusKey: key,
      anchorOffset: trimmedRight.length - offset,
      focusOffset: text.length - offset
    })

    return Modifier.replaceText(leftTrimmedContent, textToReplacRight, '')
  }, currentContent)

  return EditorState.push(currentEditorState, newContent, 'remove-range')
}

它是一个没有副作用的纯函数:它接受当前的EditorState并返回修改后的、经过修剪的EditorState。希望这对外面的人有帮助!

票数 1
EN

Stack Overflow用户

发布于 2018-12-07 10:26:16

获取原始数据,然后开始黑客攻击

代码语言:javascript
复制
editorRawData = convertToRaw(contentState);
editorRawData.blocks = editorRawData.blocks.filter(el => el.text);
editorRawData.blocks = editorRawData.blocks.map(el => {
    const indexOfFirstChar = el.text.search(/\S/);
    if (indexOfFirstChar > 0) {
        el.text = el.text.slice(indexOfFirstChar, el.text.length);
        el.entityRanges[0].offset = el.entityRanges[0].offset - indexOfFirstChar;
    }
    return el;
});
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46802855

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档