index.js
2.88 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
93
94
95
96
97
98
99
100
101
/* eslint-disable */
import React, { Component } from 'react';
// import '@ant-design/compatible/assets/index.css';
import AntdDraggableModal from '@/components/Common/AntdDraggableModal';
import BraftEditor from 'braft-editor'
import 'braft-editor/dist/index.css'
export default class EditorModal extends Component {
/** 构造函数 */
constructor(props) {
super(props);
this.controls = [
'undo', 'redo', 'separator',
'font-size', 'line-height', 'letter-spacing', 'separator',
'text-color', 'bold', 'italic', 'underline', 'strike-through', 'separator',
'superscript', 'subscript', 'remove-styles', 'separator', 'text-indent', 'text-align', 'separator',
'headings', 'list-ul', 'list-ol', 'blockquote', 'code', 'separator',
'link', 'separator', 'hr', 'separator',
'separator',
'clear', 'media',
]
this.state = {
editorState: BraftEditor.createEditorState(null)
};
}
componentWillReceiveProps(nextProps) {
if (nextProps.curEditorProps) {
if (nextProps.curEditorProps.value) {
this.setState({
editorState: BraftEditor.createEditorState(nextProps.curEditorProps.value)
})
} else {
this.setState({
editorState: BraftEditor.createEditorState(null)
})
}
}
}
handleEditorChange = (editorState) => {
this.setState({ editorState })
}
handleCloseModal = () => {
this.props.onSaveState({
curEditorProps: {
...this.props.curEditorProps,
visible: false
}
})
}
handleOk = () => {
let { masterData, curEditorProps } = this.props;
let addState = {};
const value = this.state.editorState.toHTML();
let handleType = masterData.handleType ? masterData.handleType : 'update';/* 获取操作类型 */
addState.handleType = handleType;
addState[curEditorProps.sName] = value;
this.props.onSaveState({ masterData: {...masterData, ...addState}, curEditorProps: {...curEditorProps, value} });
this.handleCloseModal();
};
handleCancel = () => {
this.handleCloseModal();
};
/** 渲染 */
render() {
const { curEditorProps = {} } = this.props;
let visible = false;
if (Object.keys(curEditorProps).length) {
visible = curEditorProps.visible;
}
return (
<div>
{
visible ? <AntdDraggableModal
zIndex={1000}
title='文本编辑'
visible={visible}
onOk={this.handleOk}
width='70%'
bodyStyle={{
height: '60vh',
}}
onCancel={this.handleCancel}
>
<BraftEditor
controls={this.controls}
readOnly={!this.props.enabled}
value={this.state.editorState}
onChange={this.handleEditorChange}
/>
</AntdDraggableModal> : ''
}
</div>
);
}
}