Skip to content

Commit 087a41e

Browse files
committed
Add material-from-css
1 parent 79a17c9 commit 087a41e

16 files changed

+1417
-66
lines changed

lib/dist/browser.esm.mjs

+42-11
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function transparent(color, opacity) {
8787
}
8888

8989
function getThemeColors(theme) {
90-
return Object.assign({ colorScheme: theme.type }, getColors(theme));
90+
return Object.assign({ colorScheme: theme.type === "from-css" ? "var(--ch-0)" : theme.type }, getColors(theme));
9191
}
9292
const colorNamesToKeys = {
9393
background: "editor.background",
@@ -231,7 +231,7 @@ async function preloadTheme(theme) {
231231
if (typeof theme === "string") {
232232
const name = theme;
233233
if (!THEME_NAMES.includes(name)) {
234-
throw new UnknownThemeError$1(name);
234+
throw new UnknownThemeError(name);
235235
}
236236
if (!promiseCache.has(name)) {
237237
const promise = reallyLoadThemeByName(name).then((theme) => {
@@ -269,7 +269,7 @@ function toFinalTheme(theme) {
269269
if (!theme) {
270270
return undefined;
271271
}
272-
const finalTheme = Object.assign(Object.assign({}, theme), { name: theme.name || "unknown-theme", type: getColorScheme(theme), settings: theme.settings || theme.tokenColors || [], 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 || {}, colorNames: theme.colorNames });
273273
const globalSetting = finalTheme.settings.find((s) => !s.name && !s.scope);
274274
if (globalSetting) {
275275
const { foreground, background } = globalSetting.settings || {};
@@ -295,10 +295,37 @@ function toFinalTheme(theme) {
295295
...finalTheme.settings,
296296
];
297297
}
298+
if (theme.type === "from-css" && !finalTheme.colorNames) {
299+
const colorNames = {};
300+
let counter = 0;
301+
finalTheme.settings = finalTheme.settings.map((s) => {
302+
const setting = Object.assign(Object.assign({}, s), { settings: Object.assign({}, s.settings) });
303+
const { foreground, background } = setting.settings || {};
304+
if (foreground && !colorNames[foreground]) {
305+
colorNames[foreground] = `#${counter.toString(16).padStart(6, "0")}`;
306+
counter++;
307+
}
308+
if (background && !colorNames[background]) {
309+
colorNames[background] = `#${counter.toString(16).padStart(6, "0")}`;
310+
counter++;
311+
}
312+
if (foreground) {
313+
setting.settings.foreground = colorNames[foreground];
314+
}
315+
if (background) {
316+
setting.settings.background = colorNames[background];
317+
}
318+
return setting;
319+
});
320+
finalTheme.colorNames = colorNames;
321+
}
298322
return finalTheme;
299323
}
300324
function getColorScheme(theme) {
301325
var _a;
326+
if (theme.type === "from-css") {
327+
return "from-css";
328+
}
302329
const themeType = theme.type
303330
? theme.type
304331
: ((_a = theme.name) === null || _a === void 0 ? void 0 : _a.toLowerCase().includes("light"))
@@ -321,6 +348,7 @@ const THEME_NAMES = [
321348
"light-plus",
322349
"material-darker",
323350
"material-default",
351+
"material-from-css",
324352
"material-lighter",
325353
"material-ocean",
326354
"material-palenight",
@@ -335,7 +363,7 @@ const THEME_NAMES = [
335363
"solarized-dark",
336364
"solarized-light",
337365
];
338-
class UnknownThemeError$1 extends Error {
366+
class UnknownThemeError extends Error {
339367
constructor(theme) {
340368
super(`Unknown theme: ${theme}`);
341369
this.theme = theme;
@@ -2582,9 +2610,18 @@ class UnknownLanguageError extends Error {
25822610
}
25832611
function highlightTokens(code, grammar, theme) {
25842612
registry.setTheme(theme);
2585-
const colorMap = registry.getColorMap();
2613+
const colorMap = getColorMap(theme);
25862614
return tokenize(code, grammar, colorMap);
25872615
}
2616+
function getColorMap(theme) {
2617+
const colorMap = registry.getColorMap();
2618+
if (!theme.colorNames)
2619+
return colorMap;
2620+
return colorMap.map((c) => {
2621+
const key = Object.keys(theme.colorNames).find((key) => theme.colorNames[key].toUpperCase() === c.toUpperCase());
2622+
return key || c;
2623+
});
2624+
}
25882625
function highlightTokensWithScopes(code, grammar, theme) {
25892626
registry.setTheme(theme);
25902627
const colorMap = registry.getColorMap();
@@ -3054,12 +3091,6 @@ function splitAnnotations(annotations) {
30543091
};
30553092
}
30563093

3057-
class UnknownThemeError extends Error {
3058-
constructor(theme) {
3059-
super(`Unknown theme: ${theme}`);
3060-
this.theme = theme;
3061-
}
3062-
}
30633094
function isAnnotatedConfig(config) {
30643095
return "annotations" in config;
30653096
}

lib/dist/index.cjs.js

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

lib/dist/index.d.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,23 @@ type ThemeSetting = {
1818
};
1919
type FinalTheme = {
2020
name: string;
21-
type: "dark" | "light";
21+
type: "dark" | "light" | "from-css";
2222
settings: ThemeSetting[];
2323
colors: {
2424
[key: string]: string;
2525
};
26+
colorNames?: {
27+
[key: string]: string;
28+
};
2629
};
27-
declare const THEME_NAMES: readonly ["dark-plus", "dracula-soft", "dracula", "github-dark", "github-dark-dimmed", "github-light", "light-plus", "material-darker", "material-default", "material-lighter", "material-ocean", "material-palenight", "min-dark", "min-light", "monokai", "nord", "one-dark-pro", "poimandres", "slack-dark", "slack-ochin", "solarized-dark", "solarized-light"];
30+
declare const THEME_NAMES: readonly ["dark-plus", "dracula-soft", "dracula", "github-dark", "github-dark-dimmed", "github-light", "light-plus", "material-darker", "material-default", "material-from-css", "material-lighter", "material-ocean", "material-palenight", "min-dark", "min-light", "monokai", "nord", "one-dark-pro", "poimandres", "slack-dark", "slack-ochin", "solarized-dark", "solarized-light"];
2831
type NamesTuple$1 = typeof THEME_NAMES;
2932
type StringTheme = NamesTuple$1[number];
3033
type Theme = StringTheme | RawTheme;
34+
declare class UnknownThemeError extends Error {
35+
theme: string;
36+
constructor(theme: string);
37+
}
3138

3239
declare const LANG_NAMES: string[];
3340
type NamesTuple = typeof LANG_NAMES;
@@ -64,7 +71,7 @@ declare function getThemeColors(theme: FinalTheme): {
6471
activeTabTopBorder: string;
6572
hoverTabBackground: string;
6673
hoverTabForeground: string;
67-
colorScheme: "dark" | "light";
74+
colorScheme: string;
6875
};
6976

7077
type LineNumber = number;
@@ -122,10 +129,6 @@ declare class UnknownLanguageError extends Error {
122129
constructor(alias: string);
123130
}
124131

125-
declare class UnknownThemeError extends Error {
126-
theme: string;
127-
constructor(theme: string);
128-
}
129132
type Config = {
130133
scopes?: boolean;
131134
};

lib/dist/index.esm.mjs

+42-11
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ function transparent(color, opacity) {
435435
}
436436

437437
function getThemeColors(theme) {
438-
return Object.assign({ colorScheme: theme.type }, getColors(theme));
438+
return Object.assign({ colorScheme: theme.type === "from-css" ? "var(--ch-0)" : theme.type }, getColors(theme));
439439
}
440440
const colorNamesToKeys = {
441441
background: "editor.background",
@@ -579,7 +579,7 @@ async function preloadTheme(theme) {
579579
if (typeof theme === "string") {
580580
const name = theme;
581581
if (!THEME_NAMES.includes(name)) {
582-
throw new UnknownThemeError$1(name);
582+
throw new UnknownThemeError(name);
583583
}
584584
if (!promiseCache.has(name)) {
585585
const promise = reallyLoadThemeByName(name).then((theme) => {
@@ -617,7 +617,7 @@ function toFinalTheme(theme) {
617617
if (!theme) {
618618
return undefined;
619619
}
620-
const finalTheme = Object.assign(Object.assign({}, theme), { name: theme.name || "unknown-theme", type: getColorScheme(theme), settings: theme.settings || theme.tokenColors || [], 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 || {}, colorNames: theme.colorNames });
621621
const globalSetting = finalTheme.settings.find((s) => !s.name && !s.scope);
622622
if (globalSetting) {
623623
const { foreground, background } = globalSetting.settings || {};
@@ -643,10 +643,37 @@ function toFinalTheme(theme) {
643643
...finalTheme.settings,
644644
];
645645
}
646+
if (theme.type === "from-css" && !finalTheme.colorNames) {
647+
const colorNames = {};
648+
let counter = 0;
649+
finalTheme.settings = finalTheme.settings.map((s) => {
650+
const setting = Object.assign(Object.assign({}, s), { settings: Object.assign({}, s.settings) });
651+
const { foreground, background } = setting.settings || {};
652+
if (foreground && !colorNames[foreground]) {
653+
colorNames[foreground] = `#${counter.toString(16).padStart(6, "0")}`;
654+
counter++;
655+
}
656+
if (background && !colorNames[background]) {
657+
colorNames[background] = `#${counter.toString(16).padStart(6, "0")}`;
658+
counter++;
659+
}
660+
if (foreground) {
661+
setting.settings.foreground = colorNames[foreground];
662+
}
663+
if (background) {
664+
setting.settings.background = colorNames[background];
665+
}
666+
return setting;
667+
});
668+
finalTheme.colorNames = colorNames;
669+
}
646670
return finalTheme;
647671
}
648672
function getColorScheme(theme) {
649673
var _a;
674+
if (theme.type === "from-css") {
675+
return "from-css";
676+
}
650677
const themeType = theme.type
651678
? theme.type
652679
: ((_a = theme.name) === null || _a === void 0 ? void 0 : _a.toLowerCase().includes("light"))
@@ -669,6 +696,7 @@ const THEME_NAMES = [
669696
"light-plus",
670697
"material-darker",
671698
"material-default",
699+
"material-from-css",
672700
"material-lighter",
673701
"material-ocean",
674702
"material-palenight",
@@ -683,7 +711,7 @@ const THEME_NAMES = [
683711
"solarized-dark",
684712
"solarized-light",
685713
];
686-
class UnknownThemeError$1 extends Error {
714+
class UnknownThemeError extends Error {
687715
constructor(theme) {
688716
super(`Unknown theme: ${theme}`);
689717
this.theme = theme;
@@ -2930,9 +2958,18 @@ class UnknownLanguageError extends Error {
29302958
}
29312959
function highlightTokens(code, grammar, theme) {
29322960
registry.setTheme(theme);
2933-
const colorMap = registry.getColorMap();
2961+
const colorMap = getColorMap(theme);
29342962
return tokenize(code, grammar, colorMap);
29352963
}
2964+
function getColorMap(theme) {
2965+
const colorMap = registry.getColorMap();
2966+
if (!theme.colorNames)
2967+
return colorMap;
2968+
return colorMap.map((c) => {
2969+
const key = Object.keys(theme.colorNames).find((key) => theme.colorNames[key].toUpperCase() === c.toUpperCase());
2970+
return key || c;
2971+
});
2972+
}
29362973
function highlightTokensWithScopes(code, grammar, theme) {
29372974
registry.setTheme(theme);
29382975
const colorMap = registry.getColorMap();
@@ -3402,12 +3439,6 @@ function splitAnnotations(annotations) {
34023439
};
34033440
}
34043441

3405-
class UnknownThemeError extends Error {
3406-
constructor(theme) {
3407-
super(`Unknown theme: ${theme}`);
3408-
this.theme = theme;
3409-
}
3410-
}
34113442
function isAnnotatedConfig(config) {
34123443
return "annotations" in config;
34133444
}

0 commit comments

Comments
 (0)