PluginProbe
Code Block Pro – Beautiful Syntax Highlighting / 1.8.0
Code Block Pro – Beautiful Syntax Highlighting v1.8.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.8.0, at src/index.tsx

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