Skip to content

Commit

Permalink
feat: use iframe for preview
Browse files Browse the repository at this point in the history
  • Loading branch information
luciosnd committed Sep 13, 2024
1 parent 1ded24d commit 173f167
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
41 changes: 41 additions & 0 deletions packages/editor-sample/src/App/TemplatePanel/EmailPreview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React, { useMemo } from 'react';
import { useEffect, useRef } from 'react';
import { useImages } from '../../documents/editor/EditorContext';

interface EmailPreviewProps {
html: string;
}

export const EmailPreview = ({ html }: EmailPreviewProps) => {
const images = useImages();
const iframeRef = useRef<HTMLIFrameElement>(null);

const content = useMemo(() => {
let copy = html;
images.forEach((image) => {
copy = copy.replaceAll(image.file.name, image.base64);
})
return copy
}, [images]);

useEffect(() => {
if (iframeRef.current && content) {
const newChild = new DOMParser().parseFromString(content, 'text/html');
iframeRef.current?.contentDocument?.replaceChild(
newChild.documentElement,
iframeRef.current.contentDocument.documentElement
);
}
}, [iframeRef.current, content]);

return (
<iframe
ref={iframeRef}
style={{
width: '100%',
height: '100%',
}}
title="Email preview"
/>
);
};
9 changes: 6 additions & 3 deletions packages/editor-sample/src/App/TemplatePanel/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import React, { useMemo } from 'react';

import { MonitorOutlined, PhoneIphoneOutlined } from '@mui/icons-material';
import { Box, Stack, SxProps, ToggleButton, ToggleButtonGroup, Tooltip } from '@mui/material';
import { Reader } from '@usewaypoint/email-builder';
import { Reader, renderToStaticMarkup } from '@usewaypoint/email-builder';

import EditorBlock from '../../documents/editor/EditorBlock';
import {
Expand All @@ -20,11 +20,13 @@ import ImportJson from './ImportJson';
import JsonPanel from './JsonPanel';
import MainTabsGroup from './MainTabsGroup';
import ShareButton from './ShareButton';
import { EmailPreview } from './EmailPreview';

export default function TemplatePanel() {
const document = useDocument();
const selectedMainTab = useSelectedMainTab();
const selectedScreenSize = useSelectedScreenSize();
const preview = useMemo(() => renderToStaticMarkup(document, { rootBlockId: 'root' }), [document])

let mainBoxSx: SxProps = {
height: '100%',
Expand Down Expand Up @@ -62,7 +64,8 @@ export default function TemplatePanel() {
case 'preview':
return (
<Box sx={mainBoxSx}>
<Reader document={document} rootBlockId="root" />
<EmailPreview html={preview}/>
{/* <Reader document={document} rootBlockId="root" /> */}
</Box>
);
case 'html':
Expand Down

0 comments on commit 173f167

Please sign in to comment.