我正在使用Draft.js为我的React应用程序引入一个文本编辑器。我已经使其适用于粗体、斜体和下划线,但我不知道如何将文本更改为项目符号。我已经阅读了文档,但我找不到任何有用的东西。有人能帮帮忙吗?
发布于 2016-09-14 18:41:27
您可以使用draft-js的RichUtils将任何文本块转换为项目符号。下面是你怎么做的:
// button to turn text block to bullet points
<button onClick={this.toggleBulletPoints}>Bullet points</button>
toggleBulletPoints(){
this.setState({
editorState: RichUtils.toggleBlockType(
this.state.editorState,
'unordered-list-item'
)
})
}下面是在draft-js编辑器中更改内联样式和块类型的完整示例:https://github.com/facebook/draft-js/blob/master/examples/draft-0-10-0/rich/rich.html
发布于 2019-08-14 04:45:59
我只想留下一个评论,但我没有足够的因果报应...
在标记为正确的答案中,我不确定这是如何工作的。看起来状态设置不正确。如果不是这样设置的:
<button onClick={this.toggleBulletPoints}>Bullet points</button>
toggleBulletPoints(){
this.setState({
editorState: RichUtils.toggleBlockType(
this.state.editorState,
'unordered-list-item'
)
})
}我不认为你可以直接将一个函数的输出保存到state中,而不定义它的键。至少,当我尝试标记为正确的答案时,它对我不起作用。
此外,由于这是一年前更新的,因此这里有一个更新的可能解决方案:
constructor(props) {
super(props);
this.state = {
editorState: EditorState.createEmpty()
};
}
onChange = (editorState) => {
this.setState({
editorState
});
};
toggleBlockType = () => {
this.onChange(
RichUtils.toggleBlockType(this.state.editorSection, 'unordered-list-item')
);
};
render() {
return (
<div>
<Editor
editorState={this.state.editorState}
onChange={this.onChange}
/>
</div>
)
}希望这对某些人有帮助!
https://stackoverflow.com/questions/39293846
复制相似问题