【发布时间】:2021-12-20 12:57:45
【问题描述】:
如果在富文本编辑器中单击 ? 工具栏项,我想添加新的自定义 HTML 标记,例如 <h1></h1>。但是,在onClickHelp 函数中单击自定义工具栏项时,我无法获取最新值。
在 RichTextEditor.tsx 中,
import '../../node_modules/@syncfusion/ej2-icons/styles/material.css';
import '../../node_modules/@syncfusion/ej2-buttons/styles/material.css';
import '../../node_modules/@syncfusion/ej2-splitbuttons/styles/material.css';
import '../../node_modules/@syncfusion/ej2-inputs/styles/material.css';
import '../../node_modules/@syncfusion/ej2-lists/styles/material.css';
import '../../node_modules/@syncfusion/ej2-navigations/styles/material.css';
import '../../node_modules/@syncfusion/ej2-popups/styles/material.css';
import '../../node_modules/@syncfusion/ej2-richtexteditor/styles/material.css';
import {
Count,
HtmlEditor,
Inject,
Image,
Link,
QuickToolbar,
RichTextEditorComponent,
Toolbar,
ToolbarSettingsModel,
ToolbarType
} from '@syncfusion/ej2-react-richtexteditor';
import { FC, useRef, useState } from 'react';
interface RichTextEditorProps {
value?: string;
onChange?: (value: string) => void;
}
const RichTextEditor: FC<RichTextEditorProps> = ({
value,
onChange
}) => {
const editorRef = useRef<RichTextEditorComponent>(null);
const onClickHelp = () => {
console.log(value);
};
const toolbarSettings: ToolbarSettingsModel = {
type: ToolbarType.MultiRow,
items: [{
template:
'<button type="button" class="e-tbar-btn e-btn" tabindex="-1" id="custom_tbar_help" style="width:100%"><div class="e-tbar-btn-text" style="font-weight: 800;"> ?</div></button>',
undo: true,
click: onClickHelp,
tooltipText: 'Help'
}, 'Bold', 'Italic', 'Underline', 'StrikeThrough', 'FontName', 'FontSize', 'FontColor', 'BackgroundColor', 'LowerCase', 'UpperCase', '|', 'Formats', 'Alignments', 'OrderedList', 'UnorderedList', 'Outdent', 'Indent', '|', 'CreateLink', 'Image', '|', 'ClearFormat', 'SourceCode', '|', 'NumberFormatList', 'BulletFormatList', 'Undo', 'Redo'
],
};
return (
<RichTextEditorComponent
ref={editorRef}
value={value}
toolbarSettings={toolbarSettings}
maxLength={30000}
height={400}
saveInterval={1}
showCharCount
iframeSettings={{ enable: true }}
change={(e) => {
onChange(e?.value);
}}
>
<Inject services={[Link, Image, HtmlEditor, Toolbar, QuickToolbar, Count]} />
</RichTextEditorComponent>
);
};
RichTextEditor.defaultProps = {
value: ''
};
export default RichTextEditor;
来自rich text editor 和example 的参考
【问题讨论】: