|
| 1 | +import { Theme, useTheme } from "@material-ui/core/styles" |
| 2 | +import { useMonaco } from "@monaco-editor/react" |
| 3 | +import { useEffect, useState } from "react" |
| 4 | +import { hslToHex } from "util/colors" |
| 5 | + |
| 6 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any -- The theme is not typed |
| 7 | +export const coderTheme = (theme: Theme): Record<string, any> => ({ |
| 8 | + base: "vs-dark", |
| 9 | + inherit: true, |
| 10 | + rules: [ |
| 11 | + { |
| 12 | + background: "282a36", |
| 13 | + token: "", |
| 14 | + }, |
| 15 | + { |
| 16 | + foreground: "6272a4", |
| 17 | + token: "comment", |
| 18 | + }, |
| 19 | + { |
| 20 | + foreground: "f1fa8c", |
| 21 | + token: "string", |
| 22 | + }, |
| 23 | + { |
| 24 | + foreground: "bd93f9", |
| 25 | + token: "constant.numeric", |
| 26 | + }, |
| 27 | + { |
| 28 | + foreground: "bd93f9", |
| 29 | + token: "constant.language", |
| 30 | + }, |
| 31 | + { |
| 32 | + foreground: "bd93f9", |
| 33 | + token: "constant.character", |
| 34 | + }, |
| 35 | + { |
| 36 | + foreground: "bd93f9", |
| 37 | + token: "constant.other", |
| 38 | + }, |
| 39 | + { |
| 40 | + foreground: "ffb86c", |
| 41 | + token: "variable.other.readwrite.instance", |
| 42 | + }, |
| 43 | + { |
| 44 | + foreground: "ff79c6", |
| 45 | + token: "constant.character.escaped", |
| 46 | + }, |
| 47 | + { |
| 48 | + foreground: "ff79c6", |
| 49 | + token: "constant.character.escape", |
| 50 | + }, |
| 51 | + { |
| 52 | + foreground: "ff79c6", |
| 53 | + token: "string source", |
| 54 | + }, |
| 55 | + { |
| 56 | + foreground: "ff79c6", |
| 57 | + token: "string source.ruby", |
| 58 | + }, |
| 59 | + { |
| 60 | + foreground: "ff79c6", |
| 61 | + token: "keyword", |
| 62 | + }, |
| 63 | + { |
| 64 | + foreground: "ff79c6", |
| 65 | + token: "storage", |
| 66 | + }, |
| 67 | + { |
| 68 | + foreground: "8be9fd", |
| 69 | + fontStyle: "italic", |
| 70 | + token: "storage.type", |
| 71 | + }, |
| 72 | + { |
| 73 | + foreground: "50fa7b", |
| 74 | + fontStyle: "underline", |
| 75 | + token: "entity.name.class", |
| 76 | + }, |
| 77 | + { |
| 78 | + foreground: "50fa7b", |
| 79 | + fontStyle: "italic underline", |
| 80 | + token: "entity.other.inherited-class", |
| 81 | + }, |
| 82 | + { |
| 83 | + foreground: "50fa7b", |
| 84 | + token: "entity.name.function", |
| 85 | + }, |
| 86 | + { |
| 87 | + foreground: "ffb86c", |
| 88 | + fontStyle: "italic", |
| 89 | + token: "variable.parameter", |
| 90 | + }, |
| 91 | + { |
| 92 | + foreground: "ff79c6", |
| 93 | + token: "entity.name.tag", |
| 94 | + }, |
| 95 | + { |
| 96 | + foreground: "50fa7b", |
| 97 | + token: "entity.other.attribute-name", |
| 98 | + }, |
| 99 | + { |
| 100 | + foreground: "8be9fd", |
| 101 | + token: "support.function", |
| 102 | + }, |
| 103 | + { |
| 104 | + foreground: "6be5fd", |
| 105 | + token: "support.constant", |
| 106 | + }, |
| 107 | + { |
| 108 | + foreground: "66d9ef", |
| 109 | + fontStyle: " italic", |
| 110 | + token: "support.type", |
| 111 | + }, |
| 112 | + { |
| 113 | + foreground: "66d9ef", |
| 114 | + fontStyle: " italic", |
| 115 | + token: "support.class", |
| 116 | + }, |
| 117 | + { |
| 118 | + foreground: "f8f8f0", |
| 119 | + background: "ff79c6", |
| 120 | + token: "invalid", |
| 121 | + }, |
| 122 | + { |
| 123 | + foreground: "f8f8f0", |
| 124 | + background: "bd93f9", |
| 125 | + token: "invalid.deprecated", |
| 126 | + }, |
| 127 | + { |
| 128 | + foreground: "cfcfc2", |
| 129 | + token: "meta.structure.dictionary.json string.quoted.double.json", |
| 130 | + }, |
| 131 | + { |
| 132 | + foreground: "6272a4", |
| 133 | + token: "meta.diff", |
| 134 | + }, |
| 135 | + { |
| 136 | + foreground: "6272a4", |
| 137 | + token: "meta.diff.header", |
| 138 | + }, |
| 139 | + { |
| 140 | + foreground: "ff79c6", |
| 141 | + token: "markup.deleted", |
| 142 | + }, |
| 143 | + { |
| 144 | + foreground: "50fa7b", |
| 145 | + token: "markup.inserted", |
| 146 | + }, |
| 147 | + { |
| 148 | + foreground: "e6db74", |
| 149 | + token: "markup.changed", |
| 150 | + }, |
| 151 | + { |
| 152 | + foreground: "bd93f9", |
| 153 | + token: "constant.numeric.line-number.find-in-files - match", |
| 154 | + }, |
| 155 | + { |
| 156 | + foreground: "e6db74", |
| 157 | + token: "entity.name.filename", |
| 158 | + }, |
| 159 | + { |
| 160 | + foreground: "f83333", |
| 161 | + token: "message.error", |
| 162 | + }, |
| 163 | + { |
| 164 | + foreground: "eeeeee", |
| 165 | + token: |
| 166 | + "punctuation.definition.string.begin.json - meta.structure.dictionary.value.json", |
| 167 | + }, |
| 168 | + { |
| 169 | + foreground: "eeeeee", |
| 170 | + token: |
| 171 | + "punctuation.definition.string.end.json - meta.structure.dictionary.value.json", |
| 172 | + }, |
| 173 | + { |
| 174 | + foreground: "8be9fd", |
| 175 | + token: "meta.structure.dictionary.json string.quoted.double.json", |
| 176 | + }, |
| 177 | + { |
| 178 | + foreground: "f1fa8c", |
| 179 | + token: "meta.structure.dictionary.value.json string.quoted.double.json", |
| 180 | + }, |
| 181 | + { |
| 182 | + foreground: "50fa7b", |
| 183 | + token: |
| 184 | + "meta meta meta meta meta meta meta.structure.dictionary.value string", |
| 185 | + }, |
| 186 | + { |
| 187 | + foreground: "ffb86c", |
| 188 | + token: "meta meta meta meta meta meta.structure.dictionary.value string", |
| 189 | + }, |
| 190 | + { |
| 191 | + foreground: "ff79c6", |
| 192 | + token: "meta meta meta meta meta.structure.dictionary.value string", |
| 193 | + }, |
| 194 | + { |
| 195 | + foreground: "bd93f9", |
| 196 | + token: "meta meta meta meta.structure.dictionary.value string", |
| 197 | + }, |
| 198 | + { |
| 199 | + foreground: "50fa7b", |
| 200 | + token: "meta meta meta.structure.dictionary.value string", |
| 201 | + }, |
| 202 | + { |
| 203 | + foreground: "ffb86c", |
| 204 | + token: "meta meta.structure.dictionary.value string", |
| 205 | + }, |
| 206 | + ], |
| 207 | + colors: { |
| 208 | + "editor.foreground": hslToHex(theme.palette.text.secondary), |
| 209 | + "editor.background": hslToHex(theme.palette.background.paper), |
| 210 | + "editor.selectionBackground": hslToHex(theme.palette.action.hover), |
| 211 | + "editor.lineHighlightBackground": hslToHex( |
| 212 | + theme.palette.background.paperLight, |
| 213 | + ), |
| 214 | + "editorCursor.foreground": "#f8f8f0", |
| 215 | + "editorWhitespace.foreground": "#3B3A32", |
| 216 | + "editorIndentGuide.activeBackground": "#9D550FB0", |
| 217 | + "editor.selectionHighlightBorder": "#222218", |
| 218 | + }, |
| 219 | +}) |
| 220 | + |
| 221 | +export const useCoderTheme = (): { isLoading: boolean; name: string } => { |
| 222 | + const [isLoading, setIsLoading] = useState(true) |
| 223 | + const monaco = useMonaco() |
| 224 | + const theme = useTheme<Theme>() |
| 225 | + const name = "coder" |
| 226 | + |
| 227 | + useEffect(() => { |
| 228 | + if (monaco) { |
| 229 | + monaco.editor.defineTheme(name, coderTheme(theme)) |
| 230 | + setIsLoading(false) |
| 231 | + } |
| 232 | + }, [monaco, theme]) |
| 233 | + |
| 234 | + return { |
| 235 | + isLoading, |
| 236 | + name, |
| 237 | + } |
| 238 | +} |
0 commit comments