| @@ -1,13 +1,16 @@ | ||
| 1 | +import { RichText, useBlockProps as blockProps } from '@wordpress/block-editor'; | |
| 1 | 2 | import { registerBlockType } from '@wordpress/blocks'; |
| 2 | 3 | import { addFilter } from '@wordpress/hooks'; |
| 3 | 4 | import { __ } from '@wordpress/i18n'; |
| 4 | -import { Editor } from './Editor'; | |
| 5 | 5 | import blockConfig from './block.json'; |
| 6 | +import { Edit } from './editor/Edit'; | |
| 6 | 7 | import { BlockFilter } from './editor/components/BlockFilter'; |
| 8 | +import { SidebarControls } from './editor/controls/Sidebar'; | |
| 9 | +import { ToolbarControls } from './editor/controls/Toolbar'; | |
| 7 | 10 | import './editor/editor.css'; |
| 8 | -import { transformToCBP, transformFromCBP } from './editor/transforms'; | |
| 9 | -import { BlockOutput } from './front/BlockOutput'; | |
| 11 | +import { CopyButton } from './front/CopyButton'; | |
| 12 | +import './front/style.css'; | |
| 10 | 13 | import { blockIcon } from './icons'; |
| 11 | 14 | import { Attributes } from './types'; |
| 12 | 15 | |
| 13 | 16 | registerBlockType<Attributes>(blockConfig.name, { |
| @@ -18,85 +21,46 @@ | ||
| 18 | 21 | code: { type: 'string' }, |
| 19 | 22 | codeHTML: { type: 'string' }, |
| 20 | 23 | language: { type: 'string' }, |
| 21 | 24 | theme: { type: 'string' }, |
| 22 | - align: { type: 'string' }, | |
| 25 | + align: { type: 'string', default: 'none' }, | |
| 23 | 26 | bgColor: { type: 'string', default: '#282a37' }, |
| 24 | 27 | textColor: { type: 'string', default: '#f8f8f2' }, |
| 25 | - fontSize: { type: 'string' }, | |
| 26 | - fontFamily: { type: 'string' }, | |
| 27 | - lineHeight: { type: 'string' }, | |
| 28 | - clampFonts: { type: 'boolean' }, | |
| 29 | - editorHeight: { type: 'string' }, | |
| 30 | 28 | lineNumbers: { type: 'boolean' }, |
| 31 | - headerType: { type: 'string' }, | |
| 32 | - headerString: { type: 'string' }, | |
| 33 | - disablePadding: { type: 'boolean' }, | |
| 34 | - footerType: { type: 'string' }, | |
| 35 | - footerString: { type: 'string' }, | |
| 36 | - footerLink: { type: 'string' }, | |
| 37 | - footerLinkTarget: { type: 'boolean' }, | |
| 38 | - enableMaxHeight: { type: 'boolean' }, | |
| 39 | - seeMoreType: { type: 'string' }, | |
| 40 | - seeMoreString: { type: 'string' }, | |
| 41 | - seeMoreAfterLine: { type: 'string' }, | |
| 42 | - seeMoreTransition: { type: 'boolean' }, | |
| 43 | - seeMoreCollapse: { type: 'boolean' }, | |
| 44 | - seeMoreCollapseString: { type: 'string' }, | |
| 45 | - startingLineNumber: { type: 'string' }, | |
| 46 | - lineNumbersWidth: { type: 'number' }, | |
| 47 | - highestLineNumber: { type: 'number' }, | |
| 48 | - enableHighlighting: { type: 'boolean' }, | |
| 49 | - highlightingHover: { type: 'boolean' }, | |
| 50 | - lineHighlights: { type: 'string' }, | |
| 51 | - lineHighlightColor: { type: 'string' }, | |
| 52 | - enableBlurring: { type: 'boolean' }, | |
| 53 | - lineBlurs: { type: 'string' }, | |
| 54 | - removeBlurOnHover: { type: 'boolean' }, | |
| 29 | + startingLineNumber: { type: 'number', default: 1 }, | |
| 55 | 30 | frame: { type: 'boolean' }, |
| 56 | 31 | renderType: { type: 'string', default: 'code' }, |
| 57 | 32 | label: { type: 'string', default: '' }, |
| 58 | 33 | copyButton: { type: 'boolean' }, |
| 59 | - copyButtonType: { type: 'string' }, | |
| 60 | - copyButtonString: { | |
| 61 | - type: 'string', | |
| 62 | - default: __('Copy', 'code-block-pro'), | |
| 63 | - }, | |
| 64 | - copyButtonStringCopied: { | |
| 65 | - type: 'string', | |
| 66 | - default: __('Copied!', 'code-block-pro'), | |
| 67 | - }, | |
| 68 | - useDecodeURI: { type: 'boolean', default: false }, | |
| 69 | - useEscapeShortCodes: { type: 'boolean', default: false }, | |
| 70 | - tabSize: { type: 'number', default: 2 }, | |
| 71 | - useTabs: { type: 'boolean' }, | |
| 72 | 34 | }, |
| 73 | - // Need to add these here to avoid TS type errors | |
| 74 | - supports: { | |
| 75 | - html: false, | |
| 76 | - align: ['wide', 'full'], | |
| 77 | - }, | |
| 78 | 35 | title: __('Code Pro', 'code-block-pro'), |
| 79 | 36 | edit: ({ attributes, setAttributes }) => ( |
| 80 | - <Editor attributes={attributes} setAttributes={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 | + </> | |
| 81 | 50 | ), |
| 82 | - save: ({ attributes }) => <BlockOutput attributes={attributes} />, | |
| 83 | - transforms: { | |
| 84 | - from: [ | |
| 85 | - { | |
| 86 | - type: 'block', | |
| 87 | - blocks: ['core/code', 'syntaxhighlighter/code'], | |
| 88 | - transform: transformToCBP, | |
| 89 | - }, | |
| 90 | - ], | |
| 91 | - to: [ | |
| 92 | - { | |
| 93 | - type: 'block', | |
| 94 | - blocks: ['core/code'], | |
| 95 | - transform: transformFromCBP, | |
| 96 | - }, | |
| 97 | - ], | |
| 98 | - }, | |
| 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 | + ), | |
| 99 | 63 | }); |
| 100 | 64 | |
| 101 | 65 | addFilter( |
| 102 | 66 | 'editor.BlockEdit', |