UNPKG

@craftercms/studio-ui

Version:

Services, components, models & utils to build CrafterCMS authoring extensions.

154 lines (152 loc) 4.95 kB
/* * Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 3 as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* * Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import { fromEvent, NEVER } from 'rxjs'; import { makeStyles } from 'tss-react/mui'; import { defineMessages, useIntl } from 'react-intl'; import React, { useEffect, useRef } from 'react'; import { filter, map, pluck } from 'rxjs/operators'; const message$ = fromEvent(window, 'message'); const useStyles = makeStyles()((theme) => ({ iframe: { width: '100%', maxWidth: '100%', border: 'none', height: '100%', transition: 'width .25s ease, height .25s ease', margin: 'auto' }, iframeWithBorder: { borderRadius: 20, borderColor: '#555' }, iframeWithBorderLandscape: { borderWidth: '10px 50px' }, iframeWithBorderPortrait: { borderWidth: '50px 10px' }, hostContainer: { flexGrow: 1, display: 'flex', alignItems: 'center', justifyContent: 'center', background: '#f3f3f3', height: '100%', maxHeight: 'calc(100% - 64px)', overflow: 'auto', transition: theme.transitions.create('margin', { easing: theme.transitions.easing.sharp, duration: theme.transitions.duration.leavingScreen }) }, shift: { transition: theme.transitions.create('margin', { easing: theme.transitions.easing.easeOut, duration: theme.transitions.duration.enteringScreen }) // width: `calc(100% - ${DRAWER_WIDTH}px)`, // marginLeft: DRAWER_WIDTH } })); const meta = { craftercms: true, source: 'host' }; const translations = defineMessages({ iframeTitle: { id: 'preview.previewIFrameTitle', defaultMessage: 'Preview Frame' } }); export function HostUI(props) { const { classes, cx } = useStyles(); const { formatMessage } = useIntl(); const { url, site, width, origin, height, border, className, onMessage, postMessage$ = NEVER } = props; const iframeRef = useRef(null); const cls = cx(classes.iframe, { [className || '']: !!className, [classes.iframeWithBorder]: border != null, [classes.iframeWithBorderPortrait]: border === 'landscape', [classes.iframeWithBorderLandscape]: border === 'portrait' }); useEffect(() => { const guestToHostSubscription = message$ .pipe( filter((e) => { var _a, _b; return Boolean( (_b = (_a = e.data) === null || _a === void 0 ? void 0 : _a.meta) === null || _b === void 0 ? void 0 : _b.craftercms ); }), pluck('data'), map((data) => data.type ? data : { type: data.topic, payload: data.message } ) ) .subscribe(onMessage); const hostToGuestSubscription = postMessage$ .pipe(map((action) => Object.assign(Object.assign({}, action), { meta }))) .subscribe((action) => { const contentWindow = iframeRef.current.contentWindow; contentWindow.postMessage(action, '*'); }); return () => { guestToHostSubscription.unsubscribe(); hostToGuestSubscription.unsubscribe(); }; }, [origin, onMessage, postMessage$]); useEffect(() => { try { if (iframeRef.current.contentDocument.location.href !== url) { iframeRef.current.src = url; } } catch (_a) { iframeRef.current.src = url; } }, [url, site]); return React.createElement( React.Fragment, null, React.createElement('iframe', { key: site, style: { width, height }, id: 'crafterCMSPreviewIframe', title: formatMessage(translations.iframeTitle), ref: iframeRef, className: cls }) ); } export default HostUI;