/** * JavaScript code for the "Edit" section integration of the "Table Information". * * @package TablePress * @subpackage Views JavaScript * @author Tobias Bäthge * @since 3.0.0 */ /** * WordPress dependencies. */ import { useEffect, useState } from 'react'; import { Button, __experimentalHStack as HStack, // eslint-disable-line @wordpress/no-unsafe-wp-apis Icon, __experimentalInputControl as InputControl, // eslint-disable-line @wordpress/no-unsafe-wp-apis __experimentalInputControlSuffixWrapper as InputControlSuffixWrapper, // eslint-disable-line @wordpress/no-unsafe-wp-apis TextareaControl, TextControl, __experimentalVStack as VStack, // eslint-disable-line @wordpress/no-unsafe-wp-apis } from '@wordpress/components'; import { useCopyToClipboard } from '@wordpress/compose'; import { useDispatch } from '@wordpress/data'; import { createInterpolateElement } from '@wordpress/element'; import { copySmall } from '@wordpress/icons'; import { __, sprintf } from '@wordpress/i18n'; import { store as noticesStore } from '@wordpress/notices'; /** * Internal dependencies. */ import { initializeReactComponentInPortal } from '../common/react-loader'; import { TablePressIconSimple } from '../../img/tablepress-icon'; const Section = ( { tableMeta, updateTableMeta } ) => { const { createSuccessNotice } = useDispatch( noticesStore ); const [ tableId, setTableId ] = useState( tableMeta.id ); const copyShortcodeButtonRef = useCopyToClipboard( `[${ tp.table.shortcode } id=${ tableMeta.id } /]`, () => { createSuccessNotice( __( 'Copied Shortcode to clipboard.', 'tablepress' ), { type: 'snackbar', icon: , } ); } ); useEffect( () => { setTableId( tableMeta.id ); }, [ tableMeta.id ] ); return (
setTableId( newTableId.replace( /[^0-9a-zA-Z_-]/g, '' ) ) } onBlur={ ( event ) => { if ( tableMeta.id === tableId ) { return; } // The table IDs "" and "0" are not allowed, or in other words, the table ID has to fulfill /[A-Za-z1-9-_]|[A-Za-z0-9-_]{2,}/. if ( '' === tableId || '0' === tableId ) { // This alert can not be replaced by the `Alert` component, as that does not stop the cmd+S keyboard shortcut from running. window.alert( __( 'This table ID is invalid. Please enter a different table ID.', 'tablepress' ) ); setTableId( tableMeta.id ); event.target.focus(); return; } // This alert can not be replaced by a `Modal` component, as that does not stop the cmd+S keyboard shortcut from running. if ( ! window.confirm( __( 'Do you really want to change the Table ID? All blocks and Shortcodes for this table in your posts and pages will have to be adjusted!', 'tablepress' ) ) ) { setTableId( tableMeta.id ); return; } // Set the new table ID. updateTableMeta( { id: tableId } ); document.getElementById( 'table-information-shortcode' ).focus(); } } required={ true } readOnly={ ! tp.screenOptions.currentUserCanEditTableId } />
updateTableMeta( { name } ) } />
updateTableMeta( { description } ) } rows="4" />
{ __( 'Last Modified', 'tablepress' ) }: { sprintf( __( '%1$s by %2$s', 'tablepress' ), tableMeta.lastModified, tableMeta.lastEditor ) }
); }; initializeReactComponentInPortal( 'table-information', 'edit', Section, );