| 1 |
import { __ } from '@wordpress/i18n'; |
| 2 |
import { InspectorControls, BlockControls } from "@wordpress/block-editor"; |
| 3 |
import ServerSideRender from "@wordpress/server-side-render"; |
| 4 |
import { |
| 5 |
SelectControl, |
| 6 |
ToolbarGroup, |
| 7 |
ToolbarButton, |
| 8 |
ToggleControl, |
| 9 |
Panel, |
| 10 |
PanelBody, |
| 11 |
PanelRow, |
| 12 |
} from "@wordpress/components"; |
| 13 |
import { useBlockProps } from "@wordpress/block-editor"; |
| 14 |
|
| 15 |
export default function Edit({ attributes, setAttributes }) { |
| 16 |
const blockProps = useBlockProps(); |
| 17 |
|
| 18 |
return ( |
| 19 |
<div {...blockProps}> |
| 20 |
<InspectorControls> |
| 21 |
<Panel> |
| 22 |
<PanelBody> |
| 23 |
<PanelRow> |
| 24 |
<SelectControl |
| 25 |
label={__("Maximum Level", "simpletoc")} |
| 26 |
help={__("Maximum depth of the headings.", "simpletoc")} |
| 27 |
value={attributes.max_level} |
| 28 |
options={[ |
| 29 |
{ |
| 30 |
label: |
| 31 |
__("Including", "simpletoc") + |
| 32 |
" H6 (" + |
| 33 |
__("Show all", "simpletoc") + |
| 34 |
")", |
| 35 |
value: "6", |
| 36 |
}, |
| 37 |
{ |
| 38 |
label: __("Including", "simpletoc") + " H5", |
| 39 |
value: "5", |
| 40 |
}, |
| 41 |
{ |
| 42 |
label: __("Including", "simpletoc") + " H4", |
| 43 |
value: "4", |
| 44 |
}, |
| 45 |
{ |
| 46 |
label: __("Including", "simpletoc") + " H3", |
| 47 |
value: "3", |
| 48 |
}, |
| 49 |
{ |
| 50 |
label: __("Including", "simpletoc") + " H2", |
| 51 |
value: "2", |
| 52 |
}, |
| 53 |
]} |
| 54 |
onChange={(level) => |
| 55 |
setAttributes({ max_level: Number(level) }) |
| 56 |
} |
| 57 |
/> |
| 58 |
</PanelRow> |
| 59 |
<PanelRow> |
| 60 |
<ToggleControl |
| 61 |
label={__("Remove heading", "simpletoc")} |
| 62 |
help={__( |
| 63 |
'Disable the "Table of contents" block heading.', |
| 64 |
"simpletoc" |
| 65 |
)} |
| 66 |
checked={attributes.no_title} |
| 67 |
onChange={() => |
| 68 |
setAttributes({ no_title: !attributes.no_title }) |
| 69 |
} |
| 70 |
/> |
| 71 |
</PanelRow> |
| 72 |
<PanelRow> |
| 73 |
<ToggleControl |
| 74 |
label={__("Use an ordered list", "simpletoc")} |
| 75 |
help={__( |
| 76 |
"Replace the <ul> tag with an <ol> tag. This adds decimal numbers to each heading in the TOC.", |
| 77 |
"simpletoc" |
| 78 |
)} |
| 79 |
checked={attributes.use_ol} |
| 80 |
onChange={() => setAttributes({ use_ol: !attributes.use_ol })} |
| 81 |
/> |
| 82 |
</PanelRow> |
| 83 |
<PanelRow> |
| 84 |
<ToggleControl |
| 85 |
label={__("Remove list indent", "simpletoc")} |
| 86 |
help={__( |
| 87 |
"No bullet points or numbers at the first level.", |
| 88 |
"simpletoc" |
| 89 |
)} |
| 90 |
checked={attributes.remove_indent} |
| 91 |
onChange={() => setAttributes({ remove_indent: !attributes.remove_indent })} |
| 92 |
/> |
| 93 |
</PanelRow> |
| 94 |
<PanelRow> |
| 95 |
<ToggleControl |
| 96 |
label={__("Use absolute urls", "simpletoc")} |
| 97 |
help={__( |
| 98 |
"Adds the permalink url to the fragment.", |
| 99 |
"simpletoc" |
| 100 |
)} |
| 101 |
checked={attributes.use_absolute_urls} |
| 102 |
onChange={() => |
| 103 |
setAttributes({ |
| 104 |
use_absolute_urls: !attributes.use_absolute_urls, |
| 105 |
}) |
| 106 |
} |
| 107 |
/> |
| 108 |
</PanelRow> |
| 109 |
<PanelRow> |
| 110 |
<ToggleControl |
| 111 |
label={__("Smooth scrolling support", "simpletoc")} |
| 112 |
help={__( |
| 113 |
'Add the css class "smooth-scroll" to the links. This enables smooth scrolling in some themes like GeneratePress.', |
| 114 |
"simpletoc" |
| 115 |
)} |
| 116 |
checked={attributes.add_smooth} |
| 117 |
onChange={() => |
| 118 |
setAttributes({ |
| 119 |
add_smooth: !attributes.add_smooth, |
| 120 |
}) |
| 121 |
} |
| 122 |
/> |
| 123 |
</PanelRow> |
| 124 |
</PanelBody> |
| 125 |
</Panel> |
| 126 |
</InspectorControls> |
| 127 |
<BlockControls> |
| 128 |
<ToolbarGroup> |
| 129 |
<ToolbarButton |
| 130 |
className="components-icon-button components-toolbar__control" |
| 131 |
label={__("Update table of contents", "simpletoc")} |
| 132 |
onClick={() => setAttributes({ updated: Date.now() })} |
| 133 |
icon="update" |
| 134 |
/> |
| 135 |
</ToolbarGroup> |
| 136 |
</BlockControls> |
| 137 |
<ServerSideRender block="simpletoc/toc" attributes={attributes} /> |
| 138 |
</div> |
| 139 |
); |
| 140 |
} |
| 141 |
|