88448cae by acetousk

Create publish button with html and grapes published version name fields, necess…

…ary rest endpoint, remove and add button when appropriate
1 parent 3169a2e4
...@@ -16,7 +16,9 @@ along with this software (see the LICENSE.md file). If not, see ...@@ -16,7 +16,9 @@ along with this software (see the LICENSE.md file). If not, see
16 16
17 <extend-entity entity-name="EmailTemplate" package="moqui.basic.email"> 17 <extend-entity entity-name="EmailTemplate" package="moqui.basic.email">
18 <field name="grapesLocation" type="text-medium"/> 18 <field name="grapesLocation" type="text-medium"/>
19 <field name="grapesPublishedVersionName" type="text-short"/>
19 <field name="htmlLocation" type="text-medium"/> 20 <field name="htmlLocation" type="text-medium"/>
21 <field name="htmlPublishedVersionName" type="text-short"/>
20 </extend-entity> 22 </extend-entity>
21 <view-entity entity-name="ProductStoreEmailDetail" package="mantle.product.store"> 23 <view-entity entity-name="ProductStoreEmailDetail" package="mantle.product.store">
22 <member-entity entity-alias="PSE" entity-name="mantle.product.store.ProductStoreEmail"/> 24 <member-entity entity-alias="PSE" entity-name="mantle.product.store.ProductStoreEmail"/>
......
...@@ -180,7 +180,11 @@ along with this software (see the LICENSE.md file). If not, see ...@@ -180,7 +180,11 @@ along with this software (see the LICENSE.md file). If not, see
180 } 180 }
181 // console.log('response ', response) 181 // console.log('response ', response)
182 window.htmlLocation = response.htmlLocation; 182 window.htmlLocation = response.htmlLocation;
183 window.htmlVersionName = response.htmlVersionName;
184 window.htmlPublishedVersionName = response.htmlPublishedVersionName;
183 window.grapesLocation = response.grapesLocation; 185 window.grapesLocation = response.grapesLocation;
186 window.grapesVersionName = response.grapesVersionName;
187 window.grapesPublishedVersionName = response.grapesPublishedVersionName;
184 // const url = new URL(window.location.href) 188 // const url = new URL(window.location.href)
185 // url.searchParams.set('htmlLocation', response.htmlLocation); 189 // url.searchParams.set('htmlLocation', response.htmlLocation);
186 // url.searchParams.set('grapesLocation', response.grapesLocation); 190 // url.searchParams.set('grapesLocation', response.grapesLocation);
...@@ -191,6 +195,67 @@ along with this software (see the LICENSE.md file). If not, see ...@@ -191,6 +195,67 @@ along with this software (see the LICENSE.md file). If not, see
191 const projectData = JSON.parse(response.data); 195 const projectData = JSON.parse(response.data);
192 // console.log('window.projectData ', window.projectData) 196 // console.log('window.projectData ', window.projectData)
193 197
198 function moquiPlugin(editor) {
199 // Use the API: https://grapesjs.com/docs/api/
200
201 window.moquiPublishText = 'moqui-publish';
202 editor.Commands.add(window.moquiPublishText, () => {
203 const request = new XMLHttpRequest();
204 request.open("POST", "${baseLinkUrl}/rest/s1/moqui-mjml/mjml?emailTemplateId="+window.emailTemplateId, false); // `false` makes the request synchronous
205 request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
206 request.setRequestHeader("X-CSRF-Token", document.getElementById('confMoquiSessionToken').value);
207 request.send(JSON.stringify({ publish:true, data: window.editor.getProjectData(), moquiVars:window.moquiVars, html:window.editor.runCommand('mjml-code-to-html')?.html }));
208 // console.log('request ', request)
209 if (request.status === 200) {
210 const result = JSON.parse(request.responseText);
211 window.htmlLocation = result.htmlLocation;
212 window.htmlVersionName = result.htmlVersionName;
213 window.htmlPublishedVersionName = result.htmlPublishedVersionName;
214 window.grapesLocation = result.grapesLocation;
215 window.grapesVersionName = result.grapesVersionName;
216 window.grapesPublishedVersionName = result.grapesPublishedVersionName;
217 if (window.grapesVersionName !== window.grapesPublishedVersionName && window.htmlVersionName !== window.htmlPublishedVersionName) {
218 editor.Panels.addButton('options', window.moquiPublishButtonSettings);
219 } else {
220 editor.Panels.removeButton('options', window.moquiPublishText);
221 }
222 }
223 });
224 editor.on('storage:end:store', () => {
225 // console.log('Storage store request ended');
226 const request = new XMLHttpRequest();
227 request.open("GET", "${baseLinkUrl}/rest/s1/moqui-mjml/mjml/afterMjmlStore?emailTemplateId="+window.emailTemplateId, false); // `false` makes the request synchronous
228 request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
229 request.setRequestHeader("X-CSRF-Token", document.getElementById('confMoquiSessionToken').value);
230 request.send();
231 if (request.status === 200) {
232 const result = JSON.parse(request.responseText);
233 window.htmlLocation = result.htmlLocation;
234 window.htmlVersionName = result.htmlVersionName;
235 window.htmlPublishedVersionName = result.htmlPublishedVersionName;
236 window.grapesLocation = result.grapesLocation;
237 window.grapesVersionName = result.grapesVersionName;
238 window.grapesPublishedVersionName = result.grapesPublishedVersionName;
239 if (window.grapesVersionName !== window.grapesPublishedVersionName && window.htmlVersionName !== window.htmlPublishedVersionName) {
240 editor.Panels.addButton('options', window.moquiPublishButtonSettings);
241 } else {
242 editor.Panels.removeButton('options', window.moquiPublishText);
243 }
244 }
245 });
246
247 // Add Publish button
248 window.moquiPublishButtonSettings = {
249 id: window.moquiPublishText,
250 command: 'moqui-publish',
251 attributes: { title: 'publish' },
252 label: `<div style="background-color: #f45e43; color:#2c2e35; padding: 0px 10px; border: none; border-radius: 10px; cursor: pointer;">Publish</div>`,
253 };
254 if (window.grapesVersionName !== window.grapesPublishedVersionName && window.htmlVersionName !== window.htmlPublishedVersionName) {
255 editor.Panels.addButton('options', window.moquiPublishButtonSettings);
256 }
257 }
258
194 window.editor = grapesjs.init({ 259 window.editor = grapesjs.init({
195 projectData: projectData, 260 projectData: projectData,
196 height: '100%', 261 height: '100%',
...@@ -226,11 +291,19 @@ along with this software (see the LICENSE.md file). If not, see ...@@ -226,11 +291,19 @@ along with this software (see the LICENSE.md file). If not, see
226 // url.searchParams.set('htmlLocation', result.htmlLocation); 291 // url.searchParams.set('htmlLocation', result.htmlLocation);
227 // url.searchParams.set('emailTemplateId', result.emailTemplateId); 292 // url.searchParams.set('emailTemplateId', result.emailTemplateId);
228 window.history.pushState({}, '', url) 293 window.history.pushState({}, '', url)
229 window.grapesLocation = result.grapesLocation;
230 window.htmlLocation = result.htmlLocation; 294 window.htmlLocation = result.htmlLocation;
295 window.htmlVersionName = result.htmlVersionName;
296 window.htmlPublishedVersionName = result.htmlPublishedVersionName;
297 window.grapesLocation = result.grapesLocation;
298 window.grapesVersionName = result.grapesVersionName;
299 window.grapesPublishedVersionName = result.grapesPublishedVersionName;
231 window.emailTemplateId = result.emailTemplateId; 300 window.emailTemplateId = result.emailTemplateId;
232 window.moquiVars = result.moquiVars; 301 window.moquiVars = result.moquiVars;
233 // console.log('onLoad window.moquiVars ', window.moquiVars) 302 if (window.grapesVersionName !== window.grapesPublishedVersionName && window.htmlVersionName !== window.htmlPublishedVersionName) {
303 editor.Panels.addButton('options', window.moquiPublishButtonSettings);
304 } else {
305 editor.Panels.removeButton('options', window.moquiPublishText);
306 }
234 } 307 }
235 // console.log('onLoad ', result) 308 // console.log('onLoad ', result)
236 return result.data 309 return result.data
...@@ -241,7 +314,7 @@ along with this software (see the LICENSE.md file). If not, see ...@@ -241,7 +314,7 @@ along with this software (see the LICENSE.md file). If not, see
241 fromElement: true, 314 fromElement: true,
242 container: '#gjs', 315 container: '#gjs',
243 316
244 plugins: ['grapesjs-mjml'], 317 plugins: ['grapesjs-mjml',moquiPlugin],
245 pluginsOpts: { 318 pluginsOpts: {
246 'grapesjs-mjml': {} 319 'grapesjs-mjml': {}
247 } 320 }
......
...@@ -21,6 +21,9 @@ along with this software (see the LICENSE.md file). If not, see ...@@ -21,6 +21,9 @@ along with this software (see the LICENSE.md file). If not, see
21 <resource name="mjml"> 21 <resource name="mjml">
22 <method type="get"><service name="mjml.MjmlServices.load#GrapeJs"/></method> 22 <method type="get"><service name="mjml.MjmlServices.load#GrapeJs"/></method>
23 <method type="post"><service name="mjml.MjmlServices.store#GrapeJs"/></method> 23 <method type="post"><service name="mjml.MjmlServices.store#GrapeJs"/></method>
24 <resource name="afterMjmlStore">
25 <method type="get"><service name="mjml.MjmlServices.get#AfterMjmlStore"/></method>
26 </resource>
24 </resource> 27 </resource>
25 28
26 </resource> 29 </resource>
......
...@@ -34,7 +34,11 @@ along with this software (see the LICENSE.md file). If not, see ...@@ -34,7 +34,11 @@ along with this software (see the LICENSE.md file). If not, see
34 </in-parameters> 34 </in-parameters>
35 <out-parameters> 35 <out-parameters>
36 <parameter name="grapesLocation"/> 36 <parameter name="grapesLocation"/>
37 <parameter name="grapesVersionName"/>
38 <parameter name="grapesPublishedVersionName"/>
37 <parameter name="htmlLocation"/> 39 <parameter name="htmlLocation"/>
40 <parameter name="htmlVersionName"/>
41 <parameter name="htmlPublishedVersionName"/>
38 <parameter name="data"/> 42 <parameter name="data"/>
39 <parameter name="emailTemplateId"/> 43 <parameter name="emailTemplateId"/>
40 <parameter name="moquiVars"/> 44 <parameter name="moquiVars"/>
...@@ -45,7 +49,9 @@ along with this software (see the LICENSE.md file). If not, see ...@@ -45,7 +49,9 @@ along with this software (see the LICENSE.md file). If not, see
45 <entity-find-one entity-name="moqui.basic.email.EmailTemplate" value-field="emailTemplate" auto-field-map="[emailTemplateId:emailTemplateId]"/> 49 <entity-find-one entity-name="moqui.basic.email.EmailTemplate" value-field="emailTemplate" auto-field-map="[emailTemplateId:emailTemplateId]"/>
46 <if condition="!emailTemplate"><return error="true" message="Resource not found"/></if> 50 <if condition="!emailTemplate"><return error="true" message="Resource not found"/></if>
47 <set field="grapesLocation" from="emailTemplate?.grapesLocation"/> 51 <set field="grapesLocation" from="emailTemplate?.grapesLocation"/>
52 <set field="grapesPublishedVersionName" from="emailTemplate?.grapesPublishedVersionName"/>
48 <set field="htmlLocation" from="emailTemplate?.htmlLocation"/> 53 <set field="htmlLocation" from="emailTemplate?.htmlLocation"/>
54 <set field="htmlPublishedVersionName" from="emailTemplate?.htmlPublishedVersionName"/>
49 55
50 <set field="grapesJsResource" from="ec.resource.getLocationReference('dbresource://grapesjs/project')"/> 56 <set field="grapesJsResource" from="ec.resource.getLocationReference('dbresource://grapesjs/project')"/>
51 <if condition="!grapesLocation &amp;&amp; !htmlLocation"> 57 <if condition="!grapesLocation &amp;&amp; !htmlLocation">
...@@ -62,6 +68,8 @@ along with this software (see the LICENSE.md file). If not, see ...@@ -62,6 +68,8 @@ along with this software (see the LICENSE.md file). If not, see
62 grapesFile.putText(dataRaw) 68 grapesFile.putText(dataRaw)
63 grapesFile.move(grapesLocation) 69 grapesFile.move(grapesLocation)
64 ]]></script> 70 ]]></script>
71 <set field="htmlVersionName" from="htmlFile?.getCurrentVersion()?.getVersionName()"/>
72 <set field="grapesVersionName" from="grapesFile?.getCurrentVersion()?.getVersionName()"/>
65 <service-call name="update#moqui.basic.email.EmailTemplate" in-map="[emailTemplateId:emailTemplateId,grapesLocation:grapesLocation,htmlLocation:htmlLocation]"/> 73 <service-call name="update#moqui.basic.email.EmailTemplate" in-map="[emailTemplateId:emailTemplateId,grapesLocation:grapesLocation,htmlLocation:htmlLocation]"/>
66 74
67 <service-call name="mjml.MjmlServices.get#GlobalGrapeVarList" out-map="globalGrapeVarList"/> 75 <service-call name="mjml.MjmlServices.get#GlobalGrapeVarList" out-map="globalGrapeVarList"/>
...@@ -120,6 +128,8 @@ along with this software (see the LICENSE.md file). If not, see ...@@ -120,6 +128,8 @@ along with this software (see the LICENSE.md file). If not, see
120 <if condition="!grapesFile || grapesFile.parent?.location != grapesJsResource.location"> 128 <if condition="!grapesFile || grapesFile.parent?.location != grapesJsResource.location">
121 <return error="true" message="Resource not found"/> 129 <return error="true" message="Resource not found"/>
122 </if> 130 </if>
131 <set field="grapesVersionName" from="grapesFile?.getCurrentVersion()?.getVersionName()"/>
132 <set field="htmlVersionName" from="ec.resource.getLocationReference(htmlLocation)?.getCurrentVersion()?.getVersionName()"/>
123 133
124 <service-call name="mjml.MjmlServices.get#GlobalGrapeVarList" out-map="globalGrapeVarList"/> 134 <service-call name="mjml.MjmlServices.get#GlobalGrapeVarList" out-map="globalGrapeVarList"/>
125 <set field="dataRaw" from="grapesFile.getText()"/> 135 <set field="dataRaw" from="grapesFile.getText()"/>
...@@ -183,14 +193,19 @@ along with this software (see the LICENSE.md file). If not, see ...@@ -183,14 +193,19 @@ along with this software (see the LICENSE.md file). If not, see
183 <in-parameters> 193 <in-parameters>
184 <parameter name="htmlLocation"/> 194 <parameter name="htmlLocation"/>
185 <parameter name="grapesLocation"/> 195 <parameter name="grapesLocation"/>
186 <parameter name="emailTemplateId"/> 196 <parameter name="emailTemplateId" required="true"/>
187 <parameter name="data"/> 197 <parameter name="data" required="true"/>
188 <parameter name="moquiVars"/> 198 <parameter name="moquiVars" required="true"/>
189 <parameter name="html" allow-html="any"/> 199 <parameter name="html" allow-html="any" required="true"/>
200 <parameter name="publish" default="false" type="Boolean" required="true"/>
190 </in-parameters> 201 </in-parameters>
191 <out-parameters> 202 <out-parameters>
192 <parameter name="htmlLocation"/> 203 <parameter name="htmlLocation"/>
204 <parameter name="htmlVersionName"/>
205 <parameter name="htmlPublishedVersionName"/>
193 <parameter name="grapesLocation"/> 206 <parameter name="grapesLocation"/>
207 <parameter name="grapesVersionName"/>
208 <parameter name="grapesPublishedVersionName"/>
194 </out-parameters> 209 </out-parameters>
195 <actions> 210 <actions>
196 <if condition="htmlLocation == 'null'"><set field="htmlLocation" from="null"/></if> 211 <if condition="htmlLocation == 'null'"><set field="htmlLocation" from="null"/></if>
...@@ -198,7 +213,9 @@ along with this software (see the LICENSE.md file). If not, see ...@@ -198,7 +213,9 @@ along with this software (see the LICENSE.md file). If not, see
198 <entity-find-one entity-name="moqui.basic.email.EmailTemplate" value-field="emailTemplate" auto-field-map="[emailTemplateId:emailTemplateId]"/> 213 <entity-find-one entity-name="moqui.basic.email.EmailTemplate" value-field="emailTemplate" auto-field-map="[emailTemplateId:emailTemplateId]"/>
199 <if condition="!emailTemplate"><return error="true" message="Resource not found"/></if> 214 <if condition="!emailTemplate"><return error="true" message="Resource not found"/></if>
200 <set field="grapesLocation" from="emailTemplate?.grapesLocation"/> 215 <set field="grapesLocation" from="emailTemplate?.grapesLocation"/>
216 <set field="grapesPublishedVersionName" from="emailTemplate?.grapesPublishedVersionName"/>
201 <set field="htmlLocation" from="emailTemplate?.htmlLocation"/> 217 <set field="htmlLocation" from="emailTemplate?.htmlLocation"/>
218 <set field="htmlPublishedVersionName" from="emailTemplate?.htmlPublishedVersionName"/>
202 <set field="dataMap" from="new groovy.json.JsonSlurper().parseText(ec.web.secureRequestParameters._requestBodyText).data"/> 219 <set field="dataMap" from="new groovy.json.JsonSlurper().parseText(ec.web.secureRequestParameters._requestBodyText).data"/>
203 <!-- <log level="warn" message="dataMap is ${dataMap}"/>--> 220 <!-- <log level="warn" message="dataMap is ${dataMap}"/>-->
204 <set field="moquiVars" from="new groovy.json.JsonSlurper().parseText(ec.web.secureRequestParameters._requestBodyText).moquiVars"/> 221 <set field="moquiVars" from="new groovy.json.JsonSlurper().parseText(ec.web.secureRequestParameters._requestBodyText).moquiVars"/>
...@@ -243,10 +260,7 @@ along with this software (see the LICENSE.md file). If not, see ...@@ -243,10 +260,7 @@ along with this software (see the LICENSE.md file). If not, see
243 } 260 }
244 ]]></script> 261 ]]></script>
245 <script>grapesFile.putText(dataPre)</script> 262 <script>grapesFile.putText(dataPre)</script>
246 263 <set field="grapesVersionName" from="grapesFile?.getCurrentVersion()?.getVersionName()"/>
247 <!-- <set field="dataRaw" from="grapesFile.getText()"/>-->
248 <!-- <set field="dataRawMap" from="new groovy.json.JsonSlurper().parseText(dataRaw)"/>-->
249 <!-- <script>grapesFile.putText(data)</script>-->
250 264
251 <set field="htmlFile" from="null"/> 265 <set field="htmlFile" from="null"/>
252 <if condition="htmlLocation"><then> 266 <if condition="htmlLocation"><then>
...@@ -269,7 +283,14 @@ along with this software (see the LICENSE.md file). If not, see ...@@ -269,7 +283,14 @@ along with this software (see the LICENSE.md file). If not, see
269 <if condition="!htmlFile || htmlFile.parent?.location != grapesJsResource.location"> 283 <if condition="!htmlFile || htmlFile.parent?.location != grapesJsResource.location">
270 <return error="true" message="Resource not found"/> 284 <return error="true" message="Resource not found"/>
271 </if> 285 </if>
272 <!-- <script><![CDATA[htmlFile.putText(html)]]></script>--> 286 <script><![CDATA[htmlFile.putText(html)]]></script>
287 <set field="htmlVersionName" from="htmlFile?.getCurrentVersion()?.getVersionName()"/>
288 <if condition="publish">
289 <service-call name="update#moqui.basic.email.EmailTemplate" in-map="[emailTemplateId:emailTemplateId,
290 htmlPublishedVersionName:htmlVersionName,grapesPublishedVersionName:grapesVersionName]"/>
291 <set field="htmlPublishedVersionName" from="htmlVersionName"/>
292 <set field="grapesPublishedVersionName" from="grapesVersionName"/>
293 </if>
273 </else-if> 294 </else-if>
274 <else> 295 <else>
275 <return error="true" message="Resource not found"/> 296 <return error="true" message="Resource not found"/>
...@@ -277,6 +298,29 @@ along with this software (see the LICENSE.md file). If not, see ...@@ -277,6 +298,29 @@ along with this software (see the LICENSE.md file). If not, see
277 </if> 298 </if>
278 </actions> 299 </actions>
279 </service> 300 </service>
280 301 <service verb="get" noun="AfterMjmlStore">
302 <description>Get the data after storing the MJML data</description>
303 <in-parameters>
304 <parameter name="emailTemplateId" required="true"/>
305 </in-parameters>
306 <out-parameters>
307 <parameter name="htmlLocation"/>
308 <parameter name="htmlVersionName"/>
309 <parameter name="htmlPublishedVersionName"/>
310 <parameter name="grapesLocation"/>
311 <parameter name="grapesVersionName"/>
312 <parameter name="grapesPublishedVersionName"/>
313 </out-parameters>
314 <actions>
315 <entity-find-one entity-name="moqui.basic.email.EmailTemplate" value-field="emailTemplate" auto-field-map="[emailTemplateId:emailTemplateId]"/>
316 <if condition="!emailTemplate"><return error="true" message="Resource not found"/></if>
317 <set field="grapesLocation" from="emailTemplate?.grapesLocation"/>
318 <set field="grapesVersionName" from="ec.resource.getLocationReference(grapesLocation)?.getCurrentVersion()?.getVersionName()"/>
319 <set field="grapesPublishedVersionName" from="emailTemplate?.grapesPublishedVersionName"/>
320 <set field="htmlLocation" from="emailTemplate?.htmlLocation"/>
321 <set field="htmlVersionName" from="ec.resource.getLocationReference(htmlLocation)?.getCurrentVersion()?.getVersionName()"/>
322 <set field="htmlPublishedVersionName" from="emailTemplate?.htmlPublishedVersionName"/>
323 </actions>
324 </service>
281 325
282 </services> 326 </services>
......