| 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 |
Button, |
| 16 |
__experimentalHStack as HStack, // eslint-disable-line @wordpress/no-unsafe-wp-apis |
| 17 |
Icon, |
| 18 |
__experimentalInputControl as InputControl, // eslint-disable-line @wordpress/no-unsafe-wp-apis |
| 19 |
__experimentalInputControlSuffixWrapper as InputControlSuffixWrapper, // eslint-disable-line @wordpress/no-unsafe-wp-apis |
| 20 |
TextareaControl, |
| 21 |
TextControl, |
| 22 |
__experimentalVStack as VStack, // eslint-disable-line @wordpress/no-unsafe-wp-apis |
| 23 |
} from '@wordpress/components'; |
| 24 |
import { useCopyToClipboard } from '@wordpress/compose'; |
| 25 |
import { useDispatch } from '@wordpress/data'; |
| 26 |
import { createInterpolateElement } from '@wordpress/element'; |
| 27 |
import { copySmall } from '@wordpress/icons'; |
| 28 |
import { __, sprintf } from '@wordpress/i18n'; |
| 29 |
import { store as noticesStore } from '@wordpress/notices'; |
| 30 |
|
| 31 |
/** |
| 32 |
* Internal dependencies. |
| 33 |
*/ |
| 34 |
import { initializeReactComponentInPortal } from '../common/react-loader'; |
| 35 |
import { TablePressIconSimple } from '../../img/tablepress-icon'; |
| 36 |
|
| 37 |
const Section = ( { tableMeta, updateTableMeta } ) => { |
| 38 |
const { createSuccessNotice } = useDispatch( noticesStore ); |
| 39 |
const [ tableId, setTableId ] = useState( tableMeta.id ); |
| 40 |
|
| 41 |
const copyShortcodeButtonRef = useCopyToClipboard( `[${ tp.table.shortcode } id=${ tableMeta.id } /]`, () => { |
| 42 |
createSuccessNotice( |
| 43 |
__( 'Copied Shortcode to clipboard.', 'tablepress' ), |
| 44 |
{ |
| 45 |
type: 'snackbar', |
| 46 |
icon: <Icon icon={ TablePressIconSimple } />, |
| 47 |
} |
| 48 |
); |
| 49 |
} ); |
| 50 |
|
| 51 |
useEffect( () => { |
| 52 |
setTableId( tableMeta.id ); |
| 53 |
}, [ tableMeta.id ] ); |
| 54 |
|
| 55 |
return ( |
| 56 |
<VStack |
| 57 |
spacing="16px" |
| 58 |
style={ { |
| 59 |
paddingTop: '6px', |
| 60 |
} } |
| 61 |
> |
| 62 |
<table className="tablepress-postbox-table fixed"> |
| 63 |
<tbody> |
| 64 |
<tr className="bottom-border"> |
| 65 |
<th className="column-1" scope="row"><label htmlFor="table-id">{ __( 'Table ID', 'tablepress' ) }:</label></th> |
| 66 |
<td className="column-2"> |
| 67 |
<HStack> |
| 68 |
<TextControl |
| 69 |
__nextHasNoMarginBottom |
| 70 |
__next40pxDefaultSize |
| 71 |
id="table-id" |
| 72 |
title={ __( 'The Table ID can only consist of letters, numbers, hyphens (-), and underscores (_).', 'tablepress' ) } |
| 73 |
pattern="[A-Za-z1-9_\-]|[A-Za-z0-9_\-]{2,}" |
| 74 |
value={ tableId } |
| 75 |
onChange={ ( newTableId ) => setTableId( newTableId.replace( /[^0-9a-zA-Z_-]/g, '' ) ) } |
| 76 |
onBlur={ ( event ) => { |
| 77 |
if ( tableMeta.id === tableId ) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
// 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,}/. |
| 82 |
if ( '' === tableId || '0' === tableId ) { |
| 83 |
// This alert can not be replaced by the `Alert` component, as that does not stop the cmd+S keyboard shortcut from running. |
| 84 |
window.alert( __( 'This table ID is invalid. Please enter a different table ID.', 'tablepress' ) ); |
| 85 |
setTableId( tableMeta.id ); |
| 86 |
event.target.focus(); |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
// This alert can not be replaced by a `Modal` component, as that does not stop the cmd+S keyboard shortcut from running. |
| 91 |
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' ) ) ) { |
| 92 |
setTableId( tableMeta.id ); |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
// Set the new table ID. |
| 97 |
updateTableMeta( { id: tableId } ); |
| 98 |
document.getElementById( 'table-information-shortcode' ).focus(); |
| 99 |
} } |
| 100 |
required={ true } |
| 101 |
readOnly={ ! tp.screenOptions.currentUserCanEditTableId } |
| 102 |
/> |
| 103 |
<label htmlFor="table-information-shortcode"> |
| 104 |
<HStack alignment="left"> |
| 105 |
{ |
| 106 |
createInterpolateElement( |
| 107 |
__( 'Shortcode: <input />', 'tablepress' ), |
| 108 |
{ |
| 109 |
input: ( |
| 110 |
<InputControl |
| 111 |
__next40pxDefaultSize |
| 112 |
type="text" |
| 113 |
id="table-information-shortcode" |
| 114 |
value={ `[${ tp.table.shortcode } id=${ tableMeta.id } /]` } |
| 115 |
onFocus={ ( event ) => event.target.select() } |
| 116 |
readOnly={ true } |
| 117 |
suffix={ |
| 118 |
<InputControlSuffixWrapper variant="control"> |
| 119 |
<Button |
| 120 |
icon={ copySmall } |
| 121 |
ref={ copyShortcodeButtonRef } |
| 122 |
size="small" |
| 123 |
label={ __( 'Copy Shortcode to clipboard', 'tablepress' ) } |
| 124 |
/> |
| 125 |
</InputControlSuffixWrapper> |
| 126 |
} |
| 127 |
/> |
| 128 |
), |
| 129 |
}, |
| 130 |
) |
| 131 |
} |
| 132 |
</HStack> |
| 133 |
</label> |
| 134 |
</HStack> |
| 135 |
</td> |
| 136 |
</tr> |
| 137 |
<tr className="top-border"> |
| 138 |
<th className="column-1" scope="row"><label htmlFor="table-name">{ __( 'Table Name', 'tablepress' ) }:</label></th> |
| 139 |
<td className="column-2"> |
| 140 |
<TextControl |
| 141 |
__nextHasNoMarginBottom |
| 142 |
__next40pxDefaultSize |
| 143 |
id="table-name" |
| 144 |
value={ tableMeta.name } |
| 145 |
onChange={ ( name ) => updateTableMeta( { name } ) } |
| 146 |
/> |
| 147 |
</td> |
| 148 |
</tr> |
| 149 |
<tr className="bottom-border"> |
| 150 |
<th className="column-1 top-align" scope="row"><label htmlFor="table-description">{ __( 'Description', 'tablepress' ) }:</label></th> |
| 151 |
<td className="column-2"> |
| 152 |
<TextareaControl |
| 153 |
__nextHasNoMarginBottom |
| 154 |
id="table-description" |
| 155 |
value={ tableMeta.description } |
| 156 |
onChange={ ( description ) => updateTableMeta( { description } ) } |
| 157 |
rows="4" |
| 158 |
/> |
| 159 |
</td> |
| 160 |
</tr> |
| 161 |
<tr className="top-border"> |
| 162 |
<th className="column-1" scope="row">{ __( 'Last Modified', 'tablepress' ) }:</th> |
| 163 |
<td className="column-2"> |
| 164 |
{ sprintf( __( '%1$s by %2$s', 'tablepress' ), tableMeta.lastModified, tableMeta.lastEditor ) } |
| 165 |
</td> |
| 166 |
</tr> |
| 167 |
</tbody> |
| 168 |
</table> |
| 169 |
</VStack> |
| 170 |
); |
| 171 |
}; |
| 172 |
|
| 173 |
initializeReactComponentInPortal( |
| 174 |
'table-information', |
| 175 |
'edit', |
| 176 |
Section, |
| 177 |
); |
| 178 |
|