PluginProbe
SimpleTOC – Table of Contents Block / 5.0.32
SimpleTOC – Table of Contents Block v5.0.32
7.2.0 7.3.0 7.1.1 7.1.0 7.0.10 7.0.9 7.0.8 7.0.7 7.0.6 7.0.4 7.0.5 5.0.21.1 5.0.22 5.0.23 5.0.24 5.0.25 5.0.25.1 5.0.27 5.0.28 5.0.29 5.0.3 5.0.30 5.0.31 5.0.32 5.0.33 All 159 releases
simpletoc / src / edit.js

edit.js in SimpleTOC – Table of Contents Block 5.0.32, at src/edit.js

341 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { __ } from '@wordpress/i18n';
2 import {
3 InspectorControls,
4 BlockControls,
5 useBlockProps,
6 } from '@wordpress/block-editor';
7 import ServerSideRender from '@wordpress/server-side-render';
8 import {
9 formatListBullets,
10 formatOutdent,
11 formatIndent,
12 formatListNumbered,
13 } from '@wordpress/icons';
14 import {
15 SelectControl,
16 ToolbarButton,
17 ToggleControl,
18 TextControl,
19 Panel,
20 PanelBody,
21 PanelRow,
22 ExternalLink,
23 } from '@wordpress/components';
24 import { select, subscribe } from '@wordpress/data';
25 import { useEffect, useState } from 'react';
26
27 export default function Edit( { attributes, setAttributes } ) {
28 const blockProps = useBlockProps();
29 /* Update SimpleTOC if the post is saved successfully. */
30 /* Source: https://github.com/WordPress/gutenberg/issues/17632 */
31
32 const { isSavingPost } = select( 'core/editor' );
33 const [ isSavingProcess, setSavingProcess ] = useState( false );
34 const advpanelicon = 'settings';
35
36 const updatePost = function () {
37 if ( attributes.autorefresh === true ) {
38 /* refresh block with changed attribute */
39 /* There is no better way at the moment https://github.com/WordPress/gutenberg/issues/44469 */
40 setAttributes( { updated: new Date().getTime() } );
41 }
42 };
43
44 const controls = (
45 <BlockControls group="block">
46 <ToolbarButton
47 icon={ formatListBullets }
48 title={ __( 'Unordered' ) }
49 describedBy={ __( 'Convert to unordered list' ) }
50 isActive={ attributes.use_ol === false }
51 onClick={ () => {
52 setAttributes( { use_ol: false } );
53 } }
54 />
55 <ToolbarButton
56 icon={ formatListNumbered }
57 title={ __( 'Ordered' ) }
58 describedBy={ __( 'Convert to ordered list' ) }
59 isActive={ attributes.use_ol === true }
60 onClick={ () => {
61 setAttributes( { use_ol: true } );
62 } }
63 />
64 <ToolbarButton
65 icon={ formatOutdent }
66 title={ __( 'Indent' ) }
67 describedBy={ __( 'Indent list' ) }
68 isActive={ attributes.remove_indent === true }
69 onClick={ () => {
70 setAttributes( { remove_indent: true } );
71 } }
72 />
73 <ToolbarButton
74 icon={ formatIndent }
75 title={ __( 'Outdent' ) }
76 describedBy={ __( 'Outdent list' ) }
77 isActive={ attributes.remove_indent === false }
78 onClick={ () => {
79 setAttributes( { remove_indent: false } );
80 } }
81 />
82 </BlockControls>
83 );
84
85 const controlssidebar = (
86 <InspectorControls>
87 <Panel>
88 <PanelBody>
89 { ! attributes.no_title && (
90 <PanelRow>
91 <TextControl
92 label={ __( 'Heading Text', 'simpletoc' ) }
93 help={
94 __(
95 'Set the heading text of the block.',
96 'simpletoc'
97 ) +
98 ' ' +
99 __( 'Default value', 'simpletoc' ) +
100 ': ' +
101 __( 'Table of Contents', 'simpletoc' )
102 }
103 value={ attributes.title_text }
104 onChange={ ( value ) =>
105 setAttributes( {
106 title_text:
107 value ||
108 __(
109 'Table of Contents',
110 'simpletoc'
111 ),
112 } )
113 }
114 />
115 </PanelRow>
116 ) }
117 <PanelRow>
118 <ToggleControl
119 label={ __( 'Remove heading', 'simpletoc' ) }
120 help={ __(
121 'Disable the "Table of contents" block heading.',
122 'simpletoc'
123 ) }
124 checked={ attributes.no_title }
125 onChange={ () =>
126 setAttributes( {
127 no_title: ! attributes.no_title,
128 } )
129 }
130 />
131 </PanelRow>
132 <PanelRow>
133 <SelectControl
134 label={ __( 'Minimum level', 'simpletoc' ) }
135 help={ __(
136 'Minimum depth of the headings.',
137 'simpletoc'
138 ) }
139 value={ attributes.min_level }
140 options={ [
141 {
142 label:
143 __( 'Including', 'simpletoc' ) + ' H6',
144 value: '6',
145 },
146 {
147 label:
148 __( 'Including', 'simpletoc' ) + ' H5',
149 value: '5',
150 },
151 {
152 label:
153 __( 'Including', 'simpletoc' ) + ' H4',
154 value: '4',
155 },
156 {
157 label:
158 __( 'Including', 'simpletoc' ) + ' H3',
159 value: '3',
160 },
161 {
162 label:
163 __( 'Including', 'simpletoc' ) + ' H2',
164 value: '2',
165 },
166 {
167 label:
168 __( 'Including', 'simpletoc' ) +
169 ' H1 (' +
170 __( 'default', 'simpletoc' ) +
171 ')',
172 value: '1',
173 },
174 ] }
175 onChange={ ( level ) =>
176 setAttributes( {
177 min_level: Number( level ),
178 } )
179 }
180 />
181 </PanelRow>
182 <PanelRow>
183 <SelectControl
184 label={ __( 'Maximum level', 'simpletoc' ) }
185 help={ __(
186 'Maximum depth of the headings.',
187 'simpletoc'
188 ) }
189 value={ attributes.max_level }
190 options={ [
191 {
192 label:
193 __( 'Including', 'simpletoc' ) +
194 ' H6 (' +
195 __( 'default', 'simpletoc' ) +
196 ')',
197 value: '6',
198 },
199 {
200 label:
201 __( 'Including', 'simpletoc' ) + ' H5',
202 value: '5',
203 },
204 {
205 label:
206 __( 'Including', 'simpletoc' ) + ' H4',
207 value: '4',
208 },
209 {
210 label:
211 __( 'Including', 'simpletoc' ) + ' H3',
212 value: '3',
213 },
214 {
215 label:
216 __( 'Including', 'simpletoc' ) + ' H2',
217 value: '2',
218 },
219 {
220 label:
221 __( 'Including', 'simpletoc' ) + ' H1',
222 value: '1',
223 },
224 ] }
225 onChange={ ( level ) =>
226 setAttributes( {
227 max_level: Number( level ),
228 } )
229 }
230 />
231 </PanelRow>
232 </PanelBody>
233 </Panel>
234 <Panel>
235 <PanelBody
236 title={ __( 'Advanced Features', 'simpletoc' ) }
237 icon={ advpanelicon }
238 initialOpen={ false }
239 >
240 <PanelRow>
241 <div
242 style={ {
243 marginBottom: '1em',
244 border: '1px solid rgba(0, 0, 0, 0.05)',
245 padding: '0.5em',
246 backgroundColor: '#f7f7f7',
247 } }
248 >
249 <p>
250 <strong>
251 { __(
252 'Think about making a donation if you use any of these features.',
253 'simpletoc'
254 ) }
255 </strong>
256 </p>
257 <ExternalLink href="https://marc.tv/out/donate">
258 { __( 'Donate here!', 'simpletoc' ) }
259 </ExternalLink>
260 </div>
261 </PanelRow>
262 <PanelRow>
263 <ToggleControl
264 label={ __(
265 'Smooth scrolling support',
266 'simpletoc'
267 ) }
268 help={ __(
269 'Add the css class "smooth-scroll" to the links. This enables smooth scrolling in some themes like GeneratePress.',
270 'simpletoc'
271 ) }
272 checked={ attributes.add_smooth }
273 onChange={ () =>
274 setAttributes( {
275 add_smooth: ! attributes.add_smooth,
276 } )
277 }
278 />
279 </PanelRow>
280 <PanelRow>
281 <ToggleControl
282 label={ __( 'Use absolute urls', 'simpletoc' ) }
283 help={ __(
284 'Adds the permalink url to the fragment.',
285 'simpletoc'
286 ) }
287 checked={ attributes.use_absolute_urls }
288 onChange={ () =>
289 setAttributes( {
290 use_absolute_urls:
291 ! attributes.use_absolute_urls,
292 } )
293 }
294 />
295 </PanelRow>
296 <PanelRow>
297 <ToggleControl
298 label={ __(
299 'Automatically refresh TOC',
300 'simpletoc'
301 ) }
302 help={ __(
303 'Disable this to remove redundant changed content warning in editor.',
304 'simpletoc'
305 ) }
306 checked={ attributes.autorefresh }
307 onChange={ () =>
308 setAttributes( {
309 autorefresh: ! attributes.autorefresh,
310 } )
311 }
312 />
313 </PanelRow>
314 </PanelBody>
315 </Panel>
316 </InspectorControls>
317 );
318
319 subscribe( () => {
320 if ( isSavingPost() ) {
321 setSavingProcess( true );
322 } else {
323 setSavingProcess( false );
324 }
325 } );
326
327 useEffect( () => {
328 if ( isSavingProcess ) {
329 updatePost();
330 }
331 }, [ isSavingProcess ] );
332
333 return (
334 <div { ...blockProps }>
335 { controls }
336 { controlssidebar }
337 <ServerSideRender block="simpletoc/toc" attributes={ attributes } />
338 </div>
339 );
340 }
341