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

200 lines 8.7 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, applyFilters } from '@wordpress/hooks';
4 import { __ } from '@wordpress/i18n';
5 import classnames from 'classnames';
6 import blockConfig from './block.json';
7 import defaultThemes from './defaultThemes.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 { SeeMoreType } from './editor/components/SeeMoreSelect';
13 import { ButtonList } from './editor/components/buttons/ButtonList';
14 import { SidebarControls } from './editor/controls/Sidebar';
15 import { ToolbarControls } from './editor/controls/Toolbar';
16 import './editor/editor.css';
17 import { BlockOutput } from './front/BlockOutput';
18 import { blockIcon } from './icons';
19 import { Attributes, Lang, ThemeOption } from './types';
20 import { findLineNumberColor } from './util/colors';
21 import { fontFamilyLong, maybeClamp } from './util/fonts';
22 import { getMainAlias } from './util/languages';
23
24 registerBlockType<Attributes>(blockConfig.name, {
25 ...blockConfig,
26 icon: blockIcon,
27 // Types seem to be mismatched if importing these from block.json
28 attributes: {
29 code: { type: 'string' },
30 codeHTML: { type: 'string' },
31 language: { type: 'string' },
32 theme: { type: 'string' },
33 align: { type: 'string' },
34 bgColor: { type: 'string', default: '#282a37' },
35 textColor: { type: 'string', default: '#f8f8f2' },
36 fontSize: { type: 'string' },
37 fontFamily: { type: 'string' },
38 lineHeight: { type: 'string' },
39 clampFonts: { type: 'boolean' },
40 editorHeight: { type: 'string' },
41 lineNumbers: { type: 'boolean' },
42 headerType: { type: 'string' },
43 headerString: { type: 'string' },
44 disablePadding: { type: 'boolean' },
45 footerType: { type: 'string' },
46 footerString: { type: 'string' },
47 footerLink: { type: 'string' },
48 footerLinkTarget: { type: 'boolean' },
49 enableMaxHeight: { type: 'boolean' },
50 seeMoreType: { type: 'string' },
51 seeMoreString: { type: 'string' },
52 seeMoreAfterLine: { type: 'string' },
53 seeMoreTransition: { type: 'boolean' },
54 startingLineNumber: { type: 'string' },
55 lineNumbersWidth: { type: 'number' },
56 enableHighlighting: { type: 'boolean' },
57 highlightingHover: { type: 'boolean' },
58 lineHighlights: { type: 'string' },
59 lineHighlightColor: { type: 'string' },
60 enableBlurring: { type: 'boolean' },
61 lineBlurs: { type: 'string' },
62 removeBlurOnHover: { type: 'boolean' },
63 frame: { type: 'boolean' },
64 renderType: { type: 'string', default: 'code' },
65 label: { type: 'string', default: '' },
66 copyButton: { type: 'boolean' },
67 buttonTheme: { type: 'string' },
68 useDecodeURI: { type: 'boolean', default: false },
69 },
70 // Need to add these here to avoid TS type errors
71 supports: {
72 html: false,
73 align: ['wide', 'full'],
74 },
75 title: __('Code Pro', 'code-block-pro'),
76 edit: ({ attributes, setAttributes }) => {
77 // Restricts users without unfiltered HTML access
78 const hasPermission = window.codeBlockPro.canSaveHtml;
79 setAttributes = hasPermission ? setAttributes : () => undefined;
80
81 // To add custom styles
82 const themes = applyFilters(
83 'blocks.codeBlockPro.themes',
84 defaultThemes,
85 ) as ThemeOption;
86 const styles = themes[attributes.theme]?.styles;
87 return (
88 <>
89 <SidebarControls
90 attributes={attributes}
91 canEdit={hasPermission}
92 setAttributes={setAttributes}
93 />
94 <ToolbarControls
95 attributes={attributes}
96 setAttributes={setAttributes}
97 />
98 <div
99 {...blockProps({
100 className: classnames('code-block-pro-editor', {
101 'padding-disabled': attributes.disablePadding,
102 'padding-bottom-disabled':
103 attributes?.footerType &&
104 attributes?.footerType !== 'none',
105 'cbp-has-line-numbers': attributes.lineNumbers,
106 'cbp-blur-enabled': attributes.enableBlurring,
107 'cbp-unblur-on-hover': attributes.removeBlurOnHover,
108 'cbp-highlight-hover': attributes.highlightingHover,
109 }),
110 style: {
111 fontSize: maybeClamp(
112 attributes.fontSize,
113 attributes.clampFonts,
114 ),
115 '--cbp-line-number-color': attributes?.lineNumbers
116 ? findLineNumberColor(attributes)
117 : undefined,
118 '--cbp-line-number-start':
119 Number(attributes?.startingLineNumber) > 1
120 ? attributes.startingLineNumber
121 : undefined,
122 '--cbp-line-number-width':
123 attributes.lineNumbersWidth
124 ? `${attributes.lineNumbersWidth}px`
125 : undefined,
126 '--cbp-line-highlight-color':
127 attributes?.enableHighlighting ||
128 attributes?.highlightingHover
129 ? attributes.lineHighlightColor
130 : undefined,
131 '--cbp-line-height': attributes.lineHeight,
132 // Disabled as ligatures will break the editor line widths
133 // fontFamily: fontFamilyLong(attributes.fontFamily),
134 fontFamily: fontFamilyLong(''),
135 lineHeight: maybeClamp(
136 attributes.lineHeight,
137 attributes.clampFonts,
138 ),
139 ...Object.entries(styles ?? {}).reduce(
140 (acc, [key, value]) => ({
141 ...acc,
142 [`--shiki-${key}`]: value,
143 }),
144 {},
145 ),
146 ...(applyFilters(
147 'blocks.codeBlockPro.additionalEditorAttributes',
148 {},
149 attributes,
150 ) as object),
151 },
152 })}>
153 <HeaderType {...attributes} />
154 <ButtonList {...attributes} />
155 <Edit
156 attributes={attributes}
157 setAttributes={setAttributes}
158 canEdit={hasPermission}
159 />
160 <FooterType {...attributes} />
161 <SeeMoreType {...attributes} />
162 </div>
163 </>
164 );
165 },
166 save: ({ attributes }) => <BlockOutput attributes={attributes} />,
167 transforms: {
168 from: [
169 {
170 type: 'block',
171 blocks: ['core/code', 'syntaxhighlighter/code'],
172 transform: (attrs) => {
173 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
174 // @ts-ignore-next-line - Why is this reading the block generic?
175 const { content, language } = attrs;
176 const decode = (value: string) => {
177 const txt = document.createElement('textarea');
178 txt.innerHTML = value;
179 return txt.value;
180 };
181 return createBlock(blockConfig.name, {
182 code: content ? decode(content) : undefined,
183 language: getMainAlias(language) as Lang,
184 });
185 },
186 },
187 ],
188 },
189 });
190
191 addFilter(
192 'editor.BlockEdit',
193 blockConfig.name,
194 (CurrentMenuItems) =>
195 // Not sure how to type these incoming props
196 // eslint-disable-next-line
197 (props: any) =>
198 BlockFilter(CurrentMenuItems, props),
199 );
200