我为 react-draft-wysiwyg 提供的编辑器创建了两个自定义按钮:一个引用按钮和一个水平规则按钮。
以下代码在编辑器状态更新时运行:
handleChange = editorState => {
const { onChange } = this.props;
const html = draftToHtml(convertToRaw(editorState.getCurrentContent()));
onChange("content")({ target: { value: html } });
this.setState({ editorState });
};
但是,在将当前内容转换为 HTML 时,引用的文本和 hr 标签都不会被转换。它们只是在 HTML 中显示为空的 p 标记。可以做些什么来使自定义内容转换为 HTML?(我已确认 H1、H4、Bold 等默认按钮功能正常)
更多信息,这里是 blockStyle 和 blockRenderer 函数,以及 Editor 组件:
blockStyle = contentBlock => {
const type = contentBlock.getType();
if (type === "quote") {
// css using tailwindcss
return "mb-4 p-3 bg-secondary-50 rounded";
}
};
blockRenderer = contentBlock => {
const { editorState } = this.state;
const type = contentBlock.getType();
if (type === "atomic") {
const contentState = editorState.getCurrentContent();
const entityKey = contentBlock.getEntityAt(0);
const entity = contentState.getEntity(entityKey);
if (entity && entity.type === "HORIZONTAL_RULE") {
return {
component: () => { return <hr /> },
editable: false
};
}
}
};
render() {
const { editorState } = this.state;
return (
<Editor
editorState={editorState}
toolbarClassName="toolbarClassName"
wrapperClassName="wrapperClassName"
editorClassName="editorClassName"
onEditorStateChange={this.handleChange}
toolbar={TOOLBAR}
toolbarOnFocus
handlePastedText={() => false}
toolbarCustomButtons={[<Blockquote key="quote" />, <HRDivider key="hr" />]}
blockStyleFn={this.blockStyle}
customBlockRenderFunc={this.blockRenderer}
/>
);
}