| 1 |
import { __ } from '@wordpress/i18n'; |
| 2 |
import { InspectorControls, MediaUpload, MediaUploadCheck } 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 './components/CodeMirrorControl'; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
import { |
| 20 |
LANGUAGE_OPTIONS, |
| 21 |
THEME_OPTIONS, |
| 22 |
CODE_SNIPPET_WRAPPER_MARGIN, |
| 23 |
CODE_SNIPPET_WRAPPER_PADDING, |
| 24 |
CODE_SNIPPET_HEADER_PADDING, |
| 25 |
CODE_SNIPPET_LINE_NUMBERS_PADDING, |
| 26 |
CODE_SNIPPET_CODE_PADDING, |
| 27 |
} from './constants'; |
| 28 |
|
| 29 |
import { |
| 30 |
typoPrefix_fileName |
| 31 |
} from "./typographyPrefixConstants"; |
| 32 |
|
| 33 |
import ResponsiveDimensionsControl from "../../../util/dimensions-control-v2"; |
| 34 |
import ColorControl from "../../../util/color-control"; |
| 35 |
import TypographyDropdown from "../../../util/typography-control-v2"; |
| 36 |
import objAttributes from './attributes'; |
| 37 |
|
| 38 |
const Inspector = ({attributes, setAttributes}) => { |
| 39 |
const { |
| 40 |
codeContent, |
| 41 |
language, |
| 42 |
codeVariants = [], |
| 43 |
showCopyButton, |
| 44 |
showHeader, |
| 45 |
showLineNumbers, |
| 46 |
theme, |
| 47 |
lineNumberColor, |
| 48 |
lineNumberBgColor, |
| 49 |
lineNumberBorderColor, |
| 50 |
lineNumberBorderWidth, |
| 51 |
resOption, |
| 52 |
fileName, |
| 53 |
showFileIcon, |
| 54 |
fileIcon, |
| 55 |
showTrafficLights, |
| 56 |
wrapperBackgroundColor, |
| 57 |
wrapperBorderColor, |
| 58 |
wrapperBorderWidth, |
| 59 |
wrapperBorderRadius, |
| 60 |
headerBackgroundColor, |
| 61 |
headerBorderColor, |
| 62 |
headerBorderWidth, |
| 63 |
fileNameColor, |
| 64 |
copyButtonColor, |
| 65 |
copyButtonBorderColor, |
| 66 |
codeBackgroundColor, |
| 67 |
} = attributes; |
| 68 |
|
| 69 |
const resRequiredProps = { |
| 70 |
setAttributes, |
| 71 |
resOption, |
| 72 |
attributes, |
| 73 |
objAttributes, |
| 74 |
}; |
| 75 |
|
| 76 |
// Additional-language repeater helpers. The primary language/code above is |
| 77 |
// variant #1; these append to it. When 1+ exist the frontend shows a |
| 78 |
// language dropdown to switch between them. |
| 79 |
const addVariant = () => { |
| 80 |
setAttributes({ |
| 81 |
codeVariants: [ |
| 82 |
...codeVariants, |
| 83 |
{ language: 'php', codeContent: '' }, |
| 84 |
], |
| 85 |
}); |
| 86 |
}; |
| 87 |
|
| 88 |
const updateVariant = (index, key, value) => { |
| 89 |
const next = codeVariants.map((variant, i) => |
| 90 |
i === index ? { ...variant, [key]: value } : variant |
| 91 |
); |
| 92 |
setAttributes({ codeVariants: next }); |
| 93 |
}; |
| 94 |
|
| 95 |
const removeVariant = (index) => { |
| 96 |
setAttributes({ |
| 97 |
codeVariants: codeVariants.filter((_, i) => i !== index), |
| 98 |
}); |
| 99 |
}; |
| 100 |
|
| 101 |
return( |
| 102 |
<InspectorControls key="controls"> |
| 103 |
<div className="eb-panel-control"> |
| 104 |
<TabPanel |
| 105 |
className="eb-parent-tab-panel" |
| 106 |
activeClass="active-tab" |
| 107 |
tabs={[ |
| 108 |
{ |
| 109 |
name: "general", |
| 110 |
title: __("General", "betterdocs"), |
| 111 |
className: "eb-tab general" |
| 112 |
}, |
| 113 |
{ |
| 114 |
name: "styles", |
| 115 |
title: __("Styles", "betterdocs"), |
| 116 |
className: "eb-tab styles", |
| 117 |
} |
| 118 |
]} |
| 119 |
> |
| 120 |
{(tab) => ( |
| 121 |
<div className={"eb-tab-controls " + tab.name}> |
| 122 |
{tab.name === 'general' && ( |
| 123 |
<> |
| 124 |
<PanelBody title={__("Code Settings", "betterdocs")} initialOpen={true}> |
| 125 |
<SelectControl |
| 126 |
label={__("Choose Programming Language", "betterdocs")} |
| 127 |
value={language} |
| 128 |
options={LANGUAGE_OPTIONS} |
| 129 |
onChange={(value) => setAttributes({ language: value })} |
| 130 |
help={__("Choose language for syntax highlighting.", "betterdocs")} |
| 131 |
__next40pxDefaultSize |
| 132 |
__nextHasNoMarginBottom |
| 133 |
/> |
| 134 |
|
| 135 |
<CodeMirrorControl |
| 136 |
label={__("Code Snippet", "betterdocs")} |
| 137 |
value={codeContent} |
| 138 |
onChange={(value) => setAttributes({ codeContent: value })} |
| 139 |
placeholder={__("// Paste or type your code here...", "betterdocs")} |
| 140 |
rows={15} |
| 141 |
help={__("Enter your code snippet. Use Tab for indentation. Syntax highlighting will appear in the preview.", "betterdocs")} |
| 142 |
/> |
| 143 |
|
| 144 |
<BaseControl |
| 145 |
className="betterdocs-code-variants-control" |
| 146 |
help={__("Add the same snippet in other languages. When you add one or more, the header shows a language dropdown to switch between them.", "betterdocs")} |
| 147 |
> |
| 148 |
{codeVariants.map((variant, index) => ( |
| 149 |
<div |
| 150 |
className="betterdocs-code-variant" |
| 151 |
key={index} |
| 152 |
style={{ |
| 153 |
marginTop: '16px', |
| 154 |
paddingTop: '12px', |
| 155 |
borderTop: '1px solid #e0e0e0', |
| 156 |
}} |
| 157 |
> |
| 158 |
<div |
| 159 |
className="betterdocs-code-variant__head" |
| 160 |
style={{ |
| 161 |
display: 'flex', |
| 162 |
alignItems: 'center', |
| 163 |
justifyContent: 'space-between', |
| 164 |
gap: '12px', |
| 165 |
marginBottom: '8px', |
| 166 |
}} |
| 167 |
> |
| 168 |
<strong> |
| 169 |
{__("Language", "betterdocs")} {index + 2} |
| 170 |
</strong> |
| 171 |
<Button |
| 172 |
isDestructive |
| 173 |
variant="link" |
| 174 |
onClick={() => removeVariant(index)} |
| 175 |
> |
| 176 |
{__("Remove", "betterdocs")} |
| 177 |
</Button> |
| 178 |
</div> |
| 179 |
<SelectControl |
| 180 |
label={__("Programming Language", "betterdocs")} |
| 181 |
value={variant.language} |
| 182 |
options={LANGUAGE_OPTIONS} |
| 183 |
onChange={(value) => updateVariant(index, 'language', value)} |
| 184 |
__next40pxDefaultSize |
| 185 |
__nextHasNoMarginBottom |
| 186 |
/> |
| 187 |
<CodeMirrorControl |
| 188 |
label={__("Code Snippet", "betterdocs")} |
| 189 |
value={variant.codeContent} |
| 190 |
onChange={(value) => updateVariant(index, 'codeContent', value)} |
| 191 |
placeholder={__("// Paste or type your code here...", "betterdocs")} |
| 192 |
rows={10} |
| 193 |
/> |
| 194 |
</div> |
| 195 |
))} |
| 196 |
<Button |
| 197 |
variant="secondary" |
| 198 |
onClick={addVariant} |
| 199 |
className="betterdocs-code-variant__add" |
| 200 |
__next40pxDefaultSize |
| 201 |
> |
| 202 |
{__("+ Add Language", "betterdocs")} |
| 203 |
</Button> |
| 204 |
</BaseControl> |
| 205 |
</PanelBody> |
| 206 |
|
| 207 |
<PanelBody title={__("Appearance", "betterdocs")} initialOpen={false}> |
| 208 |
<BaseControl |
| 209 |
label={__("Theme", "betterdocs")} |
| 210 |
help={__("Choose light or dark styling for the code snippet block.", "betterdocs")} |
| 211 |
> |
| 212 |
<ButtonGroup> |
| 213 |
{THEME_OPTIONS.map((option) => ( |
| 214 |
<Button |
| 215 |
key={option.value} |
| 216 |
variant={theme === option.value ? 'primary' : 'secondary'} |
| 217 |
onClick={() => setAttributes({ theme: option.value })} |
| 218 |
> |
| 219 |
{option.label} |
| 220 |
</Button> |
| 221 |
))} |
| 222 |
</ButtonGroup> |
| 223 |
</BaseControl> |
| 224 |
<ToggleControl |
| 225 |
label={__("Show header bar", "betterdocs")} |
| 226 |
checked={showHeader} |
| 227 |
onChange={(value) => setAttributes({ showHeader: value })} |
| 228 |
help={__("Toggle the filename header and copy button.", "betterdocs")} |
| 229 |
/> |
| 230 |
{showHeader && ( |
| 231 |
<ToggleControl |
| 232 |
label={__("Enable copy button", "betterdocs")} |
| 233 |
checked={showCopyButton} |
| 234 |
onChange={(value) => setAttributes({ showCopyButton: value })} |
| 235 |
help={__("Show a one-click copy-to-clipboard icon.", "betterdocs")} |
| 236 |
/> |
| 237 |
)} |
| 238 |
|
| 239 |
help={__("Display a tooltip (‘Copied!’) on copy button hover or click.", "betterdocs")} |
| 240 |
/> |
| 241 |
)} |
| 242 |
<ToggleControl |
| 243 |
label={__("Show line numbers", "betterdocs")} |
| 244 |
checked={showLineNumbers} |
| 245 |
onChange={(value) => setAttributes({ showLineNumbers: value })} |
| 246 |
help={__("Display line numbers in the code block.", "betterdocs")} |
| 247 |
/> |
| 248 |
</PanelBody> |
| 249 |
{showHeader && ( |
| 250 |
<PanelBody title={__("File Preview Header", "betterdocs")} initialOpen={false}> |
| 251 |
<TextControl |
| 252 |
label={__("Filename", "betterdocs")} |
| 253 |
value={fileName} |
| 254 |
onChange={(value) => setAttributes({ fileName: value })} |
| 255 |
placeholder={__("hero-section.tsx", "betterdocs")} |
| 256 |
help={__("Enter the name to display in header (e.g., hero-section.tsx)", "betterdocs")} |
| 257 |
__next40pxDefaultSize |
| 258 |
__nextHasNoMarginBottom |
| 259 |
/> |
| 260 |
|
| 261 |
<ToggleControl |
| 262 |
label={__("Show window buttons", "betterdocs")} |
| 263 |
checked={showTrafficLights} |
| 264 |
onChange={(value) => setAttributes({ showTrafficLights: value })} |
| 265 |
help={__("Display macOS-style close/minimize/maximize circles.", "betterdocs")} |
| 266 |
/> |
| 267 |
|
| 268 |
<ToggleControl |
| 269 |
label={__("Show language icon", "betterdocs")} |
| 270 |
checked={showFileIcon} |
| 271 |
onChange={(value) => setAttributes({ showFileIcon: value })} |
| 272 |
help={__("Display the default icon for this file type.", "betterdocs")} |
| 273 |
/> |
| 274 |
|
| 275 |
{showFileIcon && ( |
| 276 |
<MediaUploadCheck> |
| 277 |
<MediaUpload |
| 278 |
onSelect={(media) => setAttributes({ fileIcon: media.url })} |
| 279 |
allowedTypes={['image']} |
| 280 |
value={fileIcon} |
| 281 |
render={({ open }) => ( |
| 282 |
<BaseControl |
| 283 |
label={__("Custom language icon", "betterdocs")} |
| 284 |
help={__("Upload a custom icon to override the default.", "betterdocs")} |
| 285 |
> |
| 286 |
<div style={{ display: 'flex', alignItems: 'center', gap: '10px' }}> |
| 287 |
<Button |
| 288 |
onClick={open} |
| 289 |
variant="secondary" |
| 290 |
__next40pxDefaultSize |
| 291 |
> |
| 292 |
{fileIcon ? __("Change Icon", "betterdocs") : __("Upload Icon", "betterdocs")} |
| 293 |
</Button> |
| 294 |
{fileIcon && ( |
| 295 |
<> |
| 296 |
<img |
| 297 |
src={fileIcon} |
| 298 |
alt={__("File icon preview", "betterdocs")} |
| 299 |
style={{ width: '24px', height: '24px', objectFit: 'contain' }} |
| 300 |
/> |
| 301 |
<Button |
| 302 |
onClick={() => setAttributes({ fileIcon: '' })} |
| 303 |
variant="link" |
| 304 |
isDestructive |
| 305 |
__next40pxDefaultSize |
| 306 |
> |
| 307 |
{__("Remove", "betterdocs")} |
| 308 |
</Button> |
| 309 |
</> |
| 310 |
)} |
| 311 |
</div> |
| 312 |
</BaseControl> |
| 313 |
)} |
| 314 |
/> |
| 315 |
</MediaUploadCheck> |
| 316 |
)} |
| 317 |
</PanelBody> |
| 318 |
)} |
| 319 |
</> |
| 320 |
)} |
| 321 |
{tab.name === 'styles' && ( |
| 322 |
<div> |
| 323 |
<PanelBody title={__("Wrapper", "betterdocs")} initialOpen={false}> |
| 324 |
<ResponsiveDimensionsControl |
| 325 |
baseLabel={__("Margin", "betterdocs")} |
| 326 |
resRequiredProps={resRequiredProps} |
| 327 |
controlName={CODE_SNIPPET_WRAPPER_MARGIN} |
| 328 |
/> |
| 329 |
<ResponsiveDimensionsControl |
| 330 |
baseLabel={__("Padding", "betterdocs")} |
| 331 |
resRequiredProps={resRequiredProps} |
| 332 |
controlName={CODE_SNIPPET_WRAPPER_PADDING} |
| 333 |
/> |
| 334 |
<ColorControl |
| 335 |
label={__("Background Color", "betterdocs")} |
| 336 |
color={wrapperBackgroundColor} |
| 337 |
onChange={(colorValue) => { |
| 338 |
setAttributes({wrapperBackgroundColor: colorValue}) |
| 339 |
}} |
| 340 |
/> |
| 341 |
<ColorControl |
| 342 |
label={__("Border Color", "betterdocs")} |
| 343 |
color={wrapperBorderColor} |
| 344 |
onChange={(colorValue) => { |
| 345 |
setAttributes({wrapperBorderColor: colorValue}) |
| 346 |
}} |
| 347 |
/> |
| 348 |
<RangeControl |
| 349 |
label={__("Border Width", "betterdocs")} |
| 350 |
value={wrapperBorderWidth} |
| 351 |
onChange={(value) => setAttributes({wrapperBorderWidth: value})} |
| 352 |
min={0} |
| 353 |
max={10} |
| 354 |
step={1} |
| 355 |
/> |
| 356 |
<RangeControl |
| 357 |
label={__("Border Radius", "betterdocs")} |
| 358 |
value={wrapperBorderRadius} |
| 359 |
onChange={(value) => setAttributes({wrapperBorderRadius: value})} |
| 360 |
min={0} |
| 361 |
max={50} |
| 362 |
step={1} |
| 363 |
/> |
| 364 |
</PanelBody> |
| 365 |
|
| 366 |
{showHeader && ( |
| 367 |
<PanelBody title={__("Header", "betterdocs")} initialOpen={false}> |
| 368 |
<ResponsiveDimensionsControl |
| 369 |
baseLabel={__("Padding", "betterdocs")} |
| 370 |
resRequiredProps={resRequiredProps} |
| 371 |
controlName={CODE_SNIPPET_HEADER_PADDING} |
| 372 |
/> |
| 373 |
<ColorControl |
| 374 |
label={__("Background Color", "betterdocs")} |
| 375 |
color={headerBackgroundColor} |
| 376 |
onChange={(colorValue) => { |
| 377 |
setAttributes({headerBackgroundColor: colorValue}) |
| 378 |
}} |
| 379 |
/> |
| 380 |
<ColorControl |
| 381 |
label={__("Border Color", "betterdocs")} |
| 382 |
color={headerBorderColor} |
| 383 |
onChange={(colorValue) => { |
| 384 |
setAttributes({headerBorderColor: colorValue}) |
| 385 |
}} |
| 386 |
/> |
| 387 |
<RangeControl |
| 388 |
label={__("Border Width", "betterdocs")} |
| 389 |
value={headerBorderWidth} |
| 390 |
onChange={(value) => setAttributes({headerBorderWidth: value})} |
| 391 |
min={0} |
| 392 |
max={10} |
| 393 |
step={1} |
| 394 |
help={__("Controls border bottom width", "betterdocs")} |
| 395 |
/> |
| 396 |
<TypographyDropdown |
| 397 |
baseLabel={__("File Name Typography", "betterdocs")} |
| 398 |
typographyPrefixConstant={typoPrefix_fileName} |
| 399 |
resRequiredProps={resRequiredProps} |
| 400 |
defaultFontSize={14} |
| 401 |
/> |
| 402 |
<ColorControl |
| 403 |
label={__("File Name Color", "betterdocs")} |
| 404 |
color={fileNameColor} |
| 405 |
onChange={(colorValue) => { |
| 406 |
setAttributes({fileNameColor: colorValue}) |
| 407 |
}} |
| 408 |
/> |
| 409 |
<ColorControl |
| 410 |
label={__("Copy Button Color", "betterdocs")} |
| 411 |
color={copyButtonColor} |
| 412 |
onChange={(colorValue) => { |
| 413 |
setAttributes({copyButtonColor: colorValue}) |
| 414 |
}} |
| 415 |
/> |
| 416 |
<ColorControl |
| 417 |
label={__("Copy Button Border Color", "betterdocs")} |
| 418 |
color={copyButtonBorderColor} |
| 419 |
onChange={(colorValue) => { |
| 420 |
setAttributes({copyButtonBorderColor: colorValue}) |
| 421 |
}} |
| 422 |
/> |
| 423 |
</PanelBody> |
| 424 |
)} |
| 425 |
|
| 426 |
{showLineNumbers && ( |
| 427 |
<PanelBody title={__("Line Numbers", "betterdocs")} initialOpen={false}> |
| 428 |
<ResponsiveDimensionsControl |
| 429 |
baseLabel={__("Padding", "betterdocs")} |
| 430 |
resRequiredProps={resRequiredProps} |
| 431 |
controlName={CODE_SNIPPET_LINE_NUMBERS_PADDING} |
| 432 |
/> |
| 433 |
<ColorControl |
| 434 |
label={__("Line Number Color", "betterdocs")} |
| 435 |
color={lineNumberColor} |
| 436 |
onChange={(colorValue) => { |
| 437 |
setAttributes({lineNumberColor: colorValue}) |
| 438 |
}} |
| 439 |
/> |
| 440 |
<ColorControl |
| 441 |
label={__("Line Number Background", "betterdocs")} |
| 442 |
color={lineNumberBgColor} |
| 443 |
onChange={(colorValue) => { |
| 444 |
setAttributes({lineNumberBgColor: colorValue}) |
| 445 |
}} |
| 446 |
/> |
| 447 |
<ColorControl |
| 448 |
label={__("Border Color", "betterdocs")} |
| 449 |
color={lineNumberBorderColor} |
| 450 |
onChange={(colorValue) => { |
| 451 |
setAttributes({lineNumberBorderColor: colorValue}) |
| 452 |
}} |
| 453 |
/> |
| 454 |
<RangeControl |
| 455 |
label={__("Border Width", "betterdocs")} |
| 456 |
value={lineNumberBorderWidth} |
| 457 |
onChange={(value) => setAttributes({lineNumberBorderWidth: value})} |
| 458 |
min={0} |
| 459 |
max={10} |
| 460 |
step={1} |
| 461 |
help={__("Controls border right width", "betterdocs")} |
| 462 |
/> |
| 463 |
</PanelBody> |
| 464 |
)} |
| 465 |
|
| 466 |
<PanelBody title={__("Code Content Area", "betterdocs")} initialOpen={false}> |
| 467 |
<ResponsiveDimensionsControl |
| 468 |
baseLabel={__("Padding", "betterdocs")} |
| 469 |
resRequiredProps={resRequiredProps} |
| 470 |
controlName={CODE_SNIPPET_CODE_PADDING} |
| 471 |
/> |
| 472 |
<ColorControl |
| 473 |
label={__("Background Color", "betterdocs")} |
| 474 |
color={codeBackgroundColor} |
| 475 |
onChange={(colorValue) => { |
| 476 |
setAttributes({codeBackgroundColor: colorValue}) |
| 477 |
}} |
| 478 |
/> |
| 479 |
</PanelBody> |
| 480 |
</div> |
| 481 |
)} |
| 482 |
</div> |
| 483 |
)} |
| 484 |
</TabPanel> |
| 485 |
</div> |
| 486 |
</InspectorControls> |
| 487 |
); |
| 488 |
} |
| 489 |
|
| 490 |
export default Inspector; |
| 491 |
|