Blame view

天文台pc/tianwentai-ui/node_modules/tailwind-merge/src/lib/class-group-utils.ts 5.34 KB
bc518174   王天杨   提交两个项目文件
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
  import {
      AnyClassGroupIds,
      AnyConfig,
      AnyThemeGroupIds,
      ClassGroup,
      ClassValidator,
      Config,
      ThemeGetter,
      ThemeObject,
  } from './types'
  
  export interface ClassPartObject {
      nextPart: Map<string, ClassPartObject>
      validators: ClassValidatorObject[]
      classGroupId?: AnyClassGroupIds
  }
  
  interface ClassValidatorObject {
      classGroupId: AnyClassGroupIds
      validator: ClassValidator
  }
  
  const CLASS_PART_SEPARATOR = '-'
  
  export const createClassGroupUtils = (config: AnyConfig) => {
      const classMap = createClassMap(config)
      const { conflictingClassGroups, conflictingClassGroupModifiers } = config
  
      const getClassGroupId = (className: string) => {
          const classParts = className.split(CLASS_PART_SEPARATOR)
  
          // Classes like `-inset-1` produce an empty string as first classPart. We assume that classes for negative values are used correctly and remove it from classParts.
          if (classParts[0] === '' && classParts.length !== 1) {
              classParts.shift()
          }
  
          return getGroupRecursive(classParts, classMap) || getGroupIdForArbitraryProperty(className)
      }
  
      const getConflictingClassGroupIds = (
          classGroupId: AnyClassGroupIds,
          hasPostfixModifier: boolean,
      ) => {
          const conflicts = conflictingClassGroups[classGroupId] || []
  
          if (hasPostfixModifier && conflictingClassGroupModifiers[classGroupId]) {
              return [...conflicts, ...conflictingClassGroupModifiers[classGroupId]!]
          }
  
          return conflicts
      }
  
      return {
          getClassGroupId,
          getConflictingClassGroupIds,
      }
  }
  
  const getGroupRecursive = (
      classParts: string[],
      classPartObject: ClassPartObject,
  ): AnyClassGroupIds | undefined => {
      if (classParts.length === 0) {
          return classPartObject.classGroupId
      }
  
      const currentClassPart = classParts[0]!
      const nextClassPartObject = classPartObject.nextPart.get(currentClassPart)
      const classGroupFromNextClassPart = nextClassPartObject
          ? getGroupRecursive(classParts.slice(1), nextClassPartObject)
          : undefined
  
      if (classGroupFromNextClassPart) {
          return classGroupFromNextClassPart
      }
  
      if (classPartObject.validators.length === 0) {
          return undefined
      }
  
      const classRest = classParts.join(CLASS_PART_SEPARATOR)
  
      return classPartObject.validators.find(({ validator }) => validator(classRest))?.classGroupId
  }
  
  const arbitraryPropertyRegex = /^\[(.+)\]$/
  
  const getGroupIdForArbitraryProperty = (className: string) => {
      if (arbitraryPropertyRegex.test(className)) {
          const arbitraryPropertyClassName = arbitraryPropertyRegex.exec(className)![1]
          const property = arbitraryPropertyClassName?.substring(
              0,
              arbitraryPropertyClassName.indexOf(':'),
          )
  
          if (property) {
              // I use two dots here because one dot is used as prefix for class groups in plugins
              return 'arbitrary..' + property
          }
      }
  }
  
  /**
   * Exported for testing only
   */
  export const createClassMap = (config: Config<AnyClassGroupIds, AnyThemeGroupIds>) => {
      const { theme, classGroups } = config
      const classMap: ClassPartObject = {
          nextPart: new Map<string, ClassPartObject>(),
          validators: [],
      }
  
      for (const classGroupId in classGroups) {
          processClassesRecursively(classGroups[classGroupId]!, classMap, classGroupId, theme)
      }
  
      return classMap
  }
  
  const processClassesRecursively = (
      classGroup: ClassGroup<AnyThemeGroupIds>,
      classPartObject: ClassPartObject,
      classGroupId: AnyClassGroupIds,
      theme: ThemeObject<AnyThemeGroupIds>,
  ) => {
      classGroup.forEach((classDefinition) => {
          if (typeof classDefinition === 'string') {
              const classPartObjectToEdit =
                  classDefinition === '' ? classPartObject : getPart(classPartObject, classDefinition)
              classPartObjectToEdit.classGroupId = classGroupId
              return
          }
  
          if (typeof classDefinition === 'function') {
              if (isThemeGetter(classDefinition)) {
                  processClassesRecursively(
                      classDefinition(theme),
                      classPartObject,
                      classGroupId,
                      theme,
                  )
                  return
              }
  
              classPartObject.validators.push({
                  validator: classDefinition,
                  classGroupId,
              })
  
              return
          }
  
          Object.entries(classDefinition).forEach(([key, classGroup]) => {
              processClassesRecursively(
                  classGroup,
                  getPart(classPartObject, key),
                  classGroupId,
                  theme,
              )
          })
      })
  }
  
  const getPart = (classPartObject: ClassPartObject, path: string) => {
      let currentClassPartObject = classPartObject
  
      path.split(CLASS_PART_SEPARATOR).forEach((pathPart) => {
          if (!currentClassPartObject.nextPart.has(pathPart)) {
              currentClassPartObject.nextPart.set(pathPart, {
                  nextPart: new Map(),
                  validators: [],
              })
          }
  
          currentClassPartObject = currentClassPartObject.nextPart.get(pathPart)!
      })
  
      return currentClassPartObject
  }
  
  const isThemeGetter = (func: ClassValidator | ThemeGetter): func is ThemeGetter =>
      (func as ThemeGetter).isThemeGetter