Skip to content

Commit 9ec80dc

Browse files
committed
Move theme to utils
1 parent e5998e1 commit 9ec80dc

File tree

6 files changed

+98
-10
lines changed

6 files changed

+98
-10
lines changed

packages/mini-editor/src/editor-frame.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import React from "react"
22
import { FrameButtons } from "@code-hike/mini-frame"
33
import { useClasser, Classes } from "@code-hike/classer"
4-
import { EditorTheme } from "@code-hike/smooth-code/dist/themes"
5-
import { getColor, ColorName } from "./theme-colors"
4+
import {
5+
EditorTheme,
6+
getColor,
7+
ColorName,
8+
} from "@code-hike/utils"
69

710
export {
811
EditorFrameProps,

packages/smooth-code/src/annotations.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import {
1818
splitParts,
1919
ColumnExtremes,
2020
parseExtremes,
21+
EditorTheme,
2122
} from "@code-hike/utils"
22-
import { EditorTheme } from "./themes"
2323
import React from "react"
2424

2525
export function parseAnnotations(

packages/smooth-code/src/partial-step-parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import {
44
map,
55
FullTween,
66
withDefault,
7+
EditorTheme,
78
} from "@code-hike/utils"
8-
import { EditorTheme } from "./themes"
99
import { mergeLines } from "./differ"
1010
import { splitByFocus } from "./splitter"
1111
import React from "react"

packages/storybook/src/themes.story.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export const themes = () => {
6565
React.useEffect(() => {
6666
const files = [
6767
{ name: "foo.js", lang: "js", code },
68-
{ name: "index.js", lang: "js", code },
68+
{ name: "index.js", lang: "js", code, focus: "2:4" },
6969
{ name: "bar.js", lang: "js", code },
7070
{
7171
name: "app.js",
@@ -124,7 +124,7 @@ export const themes = () => {
124124
{...props}
125125
frameProps={{ height: 500 }}
126126
codeConfig={{
127-
maxZoom: 1.2,
127+
maxZoom: 1,
128128
minColumns: 10,
129129
theme,
130130
}}

packages/utils/src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./tween"
22
export * from "./code"
33
export * from "./focus"
4+
export * from "./theme"

packages/mini-editor/src/theme-colors.ts renamed to packages/utils/src/theme.ts

Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
1-
import { EditorTheme } from "@code-hike/smooth-code/dist/themes"
1+
/**
2+
* A single theme setting.
3+
*/
4+
interface IRawThemeSetting {
5+
name?: string
6+
scope?: string | string[]
7+
settings: {
8+
fontStyle?: string
9+
foreground?: string
10+
background?: string
11+
}
12+
}
13+
/**
14+
* A TextMate theme.
15+
*/
16+
export interface EditorTheme {
17+
name?: string
18+
type?: string
19+
colors?: Record<string, string>
20+
21+
// not used:
22+
tokenColors?: IRawThemeSetting[]
23+
settings?: IRawThemeSetting[]
24+
semanticTokenColors?: any
25+
semanticHighlighting?: boolean
26+
}
227

328
export enum ColorName {
29+
CodeForeground,
30+
CodeBackground,
31+
EditorForeground,
432
EditorBackground,
533
ActiveTabBackground,
634
ActiveTabForeground,
@@ -10,20 +38,32 @@ export enum ColorName {
1038
EditorGroupHeaderBackground,
1139
TabBorder,
1240
ActiveTabBottomBorder,
41+
LineNumberForeground,
1342
}
1443

1544
type Color = string | undefined
1645

1746
const contrastBorder = "#6FC3DF"
1847

1948
// defaults from: https://github.com/microsoft/vscode/blob/main/src/vs/workbench/common/theme.ts
49+
// and: https://github.com/microsoft/vscode/blob/main/src/vs/editor/common/view/editorColorRegistry.ts
2050
// keys from : https://code.visualstudio.com/api/references/theme-color#editor-groups-tabs
2151
export function getColor(
2252
theme: EditorTheme,
2353
colorName: ColorName
2454
): Color {
25-
const colors = (theme as any).colors || {}
55+
const colors = theme.colors || {}
2656
switch (colorName) {
57+
case ColorName.CodeForeground:
58+
return (
59+
getGlobalSettings(theme)?.foreground ||
60+
getColor(theme, ColorName.EditorForeground)
61+
)
62+
case ColorName.CodeBackground:
63+
return (
64+
getGlobalSettings(theme)?.background ||
65+
getColor(theme, ColorName.EditorBackground)
66+
)
2767
case ColorName.EditorBackground:
2868
return (
2969
colors["editor.background"] ||
@@ -33,6 +73,15 @@ export function getColor(
3373
hc: "#000000",
3474
})
3575
)
76+
case ColorName.EditorForeground:
77+
return (
78+
colors["editor.foreground"] ||
79+
getDefault(theme, {
80+
light: "#333333",
81+
dark: "#BBBBBB",
82+
hc: "#fffffe",
83+
})
84+
)
3685
case ColorName.ActiveTabBackground:
3786
return (
3887
colors["tab.activeBackground"] ||
@@ -103,6 +152,15 @@ export function getColor(
103152
hc: undefined,
104153
})
105154
)
155+
case ColorName.LineNumberForeground:
156+
return (
157+
colors["editorLineNumber.foreground"] ||
158+
getDefault(theme, {
159+
dark: "#858585",
160+
light: "#237893",
161+
hc: "#fffffe",
162+
})
163+
)
106164

107165
default:
108166
return "#f00"
@@ -122,10 +180,36 @@ function getDefault(
122180
theme: EditorTheme,
123181
defaults: { dark: Color; light: Color; hc: Color }
124182
): Color {
125-
const themeType = (theme.type
183+
return defaults[getThemeType(theme)]
184+
}
185+
186+
function getThemeType(
187+
theme: EditorTheme
188+
): "dark" | "light" | "hc" {
189+
return (theme.type
126190
? theme.type
127191
: theme.name?.toLowerCase().includes("light")
128192
? "light"
129193
: "dark") as "dark" | "light" | "hc"
130-
return defaults[themeType]
194+
}
195+
196+
export function getCodeColors(
197+
theme: EditorTheme
198+
): { fg: string; bg: string } {
199+
return {
200+
fg: getColor(theme, ColorName.CodeForeground)!,
201+
bg: getColor(theme, ColorName.CodeBackground)!,
202+
}
203+
}
204+
205+
function getGlobalSettings(theme: EditorTheme) {
206+
let settings = theme.settings
207+
? theme.settings
208+
: theme.tokenColors
209+
const globalSetting = settings
210+
? settings.find(s => {
211+
return !s.name && !s.scope
212+
})
213+
: undefined
214+
return globalSetting?.settings
131215
}

0 commit comments

Comments
 (0)