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 (
{ sprintf( /* translators: %s: error message returned when rendering the block. */ __( 'Error: %s' ), error! ) }