|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import type { CSSProperties } from 'react'; |
|
|
import type { SemanticColors } from './semantic-tokens.js'; |
|
|
import { resolveColor } from './color-utils.js'; |
|
|
|
|
|
export type ThemeType = 'light' | 'dark' | 'ansi' | 'custom'; |
|
|
|
|
|
export interface ColorsTheme { |
|
|
type: ThemeType; |
|
|
Background: string; |
|
|
Foreground: string; |
|
|
LightBlue: string; |
|
|
AccentBlue: string; |
|
|
AccentPurple: string; |
|
|
AccentCyan: string; |
|
|
AccentGreen: string; |
|
|
AccentYellow: string; |
|
|
AccentRed: string; |
|
|
DiffAdded: string; |
|
|
DiffRemoved: string; |
|
|
Comment: string; |
|
|
Gray: string; |
|
|
GradientColors?: string[]; |
|
|
} |
|
|
|
|
|
export interface CustomTheme { |
|
|
type: 'custom'; |
|
|
name: string; |
|
|
|
|
|
text?: { |
|
|
primary?: string; |
|
|
secondary?: string; |
|
|
link?: string; |
|
|
accent?: string; |
|
|
}; |
|
|
background?: { |
|
|
primary?: string; |
|
|
diff?: { |
|
|
added?: string; |
|
|
removed?: string; |
|
|
}; |
|
|
}; |
|
|
border?: { |
|
|
default?: string; |
|
|
focused?: string; |
|
|
}; |
|
|
ui?: { |
|
|
comment?: string; |
|
|
symbol?: string; |
|
|
gradient?: string[]; |
|
|
}; |
|
|
status?: { |
|
|
error?: string; |
|
|
success?: string; |
|
|
warning?: string; |
|
|
}; |
|
|
|
|
|
|
|
|
Background?: string; |
|
|
Foreground?: string; |
|
|
LightBlue?: string; |
|
|
AccentBlue?: string; |
|
|
AccentPurple?: string; |
|
|
AccentCyan?: string; |
|
|
AccentGreen?: string; |
|
|
AccentYellow?: string; |
|
|
AccentRed?: string; |
|
|
DiffAdded?: string; |
|
|
DiffRemoved?: string; |
|
|
Comment?: string; |
|
|
Gray?: string; |
|
|
GradientColors?: string[]; |
|
|
} |
|
|
|
|
|
export const lightTheme: ColorsTheme = { |
|
|
type: 'light', |
|
|
Background: '#FAFAFA', |
|
|
Foreground: '#3C3C43', |
|
|
LightBlue: '#89BDCD', |
|
|
AccentBlue: '#3B82F6', |
|
|
AccentPurple: '#8B5CF6', |
|
|
AccentCyan: '#06B6D4', |
|
|
AccentGreen: '#3CA84B', |
|
|
AccentYellow: '#D5A40A', |
|
|
AccentRed: '#DD4C4C', |
|
|
DiffAdded: '#C6EAD8', |
|
|
DiffRemoved: '#FFCCCC', |
|
|
Comment: '#008000', |
|
|
Gray: '#97a0b0', |
|
|
GradientColors: ['#4796E4', '#847ACE', '#C3677F'], |
|
|
}; |
|
|
|
|
|
export const darkTheme: ColorsTheme = { |
|
|
type: 'dark', |
|
|
Background: '#1E1E2E', |
|
|
Foreground: '#CDD6F4', |
|
|
LightBlue: '#ADD8E6', |
|
|
AccentBlue: '#89B4FA', |
|
|
AccentPurple: '#CBA6F7', |
|
|
AccentCyan: '#89DCEB', |
|
|
AccentGreen: '#A6E3A1', |
|
|
AccentYellow: '#F9E2AF', |
|
|
AccentRed: '#F38BA8', |
|
|
DiffAdded: '#28350B', |
|
|
DiffRemoved: '#430000', |
|
|
Comment: '#6C7086', |
|
|
Gray: '#6C7086', |
|
|
GradientColors: ['#4796E4', '#847ACE', '#C3677F'], |
|
|
}; |
|
|
|
|
|
export const ansiTheme: ColorsTheme = { |
|
|
type: 'ansi', |
|
|
Background: 'black', |
|
|
Foreground: 'white', |
|
|
LightBlue: 'blue', |
|
|
AccentBlue: 'blue', |
|
|
AccentPurple: 'magenta', |
|
|
AccentCyan: 'cyan', |
|
|
AccentGreen: 'green', |
|
|
AccentYellow: 'yellow', |
|
|
AccentRed: 'red', |
|
|
DiffAdded: 'green', |
|
|
DiffRemoved: 'red', |
|
|
Comment: 'gray', |
|
|
Gray: 'gray', |
|
|
}; |
|
|
|
|
|
export class Theme { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
readonly defaultColor: string; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected readonly _colorMap: Readonly<Record<string, string>>; |
|
|
readonly semanticColors: SemanticColors; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor( |
|
|
readonly name: string, |
|
|
readonly type: ThemeType, |
|
|
rawMappings: Record<string, CSSProperties>, |
|
|
readonly colors: ColorsTheme, |
|
|
semanticColors?: SemanticColors, |
|
|
) { |
|
|
this.semanticColors = semanticColors ?? { |
|
|
text: { |
|
|
primary: this.colors.Foreground, |
|
|
secondary: this.colors.Gray, |
|
|
link: this.colors.AccentBlue, |
|
|
accent: this.colors.AccentPurple, |
|
|
}, |
|
|
background: { |
|
|
primary: this.colors.Background, |
|
|
diff: { |
|
|
added: this.colors.DiffAdded, |
|
|
removed: this.colors.DiffRemoved, |
|
|
}, |
|
|
}, |
|
|
border: { |
|
|
default: this.colors.Gray, |
|
|
focused: this.colors.AccentBlue, |
|
|
}, |
|
|
ui: { |
|
|
comment: this.colors.Comment, |
|
|
symbol: this.colors.Gray, |
|
|
gradient: this.colors.GradientColors, |
|
|
}, |
|
|
status: { |
|
|
error: this.colors.AccentRed, |
|
|
success: this.colors.AccentGreen, |
|
|
warning: this.colors.AccentYellow, |
|
|
}, |
|
|
}; |
|
|
this._colorMap = Object.freeze(this._buildColorMap(rawMappings)); |
|
|
|
|
|
|
|
|
const rawDefaultColor = rawMappings['hljs']?.color; |
|
|
this.defaultColor = |
|
|
(rawDefaultColor ? Theme._resolveColor(rawDefaultColor) : undefined) ?? |
|
|
''; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getInkColor(hljsClass: string): string | undefined { |
|
|
return this._colorMap[hljsClass]; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static _resolveColor(colorValue: string): string | undefined { |
|
|
return resolveColor(colorValue); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected _buildColorMap( |
|
|
hljsTheme: Record<string, CSSProperties>, |
|
|
): Record<string, string> { |
|
|
const inkTheme: Record<string, string> = {}; |
|
|
for (const key in hljsTheme) { |
|
|
|
|
|
if (!key.startsWith('hljs-') && key !== 'hljs') { |
|
|
continue; |
|
|
} |
|
|
|
|
|
const style = hljsTheme[key]; |
|
|
if (style?.color) { |
|
|
const resolvedColor = Theme._resolveColor(style.color); |
|
|
if (resolvedColor !== undefined) { |
|
|
|
|
|
inkTheme[key] = resolvedColor; |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
return inkTheme; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function createCustomTheme(customTheme: CustomTheme): Theme { |
|
|
const colors: ColorsTheme = { |
|
|
type: 'custom', |
|
|
Background: customTheme.background?.primary ?? customTheme.Background ?? '', |
|
|
Foreground: customTheme.text?.primary ?? customTheme.Foreground ?? '', |
|
|
LightBlue: customTheme.text?.link ?? customTheme.LightBlue ?? '', |
|
|
AccentBlue: customTheme.text?.link ?? customTheme.AccentBlue ?? '', |
|
|
AccentPurple: customTheme.text?.accent ?? customTheme.AccentPurple ?? '', |
|
|
AccentCyan: customTheme.text?.link ?? customTheme.AccentCyan ?? '', |
|
|
AccentGreen: customTheme.status?.success ?? customTheme.AccentGreen ?? '', |
|
|
AccentYellow: customTheme.status?.warning ?? customTheme.AccentYellow ?? '', |
|
|
AccentRed: customTheme.status?.error ?? customTheme.AccentRed ?? '', |
|
|
DiffAdded: |
|
|
customTheme.background?.diff?.added ?? customTheme.DiffAdded ?? '', |
|
|
DiffRemoved: |
|
|
customTheme.background?.diff?.removed ?? customTheme.DiffRemoved ?? '', |
|
|
Comment: customTheme.ui?.comment ?? customTheme.Comment ?? '', |
|
|
Gray: customTheme.text?.secondary ?? customTheme.Gray ?? '', |
|
|
GradientColors: customTheme.ui?.gradient ?? customTheme.GradientColors, |
|
|
}; |
|
|
|
|
|
|
|
|
const rawMappings: Record<string, CSSProperties> = { |
|
|
hljs: { |
|
|
display: 'block', |
|
|
overflowX: 'auto', |
|
|
padding: '0.5em', |
|
|
background: colors.Background, |
|
|
color: colors.Foreground, |
|
|
}, |
|
|
'hljs-keyword': { |
|
|
color: colors.AccentBlue, |
|
|
}, |
|
|
'hljs-literal': { |
|
|
color: colors.AccentBlue, |
|
|
}, |
|
|
'hljs-symbol': { |
|
|
color: colors.AccentBlue, |
|
|
}, |
|
|
'hljs-name': { |
|
|
color: colors.AccentBlue, |
|
|
}, |
|
|
'hljs-link': { |
|
|
color: colors.AccentBlue, |
|
|
textDecoration: 'underline', |
|
|
}, |
|
|
'hljs-built_in': { |
|
|
color: colors.AccentCyan, |
|
|
}, |
|
|
'hljs-type': { |
|
|
color: colors.AccentCyan, |
|
|
}, |
|
|
'hljs-number': { |
|
|
color: colors.AccentGreen, |
|
|
}, |
|
|
'hljs-class': { |
|
|
color: colors.AccentGreen, |
|
|
}, |
|
|
'hljs-string': { |
|
|
color: colors.AccentYellow, |
|
|
}, |
|
|
'hljs-meta-string': { |
|
|
color: colors.AccentYellow, |
|
|
}, |
|
|
'hljs-regexp': { |
|
|
color: colors.AccentRed, |
|
|
}, |
|
|
'hljs-template-tag': { |
|
|
color: colors.AccentRed, |
|
|
}, |
|
|
'hljs-subst': { |
|
|
color: colors.Foreground, |
|
|
}, |
|
|
'hljs-function': { |
|
|
color: colors.Foreground, |
|
|
}, |
|
|
'hljs-title': { |
|
|
color: colors.Foreground, |
|
|
}, |
|
|
'hljs-params': { |
|
|
color: colors.Foreground, |
|
|
}, |
|
|
'hljs-formula': { |
|
|
color: colors.Foreground, |
|
|
}, |
|
|
'hljs-comment': { |
|
|
color: colors.Comment, |
|
|
fontStyle: 'italic', |
|
|
}, |
|
|
'hljs-quote': { |
|
|
color: colors.Comment, |
|
|
fontStyle: 'italic', |
|
|
}, |
|
|
'hljs-doctag': { |
|
|
color: colors.Comment, |
|
|
}, |
|
|
'hljs-meta': { |
|
|
color: colors.Gray, |
|
|
}, |
|
|
'hljs-meta-keyword': { |
|
|
color: colors.Gray, |
|
|
}, |
|
|
'hljs-tag': { |
|
|
color: colors.Gray, |
|
|
}, |
|
|
'hljs-variable': { |
|
|
color: colors.AccentPurple, |
|
|
}, |
|
|
'hljs-template-variable': { |
|
|
color: colors.AccentPurple, |
|
|
}, |
|
|
'hljs-attr': { |
|
|
color: colors.LightBlue, |
|
|
}, |
|
|
'hljs-attribute': { |
|
|
color: colors.LightBlue, |
|
|
}, |
|
|
'hljs-builtin-name': { |
|
|
color: colors.LightBlue, |
|
|
}, |
|
|
'hljs-section': { |
|
|
color: colors.AccentYellow, |
|
|
}, |
|
|
'hljs-emphasis': { |
|
|
fontStyle: 'italic', |
|
|
}, |
|
|
'hljs-strong': { |
|
|
fontWeight: 'bold', |
|
|
}, |
|
|
'hljs-bullet': { |
|
|
color: colors.AccentYellow, |
|
|
}, |
|
|
'hljs-selector-tag': { |
|
|
color: colors.AccentYellow, |
|
|
}, |
|
|
'hljs-selector-id': { |
|
|
color: colors.AccentYellow, |
|
|
}, |
|
|
'hljs-selector-class': { |
|
|
color: colors.AccentYellow, |
|
|
}, |
|
|
'hljs-selector-attr': { |
|
|
color: colors.AccentYellow, |
|
|
}, |
|
|
'hljs-selector-pseudo': { |
|
|
color: colors.AccentYellow, |
|
|
}, |
|
|
'hljs-addition': { |
|
|
backgroundColor: colors.AccentGreen, |
|
|
display: 'inline-block', |
|
|
width: '100%', |
|
|
}, |
|
|
'hljs-deletion': { |
|
|
backgroundColor: colors.AccentRed, |
|
|
display: 'inline-block', |
|
|
width: '100%', |
|
|
}, |
|
|
}; |
|
|
|
|
|
const semanticColors: SemanticColors = { |
|
|
text: { |
|
|
primary: colors.Foreground, |
|
|
secondary: colors.Gray, |
|
|
link: colors.AccentBlue, |
|
|
accent: colors.AccentPurple, |
|
|
}, |
|
|
background: { |
|
|
primary: colors.Background, |
|
|
diff: { |
|
|
added: colors.DiffAdded, |
|
|
removed: colors.DiffRemoved, |
|
|
}, |
|
|
}, |
|
|
border: { |
|
|
default: colors.Gray, |
|
|
focused: colors.AccentBlue, |
|
|
}, |
|
|
ui: { |
|
|
comment: colors.Comment, |
|
|
symbol: colors.Gray, |
|
|
gradient: colors.GradientColors, |
|
|
}, |
|
|
status: { |
|
|
error: colors.AccentRed, |
|
|
success: colors.AccentGreen, |
|
|
warning: colors.AccentYellow, |
|
|
}, |
|
|
}; |
|
|
|
|
|
return new Theme( |
|
|
customTheme.name, |
|
|
'custom', |
|
|
rawMappings, |
|
|
colors, |
|
|
semanticColors, |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function validateCustomTheme(customTheme: Partial<CustomTheme>): { |
|
|
isValid: boolean; |
|
|
error?: string; |
|
|
warning?: string; |
|
|
} { |
|
|
|
|
|
if (customTheme.name && !isValidThemeName(customTheme.name)) { |
|
|
return { |
|
|
isValid: false, |
|
|
error: `Invalid theme name: ${customTheme.name}`, |
|
|
}; |
|
|
} |
|
|
|
|
|
return { |
|
|
isValid: true, |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isValidThemeName(name: string): boolean { |
|
|
|
|
|
return name.trim().length > 0 && name.trim().length <= 50; |
|
|
} |
|
|
|