2022-04-11 14:04:30 +02:00
|
|
|
!function(){"use strict";var e={n:function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,{a:n}),n},d:function(t,n){for(var o in n)e.o(n,o)&&!e.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:n[o]})},o:function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r:function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t);var n=window.wp.element,o=window.wp.i18n,i=window.lodash,r=window.wp.apiFetch,s=e.n(r);var a=async function(e){const t=await s()({path:"/wp/v2/types/wp_block"}),n=await s()({path:`/wp/v2/${t.rest_base}/${e}?context=edit`}),o=n.title.raw,r=n.content.raw,a=JSON.stringify({__file:"wp_block",title:o,content:r},null,2);!function(e,t,n){const o=new window.Blob([t],{type:n});if(window.navigator.msSaveOrOpenBlob)window.navigator.msSaveOrOpenBlob(o,e);else{const t=document.createElement("a");t.href=URL.createObjectURL(o),t.download=e,t.style.display="none",document.body.appendChild(t),t.click(),document.body.removeChild(t)}}((0,i.kebabCase)(o)+".json",a,"application/json")},l=window.wp.components,c=window.wp.compose;var d=async function(e){const t=await function(e){const t=new window.FileReader;return new Promise((n=>{t.onload=()=>{n(t.result)},t.readAsText(e)}))}(e);let n;try{n=JSON.parse(t)}catch(e){throw new Error("Invalid JSON file")}if(!("wp_block"===n.__file&&n.title&&n.content&&(0,i.isString)(n.title)&&(0,i.isString)(n.content)))throw new Error("Invalid Reusable block JSON file");const o=await s()({path:"/wp/v2/types/wp_block"});return await s()({path:`/wp/v2/${o.rest_base}`,data:{title:n.title,content:n.content,status:"publish"},method:"POST"})};class u extends n.Component{constructor(){super(...arguments),this.state={isLoading:!1,error:null,file:null},this.isStillMounted=!0,this.onChangeFile=this.onChangeFile.bind(this),this.onSubmit=this.onSubmit.bind(this)}componentWillUnmount(){this.isStillMounted=!1}onChangeFile(e){this.setState({file:e.target.files[0],error:null})}onSubmit(e){e.preventDefault();const{file:t}=this.state,{onUpload:n}=this.props;t&&(this.setState({isLoading:!0}),d(t).then((e=>{this.isStillMounted&&(this.setState({isLoading:!1}),n(e))})).catch((e=>{if(!this.isStillMounted)return;let t;switch(e.message){case"Invalid JSON file":t=(0,o.__)("Invalid JSON file");break;case"Invalid Reusable block JSON file":t=(0,o.__)("Invalid Reusable block JSON file");break;default:t=(0,o.__)("Unknown error")}this.setState({isLoading:!1,error:t})})))}onDismissError(){this.setState({error:null})}render(){const{instanceId:e}=this.props,{file:t,isLoading:i,error:r}=this.state,s="list-reusable-blocks-import-form-"+e;return(0,n.createElement)("form",{className:"list-reusable-blocks-import-form",onSubmit:this.onSubmit},r&&(0,n.createElement)(l.Notice,{status:"error",onRemove:()=>this.onDismissError()},r),(0,n.createElement)("label",{htmlFor:s,className:"list-reusable-blocks-import-form__label"},(0,o.__)("File")),(0,n.createElement)("input",{id:s,type:"file",onChange:this.onChangeFile}),(0,n.createElement)(l.Button,{type:"submit",isBusy:i,disabled:!t||i,variant:"secondary",className:"list-reusable-blocks-import-form__button"},(0,o._x)("Import","button label")))}}var p=(0,c.withInstanceId)(u);var m=function(e){let{onUpload:t}=e;return(0,n.createElement)(l.Dropdown,{position:"bottom right",contentClassName:"list-reusable-blocks-import-dropdown__content",renderToggle:e=>{let{isOpen:t,onToggle:i}=e;return(0,n.createElement)(l.Button,{"aria-expanded":t,onClick:i,variant:"primary"},(0,o.__)("Import from JSON"))},renderContent:e=>{let{onClose:o}=e;return(0,n.createElement)(p,{onUpload:(0,i.flow)(o,t)})}})};document.body.addEventListener("click",(e=>{e.target.classList.contains("wp-list-reusable-blocks__export")&&(e.preventDefault(),a(e.target.dataset.id))})),document.addEventListener("DOMContentLoaded",(()=>{const e=document.querySelector(".page-title-action");if(!e)return;const t=document.createElement("div");t.className="list-reusable-blocks__con
|