@@ -2,13 +2,32 @@ import React, {useRef, useState} from "react";
22import MailUiEditor , { MailUiEditorProps , MailUiEditorRef } from "../../src" ;
33import packageJson from "../package.json" ;
44import sampleTemplate from "./sampleTemplate.json" ;
5+ import unlayerTemplate from "./unlayer-template.json" ;
56import { FaDesktop , FaMobile , FaRedoAlt , FaTablet , FaUndoAlt } from "react-icons/fa" ;
6- import crypto from "crypto " ;
7+ import { generateSignature } from "../utils " ;
78
89const classNames = ( ...args : ( string | undefined | null | false | number ) [ ] ) : string => {
910 return args . filter ( arg => typeof arg === 'string' ) . join ( ' ' ) ;
1011} ;
1112
13+ const displayConditions = [
14+ {
15+ label : "Men" ,
16+ description : "All man customers" ,
17+ key : "ismen" ,
18+ } ,
19+ {
20+ label : "Women" ,
21+ description : "All women customers" ,
22+ key : "iswomen" ,
23+ } ,
24+ {
25+ label : "Children" ,
26+ description : "All children customers" ,
27+ key : "ischild" ,
28+ }
29+ ]
30+
1231const BasicExample = ( ) => {
1332 const mailUiEditorRef = useRef < MailUiEditorRef | null > ( null ) ;
1433 const [ preview , setPreview ] = useState ( false ) ;
@@ -109,6 +128,7 @@ const BasicExample = () => {
109128 const loadTemplate = ( ) => {
110129 const mailui = mailUiEditorRef . current ?. editor ;
111130 mailui ?. loadTemplate ( 12345 ) ;
131+
112132 } ;
113133
114134 const loadDesign = ( ) => {
@@ -130,20 +150,26 @@ const BasicExample = () => {
130150 const onLoad : MailUiEditorProps [ 'onLoad' ] = ( mailui ) => {
131151 // console.log("onLoad", mailui);
132152
133- mailui . addEventListener ( "design:updated" , ( data : {
134- type : string
135- } ) => {
136- const type = data . type ; // type will be of type string
137- console . log ( "__design:updated__" , data , type ) ;
153+ // mailui.addEventListener("design:updated", (data: {
154+ // type: string
155+ // }) => {
156+ // const type = data.type; // type will be of type string
157+ // console.log("__design:updated__", data, type);
158+ // });
159+
160+ mailui . addEventListener ( "design:updated" , function ( ) {
161+ mailui . exportJson ( function ( data ) {
162+ console . log ( 'JSON' , data ) ;
163+ } ) ;
138164 } ) ;
139165
140166
141167 mailui . addEventListener ( "design:loaded" , onDesignLoad ) ;
142168 // mailui.loadDesign({});
143- mailui . addEventListener ( "design:updated" , function ( data : object ) {
144- // var type = data.type; // html:updated
145- console . log ( "__design:updated__" , data ) ;
146- } ) ;
169+ // mailui.addEventListener("design:updated", function (data: object) {
170+ // // var type = data.type; // html:updated
171+ // console.log("__design:updated__", data);
172+ // });
147173 mailui . addEventListener ( "content:modified" , function ( data : object ) {
148174 // var type = data.type; // html:updated
149175 console . log ( "__content:modified__" , data ) ;
@@ -234,15 +260,48 @@ const BasicExample = () => {
234260 console . log ( "library:saveAuthAlert" , params ) ;
235261 done ( true ) ;
236262 } ) ;
263+ mailui . registerCallback ( "displayCondition" , async ( params : object , done : Function ) => {
264+ console . log ( "displayCondition" , params ?. condition ) ;
265+
266+ done ( {
267+ label : "Men" ,
268+ description : "All man customers" ,
269+ key : "ismen" ,
270+ } )
271+ } ) ;
272+
273+ mailui . registerCallback ( "previewHtml" , async ( params : object , done : Function ) => {
274+ console . log ( "previewHtml" , params ) ;
275+
276+ var conditionKey = prompt (
277+ `Please enter a condition key: ${ displayConditions
278+ . map ( ( e ) => e . key )
279+ . join ( ", " ) } `,
280+ displayConditions [ 0 ] ?. key
281+ ) ;
282+
283+ if ( conditionKey === null || conditionKey === "" ) {
284+ console . log ( "User cancelled the prompt." ) ;
285+ }
286+
287+ var data = displayConditions . reduce ( ( acc , condition ) => {
288+ acc [ condition . key ] =
289+ condition . key === conditionKey ||
290+ condition . key === conditionKey . toLowerCase ( ) ;
291+ return acc ;
292+ } , { } ) ;
293+
294+ done ( data )
295+ } ) ;
296+
297+ mailui ?. setDisplayConditions ( displayConditions )
237298 } ;
238299
239300 const onReady : MailUiEditorProps [ 'onReady' ] = ( mailui ) => {
240301 console . log ( "onReady" , mailui ) ;
241302 } ;
242303
243- const projectId = process . env . NEXTAUTH_MAILUI_PROJECT_ID || "" ;
244- const secretKey = process . env . NEXTAUTH_MAILUI_SECRET || "" ;
245- const signature = crypto . createHmac ( "sha256" , secretKey ) . update ( projectId ) . digest ( "hex" ) ;
304+ const signature = generateSignature ( ) ;
246305
247306 return (
248307 < main className = "App" >
@@ -323,8 +382,14 @@ const BasicExample = () => {
323382 onLoad = { onLoad }
324383 onReady = { onReady }
325384 minHeight = "calc(100vh - 57px)"
385+ scriptUrl = {
386+ process . env . NODE_ENV === "production"
387+ ? "https://editor.mailui.co/embed.min.js"
388+ : "https://editor.mailui.co/embed.local.min.js"
389+ }
326390 options = { {
327391 signature,
392+ projectId : process . env . REACT_APP_MAILUI_PROJECT_ID ,
328393
329394 defaultDevice : "desktop" ,
330395 devices : [ "desktop" , "tablet" , "mobile" ] ,
@@ -350,6 +415,9 @@ const BasicExample = () => {
350415 enabled : true ,
351416 authAlert : true ,
352417 } ,
418+ displayCondition : {
419+ enabled : true ,
420+ } ,
353421 } ,
354422 excludeTools : [ ] ,
355423 mergeTags : [
@@ -379,6 +447,7 @@ const BasicExample = () => {
379447 ] ,
380448 } ,
381449 ] ,
450+ protectedModules : [ ]
382451 } }
383452 />
384453 </ main >
0 commit comments