PluginProbe
Code Block Pro – Beautiful Syntax Highlighting / 1.4.0
Code Block Pro – Beautiful Syntax Highlighting v1.4.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 / components / ThemePreview.tsx

ThemePreview.tsx in Code Block Pro – Beautiful Syntax Highlighting 1.4.0, at src/editor/components/ThemePreview.tsx

91 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { useEffect, useRef, useState } from '@wordpress/element';
2 import { __ } from '@wordpress/i18n';
3 import { Lang, Theme } from 'shiki';
4 import { useTheme } from '../../hooks/useTheme';
5
6 type ThemePreviewProps = {
7 id: string;
8 theme: Theme;
9 lang: Lang;
10 code: string;
11 onClick: () => void;
12 };
13 export const ThemePreview = ({
14 id,
15 theme,
16 lang,
17 onClick,
18 code,
19 }: ThemePreviewProps) => {
20 const [inView, setInView] = useState(false);
21 const { highlighter, error, loading } = useTheme({
22 theme,
23 // If no code is written yet, show a classic Carmack snippet
24 lang: code ? lang : 'c',
25 ready: inView,
26 });
27 const [codeRendered, setCode] = useState('');
28 const [backgroundColor, setBg] = useState('#ffffff');
29 const observer = useRef<IntersectionObserver>(
30 new IntersectionObserver(([entry]) => {
31 if (entry.isIntersecting) {
32 setInView(true);
33 observer.current.disconnect();
34 }
35 }),
36 );
37 const codeSnippet = `
38 float Q_rsqrt( float number )
39 {
40 long i;
41 float x2, y;
42 const float threehalfs = 1.5F;
43
44 x2 = number * 0.5F;
45 y = number;
46 i = * ( long * ) &y; // evil floating point bit level hacking
47 i = 0x5f3759df - ( i >> 1 ); // what the frack?
48 y = * ( float * ) &i;
49 y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
50 // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
51
52 return y;
53 }`.trim();
54
55 useEffect(() => {
56 if (!highlighter) return;
57 const hl = code
58 ? highlighter.codeToHtml(code, { lang })
59 : highlighter.codeToHtml(codeSnippet, { lang: 'c' });
60 setCode(hl);
61 setBg(highlighter.getBackgroundColor());
62 }, [highlighter, lang, code, codeSnippet]);
63
64 return (
65 <button
66 id={id}
67 type="button"
68 onClick={onClick}
69 className="p-4 px-3 border flex items-start w-full text-left outline-none cursor-pointer no-underline ring-offset-2 ring-offset-white focus:shadow-none focus:ring-wp overflow-x-scroll"
70 style={{ backgroundColor, minHeight: '50px' }}>
71 {loading || error || !inView ? (
72 <span
73 id={id}
74 ref={(el) => {
75 if (!el) return;
76 observer.current.observe(el);
77 }}
78 style={{ minHeight: '200px' }}
79 className="flex items-center justify-center p-6 bg-gray-50">
80 {error?.message || __('Loading...', 'code-block-pro')}
81 </span>
82 ) : (
83 <span
84 className="pointer-events-none"
85 dangerouslySetInnerHTML={{ __html: codeRendered }}
86 />
87 )}
88 </button>
89 );
90 };
91