| 1 | import { createContext, useContext, useState } from "react"; |
| 2 | import { useWebviewListener } from "../hooks/useWebviewListener"; |
| 3 | import { parseHexColor } from "../styles/utils"; |
| 4 | |
| 5 | const hljsToTextMate: Record<string, string[]> = { |
| 6 | ".hljs-comment": ["comment"], |
| 7 | ".hljs-tag": ["tag"], |
| 8 | |
| 9 | ".hljs-doctag": ["keyword"], |
| 10 | ".hljs-keyword": ["keyword"], |
| 11 | ".hljs-meta .hljs-keyword": ["keyword"], |
| 12 | ".hljs-template-tag": ["keyword"], |
| 13 | ".hljs-template-variable": ["keyword"], |
| 14 | ".hljs-type": ["keyword"], |
| 15 | ".hljs-variable.language_": ["keyword"], |
| 16 | |
| 17 | ".hljs-title": ["title", "function", "class"], |
| 18 | ".hljs-title.class_": ["title", "function", "class", "variable"], |
| 19 | ".hljs-title.class_.inherited__": ["title", "function", "class", "variable"], |
| 20 | ".hljs-title.function_": [ |
| 21 | "support.function", |
| 22 | "entity.name.function", |
| 23 | "title", |
| 24 | "function", |
| 25 | "class", |
| 26 | ], |
| 27 | |
| 28 | ".hljs-built_in": [ |
| 29 | "support.function", |
| 30 | "entity.name.function", |
| 31 | "title", |
| 32 | "function", |
| 33 | "class", |
| 34 | ], |
| 35 | |
| 36 | ".hljs-name": ["constant"], |
| 37 | |
| 38 | ".hljs-attr": ["variable", "operator", "number"], |
| 39 | ".hljs-attribute": ["attribute", "variable", "operator", "number"], |
| 40 | ".hljs-literal": ["variable", "operator", "number"], |
| 41 | ".hljs-meta": ["variable", "operator", "number"], |
| 42 | ".hljs-number": ["constant.numeric", "number", "variable", "operator"], |
| 43 | ".hljs-operator": ["variable", "operator", "number"], |
| 44 | ".hljs-variable": ["variable", "operator", "number"], |
| 45 | ".hljs-selector-attr": ["variable", "operator", "number"], |
| 46 | ".hljs-selector-class": ["variable", "operator", "number"], |
| 47 | ".hljs-selector-id": ["variable", "operator", "number"], |
| 48 | |
| 49 | ".hljs-regexp": ["string"], |
| 50 | ".hljs-string": ["string"], |
| 51 | ".hljs-meta .hljs-string": ["string"], |
| 52 | |
| 53 | ".hljs-params": ["variable", "operator", "number"], |
| 54 | }; |
| 55 | |
| 56 | function constructTheme( |
| 57 | tmTheme: typeof window.fullColorTheme, |
| 58 | ): Record<string, string> { |
| 59 | const rules = tmTheme?.["rules"] || []; |
| 60 | |
| 61 | const tokenToForeground: Record<string, string> = {}; |
| 62 | rules.forEach(({ token, foreground }) => { |
| 63 | if (!foreground || !token) { |
| 64 | return; |
| 65 | } |
| 66 | tokenToForeground[token] = foreground; |
| 67 | }); |
| 68 | |
| 69 | const theme: Record<string, string> = {}; |
| 70 | Object.keys(hljsToTextMate).forEach((className) => { |
| 71 | const tokens = hljsToTextMate[className]; |
| 72 | for (const scope of tokens) { |
| 73 | if (tokenToForeground[scope]) { |
| 74 | theme[className] = tokenToForeground[scope]; |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | }); |
| 79 | |
| 80 | if (Object.keys(theme).length === 0) { |
| 81 | return fallbackTheme(); |
| 82 | } |
| 83 | |
| 84 | return theme; |
| 85 | } |
| 86 | |
| 87 | function fallbackTheme() { |
| 88 | const styles = getComputedStyle(document.body); |
| 89 | const backgroundColor = styles.getPropertyValue("--vscode-editor-background"); |
| 90 | const { r, g, b } = parseHexColor(backgroundColor); |
| 91 | const avg = (r + g + b) / 3; |
| 92 | |
| 93 | return avg >= 128 |
| 94 | ? { |
| 95 | ".hljs-comment": "#008000", |
| 96 | ".hljs-doctag": "#0000ff", |
| 97 | ".hljs-keyword": "#0000ff", |
| 98 | ".hljs-meta .hljs-keyword": "#0000ff", |
| 99 | ".hljs-template-tag": "#0000ff", |
| 100 | ".hljs-template-variable": "#0000ff", |
| 101 | ".hljs-type": "#0000ff", |
| 102 | ".hljs-variable.language_": "#0000ff", |
| 103 | ".hljs-title.class_": "#001080", |
| 104 | ".hljs-title.class_.inherited__": "#001080", |
| 105 | ".hljs-title.function_": "#795E26", |
| 106 | ".hljs-built_in": "#795E26", |
| 107 | ".hljs-attr": "#001080", |
| 108 | ".hljs-attribute": "#001080", |
| 109 | ".hljs-literal": "#001080", |
| 110 | ".hljs-meta": "#001080", |
| 111 | ".hljs-number": "#098658", |
| 112 | ".hljs-operator": "#001080", |
| 113 | ".hljs-variable": "#001080", |
| 114 | ".hljs-selector-attr": "#001080", |
| 115 | ".hljs-selector-class": "#001080", |
| 116 | ".hljs-selector-id": "#001080", |
| 117 | ".hljs-regexp": "#a31515", |
| 118 | ".hljs-string": "#a31515", |
| 119 | ".hljs-meta .hljs-string": "#a31515", |
| 120 | ".hljs-params": "#001080", |
| 121 | } |
| 122 | : { |
| 123 | ".hljs-comment": "#6A9955", |
| 124 | ".hljs-doctag": "#569cd6", |
| 125 | ".hljs-keyword": "#569cd6", |
| 126 | ".hljs-meta .hljs-keyword": "#569cd6", |
| 127 | ".hljs-template-tag": "#569cd6", |
| 128 | ".hljs-template-variable": "#569cd6", |
| 129 | ".hljs-type": "#569cd6", |
| 130 | ".hljs-variable.language_": "#569cd6", |
| 131 | ".hljs-title.class_": "#9CDCFE", |
| 132 | ".hljs-title.class_.inherited__": "#9CDCFE", |
| 133 | ".hljs-title.function_": "#DCDCAA", |
| 134 | ".hljs-built_in": "#DCDCAA", |
| 135 | ".hljs-attr": "#9CDCFE", |
| 136 | ".hljs-attribute": "#9CDCFE", |
| 137 | ".hljs-literal": "#9CDCFE", |
| 138 | ".hljs-meta": "#9CDCFE", |
| 139 | ".hljs-number": "#b5cea8", |
| 140 | ".hljs-operator": "#9CDCFE", |
| 141 | ".hljs-variable": "#9CDCFE", |
| 142 | ".hljs-selector-attr": "#9CDCFE", |
| 143 | ".hljs-selector-class": "#9CDCFE", |
| 144 | ".hljs-selector-id": "#9CDCFE", |
| 145 | ".hljs-regexp": "#ce9178", |
| 146 | ".hljs-string": "#ce9178", |
| 147 | ".hljs-meta .hljs-string": "#ce9178", |
| 148 | ".hljs-params": "#9CDCFE", |
| 149 | }; |
| 150 | } |
| 151 | |
| 152 | interface VscThemeContextType { |
| 153 | theme: Record<string, string>; |
| 154 | } |
| 155 | |
| 156 | const initialVscThemeContext: VscThemeContextType = { |
| 157 | theme: constructTheme(window.fullColorTheme || {}), |
| 158 | }; |
| 159 | export const VscThemeContext = createContext<VscThemeContextType>( |
| 160 | initialVscThemeContext, |
| 161 | ); |
| 162 | |
| 163 | export const VscThemeProvider = ({ |
| 164 | children, |
| 165 | }: { |
| 166 | children: React.ReactNode; |
| 167 | }) => { |
| 168 | const [theme, setTheme] = useState<Record<string, string>>( |
| 169 | initialVscThemeContext.theme, |
| 170 | ); |
| 171 | |
| 172 | useWebviewListener("setTheme", async (data) => { |
| 173 | window.fullColorTheme = data.theme; |
| 174 | setTheme(constructTheme(data.theme)); |
| 175 | }); |
| 176 | |
| 177 | return ( |
| 178 | <VscThemeContext.Provider value={{ theme }}> |
| 179 | {children} |
| 180 | </VscThemeContext.Provider> |
| 181 | ); |
| 182 | }; |
| 183 | |
| 184 | export const useVscTheme = () => useContext(VscThemeContext); |