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
<extend-entity entity-name="EmailTemplate" package="moqui.basic.email">
<field name="grapesLocation" type="text-medium"/>
<field name="grapesPublishedVersionName" type="text-short"/>
<field name="htmlLocation" type="text-medium"/>
<field name="htmlPublishedVersionName" type="text-short"/>
</extend-entity>
<view-entity entity-name="ProductStoreEmailDetail" package="mantle.product.store">
<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
}
// console.log('response ', response)
window.htmlLocation = response.htmlLocation;
window.htmlVersionName = response.htmlVersionName;
window.htmlPublishedVersionName = response.htmlPublishedVersionName;
window.grapesLocation = response.grapesLocation;
window.grapesVersionName = response.grapesVersionName;
window.grapesPublishedVersionName = response.grapesPublishedVersionName;
// const url = new URL(window.location.href)
// url.searchParams.set('htmlLocation', response.htmlLocation);
// url.searchParams.set('grapesLocation', response.grapesLocation);
......@@ -191,6 +195,67 @@ along with this software (see the LICENSE.md file). If not, see
const projectData = JSON.parse(response.data);
// console.log('window.projectData ', window.projectData)
function moquiPlugin(editor) {
// Use the API: https://grapesjs.com/docs/api/
window.moquiPublishText = 'moqui-publish';
editor.Commands.add(window.moquiPublishText, () => {
const request = new XMLHttpRequest();
request.open("POST", "${baseLinkUrl}/rest/s1/moqui-mjml/mjml?emailTemplateId="+window.emailTemplateId, false); // `false` makes the request synchronous
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
request.setRequestHeader("X-CSRF-Token", document.getElementById('confMoquiSessionToken').value);
request.send(JSON.stringify({ publish:true, data: window.editor.getProjectData(), moquiVars:window.moquiVars, html:window.editor.runCommand('mjml-code-to-html')?.html }));
// console.log('request ', request)
if (request.status === 200) {
const result = JSON.parse(request.responseText);
window.htmlLocation = result.htmlLocation;
window.htmlVersionName = result.htmlVersionName;
window.htmlPublishedVersionName = result.htmlPublishedVersionName;
window.grapesLocation = result.grapesLocation;
window.grapesVersionName = result.grapesVersionName;
window.grapesPublishedVersionName = result.grapesPublishedVersionName;
if (window.grapesVersionName !== window.grapesPublishedVersionName && window.htmlVersionName !== window.htmlPublishedVersionName) {
editor.Panels.addButton('options', window.moquiPublishButtonSettings);
} else {
editor.Panels.removeButton('options', window.moquiPublishText);
}
}
});
editor.on('storage:end:store', () => {
// console.log('Storage store request ended');
const request = new XMLHttpRequest();
request.open("GET", "${baseLinkUrl}/rest/s1/moqui-mjml/mjml/afterMjmlStore?emailTemplateId="+window.emailTemplateId, false); // `false` makes the request synchronous
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
request.setRequestHeader("X-CSRF-Token", document.getElementById('confMoquiSessionToken').value);
request.send();
if (request.status === 200) {
const result = JSON.parse(request.responseText);
window.htmlLocation = result.htmlLocation;
window.htmlVersionName = result.htmlVersionName;
window.htmlPublishedVersionName = result.htmlPublishedVersionName;
window.grapesLocation = result.grapesLocation;
window.grapesVersionName = result.grapesVersionName;
window.grapesPublishedVersionName = result.grapesPublishedVersionName;
if (window.grapesVersionName !== window.grapesPublishedVersionName && window.htmlVersionName !== window.htmlPublishedVersionName) {
editor.Panels.addButton('options', window.moquiPublishButtonSettings);
} else {
editor.Panels.removeButton('options', window.moquiPublishText);
}
}
});
// Add Publish button
window.moquiPublishButtonSettings = {
id: window.moquiPublishText,
command: 'moqui-publish',
attributes: { title: 'publish' },
label: `<div style="background-color: #f45e43; color:#2c2e35; padding: 0px 10px; border: none; border-radius: 10px; cursor: pointer;">Publish</div>`,
};
if (window.grapesVersionName !== window.grapesPublishedVersionName && window.htmlVersionName !== window.htmlPublishedVersionName) {
editor.Panels.addButton('options', window.moquiPublishButtonSettings);
}
}
window.editor = grapesjs.init({
projectData: projectData,
height: '100%',
......@@ -226,11 +291,19 @@ along with this software (see the LICENSE.md file). If not, see
// url.searchParams.set('htmlLocation', result.htmlLocation);
// url.searchParams.set('emailTemplateId', result.emailTemplateId);
window.history.pushState({}, '', url)
window.grapesLocation = result.grapesLocation;
window.htmlLocation = result.htmlLocation;
window.htmlVersionName = result.htmlVersionName;
window.htmlPublishedVersionName = result.htmlPublishedVersionName;
window.grapesLocation = result.grapesLocation;
window.grapesVersionName = result.grapesVersionName;
window.grapesPublishedVersionName = result.grapesPublishedVersionName;
window.emailTemplateId = result.emailTemplateId;
window.moquiVars = result.moquiVars;
// console.log('onLoad window.moquiVars ', window.moquiVars)
if (window.grapesVersionName !== window.grapesPublishedVersionName && window.htmlVersionName !== window.htmlPublishedVersionName) {
editor.Panels.addButton('options', window.moquiPublishButtonSettings);
} else {
editor.Panels.removeButton('options', window.moquiPublishText);
}
}
// console.log('onLoad ', result)
return result.data
......@@ -241,7 +314,7 @@ along with this software (see the LICENSE.md file). If not, see
fromElement: true,
container: '#gjs',
plugins: ['grapesjs-mjml'],
plugins: ['grapesjs-mjml',moquiPlugin],
pluginsOpts: {
'grapesjs-mjml': {}
}
......
......@@ -21,6 +21,9 @@ along with this software (see the LICENSE.md file). If not, see
<resource name="mjml">
<method type="get"><service name="mjml.MjmlServices.load#GrapeJs"/></method>
<method type="post"><service name="mjml.MjmlServices.store#GrapeJs"/></method>
<resource name="afterMjmlStore">
<method type="get"><service name="mjml.MjmlServices.get#AfterMjmlStore"/></method>
</resource>
</resource>
</resource>
......
......@@ -34,7 +34,11 @@ along with this software (see the LICENSE.md file). If not, see
</in-parameters>
<out-parameters>
<parameter name="grapesLocation"/>
<parameter name="grapesVersionName"/>
<parameter name="grapesPublishedVersionName"/>
<parameter name="htmlLocation"/>
<parameter name="htmlVersionName"/>
<parameter name="htmlPublishedVersionName"/>
<parameter name="data"/>
<parameter name="emailTemplateId"/>
<parameter name="moquiVars"/>
......@@ -45,7 +49,9 @@ along with this software (see the LICENSE.md file). If not, see
<entity-find-one entity-name="moqui.basic.email.EmailTemplate" value-field="emailTemplate" auto-field-map="[emailTemplateId:emailTemplateId]"/>
<if condition="!emailTemplate"><return error="true" message="Resource not found"/></if>
<set field="grapesLocation" from="emailTemplate?.grapesLocation"/>
<set field="grapesPublishedVersionName" from="emailTemplate?.grapesPublishedVersionName"/>
<set field="htmlLocation" from="emailTemplate?.htmlLocation"/>
<set field="htmlPublishedVersionName" from="emailTemplate?.htmlPublishedVersionName"/>
<set field="grapesJsResource" from="ec.resource.getLocationReference('dbresource://grapesjs/project')"/>
<if condition="!grapesLocation &amp;&amp; !htmlLocation">
......@@ -62,6 +68,8 @@ along with this software (see the LICENSE.md file). If not, see
grapesFile.putText(dataRaw)
grapesFile.move(grapesLocation)
]]></script>
<set field="htmlVersionName" from="htmlFile?.getCurrentVersion()?.getVersionName()"/>
<set field="grapesVersionName" from="grapesFile?.getCurrentVersion()?.getVersionName()"/>
<service-call name="update#moqui.basic.email.EmailTemplate" in-map="[emailTemplateId:emailTemplateId,grapesLocation:grapesLocation,htmlLocation:htmlLocation]"/>
<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
<if condition="!grapesFile || grapesFile.parent?.location != grapesJsResource.location">
<return error="true" message="Resource not found"/>
</if>
<set field="grapesVersionName" from="grapesFile?.getCurrentVersion()?.getVersionName()"/>
<set field="htmlVersionName" from="ec.resource.getLocationReference(htmlLocation)?.getCurrentVersion()?.getVersionName()"/>
<service-call name="mjml.MjmlServices.get#GlobalGrapeVarList" out-map="globalGrapeVarList"/>
<set field="dataRaw" from="grapesFile.getText()"/>
......@@ -183,14 +193,19 @@ along with this software (see the LICENSE.md file). If not, see
<in-parameters>
<parameter name="htmlLocation"/>
<parameter name="grapesLocation"/>
<parameter name="emailTemplateId"/>
<parameter name="data"/>
<parameter name="moquiVars"/>
<parameter name="html" allow-html="any"/>
<parameter name="emailTemplateId" required="true"/>
<parameter name="data" required="true"/>
<parameter name="moquiVars" required="true"/>
<parameter name="html" allow-html="any" required="true"/>
<parameter name="publish" default="false" type="Boolean" required="true"/>
</in-parameters>
<out-parameters>
<parameter name="htmlLocation"/>
<parameter name="htmlVersionName"/>
<parameter name="htmlPublishedVersionName"/>
<parameter name="grapesLocation"/>
<parameter name="grapesVersionName"/>
<parameter name="grapesPublishedVersionName"/>
</out-parameters>
<actions>
<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
<entity-find-one entity-name="moqui.basic.email.EmailTemplate" value-field="emailTemplate" auto-field-map="[emailTemplateId:emailTemplateId]"/>
<if condition="!emailTemplate"><return error="true" message="Resource not found"/></if>
<set field="grapesLocation" from="emailTemplate?.grapesLocation"/>
<set field="grapesPublishedVersionName" from="emailTemplate?.grapesPublishedVersionName"/>
<set field="htmlLocation" from="emailTemplate?.htmlLocation"/>
<set field="htmlPublishedVersionName" from="emailTemplate?.htmlPublishedVersionName"/>
<set field="dataMap" from="new groovy.json.JsonSlurper().parseText(ec.web.secureRequestParameters._requestBodyText).data"/>
<!-- <log level="warn" message="dataMap is ${dataMap}"/>-->
<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
}
]]></script>
<script>grapesFile.putText(dataPre)</script>
<!-- <set field="dataRaw" from="grapesFile.getText()"/>-->
<!-- <set field="dataRawMap" from="new groovy.json.JsonSlurper().parseText(dataRaw)"/>-->
<!-- <script>grapesFile.putText(data)</script>-->
<set field="grapesVersionName" from="grapesFile?.getCurrentVersion()?.getVersionName()"/>
<set field="htmlFile" from="null"/>
<if condition="htmlLocation"><then>
......@@ -269,7 +283,14 @@ along with this software (see the LICENSE.md file). If not, see
<if condition="!htmlFile || htmlFile.parent?.location != grapesJsResource.location">
<return error="true" message="Resource not found"/>
</if>
<!-- <script><![CDATA[htmlFile.putText(html)]]></script>-->
<script><![CDATA[htmlFile.putText(html)]]></script>
<set field="htmlVersionName" from="htmlFile?.getCurrentVersion()?.getVersionName()"/>
<if condition="publish">
<service-call name="update#moqui.basic.email.EmailTemplate" in-map="[emailTemplateId:emailTemplateId,
htmlPublishedVersionName:htmlVersionName,grapesPublishedVersionName:grapesVersionName]"/>
<set field="htmlPublishedVersionName" from="htmlVersionName"/>
<set field="grapesPublishedVersionName" from="grapesVersionName"/>
</if>
</else-if>
<else>
<return error="true" message="Resource not found"/>
......@@ -277,6 +298,29 @@ along with this software (see the LICENSE.md file). If not, see
</if>
</actions>
</service>
<service verb="get" noun="AfterMjmlStore">
<description>Get the data after storing the MJML data</description>
<in-parameters>
<parameter name="emailTemplateId" required="true"/>
</in-parameters>
<out-parameters>
<parameter name="htmlLocation"/>
<parameter name="htmlVersionName"/>
<parameter name="htmlPublishedVersionName"/>
<parameter name="grapesLocation"/>
<parameter name="grapesVersionName"/>
<parameter name="grapesPublishedVersionName"/>
</out-parameters>
<actions>
<entity-find-one entity-name="moqui.basic.email.EmailTemplate" value-field="emailTemplate" auto-field-map="[emailTemplateId:emailTemplateId]"/>
<if condition="!emailTemplate"><return error="true" message="Resource not found"/></if>
<set field="grapesLocation" from="emailTemplate?.grapesLocation"/>
<set field="grapesVersionName" from="ec.resource.getLocationReference(grapesLocation)?.getCurrentVersion()?.getVersionName()"/>
<set field="grapesPublishedVersionName" from="emailTemplate?.grapesPublishedVersionName"/>
<set field="htmlLocation" from="emailTemplate?.htmlLocation"/>
<set field="htmlVersionName" from="ec.resource.getLocationReference(htmlLocation)?.getCurrentVersion()?.getVersionName()"/>
<set field="htmlPublishedVersionName" from="emailTemplate?.htmlPublishedVersionName"/>
</actions>
</service>
</services>
......