/
Edit.jsx
200 lines (186 loc) · 5.1 KB
/
Edit.jsx
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* Edit html block.
* @module components/manage/Blocks/HTML/Edit
*/
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Editor from 'react-simple-code-editor';
import { highlight, languages } from 'prismjs/components/prism-core';
import 'prismjs/components/prism-markup';
import { Button } from 'semantic-ui-react';
import loadable from '@loadable/component';
import { defineMessages, injectIntl } from 'react-intl';
import { Icon } from '@plone/volto/components';
import showSVG from '@plone/volto/icons/show.svg';
import clearSVG from '@plone/volto/icons/clear.svg';
import codeSVG from '@plone/volto/icons/code.svg';
const Pretty = loadable.lib(() => import('pretty'));
const messages = defineMessages({
source: {
id: 'Source',
defaultMessage: 'Source',
},
preview: {
id: 'Preview',
defaultMessage: 'Preview',
},
placeholder: {
id: '<p>Add some HTML here</p>',
defaultMessage: '<p>Add some HTML here</p>',
},
});
/**
* Edit html block class.
* @class Edit
* @extends Component
*/
class Edit extends Component {
/**
* Property types.
* @property {Object} propTypes Property types.
* @static
*/
static propTypes = {
selected: PropTypes.bool.isRequired,
block: PropTypes.string.isRequired,
index: PropTypes.number.isRequired,
data: PropTypes.objectOf(PropTypes.any).isRequired,
onChangeBlock: PropTypes.func.isRequired,
onSelectBlock: PropTypes.func.isRequired,
onDeleteBlock: PropTypes.func.isRequired,
handleKeyDown: PropTypes.func.isRequired,
};
/**
* Constructor
* @method constructor
* @param {Object} props Component properties
*/
constructor(props) {
super(props);
this.state = {
code: this.props.data.html || '',
isPreview: false,
};
this.onChangeCode = this.onChangeCode.bind(this);
this.onPreview = this.onPreview.bind(this);
this.onCodeEditor = this.onCodeEditor.bind(this);
}
/**
* Component will receive props
* @method componentDidMount
* @returns {undefined}
*/
componentDidMount() {
if (this.props.selected) {
this.codeEditor._input.focus();
}
}
/**
* Component will receive props
* @method componentWillReceiveProps
* @param {Object} nextProps Next properties
* @returns {undefined}
*/
UNSAFE_componentWillReceiveProps(nextProps) {
if (nextProps.selected) {
this.codeEditor._input.focus();
}
}
/**
* Change html handler
* @method onChangeCode
* @param {string} code New value html
* @returns {undefined}
*/
onChangeCode(code) {
this.props.onChangeBlock(this.props.block, {
...this.props.data,
html: code,
});
this.setState({ code });
}
pretty = React.createRef();
/**
* Preview mode handler
* @method onPreview
* @returns {undefined}
*/
onPreview() {
this.setState({
isPreview: !this.state.isPreview,
code: this.pretty.current.default(this.state.code),
});
}
/**
* Code Editor mode handler
* @method onPreview
* @returns {undefined}
*/
onCodeEditor() {
this.setState({ isPreview: !this.state.isPreview });
}
/**
* Render method.
* @method render
* @returns {string} Markup for the component.
*/
render() {
const placeholder =
this.props.data.placeholder ||
this.props.intl.formatMessage(messages.placeholder);
return (
<>
<Pretty ref={this.pretty} />
{this.props.selected && !!this.state.code && (
<div className="toolbar">
<Button.Group>
<Button
icon
basic
aria-label={this.props.intl.formatMessage(messages.source)}
active={!this.state.isPreview}
onClick={this.onCodeEditor}
>
<Icon name={codeSVG} size="24px" />
</Button>
</Button.Group>
<Button.Group>
<Button
icon
basic
aria-label={this.props.intl.formatMessage(messages.preview)}
active={this.state.isPreview}
onClick={this.onPreview}
>
<Icon name={showSVG} size="24px" />
</Button>
</Button.Group>
<div className="separator" />
<Button.Group>
<Button icon basic onClick={() => this.onChangeCode('')}>
<Icon name={clearSVG} size="24px" color="#e40166" />
</Button>
</Button.Group>
</div>
)}
{this.state.isPreview && (
<div dangerouslySetInnerHTML={{ __html: this.state.code }} />
)}
{!this.state.isPreview && (
<Editor
value={this.state.code}
placeholder={placeholder}
onValueChange={(code) => this.onChangeCode(code)}
highlight={(code) => highlight(code, languages.html)}
padding={8}
className="html-editor"
ref={(node) => {
this.codeEditor = node;
}}
/>
)}
</>
);
}
}
export default injectIntl(Edit);