-
Notifications
You must be signed in to change notification settings - Fork 2k
/
body.jsx
179 lines (156 loc) · 4.18 KB
/
body.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
/**
* External dependencies
*/
import React from 'react';
import createReactClass from 'create-react-class';
import { localize } from 'i18n-calypso';
/**
* Internal dependencies
*/
import { wpcom } from '../rest-client/wpcom';
import Comment from './block-comment';
import NoteActions from './actions';
import NotePreface from './preface';
import Post from './block-post';
import User from './block-user';
import { bumpStat } from '../rest-client/bump-stat';
import { html } from '../indices-to-html';
import { p, zipWithSignature } from './functions';
class ReplyBlock extends React.Component {
render() {
// explicitly send className of '' here so we don't get the default of
// "paragraph"
var replyText = p( html( this.props.block ), '' );
return <div className="wpnc__reply">{ replyText }</div>;
}
}
export const NoteBody = createReactClass( {
displayName: 'NoteBody',
getInitialState: function () {
return {
reply: null,
};
},
componentDidMount: function () {
bumpStat( 'notes-click-type', this.props.note.type );
},
replyLoaded: function ( error, data ) {
if ( error || ! this.isMounted() ) {
return;
}
this.setState( { reply: data } );
},
UNSAFE_componentWillMount: function () {
var note = this.props.note;
var hasReplyBlock;
if ( note.meta && note.meta.ids.reply_comment ) {
hasReplyBlock =
note.body.filter( function ( block ) {
return (
block.ranges &&
block.ranges.length > 1 &&
block.ranges[ 1 ].id == note.meta.ids.reply_comment
);
} ).length > 0;
if ( ! hasReplyBlock ) {
wpcom()
.site( this.props.note.meta.ids.site )
.comment( this.props.note.meta.ids.reply_comment )
.get( this.replyLoaded );
}
}
},
render: function () {
var blocks = zipWithSignature( this.props.note.body, this.props.note );
var actions = '';
var preface = '';
var replyBlock = null;
var replyMessage;
var firstNonTextIndex;
var i;
for ( i = 0; i < blocks.length; i++ ) {
if ( 'text' !== blocks[ i ].signature.type ) {
firstNonTextIndex = i;
break;
}
}
if ( firstNonTextIndex ) {
preface = <NotePreface blocks={ this.props.note.body.slice( 0, i ) } />;
blocks = blocks.slice( i );
}
var body = [];
for ( i = 0; i < blocks.length; i++ ) {
var block = blocks[ i ];
var blockKey = 'block-' + this.props.note.id + '-' + i;
if ( block.block.actions && 'user' !== block.signature.type ) {
actions = (
<NoteActions
note={ this.props.note }
block={ block.block }
global={ this.props.global }
/>
);
}
switch ( block.signature.type ) {
case 'user':
body.push(
<User
key={ blockKey }
block={ block.block }
noteType={ this.props.note.type }
note={ this.props.note }
timestamp={ this.props.note.timestamp }
url={ this.props.note.url }
/>
);
break;
case 'comment':
body.push(
<Comment key={ blockKey } block={ block.block } meta={ this.props.note.meta } />
);
break;
case 'post':
body.push(
<Post key={ blockKey } block={ block.block } meta={ this.props.note.meta } />
);
break;
case 'reply':
replyBlock = <ReplyBlock key={ blockKey } block={ block.block } />;
break;
default:
body.push( p( html( block.block ) ) );
break;
}
}
if ( this.state.reply && this.state.reply.URL ) {
if ( this.props.note.meta.ids.comment ) {
replyMessage = this.props.translate( 'You {{a}}replied{{/a}} to this comment.', {
components: {
a: <a href={ this.state.reply.URL } target="_blank" rel="noopener noreferrer" />,
},
} );
} else {
replyMessage = this.props.translate( 'You {{a}}replied{{/a}} to this post.', {
components: {
a: <a href={ this.state.reply.URL } target="_blank" rel="noopener noreferrer" />,
},
} );
}
replyBlock = (
<div className="wpnc__reply">
<span className="wpnc__gridicon"></span>
{ replyMessage }
</div>
);
}
return (
<div className="wpnc__body">
{ preface }
<div className="wpnc__body-content">{ body }</div>
{ replyBlock }
{ actions }
</div>
);
},
} );
export default localize( NoteBody );