PluginProbe
Code Block Pro – Beautiful Syntax Highlighting / 1.3.0
Code Block Pro – Beautiful Syntax Highlighting v1.3.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 / editor / Edit.tsx

Edit.tsx in Code Block Pro – Beautiful Syntax Highlighting 1.3.0, at src/editor/Edit.tsx

119 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { useEffect, useRef } from '@wordpress/element';
2 import { applyFilters } from '@wordpress/hooks';
3 import Editor from 'react-simple-code-editor';
4 import { Theme } from 'shiki';
5 import { useTheme } from '../hooks/useTheme';
6 import { useGlobalStore } from '../state/global';
7 import { useLanguageStore } from '../state/language';
8 import { useThemeStore } from '../state/theme';
9 import { AttributesPropsAndSetter } from '../types';
10
11 export const Edit = ({
12 attributes,
13 setAttributes,
14 }: AttributesPropsAndSetter) => {
15 const {
16 language,
17 theme,
18 code = '',
19 bgColor: backgroundColor,
20 textColor: color,
21 fontSize,
22 lineHeight,
23 } = attributes;
24 const textAreaRef = useRef<HTMLDivElement>(null);
25 const handleChange = (code: string) => setAttributes({ code });
26 const { previousLanguage } = useLanguageStore();
27 const { previousSettings } = useGlobalStore();
28 const { previousTheme, previousFontSize, previousLineHeight } =
29 useThemeStore();
30 const { highlighter, error, loading } = useTheme({
31 theme,
32 lang: language ?? previousLanguage,
33 });
34
35 useEffect(() => {
36 if (!attributes.copyButton) {
37 setAttributes({ copyButton: previousSettings.copyButton });
38 }
39 }, [previousSettings, attributes, setAttributes]);
40
41 useEffect(() => {
42 if (theme || !previousTheme) return;
43 setAttributes({ theme: previousTheme as Theme });
44 }, [previousTheme, theme, setAttributes]);
45
46 useEffect(() => {
47 if (fontSize || !previousFontSize) return;
48 setAttributes({ fontSize: previousFontSize });
49 }, [previousFontSize, fontSize, setAttributes]);
50
51 useEffect(() => {
52 if (lineHeight || !previousLineHeight) return;
53 setAttributes({ lineHeight: previousLineHeight });
54 }, [previousLineHeight, lineHeight, setAttributes]);
55
56 useEffect(() => {
57 if (!highlighter) return;
58 setAttributes({
59 bgColor: highlighter.getBackgroundColor(),
60 textColor: highlighter.getForegroundColor(),
61 });
62 }, [theme, highlighter, setAttributes]);
63
64 useEffect(() => {
65 if (!highlighter) return;
66 // applyFilters()
67 setAttributes({
68 codeHTML: applyFilters(
69 'blocks.codeBlockPro.codeHTML',
70 highlighter.codeToHtml(code, {
71 lang: language ?? previousLanguage,
72 }),
73 attributes,
74 ) as string,
75 });
76 }, [
77 highlighter,
78 code,
79 language,
80 setAttributes,
81 previousLanguage,
82 attributes,
83 ]);
84
85 if ((loading && code) || error) {
86 return (
87 <div
88 className="p-8 px-6 text-left"
89 style={{ backgroundColor, color }}>
90 {error?.message ?? ''}
91 </div>
92 );
93 }
94
95 return (
96 <div ref={textAreaRef}>
97 <Editor
98 value={code}
99 onValueChange={handleChange}
100 padding={24}
101 style={{ backgroundColor, color }}
102 // eslint-disable-next-line
103 onKeyDown={(e: any) =>
104 e.key === 'Tab' &&
105 // Tab lock here. Pressing Escape will unlock.
106 textAreaRef.current?.querySelector('textarea')?.focus()
107 }
108 highlight={(code: string) =>
109 highlighter
110 ?.codeToHtml(code, {
111 lang: language ?? previousLanguage,
112 })
113 ?.replace(/<\/?[pre|code][^>]*>/g, '')
114 }
115 />
116 </div>
117 );
118 };
119