PluginProbe
bBlocks – Essential Gutenberg Blocks & Patterns Collection / 2.1.5
bBlocks – Essential Gutenberg Blocks & Patterns Collection v2.1.5
2.1.8 2.1.7 2.1.6 2.1.5 2.1.4 2.1.3 2.1.2 2.1.1 2.1.0 2.0.43 2.0.42 2.0.41 2.0.40 2.0.39 2.0.38 trunk 1.0 1.1 1.2 1.3 1.4 1.5 1.5.1 1.5.2 1.5.3 All 108 releases
b-blocks / build / b-blocks-ai / content-generate-ai.js

content-generate-ai.js in bBlocks – Essential Gutenberg Blocks & Patterns Collection 2.1.5, at build/b-blocks-ai/content-generate-ai.js

1 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (()=>{"use strict";const e=window.React,t=window.wp.compose,n=window.wp.components,r=window.wp.i18n,o=window.wp.blockEditor,s=window.ReactJSXRuntime,l=({type:e="button",href:t="",target:n="",onClick:r=null,className:o,variant:l="primary",size:c="",children:i,disabled:a=!1,...d})=>{const u=`bPlButton ${l?`variant-${l}`:""} ${c?`size-${c}`:""} ${o||""} ${a?"bPlButton-disabled":""}`;return"function"!=typeof r&&t?(0,s.jsx)("a",{href:t,target:n,className:u,...d,children:i}):(0,s.jsx)("button",{type:e,className:u,...d,onClick:r,children:i})},c=(t,n={},r=!0)=>{const[o,s]=(0,e.useState)(!1),[l,c]=(0,e.useState)(!1),[i,a]=(0,e.useState)(null),[d,u]=(0,e.useState)(null);if(!wp.ajax)return void console.error("Please use wp-util as a dependency");const h=(e={})=>{s(!0),c(!1),a(null),wp.ajax.post(t,{...n,...e}).done((e=>{s(!1),u(e)})).fail((e=>{s(!1),c(!0),a(e)}))},p=(e={})=>{h(e)};return(0,e.useEffect)((()=>{r&&h(n)}),[]),{data:d,saveData:p,refetch:p,isLoading:o,isError:l,error:i}},i=(0,s.jsxs)("svg",{xmlns:"http://www.w3.org/2000/svg",width:"20",height:"20",viewBox:"0 0 43 43",fill:"none",children:[(0,s.jsxs)("g",{clipPath:"url(#clip0_1877_1771)",children:[(0,s.jsx)("rect",{width:"43",height:"43",rx:"15",fill:"url(#paint0_linear_1877_1771)"}),(0,s.jsx)("path",{d:"M24.2285 40.5754C21.8801 38.2139 19.3932 35.7203 16.8931 33.2071L19.9671 30.265C19.9209 30.1528 19.9209 30.1462 19.8747 30.0341H11.1541C10.2107 30.0341 9.43896 29.2689 9.43896 28.319V15.1984C9.43896 14.4464 10.0458 13.8395 10.7979 13.8395H19.9803L16.7348 10.7589L24.3736 3.14648L34.2487 13.0216C35.0205 13.7934 35.0205 15.0533 34.2487 15.8185L28.1798 21.8016C30.2577 23.8597 32.2763 25.8651 34.2553 27.8242C35.0271 28.5894 35.0205 29.8296 34.2553 30.5948L24.2285 40.5754ZM20.1914 14.11V29.5394C22.7706 26.9799 25.3631 24.4006 27.9357 21.8478C25.3961 19.3147 22.8234 16.742 20.1914 14.11Z",fill:"white"})]}),(0,s.jsxs)("defs",{children:[(0,s.jsxs)("linearGradient",{id:"paint0_linear_1877_1771",x1:"-4.31905",y1:"30.2033",x2:"31.8123",y2:"30.2033",gradientUnits:"userSpaceOnUse",children:[(0,s.jsx)("stop",{stopColor:"#A997F7"}),(0,s.jsx)("stop",{offset:"1",stopColor:"#8C74FD"})]}),(0,s.jsx)("clipPath",{id:"clip0_1877_1771",children:(0,s.jsx)("rect",{width:"43",height:"43",fill:"white"})})]})]}),a=e=>(0,s.jsxs)("svg",{className:"bBlocksSvgAiIcon",enableBackground:"new 0 0 32 32",viewBox:"0 0 32 32",xmlns:"http://www.w3.org/2000/svg",...e,children:[(0,s.jsx)("defs",{children:(0,s.jsxs)("linearGradient",{id:"bBlocksAiIconGradient",x1:"0%",x2:"100%",y1:"0%",y2:"0%",children:[(0,s.jsx)("stop",{offset:"0%",stopColor:"#146ef5"}),(0,s.jsx)("stop",{offset:"100%",stopColor:"#ff7a00"})]})}),(0,s.jsx)("path",{fill:"url(#bBlocksAiIconGradient)",d:"m13.294 7.436.803 2.23c.892 2.475 2.841 4.424 5.316 5.316l2.23.803c.201.073.201.358 0 .43l-2.23.803c-2.475.892-4.424 2.841-5.316 5.316l-.803 2.23c-.073.201-.358.201-.43 0l-.803-2.23c-.892-2.475-2.841-4.424-5.316-5.316l-2.23-.803c-.201-.073-.201-.358 0-.43l2.23-.803c2.475-.892 4.424-2.841 5.316-5.316l.803-2.23c.072-.202.357-.202.43 0z"}),(0,s.jsx)("path",{className:"bBlocksSvgAiIconPathFirst",fill:"url(#bBlocksAiIconGradient)",d:"m23.332 2.077.407 1.129c.452 1.253 1.439 2.24 2.692 2.692l1.129.407c.102.037.102.181 0 .218l-1.129.407c-1.253.452-2.24 1.439-2.692 2.692l-.407 1.129c-.037.102-.181.102-.218 0l-.407-1.129c-.452-1.253-1.439-2.24-2.692-2.692l-1.129-.407c-.102-.037-.102-.181 0-.218l1.129-.407c1.253-.452 2.24-1.439 2.692-2.692l.407-1.129c.037-.103.182-.103.218 0z"}),(0,s.jsx)("path",{className:"bBlocksSvgAiIconPathSecond",fill:"url(#bBlocksAiIconGradient)",d:"m23.332 21.25.407 1.129c.452 1.253 1.439 2.24 2.692 2.692l1.129.407c.102.037.102.181 0 .218l-1.129.407c-1.253.452-2.24 1.439-2.692 2.692l-.407 1.129c-.037.102-.181.102-.218 0l-.407-1.129c-.452-1.253-1.439-2.24-2.692-2.692l-1.129-.407c-.102-.037-.102-.181 0-.218l1.129-.407c1.253-.452 2.24-1.439 2.692-2.692l.407-1.129c.037-.102.182-.102.218 0z"})]}),d=(location.origin,`${location.origin}/wp-admin/admin.php?page=b-blocks#/settings`),u=(0,t.createHigherOrderComponent)((t=>u=>{const{clientId:h,isSelected:p}=u,[m,x]=(0,e.useState)(!1),[f,b]=(0,e.useState)(""),[C,g]=(0,e.useState)(""),[j,w]=(0,e.useState)(null),{data:k,isLoading:_,error:v,refetch:A}=c("generate_ai_content",{nonce:aiContentGenerator.nonce},!1),{data:S}=c("bblocks_api_keys",{_wpnonce:aiContentGenerator.nonce},!0),{ref:B,btnRef:G}=function(t){const n=(0,e.useRef)(null),r=(0,e.useRef)(null);return(0,e.useEffect)((()=>{function e(e){n.current&&!n.current.contains(e.target)&&r.current&&!r.current.contains(e.target)&&t()}return document.addEventListener("mousedown",e),()=>{document.removeEventListener("mousedown",e)}}),[t]),{ref:n,btnRef:r}}((()=>{x(!1)}));(0,e.useEffect)((()=>{!_&&k&&g(k.content)}),[k,v,_]);const N=e=>{if(C&&j){j.focus();const t=()=>{const t=j.textContent;return"replace"===e?C:"end"===e?""===t?C:t+" "+C:void 0};let n;if(/<\/?[a-z][\s\S]*>/i.test(t())){const e=document.createRange();e.selectNodeContents(j);try{n=e.createContextualFragment(t())}catch(e){const r=(new DOMParser).parseFromString(t(),"text/html");n=document.createDocumentFragment(),Array.from(r.body.childNodes).forEach((e=>n.appendChild(e)))}}else n=document.createDocumentFragment(),n.appendChild(document.createTextNode(t()));const r=window.getSelection(),o=document.createRange();o.selectNodeContents(j),o.deleteContents(),j.appendChild(n);const s=j.lastChild,l=document.createRange();s?s.nodeType===Node.TEXT_NODE?(l.setStart(s,s.length),l.collapse(!0)):(l.setStartAfter(s),l.collapse(!0)):(l.selectNodeContents(j),l.collapse(!1)),r.removeAllRanges(),r.addRange(l),j.focus(),j.normalize(),x(!1),b(""),g("")}},y=()=>{x(!1),b(""),g("")};return(0,e.useEffect)((()=>{if(m)return;const{editableEl:e=[]}=(e=>{const{clientId:t,name:n,attributes:r}=e;if(!t)return;const o=document.querySelector(`[data-block="${t}"]`);if(!o)return[];if("true"===o.getAttribute("contenteditable"))return{blockName:n,clientId:t,editableEl:[o],attributes:r};{const e=['[contenteditable="true"]',".block-editor-rich-text__editable"];return{blockName:n,clientId:t,editableEl:o.querySelectorAll(e.join(",")),attributes:r}}})(u);return p&&e?Array.from(e).forEach((e=>{e.onclick=()=>w(e)})):w(null),()=>{Array.from(e)?.forEach((e=>{e.onclick=null}))}}),[h,m,p]),(0,s.jsxs)(s.Fragment,{children:[j&&p&&(0,s.jsx)(o.BlockControls,{children:(0,s.jsxs)(n.ToolbarGroup,{children:[(0,s.jsx)(n.ToolbarButton,{icon:a,label:(0,r.__)("Generate AI Content"),onClick:()=>x(!m),ref:G}),m&&(0,s.jsx)(n.Popover,{title:(0,r.__)("Generate AI Content"),onRequestClose:y,className:"bBlocksContentGenerateAiContainer",ref:B,children:(0,s.jsxs)("div",{className:"bBlocksContentGenerateAiContent",children:[(0,s.jsxs)("div",{className:"bBlocksContentGenerateAiHeader",children:[i,(0,s.jsx)("h2",{children:"bBlocks Ai Content Generator"})]}),S?.openAi?.key?"":(0,s.jsx)("div",{className:"bBlocksContentGenerateWarning",children:(0,s.jsxs)("p",{children:["Please add your OpenAi Api Key. ",(0,s.jsx)("a",{href:d,target:"_blank",rel:"noreferrer",children:"Go to Settings"})]})}),(0,s.jsx)(n.TextareaControl,{value:f,onChange:b,placeholder:(0,r.__)("Describe the content you want to generate..."),rows:4,disabled:_}),v&&(0,s.jsxs)("div",{className:"bBlocksContentGenerateAiError",children:[(0,s.jsx)("strong",{children:(0,r.__)("Error:")})," ",v?.message]}),(0,s.jsx)("p",{children:"Suggested Prompts"}),(0,s.jsx)("div",{className:"bBlocksContentGenerateAiSuggestions",children:["Write a short description for","Create a product overview for","Generate a service intro for"].map(((e,t)=>(0,s.jsx)("p",{onClick:()=>b(e),children:e},t)))}),(0,s.jsxs)(n.Flex,{children:[(0,s.jsx)(l,{variant:"secondary",onClick:()=>A({prompt:f}),size:"medium",disabled:_||!f,children:_&&f?(0,r.__)("Generating...","b-blocks"):(0,r.__)("Generate Content","b-blocks")}),(0,s.jsxs)(l,{variant:"danger",size:"medium",onClick:y,disabled:_,children:[" ",(0,r.__)("Cancel")," "]})]}),C&&!_&&(0,s.jsxs)("div",{className:"bBlocksContentGenerateAiPreview mt10",children:[(0,s.jsx)("h4",{children:(0,r.__)("Generated Content Preview:")}),(0,s.jsxs)("div",{className:"bBlocksAiContentPreview",children:[" ",C," "]}),(0,s.jsxs)(n.Flex,{className:"mt10",children:[(0,s.jsx)(l,{size:"medium",onClick:()=>N("replace"),children:(0,r.__)("Replace","b-blocks")}),(0,s.jsx)(l,{size:"medium",onClick:()=>N("end"),children:(0,r.__)("Insert")})]})]})]})})]})}),(0,s.jsx)(t,{...u})]})}));wp.hooks.addFilter("editor.BlockEdit","b-blocks",u)})();