| 1 |
(()=>{"use strict";const e={n:t=>{const i=t&&t.__esModule?()=>t.default:()=>t;return e.d(i,{a:i}),i},d:(t,i)=>{if(Array.isArray(i))for(var n=0;n<i.length;){var a=i[n++],s=i[n++];e.o(t,a)?0===s&&n++:0===s?Object.defineProperty(t,a,{enumerable:!0,value:i[n++]}):Object.defineProperty(t,a,{enumerable:!0,get:s})}else for(var a in i)e.o(i,a)&&!e.o(t,a)&&Object.defineProperty(t,a,{enumerable:!0,get:i[a]})},o:(e,t)=>Object.hasOwn(e,t)},t=window.wp.blockEditor,i=window.wp.compose,n=window.wp.hooks,a=window.wp.components,s=window.wp.data,r=window.wp.dom,o=window.wp.element,l=window.wp.i18n,c=window.wp.notices,d=window.wp.editor,u=window.wp.apiFetch;var h=e.n(u);const g=window.wp.url;let _=!1;const w=()=>{_||(console.warn("[AI] Client ability execution is unavailable. Falling back to REST."),_=!0)},p=(e,t,i)=>{if("GET"===i||"DELETE"===i)return{path:null==t?`/wp-abilities/v1/abilities/${e}/run`:(0,g.addQueryArgs)(`/wp-abilities/v1/abilities/${e}/run`,{input:t}),method:i};const n={path:`/wp-abilities/v1/abilities/${e}/run`,method:"POST"};return void 0!==t&&(n.data={input:t}),n};function m(e){const t=e.attributes.content??e.attributes.value??"";return"string"==typeof t?t:function(e){return null!==e&&"object"==typeof e&&"toHTMLString"in e&&"function"==typeof e.toHTMLString}(t)?t.toHTMLString():""}window.wp.blocks;const b=window.ReactJSXRuntime,f=(0,b.jsx)("svg",{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24",width:"24",height:"24",fill:"currentColor",children:(0,b.jsx)("path",{d:"M3 12h5.5l-2.3-2.3 1-1L11 12.5l-3.8 3.8-1-1L8.5 13H3v-1zm18 0h-5.5l2.3-2.3-1-1L13 12.5l3.8 3.8 1-1L15.5 13H21v-1z"})}),x=(0,b.jsx)("svg",{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24",width:"24",height:"24",fill:"currentColor",children:(0,b.jsx)("path",{d:"M10 12H4.5l2.3-2.3-1-1L2 12.5l3.8 3.8 1-1L4.5 13H10v-1zm4 0h5.5l-2.3-2.3 1-1L22 12.5l-3.8 3.8-1-1L19.5 13H14v-1z"})}),v=(0,b.jsx)("svg",{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24",width:"24",height:"24",fill:"currentColor",children:(0,b.jsx)("path",{d:"M17.65 6.35A7.96 7.96 0 0012 4c-4.42 0-7.99 3.58-7.99 8s3.57 8 7.99 8c3.73 0 6.84-2.55 7.73-6h-2.08A5.99 5.99 0 0112 18c-3.31 0-6-2.69-6-6s2.69-6 6-6c1.66 0 3.14.69 4.22 1.78L13 11h7V4l-2.35 2.35z"})});const z=window.wp.wordcount;function j(e){return(0,z.count)(e,"characters_excluding_spaces")}const y=window.wp.primitives;function C({className:e="wpai-icon"}){return(0,b.jsxs)(y.SVG,{viewBox:"0 0 236 236",className:e,children:[(0,b.jsx)(y.Path,{d:"m183.227 194.475-32.474-84.289V79.172h12.744v-12.12H72.494v12.12h12.744v31.014l-32.474 84.289a11.7 11.7 0 0 0-.764 4.118C52 204.885 57.101 210 63.376 210h109.239c1.405 0 2.791-.267 4.106-.766 5.866-2.264 8.781-8.877 6.506-14.759M97.324 112.45V79.529h41.343v32.921l16.156 41.939a45.6 45.6 0 0 0-11.357-1.426c-10.878 0-21.187 3.833-29.381 10.695a33.5 33.5 0 0 1-21.56 7.824 33.4 33.4 0 0 1-16.317-4.224zm-32.918 85.43 7.412-19.214a45.6 45.6 0 0 0 20.725 4.955c10.878 0 21.187-3.833 29.381-10.695 6.025-5.026 13.561-7.824 21.56-7.824a33.4 33.4 0 0 1 17.347 4.83l10.754 27.948z"}),(0,b.jsx)(y.Path,{d:"m116.024 85.774.911 2.469c1.194 3.237 1.791 4.856 2.969 6.036 1.177 1.181 2.791 1.78 6.019 2.978l2.462.913-2.462.914c-3.228 1.198-4.842 1.797-6.019 2.977-1.178 1.181-1.775 2.799-2.969 6.037l-.911 2.468-.911-2.468c-1.195-3.238-1.792-4.856-2.969-6.037-1.178-1.18-2.792-1.779-6.02-2.977l-2.462-.914 2.462-.913c3.228-1.198 4.842-1.797 6.02-2.978 1.177-1.18 1.774-2.799 2.969-6.036zM122.546 120.224l1.223 3.317c1.605 4.349 2.408 6.524 3.99 8.11 1.582 1.587 3.75 2.391 8.087 4.001l3.308 1.227-3.308 1.227c-4.337 1.61-6.505 2.414-8.087 4.001-1.582 1.586-2.385 3.761-3.99 8.11l-1.223 3.317-1.224-3.317c-1.605-4.349-2.407-6.524-3.989-8.11-1.582-1.587-3.751-2.391-8.088-4.001l-3.308-1.227 3.308-1.227c4.337-1.61 6.506-2.414 8.088-4.001 1.582-1.586 2.384-3.761 3.989-8.11zM117.882 44.024l.645 1.749c.846 2.294 1.27 3.44 2.104 4.277s1.978 1.261 4.265 2.11l1.745.647-1.745.648c-2.287.848-3.431 1.273-4.265 2.11-.834.836-1.258 1.983-2.104 4.277l-.645 1.749-.646-1.75c-.846-2.293-1.269-3.44-2.104-4.277-.834-.836-1.978-1.26-4.265-2.11l-1.744-.647 1.744-.647c2.287-.849 3.431-1.273 4.265-2.11.835-.836 1.258-1.983 2.104-4.277zM130.622 26l.461 1.25c.605 1.638.907 2.457 1.503 3.055.596.597 1.413.9 3.047 1.507l1.246.462-1.246.462c-1.634.607-2.451.91-3.047 1.507-.596.598-.898 1.417-1.503 3.056l-.461 1.249-.461-1.25c-.604-1.638-.907-2.457-1.503-3.055-.596-.597-1.413-.9-3.046-1.507l-1.246-.462 1.246-.462c1.633-.607 2.45-.91 3.046-1.507.596-.598.899-1.417 1.503-3.055z"})]})}const k="ai_content_resizing_error";function L({clientId:e}){const[i,n]=(0,o.useState)(!1),[u,g]=(0,o.useState)(!1),[_,z]=(0,o.useState)(null),[y,L]=(0,o.useState)(null),N=(0,o.useRef)(null);(0,o.useLayoutEffect)(()=>{u&&N.current?.scrollIntoView({block:"start",behavior:"auto"})},[u,_]);const{blockContent:S,isResized:M,postId:T}=(0,s.useSelect)(i=>{const n=i(t.store).getBlock(e);return{blockContent:n?m(n):"",isResized:n?.attributes.aiResized??!1,postId:i(d.store).getCurrentPostId()}},[e]),R=(0,s.useDispatch)(t.store),H=(0,s.useDispatch)(c.store),A=(0,o.useCallback)(async e=>{if(function(e){const t=window.aiProviderData??{hasProvider:!1,connectorsUrl:""};if(t.hasProvider)return!0;const{connectorsUrl:i}=t;return(0,s.dispatch)(c.store).createErrorNotice((0,l.__)("This feature requires an AI Connector to function properly.","ai"),{id:e,isDismissible:!0,actions:i?[{label:(0,l.__)("Manage Connectors","ai"),url:i}]:[]}),!1}(k)){if("shorten"===e&&j(S)<(()=>{const e=window.aiContentResizingData??{};return{enabled:e.enabled??!1,minContentLength:e.minContentLength??25}})().minContentLength)return void H.createErrorNotice((0,l.__)("Text is too short to shorten further.","ai"),{id:k,isDismissible:!0});n(!0),L(e),z(null),g(!0),H.removeNotice(k);try{const t=await async function(e,t,i){try{const i=await(async()=>{try{const{ready:e}=await import("@wordpress/core-abilities");return await e,await import("@wordpress/abilities")}catch{return null}})();if("function"==typeof i?.executeAbility)return await i.executeAbility(e,t);w()}catch(e){if(console.error(e),!(e=>{if(!e||"object"!=typeof e)return!1;const t="message"in e&&"string"==typeof e.message?e.message:"",i="code"in e&&"string"==typeof e.code?e.code:"";return"ability_not_found"===i||t.includes("Ability not found")||t.includes("missing callback")||"ability_invalid_input"===i})(e))throw e;w()}const n=i?.method??"POST";return await h()({...p(e,t,n),...i?.signal?{signal:i.signal}:{}})}("ai/content-resizing",{content:S,action:e,post_id:T});z(t)}catch(e){const t=e instanceof Error?e.message:(0,l.__)("An error occurred while resizing content.","ai");H.createErrorNotice(t,{id:k,isDismissible:!0}),g(!1)}finally{n(!1)}}},[S,H,T]),D=(0,o.useCallback)(()=>{null!==_&&R.updateBlockAttributes(e,{content:(0,r.safeHTML)(_),aiResized:!0}),z(null),L(null),g(!1)},[R,e,_]),E=(0,o.useCallback)(()=>{z(null),L(null),g(!1)},[]),B=(0,o.useCallback)(()=>{y&&!i&&A(y)},[A,i,y]),I=(0,o.useMemo)(()=>{if(null===_)return null;const e=j(_)-j(S);if(0===e)return{modifier:"neutral",label:(0,l.__)("No change","ai"),ariaLabel:(0,l.__)("No change in character count","ai")};const t=Math.abs(e);return e>0?{modifier:"positive",label:(0,l.sprintf)(/* translators: %d: Number of characters added. */ /* translators: %d: Number of characters added. */ |
| 2 |
(0,l._n)("+%d character","+%d characters",t,"ai"),t),ariaLabel:(0,l.sprintf)(/* translators: %d: Number of characters added. */ /* translators: %d: Number of characters added. */ |
| 3 |
(0,l._n)("%d character added","%d characters added",t,"ai"),t)}:{modifier:"negative",label:(0,l.sprintf)(/* translators: %d: Number of characters removed. */ /* translators: %d: Number of characters removed. */ |
| 4 |
(0,l._n)("−%d character","−%d characters",t,"ai"),t),ariaLabel:(0,l.sprintf)(/* translators: %d: Number of characters removed. */ /* translators: %d: Number of characters removed. */ |
| 5 |
(0,l._n)("%d character removed","%d characters removed",t,"ai"),t)}},[S,_]),O=[{title:(0,l.__)("Shorten","ai"),icon:f,onClick:()=>A("shorten")},{title:(0,l.__)("Expand","ai"),icon:x,onClick:()=>A("expand")},{title:(0,l.__)("Rephrase","ai"),icon:v,onClick:()=>A("rephrase")}];return(0,b.jsxs)(b.Fragment,{children:[(0,b.jsx)(a.ToolbarGroup,{className:M?"ai-content-resizing-toolbar--has-changes":"",children:(0,b.jsx)(a.ToolbarDropdownMenu,{icon:(0,b.jsx)(C,{className:"ai-content-resizing-toolbar__icon"}),label:(0,l.__)("Resize Content","ai"),controls:O})}),u&&(0,b.jsxs)(a.Modal,{title:(0,l.__)("Suggested replacement","ai"),onRequestClose:E,isFullScreen:!1,size:"medium",className:"ai-content-resizing-modal",focusOnMount:"firstContentElement",children:[(0,b.jsxs)("section",{className:"ai-content-resizing-modal__panel","aria-label":(0,l.__)("Original content","ai"),children:[(0,b.jsx)("div",{className:"ai-content-resizing-modal__label",children:(0,b.jsx)("span",{children:(0,l.__)("Original","ai")})}),(0,b.jsx)("div",{className:"ai-content-resizing-modal__text ai-content-resizing-modal__text--original",dangerouslySetInnerHTML:{__html:(0,r.safeHTML)(S)}})]}),(0,b.jsxs)("section",{className:"ai-content-resizing-modal__panel","aria-label":(0,l.__)("Suggested content","ai"),children:[(0,b.jsxs)("div",{className:"ai-content-resizing-modal__label",children:[(0,b.jsx)("span",{children:(0,l.__)("Suggested","ai")}),!i&&I&&(0,b.jsx)("span",{className:`ai-content-resizing-modal__diff ai-content-resizing-modal__diff--${I.modifier}`,"aria-label":I.ariaLabel,children:I.label})]}),i?(0,b.jsxs)("div",{className:"ai-content-resizing-modal__text ai-content-resizing-modal__loading",role:"status","aria-live":"polite",children:[(0,b.jsxs)("div",{className:"ai-content-resizing-modal__loading-status",children:[(0,b.jsx)(a.Spinner,{}),(0,b.jsx)("span",{children:(0,l.__)("Generating…","ai")})]}),(0,b.jsx)("div",{className:"ai-content-resizing-modal__loading-skeleton","aria-hidden":"true",children:Array.from({length:3}).map((e,t)=>(0,b.jsx)("span",{className:"ai-content-resizing-modal__loading-skeleton-line"},t))})]}):(0,b.jsx)("div",{className:"ai-content-resizing-modal__text",dangerouslySetInnerHTML:{__html:(0,r.safeHTML)(_??"")}})]}),(0,b.jsxs)(a.Flex,{justify:"flex-start",gap:2,className:"ai-content-resizing-modal__actions",children:[(0,b.jsx)(a.Button,{variant:"primary",onClick:D,disabled:i||null===_,accessibleWhenDisabled:!0,ref:N,__next40pxDefaultSize:!0,children:(0,l.__)("Accept","ai")}),(0,b.jsx)(a.Button,{variant:"secondary",onClick:B,disabled:i||null===y,isBusy:i,accessibleWhenDisabled:!0,__next40pxDefaultSize:!0,children:i?(0,l.__)("Generating…","ai"):(0,l.__)("Regenerate","ai")})]})]})]})}const{aiContentResizingData:N}=window;(0,n.addFilter)("blocks.registerBlockType","ai/content-resizing-attribute",(e,t)=>"core/paragraph"===t&&N?.enabled?{...e,attributes:{...e.attributes,aiResized:{type:"boolean",default:!1}}}:e);const S=(0,i.createHigherOrderComponent)(e=>i=>"core/paragraph"===i.name&&N?.enabled?(0,b.jsxs)(b.Fragment,{children:[i.isSelected&&(0,b.jsx)(t.BlockControls,{children:(0,b.jsx)(L,{clientId:i.clientId,blockName:i.name})}),(0,b.jsx)(e,{...i})]}):(0,b.jsx)(e,{...i}),"withContentResizing");(0,n.addFilter)("editor.BlockEdit","ai/content-resizing",S)})(); |