-
Notifications
You must be signed in to change notification settings - Fork 747
/
index.tsx
121 lines (102 loc) · 2.83 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import React, { useRef, useState } from 'react';
import styled from 'styled-components';
import packageJson from '../../../package.json';
import EmailEditor, { EditorRef, EmailEditorProps } from '../../../src'; // use react-email-editor instead
import sample from './sample.json';
const Container = styled.div`
display: flex;
flex-direction: column;
position: relative;
height: 100%;
`;
const Bar = styled.div`
flex: 1;
background-color: #61dafb;
color: #000;
padding: 10px;
display: flex;
max-height: 40px;
h1 {
flex: 1;
font-size: 16px;
text-align: left;
}
button {
flex: 1;
padding: 10px;
margin-left: 10px;
font-size: 14px;
font-weight: bold;
background-color: #000;
color: #fff;
border: 0px;
max-width: 150px;
cursor: pointer;
}
`;
const Example = () => {
const emailEditorRef = useRef<EditorRef | null>(null);
const [preview, setPreview] = useState(false);
const saveDesign = () => {
const unlayer = emailEditorRef.current?.editor;
unlayer?.saveDesign((design) => {
console.log('saveDesign', design);
alert('Design JSON has been logged in your developer console.');
});
};
const exportHtml = () => {
const unlayer = emailEditorRef.current?.editor;
unlayer?.exportHtml((data) => {
const { design, html } = data;
console.log('exportHtml', html);
alert('Output HTML has been logged in your developer console.');
});
};
const togglePreview = () => {
const unlayer = emailEditorRef.current?.editor;
if (preview) {
unlayer?.hidePreview();
setPreview(false);
} else {
unlayer?.showPreview('desktop');
setPreview(true);
}
};
const onDesignLoad = (data) => {
console.log('onDesignLoad', data);
};
const onLoad: EmailEditorProps['onLoad'] = (unlayer) => {
console.log('onLoad', unlayer);
unlayer.addEventListener('design:loaded', onDesignLoad);
unlayer.loadDesign(sample);
};
const onReady: EmailEditorProps['onReady'] = (unlayer) => {
console.log('onReady', unlayer);
};
return (
<Container>
<Bar>
<h1>React Email Editor v{packageJson.version} (Demo) — (<a href="https://github.com/unlayer/react-email-editor" target="_blank">GitHub</a>)</h1>
<button onClick={togglePreview}>
{preview ? 'Hide' : 'Show'} Preview
</button>
<button onClick={saveDesign}>Save Design</button>
<button onClick={exportHtml}>Export HTML</button>
</Bar>
<React.StrictMode>
<EmailEditor
ref={emailEditorRef}
onLoad={onLoad}
onReady={onReady}
options={{
version: "latest",
appearance: {
theme: "modern_light"
}
}}
/>
</React.StrictMode>
</Container>
);
};
export default Example;