PluginProbe
Code Block Pro – Beautiful Syntax Highlighting / 1.1.0
Code Block Pro – Beautiful Syntax Highlighting v1.1.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.1.0, at src/editor/Edit.tsx

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