This repository has been archived by the owner on Nov 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
new.jsx
209 lines (187 loc) · 6.5 KB
/
new.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
// @flow
import * as React from 'react';
import { I18n } from 'react-redux-i18n';
import { EditorState, RichUtils } from 'draft-js';
import Editor from 'draft-js-plugins-editor';
import classNames from 'classnames';
import createToolbarPlugin from 'draft-js-static-toolbar-plugin';
import { ItalicButton, BoldButton, UnorderedListButton } from 'draft-js-buttons';
import createCounterPlugin from 'draft-js-counter-plugin';
// 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 attachmentsPlugin from './attachmentsPlugin';
type DraftPlugin = any;
type Props = {
editorState: EditorState,
handleInputFocus?: Function,
maxLength: number,
onChange: Function,
placeholder?: string,
textareaRef?: Function,
toolbarPosition: string,
withAttachmentButton: boolean
};
type State = {
editorHasFocus: boolean
};
const ToolbarSeparator = () => <span className="separator" />;
export default class RichTextEditor extends React.Component<Props, State> {
editor: ?Editor;
plugins: Array<DraftPlugin>;
components: { [string]: React.ComponentType<*> };
static defaultProps = {
handleInputFocus: undefined,
maxLength: 0,
toolbarPosition: 'top',
withAttachmentButton: false
};
constructor(props: Props): void {
super(props);
const modalPlugin = createModalPlugin();
const { closeModal, setModalContent, Modal } = modalPlugin;
const counterPlugin = createCounterPlugin();
const modalConfig = {
closeModal: closeModal,
setModalContent: setModalContent
};
const linkPlugin = createLinkPlugin({
...modalConfig
});
const { LinkButton } = linkPlugin;
const components = {};
const toolbarStructure = [BoldButton, ItalicButton, UnorderedListButton, ToolbarSeparator, LinkButton, ToolbarSeparator];
const plugins = [counterPlugin, linkPlugin];
if (props.withAttachmentButton) {
const attachmentPlugin = createAttachmentPlugin({
...modalConfig
});
const { AttachmentButton, Attachments } = attachmentPlugin;
toolbarStructure.push(AttachmentButton);
plugins.push(attachmentPlugin);
components.Attachments = Attachments;
}
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: 'editor-toolbar'
}
}
});
plugins.push(staticToolbarPlugin);
this.plugins = plugins;
const { CustomCounter } = counterPlugin;
const { Toolbar } = staticToolbarPlugin;
this.components = {
CustomCounter: CustomCounter,
Modal: Modal,
Toolbar: Toolbar,
...components
};
this.state = {
editorHasFocus: false
};
}
handleEditorFocus = (): void => {
const { handleInputFocus } = this.props;
this.setState(
{
editorHasFocus: true
},
handleInputFocus
);
};
countRemainingChars = (plainText: string): string => {
const regex = /(?:\r\n|\r|\n)/g; // new line, carriage return, line feed
const cleanString = plainText.replace(regex, '').trim(); // replace above characters w/ nothing
const count = this.props.maxLength - cleanString.length;
return I18n.t('debate.remaining_x_characters', { nbCharacters: count });
};
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.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';
};
deleteAttachment = (documentId: string): void => {
const { editorState, onChange } = this.props;
const contentState = editorState.getCurrentContent();
const newContentState = attachmentsPlugin.removeAttachment(contentState, documentId);
onChange(EditorState.createWithContent(newContentState));
};
render() {
const { editorState, maxLength, onChange, placeholder, textareaRef, toolbarPosition } = this.props;
const divClassName = classNames('rich-text-editor', { hidePlaceholder: this.shouldHidePlaceholder() });
const { Attachments, CustomCounter, Modal, Toolbar } = this.components;
return (
<div className={divClassName} ref={textareaRef}>
<div className="editor-header">
{editorState.getCurrentContent().hasText() ? <div className="editor-label form-label">{placeholder}</div> : null}
{toolbarPosition === 'top' ? <Toolbar /> : null}
<div className="clear" />
</div>
<Modal />
<div onClick={this.focusEditor}>
<Editor
editorState={editorState}
onChange={onChange}
onFocus={this.handleEditorFocus}
placeholder={placeholder}
plugins={this.plugins}
ref={(e) => {
this.editor = e;
}}
handleReturn={this.handleReturn}
spellCheck
/>
</div>
{maxLength ? (
<div className="annotation margin-xs">
<CustomCounter limit={maxLength} countFunction={this.countRemainingChars} />
</div>
) : null}
{toolbarPosition === 'bottom' ? <Toolbar /> : null}
{Attachments ? <Attachments /> : null}
</div>
);
}
}