| 1 |
import { useBlockProps as blockProps } from '@wordpress/block-editor'; |
| 2 |
import { createBlock, registerBlockType } from '@wordpress/blocks'; |
| 3 |
import { addFilter } from '@wordpress/hooks'; |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
import { Lang } from 'shiki'; |
| 6 |
import blockConfig from './block.json'; |
| 7 |
import { Edit } from './editor/Edit'; |
| 8 |
import { BlockFilter } from './editor/components/BlockFilter'; |
| 9 |
import { HeaderType } from './editor/components/HeaderType'; |
| 10 |
import { SidebarControls } from './editor/controls/Sidebar'; |
| 11 |
import { ToolbarControls } from './editor/controls/Toolbar'; |
| 12 |
import './editor/editor.css'; |
| 13 |
import { BlockOutput } from './front/BlockOutput'; |
| 14 |
import { blockIcon } from './icons'; |
| 15 |
import { Attributes } from './types'; |
| 16 |
import { fontFamilyLong, maybeClamp } from './util/fonts'; |
| 17 |
import { getMainAlias } from './util/languages'; |
| 18 |
|
| 19 |
registerBlockType<Attributes>(blockConfig.name, { |
| 20 |
...blockConfig, |
| 21 |
icon: blockIcon, |
| 22 |
// Types seem to be mismatched if importing these from block.json |
| 23 |
attributes: { |
| 24 |
code: { type: 'string' }, |
| 25 |
codeHTML: { type: 'string' }, |
| 26 |
language: { type: 'string' }, |
| 27 |
theme: { type: 'string' }, |
| 28 |
align: { type: 'string' }, |
| 29 |
bgColor: { type: 'string', default: '#282a37' }, |
| 30 |
textColor: { type: 'string', default: '#f8f8f2' }, |
| 31 |
fontSize: { type: 'string' }, |
| 32 |
fontFamily: { type: 'string' }, |
| 33 |
lineHeight: { type: 'string' }, |
| 34 |
clampFonts: { type: 'boolean', default: false }, |
| 35 |
lineNumbers: { type: 'boolean' }, |
| 36 |
headerType: { type: 'string' }, |
| 37 |
startingLineNumber: { type: 'number', default: 1 }, |
| 38 |
frame: { type: 'boolean' }, |
| 39 |
renderType: { type: 'string', default: 'code' }, |
| 40 |
label: { type: 'string', default: '' }, |
| 41 |
copyButton: { type: 'boolean' }, |
| 42 |
}, |
| 43 |
// Need to add these here to avoid TS type errors |
| 44 |
supports: { |
| 45 |
html: false, |
| 46 |
align: ['wide', 'full'], |
| 47 |
}, |
| 48 |
title: __('Code Pro', 'code-block-pro'), |
| 49 |
edit: ({ attributes, setAttributes }) => ( |
| 50 |
<> |
| 51 |
<SidebarControls |
| 52 |
attributes={attributes} |
| 53 |
setAttributes={setAttributes} |
| 54 |
/> |
| 55 |
<ToolbarControls |
| 56 |
attributes={attributes} |
| 57 |
setAttributes={setAttributes} |
| 58 |
/> |
| 59 |
<div |
| 60 |
{...blockProps({ |
| 61 |
className: 'code-block-pro-editor', |
| 62 |
style: { |
| 63 |
fontSize: maybeClamp( |
| 64 |
attributes.fontSize, |
| 65 |
attributes.clampFonts, |
| 66 |
), |
| 67 |
fontFamily: fontFamilyLong(attributes.fontFamily), |
| 68 |
lineHeight: maybeClamp( |
| 69 |
attributes.lineHeight, |
| 70 |
attributes.clampFonts, |
| 71 |
), |
| 72 |
}, |
| 73 |
})}> |
| 74 |
<HeaderType {...attributes} /> |
| 75 |
<Edit attributes={attributes} setAttributes={setAttributes} /> |
| 76 |
</div> |
| 77 |
</> |
| 78 |
), |
| 79 |
save: ({ attributes }) => <BlockOutput attributes={attributes} />, |
| 80 |
transforms: { |
| 81 |
from: [ |
| 82 |
{ |
| 83 |
type: 'block', |
| 84 |
blocks: ['core/code', 'syntaxhighlighter/code'], |
| 85 |
transform: (attrs) => { |
| 86 |
// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 87 |
// @ts-ignore-next-line - Why is this reading the block generic? |
| 88 |
const { content, language } = attrs; |
| 89 |
const decode = (value: string) => { |
| 90 |
const txt = document.createElement('textarea'); |
| 91 |
txt.innerHTML = value; |
| 92 |
return txt.value; |
| 93 |
}; |
| 94 |
return createBlock(blockConfig.name, { |
| 95 |
code: content ? decode(content) : undefined, |
| 96 |
language: getMainAlias(language) as Lang, |
| 97 |
}); |
| 98 |
}, |
| 99 |
}, |
| 100 |
], |
| 101 |
}, |
| 102 |
}); |
| 103 |
|
| 104 |
addFilter( |
| 105 |
'editor.BlockEdit', |
| 106 |
blockConfig.name, |
| 107 |
(CurrentMenuItems) => |
| 108 |
// Not sure how to type these incoming props |
| 109 |
// eslint-disable-next-line |
| 110 |
(props: any) => |
| 111 |
BlockFilter(CurrentMenuItems, props), |
| 112 |
); |
| 113 |
|