|
| 1 | +// @ts-check |
| 2 | +const { loadColorTheme } = require('../lib/vscode/colorThemeData'); |
| 3 | +const { generateTokensCSSForColorMap } = require('../lib/vscode/tokenization'); |
| 4 | +const { ensureThemeLocation } = require('./storeUtils'); |
| 5 | +const { sanitizeForClassName } = require('./utils'); |
| 6 | +const { |
| 7 | + joinClassNames, |
| 8 | + renderRule, |
| 9 | + prefersDark, |
| 10 | + prefersLight, |
| 11 | + prefixRules |
| 12 | +} = require('./renderUtils'); |
| 13 | + |
| 14 | +/** |
| 15 | + * @param {ColorThemeSettings} settings |
| 16 | + * @returns {{ [K in keyof ColorThemeSettings]: string }} |
| 17 | + */ |
| 18 | +function createThemeClassNames(settings) { |
| 19 | + return { |
| 20 | + defaultTheme: sanitizeForClassName(settings.defaultTheme), |
| 21 | + prefersDarkTheme: settings.prefersDarkTheme && `pd--${sanitizeForClassName(settings.prefersDarkTheme)}`, |
| 22 | + prefersLightTheme: settings.prefersLightTheme && `pl--${sanitizeForClassName(settings.prefersLightTheme)}` |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * @param {{ [K in keyof ColorThemeSettings]: string }} classNames |
| 28 | + */ |
| 29 | +function joinThemeClassNames(classNames) { |
| 30 | + return joinClassNames(...Object.keys(classNames).map(setting => classNames[setting])); |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * @param {string | ColorThemeSettings} colorThemeValue |
| 35 | + * @returns {ColorThemeSettings} |
| 36 | + */ |
| 37 | +function createColorThemeSettings(colorThemeValue) { |
| 38 | + return typeof colorThemeValue === 'string' ? { defaultTheme: colorThemeValue } : colorThemeValue; |
| 39 | +} |
| 40 | + |
| 41 | +const settingPropertyMap = { 'editor.background': 'background-color', 'editor.foreground': 'color' }; |
| 42 | + |
| 43 | +/** |
| 44 | + * @param {Record<string, string>} settings |
| 45 | + */ |
| 46 | +function getStylesFromSettings(settings) { |
| 47 | + return Object.keys(settings) |
| 48 | + .reduce((styles, setting) => { |
| 49 | + const property = settingPropertyMap[setting]; |
| 50 | + if (property) { |
| 51 | + return [...styles, `${property}: ${settings[setting]};`]; |
| 52 | + } |
| 53 | + return styles; |
| 54 | + }, []) |
| 55 | + .join('\n'); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * @param {{ |
| 60 | + * colorTheme: PluginOptions['colorTheme'], |
| 61 | + * replaceColor: PluginOptions['replaceColor'], |
| 62 | + * registry: import('vscode-textmate').Registry, |
| 63 | + * cache: any, |
| 64 | + * markdownNode: any, |
| 65 | + * codeFenceNode: any, |
| 66 | + * codeFenceOptions: object, |
| 67 | + * languageName: string, |
| 68 | + * scopeName: string, |
| 69 | + * stylesheets: Record<string, string> |
| 70 | + * }} options |
| 71 | + * @returns {Promise<string>} |
| 72 | + */ |
| 73 | +async function createThemeStyles({ |
| 74 | + colorTheme, |
| 75 | + replaceColor, |
| 76 | + registry, |
| 77 | + markdownNode, |
| 78 | + codeFenceNode, |
| 79 | + codeFenceOptions, |
| 80 | + languageName, |
| 81 | + scopeName, |
| 82 | + cache, |
| 83 | + stylesheets |
| 84 | +}) { |
| 85 | + const colorThemeValue = |
| 86 | + typeof colorTheme === 'function' |
| 87 | + ? colorTheme({ |
| 88 | + markdownNode, |
| 89 | + codeFenceNode, |
| 90 | + parsedOptions: codeFenceOptions, |
| 91 | + language: languageName |
| 92 | + }) |
| 93 | + : colorTheme; |
| 94 | + const colorThemeSettings = createColorThemeSettings(colorThemeValue); |
| 95 | + const themeClassNames = createThemeClassNames(colorThemeSettings); |
| 96 | + for (const setting in colorThemeSettings) { |
| 97 | + const colorThemeIdentifier = colorThemeSettings[setting]; |
| 98 | + if (!colorThemeIdentifier) continue; |
| 99 | + |
| 100 | + const themeClassName = themeClassNames[setting]; |
| 101 | + const themeCache = await cache.get('themes'); |
| 102 | + const colorThemePath = await ensureThemeLocation(colorThemeIdentifier, themeCache, markdownNode.fileAbsolutePath); |
| 103 | + |
| 104 | + const { resultRules: tokenColors, resultColors: settings } = loadColorTheme(colorThemePath); |
| 105 | + const defaultTokenColors = { |
| 106 | + settings: { |
| 107 | + foreground: settings['editor.foreground'] || settings.foreground, |
| 108 | + background: settings['editor.background'] || settings.background |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + registry.setTheme({ settings: [defaultTokenColors, ...tokenColors] }); |
| 113 | + if (!stylesheets[themeClassName] || scopeName) { |
| 114 | + const rules = [ |
| 115 | + renderRule(themeClassName, getStylesFromSettings(settings)), |
| 116 | + ...(scopeName |
| 117 | + ? prefixRules( |
| 118 | + generateTokensCSSForColorMap( |
| 119 | + registry.getColorMap().map(color => replaceColor(color, colorThemeIdentifier)) |
| 120 | + ).split('\n'), |
| 121 | + `.${themeClassName} ` |
| 122 | + ) |
| 123 | + : []) |
| 124 | + ]; |
| 125 | + |
| 126 | + if (setting === 'prefersDarkTheme') { |
| 127 | + stylesheets[themeClassName] = prefersDark(rules); |
| 128 | + } else if (setting === 'prefersLightTheme') { |
| 129 | + stylesheets[themeClassName] = prefersLight(rules); |
| 130 | + } else { |
| 131 | + stylesheets[themeClassName] = rules.join('\n'); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return joinThemeClassNames(themeClassNames); |
| 137 | +} |
| 138 | + |
| 139 | +module.exports = createThemeStyles; |
0 commit comments