我使用blockRenderMap在编辑时给出了小文本的选项。默认情况下,react rte不支持它。这就是要这样做的代码:
import React, {Component} from 'react';
import RichTextEditor from 'react-rte';
import Immutable from 'immutable'
import Draft from 'draft-js'
const blockRenderMap = Immutable.Map({
'small': {
element: 'small'
}
});
// Include 'paragraph' as a valid block and updated the unstyled element but
// keep support for other draft default block types
const extendedBlockRenderMap = Draft.DefaultDraftBlockRenderMap.merge(blockRenderMap);
class MyStatefulEditor extends Component {
state = {
value: RichTextEditor.createEmptyValue()
}
onChange = (value) => {
console.log(value.toString('html'))
this.setState({value});
if (this.props.onChange) {
// Send the changes up to the parent component as an HTML string.
// This is here to demonstrate using `.toString()` but in a real app it
// would be better to avoid generating a string on each change.
this.props.onChange(
value.toString('html')
);
}
};
render ()
{
const toolbarConfig = {
// Optionally specify the groups to display (displayed in the order listed).
display: ['INLINE_STYLE_BUTTONS', 'BLOCK_TYPE_BUTTONS', 'LINK_BUTTONS', 'BLOCK_TYPE_DROPDOWN', 'HISTORY_BUTTONS'],
INLINE_STYLE_BUTTONS: [
{label: 'Bold', style: 'BOLD', className: 'custom-css-class'},
{label: 'Italic', style: 'ITALIC'},
{label: 'Underline', style: 'UNDERLINE'}
],
BLOCK_TYPE_DROPDOWN: [
{label : 'Small', style: 'small'},
{label: 'Normal', style: 'unstyled'},
{label: 'Heading Large', style: 'header-one'},
{label: 'Heading Medium', style: 'header-two'},
{label: 'Heading Small', style: 'header-three'}
],
BLOCK_TYPE_BUTTONS: [
{label: 'UL', style: 'unordered-list-item'},
{label: 'OL', style: 'ordered-list-item'}
]
};
return (
<RichTextEditor
value={this.state.value}
onChange={this.onChange}
toolbarConfig={toolbarConfig}
blockRenderMap={extendedBlockRenderMap}
/>
);
}
}
export default MyStatefulEditor;
在使用编辑器时,这样做很好。
但是在onChange
函数中,如果我打印包含editorValue的"value“变量,它会显示如下所示:
<p>asdas</p>
而不是:
<small>asdas</small>
同时,如果我选择一个默认的受支持的选项,比如标题大:
我把value
打印在onChange
里面,我看到了这个:
<h1>asdas</h1>
为什么我的blockRenderMap
不能工作,而默认的工作呢?对于适用的情况,如何使value
变量包含预期的small
标记而不是p
标记?
发布于 2022-03-14 15:58:17
这里有很多斗争
<p>
的内容。我真的希望blockRenderMap的扩展能解决所有问题,但是没有。<section>
作为样式,然后尝试向它应用一个CSS类。但默认情况下,部分不可用。即使您将它指定为自定义blockRenderMap,它也会在value
变量中将其转换为<p>
。因此,我最终修改了<h4>
。溶液
编辑期间:
.rte-small-font {
font-size: smaller;
font-weight:normal;
display:block;
margin:0;
padding:0;
}
<h4>
(因为我们没有使用它)。CSS类基本上是将<h4>
转换为<small>
。BLOCK_TYPE_DROPDOWN
:{label : 'Small', style: 'header-four'},
中添加此项然后,当onChange()
被调用时(基本上是“保存”编辑的文本):
onChange()
中创建一个新字符串,方法是将<h4>
替换为<small>
:let newString = value.toString('html').replace("<h4>", "<small>").replace("</h4>", "</small>");
https://stackoverflow.com/questions/71439774
复制相似问题