diff --git a/blocks/ai-block/edit.js b/blocks/ai-block/edit.js index 7db1ba4e5fd7717bfcb1fda47ba2a418567e5abd..0e5388d02b012b2e7143f8a1ae9b0da20c031dd6 100644 --- a/blocks/ai-block/edit.js +++ b/blocks/ai-block/edit.js @@ -33,6 +33,12 @@ function Edit({ attributes, setAttributes }) { const [questionCopy, setQuestionCopy] = useState(''); const handleClick = () => { + /* title must not be empty of course */ + if (title === undefined) { + setProcessingMessage('Please enter a valid question.'); + return false; + } + /* Don't allow the user to click to request AI to answer the same question * many times, this sends unnecessary requests and consume extra pennies * that aren't needed */ @@ -40,6 +46,7 @@ function Edit({ attributes, setAttributes }) { setProcessingMessage( 'You are asking the same question, please ask another question....', ); + return false; } @@ -64,6 +71,7 @@ function Edit({ attributes, setAttributes }) { }) .then((response) => response.text()) .then((data) => { + console.log(data); const parsedData = parseAIResponse(data); setToken({ token: parsedData }); setAttributes({ aiAnswer: parsedData }); diff --git a/dist/blocks.asset.php b/dist/blocks.asset.php index 56d593557acf4083b7b6f4cc822ab51cf5ae7594..54ec9b3fe113a989ed039cf6f51517e6b5dfd76f 100644 --- a/dist/blocks.asset.php +++ b/dist/blocks.asset.php @@ -1 +1 @@ -<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components'), 'version' => '974f74cf8250bf56c817'); +<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components'), 'version' => 'd50091a699194596645f'); diff --git a/dist/blocks.js b/dist/blocks.js index e314e2f460d536fc044d6dcb8206306a5e11e593..df866e44a797161f62ae3dd65ac529a63d11ae12 100644 --- a/dist/blocks.js +++ b/dist/blocks.js @@ -1 +1 @@ -(()=>{"use strict";var e,t={93:(e,t,n)=>{const r=window.wp.blocks,l=JSON.parse('{"$schema":"https://schemas.wp.org/trunk/block.json","apiVersion":2,"name":"gutenberg-ai-tools/ai-block","title":"AI Block","description":"A block that displays content pulled from AI Services based on the input and parameters sent to it.","textdomain":"default","category":"design","icon":"businessman","attributes":{"title":{"type":"string","source":"text","selector":"h2"},"aiAnswer":{"type":"string"},"blockTitle":{"type":"string"}},"supports":{"align":false,"anchor":true,"color":{"background":true,"text":true}},"styles":[{"name":"default","label":"Default","isDefault":true}],"variations":[]}'),a=window.React;var o=n.n(a);const c=window.wp.components,s=window.wp.blockEditor,i=Drupal;class u extends o().Component{render(){return(0,a.createElement)("div",null,(0,a.createElement)("h3",null,this.props.blockTitle),(0,a.createElement)("p",{dangerouslySetInnerHTML:{__html:this.props.answer}}))}}const h=u,p=["core/heading","core/paragraph","core/quote"],v={...l,...l,icon:function(){return(0,a.createElement)("svg",{id:"Layer_1",xmlns:"http://www.w3.org/2000/svg",xmlnsXlink:"http://www.w3.org/1999/xlink",x:"0px",y:"0px",viewBox:"0 0 512 512",style:{enableBackground:"new 0 0 512 512"},xmlSpace:"preserve"},(0,a.createElement)("g",null,(0,a.createElement)("g",null,(0,a.createElement)("path",{d:"M434.863,126.093V77.137h-48.956V0h-33.391v77.137h-42.083V0h-33.391v77.137h-42.083V0h-33.391v77.137h-42.083V0h-33.391 v77.137H77.137v48.956H0v33.391h77.137v42.083H0v33.391h77.137v42.083H0v33.391h77.137v42.083H0v33.391h77.137v48.956h48.956V512 h33.391v-77.137h42.083V512h33.391v-77.137h42.083V512h33.391v-77.137h42.083V512h33.391v-77.137h48.956v-48.956H512v-33.391 h-77.137v-42.083H512v-33.391h-77.137v-42.083H512v-33.391h-77.137v-42.083H512v-33.39H434.863z M401.473,401.471h-0.001H110.529 V110.529h290.944V401.471z"}))),(0,a.createElement)("g",null,(0,a.createElement)("g",null,(0,a.createElement)("path",{d:"M375.773,229.532c0-22.913-14.194-42.903-34.426-51.239c-1.374-26.935-23.718-48.426-50.987-48.426 c-13.221,0-25.283,5.052-34.36,13.325c-9.077-8.273-21.139-13.325-34.36-13.325c-27.27,0-49.615,21.491-50.987,48.426 c-20.234,8.336-34.426,28.326-34.426,51.239c0,9.577,2.445,18.593,6.742,26.459c-4.391,8.051-6.742,17.113-6.742,26.478 c-0.001,23.125,14.25,42.974,34.428,51.253c1.381,26.928,23.722,48.411,50.986,48.411c13.221,0,25.283-5.052,34.36-13.325 c9.077,8.273,21.139,13.325,34.36,13.325c27.265,0,49.606-21.483,50.986-48.411c20.176-8.28,34.428-28.129,34.428-51.253 c0-9.366-2.351-18.428-6.742-26.478C373.328,248.124,375.773,239.108,375.773,229.532z M239.304,331.078 c0,9.74-7.924,17.664-17.664,17.664c-7.943,0-14.674-5.271-16.889-12.497c10.656-2.612,20.43-8.341,27.914-16.604l-24.749-22.417 c-4.226,4.667-10.018,7.237-16.308,7.237c-12.127,0-21.992-9.866-21.992-21.992c0-0.697,0.033-1.389,0.098-2.076 c6.719,2.904,14.12,4.521,21.895,4.521v-33.391c-12.127,0-21.992-9.866-21.992-21.993c-0.001-7.907,4.25-14.938,10.63-18.817 c5.774,8.031,13.85,14.415,23.463,18.021l11.727-31.264c-6.855-2.571-11.461-9.222-11.461-16.549 c0-9.74,7.924-17.664,17.664-17.664c9.74,0,17.664,7.924,17.664,17.664V331.078z M342.285,280.393 c0.065,0.687,0.098,1.379,0.098,2.076c0,12.127-9.866,21.992-21.993,21.992c-6.289,0-12.081-2.57-16.307-7.237l-24.748,22.417 c7.485,8.263,17.258,13.993,27.914,16.604c-2.215,7.227-8.947,12.497-16.889,12.497c-9.74,0-17.664-7.924-17.664-17.664V180.922 c0-9.74,7.924-17.664,17.664-17.664c9.739,0,17.664,7.924,17.664,17.664c0,7.327-4.606,13.978-11.461,16.549l11.727,31.264 c9.613-3.606,17.688-9.991,23.463-18.021c6.38,3.879,10.631,10.911,10.631,18.817c0,12.127-9.866,21.993-21.993,21.993v33.391 C328.164,284.915,335.566,283.297,342.285,280.393z"}))))},edit:function({attributes:e,setAttributes:t}){const{title:n,blockTitle:r,aiAnswer:l}=e,[o,u]=(0,a.useState)({token:""}),[v,m]=(0,a.useState)(!1),[g,d]=(0,a.useState)(""),[b,w]=(0,a.useState)("");return(0,a.createElement)("div",{...(0,s.useBlockProps)()},(0,a.createElement)(s.BlockControls,null),(0,a.createElement)(s.InspectorControls,null,(0,a.createElement)(c.PanelBody,{title:(0,i.t)("Block settings"),initialOpen:!0},(0,a.createElement)(c.PanelRow,null),(0,a.createElement)(c.PanelRow,null,(0,a.createElement)(c.TextareaControl,{label:(0,i.t)("Block Title"),help:(0,i.t)("Use this field if you want to put a question title above the answer"),value:r,onChange:e=>t({blockTitle:e})})),(0,a.createElement)(c.PanelRow,null,(0,a.createElement)(c.TextareaControl,{label:(0,i.t)("AI Answer"),help:(0,i.t)("AI answer generated by the engine. You can change the answer here"),value:l,onChange:e=>(e=>{t({aiAnswer:e}),m(!1),d(""),w(n)})(e)})))),(0,a.createElement)("div",null,(0,a.createElement)(s.RichText,{tagName:"h2",placeholder:(0,i.t)("Start typing your question here:"),value:n,onChange:e=>t({title:e})}),(0,a.createElement)("button",{onClick:()=>{if(b===n)return d("You are asking the same question, please ask another question...."),!1;const e={ai_prompt:n};m(!0),d("Processing...."),fetch("/session/token").then((e=>e.text())).then((r=>{fetch("/gutenberg-ai-tools/ai-rest?_format=json",{body:JSON.stringify(e),headers:{"Content-Type":"application/json","X-Csrf-Token":r},method:"POST"}).then((e=>e.text())).then((e=>{const r=function(e){let t=e.substring(1,e.length-1);var n;return n=t.replace(/\\n/g,"<br />"),t=n.replace(/\\u[\dA-F]{4}/gi,(e=>String.fromCharCode(parseInt(e.replace(/\\u/g,""),16)))),t}(e);u({token:r}),t({aiAnswer:r}),m(!1),d(""),w(n)})).catch((e=>console.error(e)))}))},disabled:v},(0,i.t)("Ask AI")),(0,a.createElement)("span",null," ",g),(0,a.createElement)("div",null,(0,a.createElement)("h3",null,r),(0,a.createElement)(h,{question:n,block_title:r,answer:l}),(0,a.createElement)(s.InnerBlocks,{allowedBlocks:p}))))},save:function({attributes:e}){const{title:t,blockTitle:n,aiAnswer:r}=e;return(0,a.createElement)("div",{...s.useBlockProps.save()},(0,a.createElement)("div",null,(0,a.createElement)("div",null,(0,a.createElement)("h3",null,n),(0,a.createElement)(h,{question:t,block_title:n,answer:r}),(0,a.createElement)(s.InnerBlocks.Content,null))))},render:h};(0,r.registerBlockType)("gutenberg-ai-tools/ai-block",v)}},n={};function r(e){var l=n[e];if(void 0!==l)return l.exports;var a=n[e]={exports:{}};return t[e](a,a.exports,r),a.exports}r.m=t,e=[],r.O=(t,n,l,a)=>{if(!n){var o=1/0;for(u=0;u<e.length;u++){for(var[n,l,a]=e[u],c=!0,s=0;s<n.length;s++)(!1&a||o>=a)&&Object.keys(r.O).every((e=>r.O[e](n[s])))?n.splice(s--,1):(c=!1,a<o&&(o=a));if(c){e.splice(u--,1);var i=l();void 0!==i&&(t=i)}}return t}a=a||0;for(var u=e.length;u>0&&e[u-1][2]>a;u--)e[u]=e[u-1];e[u]=[n,l,a]},r.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return r.d(t,{a:t}),t},r.d=(e,t)=>{for(var n in t)r.o(t,n)&&!r.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),(()=>{var e={617:0,232:0};r.O.j=t=>0===e[t];var t=(t,n)=>{var l,a,[o,c,s]=n,i=0;if(o.some((t=>0!==e[t]))){for(l in c)r.o(c,l)&&(r.m[l]=c[l]);if(s)var u=s(r)}for(t&&t(n);i<o.length;i++)a=o[i],r.o(e,a)&&e[a]&&e[a][0](),e[a]=0;return r.O(u)},n=globalThis.webpackChunkgutenberg_ai_tools=globalThis.webpackChunkgutenberg_ai_tools||[];n.forEach(t.bind(null,0)),n.push=t.bind(null,n.push.bind(n))})();var l=r.O(void 0,[232],(()=>r(93)));l=r.O(l)})(); \ No newline at end of file +(()=>{"use strict";var e,t={93:(e,t,n)=>{const l=window.wp.blocks,r=JSON.parse('{"$schema":"https://schemas.wp.org/trunk/block.json","apiVersion":2,"name":"gutenberg-ai-tools/ai-block","title":"AI Block","description":"A block that displays content pulled from AI Services based on the input and parameters sent to it.","textdomain":"default","category":"design","icon":"businessman","attributes":{"title":{"type":"string","source":"text","selector":"h2"},"aiAnswer":{"type":"string"},"blockTitle":{"type":"string"}},"supports":{"align":false,"anchor":true,"color":{"background":true,"text":true}},"styles":[{"name":"default","label":"Default","isDefault":true}],"variations":[]}'),a=window.React;var o=n.n(a);const c=window.wp.components,s=window.wp.blockEditor,i=Drupal;class u extends o().Component{render(){return(0,a.createElement)("div",null,(0,a.createElement)("h3",null,this.props.blockTitle),(0,a.createElement)("p",{dangerouslySetInnerHTML:{__html:this.props.answer}}))}}const h=u,p=["core/heading","core/paragraph","core/quote"],v={...r,...r,icon:function(){return(0,a.createElement)("svg",{id:"Layer_1",xmlns:"http://www.w3.org/2000/svg",xmlnsXlink:"http://www.w3.org/1999/xlink",x:"0px",y:"0px",viewBox:"0 0 512 512",style:{enableBackground:"new 0 0 512 512"},xmlSpace:"preserve"},(0,a.createElement)("g",null,(0,a.createElement)("g",null,(0,a.createElement)("path",{d:"M434.863,126.093V77.137h-48.956V0h-33.391v77.137h-42.083V0h-33.391v77.137h-42.083V0h-33.391v77.137h-42.083V0h-33.391 v77.137H77.137v48.956H0v33.391h77.137v42.083H0v33.391h77.137v42.083H0v33.391h77.137v42.083H0v33.391h77.137v48.956h48.956V512 h33.391v-77.137h42.083V512h33.391v-77.137h42.083V512h33.391v-77.137h42.083V512h33.391v-77.137h48.956v-48.956H512v-33.391 h-77.137v-42.083H512v-33.391h-77.137v-42.083H512v-33.391h-77.137v-42.083H512v-33.39H434.863z M401.473,401.471h-0.001H110.529 V110.529h290.944V401.471z"}))),(0,a.createElement)("g",null,(0,a.createElement)("g",null,(0,a.createElement)("path",{d:"M375.773,229.532c0-22.913-14.194-42.903-34.426-51.239c-1.374-26.935-23.718-48.426-50.987-48.426 c-13.221,0-25.283,5.052-34.36,13.325c-9.077-8.273-21.139-13.325-34.36-13.325c-27.27,0-49.615,21.491-50.987,48.426 c-20.234,8.336-34.426,28.326-34.426,51.239c0,9.577,2.445,18.593,6.742,26.459c-4.391,8.051-6.742,17.113-6.742,26.478 c-0.001,23.125,14.25,42.974,34.428,51.253c1.381,26.928,23.722,48.411,50.986,48.411c13.221,0,25.283-5.052,34.36-13.325 c9.077,8.273,21.139,13.325,34.36,13.325c27.265,0,49.606-21.483,50.986-48.411c20.176-8.28,34.428-28.129,34.428-51.253 c0-9.366-2.351-18.428-6.742-26.478C373.328,248.124,375.773,239.108,375.773,229.532z M239.304,331.078 c0,9.74-7.924,17.664-17.664,17.664c-7.943,0-14.674-5.271-16.889-12.497c10.656-2.612,20.43-8.341,27.914-16.604l-24.749-22.417 c-4.226,4.667-10.018,7.237-16.308,7.237c-12.127,0-21.992-9.866-21.992-21.992c0-0.697,0.033-1.389,0.098-2.076 c6.719,2.904,14.12,4.521,21.895,4.521v-33.391c-12.127,0-21.992-9.866-21.992-21.993c-0.001-7.907,4.25-14.938,10.63-18.817 c5.774,8.031,13.85,14.415,23.463,18.021l11.727-31.264c-6.855-2.571-11.461-9.222-11.461-16.549 c0-9.74,7.924-17.664,17.664-17.664c9.74,0,17.664,7.924,17.664,17.664V331.078z M342.285,280.393 c0.065,0.687,0.098,1.379,0.098,2.076c0,12.127-9.866,21.992-21.993,21.992c-6.289,0-12.081-2.57-16.307-7.237l-24.748,22.417 c7.485,8.263,17.258,13.993,27.914,16.604c-2.215,7.227-8.947,12.497-16.889,12.497c-9.74,0-17.664-7.924-17.664-17.664V180.922 c0-9.74,7.924-17.664,17.664-17.664c9.739,0,17.664,7.924,17.664,17.664c0,7.327-4.606,13.978-11.461,16.549l11.727,31.264 c9.613-3.606,17.688-9.991,23.463-18.021c6.38,3.879,10.631,10.911,10.631,18.817c0,12.127-9.866,21.993-21.993,21.993v33.391 C328.164,284.915,335.566,283.297,342.285,280.393z"}))))},edit:function({attributes:e,setAttributes:t}){const{title:n,blockTitle:l,aiAnswer:r}=e,[o,u]=(0,a.useState)({token:""}),[v,d]=(0,a.useState)(!1),[g,m]=(0,a.useState)(""),[b,w]=(0,a.useState)("");return(0,a.createElement)("div",{...(0,s.useBlockProps)()},(0,a.createElement)(s.BlockControls,null),(0,a.createElement)(s.InspectorControls,null,(0,a.createElement)(c.PanelBody,{title:(0,i.t)("Block settings"),initialOpen:!0},(0,a.createElement)(c.PanelRow,null),(0,a.createElement)(c.PanelRow,null,(0,a.createElement)(c.TextareaControl,{label:(0,i.t)("Block Title"),help:(0,i.t)("Use this field if you want to put a question title above the answer"),value:l,onChange:e=>t({blockTitle:e})})),(0,a.createElement)(c.PanelRow,null,(0,a.createElement)(c.TextareaControl,{label:(0,i.t)("AI Answer"),help:(0,i.t)("AI answer generated by the engine. You can change the answer here"),value:r,onChange:e=>(e=>{t({aiAnswer:e}),d(!1),m(""),w(n)})(e)})))),(0,a.createElement)("div",null,(0,a.createElement)(s.RichText,{tagName:"h2",placeholder:(0,i.t)("Start typing your question here:"),value:n,onChange:e=>t({title:e})}),(0,a.createElement)("button",{onClick:()=>{if(void 0===n)return m("Please enter a valid question."),!1;if(b===n)return m("You are asking the same question, please ask another question...."),!1;const e={ai_prompt:n};d(!0),m("Processing...."),fetch("/session/token").then((e=>e.text())).then((l=>{fetch("/gutenberg-ai-tools/ai-rest?_format=json",{body:JSON.stringify(e),headers:{"Content-Type":"application/json","X-Csrf-Token":l},method:"POST"}).then((e=>e.text())).then((e=>{console.log(e);const l=function(e){let t=e.substring(1,e.length-1);var n;return n=t.replace(/\\n/g,"<br />"),t=n.replace(/\\u[\dA-F]{4}/gi,(e=>String.fromCharCode(parseInt(e.replace(/\\u/g,""),16)))),t}(e);u({token:l}),t({aiAnswer:l}),d(!1),m(""),w(n)})).catch((e=>console.error(e)))}))},disabled:v},(0,i.t)("Ask AI")),(0,a.createElement)("span",null," ",g),(0,a.createElement)("div",null,(0,a.createElement)("h3",null,l),(0,a.createElement)(h,{question:n,block_title:l,answer:r}),(0,a.createElement)(s.InnerBlocks,{allowedBlocks:p}))))},save:function({attributes:e}){const{title:t,blockTitle:n,aiAnswer:l}=e;return(0,a.createElement)("div",{...s.useBlockProps.save()},(0,a.createElement)("div",null,(0,a.createElement)("div",null,(0,a.createElement)("h3",null,n),(0,a.createElement)(h,{question:t,block_title:n,answer:l}),(0,a.createElement)(s.InnerBlocks.Content,null))))},render:h};(0,l.registerBlockType)("gutenberg-ai-tools/ai-block",v)}},n={};function l(e){var r=n[e];if(void 0!==r)return r.exports;var a=n[e]={exports:{}};return t[e](a,a.exports,l),a.exports}l.m=t,e=[],l.O=(t,n,r,a)=>{if(!n){var o=1/0;for(u=0;u<e.length;u++){for(var[n,r,a]=e[u],c=!0,s=0;s<n.length;s++)(!1&a||o>=a)&&Object.keys(l.O).every((e=>l.O[e](n[s])))?n.splice(s--,1):(c=!1,a<o&&(o=a));if(c){e.splice(u--,1);var i=r();void 0!==i&&(t=i)}}return t}a=a||0;for(var u=e.length;u>0&&e[u-1][2]>a;u--)e[u]=e[u-1];e[u]=[n,r,a]},l.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return l.d(t,{a:t}),t},l.d=(e,t)=>{for(var n in t)l.o(t,n)&&!l.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},l.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),(()=>{var e={617:0,232:0};l.O.j=t=>0===e[t];var t=(t,n)=>{var r,a,[o,c,s]=n,i=0;if(o.some((t=>0!==e[t]))){for(r in c)l.o(c,r)&&(l.m[r]=c[r]);if(s)var u=s(l)}for(t&&t(n);i<o.length;i++)a=o[i],l.o(e,a)&&e[a]&&e[a][0](),e[a]=0;return l.O(u)},n=globalThis.webpackChunkgutenberg_ai_tools=globalThis.webpackChunkgutenberg_ai_tools||[];n.forEach(t.bind(null,0)),n.push=t.bind(null,n.push.bind(n))})();var r=l.O(void 0,[232],(()=>l(93)));r=l.O(r)})(); \ No newline at end of file