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