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 / components / ThemePreview.tsx

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

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