From 265ee2529c4758a1609c9efb4cc4cc32d66857ee Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez <danrod@gmail.com> Date: Mon, 14 Apr 2025 07:38:15 -0400 Subject: [PATCH 01/11] Issue # 3518977: Added parser function to process code samples sent by the AI Provider --- blocks/ai-block/parser.js | 44 +++++++++++++++++++++++++++++++++----- blocks/ai-block/style.scss | 4 ++++ dist/blocks.asset.php | 2 +- dist/blocks.js | 2 +- dist/style-blocks-rtl.css | 2 +- dist/style-blocks.css | 2 +- 6 files changed, 47 insertions(+), 9 deletions(-) diff --git a/blocks/ai-block/parser.js b/blocks/ai-block/parser.js index ded156b..833e086 100644 --- a/blocks/ai-block/parser.js +++ b/blocks/ai-block/parser.js @@ -1,16 +1,50 @@ /* Utility class */ -function unicodeToChar(text) { +const parseAIResponse = (AIResponse) => { + let parsedData = AIResponse.substring(1, AIResponse.length - 1); + parsedData = unicodeToChar(parsedData.replace(/\\n/g, '<br />')); + parsedData = checkForSampleCode(parsedData); + + return parsedData; +} + +const unicodeToChar = (text) => { return text.replace(/\\u[\dA-F]{4}/gi, (match) => { return String.fromCharCode(parseInt(match.replace(/\\u/g, ''), 16)); }); } -function parseAIResponse(AIResponse) { - let parsedData = AIResponse.substring(1, AIResponse.length - 1); - parsedData = unicodeToChar(parsedData.replace(/\\n/g, '<br />')); +const checkForSampleCode = (text) => { + let regex = /```[A-Za-z0-9]+/g; + let found = text.match(regex); + let finalCode = ""; - return parsedData; + /* A Snippet of code was found, wrap it around <code> */ + if ((found !== null) && (found.length > 0)) { + const beginCode = found[0]; + finalCode = text.replace(beginCode, "<code>"); + finalCode = finalCode.replace ("```", "</code>"); + } + + // Check for more ``` elements inside the text + regex = /```/g; + found = finalCode.match(regex); + + if ((found !== null) && (found.length > 0)) { + found.forEach((element, index) => { + if (index % 2 == 0) { + finalCode = finalCode.replace("```", "<code>"); + } else { + finalCode = finalCode.replace("```", "</code>"); + } + }); + } + + // Removed extra <br /> elements + finalCode = finalCode.replace("\/", "/"); + + return finalCode; } + export default parseAIResponse; diff --git a/blocks/ai-block/style.scss b/blocks/ai-block/style.scss index 7a38ec9..349639e 100644 --- a/blocks/ai-block/style.scss +++ b/blocks/ai-block/style.scss @@ -17,4 +17,8 @@ display: none; } } + + code { + background-color: white !important; + } } \ No newline at end of file diff --git a/dist/blocks.asset.php b/dist/blocks.asset.php index 54ec9b3..f989225 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' => 'd50091a699194596645f'); +<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components'), 'version' => 'e2cb037465db9802ad4f'); diff --git a/dist/blocks.js b/dist/blocks.js index df866e4..0d0c67c 100644 --- a/dist/blocks.js +++ b/dist/blocks.js @@ -1 +1 @@ -(()=>{"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 +(()=>{"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=e=>{let t=e.substring(1,e.length-1);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=>{let t=/```[A-Za-z0-9]+/g,n=e.match(t),l="";if(null!==n&&n.length>0){const t=n[0];l=e.replace(t,"<code>"),l=l.replace("```","</code>")}return t=/```/g,n=l.match(t),null!==n&&n.length>0&&n.forEach(((e,t)=>{l=t%2==0?l.replace("```","<code>"):l.replace("```","</code>")})),l=l.replace("/","/"),l})(t),t;var n},v=["core/heading","core/paragraph","core/quote"],d={...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:""}),[d,g]=(0,a.useState)(!1),[m,b]=(0,a.useState)(""),[w,k]=(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}),g(!1),b(""),k(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 b("Please enter a valid question."),!1;if(w===n)return b("You are asking the same question, please ask another question...."),!1;const e={ai_prompt:n};g(!0),b("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=p(e);u({token:l}),t({aiAnswer:l}),g(!1),b(""),k(n)})).catch((e=>console.error(e)))}))},disabled:d},(0,i.t)("Ask AI")),(0,a.createElement)("span",null," ",m),(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:v}))))},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",d)}},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 diff --git a/dist/style-blocks-rtl.css b/dist/style-blocks-rtl.css index a1ca4db..657204f 100644 --- a/dist/style-blocks-rtl.css +++ b/dist/style-blocks-rtl.css @@ -1 +1 @@ -.ai-block-content{display:flex;gap:20px;width:100%}.ai-block-content img{-o-object-fit:cover;object-fit:cover;width:100%}.ai-block-content.is-style-image-right{flex-direction:row-reverse}.ai-block-content.is-style-pet h2+p{display:none} +.ai-block-content{display:flex;gap:20px;width:100%}.ai-block-content img{-o-object-fit:cover;object-fit:cover;width:100%}.ai-block-content.is-style-image-right{flex-direction:row-reverse}.ai-block-content.is-style-pet h2+p{display:none}.ai-block-content code{background-color:#fff!important} diff --git a/dist/style-blocks.css b/dist/style-blocks.css index a1ca4db..657204f 100644 --- a/dist/style-blocks.css +++ b/dist/style-blocks.css @@ -1 +1 @@ -.ai-block-content{display:flex;gap:20px;width:100%}.ai-block-content img{-o-object-fit:cover;object-fit:cover;width:100%}.ai-block-content.is-style-image-right{flex-direction:row-reverse}.ai-block-content.is-style-pet h2+p{display:none} +.ai-block-content{display:flex;gap:20px;width:100%}.ai-block-content img{-o-object-fit:cover;object-fit:cover;width:100%}.ai-block-content.is-style-image-right{flex-direction:row-reverse}.ai-block-content.is-style-pet h2+p{display:none}.ai-block-content code{background-color:#fff!important} -- GitLab From f017a16d45f893d611c95e959f0970d6b6cb69f5 Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez <danrod@gmail.com> Date: Mon, 14 Apr 2025 07:46:21 -0400 Subject: [PATCH 02/11] Issue # 3518977: Fix ESLint issues --- blocks/ai-block/parser.js | 37 +++++++++++++++++++------------------ dist/blocks.asset.php | 2 +- dist/blocks.js | 2 +- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/blocks/ai-block/parser.js b/blocks/ai-block/parser.js index 833e086..d3caa4b 100644 --- a/blocks/ai-block/parser.js +++ b/blocks/ai-block/parser.js @@ -1,19 +1,5 @@ /* Utility class */ -const parseAIResponse = (AIResponse) => { - let parsedData = AIResponse.substring(1, AIResponse.length - 1); - parsedData = unicodeToChar(parsedData.replace(/\\n/g, '<br />')); - parsedData = checkForSampleCode(parsedData); - - return parsedData; -} - -const unicodeToChar = (text) => { - return text.replace(/\\u[\dA-F]{4}/gi, (match) => { - return String.fromCharCode(parseInt(match.replace(/\\u/g, ''), 16)); - }); -} - const checkForSampleCode = (text) => { let regex = /```[A-Za-z0-9]+/g; let found = text.match(regex); @@ -32,7 +18,7 @@ const checkForSampleCode = (text) => { if ((found !== null) && (found.length > 0)) { found.forEach((element, index) => { - if (index % 2 == 0) { + if (index % 2 === 0) { finalCode = finalCode.replace("```", "<code>"); } else { finalCode = finalCode.replace("```", "</code>"); @@ -40,11 +26,26 @@ const checkForSampleCode = (text) => { }); } - // Removed extra <br /> elements - finalCode = finalCode.replace("\/", "/"); - return finalCode; } +const unicodeToChar = (text) => { + return text.replace(/\\u[\dA-F]{4}/gi, (match) => { + return String.fromCharCode(parseInt(match.replace(/\\u/g, ''), 16)); + }); +} + +const parseAIResponse = (AIResponse) => { + let parsedData = AIResponse.substring(1, AIResponse.length - 1); + parsedData = unicodeToChar(parsedData.replace(/\\n/g, '<br />')); + parsedData = checkForSampleCode(parsedData); + + return parsedData; +} + +const cleanUpSlashes = (text) => { + +} + export default parseAIResponse; diff --git a/dist/blocks.asset.php b/dist/blocks.asset.php index f989225..d1ae912 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' => 'e2cb037465db9802ad4f'); +<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components'), 'version' => 'bea23824ca71fa3cf8cd'); diff --git a/dist/blocks.js b/dist/blocks.js index 0d0c67c..583d85a 100644 --- a/dist/blocks.js +++ b/dist/blocks.js @@ -1 +1 @@ -(()=>{"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=e=>{let t=e.substring(1,e.length-1);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=>{let t=/```[A-Za-z0-9]+/g,n=e.match(t),l="";if(null!==n&&n.length>0){const t=n[0];l=e.replace(t,"<code>"),l=l.replace("```","</code>")}return t=/```/g,n=l.match(t),null!==n&&n.length>0&&n.forEach(((e,t)=>{l=t%2==0?l.replace("```","<code>"):l.replace("```","</code>")})),l=l.replace("/","/"),l})(t),t;var n},v=["core/heading","core/paragraph","core/quote"],d={...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:""}),[d,g]=(0,a.useState)(!1),[m,b]=(0,a.useState)(""),[w,k]=(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}),g(!1),b(""),k(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 b("Please enter a valid question."),!1;if(w===n)return b("You are asking the same question, please ask another question...."),!1;const e={ai_prompt:n};g(!0),b("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=p(e);u({token:l}),t({aiAnswer:l}),g(!1),b(""),k(n)})).catch((e=>console.error(e)))}))},disabled:d},(0,i.t)("Ask AI")),(0,a.createElement)("span",null," ",m),(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:v}))))},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",d)}},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 +(()=>{"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=(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=>{let t=/```[A-Za-z0-9]+/g,n=e.match(t),l="";if(null!==n&&n.length>0){const t=n[0];l=e.replace(t,"<code>"),l=l.replace("```","</code>")}return t=/```/g,n=l.match(t),null!==n&&n.length>0&&n.forEach(((e,t)=>{l=t%2==0?l.replace("```","<code>"):l.replace("```","</code>")})),l})(t),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 -- GitLab From 2d5a3259974d363f5c0fc98ddeacb6a9ade38161 Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez <danrod@gmail.com> Date: Mon, 14 Apr 2025 09:09:47 -0400 Subject: [PATCH 03/11] Issue # 3518977: Added styling to the AI Block --- blocks/ai-block/parser.js | 6 ++++++ blocks/ai-block/style.scss | 32 +++++++++----------------------- dist/blocks.asset.php | 2 +- dist/blocks.js | 2 +- dist/style-blocks-rtl.css | 2 +- dist/style-blocks.css | 2 +- 6 files changed, 19 insertions(+), 27 deletions(-) diff --git a/blocks/ai-block/parser.js b/blocks/ai-block/parser.js index d3caa4b..3a5320c 100644 --- a/blocks/ai-block/parser.js +++ b/blocks/ai-block/parser.js @@ -1,5 +1,7 @@ /* Utility class */ +import { parse } from "@wordpress/blocks"; + const checkForSampleCode = (text) => { let regex = /```[A-Za-z0-9]+/g; let found = text.match(regex); @@ -39,12 +41,16 @@ const parseAIResponse = (AIResponse) => { let parsedData = AIResponse.substring(1, AIResponse.length - 1); parsedData = unicodeToChar(parsedData.replace(/\\n/g, '<br />')); parsedData = checkForSampleCode(parsedData); + parsedData = cleanUpSlashes(parsedData); return parsedData; } const cleanUpSlashes = (text) => { + let parsedData = ""; + parsedData = text.replace(/\\/g, ''); + return parsedData; } diff --git a/blocks/ai-block/style.scss b/blocks/ai-block/style.scss index 349639e..fabdf79 100644 --- a/blocks/ai-block/style.scss +++ b/blocks/ai-block/style.scss @@ -1,24 +1,10 @@ -.ai-block-content { - display: flex; - gap: 20px; - width: 100%; - - img { - width: 100%; - object-fit: cover; - } - - &.is-style-image-right { - flex-direction: row-reverse; - } - - &.is-style-pet { - h2 + p { - display: none; - } - } +/* Styles for the AI Block */ - code { - background-color: white !important; - } - } \ No newline at end of file +img { + width: 100%; + object-fit: cover; +} + +code { + background-color: white !important; +} \ No newline at end of file diff --git a/dist/blocks.asset.php b/dist/blocks.asset.php index d1ae912..0d49343 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' => 'bea23824ca71fa3cf8cd'); +<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components'), 'version' => '4bd09982280c700147b7'); diff --git a/dist/blocks.js b/dist/blocks.js index 583d85a..6f3ee83 100644 --- a/dist/blocks.js +++ b/dist/blocks.js @@ -1 +1 @@ -(()=>{"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=(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=>{let t=/```[A-Za-z0-9]+/g,n=e.match(t),l="";if(null!==n&&n.length>0){const t=n[0];l=e.replace(t,"<code>"),l=l.replace("```","</code>")}return t=/```/g,n=l.match(t),null!==n&&n.length>0&&n.forEach(((e,t)=>{l=t%2==0?l.replace("```","<code>"):l.replace("```","</code>")})),l})(t),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 +(()=>{"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,g]=(0,a.useState)(!1),[d,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}),g(!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};g(!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=(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=>{let t=/```[A-Za-z0-9]+/g,n=e.match(t),l="";if(null!==n&&n.length>0){const t=n[0];l=e.replace(t,"<code>"),l=l.replace("```","</code>")}return t=/```/g,n=l.match(t),null!==n&&n.length>0&&n.forEach(((e,t)=>{l=t%2==0?l.replace("```","<code>"):l.replace("```","</code>")})),l})(t),t=(e=>{let t="";return t=e.replace(/\\/g,""),t})(t),t})(e);u({token:l}),t({aiAnswer:l}),g(!1),m(""),w(n)})).catch((e=>console.error(e)))}))},disabled:v},(0,i.t)("Ask AI")),(0,a.createElement)("span",null," ",d),(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 diff --git a/dist/style-blocks-rtl.css b/dist/style-blocks-rtl.css index 657204f..04feb3c 100644 --- a/dist/style-blocks-rtl.css +++ b/dist/style-blocks-rtl.css @@ -1 +1 @@ -.ai-block-content{display:flex;gap:20px;width:100%}.ai-block-content img{-o-object-fit:cover;object-fit:cover;width:100%}.ai-block-content.is-style-image-right{flex-direction:row-reverse}.ai-block-content.is-style-pet h2+p{display:none}.ai-block-content code{background-color:#fff!important} +img{-o-object-fit:cover;object-fit:cover;width:100%}code{background-color:#fff!important} diff --git a/dist/style-blocks.css b/dist/style-blocks.css index 657204f..04feb3c 100644 --- a/dist/style-blocks.css +++ b/dist/style-blocks.css @@ -1 +1 @@ -.ai-block-content{display:flex;gap:20px;width:100%}.ai-block-content img{-o-object-fit:cover;object-fit:cover;width:100%}.ai-block-content.is-style-image-right{flex-direction:row-reverse}.ai-block-content.is-style-pet h2+p{display:none}.ai-block-content code{background-color:#fff!important} +img{-o-object-fit:cover;object-fit:cover;width:100%}code{background-color:#fff!important} -- GitLab From 1e5e3cb9c3b6aab9e5fb577e86aedc11980fb25a Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez <danrod@gmail.com> Date: Mon, 14 Apr 2025 13:03:00 -0400 Subject: [PATCH 04/11] Issue # 3518977: Added conditions when the user requests text, code or an HTML ssnippet --- blocks/ai-block/edit.js | 35 ++++++++++++++++++++++++++++++++--- blocks/ai-block/parser.js | 19 ++++++++++--------- dist/blocks.asset.php | 2 +- dist/blocks.js | 2 +- 4 files changed, 44 insertions(+), 14 deletions(-) diff --git a/blocks/ai-block/edit.js b/blocks/ai-block/edit.js index 0e5388d..825f4b4 100644 --- a/blocks/ai-block/edit.js +++ b/blocks/ai-block/edit.js @@ -25,6 +25,24 @@ import parseAIResponse from './parser'; const ALLOWED_BLOCKS = ['core/heading', 'core/paragraph', 'core/quote']; +const checkIfHTMLString = (string) => { + const regexForHTML = /<([A-Za-z][A-Za-z0-9]*)\b[^>]*>(.*?)<\/\1>/; + + string = JSON.parse(string); + + let isValid = regexForHTML.test(string); + + if (isValid){ + return true; + } else { + return false; + } +} + +const htmlEntities = (str) => { + return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/\n/g, '<br />'); +} + function Edit({ attributes, setAttributes }) { const { title, blockTitle, aiAnswer } = attributes; const [token, setToken] = useState({ token: '' }); @@ -57,7 +75,7 @@ function Edit({ attributes, setAttributes }) { setIsDisabled(true); setProcessingMessage('Processing....'); - /* TODO: these fetch chains look ugly as hell, consider using async/await instead. */ + /* @TODO: these fetch chains look ugly as hell, consider using async/await instead. */ fetch('/session/token') .then((response) => response.text()) .then((csrfToken) => { @@ -71,8 +89,19 @@ function Edit({ attributes, setAttributes }) { }) .then((response) => response.text()) .then((data) => { - console.log(data); - const parsedData = parseAIResponse(data); + let parsedData = ""; + + const regex = /\\u([\d\w]{4})/gi; + const found = data.match(regex); + + /* @TODO: Find a way to identified that the data is actually HTML and not encoded unicode characters + * from other source */ + if ((found !== null) && (found.length > 0) && checkIfHTMLString(data)) { + parsedData = "<code>" + htmlEntities(JSON.parse(data)) + "</code><br /><br />"; + } else { + parsedData = parseAIResponse(data); + } + setToken({ token: parsedData }); setAttributes({ aiAnswer: parsedData }); setIsDisabled(false); diff --git a/blocks/ai-block/parser.js b/blocks/ai-block/parser.js index 3a5320c..fefc115 100644 --- a/blocks/ai-block/parser.js +++ b/blocks/ai-block/parser.js @@ -12,6 +12,8 @@ const checkForSampleCode = (text) => { const beginCode = found[0]; finalCode = text.replace(beginCode, "<code>"); finalCode = finalCode.replace ("```", "</code>"); + } else { + return text; } // Check for more ``` elements inside the text @@ -37,15 +39,6 @@ const unicodeToChar = (text) => { }); } -const parseAIResponse = (AIResponse) => { - let parsedData = AIResponse.substring(1, AIResponse.length - 1); - parsedData = unicodeToChar(parsedData.replace(/\\n/g, '<br />')); - parsedData = checkForSampleCode(parsedData); - parsedData = cleanUpSlashes(parsedData); - - return parsedData; -} - const cleanUpSlashes = (text) => { let parsedData = ""; parsedData = text.replace(/\\/g, ''); @@ -53,5 +46,13 @@ const cleanUpSlashes = (text) => { return parsedData; } +const parseAIResponse = (AIResponse) => { + let parsedData = AIResponse.substring(1, AIResponse.length - 1); + parsedData = unicodeToChar(parsedData.replace(/\\n/g, '<br />')); + parsedData = checkForSampleCode(parsedData); + parsedData = cleanUpSlashes(parsedData); + + return parsedData + "<br /><br />"; +} export default parseAIResponse; diff --git a/dist/blocks.asset.php b/dist/blocks.asset.php index 0d49343..dc3a03d 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' => '4bd09982280c700147b7'); +<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components'), 'version' => '1cfa5a236637bcae10bc'); diff --git a/dist/blocks.js b/dist/blocks.js index 6f3ee83..7b84dec 100644 --- a/dist/blocks.js +++ b/dist/blocks.js @@ -1 +1 @@ -(()=>{"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,g]=(0,a.useState)(!1),[d,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}),g(!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};g(!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=(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=>{let t=/```[A-Za-z0-9]+/g,n=e.match(t),l="";if(null!==n&&n.length>0){const t=n[0];l=e.replace(t,"<code>"),l=l.replace("```","</code>")}return t=/```/g,n=l.match(t),null!==n&&n.length>0&&n.forEach(((e,t)=>{l=t%2==0?l.replace("```","<code>"):l.replace("```","</code>")})),l})(t),t=(e=>{let t="";return t=e.replace(/\\/g,""),t})(t),t})(e);u({token:l}),t({aiAnswer:l}),g(!1),m(""),w(n)})).catch((e=>console.error(e)))}))},disabled:v},(0,i.t)("Ask AI")),(0,a.createElement)("span",null," ",d),(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 +(()=>{"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"],g={...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:""}),[g,v]=(0,a.useState)(!1),[d,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: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}),v(!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};v(!0),m("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=>{let r="";const l=e.match(/\\u([\d\w]{4})/gi);var a,o;r=null!==l&&l.length>0&&(o=e,o=JSON.parse(o),/<([A-Za-z][A-Za-z0-9]*)\b[^>]*>(.*?)<\/\1>/.test(o))?"<code>"+(a=JSON.parse(e),String(a).replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/\n/g,"<br />")+"</code><br /><br />"):(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=>{let t=/```[A-Za-z0-9]+/g,n=e.match(t),r="";if(!(null!==n&&n.length>0))return e;{const t=n[0];r=e.replace(t,"<code>"),r=r.replace("```","</code>")}return t=/```/g,n=r.match(t),null!==n&&n.length>0&&n.forEach(((e,t)=>{r=t%2==0?r.replace("```","<code>"):r.replace("```","</code>")})),r})(t),t=(e=>{let t="";return t=e.replace(/\\/g,""),t})(t),t+"<br /><br />"})(e),u({token:r}),t({aiAnswer:r}),v(!1),m(""),w(n)})).catch((e=>console.error(e)))}))},disabled:g},(0,i.t)("Ask AI")),(0,a.createElement)("span",null," ",d),(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",g)}},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 -- GitLab From a351c6909b8dea2b593febfd4860f63f1167a358 Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez <danrod@gmail.com> Date: Mon, 14 Apr 2025 14:11:03 -0400 Subject: [PATCH 05/11] Issue # 3518977: ESLint fixes --- blocks/ai-block/edit.js | 12 ++++-------- dist/blocks.asset.php | 2 +- dist/blocks.js | 2 +- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/blocks/ai-block/edit.js b/blocks/ai-block/edit.js index 825f4b4..1b38254 100644 --- a/blocks/ai-block/edit.js +++ b/blocks/ai-block/edit.js @@ -30,14 +30,12 @@ const checkIfHTMLString = (string) => { string = JSON.parse(string); - let isValid = regexForHTML.test(string); + const isValid = regexForHTML.test(string); - if (isValid){ + if (isValid) { return true; - } else { - return false; } -} +}; const htmlEntities = (str) => { return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/\n/g, '<br />'); @@ -94,10 +92,8 @@ function Edit({ attributes, setAttributes }) { const regex = /\\u([\d\w]{4})/gi; const found = data.match(regex); - /* @TODO: Find a way to identified that the data is actually HTML and not encoded unicode characters - * from other source */ if ((found !== null) && (found.length > 0) && checkIfHTMLString(data)) { - parsedData = "<code>" + htmlEntities(JSON.parse(data)) + "</code><br /><br />"; + parsedData = '<code>' + htmlEntities(JSON.parse(data)) + '</code><br /><br />'; } else { parsedData = parseAIResponse(data); } diff --git a/dist/blocks.asset.php b/dist/blocks.asset.php index dc3a03d..1b55fae 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' => '1cfa5a236637bcae10bc'); +<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components'), 'version' => '18aa96d38249972488c0'); diff --git a/dist/blocks.js b/dist/blocks.js index 7b84dec..8cf3854 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"],g={...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:""}),[g,v]=(0,a.useState)(!1),[d,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: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}),v(!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};v(!0),m("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=>{let r="";const l=e.match(/\\u([\d\w]{4})/gi);var a,o;r=null!==l&&l.length>0&&(o=e,o=JSON.parse(o),/<([A-Za-z][A-Za-z0-9]*)\b[^>]*>(.*?)<\/\1>/.test(o))?"<code>"+(a=JSON.parse(e),String(a).replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/\n/g,"<br />")+"</code><br /><br />"):(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=>{let t=/```[A-Za-z0-9]+/g,n=e.match(t),r="";if(!(null!==n&&n.length>0))return e;{const t=n[0];r=e.replace(t,"<code>"),r=r.replace("```","</code>")}return t=/```/g,n=r.match(t),null!==n&&n.length>0&&n.forEach(((e,t)=>{r=t%2==0?r.replace("```","<code>"):r.replace("```","</code>")})),r})(t),t=(e=>{let t="";return t=e.replace(/\\/g,""),t})(t),t+"<br /><br />"})(e),u({token:r}),t({aiAnswer:r}),v(!1),m(""),w(n)})).catch((e=>console.error(e)))}))},disabled:g},(0,i.t)("Ask AI")),(0,a.createElement)("span",null," ",d),(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",g)}},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 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"],g={...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:""}),[g,v]=(0,a.useState)(!1),[d,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: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}),v(!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};v(!0),m("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=>{let r="";const l=e.match(/\\u([\d\w]{4})/gi);var a;r=null!==l&&l.length>0&&(e=>{if(e=JSON.parse(e),/<([A-Za-z][A-Za-z0-9]*)\b[^>]*>(.*?)<\/\1>/.test(e))return!0})(e)?"<code>"+(a=JSON.parse(e),String(a).replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/\n/g,"<br />")+"</code><br /><br />"):(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=>{let t=/```[A-Za-z0-9]+/g,n=e.match(t),r="";if(!(null!==n&&n.length>0))return e;{const t=n[0];r=e.replace(t,"<code>"),r=r.replace("```","</code>")}return t=/```/g,n=r.match(t),null!==n&&n.length>0&&n.forEach(((e,t)=>{r=t%2==0?r.replace("```","<code>"):r.replace("```","</code>")})),r})(t),t=(e=>{let t="";return t=e.replace(/\\/g,""),t})(t),t+"<br /><br />"})(e),u({token:r}),t({aiAnswer:r}),v(!1),m(""),w(n)})).catch((e=>console.error(e)))}))},disabled:g},(0,i.t)("Ask AI")),(0,a.createElement)("span",null," ",d),(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",g)}},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 -- GitLab From c18c02865a0761d68763c0152e024accc0bef025 Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez <danrod@gmail.com> Date: Mon, 14 Apr 2025 14:24:41 -0400 Subject: [PATCH 06/11] Issue # 3518977: ESLint fixes --- blocks/ai-block/edit.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/blocks/ai-block/edit.js b/blocks/ai-block/edit.js index 1b38254..1f53471 100644 --- a/blocks/ai-block/edit.js +++ b/blocks/ai-block/edit.js @@ -39,7 +39,7 @@ const checkIfHTMLString = (string) => { const htmlEntities = (str) => { return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/\n/g, '<br />'); -} +}; function Edit({ attributes, setAttributes }) { const { title, blockTitle, aiAnswer } = attributes; @@ -87,13 +87,16 @@ function Edit({ attributes, setAttributes }) { }) .then((response) => response.text()) .then((data) => { - let parsedData = ""; + let parsedData = ''; const regex = /\\u([\d\w]{4})/gi; const found = data.match(regex); - if ((found !== null) && (found.length > 0) && checkIfHTMLString(data)) { - parsedData = '<code>' + htmlEntities(JSON.parse(data)) + '</code><br /><br />'; + if (found !== null && found.length > 0 && checkIfHTMLString(data)) { + parsedData = + '<code>' + + htmlEntities(JSON.parse(data)) + + '</code><br /><br />'; } else { parsedData = parseAIResponse(data); } -- GitLab From 36c3f3f0bebb816549074ffe81ee5d0a0f132b80 Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez <danrod@gmail.com> Date: Mon, 14 Apr 2025 14:41:44 -0400 Subject: [PATCH 07/11] Issue # 3518977: ESLint fixes --- blocks/ai-block/edit.js | 5 +---- blocks/ai-block/parser.js | 10 ++++------ 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/blocks/ai-block/edit.js b/blocks/ai-block/edit.js index 1f53471..add07fb 100644 --- a/blocks/ai-block/edit.js +++ b/blocks/ai-block/edit.js @@ -93,10 +93,7 @@ function Edit({ attributes, setAttributes }) { const found = data.match(regex); if (found !== null && found.length > 0 && checkIfHTMLString(data)) { - parsedData = - '<code>' + - htmlEntities(JSON.parse(data)) + - '</code><br /><br />'; + parsedData ='<code>' + htmlEntities(JSON.parse(data)) + '</code><br /><br />'; } else { parsedData = parseAIResponse(data); } diff --git a/blocks/ai-block/parser.js b/blocks/ai-block/parser.js index fefc115..5226e8a 100644 --- a/blocks/ai-block/parser.js +++ b/blocks/ai-block/parser.js @@ -1,7 +1,5 @@ /* Utility class */ -import { parse } from "@wordpress/blocks"; - const checkForSampleCode = (text) => { let regex = /```[A-Za-z0-9]+/g; let found = text.match(regex); @@ -20,12 +18,12 @@ const checkForSampleCode = (text) => { regex = /```/g; found = finalCode.match(regex); - if ((found !== null) && (found.length > 0)) { + if (found !== null && found.length > 0) { found.forEach((element, index) => { if (index % 2 === 0) { - finalCode = finalCode.replace("```", "<code>"); + finalCode = finalCode.replace('```', "<code>"); } else { - finalCode = finalCode.replace("```", "</code>"); + finalCode = finalCode.replace('```', "</code>"); } }); } @@ -52,7 +50,7 @@ const parseAIResponse = (AIResponse) => { parsedData = checkForSampleCode(parsedData); parsedData = cleanUpSlashes(parsedData); - return parsedData + "<br /><br />"; + return parsedData + '<br /><br />'; } export default parseAIResponse; -- GitLab From d211006b98ba01872fd26ea63264867f1711c370 Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez <danrod@gmail.com> Date: Mon, 14 Apr 2025 14:57:17 -0400 Subject: [PATCH 08/11] Issue # 3518977: More ESLint fixes --- blocks/ai-block/edit.js | 5 ++++- blocks/ai-block/parser.js | 26 +++++++++++++------------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/blocks/ai-block/edit.js b/blocks/ai-block/edit.js index add07fb..66f2569 100644 --- a/blocks/ai-block/edit.js +++ b/blocks/ai-block/edit.js @@ -93,7 +93,10 @@ function Edit({ attributes, setAttributes }) { const found = data.match(regex); if (found !== null && found.length > 0 && checkIfHTMLString(data)) { - parsedData ='<code>' + htmlEntities(JSON.parse(data)) + '</code><br /><br />'; + parsedData = + '<code>' + + htmlEntities(JSON.parse(data)) + + '</code><br /><br />'; } else { parsedData = parseAIResponse(data); } diff --git a/blocks/ai-block/parser.js b/blocks/ai-block/parser.js index 5226e8a..6021d33 100644 --- a/blocks/ai-block/parser.js +++ b/blocks/ai-block/parser.js @@ -6,43 +6,43 @@ const checkForSampleCode = (text) => { let finalCode = ""; /* A Snippet of code was found, wrap it around <code> */ - if ((found !== null) && (found.length > 0)) { + if (found !== null && found.length > 0) { const beginCode = found[0]; - finalCode = text.replace(beginCode, "<code>"); - finalCode = finalCode.replace ("```", "</code>"); + finalCode = text.replace(beginCode, '<code>'); + finalCode = finalCode.replace ("```", '</code>'); } else { return text; } // Check for more ``` elements inside the text - regex = /```/g; - found = finalCode.match(regex); + regex = /```/g; + found = finalCode.match(regex); - if (found !== null && found.length > 0) { + if (found !== null && found.length > 0) { found.forEach((element, index) => { if (index % 2 === 0) { - finalCode = finalCode.replace('```', "<code>"); + finalCode = finalCode.replace('```', '<code>'); } else { - finalCode = finalCode.replace('```', "</code>"); + finalCode = finalCode.replace('```', '</code>'); } }); - } + } return finalCode; -} +}; const unicodeToChar = (text) => { return text.replace(/\\u[\dA-F]{4}/gi, (match) => { return String.fromCharCode(parseInt(match.replace(/\\u/g, ''), 16)); }); -} +}; const cleanUpSlashes = (text) => { let parsedData = ""; parsedData = text.replace(/\\/g, ''); return parsedData; -} +}; const parseAIResponse = (AIResponse) => { let parsedData = AIResponse.substring(1, AIResponse.length - 1); @@ -51,6 +51,6 @@ const parseAIResponse = (AIResponse) => { parsedData = cleanUpSlashes(parsedData); return parsedData + '<br /><br />'; -} +}; export default parseAIResponse; -- GitLab From 1767aab0931a4483091f34fe0662fc8961c63bd7 Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez <danrod@gmail.com> Date: Mon, 14 Apr 2025 15:11:17 -0400 Subject: [PATCH 09/11] Issue # 3518977: More ESLint fixes --- blocks/ai-block/edit.js | 8 +++++--- blocks/ai-block/parser.js | 7 ++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/blocks/ai-block/edit.js b/blocks/ai-block/edit.js index 66f2569..ad74ec7 100644 --- a/blocks/ai-block/edit.js +++ b/blocks/ai-block/edit.js @@ -38,6 +38,7 @@ const checkIfHTMLString = (string) => { }; const htmlEntities = (str) => { + /* eslint-disable-next-line prettier-prettier */ return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/\n/g, '<br />'); }; @@ -93,9 +94,10 @@ function Edit({ attributes, setAttributes }) { const found = data.match(regex); if (found !== null && found.length > 0 && checkIfHTMLString(data)) { - parsedData = - '<code>' + - htmlEntities(JSON.parse(data)) + + /* eslint-disable-next-line prefer-template */ + parsedData = + '<code>' + + htmlEntities(JSON.parse(data)) + '</code><br /><br />'; } else { parsedData = parseAIResponse(data); diff --git a/blocks/ai-block/parser.js b/blocks/ai-block/parser.js index 6021d33..98d7e32 100644 --- a/blocks/ai-block/parser.js +++ b/blocks/ai-block/parser.js @@ -3,13 +3,13 @@ const checkForSampleCode = (text) => { let regex = /```[A-Za-z0-9]+/g; let found = text.match(regex); - let finalCode = ""; + let finalCode = ''; /* A Snippet of code was found, wrap it around <code> */ if (found !== null && found.length > 0) { const beginCode = found[0]; finalCode = text.replace(beginCode, '<code>'); - finalCode = finalCode.replace ("```", '</code>'); + finalCode = finalCode.replace ('```', '</code>'); } else { return text; } @@ -38,7 +38,7 @@ const unicodeToChar = (text) => { }; const cleanUpSlashes = (text) => { - let parsedData = ""; + let parsedData = ''; parsedData = text.replace(/\\/g, ''); return parsedData; @@ -50,6 +50,7 @@ const parseAIResponse = (AIResponse) => { parsedData = checkForSampleCode(parsedData); parsedData = cleanUpSlashes(parsedData); + /* eslint-disable-next-line prefer-template */ return parsedData + '<br /><br />'; }; -- GitLab From c5db512c9f662f32714c0d87e1f9cf2ba04d3bea Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez <danrod@gmail.com> Date: Mon, 14 Apr 2025 15:24:09 -0400 Subject: [PATCH 10/11] Issue # 3518977: More ESLint fixes --- blocks/ai-block/edit.js | 14 +++++++++----- blocks/ai-block/parser.js | 2 +- dist/blocks.asset.php | 2 +- dist/blocks.js | 2 +- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/blocks/ai-block/edit.js b/blocks/ai-block/edit.js index ad74ec7..b0b2de4 100644 --- a/blocks/ai-block/edit.js +++ b/blocks/ai-block/edit.js @@ -39,7 +39,12 @@ const checkIfHTMLString = (string) => { const htmlEntities = (str) => { /* eslint-disable-next-line prettier-prettier */ - return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/\n/g, '<br />'); + return String(str) + .replace(/&/g, '&') + .replace(/</g, '<') + .replace(/>/g, '>') + .replace(/"/g, '"') + .replace(/\n/g, '<br />'); }; function Edit({ attributes, setAttributes }) { @@ -95,10 +100,9 @@ function Edit({ attributes, setAttributes }) { if (found !== null && found.length > 0 && checkIfHTMLString(data)) { /* eslint-disable-next-line prefer-template */ - parsedData = - '<code>' + - htmlEntities(JSON.parse(data)) + - '</code><br /><br />'; + parsedData = `<code>${htmlEntities( + JSON.parse(data), + )}</code><br /><br />`; } else { parsedData = parseAIResponse(data); } diff --git a/blocks/ai-block/parser.js b/blocks/ai-block/parser.js index 98d7e32..dc0ac86 100644 --- a/blocks/ai-block/parser.js +++ b/blocks/ai-block/parser.js @@ -9,7 +9,7 @@ const checkForSampleCode = (text) => { if (found !== null && found.length > 0) { const beginCode = found[0]; finalCode = text.replace(beginCode, '<code>'); - finalCode = finalCode.replace ('```', '</code>'); + finalCode = finalCode.replace('```', '</code>'); } else { return text; } diff --git a/dist/blocks.asset.php b/dist/blocks.asset.php index 1b55fae..ede8128 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' => '18aa96d38249972488c0'); +<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components'), 'version' => 'f023058c5136e39e83cf'); diff --git a/dist/blocks.js b/dist/blocks.js index 8cf3854..6781a51 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"],g={...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:""}),[g,v]=(0,a.useState)(!1),[d,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: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}),v(!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};v(!0),m("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=>{let r="";const l=e.match(/\\u([\d\w]{4})/gi);var a;r=null!==l&&l.length>0&&(e=>{if(e=JSON.parse(e),/<([A-Za-z][A-Za-z0-9]*)\b[^>]*>(.*?)<\/\1>/.test(e))return!0})(e)?"<code>"+(a=JSON.parse(e),String(a).replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/\n/g,"<br />")+"</code><br /><br />"):(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=>{let t=/```[A-Za-z0-9]+/g,n=e.match(t),r="";if(!(null!==n&&n.length>0))return e;{const t=n[0];r=e.replace(t,"<code>"),r=r.replace("```","</code>")}return t=/```/g,n=r.match(t),null!==n&&n.length>0&&n.forEach(((e,t)=>{r=t%2==0?r.replace("```","<code>"):r.replace("```","</code>")})),r})(t),t=(e=>{let t="";return t=e.replace(/\\/g,""),t})(t),t+"<br /><br />"})(e),u({token:r}),t({aiAnswer:r}),v(!1),m(""),w(n)})).catch((e=>console.error(e)))}))},disabled:g},(0,i.t)("Ask AI")),(0,a.createElement)("span",null," ",d),(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",g)}},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 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"],g={...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:""}),[g,v]=(0,a.useState)(!1),[d,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: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}),v(!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};v(!0),m("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=>{let r="";const l=e.match(/\\u([\d\w]{4})/gi);var a;r=null!==l&&l.length>0&&(e=>{if(e=JSON.parse(e),/<([A-Za-z][A-Za-z0-9]*)\b[^>]*>(.*?)<\/\1>/.test(e))return!0})(e)?`<code>${a=JSON.parse(e),String(a).replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/\n/g,"<br />")}</code><br /><br />`:(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=>{let t=/```[A-Za-z0-9]+/g,n=e.match(t),r="";if(!(null!==n&&n.length>0))return e;{const t=n[0];r=e.replace(t,"<code>"),r=r.replace("```","</code>")}return t=/```/g,n=r.match(t),null!==n&&n.length>0&&n.forEach(((e,t)=>{r=t%2==0?r.replace("```","<code>"):r.replace("```","</code>")})),r})(t),t=(e=>{let t="";return t=e.replace(/\\/g,""),t})(t),t+"<br /><br />"})(e),u({token:r}),t({aiAnswer:r}),v(!1),m(""),w(n)})).catch((e=>console.error(e)))}))},disabled:g},(0,i.t)("Ask AI")),(0,a.createElement)("span",null," ",d),(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",g)}},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 -- GitLab From 03faa840f72a8b13bb2e65f5cc3ff420886db148 Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez <danrod@gmail.com> Date: Mon, 14 Apr 2025 15:31:58 -0400 Subject: [PATCH 11/11] Issue # 3518977: More ESLint fixes --- blocks/ai-block/edit.js | 1 - 1 file changed, 1 deletion(-) diff --git a/blocks/ai-block/edit.js b/blocks/ai-block/edit.js index b0b2de4..6203c85 100644 --- a/blocks/ai-block/edit.js +++ b/blocks/ai-block/edit.js @@ -38,7 +38,6 @@ const checkIfHTMLString = (string) => { }; const htmlEntities = (str) => { - /* eslint-disable-next-line prettier-prettier */ return String(str) .replace(/&/g, '&') .replace(/</g, '<') -- GitLab