Skip to content

Commit

Permalink
fix(vscode): match correct colors (#1221)
Browse files Browse the repository at this point in the history
* fix(vscode): matching wrong colors when using 'dark:' prefix

* fix(vscode): color preview not working when AttributifyOptions.strict = true

* chore: update

Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
zam157 and antfu committed Jul 5, 2022
1 parent 28dd38d commit 6da5ebf
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions packages/vscode/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export function body2ColorValue(body: string, theme: Theme) {

for (const colorName of colorNames) {
const nameIndex = body.indexOf(colorName)

if (nameIndex > -1) {
const parsedResult = parseColor(body.substring(nameIndex), theme)
if (parsedResult?.cssColor)
Expand All @@ -56,27 +57,34 @@ export function body2ColorValue(body: string, theme: Theme) {
return null
}

const matchedAttributifyRE = /(?<=^\[.+~?=").*(?="\]$)/
const matchedAttributifyRE = /(?<=^\[.+~=").*(?="\]$)/
const matchedValuelessAttributifyRE = /(?<=^\[).+(?==""\]$)/
const _colorsMapCache = new Map<string, string>()
export function getColorsMap(uno: UnoGenerator, result: GenerateResult) {
const theme = uno.config.theme as Theme
const colorsMap = new Map<string, string>()

for (const i of result.matched) {
const _i = i.replace('~="', '="')
if (_colorsMapCache.get(_i)) {
colorsMap.set(_i, _colorsMapCache.get(_i)!)
const matchedValueless = i.match(matchedValuelessAttributifyRE)?.[0]
const colorKey = matchedValueless ?? i.replace('~="', '="')

const cachedColor = _colorsMapCache.get(colorKey)
if (cachedColor) {
colorsMap.set(colorKey, cachedColor)
continue
}

const matchedAttr = i.match(matchedAttributifyRE)
const body = matchedAttr?.[0] ?? i // remove prefix e.g. `dark:` `hover:`
const matchedAttr = i.match(matchedAttributifyRE)?.[0] ?? matchedValueless
const body = (matchedAttr ?? i)
.split(':').slice(-1)[0] ?? '' // remove prefix e.g. `dark:` `hover:`

const colorValue = body2ColorValue(body, theme)
if (colorValue) {
const colorString = colorToString(colorValue.cssColor!, colorValue.alpha)
colorsMap.set(_i, colorString)
_colorsMapCache.set(_i, colorString)
if (body) {
const colorValue = body2ColorValue(body, theme)
if (colorValue) {
const colorString = colorToString(colorValue.cssColor!, colorValue.alpha)
colorsMap.set(colorKey, colorString)
_colorsMapCache.set(colorKey, colorString)
}
}
}

Expand Down

0 comments on commit 6da5ebf

Please sign in to comment.