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