index.tsx
2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// https://www.yuque.com/braft-editor/be/lzwpnr#ecff77a8 富文本编辑器 简介说明
import React, { ReactElement } from 'react';
// 引入编辑器组件
import BraftEditor, { BraftEditorProps, EditorState } from 'braft-editor';
import 'braft-editor/dist/index.css';
import cx from 'classnames';
import './index.less';
import { FormItem } from '@formily/antd-v5';
import { Field } from '@formily/react';
import { IFieldProps } from '@formily/core';
interface Props extends BraftEditorProps {
contentStyle?: { [props: string]: string | number };
changeEditor?: (editorState: EditorState) => void;
}
const Editor: React.FC<Props> = (props) => {
const {
value = null,
changeEditor,
controls = [],
excludeControls = [],
extendControls = [],
contentStyle = { width: 600, height: 120 },
placeholder,
} = props;
const { width, height } = contentStyle;
let myContentStyle = contentStyle
? contentStyle
: {
height: '120px',
fontSize: '14px',
};
return (
<div
className={cx('editor')}
style={{
width,
height,
}}
>
<BraftEditor
value={BraftEditor.createEditorState(value)}
stripPastedStyles={true}
contentStyle={myContentStyle}
onBlur={(event: EditorState) => {
if (changeEditor) {
changeEditor(event.toHTML());
}
}}
placeholder={placeholder}
controls={controls} //控件列表
excludeControls={excludeControls} //不展示的 控件列表
extendControls={extendControls} //自定义控件
/>
</div>
);
};
interface P extends IFieldProps {
name: string | [string, string];
title?: string | ReactElement;
validator?: any;
[props: string]: any;
decoratorProps?: {
[props: string]: any;
};
componentProps?: {
[props: string]: any;
};
}
const BraftDeitor: React.FC<P> = (props) => {
const { name, title, validator, decoratorProps, componentProps } = props;
return (
<div className={cx('editor_warp')}>
<Field
name={name}
title={title}
validator={validator}
decorator={[FormItem, decoratorProps]}
component={[Editor, componentProps]}
/>
</div>
);
};
export default BraftDeitor;