Skip to content

Commit abc906e

Browse files
committed
Fix Datawrapper embed resizing in article-body.js
1 parent ad590cd commit abc906e

File tree

1 file changed

+59
-32
lines changed

1 file changed

+59
-32
lines changed

web/src/components/article-layouts/article-body.js

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,41 @@ import { DiscussionEmbed } from "disqus-react";
88

99
import React, { useEffect, useRef } from "react";
1010

11-
// Define the iframe serializer
12-
const serializers = {
13-
types: {
14-
iframe: ({ node }) => {
15-
return <DatawrapperEmbed url={node.url} caption={node.caption} />;
16-
},
17-
},
18-
};
19-
20-
21-
const DatawrapperEmbed = ({ url, caption }) => {
22-
const containerRef = useRef(null);
11+
/*const DatawrapperEmbed = ({ url, caption }) => {
12+
const iframeRef = useRef(null);
13+
const chartId = url.match(/\/([a-zA-Z0-9]+)\/\d+\/?$/)?.[1];
2314
2415
useEffect(() => {
25-
if (!url || !containerRef.current) return;
26-
27-
const match = url.match(/dwcdn\.net\/([^/]+)/);
28-
if (!match) return;
16+
window.addEventListener("message", (e) => {
17+
console.log("📩 Raw message received:", e.data);
18+
});
19+
20+
const receiveMessage = (e) => {
21+
if (!e.data || !e.data["datawrapper-height"]) return;
2922
30-
const chartId = match[1];
31-
const scriptId = `dw-script-${chartId}`;
23+
const heights = e.data["datawrapper-height"];
24+
const iframe = iframeRef.current;
3225
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]);
26+
Object.keys(heights).forEach((id) => {
27+
if (iframe?.id === `datawrapper-chart-${id}`) {
28+
const height = heights[id];
29+
console.log("🔧 Setting dynamic height for", id, ":", height);
30+
iframe.style.setProperty("height", `${height}px`, "important");
31+
}
32+
});
33+
};
4134
42-
const chartId = url.match(/dwcdn\.net\/([^/]+)/)?.[1];
43-
if (!chartId) return <p style={{ color: "red" }}>Invalid Datawrapper URL</p>;
35+
window.addEventListener("message", receiveMessage);
36+
return () => window.removeEventListener("message", receiveMessage);
37+
}, []);
4438
4539
return (
46-
<div ref={containerRef} style={{ width: "100%", margin: "2rem 0" }}>
40+
<div style={{ width: "100%", margin: "2rem 0" }}>
4741
<iframe
42+
ref={iframeRef}
43+
src={url}
44+
title={caption || "Datawrapper Chart"}
4845
id={`datawrapper-chart-${chartId}`}
49-
title={caption || "Embedded Datawrapper chart"}
50-
src={`https://datawrapper.dwcdn.net/${chartId}/1/`}
5146
scrolling="no"
5247
frameBorder="0"
5348
allowFullScreen
@@ -56,7 +51,7 @@ const DatawrapperEmbed = ({ url, caption }) => {
5651
width: "0",
5752
minWidth: "100%",
5853
border: "none",
59-
height: "400px",
54+
transition: "height 0.2s ease",
6055
}}
6156
/>
6257
{caption && (
@@ -73,9 +68,41 @@ const DatawrapperEmbed = ({ url, caption }) => {
7368
)}
7469
</div>
7570
);
71+
};*/
72+
73+
const DatawrapperEmbed = ({ chartId }) => {
74+
return (
75+
<div style={{ minHeight: "400px" }} id={`datawrapper-${chartId}`}>
76+
<script
77+
type="text/javascript"
78+
defer
79+
src={`https://datawrapper.dwcdn.net/${chartId}/embed.js`}
80+
data-target={`#datawrapper-${chartId}`}
81+
></script>
82+
<noscript>
83+
<img
84+
src={`https://datawrapper.dwcdn.net/${chartId}/full.png`}
85+
alt="Chart"
86+
/>
87+
</noscript>
88+
</div>
89+
);
7690
};
7791

7892

93+
94+
const serializers = {
95+
types: {
96+
iframe: ({ node }) => {
97+
const chartId = node.url.match(/\/([a-zA-Z0-9]+)\/?$/)?.[1];
98+
if (!chartId) return <p style={{ color: "red" }}>Invalid Datawrapper URL</p>;
99+
return <DatawrapperEmbed chartId={chartId} />;
100+
},
101+
},
102+
};
103+
104+
105+
79106
function ArticleBody(props) {
80107
const {
81108
_rawExcerpt,

0 commit comments

Comments
 (0)