@@ -12,64 +12,68 @@ import React, { useEffect, useRef } from "react";
1212const serializers = {
1313 types : {
1414 iframe : ( { node } ) => {
15- const { url, caption } = node ;
16- const containerRef = useRef ( null ) ;
15+ return < DatawrapperEmbed url = { node . url } caption = { node . caption } /> ;
16+ } ,
17+ } ,
18+ } ;
1719
18- useEffect ( ( ) => {
19- if ( ! url || ! containerRef . current ) return ;
2020
21- const match = url . match ( / d w c d n \. n e t \/ ( [ ^ / ] + ) / ) ;
22- if ( ! match ) return ;
21+ const DatawrapperEmbed = ( { url , caption } ) => {
22+ const containerRef = useRef ( null ) ;
2323
24- const chartId = match [ 1 ] ;
25- const scriptId = `dw-script- ${ chartId } ` ;
24+ useEffect ( ( ) => {
25+ if ( ! url || ! containerRef . current ) return ;
2626
27- if ( ! document . getElementById ( scriptId ) ) {
28- const script = document . createElement ( "script" ) ;
29- script . src = `https://datawrapper.dwcdn.net/${ chartId } /embed.js` ;
30- script . defer = true ;
31- script . id = scriptId ;
32- document . body . appendChild ( script ) ;
33- }
34- } , [ url ] ) ;
27+ const match = url . match ( / d w c d n \. n e t \/ ( [ ^ / ] + ) / ) ;
28+ if ( ! match ) return ;
3529
36- const chartId = url . match ( / d w c d n \. n e t \/ ( [ ^ / ] + ) / ) ?. [ 1 ] ;
30+ const chartId = match [ 1 ] ;
31+ const scriptId = `dw-script-${ chartId } ` ;
3732
38- return (
39- < div ref = { containerRef } style = { { width : "100%" , margin : "2rem 0" } } >
40- < iframe
41- id = { `datawrapper-chart-${ chartId } ` }
42- title = { caption || "Embedded chart" }
43- src = { url }
44- scrolling = "no"
45- frameBorder = "0"
46- allowFullScreen
47- data-external = "1"
48- style = { {
49- width : "0" ,
50- minWidth : "100%" ,
51- border : "none" ,
52- height : "400px" , // fallback height, will get resized
53- } }
54- />
55- { caption && (
56- < p
57- style = { {
58- textAlign : "center" ,
59- fontSize : "0.9rem" ,
60- color : "#555" ,
61- marginTop : "0.5rem" ,
62- } }
63- >
64- { caption }
65- </ p >
66- ) }
67- </ div >
68- ) ;
69- } ,
70- } ,
71- } ;
33+ if ( ! document . getElementById ( scriptId ) ) {
34+ const script = document . createElement ( "script" ) ;
35+ script . src = `https://datawrapper.dwcdn.net/${ chartId } /embed.js` ;
36+ script . defer = true ;
37+ script . id = scriptId ;
38+ document . body . appendChild ( script ) ;
39+ }
40+ } , [ url ] ) ;
41+
42+ const chartId = url . match ( / d w c d n \. n e t \/ ( [ ^ / ] + ) / ) ?. [ 1 ] ;
43+ if ( ! chartId ) return < p style = { { color : "red" } } > Invalid Datawrapper URL</ p > ;
7244
45+ return (
46+ < div ref = { containerRef } style = { { width : "100%" , margin : "2rem 0" } } >
47+ < iframe
48+ id = { `datawrapper-chart-${ chartId } ` }
49+ title = { caption || "Embedded Datawrapper chart" }
50+ src = { `https://datawrapper.dwcdn.net/${ chartId } /1/` }
51+ scrolling = "no"
52+ frameBorder = "0"
53+ allowFullScreen
54+ data-external = "1"
55+ style = { {
56+ width : "0" ,
57+ minWidth : "100%" ,
58+ border : "none" ,
59+ height : "400px" ,
60+ } }
61+ />
62+ { caption && (
63+ < p
64+ style = { {
65+ textAlign : "center" ,
66+ fontSize : "0.9rem" ,
67+ color : "#555" ,
68+ marginTop : "0.5rem" ,
69+ } }
70+ >
71+ { caption }
72+ </ p >
73+ ) }
74+ </ div >
75+ ) ;
76+ } ;
7377
7478
7579function ArticleBody ( props ) {
0 commit comments