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

173 lines 5.4 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
29 /**
30 * Load CSS code that only applies inside the block editor.
31 */
32 import './editor.scss';
33
34 // Options for the table selection dropdown, in the form [ { value: <id>, label: <text> }, ... ].
35 const ComboboxControl_options = Object.entries( tp.tables ).map( ( [ id, name ] ) => {
36 return {
37 value: id,
38 label: sprintf( __( 'ID %1$s: “%2$s”', 'tablepress' ), id, name ),
39 };
40 } );
41
42 /**
43 * Custom component for the "Manage your tables." link.
44 */
45 const ManageTablesLink = function() {
46 return (
47 '' !== tp.url &&
48 <ExternalLink href={ tp.url }>
49 { __( 'Manage your tables.', 'tablepress' ) }
50 </ExternalLink>
51 );
52 }
53
54 /**
55 * The edit function describes the structure of your block in the context of the
56 * editor. This represents what the editor will render when the block is used.
57 *
58 * @param {Object} params Function parameters.
59 * @param {Object} params.attributes Block attributes.
60 * @param {Function} params.setAttributes Function to set block attributes.
61 * @return {WPElement} Element to render.
62 */
63 export default function TablePressTableEdit( { attributes, setAttributes } ) {
64 const blockProps = useBlockProps();
65
66 let blockMarkup;
67 if ( attributes.id && tp.tables.hasOwnProperty( attributes.id ) ) {
68 blockMarkup = (
69 <div { ...blockProps }>
70 { tp.load_block_preview &&
71 <ServerSideRender
72 block={ block.name }
73 attributes={ attributes }
74 className="render-wrapper"
75 />
76 }
77 <div className="table-overlay">
78 { sprintf( __( 'TablePress table %1$s: “%2$s”', 'tablepress' ), attributes.id, tp.tables[ attributes.id ] ) }
79 </div>
80 </div>
81 );
82 } else {
83 blockMarkup = (
84 <div { ...blockProps }>
85 <Placeholder
86 icon={ <Icon icon="list-view" /> }
87 label={ __( 'TablePress table', 'tablepress' ) }
88 instructions={
89 <>
90 { /* Show an error message if a table could not be found (e.g. after a table was deleted).
91 The tp.tables.hasOwnProperty( attributes.id ) check happens above. */ }
92 { attributes.id && sprintf( __( 'There is a problem: The TablePress table with the ID “%1$s” could not be found.', 'tablepress' ), attributes.id ) + ' ' }
93
94 { 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' ) }
95 </>
96 }
97 >
98 <ManageTablesLink />
99 </Placeholder>
100 </div>
101 );
102 }
103
104 const sidebarMarkup = (
105 <>
106 <InspectorControls>
107 <PanelBody
108 opened={ true }
109 >
110 { 0 < ComboboxControl_options.length
111 ?
112 <ComboboxControl
113 label={ __( 'Table:', 'tablepress' ) }
114 help={
115 <>
116 { __( 'Select the TablePress table that you want to embed.', 'tablepress' ) }
117 { '' !== tp.url && ' ' }
118 <ManageTablesLink />
119 </>
120 }
121 value={ attributes.id }
122 options={ ComboboxControl_options }
123 onChange={ ( id ) => {
124 id ??= '';
125 setAttributes( { id: id.replace( /[^0-9a-zA-Z-_]/g, '' ) } );
126 } }
127 />
128 :
129 <>
130 { __( 'There are no TablePress tables on this site yet.', 'tablepress' ) }
131 { '' !== tp.url && ' ' }
132 <ManageTablesLink />
133 </>
134 }
135 </PanelBody>
136 </InspectorControls>
137 { attributes.id && tp.tables.hasOwnProperty( attributes.id ) &&
138 <InspectorAdvancedControls>
139 <TextControl
140 label={ __( 'Configuration parameters:', 'tablepress' ) }
141 help={ __( 'These additional parameters can be used to modify specific table features.', 'tablepress' ) + ' ' + __( 'See the TablePress Documentation for more information.', 'tablepress' ) }
142 value={ attributes.parameters }
143 onChange={ ( parameters ) => {
144 parameters = shortcode.replace(
145 tp.table.shortcode,
146 parameters,
147 ( { attrs: shortcodeAttrs } ) => {
148 shortcodeAttrs = { named: { ...shortcodeAttrs.named }, numeric: [ ...shortcodeAttrs.numeric ] }; // Use object destructuring to get a clone of the object.
149 delete shortcodeAttrs.named.id;
150 return ' ' + shortcode_attrs_to_string( shortcodeAttrs ) + ' '; // Add spaces around replacement text to have separation to possibly already existing parameters.
151 }
152 );
153 parameters = parameters.replace( /=“([^]*)/g, '="$1"' ); // Replace curly quotation marks around a value with normal ones.
154 setAttributes( { parameters } );
155 } }
156 onBlur={ ( event ) => {
157 const parameters = event.target.value.trim(); // Remove leading and trailing whitespace from the parameter string.
158 setAttributes( { parameters } );
159 } }
160 />
161 </InspectorAdvancedControls>
162 }
163 </>
164 );
165
166 return (
167 <>
168 { blockMarkup }
169 { sidebarMarkup}
170 </>
171 );
172 }
173