forked from busyorg/busy
/
EditorMain.js
457 lines (429 loc) · 15.2 KB
/
EditorMain.js
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import React from 'react';
import PropTypes from 'prop-types';
import Helmet from 'react-helmet';
import ReactDOM from 'react-dom';
import classNames from 'classnames';
import { injectIntl, FormattedMessage } from 'react-intl';
import _ from 'lodash';
import readingTime from 'reading-time';
import { Checkbox, Form, Input, Select, Button, Collapse } from 'antd';
import { rewardsValues } from '../../../common/constants/rewards';
import Action from '../Button/Action';
import requiresLogin from '../../auth/requiresLogin';
import withEditor from './withEditor';
import EditorInput from './EditorInput';
import UlogDropdown from './UlogDropdown';
import { remarkable } from '../Story/Body';
import BodyContainer from '../../containers/Story/BodyContainer';
import './Editor.less';
@injectIntl
@requiresLogin
@Form.create()
@withEditor
class EditorMain extends React.Component {
static propTypes = {
intl: PropTypes.shape().isRequired,
form: PropTypes.shape().isRequired,
title: PropTypes.string,
topics: PropTypes.arrayOf(PropTypes.string),
body: PropTypes.string,
reward: PropTypes.string,
upvote: PropTypes.bool,
loading: PropTypes.bool,
isUpdating: PropTypes.bool,
saving: PropTypes.bool,
draftId: PropTypes.string,
onUpdate: PropTypes.func,
onDelete: PropTypes.func,
onSubmit: PropTypes.func,
onError: PropTypes.func,
onImageUpload: PropTypes.func,
onImageInvalid: PropTypes.func,
handleExtraMonetization: PropTypes.func,
};
static defaultProps = {
title: '',
topics: [],
body: '',
reward: rewardsValues.half,
upvote: true,
recentTopics: [],
popularTopics: [],
loading: false,
isUpdating: false,
saving: false,
draftId: null,
onUpdate: () => {},
onDelete: () => {},
onSubmit: () => {},
onError: () => {},
onImageUpload: () => {},
onImageInvalid: () => {},
handleExtraMonetization: () => {},
};
constructor(props) {
super(props);
this.state = {
body: '',
bodyHTML: '',
};
this.onUpdate = this.onUpdate.bind(this);
this.setValues = this.setValues.bind(this);
this.setBodyAndRender = this.setBodyAndRender.bind(this);
this.throttledUpdate = this.throttledUpdate.bind(this);
this.handleDelete = this.handleDelete.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
componentDidMount() {
this.setValues(this.props);
this.props.form.setFieldsValue({
title: 'ULOG: ',
topics: ['ulog'],
});
// eslint-disable-next-line react/no-find-dom-node
const select = ReactDOM.findDOMNode(this.select);
if (select) {
const selectInput = select.querySelector('input,textarea,div[contentEditable]');
if (selectInput) {
selectInput.setAttribute('autocorrect', 'off');
selectInput.setAttribute('autocapitalize', 'none');
}
}
}
componentWillReceiveProps(nextProps) {
const { title, topics, body, reward, upvote, draftId } = this.props;
if (
title !== nextProps.title ||
!_.isEqual(topics, nextProps.topics) ||
body !== nextProps.body ||
reward !== nextProps.reward ||
upvote !== nextProps.upvote ||
(draftId && nextProps.draftId === null)
) {
this.setValues(nextProps);
}
}
onUpdate() {
_.throttle(this.throttledUpdate, 200, { leading: false, trailing: true })();
}
setValues(post) {
// NOTE: Used to rollback damaged drafts - https://github.com/busyorg/busy/issues/1412
// Might be deleted after a while.
let reward = rewardsValues.half;
if (
post.reward === rewardsValues.all ||
post.reward === rewardsValues.half ||
post.reward === rewardsValues.none
) {
reward = post.reward;
}
this.props.form.setFieldsValue({
title: post.title,
topics: post.topics,
body: post.body,
reward,
upvote: post.upvote,
});
this.setBodyAndRender(post.body);
}
setBodyAndRender(body) {
this.setState({
body,
bodyHTML: remarkable.render(body),
});
}
checkTopics = intl => (rule, value, callback) => {
if (value[0] !== 'ulog') {
callback(
intl.formatMessage({
id: 'ulog_not_topic',
defaultMessage: '#ulog must be the first tag for posts.',
})
)
}
if (!value || value.length < 1 || value.length > 5) {
callback(
intl.formatMessage({
id: 'topics_error_count',
defaultMessage: 'You must add 1 to 4 topics with #ulog as the first.',
}),
);
}
value
.map(topic => ({ topic, valid: /^[a-z0-9]+(-[a-z0-9]+)*$/.test(topic) }))
.filter(topic => !topic.valid)
.map(topic =>
callback(
intl.formatMessage(
{
id: 'topics_error_invalid_topic',
defaultMessage: 'Topic {topic} is invalid.',
},
{
topic: topic.topic,
},
),
),
);
callback();
};
throttledUpdate() {
const { form } = this.props;
const values = form.getFieldsValue();
this.setBodyAndRender(values.body);
if (Object.values(form.getFieldsError()).filter(e => e).length > 0) return;
this.props.onUpdate(values);
}
handleSubmit(e) {
e.preventDefault();
this.props.form.validateFieldsAndScroll((err, values) => {
if (err) this.props.onError();
else this.props.onSubmit(values);
});
}
handleDelete(e) {
e.stopPropagation();
e.preventDefault();
this.props.onDelete();
}
render() {
const { intl, form, loading, isUpdating, saving, draftId } = this.props;
const { getFieldDecorator } = form;
const { body, bodyHTML } = this.state;
const { words, minutes } = readingTime(bodyHTML);
const Panel = Collapse.Panel;
return (
<div>
<div>
<Collapse defaultActiveKey={['1']}>
<Panel header="The art of ULOGGING" key="1">
<p>
The art of ULOGGING is a conscious effort to "mine the human" into its "awesomest version", while reshaping the entire INTERNET and creating legends, icons, great men and women, brothers and "True Celebrities". <br/>
Together ULOGGING, we will remove "all barriers to entry" for content-creation, content-curation and steem-promotion, "making steeming as difficult as 1, 2, 3". <br/>
We will own our very cookies; we will re-tap into our shine and recover lost shine. We will fly. <br/>
("True Celebrity-hood" for "everyone" once and for all!) <br/>
In the world of crypto-kitties, pokemonGO, superman, Blacky the rich cat etc we will bear forth "True Celebrities" (the awesomest version of human). Beautiful Disruption!!! (positive world adjustment). <br/>
In due time, we will celebrate breakthrough with the @teardrops Smart Media Token; "@surpassinggoogle". <br/>
"There is now light inside the tunnel". Your Ultimate "True Fan" (your boy terry). Happy ULOGGING!!! <br/>
</p>
</Panel>
</Collapse>
</div>
<div className="hashtags">
<UlogDropdown />
</div>
<div>
<Collapse defaultActiveKey={['1']}>
<Panel header="(This is the main ULOGS.org editor!)" key="1">
ULOGS: Each day and "YOU" in it carries its own "freshness". So, not a day aren't we capable of reshaping the entire internet and touching the world with "a piece of freshness".
A ULOG can contain all formats of media, whether sound, pictures, art, videos, text or a combination of all as long as it is freshly-made (by YOU). You can be a public or private figure. Regardless, "you are a celebrity and we are your fans".
</Panel>
</Collapse>
</div>
<Form className="Editor" layout="vertical" onSubmit={this.handleSubmit}>
<Helmet>
<title>
{intl.formatMessage({ id: 'write_post', defaultMessage: 'Write post' })} - Ulog
</title>
</Helmet>
<Form.Item
label={
<span className="Editor__label">
<FormattedMessage id="title" defaultMessage="Title" />
</span>
}
>
{getFieldDecorator('title', {
initialValue: '',
rules: [
{
required: true,
message: intl.formatMessage({
id: 'title_error_empty',
defaultMessage: 'title_error_empty',
}),
},
{
max: 255,
message: intl.formatMessage({
id: 'title_error_too_long',
defaultMessage: "Title can't be longer than 255 characters.",
}),
},
],
})(
<Input
ref={title => {
this.title = title;
}}
onChange={this.onUpdate}
className="Editor__title"
placeholder={intl.formatMessage({
id: 'title_placeholder',
defaultMessage: 'ULOG: ',
})}
/>,
)}
</Form.Item>
<Form.Item
label={
<span className="Editor__label">
<FormattedMessage id="topics" defaultMessage="Topics" />
</span>
}
>
{getFieldDecorator('topics', {
initialValue: [],
rules: [
{
required: true,
message: intl.formatMessage({
id: 'topics_error_empty',
defaultMessage: 'Please enter topics',
}),
type: 'array',
},
{ validator: this.checkTopics(intl) },
],
})(
<Select
ref={ref => {
this.select = ref;
}}
onChange={this.onUpdate}
className="Editor__topics"
mode="tags"
placeholder={intl.formatMessage({
id: 'topics_placeholder',
defaultMessage: 'Add story topics here',
})}
dropdownStyle={{ display: 'none' }}
tokenSeparators={[' ', ',']}
/>,
)}
</Form.Item>
<div>
Ulogs.org allows you to enjoy the entire steem ecosystem. So, incase you change your mind and want to do a steemit post like normal, that's easy!!! Simply remove the default "ULOG:" from Title above and kindly remove the default "#ulog" from among the tags in the Hashtags box.
Please help us as we try to reserve #ulog, only for ULOGs. (If your post is a ULOG, you will not have cheetah-worries etc)
Want to "mine the human" some more, you can also try one of our specialized editors above!!!
</div>
<Form.Item>
{getFieldDecorator('body', {
rules: [
{
required: true,
message: intl.formatMessage({
id: 'story_error_empty',
defaultMessage: "Story content can't be empty.",
}),
},
],
})(
<EditorInput
rows={12}
addon={
<FormattedMessage
id="reading_time"
defaultMessage={'{words} words / {min} min read'}
values={{
words,
min: Math.ceil(minutes),
}}
/>
}
onChange={this.onUpdate}
onImageUpload={this.props.onImageUpload}
onImageInvalid={this.props.onImageInvalid}
inputId={'editor-inputfile'}
/>,
)}
</Form.Item>
{body && (
<Form.Item
label={
<span className="Editor__label">
<FormattedMessage id="preview" defaultMessage="Preview" />
</span>
}
>
<BodyContainer full body={body} />
</Form.Item>
)}
<Form.Item
className={classNames({ Editor__hidden: isUpdating })}
label={
<span className="Editor__label">
<FormattedMessage id="reward" defaultMessage="Reward" />
</span>
}
>
{getFieldDecorator('reward')(
<Select onChange={this.onUpdate} disabled={isUpdating}>
<Select.Option value={rewardsValues.all}>
<FormattedMessage id="reward_option_100" defaultMessage="100% Steem Power" />
</Select.Option>
<Select.Option value={rewardsValues.half}>
<FormattedMessage id="reward_option_50" defaultMessage="50% SBD and 50% SP" />
</Select.Option>
<Select.Option value={rewardsValues.none}>
<FormattedMessage id="reward_option_0" defaultMessage="Declined" />
</Select.Option>
</Select>,
)}
</Form.Item>
<Form.Item className={classNames({ Editor__hidden: isUpdating })}>
{getFieldDecorator('upvote', { valuePropName: 'checked', initialValue: true })(
<Checkbox onChange={this.onUpdate} disabled={isUpdating}>
<FormattedMessage id="like_post" defaultMessage="Like this post" />
</Checkbox>,
)}
<Checkbox onChange={this.props.handleExtraMonetization} disabled={isUpdating}>
<FormattedMessage id="extra_monetization" defaultMessage="Extra Monitezation" />
</Checkbox>
</Form.Item>
<div className="Editor__bottom">
<span className="Editor__bottom__info">
<i className="iconfont icon-markdown" />{' '}
<FormattedMessage
id="markdown_supported"
defaultMessage="Styling with markdown supported"
/>
</span>
<div className="Editor__bottom__right">
{saving && (
<span className="Editor__bottom__right__saving">
<FormattedMessage id="saving" defaultMessage="Saving..." />
</span>
)}
<Form.Item className="Editor__bottom__cancel">
{draftId && (
<Button type="danger" size="large" disabled={loading} onClick={this.handleDelete}>
<FormattedMessage id="draft_delete" defaultMessage="Delete this draft" />
</Button>
)}
</Form.Item>
<Form.Item className="Editor__bottom__submit">
{isUpdating ? (
<Action primary big loading={loading} disabled={loading}>
<FormattedMessage
id={loading ? 'post_send_progress' : 'post_update_send'}
defaultMessage={loading ? 'Submitting' : 'Update post'}
/>
</Action>
) : (
<Action primary big loading={loading} disabled={loading}>
<FormattedMessage
id={loading ? 'post_send_progress' : 'post_send'}
defaultMessage={loading ? 'Submitting' : 'Post'}
/>
</Action>
)}
</Form.Item>
</div>
</div>
</Form>
</div>
);
}
}
export default EditorMain;