PluginProbe
Code Block Pro – Beautiful Syntax Highlighting / 1.10.0
Code Block Pro – Beautiful Syntax Highlighting v1.10.0
1.27.1 1.27.2 1.27.3 1.27.4 1.27.5 1.27.6 1.27.7 1.28.0 1.3.0 1.4.0 1.5.0 1.5.1 1.5.2 1.6.0 1.7.0 1.8.0 1.9.0 1.9.1 1.9.2 1.9.3 trunk 1.1.0 1.10.0 1.11.0 1.11.1 All 63 releases
code-block-pro / src / index.tsx

index.tsx in Code Block Pro – Beautiful Syntax Highlighting 1.10.0, at src/index.tsx

156 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 classnames from 'classnames';
6 import { Lang } from 'shiki';
7 import blockConfig from './block.json';
8 import { Edit } from './editor/Edit';
9 import { BlockFilter } from './editor/components/BlockFilter';
10 import { FooterType } from './editor/components/FooterSelect';
11 import { HeaderType } from './editor/components/HeaderSelect';
12 import { SidebarControls } from './editor/controls/Sidebar';
13 import { ToolbarControls } from './editor/controls/Toolbar';
14 import './editor/editor.css';
15 import { BlockOutput } from './front/BlockOutput';
16 import { CopyButton } from './front/CopyButton';
17 import { blockIcon } from './icons';
18 import { Attributes } from './types';
19 import { fontFamilyLong, maybeClamp } from './util/fonts';
20 import { getMainAlias } from './util/languages';
21
22 registerBlockType<Attributes>(blockConfig.name, {
23 ...blockConfig,
24 icon: blockIcon,
25 // Types seem to be mismatched if importing these from block.json
26 attributes: {
27 code: { type: 'string' },
28 codeHTML: { type: 'string' },
29 language: { type: 'string' },
30 theme: { type: 'string' },
31 align: { type: 'string' },
32 bgColor: { type: 'string', default: '#282a37' },
33 textColor: { type: 'string', default: '#f8f8f2' },
34 fontSize: { type: 'string' },
35 fontFamily: { type: 'string' },
36 lineHeight: { type: 'string' },
37 clampFonts: { type: 'boolean' },
38 lineNumbers: { type: 'boolean' },
39 headerType: { type: 'string' },
40 headerString: { type: 'string' },
41 disablePadding: { type: 'boolean' },
42 footerType: { type: 'string' },
43 footerString: { type: 'string' },
44 footerLink: { type: 'string' },
45 footerLinkTarget: { type: 'boolean' },
46 startingLineNumber: { type: 'string' },
47 lineNumbersWidth: { type: 'number' },
48 enableHighlighting: { type: 'boolean' },
49 lineHighlights: { type: 'string' },
50 lineHighlightColor: { type: 'string' },
51 enableBlurring: { type: 'boolean' },
52 lineBlurs: { type: 'string' },
53 removeBlurOnHover: { type: 'boolean' },
54 frame: { type: 'boolean' },
55 renderType: { type: 'string', default: 'code' },
56 label: { type: 'string', default: '' },
57 copyButton: { type: 'boolean' },
58 },
59 // Need to add these here to avoid TS type errors
60 supports: {
61 html: false,
62 align: ['wide', 'full'],
63 },
64 title: __('Code Pro', 'code-block-pro'),
65 edit: ({ attributes, setAttributes }) => (
66 <>
67 <SidebarControls
68 attributes={attributes}
69 setAttributes={setAttributes}
70 />
71 <ToolbarControls
72 attributes={attributes}
73 setAttributes={setAttributes}
74 />
75 <div
76 {...blockProps({
77 className: classnames('code-block-pro-editor', {
78 'padding-disabled': attributes.disablePadding,
79 'padding-bottom-disabled':
80 attributes?.footerType &&
81 attributes?.footerType !== 'none',
82 'cbp-has-line-numbers': attributes.lineNumbers,
83 'cbp-blur-enabled': attributes.enableBlurring,
84 'cbp-unblur-on-hover': attributes.removeBlurOnHover,
85 }),
86 style: {
87 fontSize: maybeClamp(
88 attributes.fontSize,
89 attributes.clampFonts,
90 ),
91 '--cbp-line-number-color': attributes?.lineNumbers
92 ? attributes.textColor
93 : undefined,
94 '--cbp-line-number-start':
95 Number(attributes?.startingLineNumber) > 1
96 ? attributes.startingLineNumber
97 : undefined,
98 '--cbp-line-number-width': attributes.lineNumbersWidth
99 ? `${attributes.lineNumbersWidth}px`
100 : undefined,
101 '--cbp-line-highlight-color':
102 attributes?.enableHighlighting
103 ? attributes.lineHighlightColor
104 : undefined,
105 '--cbp-line-height': attributes.lineHeight,
106 fontFamily: fontFamilyLong(attributes.fontFamily),
107 lineHeight: maybeClamp(
108 attributes.lineHeight,
109 attributes.clampFonts,
110 ),
111 },
112 })}>
113 <HeaderType {...attributes} />
114 {attributes.copyButton && (
115 <CopyButton attributes={attributes} />
116 )}
117 <Edit attributes={attributes} setAttributes={setAttributes} />
118 <FooterType {...attributes} />
119 </div>
120 </>
121 ),
122 save: ({ attributes }) => <BlockOutput attributes={attributes} />,
123 transforms: {
124 from: [
125 {
126 type: 'block',
127 blocks: ['core/code', 'syntaxhighlighter/code'],
128 transform: (attrs) => {
129 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
130 // @ts-ignore-next-line - Why is this reading the block generic?
131 const { content, language } = attrs;
132 const decode = (value: string) => {
133 const txt = document.createElement('textarea');
134 txt.innerHTML = value;
135 return txt.value;
136 };
137 return createBlock(blockConfig.name, {
138 code: content ? decode(content) : undefined,
139 language: getMainAlias(language) as Lang,
140 });
141 },
142 },
143 ],
144 },
145 });
146
147 addFilter(
148 'editor.BlockEdit',
149 blockConfig.name,
150 (CurrentMenuItems) =>
151 // Not sure how to type these incoming props
152 // eslint-disable-next-line
153 (props: any) =>
154 BlockFilter(CurrentMenuItems, props),
155 );
156