EmbeddedStatus: listen for embed.js iframe events and autosize height

status-card
Alex Gleason 2022-08-21 14:32:54 -04:00
rodzic 7f7f9a6fae
commit 9ca87c1eec
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
3 zmienionych plików z 26 dodań i 0 usunięć

Wyświetl plik

@ -1,5 +1,8 @@
import loadPolyfills from './soapbox/load_polyfills';
// Load iframe event listener
require('./soapbox/iframe');
// @ts-ignore
require.context('./images/', true);

Wyświetl plik

@ -6,6 +6,7 @@ import MissingIndicator from 'soapbox/components/missing_indicator';
import Status from 'soapbox/components/status';
import { Spinner } from 'soapbox/components/ui';
import { useAppDispatch, useAppSelector } from 'soapbox/hooks';
import { iframeId } from 'soapbox/iframe';
import { makeGetStatus } from 'soapbox/selectors';
interface IEmbeddedStatus {
@ -34,6 +35,14 @@ const EmbeddedStatus: React.FC<IEmbeddedStatus> = ({ params }) => {
.catch(() => setLoading(false));
}, []);
useEffect(() => {
window.parent.postMessage({
type: 'setHeight',
id: iframeId,
height: document.getElementsByTagName('html')[0].scrollHeight,
}, '*');
}, [status, loading]);
const renderInner = () => {
if (loading) {
return <Spinner />;

Wyświetl plik

@ -0,0 +1,14 @@
/** ID of this iframe (given by embed.js) when embedded on a page. */
let iframeId: any;
/** Receive iframe messages. */
// https://github.com/mastodon/mastodon/pull/4853
const handleMessage = (e: MessageEvent) => {
if (e.data?.type === 'setHeight') {
iframeId = e.data?.id;
}
};
window.addEventListener('message', handleMessage);
export { iframeId };