-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdvancedEditor.tsx
68 lines (59 loc) · 1.69 KB
/
AdvancedEditor.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
import React, { useRef, useState } from "react";
import { useEffect } from "react";
import EmailEditor from "react-email-editor";
import { useFormik } from "formik";
import * as yup from "yup";
const schema = yup.object({
// design: yup.string().required(),
// html: yup.string().required(),
});
let InitialData = {
html: "",
design: {},
};
const AdvancedEditor = () => {
const emailEditorRef = useRef<any>(null);
const [isReady, setReady] = useState(false);
const [initialValues, setInitialValues] = useState(InitialData);
const formik = useFormik({
initialValues: initialValues,
validationSchema: schema,
enableReinitialize: true,
onSubmit: (values) => {
console.log(values);
},
});
const onReady = () => {
// editor is ready
// you can load your template here;
if (!isReady) {
setReady(true);
if (
Object.keys(formik?.values?.design).length &&
emailEditorRef?.current?.editor
) {
emailEditorRef?.current?.editor?.loadDesign(formik?.values?.design);
}
}
};
useEffect(() => {
if (isReady && emailEditorRef?.current) {
emailEditorRef.current.addEventListener(
"design:updated",
function (data: any) {
emailEditorRef.current.editor.exportHtml((data: any) => {
const { design, html } = data;
formik.setFieldValue("design", design);
formik.setFieldValue("html", html);
});
}
);
}
}, [emailEditorRef, isReady, formik]);
const handleReset = () => {
setInitialValues(InitialData);
formik.resetForm();
};
return <EmailEditor ref={emailEditorRef} onReady={onReady} />;
};
export default AdvancedEditor;