| 1 |
import { RichText, useBlockProps as blockProps } from '@wordpress/block-editor'; |
| 2 |
import { applyFilters } from '@wordpress/hooks'; |
| 3 |
import classNames from 'classnames'; |
| 4 |
import defaultThemes from '../defaultThemes.json'; |
| 5 |
import { FooterType } from '../editor/components/FooterSelect'; |
| 6 |
import { HeaderType } from '../editor/components/HeaderSelect'; |
| 7 |
import { SeeMoreType } from '../editor/components/SeeMoreSelect'; |
| 8 |
import { ButtonList } from '../editor/components/buttons/ButtonList'; |
| 9 |
import { Attributes, ThemeOption } from '../types'; |
| 10 |
import { findLineNumberColor } from '../util/colors'; |
| 11 |
import { fontFamilyLong, maybeClamp } from '../util/fonts'; |
| 12 |
import './style.css'; |
| 13 |
|
| 14 |
export const BlockOutput = ({ attributes }: { attributes: Attributes }) => { |
| 15 |
// To add custom styles |
| 16 |
const themes = applyFilters( |
| 17 |
'blocks.codeBlockPro.themes', |
| 18 |
defaultThemes, |
| 19 |
) as ThemeOption; |
| 20 |
const styles = themes[attributes.theme]?.styles; |
| 21 |
|
| 22 |
return ( |
| 23 |
<div |
| 24 |
{...blockProps.save({ |
| 25 |
className: classNames({ |
| 26 |
'padding-disabled': attributes.disablePadding, |
| 27 |
'padding-bottom-disabled': |
| 28 |
attributes?.footerType && |
| 29 |
attributes?.footerType !== 'none', |
| 30 |
'cbp-has-line-numbers': attributes.lineNumbers, |
| 31 |
'cbp-blur-enabled': attributes.enableBlurring, |
| 32 |
'cbp-unblur-on-hover': attributes.removeBlurOnHover, |
| 33 |
'cbp-highlight-hover': attributes.highlightingHover, |
| 34 |
}), |
| 35 |
})} |
| 36 |
data-code-block-pro-font-family={attributes.fontFamily} |
| 37 |
style={ |
| 38 |
{ |
| 39 |
fontSize: maybeClamp( |
| 40 |
attributes.fontSize, |
| 41 |
attributes.clampFonts, |
| 42 |
), |
| 43 |
// Tiny check to avoid block invalidation error |
| 44 |
fontFamily: fontFamilyLong(attributes.fontFamily), |
| 45 |
'--cbp-line-number-color': attributes?.lineNumbers |
| 46 |
? findLineNumberColor(attributes) |
| 47 |
: undefined, |
| 48 |
'--cbp-line-number-start': |
| 49 |
Number(attributes?.startingLineNumber) > 1 |
| 50 |
? attributes.startingLineNumber |
| 51 |
: undefined, |
| 52 |
'--cbp-line-number-width': attributes.lineNumbersWidth |
| 53 |
? `${attributes.lineNumbersWidth}px` |
| 54 |
: undefined, |
| 55 |
'--cbp-line-highlight-color': |
| 56 |
attributes?.enableHighlighting || |
| 57 |
attributes?.highlightingHover |
| 58 |
? attributes.lineHighlightColor |
| 59 |
: undefined, |
| 60 |
lineHeight: maybeClamp( |
| 61 |
attributes.lineHeight, |
| 62 |
attributes.clampFonts, |
| 63 |
), |
| 64 |
...Object.entries(styles ?? {}).reduce( |
| 65 |
(acc, [key, value]) => ({ |
| 66 |
...acc, |
| 67 |
[`--shiki-${key}`]: value, |
| 68 |
}), |
| 69 |
{}, |
| 70 |
), |
| 71 |
...(applyFilters( |
| 72 |
'blocks.codeBlockPro.additionalOutputAttributes', |
| 73 |
{}, |
| 74 |
attributes, |
| 75 |
) as object), |
| 76 |
} as React.CSSProperties |
| 77 |
}> |
| 78 |
{attributes.code?.length > 0 ? ( |
| 79 |
<> |
| 80 |
<HeaderType {...attributes} /> |
| 81 |
<ButtonList {...attributes} /> |
| 82 |
<RichText.Content value={attributes.codeHTML} /> |
| 83 |
<FooterType {...attributes} /> |
| 84 |
<SeeMoreType {...attributes} /> |
| 85 |
</> |
| 86 |
) : null} |
| 87 |
</div> |
| 88 |
); |
| 89 |
}; |
| 90 |
|