| 1 |
import { RichText, useBlockProps as blockProps } from '@wordpress/block-editor'; |
| 2 |
import { registerBlockType } from '@wordpress/blocks'; |
| 3 |
import { addFilter } from '@wordpress/hooks'; |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
import blockConfig from './block.json'; |
| 6 |
import { Edit } from './editor/Edit'; |
| 7 |
import { BlockFilter } from './editor/components/BlockFilter'; |
| 8 |
import { SidebarControls } from './editor/controls/Sidebar'; |
| 9 |
import { ToolbarControls } from './editor/controls/Toolbar'; |
| 10 |
import './editor/editor.css'; |
| 11 |
import { CopyButton } from './front/CopyButton'; |
| 12 |
import './front/style.css'; |
| 13 |
import { blockIcon } from './icons'; |
| 14 |
import { Attributes } from './types'; |
| 15 |
|
| 16 |
registerBlockType<Attributes>(blockConfig.name, { |
| 17 |
...blockConfig, |
| 18 |
icon: blockIcon, |
| 19 |
// Types seem to be mismatched if importing these from block.json |
| 20 |
attributes: { |
| 21 |
code: { type: 'string' }, |
| 22 |
codeHTML: { type: 'string' }, |
| 23 |
language: { type: 'string' }, |
| 24 |
theme: { type: 'string' }, |
| 25 |
align: { type: 'string', default: 'center' }, |
| 26 |
bgColor: { type: 'string', default: '#282a37' }, |
| 27 |
textColor: { type: 'string', default: '#f8f8f2' }, |
| 28 |
lineNumbers: { type: 'boolean' }, |
| 29 |
startingLineNumber: { type: 'number', default: 1 }, |
| 30 |
frame: { type: 'boolean' }, |
| 31 |
renderType: { type: 'string', default: 'code' }, |
| 32 |
label: { type: 'string', default: '' }, |
| 33 |
copyButton: { type: 'boolean' }, |
| 34 |
}, |
| 35 |
title: __('Code Pro', 'code-block-pro'), |
| 36 |
edit: ({ attributes, setAttributes }) => ( |
| 37 |
<> |
| 38 |
<SidebarControls |
| 39 |
attributes={attributes} |
| 40 |
setAttributes={setAttributes} |
| 41 |
/> |
| 42 |
<ToolbarControls |
| 43 |
attributes={attributes} |
| 44 |
setAttributes={setAttributes} |
| 45 |
/> |
| 46 |
<div {...blockProps({ className: 'code-block-pro-editor' })}> |
| 47 |
<Edit attributes={attributes} setAttributes={setAttributes} /> |
| 48 |
</div> |
| 49 |
</> |
| 50 |
), |
| 51 |
save: ({ attributes }) => ( |
| 52 |
<div {...blockProps.save()}> |
| 53 |
{attributes.code?.length > 0 ? ( |
| 54 |
<> |
| 55 |
{attributes.copyButton && ( |
| 56 |
<CopyButton attributes={attributes} /> |
| 57 |
)} |
| 58 |
<RichText.Content value={attributes.codeHTML} /> |
| 59 |
</> |
| 60 |
) : null} |
| 61 |
</div> |
| 62 |
), |
| 63 |
}); |
| 64 |
|
| 65 |
addFilter( |
| 66 |
'editor.BlockEdit', |
| 67 |
blockConfig.name, |
| 68 |
(CurrentMenuItems) => |
| 69 |
// Not sure how to type these incoming props |
| 70 |
// eslint-disable-next-line |
| 71 |
(props: any) => |
| 72 |
BlockFilter(CurrentMenuItems, props), |
| 73 |
); |
| 74 |
|