This repository has been archived by the owner on Nov 29, 2019. It is now read-only.
/
index.jsx
245 lines (221 loc) · 7.37 KB
/
index.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// @flow
import * as React from 'react';
import { EditorState, RichUtils } from 'draft-js';
import Editor from 'draft-js-plugins-editor';
import classNames from 'classnames';
import debounce from 'lodash/debounce';
import createToolbarPlugin from 'draft-js-static-toolbar-plugin';
import { HeadlineThreeButton, HeadlineTwoButton } from 'draft-js-buttons';
// from our workspaces
/* eslint-disable import/no-extraneous-dependencies */
import createAttachmentPlugin from 'draft-js-attachment-plugin';
import createLinkPlugin from 'draft-js-link-plugin';
import createModalPlugin from 'draft-js-modal-plugin';
/* eslint-enable import/no-extraneous-dependencies */
import { BoldButton, ItalicButton, UnorderedListButton } from './buttons';
import { addProtocol } from '../../../utils/linkify';
type DraftPlugin = any;
type Props = {
editorState: EditorState,
handleInputFocus?: Function,
handleToolbarClick?: Function,
onChange: Function,
placeholder?: string,
textareaRef?: Function,
toolbarBlocked?: boolean,
toolbarPosition: ToolbarPosition,
withAttachmentButton: boolean,
withHeaderButton?: boolean
};
type State = {
editorHasFocus: boolean,
toolbarOffset: number
};
export default class RichTextEditor extends React.Component<Props, State> {
editor: ?Editor;
toolbarRef: {| current: null | HTMLElement |};
plugins: Array<DraftPlugin>;
components: { [string]: React.ComponentType<*> };
static defaultProps = {
handleInputFocus: undefined,
handleToolbarClick: undefined,
toolbarPosition: 'top',
toolbarBlocked: false,
withAttachmentButton: false,
withHeaderButton: false
};
constructor(props: Props): void {
super(props);
this.editor = React.createRef();
this.toolbarRef = React.createRef();
const modalPlugin = createModalPlugin();
const { closeModal, setModalContent, Modal } = modalPlugin;
const modalConfig = {
closeModal: closeModal,
setModalContent: setModalContent
};
const linkPlugin = createLinkPlugin({
...modalConfig,
formatLink: addProtocol
});
const { LinkButton } = linkPlugin;
const components = {};
components.LinkButton = LinkButton;
components.Modal = Modal;
let toolbarStructure = [BoldButton, ItalicButton, UnorderedListButton, LinkButton];
if (props.withHeaderButton) {
toolbarStructure = [HeadlineTwoButton, HeadlineThreeButton, ...toolbarStructure];
}
const plugins = [linkPlugin];
if (props.withAttachmentButton) {
const attachmentPlugin = createAttachmentPlugin({
...modalConfig
});
const { AttachmentButton, Attachments } = attachmentPlugin;
toolbarStructure.push(AttachmentButton);
plugins.push(attachmentPlugin);
components.Attachments = Attachments;
components.AttachmentButton = AttachmentButton;
}
const staticToolbarPlugin = createToolbarPlugin({
structure: toolbarStructure,
// we need this for toolbar plugin to add css classes to buttons and toolbar
theme: {
buttonStyles: {
active: 'active',
button: 'btn btn-default',
buttonWrapper: 'btn-group'
},
toolbarStyles: {
toolbar: classNames('editor-toolbar', props.toolbarPosition)
}
}
});
plugins.push(staticToolbarPlugin);
const { Toolbar } = staticToolbarPlugin;
this.components = {
Toolbar: Toolbar,
...components
};
this.plugins = plugins;
this.state = {
editorHasFocus: false,
toolbarOffset: 0
};
}
componentWillMount() {
window.addEventListener('scroll', this.setToolbarOffset);
}
componentWillUnmount() {
window.removeEventListener('scroll', this.setToolbarOffset);
}
setToolbarOffset = debounce(() => {
const toolbar = this.toolbarRef.current;
if (!toolbar) {
return;
}
if (toolbar.offsetTop <= window.pageYOffset) {
this.setState({ toolbarOffset: window.pageYOffset - toolbar.offsetTop });
} else {
this.setState({ toolbarOffset: 0 });
}
}, 20);
handleEditorFocus = () => {
const { handleInputFocus } = this.props;
this.setState(
{
editorHasFocus: true
},
handleInputFocus
);
};
handleToolbarClick = () => {
if (this.props.handleToolbarClick) {
return this.props.handleToolbarClick();
}
return true;
};
shouldHidePlaceholder(): boolean {
// don't display placeholder if user changes the block type (to bullet list) before to type anything
const contentState = this.props.editorState.getCurrentContent();
if (!contentState.hasText()) {
if (
contentState
.getBlockMap()
.first()
.getType() !== 'unstyled'
) {
return true;
}
}
return false;
}
focusEditor = (): void => {
// Hacky: Wait to focus the editor so we don't lose selection.
// The toolbar actions don't work at all without this.
setTimeout(() => {
if (this.editor && this.editor.current) {
this.editor.current.focus();
}
}, 50);
};
handleReturn = (e: SyntheticKeyboardEvent<*>): 'handled' | 'not-handled' => {
// Pressing shift-enter keys creates a new line (<br/>) instead of an new paragraph (<p>)
// See https://github.com/HubSpot/draft-convert/issues/83
// For example, this enables to create line returns inside a list item.
const { editorState, onChange } = this.props;
if (e.shiftKey) {
onChange(RichUtils.insertSoftNewline(editorState));
return 'handled';
}
return 'not-handled';
};
render() {
const { editorState, onChange, placeholder, textareaRef, toolbarBlocked, toolbarPosition } = this.props;
const divClassName = classNames('rich-text-editor', { hidePlaceholder: this.shouldHidePlaceholder() });
const { Attachments, Modal, Toolbar } = this.components;
return (
<div ref={this.toolbarRef}>
<div className={divClassName} ref={textareaRef}>
<div className="editor-header">
{editorState.getCurrentContent().hasText() && placeholder ? (
<div className="editor-label form-label">{placeholder}</div>
) : (
<div className="editor-label form-label"> </div>
)}
<div className="clear" />
</div>
<Modal />
<div onClick={this.focusEditor}>
<Editor
editorState={editorState}
onChange={onChange}
onFocus={this.handleEditorFocus}
placeholder={placeholder}
plugins={this.plugins}
ref={this.editor}
handleReturn={this.handleReturn}
spellCheck
/>
</div>
{/*
we have to move toolbar in css for now since there is a bug in draft-js-plugin
It should be fixed in draft-js-plugin v3
*/}
<div
className={classNames(['toolbar-container', toolbarBlocked ? 'toolbar-blocked' : null])}
onClick={this.handleToolbarClick}
>
{toolbarPosition === 'top' || toolbarPosition === 'bottom' ? <Toolbar /> : null}
{toolbarPosition === 'sticky' ? (
<div className="editor-toolbar-sticky" style={{ top: this.state.toolbarOffset }}>
<Toolbar />
</div>
) : null}
</div>
{Attachments ? <Attachments /> : null}
</div>
</div>
);
}
}