Skip to content

如何替换选中的文字 #995

Description

@xqGosky

如果替换富文本种选中的文字,无法使用正则替换,因为需要沿用选中文字的样式,在draft-js是有个Modifier函数,可以实现我的需求,但是draft-jsbraft-editor同时安装的话会有报错,具体问题其他issues已经提过了,以下是我的获取选中文字的方法

// 判断富文本内有没有内容
const editorState = form.getFieldValue('richText');
if (!editorState || !editorState.getCurrentContent().hasText()) {
  console.log('富文本内没有内容');
  return;
}

// 判断是否有选中文字

const selection = editorState.getSelection();
if (selection.isCollapsed()) {
  console.log('没有选中文字');
  return;
}
//获取选中文本并保存到state
const selectedText = editorState.getCurrentContent().getPlainText().substring(
  selection.getStartOffset(),
  selection.getEndOffset(),
);
console.log('selectedText', selectedText);
setSelectedText(selectedText);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions