我正在尝试在我的站点中实现Syncfusion富文本编辑器reactjs小部件。我将demo code稍微修改为一个独立的类,如下所示:
import { render } from 'react-dom';
import React, { Component } from 'react';
import { addClass, removeClass, Browser } from '@syncfusion/ej2-base';
import { RichTextEditorComponent, Toolbar, Inject, Image, Link, HtmlEditor, Count, QuickToolbar, Table } from '@syncfusion/ej2-react-richtexteditor';
import { createElement } from '@syncfusion/ej2-base';
import * as CodeMirror from 'codemirror';
import 'codemirror/mode/javascript/javascript';
import 'codemirror/mode/css/css.js';
import 'codemirror/mode/htmlmixed/htmlmixed.js';
class RichTextEd extends Component {
constructor(props) {
super(props);
// Rich Text Editor items list
this.items = ['Bold', 'Italic', 'Underline', 'StrikeThrough',
'FontName', 'FontSize', 'FontColor', 'BackgroundColor',
'LowerCase', 'UpperCase', '|',
'Formats', 'Alignments', 'OrderedList', 'UnorderedList',
'Outdent', 'Indent', 'SuperScript', 'SubScript', '|',
'CreateTable', 'CreateLink', 'Image', '|', 'ClearFormat', 'Print',
'SourceCode', 'FullScreen', '|', 'Undo', 'Redo'
];
//Rich Text Editor ToolbarSettings
this.toolbarSettings = {
items: this.items
};
}
mirrorConversion(e) {
this.textArea = this.rteObj.contentModule.getEditPanel();
let id = this.rteObj.getID() + 'mirror-view';
let mirrorView = this.rteObj.element.querySelector('#' + id);
let charCount = this.rteObj.element.querySelector('.e-rte-character-count');
if (e.targetItem === 'Preview') {
this.textArea.style.display = 'block';
mirrorView.style.display = 'none';
this.textArea.innerHTML = this.myCodeMirror.getValue();
charCount.style.display = 'block';
}
else {
if (!mirrorView) {
mirrorView = createElement('div', { className: 'e-content' });
mirrorView.id = id;
this.textArea.parentNode.appendChild(mirrorView);
}
else {
mirrorView.innerHTML = '';
}
this.textArea.style.display = 'none';
mirrorView.style.display = 'block';
this.renderCodeMirror(mirrorView, this.rteObj.value);
charCount.style.display = 'none';
}
}
renderCodeMirror(mirrorView, content) {
this.myCodeMirror = CodeMirror(mirrorView, {
value: content,
lineNumbers: true,
mode: 'text/html',
lineWrapping: true,
});
}
handleFullScreen(e) {
let sbCntEle = document.querySelector('.sb-content.e-view');
let sbHdrEle = document.querySelector('.sb-header.e-view');
let leftBar;
let transformElement;
if (Browser.isDevice) {
leftBar = document.querySelector('#right-sidebar');
transformElement = document.querySelector('.sample-browser.e-view.e-content-animation');
}
else {
leftBar = document.querySelector('#left-sidebar');
transformElement = document.querySelector('#right-pane');
}
if (e.targetItem === 'Maximize') {
if (Browser.isDevice && Browser.isIos) {
addClass([sbCntEle, sbHdrEle], ['hide-header']);
}
addClass([leftBar], ['e-close']);
removeClass([leftBar], ['e-open']);
if (!Browser.isDevice) {
transformElement.style.marginLeft = '0px';
}
transformElement.style.transform = 'inherit';
}
else if (e.targetItem === 'Minimize') {
if (Browser.isDevice && Browser.isIos) {
removeClass([sbCntEle, sbHdrEle], ['hide-header']);
}
removeClass([leftBar], ['e-close']);
if (!Browser.isDevice) {
addClass([leftBar], ['e-open']);
transformElement.style.marginLeft = leftBar.offsetWidth + 'px';
}
transformElement.style.transform = 'translateX(0px)';
}
}
actionCompleteHandler(e) {
if (e.targetItem && (e.targetItem === 'SourceCode' || e.targetItem === 'Preview')) {
this.rteObj.sourceCodeModule.getPanel().style.display = 'none';
this.mirrorConversion(e);
}
else {
setTimeout(() => { this.rteObj.toolbarModule.refreshToolbarOverflow(); }, 400);
}
}
render() {
return (<div className='control-pane'>
<div className='control-section' id="rteTools">
<div className='rte-control-section'>
<RichTextEditorComponent id="toolsRTE" ref={(richtexteditor) => { this.rteObj = richtexteditor; }} showCharCount={true} actionBegin={this.handleFullScreen.bind(this)} actionComplete={this.actionCompleteHandler.bind(this)} maxLength={2000} toolbarSettings={this.toolbarSettings}>
<div>
<p>The Rich Text Editor is WYSIWYG ("what you see is what you get") editor useful to create and edit content, and return the valid <a href='https://ej2.syncfusion.com/home/' target='_blank'>HTML markup</a> or <a href='https://ej2.syncfusion.com/home/' target='_blank'>markdown</a> of the content</p> <p><b>Toolbar</b></p><ol><li> <p>Toolbar contains commands to align the text, insert link, insert image, insert list, undo/redo operations, HTML view, etc</p></li><li><p>Toolbar is fully customizable </p></li></ol> <p><b>Links</b></p><ol><li><p>You can insert a hyperlink with its corresponding dialog </p></li><li><p>Attach a hyperlink to the displayed text. </p></li><li><p>Customize the quick toolbar based on the hyperlink </p> </li></ol><p><b>Image.</b></p><ol><li><p>Allows you to insert images from an online source as well as the local computer </p> </li><li><p>You can upload an image</p></li><li><p>Provides an option to customize quick toolbar for an image</p></li></ol><img alt="Logo" src="https://ej2.syncfusion.com/react/demos/src/rich-text-editor/images/RTEImage-Feather.png" style={{ width: '300px' }}/>
</div>
<Inject services={[Toolbar, Image, Link, HtmlEditor, Count, QuickToolbar, Table]}/>
</RichTextEditorComponent>
</div>
</div>
</div>);
}
}
export default RichTextEd;然后我在我的组件中使用它
<RichTextEd />编辑器没有按照预期呈现,并且报告了一个关于在严格模式下使用ref的错误:
Warning: findDOMNode is deprecated in StrictMode. findDOMNode was passed an instance of RichTextEditorComponent which is inside StrictMode. Instead, add a ref directly to the element you want to reference.我想我知道问题出在render方法中设置ref的方式上,但我不清楚this.rteObj最初是在哪里定义的,以及如何让它在父组件中像预期的那样工作。
发布于 2020-09-11 15:08:07
来自Syncfusion支持部门的问候,
我们已经尝试了从父组件呈现的RichTextEditor的共享代码块,它工作得很好,我们没有遇到任何问题。你能检查一下下面的工作示例吗?
示例:https://stackblitz.com/edit/react-render-parent-component?file=index.js
https://stackoverflow.com/questions/63832285
复制相似问题