PluginProbe
Code Block Pro – Beautiful Syntax Highlighting / 1.28.0
Code Block Pro – Beautiful Syntax Highlighting v1.28.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 / ThemeSelect.tsx

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

135 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { BaseControl, ExternalLink } from '@wordpress/components';
2 import { applyFilters } from '@wordpress/hooks';
3 import { __, sprintf } from '@wordpress/i18n';
4 import type { Theme } from 'shiki';
5 import defaultThemes from '../../defaultThemes.json';
6 import { useSettingsStore } from '../../state/settings';
7 import type { CustomStyles, HelpUrl, Lang, ThemeOption } from '../../types';
8 import { getPriorityThemes } from '../../util/themes';
9 import { ThemePreview } from '../components/ThemePreview';
10
11 type ThemeSelectProps = {
12 theme: Theme;
13 language: Lang;
14 code: string;
15 fontSize: string;
16 lineHeight: string;
17 fontFamily: string;
18 search: string;
19 clampFonts: boolean;
20 tabSize: number;
21 onClick: (slug: Theme) => void;
22 };
23 export const ThemeSelect = (props: ThemeSelectProps) => {
24 const { hiddenThemes } = useSettingsStore();
25 const themes = applyFilters(
26 'blocks.codeBlockPro.themes',
27 defaultThemes,
28 ) as ThemeOption;
29 const themesSorted = Object.entries(themes)
30 .filter(([slug]) => !hiddenThemes.includes(slug))
31 .filter(([slug]) => slug.includes(props.search.toLocaleLowerCase()))
32 .map(([slug, data]) => ({ slug, ...data }))
33 // Put priority themes at the top
34 .sort((a, b) => (a.priority === b.priority ? 0 : a.priority ? -1 : 1))
35 // then put custom themes above that
36 .sort((a, b) => (a.custom === b.custom ? 0 : a.custom ? -1 : 1));
37
38 const priorityThemes = getPriorityThemes();
39
40 return (
41 <div className="code-block-pro-editor" style={{ tabSize: props.tabSize }}>
42 {props.search?.length > 0 ? (
43 <BaseControl id="add-on-themes">
44 {priorityThemes?.length > 0 ? null : (
45 <div className="font-semibold text-base">
46 <ExternalLink href="https://code-block-pro.com">
47 {__('Get 25+ more themes here', 'code-block-pro')}
48 </ExternalLink>
49 </div>
50 )}
51 </BaseControl>
52 ) : null}
53 {themesSorted.slice(0, 9).map((data) => (
54 <ThemeItem key={data.slug} {...data} {...props} />
55 ))}
56 {props.search?.length > 0 ? null : (
57 <BaseControl id="add-on-themes">
58 {priorityThemes?.length > 0 ? null : (
59 <div className="font-semibold text-base">
60 <ExternalLink href="https://code-block-pro.com/themes">
61 {__('Get 25+ more themes here', 'code-block-pro')}
62 </ExternalLink>
63 </div>
64 )}
65 </BaseControl>
66 )}
67 {themesSorted.slice(9).map((data) => (
68 <ThemeItem key={data.slug} {...data} {...props} />
69 ))}
70 {props.search?.length > 0 ? null : (
71 <BaseControl id="add-on-themes">
72 {priorityThemes?.length > 0 ? null : (
73 <div className="font-semibold text-base">
74 <ExternalLink href="https://code-block-pro.com/themes">
75 {__('Get 25+ more themes here', 'code-block-pro')}
76 </ExternalLink>
77 </div>
78 )}
79 </BaseControl>
80 )}
81 </div>
82 );
83 };
84
85 const ThemeItem = ({
86 slug,
87 name,
88 theme,
89 language,
90 code,
91 fontSize,
92 lineHeight,
93 fontFamily,
94 clampFonts,
95 onClick,
96 styles,
97 helpUrl,
98 }: {
99 slug: string;
100 name: string;
101 styles?: CustomStyles;
102 helpUrl?: HelpUrl;
103 } & ThemeSelectProps) => (
104 <BaseControl
105 id={`code-block-pro-theme-${slug}`}
106 label={
107 theme === slug
108 ? sprintf(__('%s (current)', 'code-block-pro'), name)
109 : name
110 }
111 >
112 <ThemePreview
113 id={`code-block-pro-theme-${slug}`}
114 theme={slug as Theme}
115 lang={language}
116 fontSize={fontSize}
117 lineHeight={lineHeight}
118 fontFamily={fontFamily}
119 clampFonts={clampFonts}
120 styles={styles}
121 onClick={() => {
122 onClick(slug as Theme);
123 }}
124 code={code}
125 />
126 {helpUrl?.url ? (
127 <div className="mt-2">
128 <ExternalLink href={helpUrl.url}>
129 {helpUrl?.text || __('Read more', 'code-block-pro')}
130 </ExternalLink>
131 </div>
132 ) : null}
133 </BaseControl>
134 );
135