| 1 |
/** |
| 2 |
* JavaScript code for the "Edit Screen" component. |
| 3 |
* |
| 4 |
* @package TablePress |
| 5 |
* @subpackage Edit Screen |
| 6 |
* @author Tobias Bäthge |
| 7 |
* @since 3.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
/* globals tp */ |
| 11 |
|
| 12 |
/** |
| 13 |
* WordPress dependencies. |
| 14 |
*/ |
| 15 |
import { useEffect, useState } from 'react'; |
| 16 |
import { |
| 17 |
withFilters, |
| 18 |
} from '@wordpress/components'; |
| 19 |
import { addAction, applyFilters, removeAction } from '@wordpress/hooks'; |
| 20 |
|
| 21 |
/* |
| 22 |
* Allow other scripts to register their UI components to be rendered on the Edit Screen. |
| 23 |
* Portals allow to render components outside of the normal React tree, in separate DOM nodes. |
| 24 |
*/ |
| 25 |
const features = applyFilters( 'tablepress.editScreenFeatures', [] ); |
| 26 |
const Portals = withFilters( 'tablepress.editScreenPortals' )( () => <></> ); |
| 27 |
|
| 28 |
/** |
| 29 |
* Returns the "Edit Screen" component's JSX markup. |
| 30 |
* |
| 31 |
* @return {Object} Edit Screen component. |
| 32 |
*/ |
| 33 |
const Screen = () => { |
| 34 |
const [ tableOptions, setTableOptions ] = useState( () => ( { ...tp.table.options } ) ); |
| 35 |
const [ tableMeta, setTableMeta ] = useState( () => ( { ...tp.table.meta } ) ); |
| 36 |
|
| 37 |
// When the component is first rendered, register the action hook that is triggered when other options are changed. |
| 38 |
useEffect( () => { |
| 39 |
addAction( 'tablepress.metaUpdated', 'tp/edit-screen/handle-meta-updated', () => { |
| 40 |
setTableMeta( { ...tp.table.meta } ); |
| 41 |
} ); |
| 42 |
|
| 43 |
return () => { |
| 44 |
removeAction( 'tablepress.metaUpdated', 'tp/edit-screen/handle-meta-updated' ); |
| 45 |
}; |
| 46 |
}, [] ); |
| 47 |
|
| 48 |
// Turn off "Enable Visitor Features" if the table has merged cells. |
| 49 |
useEffect( () => { |
| 50 |
if ( tableOptions.use_datatables && tp.helpers.editor.has_merged_body_cells() ) { |
| 51 |
updateTableOptions( { use_datatables: false } ); |
| 52 |
} |
| 53 |
}, [] ); // eslint-disable-line react-hooks/exhaustive-deps -- This should only run on the initial render, so no dependencies are needed. |
| 54 |
|
| 55 |
const updateTableOptions = ( updatedTableOptions ) => { |
| 56 |
// Use an updater function to ensure that the current state is used when updating table options. |
| 57 |
setTableOptions( ( currentTableOptions ) => ( { |
| 58 |
...currentTableOptions, |
| 59 |
...updatedTableOptions, |
| 60 |
} ) ); |
| 61 |
|
| 62 |
tp.table.options = { ...tp.table.options, ...updatedTableOptions }; |
| 63 |
tp.helpers.unsaved_changes.set(); |
| 64 |
|
| 65 |
// Redraw the table when certain options are changed. |
| 66 |
if ( [ 'table_head', 'table_foot' ].some( ( optionName ) => ( Object.keys( updatedTableOptions ).includes( optionName ) ) ) ) { |
| 67 |
tp.editor.updateTable(); |
| 68 |
} |
| 69 |
}; |
| 70 |
|
| 71 |
const updateTableMeta = ( updatedTableMeta ) => tp.helpers.meta.update( updatedTableMeta ); |
| 72 |
|
| 73 |
return ( |
| 74 |
<Portals |
| 75 |
tableMeta={ tableMeta } |
| 76 |
updateTableMeta={ updateTableMeta } |
| 77 |
tableOptions={ tableOptions } |
| 78 |
updateTableOptions={ updateTableOptions } |
| 79 |
features={ features } |
| 80 |
/> |
| 81 |
); |
| 82 |
}; |
| 83 |
|
| 84 |
export default Screen; |
| 85 |
|