PluginProbe
TablePress – Tables in WordPress made easy / 3.3
TablePress – Tables in WordPress made easy v3.3
3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 2.4.4 All 44 releases
tablepress / admin / js / edit / screen.jsx

screen.jsx in TablePress – Tables in WordPress made easy 3.3, at admin/js/edit/screen.jsx

104 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 /**
11 * WordPress dependencies.
12 */
13 import { useEffect, useState } from 'react';
14 import {
15 withFilters,
16 } from '@wordpress/components';
17 import { applyFilters } from '@wordpress/hooks';
18
19 /*
20 * Allow other scripts to register their UI components to be rendered on the Edit Screen.
21 * Portals allow to render components outside of the normal React tree, in separate DOM nodes.
22 */
23 const features = applyFilters( 'tablepress.editScreenFeatures', [] );
24 const Portals = withFilters( 'tablepress.editScreenPortals' )( () => <></> );
25
26 /**
27 * Returns the "Edit Screen" component's JSX markup.
28 *
29 * @return {Object} Edit Screen component.
30 */
31 const Screen = () => {
32 const [ screenData, setScreenData ] = useState( {
33 copyUrl: tp.screenOptions.copyUrl,
34 deleteUrl: tp.screenOptions.deleteUrl,
35 exportUrl: tp.screenOptions.exportUrl,
36 isSaving: false,
37 previewIsLoading: false,
38 previewIsOpen: false,
39 previewSrcDoc: '',
40 quickNavigationDropdownIsOpen: false,
41 previewUrl: tp.screenOptions.previewUrl,
42 triggerPreview: false,
43 triggerSaveChanges: false,
44 } );
45 const [ tableOptions, setTableOptions ] = useState( () => ( { ...tp.table.options } ) );
46 const [ tableMeta, setTableMeta ] = useState( () => ( { ...tp.table.meta } ) );
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 updateScreenData = ( updatedScreenData ) => {
56 // Use an updater function to ensure that the current state is used when updating screen data.
57 setScreenData( ( currentScreenData ) => ( {
58 ...currentScreenData,
59 ...updatedScreenData,
60 } ) );
61 };
62
63 const updateTableOptions = ( updatedTableOptions ) => {
64 // Use an updater function to ensure that the current state is used when updating table options.
65 setTableOptions( ( currentTableOptions ) => ( {
66 ...currentTableOptions,
67 ...updatedTableOptions,
68 } ) );
69
70 tp.table.options = { ...tp.table.options, ...updatedTableOptions };
71 tp.helpers.unsaved_changes.set();
72
73 // Redraw the table when certain options are changed.
74 if ( [ 'table_head', 'table_foot' ].some( ( optionName ) => ( Object.keys( updatedTableOptions ).includes( optionName ) ) ) ) {
75 tp.editor.updateTable();
76 }
77 };
78
79 const updateTableMeta = ( updatedTableMeta ) => {
80 // Use an updater function to ensure that the current state is used when updating table meta.
81 setTableMeta( ( currentTableMeta ) => ( {
82 ...currentTableMeta,
83 ...updatedTableMeta,
84 } ) );
85
86 tp.table.meta = { ...tp.table.meta, ...updatedTableMeta };
87 tp.helpers.unsaved_changes.set();
88 };
89
90 return (
91 <Portals
92 screenData={ screenData }
93 updateScreenData={ updateScreenData }
94 tableMeta={ tableMeta }
95 updateTableMeta={ updateTableMeta }
96 tableOptions={ tableOptions }
97 updateTableOptions={ updateTableOptions }
98 features={ features }
99 />
100 );
101 };
102
103 export default Screen;
104