| 1 |
import { 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 { ButtonList } from './editor/components/buttons/ButtonList'; |
| 6 |
import { FooterType } from './editor/components/FooterSelect'; |
| 7 |
import { HeaderType } from './editor/components/HeaderSelect'; |
| 8 |
import { SeeMoreType } from './editor/components/SeeMoreSelect'; |
| 9 |
import { SidebarControls } from './editor/controls/Sidebar'; |
| 10 |
import { ToolbarControls } from './editor/controls/Toolbar'; |
| 11 |
import { Edit } from './editor/Edit'; |
| 12 |
import type { AttributesPropsAndSetter, ThemeOption } from './types'; |
| 13 |
import { findLineNumberColor } from './util/colors'; |
| 14 |
import { fontFamilyLong, maybeClamp } from './util/fonts'; |
| 15 |
|
| 16 |
export const Editor = ({ |
| 17 |
attributes, |
| 18 |
setAttributes, |
| 19 |
}: AttributesPropsAndSetter) => { |
| 20 |
const { |
| 21 |
clampFonts, |
| 22 |
disablePadding, |
| 23 |
enableBlurring, |
| 24 |
enableHighlighting, |
| 25 |
footerType, |
| 26 |
fontSize, |
| 27 |
fontFamily, |
| 28 |
highestLineNumber, |
| 29 |
highlightingHover, |
| 30 |
lineHeight, |
| 31 |
lineHighlightColor, |
| 32 |
lineNumbers, |
| 33 |
lineNumbersWidth, |
| 34 |
removeBlurOnHover, |
| 35 |
startingLineNumber, |
| 36 |
tabSize, |
| 37 |
theme, |
| 38 |
useTabs, |
| 39 |
} = attributes; |
| 40 |
// To add custom styles |
| 41 |
const themes = applyFilters( |
| 42 |
'blocks.codeBlockPro.themes', |
| 43 |
defaultThemes, |
| 44 |
) as ThemeOption; |
| 45 |
const styles = themes[theme]?.styles; |
| 46 |
|
| 47 |
return ( |
| 48 |
<> |
| 49 |
<SidebarControls attributes={attributes} setAttributes={setAttributes} /> |
| 50 |
<ToolbarControls attributes={attributes} setAttributes={setAttributes} /> |
| 51 |
<div |
| 52 |
{...blockProps({ |
| 53 |
className: classnames('code-block-pro-editor', { |
| 54 |
'padding-disabled': disablePadding, |
| 55 |
'padding-bottom-disabled': footerType && footerType !== 'none', |
| 56 |
'cbp-has-line-numbers': lineNumbers, |
| 57 |
'cbp-blur-enabled': enableBlurring, |
| 58 |
'cbp-unblur-on-hover': removeBlurOnHover, |
| 59 |
'cbp-highlight-hover': highlightingHover, |
| 60 |
}), |
| 61 |
style: { |
| 62 |
fontSize: maybeClamp(fontSize, clampFonts), |
| 63 |
'--cbp-line-number-color': lineNumbers |
| 64 |
? findLineNumberColor(attributes) |
| 65 |
: undefined, |
| 66 |
'--cbp-line-number-start': |
| 67 |
Number(startingLineNumber) > 1 ? startingLineNumber : undefined, |
| 68 |
'--cbp-line-number-width': highestLineNumber |
| 69 |
? `calc(${String(highestLineNumber).length} * ${fontSize})` |
| 70 |
: lineNumbersWidth // bw compatability |
| 71 |
? `${lineNumbersWidth}px` |
| 72 |
: undefined, |
| 73 |
'--cbp-line-highlight-color': |
| 74 |
enableHighlighting || highlightingHover |
| 75 |
? lineHighlightColor |
| 76 |
: undefined, |
| 77 |
'--cbp-line-height': lineHeight, |
| 78 |
tabSize: |
| 79 |
useTabs === undefined |
| 80 |
? undefined // bw compatability |
| 81 |
: tabSize, |
| 82 |
fontFamily: fontFamilyLong(fontFamily), |
| 83 |
lineHeight: maybeClamp(lineHeight, clampFonts), |
| 84 |
...Object.entries(styles ?? {}).reduce( |
| 85 |
(acc, [key, value]) => ({ |
| 86 |
...acc, |
| 87 |
[`--shiki-${key}`]: value, |
| 88 |
}), |
| 89 |
{}, |
| 90 |
), |
| 91 |
...(applyFilters( |
| 92 |
'blocks.codeBlockPro.additionalEditorAttributes', |
| 93 |
{}, |
| 94 |
attributes, |
| 95 |
) as object), |
| 96 |
}, |
| 97 |
})} |
| 98 |
> |
| 99 |
<HeaderType {...attributes} /> |
| 100 |
<ButtonList {...attributes} /> |
| 101 |
<Edit attributes={attributes} setAttributes={setAttributes} /> |
| 102 |
<FooterType {...attributes} /> |
| 103 |
<SeeMoreType {...attributes} /> |
| 104 |
</div> |
| 105 |
</> |
| 106 |
); |
| 107 |
}; |
| 108 |
|