import type { FormEvent } from 'react'; import { BlockControls, InspectorControls, useBlockProps, } from '@wordpress/block-editor'; import { Button, Placeholder, RangeControl, Spinner as WCSpinner, ToggleControl, ToolbarGroup, TextControl, ExternalLink, __experimentalInputControl as WCInputControl, __experimentalToolsPanel as ToolsPanel, __experimentalToolsPanelItem as ToolsPanelItem, } from '@wordpress/components'; import { createInterpolateElement, useState } from '@wordpress/element'; import { grid, list, pencil, rss } from '@wordpress/icons'; import { __, _x, sprintf } from '@wordpress/i18n'; import { prependHTTPS } from '@wordpress/url'; import { useServerSideRender } from '@wordpress/server-side-render'; import { useDisabled } from '@wordpress/compose'; import type { BlockEditProps } from '@wordpress/blocks'; import { useToolsPanelDropdownMenuProps } from '../utils/hooks'; import HtmlRenderer from '../utils/html-renderer'; import type { RSSAttributes } from './types'; const DEFAULT_MIN_ITEMS = 1; const DEFAULT_MAX_ITEMS = 20; export default function RSSEdit( { attributes, setAttributes, name, }: BlockEditProps< RSSAttributes > ) { const [ isEditing, setIsEditing ] = useState( ! attributes.feedURL ); const { blockLayout, columns, displayAuthor, displayDate, displayExcerpt, excerptLength, feedURL, itemsToShow, openInNewTab, rel, } = attributes; // The typed URL is written to `feedURL` only on Apply. Reset it when // `feedURL` changes outside the form, such as on undo. const [ url, setURL ] = useState< string | undefined >( feedURL ); const [ prevFeedURL, setPrevFeedURL ] = useState( feedURL ); if ( feedURL !== prevFeedURL ) { setPrevFeedURL( feedURL ); setURL( feedURL ); } const dropdownMenuProps = useToolsPanelDropdownMenuProps(); function toggleAttribute( propName: 'displayAuthor' | 'displayDate' | 'displayExcerpt' ) { return () => { const value = attributes[ propName ]; setAttributes( { [ propName ]: ! value } ); }; } function onSubmitURL( event: FormEvent ) { event.preventDefault(); if ( url ) { setAttributes( { feedURL: prependHTTPS( url ) } ); setIsEditing( false ); } } const { content, status, error } = useServerSideRender( { attributes, skipBlockSupportAttributes: true, block: name, } ); const disabledRef = useDisabled(); const blockProps = useBlockProps( { ref: isEditing ? null : disabledRef } ); const label = __( 'RSS URL' ); if ( isEditing ) { return (
setURL( value ) } className="wp-block-rss__placeholder-input" />
); } const toolbarControls = [ { icon: pencil, title: __( 'Edit RSS URL' ), onClick: () => setIsEditing( true ), }, { icon: list, title: _x( 'List view', 'RSS block display setting' ), onClick: () => setAttributes( { blockLayout: 'list' } ), isActive: blockLayout === 'list', }, { icon: grid, title: _x( 'Grid view', 'RSS block display setting' ), onClick: () => setAttributes( { blockLayout: 'grid' } ), isActive: blockLayout === 'grid', }, ]; return ( <> { setAttributes( { itemsToShow: 5, displayAuthor: false, displayDate: false, displayExcerpt: false, excerptLength: 55, columns: 2, openInNewTab: false, } ); } } dropdownMenuProps={ dropdownMenuProps } > itemsToShow !== 5 } onDeselect={ () => setAttributes( { itemsToShow: 5 } ) } isShownByDefault > setAttributes( { itemsToShow: value } ) } min={ DEFAULT_MIN_ITEMS } max={ DEFAULT_MAX_ITEMS } required /> !! displayAuthor } onDeselect={ () => setAttributes( { displayAuthor: false } ) } isShownByDefault > !! displayDate } onDeselect={ () => setAttributes( { displayDate: false } ) } isShownByDefault > !! displayExcerpt } onDeselect={ () => setAttributes( { displayExcerpt: false } ) } isShownByDefault > { displayExcerpt && ( excerptLength !== 55 } onDeselect={ () => setAttributes( { excerptLength: 55 } ) } isShownByDefault > setAttributes( { excerptLength: value } ) } min={ 10 } max={ 100 } required /> ) } { blockLayout === 'grid' && ( columns !== 2 } onDeselect={ () => setAttributes( { columns: 2 } ) } isShownByDefault > setAttributes( { columns: value } ) } min={ 2 } max={ 6 } required /> ) } !! openInNewTab } onDeselect={ () => setAttributes( { openInNewTab: false } ) } isShownByDefault > setAttributes( { openInNewTab: value } ) } /> Link Relation attribute defines the relationship between a linked resource and the current document.' ), { a: ( // @ts-expect-error `createInterpolateElement` fills in the children. ), } ) } value={ rel || '' } onChange={ ( value ) => setAttributes( { rel: value } ) } /> { status === 'loading' && (
) } { status === 'error' && (

{ sprintf( /* translators: %s: error message returned when rendering the block. */ __( 'Error: %s' ), error! ) }

) } { status === 'success' && ( ) } ); }