Skip to content

Commit 28d5da1

Browse files
committed
Fix #22 frozen themes again
1 parent 47e6e04 commit 28d5da1

File tree

9 files changed

+217
-39
lines changed

9 files changed

+217
-39
lines changed

.changeset/mean-countries-sparkle.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@code-hike/lighter": patch
3+
---
4+
5+
Fix frozen themes again

lib/dist/browser.esm.mjs

+15-9
Original file line numberDiff line numberDiff line change
@@ -266,28 +266,34 @@ async function reallyLoadThemeByName(name) {
266266
}
267267
}
268268
function toFinalTheme(theme) {
269-
var _a, _b;
270269
if (!theme) {
271270
return undefined;
272271
}
273-
const finalTheme = Object.assign(Object.assign({}, theme), { name: theme.name || "unknown-theme", type: getColorScheme(theme), settings: ((_a = theme.settings) === null || _a === void 0 ? void 0 : _a.slice()) || ((_b = theme.tokenColors) === null || _b === void 0 ? void 0 : _b.slice()) || [], colors: theme.colors || {} });
272+
const finalTheme = Object.assign(Object.assign({}, theme), { name: theme.name || "unknown-theme", type: getColorScheme(theme), settings: theme.settings || theme.tokenColors || [], colors: theme.colors || {} });
274273
const globalSetting = finalTheme.settings.find((s) => !s.name && !s.scope);
275274
if (globalSetting) {
276275
const { foreground, background } = globalSetting.settings || {};
276+
const newColors = {};
277277
if (foreground && !finalTheme.colors["editor.foreground"]) {
278-
finalTheme.colors["editor.foreground"] = foreground;
278+
newColors["editor.foreground"] = foreground;
279279
}
280280
if (background && !finalTheme.colors["editor.background"]) {
281-
finalTheme.colors["editor.background"] = background;
281+
newColors["editor.background"] = background;
282+
}
283+
if (Object.keys(newColors).length > 0) {
284+
finalTheme.colors = Object.assign(Object.assign({}, finalTheme.colors), newColors);
282285
}
283286
}
284287
if (!globalSetting) {
285-
finalTheme.settings.unshift({
286-
settings: {
287-
foreground: getColor(finalTheme, "editor.foreground"),
288-
background: getColor(finalTheme, "editor.background"),
288+
finalTheme.settings = [
289+
{
290+
settings: {
291+
foreground: getColor(finalTheme, "editor.foreground"),
292+
background: getColor(finalTheme, "editor.background"),
293+
},
289294
},
290-
});
295+
...finalTheme.settings,
296+
];
291297
}
292298
return finalTheme;
293299
}

lib/dist/index.cjs.js

+15-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/dist/index.esm.mjs

+15-9
Original file line numberDiff line numberDiff line change
@@ -614,28 +614,34 @@ async function reallyLoadThemeByName(name) {
614614
}
615615
}
616616
function toFinalTheme(theme) {
617-
var _a, _b;
618617
if (!theme) {
619618
return undefined;
620619
}
621-
const finalTheme = Object.assign(Object.assign({}, theme), { name: theme.name || "unknown-theme", type: getColorScheme(theme), settings: ((_a = theme.settings) === null || _a === void 0 ? void 0 : _a.slice()) || ((_b = theme.tokenColors) === null || _b === void 0 ? void 0 : _b.slice()) || [], colors: theme.colors || {} });
620+
const finalTheme = Object.assign(Object.assign({}, theme), { name: theme.name || "unknown-theme", type: getColorScheme(theme), settings: theme.settings || theme.tokenColors || [], colors: theme.colors || {} });
622621
const globalSetting = finalTheme.settings.find((s) => !s.name && !s.scope);
623622
if (globalSetting) {
624623
const { foreground, background } = globalSetting.settings || {};
624+
const newColors = {};
625625
if (foreground && !finalTheme.colors["editor.foreground"]) {
626-
finalTheme.colors["editor.foreground"] = foreground;
626+
newColors["editor.foreground"] = foreground;
627627
}
628628
if (background && !finalTheme.colors["editor.background"]) {
629-
finalTheme.colors["editor.background"] = background;
629+
newColors["editor.background"] = background;
630+
}
631+
if (Object.keys(newColors).length > 0) {
632+
finalTheme.colors = Object.assign(Object.assign({}, finalTheme.colors), newColors);
630633
}
631634
}
632635
if (!globalSetting) {
633-
finalTheme.settings.unshift({
634-
settings: {
635-
foreground: getColor(finalTheme, "editor.foreground"),
636-
background: getColor(finalTheme, "editor.background"),
636+
finalTheme.settings = [
637+
{
638+
settings: {
639+
foreground: getColor(finalTheme, "editor.foreground"),
640+
background: getColor(finalTheme, "editor.background"),
641+
},
637642
},
638-
});
643+
...finalTheme.settings,
644+
];
639645
}
640646
return finalTheme;
641647
}

lib/src/theme.ts

+15-8
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,34 @@ function toFinalTheme(theme: RawTheme | undefined): FinalTheme | undefined {
5454
...theme,
5555
name: theme.name || "unknown-theme",
5656
type: getColorScheme(theme),
57-
settings: theme.settings?.slice() || theme.tokenColors?.slice() || [],
57+
settings: theme.settings || theme.tokenColors || [],
5858
colors: theme.colors || {},
5959
};
6060

6161
const globalSetting = finalTheme.settings.find((s) => !s.name && !s.scope);
6262
if (globalSetting) {
6363
const { foreground, background } = globalSetting.settings || {};
64+
const newColors = {};
6465
if (foreground && !finalTheme.colors["editor.foreground"]) {
65-
finalTheme.colors["editor.foreground"] = foreground;
66+
newColors["editor.foreground"] = foreground;
6667
}
6768
if (background && !finalTheme.colors["editor.background"]) {
68-
finalTheme.colors["editor.background"] = background;
69+
newColors["editor.background"] = background;
70+
}
71+
if (Object.keys(newColors).length > 0) {
72+
finalTheme.colors = { ...finalTheme.colors, ...newColors };
6973
}
7074
}
7175
if (!globalSetting) {
72-
finalTheme.settings.unshift({
73-
settings: {
74-
foreground: getColor(finalTheme, "editor.foreground"),
75-
background: getColor(finalTheme, "editor.background"),
76+
finalTheme.settings = [
77+
{
78+
settings: {
79+
foreground: getColor(finalTheme, "editor.foreground"),
80+
background: getColor(finalTheme, "editor.background"),
81+
},
7682
},
77-
});
83+
...finalTheme.settings,
84+
];
7885
}
7986

8087
return finalTheme;

lib/test/__snapshots__/browser.test.ts.snap

+47
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,53 @@ exports[`highlight with frozen theme 1`] = `
510510
}
511511
`;
512512

513+
exports[`highlight with frozen theme with global setting 1`] = `
514+
{
515+
"colors": {
516+
"activeTabBackground": "#1E1E1E",
517+
"activeTabBorder": "#1E1E1E",
518+
"activeTabForeground": "#ffffff",
519+
"activeTabTopBorder": undefined,
520+
"background": "#1E1E1E",
521+
"colorScheme": "dark",
522+
"diffInsertedLineBackground": "#9bb95533",
523+
"diffInsertedTextBackground": "#9ccc2c33",
524+
"diffRemovedLineBackground": "#ff000033",
525+
"diffRemovedTextBackground": "#ff000033",
526+
"editorBackground": "#1E1E1E",
527+
"editorGroupHeaderBackground": "#252526",
528+
"foreground": "#bbbbbb",
529+
"hoverTabBackground": "#2D2D2D",
530+
"hoverTabForeground": "#ffffff80",
531+
"iconForeground": "#C5C5C5",
532+
"inactiveTabBackground": "#2D2D2D",
533+
"inactiveTabForeground": "#ffffff80",
534+
"lineNumberForeground": "#858585",
535+
"listHoverBackground": "#2A2D2E",
536+
"listHoverForeground": undefined,
537+
"listSelectionBackground": "#37373D",
538+
"listSelectionForeground": undefined,
539+
"selectionBackground": "#264F78",
540+
"sideBarBackground": "#252526",
541+
"sideBarBorder": "#252526",
542+
"sideBarForeground": "#bbbbbb",
543+
"tabBorder": "#252526",
544+
"tabsBorder": undefined,
545+
},
546+
"lang": "javascript",
547+
"lines": [
548+
[
549+
{
550+
"content": "x = 1",
551+
"style": {
552+
"color": "#BBBBBB",
553+
},
554+
},
555+
],
556+
],
557+
}
558+
`;
559+
513560
exports[`highlight with scopes 1`] = `
514561
{
515562
"colors": {

lib/test/__snapshots__/cjs.test.ts.snap

+47
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,53 @@ exports[`highlight with frozen theme 1`] = `
510510
}
511511
`;
512512

513+
exports[`highlight with frozen theme with global setting 1`] = `
514+
{
515+
"colors": {
516+
"activeTabBackground": "#1E1E1E",
517+
"activeTabBorder": "#1E1E1E",
518+
"activeTabForeground": "#ffffff",
519+
"activeTabTopBorder": undefined,
520+
"background": "#1E1E1E",
521+
"colorScheme": "dark",
522+
"diffInsertedLineBackground": "#9bb95533",
523+
"diffInsertedTextBackground": "#9ccc2c33",
524+
"diffRemovedLineBackground": "#ff000033",
525+
"diffRemovedTextBackground": "#ff000033",
526+
"editorBackground": "#1E1E1E",
527+
"editorGroupHeaderBackground": "#252526",
528+
"foreground": "#bbbbbb",
529+
"hoverTabBackground": "#2D2D2D",
530+
"hoverTabForeground": "#ffffff80",
531+
"iconForeground": "#C5C5C5",
532+
"inactiveTabBackground": "#2D2D2D",
533+
"inactiveTabForeground": "#ffffff80",
534+
"lineNumberForeground": "#858585",
535+
"listHoverBackground": "#2A2D2E",
536+
"listHoverForeground": undefined,
537+
"listSelectionBackground": "#37373D",
538+
"listSelectionForeground": undefined,
539+
"selectionBackground": "#264F78",
540+
"sideBarBackground": "#252526",
541+
"sideBarBorder": "#252526",
542+
"sideBarForeground": "#bbbbbb",
543+
"tabBorder": "#252526",
544+
"tabsBorder": undefined,
545+
},
546+
"lang": "javascript",
547+
"lines": [
548+
[
549+
{
550+
"content": "x = 1",
551+
"style": {
552+
"color": "#BBBBBB",
553+
},
554+
},
555+
],
556+
],
557+
}
558+
`;
559+
513560
exports[`highlight with scopes 1`] = `
514561
{
515562
"colors": {

lib/test/__snapshots__/esm.test.ts.snap

+47
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,53 @@ exports[`highlight with frozen theme 1`] = `
510510
}
511511
`;
512512

513+
exports[`highlight with frozen theme with global setting 1`] = `
514+
{
515+
"colors": {
516+
"activeTabBackground": "#1E1E1E",
517+
"activeTabBorder": "#1E1E1E",
518+
"activeTabForeground": "#ffffff",
519+
"activeTabTopBorder": undefined,
520+
"background": "#1E1E1E",
521+
"colorScheme": "dark",
522+
"diffInsertedLineBackground": "#9bb95533",
523+
"diffInsertedTextBackground": "#9ccc2c33",
524+
"diffRemovedLineBackground": "#ff000033",
525+
"diffRemovedTextBackground": "#ff000033",
526+
"editorBackground": "#1E1E1E",
527+
"editorGroupHeaderBackground": "#252526",
528+
"foreground": "#bbbbbb",
529+
"hoverTabBackground": "#2D2D2D",
530+
"hoverTabForeground": "#ffffff80",
531+
"iconForeground": "#C5C5C5",
532+
"inactiveTabBackground": "#2D2D2D",
533+
"inactiveTabForeground": "#ffffff80",
534+
"lineNumberForeground": "#858585",
535+
"listHoverBackground": "#2A2D2E",
536+
"listHoverForeground": undefined,
537+
"listSelectionBackground": "#37373D",
538+
"listSelectionForeground": undefined,
539+
"selectionBackground": "#264F78",
540+
"sideBarBackground": "#252526",
541+
"sideBarBorder": "#252526",
542+
"sideBarForeground": "#bbbbbb",
543+
"tabBorder": "#252526",
544+
"tabsBorder": undefined,
545+
},
546+
"lang": "javascript",
547+
"lines": [
548+
[
549+
{
550+
"content": "x = 1",
551+
"style": {
552+
"color": "#BBBBBB",
553+
},
554+
},
555+
],
556+
],
557+
}
558+
`;
559+
513560
exports[`highlight with scopes 1`] = `
514561
{
515562
"colors": {

lib/test/highlight.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,19 @@ export function runTests({ highlight }) {
4141
const result = await highlight("x = 1", "js", {});
4242
expect(result).toMatchSnapshot();
4343
});
44-
4544
test("highlight with frozen theme", async () => {
4645
const theme = Object.freeze({
47-
tokenColors: Object.freeze([
48-
{ scope: ["comment"], settings: { foreground: "#8b949e" } },
49-
]),
46+
colors: Object.freeze({}),
47+
tokenColors: Object.freeze([]),
48+
});
49+
const result = await highlight("x = 1", "js", theme);
50+
expect(result).toMatchSnapshot();
51+
});
52+
53+
test("highlight with frozen theme with global setting", async () => {
54+
const theme = Object.freeze({
55+
colors: Object.freeze({}),
56+
tokenColors: Object.freeze([{ settings: { foreground: "#bbbbbb" } }]),
5057
});
5158
const result = await highlight("x = 1", "js", theme);
5259
expect(result).toMatchSnapshot();

0 commit comments

Comments
 (0)