Liferay.Loader.define("@liferay/template-web@1.0.51/js/ddm_template_editor/components/Editor",["module","exports","require","frontend-js-web","liferay!frontend-js-react-web$prop-types","liferay!frontend-js-react-web$react","./AppContext","./CodeMirrorEditor"],(function(e,t,r){window;{Object.defineProperty(t,"__esModule",{value:!0}),t.Editor=function Editor({autocompleteData:e,initialScript:t,mode:r}){const{inputChannel:i,portletNamespace:u}=(0,o.useContext)(c.AppContext),[d,l]=(0,o.useState)(t),f=(0,o.useRef)(d);return f.current=d,(0,o.useEffect)((()=>{const e=Liferay.on("".concat(u,"refreshEditor"),(()=>{const e=document.getElementById("".concat(u,"fm"));e&&(f.current===t&&l(""),Liferay.fire("".concat(u,"saveTemplate")),requestAnimationFrame((()=>{e.action=window.location.href,e.submit()})))}));return()=>{e.detach()}}),[t,u]),(0,o.useEffect)((()=>{const e=Liferay.on("".concat(u,"scriptImported"),(e=>{l(e.script),(0,n.openToast)({message:(0,n.sub)(Liferay.Language.get("x-imported"),e.fileName),title:Liferay.Language.get("success"),type:"success"})}));return()=>{e.detach()}}),[t,u]),(0,o.useEffect)((()=>{const saveTemplate=e=>{const t=document.getElementById("".concat(u,"fm"));if(!e){document.getElementById("".concat(u,"saveAndContinue")).value=!0}const r=document.querySelectorAll("save-button"),changeDisabled=e=>{r.forEach((t=>{t.disabled=e}))},o=new FormData(t);o.append("".concat(u,"scriptContent"),new File([new Blob([d])],"scriptContent")),changeDisabled(!0);const c=Liferay.Form.get(t.id);if(c){const e=c.formValidator;e.validate(),e.hasErrors()&&e.focusInvalidField()}(0,n.fetch)(t.action,{body:o,method:"POST"}).then((e=>{e.redirected&&(0,n.navigate)(e.url),changeDisabled(!1)})).catch((()=>{changeDisabled(!0)}))},e=document.querySelector(".save-and-continue-button"),t=document.querySelector(".save-button"),onSaveAndContinueButtonClick=e=>{e.preventDefault(),saveTemplate(!1)},onSaveButtonClick=e=>{e.preventDefault(),saveTemplate(!0)};return e.addEventListener("click",onSaveAndContinueButtonClick),t.addEventListener("click",onSaveButtonClick),()=>{e.removeEventListener("click",onSaveAndContinueButtonClick),t.removeEventListener("click",onSaveButtonClick)}}),[u,d]),(0,o.useEffect)((()=>{const e=Liferay.on("".concat(u,"exportScript"),(()=>{exportScript(f.current,"ftl")}));return()=>{e.detach()}}),[t,u]),o.default.createElement(o.default.Fragment,null,o.default.createElement(a.CodeMirrorEditor,{autocompleteData:e,content:d,inputChannel:i,mode:r,onChange:l}))};var n=r("frontend-js-web"),o=(function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}(r("liferay!frontend-js-react-web$prop-types")),function _interopRequireWildcard(e){if(e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var t=_getRequireWildcardCache();if(t&&t.has(e))return t.get(e);var r={},n=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var o in e)if(Object.prototype.hasOwnProperty.call(e,o)){var c=n?Object.getOwnPropertyDescriptor(e,o):null;c&&(c.get||c.set)?Object.defineProperty(r,o,c):r[o]=e[o]}r.default=e,t&&t.set(e,r);return r}(r("liferay!frontend-js-react-web$react"))),c=r("./AppContext"),a=r("./CodeMirrorEditor");function _getRequireWildcardCache(){if("function"!=typeof WeakMap)return null;var e=new WeakMap;return _getRequireWildcardCache=function _getRequireWildcardCache(){return e},e}const exportScript=e=>{const t=document.createElement("a"),r=new Blob([e]),n=URL.createObjectURL(r);t.href=n,t.download="script.ftl",t.click(),URL.revokeObjectURL(n)}}}));
//# sourceMappingURL=Editor.js.map