123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- import '@wangeditor/editor/dist/css/style.css'; // 引入 css
- import { useState, useEffect } from 'react';
- import { Editor, Toolbar } from '@wangeditor/editor-for-react';
- import type { IDomEditor, IEditorConfig, IToolbarConfig } from '@wangeditor/editor';
- function MyEditor(props: any) {
- const { data } = props;
- // editor 实例
- const [editor, setEditor] = useState<IDomEditor | null>(null);
- // 编辑器内容
- const [html, setHtml] = useState('');
- useEffect(() => {
- setHtml(data);
- }, []);
- // 工具栏配置
- const toolbarConfig: Partial<IToolbarConfig> = {};
- // 编辑器配置
- const editorConfig: Partial<IEditorConfig> = {
- placeholder: '请输入内容...',
- };
- // 及时销毁 editor ,重要!
- useEffect(() => {
- return () => {
- if (editor == null) return;
- editor.destroy();
- setEditor(null);
- };
- }, [editor]);
- return (
- <>
- <div style={{ border: '1px solid #ccc', zIndex: 100 }}>
- <Toolbar
- editor={editor}
- defaultConfig={toolbarConfig}
- mode="default"
- style={{ borderBottom: '1px solid #ccc' }}
- />
- <Editor
- defaultConfig={editorConfig}
- value={html}
- onCreated={setEditor}
- onChange={(el) => setHtml(el.getHtml())}
- mode="default"
- style={{ height: '300px', overflowY: 'hidden' }}
- />
- </div>
- </>
- );
- }
- export default MyEditor;
|