PluginProbe
TablePress – Tables in WordPress made easy / 3.1
TablePress – Tables in WordPress made easy v3.1
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 / blocks / table / src / edit.js

edit.js in TablePress – Tables in WordPress made easy 3.1, at blocks/table/src/edit.js

180 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * JavaScript code for the TablePress table block in the block editor.
3 *
4 * @package TablePress
5 * @subpackage Blocks
6 * @author Tobias Bäthge
7 * @since 2.0.0
8 */
9
10 /**
11 * WordPress dependencies.
12 */
13 import { __, sprintf } from '@wordpress/i18n';
14 import ServerSideRender from '@wordpress/server-side-render';
15 import { useBlockProps, InspectorControls, InspectorAdvancedControls } from '@wordpress/block-editor';
16 import { ComboboxControl, ExternalLink, Icon, PanelBody, Placeholder, TextControl } from '@wordpress/components';
17 import shortcode from '@wordpress/shortcode';
18
19 /**
20 * Get the block name from the block.json.
21 */
22 import block from '../block.json';
23
24 /**
25 * Internal dependencies.
26 */
27 import { shortcodeAttrsToString } from './common/functions';
28 import TablePressTableIcon from './icon';
29
30 /**
31 * Load CSS code that only applies inside the block editor.
32 */
33 import './editor.scss';
34
35 // Options for the table selection dropdown, in the form [ { value: <id>, label: <text> }, ... ].
36 const ComboboxControlOptions = Object.entries( tp.tables ).map( ( [ id, name ] ) => {
37 return {
38 value: id,
39 label: sprintf( __( 'ID %1$s: “%2$s”', 'tablepress' ), id, name ),
40 };
41 } );
42
43 /**
44 * Custom component for the "Manage your tables." link.
45 */
46 const ManageTablesLink = function () {
47 return (
48 '' !== tp.url &&
49 <ExternalLink href={ tp.url }>
50 { __( 'Manage your tables.', 'tablepress' ) }
51 </ExternalLink>
52 );
53 }
54
55 /**
56 * The edit function describes the structure of your block in the context of the
57 * editor. This represents what the editor will render when the block is used.
58 *
59 * @param {Object} params Function parameters.
60 * @param {Object} params.attributes Block attributes.
61 * @param {Function} params.setAttributes Function to set block attributes.
62 * @return {Element} Element to render.
63 */
64 const TablePressTableEdit = ( { attributes, setAttributes } ) => {
65 const blockProps = useBlockProps();
66
67 let blockMarkup;
68 if ( attributes.id && tp.tables.hasOwnProperty( attributes.id ) ) {
69 blockMarkup = (
70 <div { ...blockProps }>
71 { tp.load_block_preview &&
72 <ServerSideRender
73 block={ block.name }
74 attributes={ {
75 id: attributes.id,
76 parameters: `block_preview=true ${ attributes.parameters }`.trim(), // Set the `block_preview` parameter to allow detecting that this is a block preview.
77 } }
78 className="render-wrapper"
79 />
80 }
81 <div className="table-overlay">
82 { sprintf( __( 'TablePress table %1$s: “%2$s”', 'tablepress' ), attributes.id, tp.tables[ attributes.id ] ) }
83 </div>
84 </div>
85 );
86 } else {
87 let instructions = 0 < ComboboxControlOptions.length ? __( 'Select the TablePress table that you want to embed in the Settings sidebar.', 'tablepress' ) : __( 'There are no TablePress tables on this site yet.', 'tablepress' );
88 if ( attributes.id ) {
89 // Show an error message if a table could not be found (e.g. after a table was deleted). The tp.tables.hasOwnProperty( attributes.id ) check happens above.
90 instructions = sprintf( __( 'There is a problem: The TablePress table with the ID “%1$s” could not be found.', 'tablepress' ), attributes.id ) + ' ' + instructions;
91 }
92 blockMarkup = (
93 <div { ...blockProps }>
94 <Placeholder
95 icon={ <Icon icon={ TablePressTableIcon } /> }
96 label={ __( 'TablePress table', 'tablepress' ) }
97 instructions={ instructions }
98 >
99 <ManageTablesLink />
100 </Placeholder>
101 </div>
102 );
103 }
104
105 const sidebarMarkup = (
106 <>
107 <InspectorControls>
108 <PanelBody
109 opened={ true }
110 >
111 { 0 < ComboboxControlOptions.length
112 ?
113 <ComboboxControl
114 __nextHasNoMarginBottom
115 __next40pxDefaultSize
116 label={ __( 'Table:', 'tablepress' ) }
117 help={
118 <>
119 { __( 'Select the TablePress table that you want to embed.', 'tablepress' ) }
120 { '' !== tp.url && ' ' }
121 <ManageTablesLink />
122 </>
123 }
124 value={ attributes.id }
125 options={ ComboboxControlOptions }
126 onChange={ ( id ) => {
127 id ??= '';
128 setAttributes( { id: id.replace( /[^0-9a-zA-Z-_]/g, '' ) } );
129 } }
130 />
131 :
132 <>
133 { __( 'There are no TablePress tables on this site yet.', 'tablepress' ) }
134 { '' !== tp.url && ' ' }
135 <ManageTablesLink />
136 </>
137 }
138 </PanelBody>
139 </InspectorControls>
140 { attributes.id && tp.tables.hasOwnProperty( attributes.id ) &&
141 <InspectorAdvancedControls>
142 <TextControl
143 __nextHasNoMarginBottom
144 __next40pxDefaultSize
145 label={ __( 'Configuration parameters:', 'tablepress' ) }
146 help={ __( 'These additional parameters can be used to modify specific table features.', 'tablepress' ) + ' ' + __( 'See the TablePress Documentation for more information.', 'tablepress' ) }
147 value={ attributes.parameters }
148 onChange={ ( parameters ) => {
149 parameters = shortcode.replace(
150 tp.table.shortcode,
151 parameters,
152 ( { attrs: shortcodeAttrs } ) => {
153 shortcodeAttrs = { named: { ...shortcodeAttrs.named }, numeric: [ ...shortcodeAttrs.numeric ] }; // Use object destructuring to get a clone of the object.
154 delete shortcodeAttrs.named.id;
155 return ' ' + shortcodeAttrsToString( shortcodeAttrs ) + ' '; // Add spaces around replacement text to have separation to possibly already existing parameters.
156 }
157 );
158 parameters = parameters.replace( /=“([^]*)/g, '="$1"' ); // Replace curly quotation marks around a value with normal ones.
159 setAttributes( { parameters } );
160 } }
161 onBlur={ ( event ) => {
162 const parameters = event.target.value.trim(); // Remove leading and trailing whitespace from the parameter string.
163 setAttributes( { parameters } );
164 } }
165 />
166 </InspectorAdvancedControls>
167 }
168 </>
169 );
170
171 return (
172 <>
173 { blockMarkup }
174 { sidebarMarkup }
175 </>
176 );
177 };
178
179 export default TablePressTableEdit;
180