Commit inicial powerservice
This commit is contained in:
+17
@@ -0,0 +1,17 @@
|
||||
<?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase -- This filename format is required to dynamically load the necessary block dependencies.
|
||||
/**
|
||||
* Block script dependencies.
|
||||
*
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\ContentAI
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
return [
|
||||
'dependencies' => [
|
||||
'wp-element',
|
||||
'wp-block-editor',
|
||||
'lodash',
|
||||
],
|
||||
'version' => rank_math()->version,
|
||||
];
|
||||
+1
File diff suppressed because one or more lines are too long
+21
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"$schema": "https://schemas.wp.org/trunk/block.json",
|
||||
"apiVersion": 3,
|
||||
"name": "rank-math/command",
|
||||
"title": "AI Assistant [Content AI]",
|
||||
"icon": "star",
|
||||
"category": "rank-math-blocks",
|
||||
"description": "Generate content without any hassle, powered by Rank Math's Content AI.",
|
||||
"keywords": [ "ai", "content", "rank math", "rankmath", "content ai", "seo" ],
|
||||
"textdomain": "rank-math-pro",
|
||||
"editorScript": "file:../js/index.js",
|
||||
"attributes": {
|
||||
"content": {
|
||||
"type": "string",
|
||||
"source": "html",
|
||||
"selector": "p",
|
||||
"default": "<span>//</span>",
|
||||
"__experimentalRole": "content"
|
||||
}
|
||||
}
|
||||
}
|
||||
+229
@@ -0,0 +1,229 @@
|
||||
/**
|
||||
* WordPress dependencies
|
||||
*/
|
||||
import {
|
||||
RichText,
|
||||
useBlockProps,
|
||||
BlockControls,
|
||||
store as blockEditorStore,
|
||||
} from '@wordpress/block-editor'
|
||||
import { useDispatch } from '@wordpress/data'
|
||||
import { useRef, useEffect } from '@wordpress/element'
|
||||
import { __ } from '@wordpress/i18n'
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import hasError from '../../../../assets/src/helpers/hasError'
|
||||
import insertCommandBox, { useBlock, regenerateOutput, writeMore } from '../../../../assets/src/shortcutCommand/insertCommandBox'
|
||||
import getWriteAttributes from '../../../../assets/src/helpers/getWriteAttributes'
|
||||
import getBlockContent from '../../../../assets/src/helpers/getBlockContent'
|
||||
|
||||
const getErrorMessage = () => {
|
||||
// This function can be simplified now that the error message comes from the API.
|
||||
// It's still useful for showing general errors not from the API.
|
||||
|
||||
if ( ! rankMath.contentAI.isUserRegistered ) {
|
||||
return (
|
||||
<>
|
||||
{ __( 'Start using Content AI by connecting your RankMath account.', 'seo-by-rank-math' ) }
|
||||
<a href={ rankMath.connectSiteUrl }>{ __( 'Connect Now', 'seo-by-rank-math' ) }</a>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
if ( ! rankMath.contentAI.plan ) {
|
||||
return (
|
||||
<>
|
||||
{ __( 'You do not have a Rank Math AI plan.', 'seo-by-rank-math' ) }
|
||||
<a href="https://rankmath.com/ai/pricing/?utm_source=Plugin&utm_medium=Buy+Plan+Button&utm_campaign=WP">
|
||||
{ __( 'Choose your plan', 'seo-by-rank-math' ) }
|
||||
</a>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
{ __( 'You have exhausted your Rank Math AI Usage.', 'seo-by-rank-math' ) }
|
||||
<a href="https://rankmath.com/ai/pricing/?utm_source=Plugin&utm_medium=Buy+Credits+Button&utm_campaign=WP" target="_blank" rel="noreferrer">
|
||||
{ __( 'Get more', 'seo-by-rank-math' ) }
|
||||
</a>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default ( {
|
||||
attributes,
|
||||
onReplace,
|
||||
setAttributes,
|
||||
clientId,
|
||||
} ) => {
|
||||
// Note the addition of `hasApiError` in the attributes destructuring.
|
||||
const { content, isAiGenerated, hasApiError, endpoint, params } = attributes
|
||||
const blockProps = useBlockProps( {
|
||||
className: 'rank-math-content-ai-command',
|
||||
} )
|
||||
const { updateBlockAttributes, removeBlock } = useDispatch( blockEditorStore )
|
||||
const blockHook = useBlock // assign the hook to a variable
|
||||
|
||||
const contentEditableRef = useRef( null )
|
||||
useEffect( () => {
|
||||
const { current: contentEditable } = contentEditableRef
|
||||
if ( ! contentEditable ) {
|
||||
return
|
||||
}
|
||||
contentEditable.focus()
|
||||
|
||||
const range = document.createRange()
|
||||
const selection = window.getSelection()
|
||||
range.selectNodeContents( contentEditable )
|
||||
range.collapse( false )
|
||||
selection.removeAllRanges()
|
||||
selection.addRange( range )
|
||||
}, [] )
|
||||
|
||||
const handleRunCommand = () => {
|
||||
const text = getBlockContent( { attributes } )
|
||||
|
||||
if ( ! text.trim() ) {
|
||||
return
|
||||
}
|
||||
|
||||
updateBlockAttributes(
|
||||
clientId,
|
||||
{
|
||||
content: '',
|
||||
className: '',
|
||||
}
|
||||
)
|
||||
|
||||
insertCommandBox( 'Write', getWriteAttributes( text ), clientId )
|
||||
}
|
||||
|
||||
const handleDismissCommand = () => {
|
||||
removeBlock( clientId )
|
||||
}
|
||||
|
||||
const handleUseBlock = () => {
|
||||
blockHook( clientId, attributes )
|
||||
}
|
||||
|
||||
const handleRegenerateOutput = () => {
|
||||
regenerateOutput( clientId, endpoint, params )
|
||||
}
|
||||
|
||||
const handleWriteMore = () => {
|
||||
writeMore( clientId )
|
||||
}
|
||||
|
||||
const handleKeyDown = ( event ) => {
|
||||
if ( event.key === 'Enter' && ! isAiGenerated ) {
|
||||
event.preventDefault()
|
||||
handleRunCommand()
|
||||
}
|
||||
}
|
||||
|
||||
const contentTrimmed = content.replace( /(<([^>]+)>)/ig, '' ).trim()
|
||||
|
||||
const renderActionButtons = () => {
|
||||
// This is the correct condition to show the dismiss button on API error.
|
||||
if ( hasApiError ) {
|
||||
return (
|
||||
<button
|
||||
className="button button-small rank-math-content-ai-dismiss"
|
||||
title={ __( 'Dismiss', 'seo-by-rank-math' ) }
|
||||
onClick={ handleDismissCommand }
|
||||
contentEditable={ false }
|
||||
>
|
||||
{ __( 'Dismiss', 'seo-by-rank-math' ) }
|
||||
</button>
|
||||
)
|
||||
}
|
||||
|
||||
if ( isAiGenerated ) {
|
||||
return (
|
||||
<div className="rank-math-content-ai-command-buttons">
|
||||
<button
|
||||
className="button button-small rank-math-content-ai-use"
|
||||
onClick={ handleUseBlock }
|
||||
>
|
||||
<span>{ __( 'Use', 'seo-by-rank-math' ) }</span>
|
||||
</button>
|
||||
<button
|
||||
className="button button-small rank-math-content-ai-regenerate"
|
||||
onClick={ handleRegenerateOutput }
|
||||
>
|
||||
<span>{ __( 'Regenerate', 'seo-by-rank-math' ) }</span>
|
||||
</button>
|
||||
<button
|
||||
className="button button-small rank-math-content-ai-write-more"
|
||||
onClick={ handleWriteMore }
|
||||
>
|
||||
<span>{ __( 'Write More', 'seo-by-rank-math' ) }</span>
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// This is the condition for the initial command box.
|
||||
if ( contentTrimmed.length > 0 ) {
|
||||
return (
|
||||
<>
|
||||
<button
|
||||
className="rank-math-content-ai-command-button"
|
||||
title={ __( 'Click or Press Enter', 'seo-by-rank-math' ) }
|
||||
onClick={ handleRunCommand }
|
||||
contentEditable={ false }
|
||||
>
|
||||
<i className="rm-icon rm-icon-enter-key"></i>
|
||||
</button>
|
||||
<button
|
||||
className="rank-math-command-dismiss-button"
|
||||
title={ __( 'Dismiss', 'seo-by-rank-math' ) }
|
||||
onClick={ handleDismissCommand }
|
||||
contentEditable={ false }
|
||||
>
|
||||
{ __( 'Close', 'seo-by-rank-math' ) }
|
||||
</button>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
<div { ...blockProps }>
|
||||
<BlockControls />
|
||||
{
|
||||
hasError() &&
|
||||
<div className="rich-text" ref={ contentEditableRef }>
|
||||
{ getErrorMessage() }
|
||||
</div>
|
||||
}
|
||||
|
||||
{ ! hasError() &&
|
||||
<>
|
||||
<RichText
|
||||
tagName="div"
|
||||
allowedFormats={ [] }
|
||||
value={ content }
|
||||
onChange={ ( newContent ) => {
|
||||
setAttributes( { content: newContent } )
|
||||
} }
|
||||
onSplit={ () => false }
|
||||
onReplace={ onReplace }
|
||||
data-empty={ content ? false : true }
|
||||
onKeyDown={ handleKeyDown }
|
||||
ref={ contentEditableRef }
|
||||
/>
|
||||
|
||||
<div className="rank-math-content-ai-command-buttons">
|
||||
{ renderActionButtons() }
|
||||
</div>
|
||||
</>
|
||||
}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* WordPress dependencies
|
||||
*/
|
||||
import { registerBlockType } from '@wordpress/blocks'
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import edit from './edit'
|
||||
import metadata from './block.json'
|
||||
|
||||
const { name } = metadata
|
||||
|
||||
export { metadata, name }
|
||||
|
||||
export const settings = {
|
||||
icon: <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 17.08 18.02"><g id="Layer_2" data-name="Layer 2"><g id="Layer_1-2" data-name="Layer 1"><path d="M16.65,12.08a.83.83,0,0,0-1.11.35A7.38,7.38,0,1,1,9,1.63a7.11,7.11,0,0,1,.92.06A2.52,2.52,0,0,1,11,.23,8.87,8.87,0,0,0,9,0a9,9,0,1,0,8,13.19A.83.83,0,0,0,16.65,12.08Z"/><path d="M7.68,7.29A1.58,1.58,0,0,0,6.2,8.94a1.57,1.57,0,0,0,1.48,1.64A1.56,1.56,0,0,0,9.16,8.94,1.57,1.57,0,0,0,7.68,7.29Z" /><path d="M13.34,4.71a2.45,2.45,0,0,1-1,.2A2.53,2.53,0,0,1,9.93,3,7.18,7.18,0,0,0,9.12,3a6,6,0,1,0,4.22,1.73ZM10.53,11.3a.75.75,0,1,1-1.5,0v-.06a2.4,2.4,0,0,1-1.66.69,2.81,2.81,0,0,1-2.58-3,2.82,2.82,0,0,1,2.58-3A2.39,2.39,0,0,1,9,6.64a.75.75,0,0,1,1.5.07Zm2.56,0a.75.75,0,1,1-1.5,0V6.71a.75.75,0,1,1,1.5,0Z" /><circle cx="12.42" cy="2.37" r="1.45" /></g></g></svg>,
|
||||
edit,
|
||||
}
|
||||
|
||||
registerBlockType( { name, ...metadata }, settings )
|
||||
Reference in New Issue
Block a user