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

178 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 { shortcode_attrs_to_string } from './common/functions';
28 import TablePress_Table_Icon 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 ComboboxControl_options = 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 < ComboboxControl_options.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={ TablePress_Table_Icon } /> }
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 < ComboboxControl_options.length
112 ?
113 <ComboboxControl
114 __nextHasNoMarginBottom
115 label={ __( 'Table:', 'tablepress' ) }
116 help={
117 <>
118 { __( 'Select the TablePress table that you want to embed.', 'tablepress' ) }
119 { '' !== tp.url && ' ' }
120 <ManageTablesLink />
121 </>
122 }
123 value={ attributes.id }
124 options={ ComboboxControl_options }
125 onChange={ ( id ) => {
126 id ??= '';
127 setAttributes( { id: id.replace( /[^0-9a-zA-Z-_]/g, '' ) } );
128 } }
129 />
130 :
131 <>
132 { __( 'There are no TablePress tables on this site yet.', 'tablepress' ) }
133 { '' !== tp.url && ' ' }
134 <ManageTablesLink />
135 </>
136 }
137 </PanelBody>
138 </InspectorControls>
139 { attributes.id && tp.tables.hasOwnProperty( attributes.id ) &&
140 <InspectorAdvancedControls>
141 <TextControl
142 __nextHasNoMarginBottom
143 label={ __( 'Configuration parameters:', 'tablepress' ) }
144 help={ __( 'These additional parameters can be used to modify specific table features.', 'tablepress' ) + ' ' + __( 'See the TablePress Documentation for more information.', 'tablepress' ) }
145 value={ attributes.parameters }
146 onChange={ ( parameters ) => {
147 parameters = shortcode.replace(
148 tp.table.shortcode,
149 parameters,
150 ( { attrs: shortcodeAttrs } ) => {
151 shortcodeAttrs = { named: { ...shortcodeAttrs.named }, numeric: [ ...shortcodeAttrs.numeric ] }; // Use object destructuring to get a clone of the object.
152 delete shortcodeAttrs.named.id;
153 return ' ' + shortcode_attrs_to_string( shortcodeAttrs ) + ' '; // Add spaces around replacement text to have separation to possibly already existing parameters.
154 }
155 );
156 parameters = parameters.replace( /=“([^]*)/g, '="$1"' ); // Replace curly quotation marks around a value with normal ones.
157 setAttributes( { parameters } );
158 } }
159 onBlur={ ( event ) => {
160 const parameters = event.target.value.trim(); // Remove leading and trailing whitespace from the parameter string.
161 setAttributes( { parameters } );
162 } }
163 />
164 </InspectorAdvancedControls>
165 }
166 </>
167 );
168
169 return (
170 <>
171 { blockMarkup }
172 { sidebarMarkup }
173 </>
174 );
175 };
176
177 export default TablePressTableEdit;
178