/
draftRenderer.tsx
359 lines (335 loc) · 10.5 KB
/
draftRenderer.tsx
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
/**
* You can use inline styles or classNames inside your callbacks
*/
import React, { Component } from 'react';
import redraft from 'redraft';
import { AttachmentIcon2 } from '../icons';
import Prism from 'prismjs';
const handlePrismRenderer = (syntax, code) => {
const formattedCode = Prism.highlight(
code,
Prism.languages.javascript,
'javascript'
);
return { __html: formattedCode };
};
function Pre({ children, data }) {
const el = React.useRef(null);
const [code, setCode] = React.useState(null);
React.useEffect(() => {
setCode(handlePrismRenderer(data.syntax, el.current.innerHTML));
}, []);
return (
<div>
<div ref={el} style={{ display: 'none' }}>
{children}
</div>
{code && (
<pre className="graf graf--code" dangerouslySetInnerHTML={code} />
)}
</div>
);
}
// just a helper to add a <br /> after a block
const addBreaklines = (children) => children.map((child) => [child, <br />]);
function getImageUrl(url, props) {
if (!url) return;
if (url.includes('://')) return url;
return `${props.domain}${url}`;
}
/**
* Define the renderers
*/
function renderers(props) {
return {
/**
* Those callbacks will be called recursively to render a nested structure
*/
inline: {
// The key passed here is just an index based on rendering order inside a block
BOLD: (children, { key }) => <strong key={key}>{children}</strong>,
ITALIC: (children, { key }) => <em key={key}>{children}</em>,
UNDERLINE: (children, { key }) => <u key={key}>{children}</u>,
/* CODE: (children, { key }) => <span
key={key}
dangerouslySetInnerHTML={handlePrismRenderer(children)}
/>, */
},
/**
* Blocks receive children and depth
* Note that children are an array of blocks with same styling,
*/
blocks: {
unstyled: (children, { keys }) => {
return children.map((o, i) => (
<p key={keys[i]} className="graf graf--p">
{o}
</p>
));
},
blockquote: (children, { keys }) => (
<blockquote key={keys[0]} className="graf graf--blockquote">
{addBreaklines(children)}
</blockquote>
),
'header-one': (children, { keys }) => (
<h1 key={keys[0]} className="graf graf--h2">
{children}
</h1>
),
'header-two': (children, { keys }) => (
<h2 key={keys[0]} className="graf graf--h3">
{children}
</h2>
),
// You can also access the original keys of the blocks
'code-block': (children, { _keys, data }) => {
return <Pre data={data}>{children}</Pre>;
},
// or depth for nested lists
'unordered-list-item': (children, { depth, keys }) => (
<ul key={keys[keys.length - 1]} className={`ul-level-${depth}`}>
{children.map(
(child) => (
<li className="graf graf--insertunorderedlist">{child}</li>
) // eslint-disable-next-line react/jsx-key
)}
</ul>
),
'ordered-list-item': (children, { depth, keys }) => (
<ol key={keys.join('|')} className={`ol-level-${depth}`}>
{children.map((child, index) => (
<li key={keys[index]} className="graf graf--insertorderedlist">
{child}
</li>
))}
</ol>
),
file: (children, { _keys, data }) => {
const fileName = data[0].url.split('/').pop();
return (
<div>
<a
href={getImageUrl(data[0].url, props)}
target="blank"
rel="noopener noreferrer"
className="graf graf--attachment"
>
<AttachmentIcon2 width={20} height={20} />
{fileName}
</a>
</div>
);
},
image: (children, { keys, data }) => {
return keys.map((key, index) => (
<ImageRenderer
blockKey={key}
key={`image-${key}`}
data={data[index]}
props={props}
>
{children[index]}
</ImageRenderer>
));
},
embed: (children, { keys, data }) => {
const { provisory_text, _type, embed_data } = data[0];
const {
images,
title,
_media,
provider_url,
description,
_url,
} = embed_data;
return (
<div key={keys[0]} className="graf graf--mixtapeEmbed">
<span>
{images[0].url ? (
<a
target="_blank"
rel="noopener noreferrer"
className="js-mixtapeImage mixtapeImage"
href={provisory_text}
style={{
backgroundImage: `url(${images[0].url})`,
}}
></a>
) : null}
<a
className="markup--anchor markup--mixtapeEmbed-anchor"
target="_blank"
rel="noopener noreferrer"
href={provisory_text}
>
<strong className="markup--strong markup--mixtapeEmbed-strong">
{title}
</strong>
<em className="markup--em markup--mixtapeEmbed-em">
{description}
</em>
</a>
{provider_url}
</span>
</div>
);
},
video: (children, { keys, data }) => {
const { provisory_text, _type, embed_data } = data[0];
const { html } = embed_data;
return (
<figure
key={keys[0]}
className="graf--figure graf--iframe graf--first"
>
<div
className="iframeContainer"
dangerouslySetInnerHTML={{ __html: `${html}` }}
/>
{provisory_text && provisory_text === 'type a caption (optional)' && (
<figcaption className="imageCaption">
<div className="public-DraftStyleDefault-block public-DraftStyleDefault-ltr">
<span>
<span>{provisory_text}</span>
</span>
</div>
</figcaption>
)}
</figure>
);
},
'recorded-video': (children, { keys, data }) => {
const { url, text } = data[0];
return (
<figure
key={keys[0]}
className="graf--figure graf--iframe graf--first"
>
<div className="iframeContainer">
<video
autoPlay={false}
style={{ width: '100%' }}
controls={true}
src={getImageUrl(url, props)}
></video>
</div>
<figcaption className="imageCaption">
<div className="public-DraftStyleDefault-block public-DraftStyleDefault-ltr">
<span>{text}</span>
</div>
</figcaption>
</figure>
);
},
// If your blocks use meta data it can also be accessed like keys
// atomic: (children, { keys, data }) => children.map((child, i) => <Atomic key={keys[i]} {...data[i]} />),
},
/**
* Entities receive children and the entity data
*/
entities: {
// key is the entity key value from raw
LINK: (children, data, { key }) => (
<a key={key} href={data.url} target="_blank" rel="noopener noreferrer">
{children}
</a>
),
},
/**
* Array of decorators,
* Entities receive children and the entity data,
* inspired by https://facebook.github.io/draft-js/docs/advanced-topics-decorators.html
* it's also possible to pass a custom Decorator class that matches the [DraftDecoratorType](https://github.com/facebook/draft-js/blob/master/src/model/decorators/DraftDecoratorType.js)
*/
/* decorators: [
{
// by default linkStrategy receives a ContentBlock stub (more info under Creating the ContentBlock)
// strategy only receives first two arguments, contentState is yet not provided
strategy: linkStrategy,
// component - a callback as with other renderers
// decoratedText a plain string matched by the strategy
// if your decorator depends on draft-js contentState you need to provide convertFromRaw in redraft options
component: ({ children, decoratedText }) => <a href={decoratedText}>{children}</a>,
},
new CustomDecorator(someOptions),
], */
};
}
function ImageRenderer({ children, blockKey, data, props }) {
const data2 = data;
const { url, aspect_ratio, caption } = data2;
let height, width, ratio;
if (!aspect_ratio) {
height = '100%';
width = '100%';
ratio = '0';
} else {
height = aspect_ratio.height;
width = aspect_ratio.width;
ratio = aspect_ratio.ratio;
}
const defaultStyle = { maxWidth: `${width}px`, maxHeight: `${height}px` };
return (
<figure key={blockKey} className="graf graf--figure">
<div>
<div className="aspectRatioPlaceholder is-locked" style={defaultStyle}>
<div
className="aspect-ratio-fill"
style={{ paddingBottom: `${ratio}%` }}
></div>
{/* <ConnectedImage url={url} width={width} height={height} /> */}
<img
src={getImageUrl(url, props)}
className="graf-image"
width={width}
height={height}
contentEditable="false"
/>
</div>
</div>
{caption && caption !== 'type a caption (optional)' && (
<figcaption className="imageCaption">
<span>
<span data-text="true">{children}</span>
</span>
</figcaption>
)}
</figure>
);
}
type RendererProps = {
message?: any;
domain?: string;
raw: string;
};
export default class Renderer extends Component<RendererProps> {
/* static propTypes = {
raw: PropTypes.object
} */
renderWarning() {
if (this.props.message && this.props.message.message.htmlContent) {
return (
<div
dangerouslySetInnerHTML={{
__html: this.props.message.message.htmlContent,
}}
/>
);
} else {
return <div>---</div>;
}
}
render() {
const { raw } = this.props;
if (!raw) {
return this.renderWarning();
}
const rendered = redraft(raw, renderers(this.props));
// redraft returns a null if there's nothing to render
if (!rendered) {
return this.renderWarning();
}
return <div>{rendered}</div>;
}
}