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

201 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 copyButtonType: { type: 'string' },
68 useDecodeURI: { type: 'boolean', default: false },
69 tabSize: { type: 'number', default: 2 },
70 },
71 // Need to add these here to avoid TS type errors
72 supports: {
73 html: false,
74 align: ['wide', 'full'],
75 },
76 title: __('Code Pro', 'code-block-pro'),
77 edit: ({ attributes, setAttributes }) => {
78 // Restricts users without unfiltered HTML access
79 const hasPermission = window.codeBlockPro.canSaveHtml;
80 setAttributes = hasPermission ? setAttributes : () => undefined;
81
82 // To add custom styles
83 const themes = applyFilters(
84 'blocks.codeBlockPro.themes',
85 defaultThemes,
86 ) as ThemeOption;
87 const styles = themes[attributes.theme]?.styles;
88 return (
89 <>
90 <SidebarControls
91 attributes={attributes}
92 canEdit={hasPermission}
93 setAttributes={setAttributes}
94 />
95 <ToolbarControls
96 attributes={attributes}
97 setAttributes={setAttributes}
98 />
99 <div
100 {...blockProps({
101 className: classnames('code-block-pro-editor', {
102 'padding-disabled': attributes.disablePadding,
103 'padding-bottom-disabled':
104 attributes?.footerType &&
105 attributes?.footerType !== 'none',
106 'cbp-has-line-numbers': attributes.lineNumbers,
107 'cbp-blur-enabled': attributes.enableBlurring,
108 'cbp-unblur-on-hover': attributes.removeBlurOnHover,
109 'cbp-highlight-hover': attributes.highlightingHover,
110 }),
111 style: {
112 fontSize: maybeClamp(
113 attributes.fontSize,
114 attributes.clampFonts,
115 ),
116 '--cbp-line-number-color': attributes?.lineNumbers
117 ? findLineNumberColor(attributes)
118 : undefined,
119 '--cbp-line-number-start':
120 Number(attributes?.startingLineNumber) > 1
121 ? attributes.startingLineNumber
122 : undefined,
123 '--cbp-line-number-width':
124 attributes.lineNumbersWidth
125 ? `${attributes.lineNumbersWidth}px`
126 : undefined,
127 '--cbp-line-highlight-color':
128 attributes?.enableHighlighting ||
129 attributes?.highlightingHover
130 ? attributes.lineHighlightColor
131 : undefined,
132 '--cbp-line-height': attributes.lineHeight,
133 // Disabled as ligatures will break the editor line widths
134 // fontFamily: fontFamilyLong(attributes.fontFamily),
135 fontFamily: fontFamilyLong(''),
136 lineHeight: maybeClamp(
137 attributes.lineHeight,
138 attributes.clampFonts,
139 ),
140 ...Object.entries(styles ?? {}).reduce(
141 (acc, [key, value]) => ({
142 ...acc,
143 [`--shiki-${key}`]: value,
144 }),
145 {},
146 ),
147 ...(applyFilters(
148 'blocks.codeBlockPro.additionalEditorAttributes',
149 {},
150 attributes,
151 ) as object),
152 },
153 })}>
154 <HeaderType {...attributes} />
155 <ButtonList {...attributes} />
156 <Edit
157 attributes={attributes}
158 setAttributes={setAttributes}
159 canEdit={hasPermission}
160 />
161 <FooterType {...attributes} />
162 <SeeMoreType {...attributes} />
163 </div>
164 </>
165 );
166 },
167 save: ({ attributes }) => <BlockOutput attributes={attributes} />,
168 transforms: {
169 from: [
170 {
171 type: 'block',
172 blocks: ['core/code', 'syntaxhighlighter/code'],
173 transform: (attrs) => {
174 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
175 // @ts-ignore-next-line - Why is this reading the block generic?
176 const { content, language } = attrs;
177 const decode = (value: string) => {
178 const txt = document.createElement('textarea');
179 txt.innerHTML = value;
180 return txt.value;
181 };
182 return createBlock(blockConfig.name, {
183 code: content ? decode(content) : undefined,
184 language: getMainAlias(language) as Lang,
185 });
186 },
187 },
188 ],
189 },
190 });
191
192 addFilter(
193 'editor.BlockEdit',
194 blockConfig.name,
195 (CurrentMenuItems) =>
196 // Not sure how to type these incoming props
197 // eslint-disable-next-line
198 (props: any) =>
199 BlockFilter(CurrentMenuItems, props),
200 );
201