| 1 |
import { __ } from '@wordpress/i18n'; |
| 2 |
import { InspectorControls } from '@wordpress/block-editor'; |
| 3 |
import { |
| 4 |
BaseControl, |
| 5 |
ButtonGroup, |
| 6 |
Button, |
| 7 |
PanelBody, |
| 8 |
TabPanel, |
| 9 |
ToggleControl, |
| 10 |
SelectControl, |
| 11 |
TextControl, |
| 12 |
RangeControl, |
| 13 |
} from '@wordpress/components'; |
| 14 |
|
| 15 |
import CodeMirrorControl from '../../code-snippet/src/components/CodeMirrorControl'; |
| 16 |
|
| 17 |
import { |
| 18 |
THEME_OPTIONS, |
| 19 |
CODE_SNIPPET_WRAPPER_MARGIN, |
| 20 |
CODE_SNIPPET_WRAPPER_PADDING, |
| 21 |
CODE_SNIPPET_HEADER_PADDING, |
| 22 |
CODE_SNIPPET_LINE_NUMBERS_PADDING, |
| 23 |
CODE_SNIPPET_CODE_PADDING, |
| 24 |
} from '../../code-snippet/src/constants'; |
| 25 |
|
| 26 |
import ResponsiveDimensionsControl from '../../../util/dimensions-control-v2'; |
| 27 |
import ColorControl from '../../../util/color-control'; |
| 28 |
import objAttributes from './attributes'; |
| 29 |
|
| 30 |
// Response bodies default to JSON but any of these are selectable per tab. |
| 31 |
const RESPONSE_LANGUAGE_OPTIONS = [ |
| 32 |
{ label: __('JSON', 'betterdocs'), value: 'json' }, |
| 33 |
{ label: __('XML', 'betterdocs'), value: 'xml' }, |
| 34 |
{ label: __('YAML', 'betterdocs'), value: 'yaml' }, |
| 35 |
{ label: __('JavaScript', 'betterdocs'), value: 'javascript' }, |
| 36 |
{ label: __('PHP', 'betterdocs'), value: 'php' }, |
| 37 |
{ label: __('Python', 'betterdocs'), value: 'python' }, |
| 38 |
{ label: __('Bash', 'betterdocs'), value: 'bash' }, |
| 39 |
{ label: __('HTML', 'betterdocs'), value: 'html' }, |
| 40 |
{ label: __('Plain Text', 'betterdocs'), value: 'text' }, |
| 41 |
]; |
| 42 |
|
| 43 |
const Inspector = ({ attributes, setAttributes }) => { |
| 44 |
const { |
| 45 |
responses = [], |
| 46 |
showCopyButton, |
| 47 |
showHeader, |
| 48 |
showLineNumbers, |
| 49 |
theme, |
| 50 |
lineNumberColor, |
| 51 |
lineNumberBgColor, |
| 52 |
lineNumberBorderColor, |
| 53 |
lineNumberBorderWidth, |
| 54 |
resOption, |
| 55 |
wrapperBackgroundColor, |
| 56 |
wrapperBorderColor, |
| 57 |
wrapperBorderWidth, |
| 58 |
wrapperBorderRadius, |
| 59 |
headerBackgroundColor, |
| 60 |
headerBorderColor, |
| 61 |
headerBorderWidth, |
| 62 |
copyButtonColor, |
| 63 |
copyButtonBorderColor, |
| 64 |
codeBackgroundColor, |
| 65 |
} = attributes; |
| 66 |
|
| 67 |
const resRequiredProps = { setAttributes, resOption, attributes, objAttributes }; |
| 68 |
|
| 69 |
const addResponse = () => { |
| 70 |
setAttributes({ |
| 71 |
responses: [ |
| 72 |
...responses, |
| 73 |
{ status: '', language: 'json', codeContent: '' }, |
| 74 |
], |
| 75 |
}); |
| 76 |
}; |
| 77 |
|
| 78 |
const updateResponse = (index, key, value) => { |
| 79 |
const next = responses.map((response, i) => |
| 80 |
i === index ? { ...response, [key]: value } : response |
| 81 |
); |
| 82 |
setAttributes({ responses: next }); |
| 83 |
}; |
| 84 |
|
| 85 |
const removeResponse = (index) => { |
| 86 |
setAttributes({ responses: responses.filter((_, i) => i !== index) }); |
| 87 |
}; |
| 88 |
|
| 89 |
return ( |
| 90 |
<InspectorControls key="controls"> |
| 91 |
<div className="eb-panel-control"> |
| 92 |
<TabPanel |
| 93 |
className="eb-parent-tab-panel" |
| 94 |
activeClass="active-tab" |
| 95 |
tabs={[ |
| 96 |
{ name: 'general', title: __('General', 'betterdocs'), className: 'eb-tab general' }, |
| 97 |
{ name: 'styles', title: __('Styles', 'betterdocs'), className: 'eb-tab styles' }, |
| 98 |
]} |
| 99 |
> |
| 100 |
{(tab) => ( |
| 101 |
<div className={'eb-tab-controls ' + tab.name}> |
| 102 |
{tab.name === 'general' && ( |
| 103 |
<> |
| 104 |
<PanelBody title={__('Responses', 'betterdocs')} initialOpen={true}> |
| 105 |
<BaseControl |
| 106 |
className="betterdocs-code-snippet-tab-repeater" |
| 107 |
help={__('Add one entry per HTTP status. Each becomes a tab (200, 404, …).', 'betterdocs')} |
| 108 |
> |
| 109 |
{responses.map((response, index) => ( |
| 110 |
<div |
| 111 |
className="betterdocs-code-snippet-tab-repeater-row" |
| 112 |
key={index} |
| 113 |
style={{ |
| 114 |
marginTop: '16px', |
| 115 |
paddingTop: '12px', |
| 116 |
borderTop: '1px solid #e0e0e0', |
| 117 |
}} |
| 118 |
> |
| 119 |
<div |
| 120 |
style={{ |
| 121 |
display: 'flex', |
| 122 |
alignItems: 'center', |
| 123 |
justifyContent: 'space-between', |
| 124 |
gap: '12px', |
| 125 |
marginBottom: '8px', |
| 126 |
}} |
| 127 |
> |
| 128 |
<strong>{__('Response', 'betterdocs')} {index + 1}</strong> |
| 129 |
<Button isDestructive variant="link" onClick={() => removeResponse(index)}> |
| 130 |
{__('Remove', 'betterdocs')} |
| 131 |
</Button> |
| 132 |
</div> |
| 133 |
<TextControl |
| 134 |
label={__('Status / Response type', 'betterdocs')} |
| 135 |
value={response.status || ''} |
| 136 |
onChange={(value) => updateResponse(index, 'status', value)} |
| 137 |
placeholder={__('e.g. 200, 404', 'betterdocs')} |
| 138 |
__next40pxDefaultSize |
| 139 |
__nextHasNoMarginBottom |
| 140 |
/> |
| 141 |
<SelectControl |
| 142 |
label={__('Language', 'betterdocs')} |
| 143 |
value={response.language || 'json'} |
| 144 |
options={RESPONSE_LANGUAGE_OPTIONS} |
| 145 |
onChange={(value) => updateResponse(index, 'language', value)} |
| 146 |
__next40pxDefaultSize |
| 147 |
__nextHasNoMarginBottom |
| 148 |
/> |
| 149 |
<CodeMirrorControl |
| 150 |
label={__('Response Body', 'betterdocs')} |
| 151 |
value={response.codeContent || ''} |
| 152 |
onChange={(value) => updateResponse(index, 'codeContent', value)} |
| 153 |
placeholder={__('Paste the example response here…', 'betterdocs')} |
| 154 |
rows={10} |
| 155 |
/> |
| 156 |
</div> |
| 157 |
))} |
| 158 |
<Button |
| 159 |
variant="secondary" |
| 160 |
onClick={addResponse} |
| 161 |
className="betterdocs-code-snippet-tab-repeater__add" |
| 162 |
__next40pxDefaultSize |
| 163 |
style={{ marginTop: '16px' }} |
| 164 |
> |
| 165 |
{__('+ Add response', 'betterdocs')} |
| 166 |
</Button> |
| 167 |
</BaseControl> |
| 168 |
</PanelBody> |
| 169 |
|
| 170 |
<PanelBody title={__('Appearance', 'betterdocs')} initialOpen={false}> |
| 171 |
<BaseControl |
| 172 |
label={__('Theme', 'betterdocs')} |
| 173 |
help={__('Choose light or dark styling for the response block.', 'betterdocs')} |
| 174 |
> |
| 175 |
<ButtonGroup> |
| 176 |
{THEME_OPTIONS.map((option) => ( |
| 177 |
<Button |
| 178 |
key={option.value} |
| 179 |
variant={theme === option.value ? 'primary' : 'secondary'} |
| 180 |
onClick={() => setAttributes({ theme: option.value })} |
| 181 |
> |
| 182 |
{option.label} |
| 183 |
</Button> |
| 184 |
))} |
| 185 |
</ButtonGroup> |
| 186 |
</BaseControl> |
| 187 |
<ToggleControl |
| 188 |
label={__('Show header bar', 'betterdocs')} |
| 189 |
checked={showHeader} |
| 190 |
onChange={(value) => setAttributes({ showHeader: value })} |
| 191 |
help={__('Toggle the status tabs + copy button.', 'betterdocs')} |
| 192 |
/> |
| 193 |
{showHeader && ( |
| 194 |
<ToggleControl |
| 195 |
label={__('Enable copy button', 'betterdocs')} |
| 196 |
checked={showCopyButton} |
| 197 |
onChange={(value) => setAttributes({ showCopyButton: value })} |
| 198 |
help={__('Show a one-click copy-to-clipboard icon.', 'betterdocs')} |
| 199 |
/> |
| 200 |
)} |
| 201 |
help={__('Display a tooltip (‘Copied!’) on copy button hover or click.', 'betterdocs')} |
| 202 |
/> |
| 203 |
)} |
| 204 |
<ToggleControl |
| 205 |
label={__('Show line numbers', 'betterdocs')} |
| 206 |
checked={showLineNumbers} |
| 207 |
onChange={(value) => setAttributes({ showLineNumbers: value })} |
| 208 |
help={__('Display line numbers in the response body.', 'betterdocs')} |
| 209 |
/> |
| 210 |
</PanelBody> |
| 211 |
</> |
| 212 |
)} |
| 213 |
{tab.name === 'styles' && ( |
| 214 |
<div> |
| 215 |
<PanelBody title={__('Wrapper', 'betterdocs')} initialOpen={false}> |
| 216 |
<ResponsiveDimensionsControl |
| 217 |
baseLabel={__('Margin', 'betterdocs')} |
| 218 |
resRequiredProps={resRequiredProps} |
| 219 |
controlName={CODE_SNIPPET_WRAPPER_MARGIN} |
| 220 |
/> |
| 221 |
<ResponsiveDimensionsControl |
| 222 |
baseLabel={__('Padding', 'betterdocs')} |
| 223 |
resRequiredProps={resRequiredProps} |
| 224 |
controlName={CODE_SNIPPET_WRAPPER_PADDING} |
| 225 |
/> |
| 226 |
<ColorControl |
| 227 |
label={__('Background Color', 'betterdocs')} |
| 228 |
color={wrapperBackgroundColor} |
| 229 |
onChange={(colorValue) => setAttributes({ wrapperBackgroundColor: colorValue })} |
| 230 |
/> |
| 231 |
<ColorControl |
| 232 |
label={__('Border Color', 'betterdocs')} |
| 233 |
color={wrapperBorderColor} |
| 234 |
onChange={(colorValue) => setAttributes({ wrapperBorderColor: colorValue })} |
| 235 |
/> |
| 236 |
<RangeControl |
| 237 |
label={__('Border Width', 'betterdocs')} |
| 238 |
value={wrapperBorderWidth} |
| 239 |
onChange={(value) => setAttributes({ wrapperBorderWidth: value })} |
| 240 |
min={0} |
| 241 |
max={10} |
| 242 |
step={1} |
| 243 |
/> |
| 244 |
<RangeControl |
| 245 |
label={__('Border Radius', 'betterdocs')} |
| 246 |
value={wrapperBorderRadius} |
| 247 |
onChange={(value) => setAttributes({ wrapperBorderRadius: value })} |
| 248 |
min={0} |
| 249 |
max={50} |
| 250 |
step={1} |
| 251 |
/> |
| 252 |
</PanelBody> |
| 253 |
|
| 254 |
{showHeader && ( |
| 255 |
<PanelBody title={__('Header', 'betterdocs')} initialOpen={false}> |
| 256 |
<ResponsiveDimensionsControl |
| 257 |
baseLabel={__('Padding', 'betterdocs')} |
| 258 |
resRequiredProps={resRequiredProps} |
| 259 |
controlName={CODE_SNIPPET_HEADER_PADDING} |
| 260 |
/> |
| 261 |
<ColorControl |
| 262 |
label={__('Background Color', 'betterdocs')} |
| 263 |
color={headerBackgroundColor} |
| 264 |
onChange={(colorValue) => setAttributes({ headerBackgroundColor: colorValue })} |
| 265 |
/> |
| 266 |
<ColorControl |
| 267 |
label={__('Border Color', 'betterdocs')} |
| 268 |
color={headerBorderColor} |
| 269 |
onChange={(colorValue) => setAttributes({ headerBorderColor: colorValue })} |
| 270 |
/> |
| 271 |
<RangeControl |
| 272 |
label={__('Border Width', 'betterdocs')} |
| 273 |
value={headerBorderWidth} |
| 274 |
onChange={(value) => setAttributes({ headerBorderWidth: value })} |
| 275 |
min={0} |
| 276 |
max={10} |
| 277 |
step={1} |
| 278 |
help={__('Controls border bottom width', 'betterdocs')} |
| 279 |
/> |
| 280 |
<ColorControl |
| 281 |
label={__('Copy Button Color', 'betterdocs')} |
| 282 |
color={copyButtonColor} |
| 283 |
onChange={(colorValue) => setAttributes({ copyButtonColor: colorValue })} |
| 284 |
/> |
| 285 |
<ColorControl |
| 286 |
label={__('Copy Button Border Color', 'betterdocs')} |
| 287 |
color={copyButtonBorderColor} |
| 288 |
onChange={(colorValue) => setAttributes({ copyButtonBorderColor: colorValue })} |
| 289 |
/> |
| 290 |
</PanelBody> |
| 291 |
)} |
| 292 |
|
| 293 |
{showLineNumbers && ( |
| 294 |
<PanelBody title={__('Line Numbers', 'betterdocs')} initialOpen={false}> |
| 295 |
<ResponsiveDimensionsControl |
| 296 |
baseLabel={__('Padding', 'betterdocs')} |
| 297 |
resRequiredProps={resRequiredProps} |
| 298 |
controlName={CODE_SNIPPET_LINE_NUMBERS_PADDING} |
| 299 |
/> |
| 300 |
<ColorControl |
| 301 |
label={__('Line Number Color', 'betterdocs')} |
| 302 |
color={lineNumberColor} |
| 303 |
onChange={(colorValue) => setAttributes({ lineNumberColor: colorValue })} |
| 304 |
/> |
| 305 |
<ColorControl |
| 306 |
label={__('Line Number Background', 'betterdocs')} |
| 307 |
color={lineNumberBgColor} |
| 308 |
onChange={(colorValue) => setAttributes({ lineNumberBgColor: colorValue })} |
| 309 |
/> |
| 310 |
<ColorControl |
| 311 |
label={__('Border Color', 'betterdocs')} |
| 312 |
color={lineNumberBorderColor} |
| 313 |
onChange={(colorValue) => setAttributes({ lineNumberBorderColor: colorValue })} |
| 314 |
/> |
| 315 |
<RangeControl |
| 316 |
label={__('Border Width', 'betterdocs')} |
| 317 |
value={lineNumberBorderWidth} |
| 318 |
onChange={(value) => setAttributes({ lineNumberBorderWidth: value })} |
| 319 |
min={0} |
| 320 |
max={10} |
| 321 |
step={1} |
| 322 |
help={__('Controls border right width', 'betterdocs')} |
| 323 |
/> |
| 324 |
</PanelBody> |
| 325 |
)} |
| 326 |
|
| 327 |
<PanelBody title={__('Code Content Area', 'betterdocs')} initialOpen={false}> |
| 328 |
<ResponsiveDimensionsControl |
| 329 |
baseLabel={__('Padding', 'betterdocs')} |
| 330 |
resRequiredProps={resRequiredProps} |
| 331 |
controlName={CODE_SNIPPET_CODE_PADDING} |
| 332 |
/> |
| 333 |
<ColorControl |
| 334 |
label={__('Background Color', 'betterdocs')} |
| 335 |
color={codeBackgroundColor} |
| 336 |
onChange={(colorValue) => setAttributes({ codeBackgroundColor: colorValue })} |
| 337 |
/> |
| 338 |
</PanelBody> |
| 339 |
</div> |
| 340 |
)} |
| 341 |
</div> |
| 342 |
)} |
| 343 |
</TabPanel> |
| 344 |
</div> |
| 345 |
</InspectorControls> |
| 346 |
); |
| 347 |
}; |
| 348 |
|
| 349 |
export default Inspector; |
| 350 |
|