Skip to content

Commit 782acf6

Browse files
feat(app): change syntax highlight with locked bg (#477)
* feat(app): change syntax highlight with locked bg * feat(app): improve code and fix theme effect * feat(app): refactor dispatchUpdateTHeme with rxjs * Create .changeset/early-lamps-cover.md * feat(app): refactor dispatchUpdateTheme with promise * feat(app): change syntax highlight with locked bg * feat(app): improve code and fix theme effect * feat(app): refactor dispatchUpdateTHeme with rxjs * Create .changeset/early-lamps-cover.md * feat(app): refactor dispatchUpdateTheme with promise
1 parent ff6dfdc commit 782acf6

5 files changed

Lines changed: 79 additions & 9 deletions

File tree

.changeset/early-lamps-cover.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@codeimage/app": minor
3+
---
4+
5+
feat(app): change syntax highlight with locked bg

apps/codeimage/src/components/PropertyEditor/EditorStyleForm.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import {SUPPORTED_LANGUAGES} from '@codeimage/config';
2+
import {CustomTheme} from '@codeimage/highlight';
23
import {useI18n} from '@codeimage/locale';
34
import {getRootEditorStore} from '@codeimage/store/editor';
45
import {getActiveEditorStore} from '@codeimage/store/editor/activeEditor';
6+
import {dispatchUpdateTheme} from '@codeimage/store/effects/onThemeChange';
7+
import {getThemeStore} from '@codeimage/store/theme/theme.store';
58
import {SegmentedField, Select, Text} from '@codeimage/ui';
69
import {SUPPORTED_FONTS} from '@core/configuration/font';
710
import {getUmami} from '@core/constants/umami';
@@ -14,7 +17,18 @@ import {PanelRow, TwoColumnPanelRow} from './PanelRow';
1417
import {SuspenseEditorItem} from './SuspenseEditorItem';
1518

1619
export const EditorStyleForm: ParentComponent = () => {
20+
const {themeArray} = getThemeStore();
1721
const languages = SUPPORTED_LANGUAGES;
22+
23+
const themeItems = () =>
24+
themeArray()
25+
.map(theme => theme())
26+
.filter((theme): theme is CustomTheme => !!theme)
27+
.map(theme => ({
28+
label: theme.properties.label,
29+
value: theme.id,
30+
}));
31+
1832
const fonts = SUPPORTED_FONTS;
1933
const modality = useModality();
2034
const [t] = useI18n<AppLocaleEntries>();
@@ -69,6 +83,28 @@ export const EditorStyleForm: ParentComponent = () => {
6983
</TwoColumnPanelRow>
7084
</PanelRow>
7185

86+
<PanelRow for={'frameLanguageField'} label={t('frame.theme')}>
87+
<TwoColumnPanelRow>
88+
<SuspenseEditorItem
89+
fallback={<SkeletonLine width={'100%'} height={'26px'} />}
90+
>
91+
<Select
92+
id={'frameSyntaxHighlightField'}
93+
multiple={false}
94+
native={modality === 'mobile'}
95+
items={themeItems()}
96+
value={state.options.themeId}
97+
onSelectChange={theme => {
98+
dispatchUpdateTheme({
99+
updateBackground: false,
100+
theme,
101+
});
102+
}}
103+
/>
104+
</SuspenseEditorItem>
105+
</TwoColumnPanelRow>
106+
</PanelRow>
107+
72108
<PanelRow
73109
for={'frameLineNumbersField'}
74110
label={t('frame.lineNumbers')}

apps/codeimage/src/i18n/sidebar.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default {
1616
language: 'Language',
1717
lineNumbers: 'Line number',
1818
font: 'Font',
19+
theme: 'Theme',
1920
fontWeight: 'Font weight',
2021
reflection: 'Reflection',
2122
backgroundType: 'Background',
@@ -41,6 +42,7 @@ export default {
4142
reflection: 'Riflesso',
4243
watermark: 'Watermark',
4344
backgroundType: 'Tipo background',
45+
theme: 'Tema',
4446
},
4547
},
4648
de: {
@@ -63,6 +65,7 @@ export default {
6365
reflection: 'Reflektion',
6466
watermark: 'Wasserzeichen',
6567
backgroundType: 'Hintergrundtyp',
68+
theme: 'Theme',
6669
},
6770
},
6871
es: {
@@ -85,6 +88,7 @@ export default {
8588
reflection: 'Reflexión',
8689
watermark: 'filigrana',
8790
backgroundType: 'Tipo de fondo',
91+
theme: 'Theme',
8892
},
8993
},
9094
} as const;

apps/codeimage/src/state/effects/onThemeChange.tsx

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {effect} from '@codeimage/atomic-state';
12
import {AVAILABLE_COLORS, AVAILABLE_GRADIENTS} from '@codeimage/config';
23
import {CustomTheme} from '@codeimage/highlight';
34
import {getRootEditorStore} from '@codeimage/store/editor';
@@ -6,21 +7,42 @@ import {getTerminalState} from '@codeimage/store/editor/terminal';
67
import {getThemeStore} from '@codeimage/store/theme/theme.store';
78
import {TERMINAL_SHADOWS} from '@core/configuration/shadow';
89
import {AVAILABLE_TERMINAL_THEMES} from '@core/configuration/terminal-themes';
10+
import {getUmami} from '@core/constants/umami';
911
import {pipe, tap} from 'rxjs';
1012
import {batch} from 'solid-js';
11-
import {effect} from '@codeimage/atomic-state';
1213

13-
export type DispatchUpdateThemeParams = {theme: CustomTheme};
14+
export type DispatchUpdateThemeParams = {
15+
theme: CustomTheme | string;
16+
updateBackground?: boolean;
17+
};
1418

15-
export function dispatchUpdateTheme({theme}: DispatchUpdateThemeParams): void {
19+
export function dispatchUpdateTheme(params: DispatchUpdateThemeParams): void {
20+
const {theme, updateBackground} = params;
1621
const frame = getFrameState();
1722
const terminal = getTerminalState();
1823
const editor = getRootEditorStore();
19-
batch(() => {
20-
frame.setBackground(theme.properties.previewBackground);
21-
terminal.setState('background', theme.properties.terminal.main);
22-
terminal.setState('textColor', theme.properties.terminal.text);
23-
editor.actions.setThemeId(theme.id);
24+
const {getThemeDef} = getThemeStore();
25+
26+
const resolvedTheme = new Promise<CustomTheme | undefined>(r => {
27+
if (typeof theme === 'string') {
28+
return getThemeDef(theme)?.load().then(r);
29+
} else {
30+
return r(theme);
31+
}
32+
});
33+
34+
resolvedTheme.then(theme => {
35+
if (theme) {
36+
batch(() => {
37+
if (updateBackground) {
38+
frame.setBackground(theme.properties.previewBackground);
39+
}
40+
terminal.setState('background', theme.properties.terminal.main);
41+
terminal.setState('textColor', theme.properties.terminal.text);
42+
editor.actions.setThemeId(theme.id);
43+
});
44+
getUmami().trackEvent(theme.id, `theme-change`);
45+
}
2446
});
2547
}
2648

apps/codeimage/src/state/theme/theme.store.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import {CustomTheme} from '@codeimage/highlight';
12
import {THEME_REGISTRY} from '@codeimage/store/theme/themeRegistry';
23
import {
34
createMemo,
45
createResource,
56
createRoot,
67
createSignal,
78
mapArray,
9+
ResourceReturn,
810
} from 'solid-js';
911

1012
function $getThemeStore() {
@@ -25,7 +27,8 @@ function $getThemeStore() {
2527
themeArray().some(theme => theme.loading),
2628
);
2729

28-
const getThemeResource = (themeId: string) => themes[themeId];
30+
const getThemeResource = (themeId: string): ResourceReturn<CustomTheme> =>
31+
themes[themeId];
2932

3033
const getThemeDef = (id: string) =>
3134
THEME_REGISTRY.find(theme => theme.id === id);

0 commit comments

Comments
 (0)