Home

tailwind-ctp-intellisense @6ecb0011cfb7ac1e5e234acfd49459fb6ca2783e - refs - log -
-
https://git.jolheiser.com/tailwind-ctp-intellisense.git
Tailwind intellisense + Catppuccin
tailwind-ctp-intellisense / packages / tailwindcss-language-service / src / util / pixelEquivalents.ts
- raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import type { Plugin } from 'postcss'
import parseValue from 'postcss-value-parser'
import { parse as parseMediaQueryList } from '@csstools/media-query-list-parser'
import postcss from 'postcss'
import { isTokenNode } from '@csstools/css-parser-algorithms'

type Comment = { index: number; value: string }

export function addPixelEquivalentsToValue(value: string, rootFontSize: number): string {
  if (!value.includes('rem')) {
    return value
  }

  parseValue(value).walk((node) => {
    if (node.type !== 'word') {
      return true
    }

    let unit = parseValue.unit(node.value)
    if (!unit || unit.unit !== 'rem') {
      return false
    }

    let commentStr = `/* ${parseFloat(unit.number) * rootFontSize}px */`
    value = value.slice(0, node.sourceEndIndex) + commentStr + value.slice(node.sourceEndIndex)

    return false
  })

  return value
}

export function addPixelEquivalentsToCss(css: string, rootFontSize: number): string {
  if (!css.includes('em')) {
    return css
  }

  let comments: Comment[] = []

  try {
    postcss([postcssPlugin({ comments, rootFontSize })]).process(css, { from: undefined }).css
  } catch {
    return css
  }

  return applyComments(css, comments)
}

function applyComments(str: string, comments: Comment[]): string {
  let offset = 0

  for (let comment of comments) {
    let index = comment.index + offset
    let commentStr = `/* ${comment.value} */`
    str = str.slice(0, index) + commentStr + str.slice(index)
    offset += commentStr.length
  }

  return str
}

function getPixelEquivalentsForMediaQuery(params: string, rootFontSize: number): Comment[] {
  let comments: Comment[] = []

  try {
    parseMediaQueryList(params).forEach((mediaQuery) => {
      mediaQuery.walk(({ node }) => {
        if (
          isTokenNode(node) &&
          node.type === 'token' &&
          node.value[0] === 'dimension-token' &&
          (node.value[4].type === 'integer' || node.value[4].type === 'number') &&
          (node.value[4].unit === 'rem' || node.value[4].unit === 'em')
        ) {
          comments.push({
            index: params.length - (params.length - node.value[3] - 1),
            value: `${node.value[4].value * rootFontSize}px`,
          })
        }
      })
    })
  } catch {}

  return comments
}

export function addPixelEquivalentsToMediaQuery(query: string, rootFontSize: number): string {
  return query.replace(/(?<=^\s*@media\s*).*?$/, (params) => {
    let comments = getPixelEquivalentsForMediaQuery(params, rootFontSize)
    return applyComments(params, comments)
  })
}

function postcssPlugin({
  comments,
  rootFontSize,
}: {
  comments: Comment[]
  rootFontSize: number
}): Plugin {
  return {
    postcssPlugin: 'plugin',
    AtRule: {
      media(atRule) {
        if (!atRule.params.includes('em')) {
          return
        }

        comments.push(
          ...getPixelEquivalentsForMediaQuery(atRule.params, rootFontSize).map(
            ({ index, value }) => ({
              index: index + atRule.source.start.offset + `@media${atRule.raws.afterName}`.length,
              value,
            })
          )
        )
      },
    },
    Declaration(decl) {
      if (!decl.value.includes('rem')) {
        return
      }

      parseValue(decl.value).walk((node) => {
        if (node.type !== 'word') {
          return true
        }

        let unit = parseValue.unit(node.value)
        if (!unit || unit.unit !== 'rem') {
          return false
        }

        comments.push({
          index:
            decl.source.start.offset +
            `${decl.prop}${decl.raws.between}`.length +
            node.sourceEndIndex,
          value: `${parseFloat(unit.number) * rootFontSize}px`,
        })

        return false
      })
    },
  }
}
postcssPlugin.postcss = true