| @@ -1,15 +1,24 @@ | ||
| 1 | -import { registerBlockType } from '@wordpress/blocks'; | |
| 1 | +import { useBlockProps as blockProps } from '@wordpress/block-editor'; | |
| 2 | +import { createBlock, registerBlockType } from '@wordpress/blocks'; | |
| 2 | 3 | import { addFilter } from '@wordpress/hooks'; |
| 3 | 4 | import { __ } from '@wordpress/i18n'; |
| 4 | -import { Editor } from './Editor'; | |
| 5 | +import classnames from 'classnames'; | |
| 5 | 6 | import blockConfig from './block.json'; |
| 7 | +import { Edit } from './editor/Edit'; | |
| 6 | 8 | import { BlockFilter } from './editor/components/BlockFilter'; |
| 9 | +import { FooterType } from './editor/components/FooterSelect'; | |
| 10 | +import { HeaderType } from './editor/components/HeaderSelect'; | |
| 11 | +import { SeeMoreType } from './editor/components/SeeMoreSelect'; | |
| 12 | +import { SidebarControls } from './editor/controls/Sidebar'; | |
| 13 | +import { ToolbarControls } from './editor/controls/Toolbar'; | |
| 7 | 14 | import './editor/editor.css'; |
| 8 | -import { transformToCBP, transformFromCBP } from './editor/transforms'; | |
| 9 | 15 | import { BlockOutput } from './front/BlockOutput'; |
| 16 | +import { CopyButton } from './front/CopyButton'; | |
| 10 | 17 | import { blockIcon } from './icons'; |
| 11 | -import { Attributes } from './types'; | |
| 18 | +import { Attributes, Lang } from './types'; | |
| 19 | +import { fontFamilyLong, maybeClamp } from './util/fonts'; | |
| 20 | +import { getMainAlias } from './util/languages'; | |
| 12 | 21 | |
| 13 | 22 | registerBlockType<Attributes>(blockConfig.name, { |
| 14 | 23 | ...blockConfig, |
| 15 | 24 | icon: blockIcon, |
| @@ -39,15 +48,11 @@ | ||
| 39 | 48 | seeMoreType: { type: 'string' }, |
| 40 | 49 | seeMoreString: { type: 'string' }, |
| 41 | 50 | seeMoreAfterLine: { type: 'string' }, |
| 42 | 51 | seeMoreTransition: { type: 'boolean' }, |
| 43 | - seeMoreCollapse: { type: 'boolean' }, | |
| 44 | - seeMoreCollapseString: { type: 'string' }, | |
| 45 | 52 | startingLineNumber: { type: 'string' }, |
| 46 | 53 | lineNumbersWidth: { type: 'number' }, |
| 47 | - highestLineNumber: { type: 'number' }, | |
| 48 | 54 | enableHighlighting: { type: 'boolean' }, |
| 49 | - highlightingHover: { type: 'boolean' }, | |
| 50 | 55 | lineHighlights: { type: 'string' }, |
| 51 | 56 | lineHighlightColor: { type: 'string' }, |
| 52 | 57 | enableBlurring: { type: 'boolean' }, |
| 53 | 58 | lineBlurs: { type: 'string' }, |
| @@ -55,22 +60,8 @@ | ||
| 55 | 60 | frame: { type: 'boolean' }, |
| 56 | 61 | renderType: { type: 'string', default: 'code' }, |
| 57 | 62 | label: { type: 'string', default: '' }, |
| 58 | 63 | 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 | - copyButtonUseTextarea: { type: 'boolean', default: false }, | |
| 69 | - useDecodeURI: { type: 'boolean', default: false }, | |
| 70 | - useEscapeShortCodes: { type: 'boolean', default: false }, | |
| 71 | - tabSize: { type: 'number', default: 2 }, | |
| 72 | - useTabs: { type: 'boolean' }, | |
| 73 | 64 | }, |
| 74 | 65 | // Need to add these here to avoid TS type errors |
| 75 | 66 | supports: { |
| 76 | 67 | html: false, |
| @@ -77,9 +68,66 @@ | ||
| 77 | 68 | align: ['wide', 'full'], |
| 78 | 69 | }, |
| 79 | 70 | title: __('Code Pro', 'code-block-pro'), |
| 80 | 71 | edit: ({ attributes, setAttributes }) => ( |
| 81 | - <Editor attributes={attributes} setAttributes={setAttributes} /> | |
| 72 | + <> | |
| 73 | + <SidebarControls | |
| 74 | + attributes={attributes} | |
| 75 | + setAttributes={setAttributes} | |
| 76 | + /> | |
| 77 | + <ToolbarControls | |
| 78 | + attributes={attributes} | |
| 79 | + setAttributes={setAttributes} | |
| 80 | + /> | |
| 81 | + <div | |
| 82 | + {...blockProps({ | |
| 83 | + className: classnames('code-block-pro-editor', { | |
| 84 | + 'padding-disabled': attributes.disablePadding, | |
| 85 | + 'padding-bottom-disabled': | |
| 86 | + attributes?.footerType && | |
| 87 | + attributes?.footerType !== 'none', | |
| 88 | + 'cbp-has-line-numbers': attributes.lineNumbers, | |
| 89 | + 'cbp-blur-enabled': attributes.enableBlurring, | |
| 90 | + 'cbp-unblur-on-hover': attributes.removeBlurOnHover, | |
| 91 | + }), | |
| 92 | + style: { | |
| 93 | + fontSize: maybeClamp( | |
| 94 | + attributes.fontSize, | |
| 95 | + attributes.clampFonts, | |
| 96 | + ), | |
| 97 | + '--cbp-line-number-color': attributes?.lineNumbers | |
| 98 | + ? attributes.textColor | |
| 99 | + : undefined, | |
| 100 | + '--cbp-line-number-start': | |
| 101 | + Number(attributes?.startingLineNumber) > 1 | |
| 102 | + ? attributes.startingLineNumber | |
| 103 | + : undefined, | |
| 104 | + '--cbp-line-number-width': attributes.lineNumbersWidth | |
| 105 | + ? `${attributes.lineNumbersWidth}px` | |
| 106 | + : undefined, | |
| 107 | + '--cbp-line-highlight-color': | |
| 108 | + attributes?.enableHighlighting | |
| 109 | + ? attributes.lineHighlightColor | |
| 110 | + : undefined, | |
| 111 | + '--cbp-line-height': attributes.lineHeight, | |
| 112 | + // Disabled as ligatures will break the editor line widths | |
| 113 | + // fontFamily: fontFamilyLong(attributes.fontFamily), | |
| 114 | + fontFamily: fontFamilyLong(''), | |
| 115 | + lineHeight: maybeClamp( | |
| 116 | + attributes.lineHeight, | |
| 117 | + attributes.clampFonts, | |
| 118 | + ), | |
| 119 | + }, | |
| 120 | + })}> | |
| 121 | + <HeaderType {...attributes} /> | |
| 122 | + {attributes.copyButton && ( | |
| 123 | + <CopyButton attributes={attributes} /> | |
| 124 | + )} | |
| 125 | + <Edit attributes={attributes} setAttributes={setAttributes} /> | |
| 126 | + <FooterType {...attributes} /> | |
| 127 | + <SeeMoreType {...attributes} /> | |
| 128 | + </div> | |
| 129 | + </> | |
| 82 | 130 | ), |
| 83 | 131 | save: ({ attributes }) => <BlockOutput attributes={attributes} />, |
| 84 | 132 | transforms: { |
| 85 | 133 | from: [ |
| @@ -85,16 +133,22 @@ | ||
| 85 | 133 | from: [ |
| 86 | 134 | { |
| 87 | 135 | type: 'block', |
| 88 | 136 | blocks: ['core/code', 'syntaxhighlighter/code'], |
| 89 | - transform: transformToCBP, | |
| 90 | - }, | |
| 91 | - ], | |
| 92 | - to: [ | |
| 93 | - { | |
| 94 | - type: 'block', | |
| 95 | - blocks: ['core/code'], | |
| 96 | - transform: transformFromCBP, | |
| 137 | + transform: (attrs) => { | |
| 138 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment | |
| 139 | + // @ts-ignore-next-line - Why is this reading the block generic? | |
| 140 | + const { content, language } = attrs; | |
| 141 | + const decode = (value: string) => { | |
| 142 | + const txt = document.createElement('textarea'); | |
| 143 | + txt.innerHTML = value; | |
| 144 | + return txt.value; | |
| 145 | + }; | |
| 146 | + return createBlock(blockConfig.name, { | |
| 147 | + code: content ? decode(content) : undefined, | |
| 148 | + language: getMainAlias(language) as Lang, | |
| 149 | + }); | |
| 150 | + }, | |
| 97 | 151 | }, |
| 98 | 152 | ], |
| 99 | 153 | }, |
| 100 | 154 | }); |