PluginProbe
SimpleTOC – Table of Contents Block / 5.0.21.1
SimpleTOC – Table of Contents Block v5.0.21.1
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.21.1, at src/edit.js

202 lines 6.6 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 { 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 import { select, subscribe } from '@wordpress/data';
15 import {useEffect, useState} from 'react';
16
17
18 export default function Edit({ attributes, setAttributes }) {
19 const blockProps = useBlockProps();
20
21 /* Update SimpleTOC if the post is saved successfully. */
22 /* Source: https://github.com/WordPress/gutenberg/issues/17632 */
23
24 const { isSavingPost } = select( 'core/editor' );
25 const [isSavingProcess, setSavingProcess] = useState(false);
26
27 subscribe(() => {
28 if (isSavingPost()) {
29 setSavingProcess(true);
30 } else {
31 setSavingProcess(false);
32 }
33 });
34 const updatePost = function () {
35 setAttributes({ updated: Date.now() });
36 };
37
38 useEffect(() => {
39 if (isSavingProcess) {
40 updatePost();
41 }
42 }, [isSavingProcess]);
43
44 return (
45 <div {...blockProps}>
46 <InspectorControls>
47 <Panel>
48 <PanelBody>
49 <PanelRow>
50 <SelectControl
51 label={__("Maximum level", "simpletoc")}
52 help={__("Maximum depth of the headings.", "simpletoc")}
53 value={attributes.max_level}
54 options={[
55 {
56 label:
57 __("Including", "simpletoc") +
58 " H6 (" +
59 __("default", "simpletoc") +
60 ")",
61 value: "6",
62 },
63 {
64 label: __("Including", "simpletoc") + " H5",
65 value: "5",
66 },
67 {
68 label: __("Including", "simpletoc") + " H4",
69 value: "4",
70 },
71 {
72 label: __("Including", "simpletoc") + " H3",
73 value: "3",
74 },
75 {
76 label: __("Including", "simpletoc") + " H2",
77 value: "2",
78 },
79 ]}
80 onChange={(level) =>
81 setAttributes({ max_level: Number(level) })
82 }
83 />
84 </PanelRow>
85 <PanelRow>
86 <SelectControl
87 label={__("Minimum level", "simpletoc")}
88 help={__("Minimum depth of the headings.", "simpletoc")}
89 value={attributes.min_level}
90 options={[
91 {
92 label: __("Including", "simpletoc") + " H6",
93 value: "6",
94 },
95 {
96 label: __("Including", "simpletoc") + " H5",
97 value: "5",
98 },
99 {
100 label: __("Including", "simpletoc") + " H4",
101 value: "4",
102 },
103 {
104 label: __("Including", "simpletoc") + " H3",
105 value: "3",
106 },
107 {
108 label: __("Including", "simpletoc") +
109 " H2 (" +
110 __("default", "simpletoc") +
111 ")",
112 value: "2",
113 },
114 ]}
115 onChange={(level) =>
116 setAttributes({ min_level: Number(level) })
117 }
118 />
119 </PanelRow>
120 <PanelRow>
121 <ToggleControl
122 label={__("Remove heading", "simpletoc")}
123 help={__(
124 'Disable the "Table of contents" block heading and add your own heading block.',
125 "simpletoc"
126 )}
127 checked={attributes.no_title}
128 onChange={() =>
129 setAttributes({ no_title: !attributes.no_title })
130 }
131 />
132 </PanelRow>
133 <PanelRow>
134 <ToggleControl
135 label={__("Use an ordered list", "simpletoc")}
136 help={__(
137 "Replace the <ul> tag with an <ol> tag. This adds decimal numbers to each heading in the TOC.",
138 "simpletoc"
139 )}
140 checked={attributes.use_ol}
141 onChange={() => setAttributes({ use_ol: !attributes.use_ol })}
142 />
143 </PanelRow>
144 <PanelRow>
145 <ToggleControl
146 label={__("Remove list indent", "simpletoc")}
147 help={__(
148 "No bullet points or numbers at the first level.",
149 "simpletoc"
150 )}
151 checked={attributes.remove_indent}
152 onChange={() => setAttributes({ remove_indent: !attributes.remove_indent })}
153 />
154 </PanelRow>
155 <PanelRow>
156 <ToggleControl
157 label={__("Use absolute urls", "simpletoc")}
158 help={__(
159 "Adds the permalink url to the fragment.",
160 "simpletoc"
161 )}
162 checked={attributes.use_absolute_urls}
163 onChange={() =>
164 setAttributes({
165 use_absolute_urls: !attributes.use_absolute_urls,
166 })
167 }
168 />
169 </PanelRow>
170 <PanelRow>
171 <ToggleControl
172 label={__("Smooth scrolling support", "simpletoc")}
173 help={__(
174 'Add the css class "smooth-scroll" to the links. This enables smooth scrolling in some themes like GeneratePress.',
175 "simpletoc"
176 )}
177 checked={attributes.add_smooth}
178 onChange={() =>
179 setAttributes({
180 add_smooth: !attributes.add_smooth,
181 })
182 }
183 />
184 </PanelRow>
185 </PanelBody>
186 </Panel>
187 </InspectorControls>
188 <BlockControls>
189 <ToolbarGroup>
190 <ToolbarButton
191 className="components-icon-button components-toolbar__control"
192 label={__("Update table of contents", "simpletoc")}
193 onClick={() => setAttributes({ updated: Date.now() })}
194 icon="update"
195 />
196 </ToolbarGroup>
197 </BlockControls>
198 <ServerSideRender block="simpletoc/toc" attributes={attributes} />
199 </div>
200 );
201 }
202