| 1 |
/** |
| 2 |
* JavaScript code for the "Edit" section integration of the "Table Information". |
| 3 |
* |
| 4 |
* @package TablePress |
| 5 |
* @subpackage Views JavaScript |
| 6 |
* @author Tobias Bäthge |
| 7 |
* @since 3.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* WordPress dependencies. |
| 12 |
*/ |
| 13 |
import { useEffect, useState } from 'react'; |
| 14 |
import { |
| 15 |
__experimentalHStack as HStack, // eslint-disable-line @wordpress/no-unsafe-wp-apis |
| 16 |
TextareaControl, |
| 17 |
TextControl, |
| 18 |
__experimentalVStack as VStack, // eslint-disable-line @wordpress/no-unsafe-wp-apis |
| 19 |
} from '@wordpress/components'; |
| 20 |
import { createInterpolateElement } from '@wordpress/element'; |
| 21 |
import { __, sprintf } from '@wordpress/i18n'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Internal dependencies. |
| 25 |
*/ |
| 26 |
import { initializeReactComponentInPortal } from '../common/react-loader'; |
| 27 |
import { $ } from '../common/functions'; |
| 28 |
|
| 29 |
const Section = ( { tableMeta, updateTableMeta } ) => { |
| 30 |
const [ tableId, setTableId ] = useState( tableMeta.newId ); |
| 31 |
|
| 32 |
useEffect( () => { |
| 33 |
setTableId( tableMeta.newId ); |
| 34 |
}, [ tableMeta.newId ] ); |
| 35 |
|
| 36 |
return ( |
| 37 |
<VStack |
| 38 |
spacing="16px" |
| 39 |
style={ { |
| 40 |
paddingTop: '6px', |
| 41 |
} } |
| 42 |
> |
| 43 |
<table className="tablepress-postbox-table fixed"> |
| 44 |
<tbody> |
| 45 |
<tr className="bottom-border"> |
| 46 |
<th className="column-1" scope="row"><label htmlFor="table-id">{ __( 'Table ID', 'tablepress' ) }:</label></th> |
| 47 |
<td className="column-2"> |
| 48 |
<HStack> |
| 49 |
<TextControl |
| 50 |
__nextHasNoMarginBottom |
| 51 |
id="table-id" |
| 52 |
title={ __( 'The Table ID can only consist of letters, numbers, hyphens (-), and underscores (_).', 'tablepress' ) } |
| 53 |
pattern="[A-Za-z1-9_\-]|[A-Za-z0-9_\-]{2,}" |
| 54 |
value={ tableId } |
| 55 |
onChange={ ( newTableId ) => setTableId( newTableId.replace( /[^0-9a-zA-Z_-]/g, '' ) ) } |
| 56 |
onBlur={ ( event ) => { |
| 57 |
if ( tableMeta.newId === tableId ) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
// 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,}/. |
| 62 |
if ( '' === tableId || '0' === tableId ) { |
| 63 |
window.alert( __( 'This table ID is invalid. Please enter a different table ID.', 'tablepress' ) ); |
| 64 |
setTableId( tableMeta.newId ); |
| 65 |
event.target.focus(); |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
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' ) ) ) { |
| 70 |
setTableId( tableMeta.newId ); |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
// Set the new table ID. |
| 75 |
updateTableMeta( { newId: tableId } ); |
| 76 |
$( '#table-information-shortcode' ).focus(); |
| 77 |
} } |
| 78 |
required={ true } |
| 79 |
readOnly={ ! tp.screen_options.currentUserCanEditTableId } |
| 80 |
/> |
| 81 |
<label htmlFor="table-information-shortcode"> |
| 82 |
<HStack alignment="left"> |
| 83 |
{ |
| 84 |
createInterpolateElement( |
| 85 |
__( 'Shortcode: <input />', 'tablepress' ), |
| 86 |
{ |
| 87 |
input: ( |
| 88 |
<TextControl |
| 89 |
__nextHasNoMarginBottom |
| 90 |
id="table-information-shortcode" |
| 91 |
value={ `[${ tp.table.shortcode } id=${ tableMeta.newId } /]` } |
| 92 |
onFocus={ ( event ) => event.target.select() } |
| 93 |
readOnly={ true } |
| 94 |
/> |
| 95 |
), |
| 96 |
}, |
| 97 |
) |
| 98 |
} |
| 99 |
</HStack> |
| 100 |
</label> |
| 101 |
</HStack> |
| 102 |
</td> |
| 103 |
</tr> |
| 104 |
<tr className="top-border"> |
| 105 |
<th className="column-1" scope="row"><label htmlFor="table-name">{ __( 'Table Name', 'tablepress' ) }:</label></th> |
| 106 |
<td className="column-2"> |
| 107 |
<TextControl |
| 108 |
__nextHasNoMarginBottom |
| 109 |
id="table-name" |
| 110 |
value={ tableMeta.name } |
| 111 |
onChange={ ( name ) => updateTableMeta( { name } ) } |
| 112 |
/> |
| 113 |
</td> |
| 114 |
</tr> |
| 115 |
<tr className="bottom-border"> |
| 116 |
<th className="column-1 top-align" scope="row"><label htmlFor="table-description">{ __( 'Description', 'tablepress' ) }:</label></th> |
| 117 |
<td className="column-2"> |
| 118 |
<TextareaControl |
| 119 |
__nextHasNoMarginBottom |
| 120 |
id="table-description" |
| 121 |
value={ tableMeta.description } |
| 122 |
onChange={ ( description ) => updateTableMeta( { description } ) } |
| 123 |
rows="4" |
| 124 |
/> |
| 125 |
</td> |
| 126 |
</tr> |
| 127 |
<tr className="top-border"> |
| 128 |
<th className="column-1" scope="row">{ __( 'Last Modified', 'tablepress' ) }:</th> |
| 129 |
<td className="column-2"> |
| 130 |
{ sprintf( __( '%1$s by %2$s', 'tablepress' ), tableMeta.lastModified, tableMeta.lastEditor ) } |
| 131 |
</td> |
| 132 |
</tr> |
| 133 |
</tbody> |
| 134 |
</table> |
| 135 |
</VStack> |
| 136 |
); |
| 137 |
}; |
| 138 |
|
| 139 |
initializeReactComponentInPortal( |
| 140 |
'table-information', |
| 141 |
'edit', |
| 142 |
Section, |
| 143 |
); |
| 144 |
|