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

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