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