PluginProbe
SimpleTOC – Table of Contents Block / 5.0.33
SimpleTOC – Table of Contents Block v5.0.33
7.4.0 7.3.1 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 All 161 releases
simpletoc / src / edit.js

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

337 lines 8.0 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={ __( 'Convert to unordered list', 'simpletoc' ) }
49 describedBy={ __( 'Convert to unordered list', 'simpletoc' ) }
50 isActive={ attributes.use_ol === false }
51 onClick={ () => {
52 setAttributes( { use_ol: false } );
53 } }
54 />
55 <ToolbarButton
56 icon={ formatListNumbered }
57 title={ __( 'Convert to ordered list', 'simpletoc' ) }
58 describedBy={ __( 'Convert to ordered list', 'simpletoc' ) }
59 isActive={ attributes.use_ol === true }
60 onClick={ () => {
61 setAttributes( { use_ol: true } );
62 } }
63 />
64 <ToolbarButton
65 icon={ formatOutdent }
66 title={ __( 'Indent list', 'simpletoc' ) }
67 describedBy={ __( 'Indent list', 'simpletoc' ) }
68 isActive={ attributes.remove_indent === true }
69 onClick={ () => {
70 setAttributes( { remove_indent: true } );
71 } }
72 />
73 <ToolbarButton
74 icon={ formatIndent }
75 title={ __( 'Outdent list', 'simpletoc' ) }
76 describedBy={ __( 'Outdent list', 'simpletoc' ) }
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 checked={ attributes.no_title }
121 onChange={ () =>
122 setAttributes( {
123 no_title: ! attributes.no_title,
124 } )
125 }
126 />
127 </PanelRow>
128 <PanelRow>
129 <SelectControl
130 label={ __( 'Minimum level', 'simpletoc' ) }
131 help={ __(
132 'Minimum depth of the headings.',
133 'simpletoc'
134 ) }
135 value={ attributes.min_level }
136 options={ [
137 {
138 label:
139 __( 'Including', 'simpletoc' ) + ' H6',
140 value: '6',
141 },
142 {
143 label:
144 __( 'Including', 'simpletoc' ) + ' H5',
145 value: '5',
146 },
147 {
148 label:
149 __( 'Including', 'simpletoc' ) + ' H4',
150 value: '4',
151 },
152 {
153 label:
154 __( 'Including', 'simpletoc' ) + ' H3',
155 value: '3',
156 },
157 {
158 label:
159 __( 'Including', 'simpletoc' ) + ' H2',
160 value: '2',
161 },
162 {
163 label:
164 __( 'Including', 'simpletoc' ) +
165 ' H1 (' +
166 __( 'default', 'simpletoc' ) +
167 ')',
168 value: '1',
169 },
170 ] }
171 onChange={ ( level ) =>
172 setAttributes( {
173 min_level: Number( level ),
174 } )
175 }
176 />
177 </PanelRow>
178 <PanelRow>
179 <SelectControl
180 label={ __( 'Maximum level', 'simpletoc' ) }
181 help={ __(
182 'Maximum depth of the headings.',
183 'simpletoc'
184 ) }
185 value={ attributes.max_level }
186 options={ [
187 {
188 label:
189 __( 'Including', 'simpletoc' ) +
190 ' H6 (' +
191 __( 'default', 'simpletoc' ) +
192 ')',
193 value: '6',
194 },
195 {
196 label:
197 __( 'Including', 'simpletoc' ) + ' H5',
198 value: '5',
199 },
200 {
201 label:
202 __( 'Including', 'simpletoc' ) + ' H4',
203 value: '4',
204 },
205 {
206 label:
207 __( 'Including', 'simpletoc' ) + ' H3',
208 value: '3',
209 },
210 {
211 label:
212 __( 'Including', 'simpletoc' ) + ' H2',
213 value: '2',
214 },
215 {
216 label:
217 __( 'Including', 'simpletoc' ) + ' H1',
218 value: '1',
219 },
220 ] }
221 onChange={ ( level ) =>
222 setAttributes( {
223 max_level: Number( level ),
224 } )
225 }
226 />
227 </PanelRow>
228 </PanelBody>
229 </Panel>
230 <Panel>
231 <PanelBody
232 title={ __( 'Advanced Features', 'simpletoc' ) }
233 icon={ advpanelicon }
234 initialOpen={ false }
235 >
236 <PanelRow>
237 <div
238 style={ {
239 marginBottom: '1em',
240 border: '1px solid rgba(0, 0, 0, 0.05)',
241 padding: '0.5em',
242 backgroundColor: '#f7f7f7',
243 } }
244 >
245 <p>
246 <strong>
247 { __(
248 'Think about making a donation if you use any of these features.',
249 'simpletoc'
250 ) }
251 </strong>
252 </p>
253 <ExternalLink href="https://marc.tv/out/donate">
254 { __( 'Donate here!', 'simpletoc' ) }
255 </ExternalLink>
256 </div>
257 </PanelRow>
258 <PanelRow>
259 <ToggleControl
260 label={ __(
261 'Smooth scrolling support',
262 'simpletoc'
263 ) }
264 help={ __(
265 'Add the css class "smooth-scroll" to the links. This enables smooth scrolling in some themes like GeneratePress.',
266 'simpletoc'
267 ) }
268 checked={ attributes.add_smooth }
269 onChange={ () =>
270 setAttributes( {
271 add_smooth: ! attributes.add_smooth,
272 } )
273 }
274 />
275 </PanelRow>
276 <PanelRow>
277 <ToggleControl
278 label={ __( 'Use absolute urls', 'simpletoc' ) }
279 help={ __(
280 'Adds the permalink url to the fragment.',
281 'simpletoc'
282 ) }
283 checked={ attributes.use_absolute_urls }
284 onChange={ () =>
285 setAttributes( {
286 use_absolute_urls:
287 ! attributes.use_absolute_urls,
288 } )
289 }
290 />
291 </PanelRow>
292 <PanelRow>
293 <ToggleControl
294 label={ __(
295 'Automatically refresh TOC',
296 'simpletoc'
297 ) }
298 help={ __(
299 'Disable this to remove redundant changed content warning in editor.',
300 'simpletoc'
301 ) }
302 checked={ attributes.autorefresh }
303 onChange={ () =>
304 setAttributes( {
305 autorefresh: ! attributes.autorefresh,
306 } )
307 }
308 />
309 </PanelRow>
310 </PanelBody>
311 </Panel>
312 </InspectorControls>
313 );
314
315 subscribe( () => {
316 if ( isSavingPost() ) {
317 setSavingProcess( true );
318 } else {
319 setSavingProcess( false );
320 }
321 } );
322
323 useEffect( () => {
324 if ( isSavingProcess ) {
325 updatePost();
326 }
327 }, [ isSavingProcess ] );
328
329 return (
330 <div { ...blockProps }>
331 { controls }
332 { controlssidebar }
333 <ServerSideRender block="simpletoc/toc" attributes={ attributes } />
334 </div>
335 );
336 }
337