web-component-gallery 2.1.21 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,8 @@
1
1
  // 引入所需依赖
2
2
  const fs = require("fs")
3
3
  const path = require("path")
4
+ // 指向宿主项目路径 ../../为node_modules路径
5
+ const hostPath = path.join(__dirname, '../../')
4
6
  const { generateTheme } = require('antd-theme-generator')
5
7
  const { cssFileName, parseCSSVariables, parseLessVariables, parseExtendVariables, fetchAndParseLessFile } = require('./UpdateTheme')
6
8
 
@@ -28,7 +30,7 @@ function addLessVariables(lessContent, newVars) {
28
30
  }
29
31
 
30
32
  // 如果没有找到:export块,则添加到文件末尾
31
- if (exportStartIndex === -1) exportStartIndex = lines.length
33
+ if (exportStartIndex === -1) exportStartIndex = lines.length
32
34
 
33
35
  // 添加非导出变量
34
36
  const addNonExportVars = () => {
@@ -69,8 +71,8 @@ async function processLessFiles(theme) {
69
71
  // 处理颜色变量
70
72
  let processedContent = fileBContent
71
73
 
72
- for (const [key, colorA] of Object.entries(fileAVariables)) {
73
- const colorB = extractColor(fileBContent, key)
74
+ for (const [key, colorA] of Object.entries(fileAVariables)) {
75
+ const colorB = extractColor(fileBContent, key)
74
76
 
75
77
  if (colorB) {
76
78
  processedContent = processedContent.replace(
@@ -94,11 +96,21 @@ async function processLessFiles(theme) {
94
96
  }
95
97
  )
96
98
 
99
+ const basePath = path.resolve(hostPath, '../src/styles')
100
+
97
101
  // 写入输出文件
98
- const outputPath = path.join(__dirname, 'src', 'styles', `${cssFileName}.less`)
102
+ const outputPath = path.join(basePath, `${cssFileName}.less`)
99
103
  fs.writeFileSync(outputPath, processedContent)
100
104
 
101
- return '文件处理完成'
105
+ // 转换为 "xxx:111;" 格式
106
+ // const formattedString = Object.entries(fileAVariables)
107
+ // .map(([key, value]) => `${key}: ${value};`)
108
+ // .join('\n') // 每行一个键值对
109
+
110
+ // const themeOutputPath = path.join(basePath, `${theme}.less`)
111
+ // fs.writeFileSync(themeOutputPath, formattedString)
112
+
113
+ return fileAVariables
102
114
  } catch (error) {
103
115
  console.error('处理失败:', error)
104
116
  throw error
@@ -107,32 +119,101 @@ async function processLessFiles(theme) {
107
119
 
108
120
  // 自定义less内置函数处理
109
121
  const customFunctions = {
110
- install: function (less) {
111
- const funcs = ['fade', 'ceil', 'tint', 'shade', 'lighten']
112
- funcs.forEach(func => {
113
- less.functions.functionRegistry.add(func, function () {
114
- const args = Array.from(arguments)
115
- .map(arg => arg.toCSS ? arg.toCSS() : arg.value)
116
- return new less.tree.Quoted('~"', `${func}(${args.join(',')})`)
117
- })
118
- })
122
+ install: function(less, pluginManager, functions) {
123
+
124
+ // 辅助函数:检查是否为 CSS 变量(Call 类型)
125
+ function isCSSVariable(color) {
126
+ return color.type === 'Call' &&
127
+ color.name === 'var' &&
128
+ color.args &&
129
+ color.args[0].value.indexOf('--') === 0
130
+ }
131
+
132
+ // 辅助函数:提取 CSS 变量信息
133
+ function extractCSSVariable(callNode) {
134
+ if (callNode.name === 'var') {
135
+ return {
136
+ varName: callNode.args[0].value,
137
+ defaultValue: callNode.args[1] ? callNode.args[1] : null
138
+ }
139
+ }
140
+ return null
119
141
  }
142
+
143
+ // tint 函数实现(支持 Call 类型和静态颜色)
144
+ functions.add('tint', function(color, amount) {
145
+ if (isCSSVariable(color)) {
146
+ const {varName, defaultValue} = extractCSSVariable(color)
147
+ return new less.tree.Quoted('"', `tint(${defaultValue}, ${amount.value}%)`)
148
+ }
149
+ // 处理静态颜色值
150
+ const ratio = Math.max(0, Math.min(1, parseFloat(amount.value) / 100))
151
+ return new less.tree.Color(
152
+ [
153
+ Math.min(255, Math.round(color.rgb[0] + (255 - color.rgb[0]) * ratio)),
154
+ Math.min(255, Math.round(color.rgb[1] + (255 - color.rgb[1]) * ratio)),
155
+ Math.min(255, Math.round(color.rgb[2] + (255 - color.rgb[2]) * ratio))
156
+ ],
157
+ color.alpha
158
+ )
159
+ })
160
+
161
+ // lighten 函数实现
162
+ functions.add('lighten', function(color, amount) {
163
+ if (isCSSVariable(color)) {
164
+ const {varName, defaultValue} = extractCSSVariable(color)
165
+ return new less.tree.Quoted('"', `lighten(${defaultValue}, ${amount.value}%)`)
166
+ }
167
+ // 处理静态颜色值
168
+ const hsl = color.toHSL()
169
+ const newLightness = Math.min(100, hsl.l + parseFloat(amount.value))
170
+ return new less.tree.Color(
171
+ [
172
+ Math.round(hsl.h * 360),
173
+ Math.round(hsl.s * 100),
174
+ Math.round(newLightness)
175
+ ],
176
+ color.alpha
177
+ )
178
+ })
179
+
180
+ // fade 函数实现
181
+ functions.add('fade', function(color, amount) {
182
+ if (isCSSVariable(color)) {
183
+ const {varName, defaultValue} = extractCSSVariable(color)
184
+ return new less.tree.Quoted('"', `fade(${defaultValue}, ${amount.value}%)`)
185
+ }
186
+ // 处理静态颜色值
187
+ const alpha = Math.max(0, Math.min(1, color.alpha * (1 - amount.value / 100)))
188
+ return new less.tree.Color(color.rgb, alpha)
189
+ })
190
+ }
120
191
  }
121
192
 
122
193
  // 生成AntdVue主题文件及自定义主题文件
123
194
  function createGeneratorTheme(theme) {
124
195
  // 初始化主题
125
196
  const resetTheme = theme || 'dark'
197
+ // 抛出基础色
198
+ const notIncludes = ['@white', '@black']
126
199
 
127
- // 输出AntdVue样式打包放置public引入
128
- const options = {
129
- antDir: path.join(__dirname, 'node_modules/ant-design-vue'),
130
- stylesDir: path.join(__dirname, 'node_modules/ant-design-vue/lib/style/themes'),
131
- outputFilePath: path.join(__dirname, 'public/static/style/antdVue.less')
132
- }
133
- generateTheme(options)
134
200
  // 输出自定义样式文件
135
- processLessFiles(resetTheme)
201
+ processLessFiles(resetTheme).then(themeVariables => {
202
+ // 输出AntdVue样式打包放置public引入
203
+ const options = {
204
+ antDir: path.join(hostPath, 'ant-design-vue'),
205
+ // 指定项目自定义样式文件目录
206
+ stylesDir: path.join(hostPath, 'ant-design-vue/lib/style/themes'),
207
+ // 指定项目自定义样式文件目录
208
+ // stylesDir: path.join(hostPath, '../src/styles'),
209
+ // 指定自定义主题变量文件路径
210
+ // varFile: path.join(hostPath, `../src/styles/${resetTheme}.less`),
211
+ // 列出需要动态替换的主题变量
212
+ themeVariables: Object.keys(themeVariables).filter(key => !notIncludes.includes(key)),
213
+ outputFilePath: path.join(hostPath, '../public/static/style/antdVue.less')
214
+ }
215
+ generateTheme(options)
216
+ })
136
217
  }
137
218
 
138
219
  module.exports = {
@@ -7,14 +7,21 @@ const colorPalette = require('../utils/ColorPalette')
7
7
 
8
8
  // 获取并解析LESS文件
9
9
  async function fetchAndParseLessFile(fileName, isParseVariables) {
10
- // 默认值需要给一个内外网都能访问到的固定地址
11
- let origin = process.env.VUE_APP_API_BASE_URL || 'http://qxfnkj.com'
10
+ // 确定请求源地址
11
+ let origin = 'http://qxfnkj.com'
12
+
13
+ // 浏览器环境:使用当前页面的origin
14
+ if (typeof window !== 'undefined' && window.location && window.location.origin && process.env.NODE_ENV !== 'development') origin = window.location.origin
15
+
12
16
  const response = await fetch(`${origin}/base-subject/themes/${fileName}.less`)
17
+
18
+ if (!response.ok) throw new Error(`${fileName}文件请求失败!`)
19
+
13
20
  const responseText = await response.text()
14
21
 
15
22
  return isParseVariables
16
- ? await parseLessVariables(responseText)
17
- : responseText
23
+ ? await parseLessVariables(responseText)
24
+ : responseText
18
25
  }
19
26
 
20
27
  // 解析LESS变量
@@ -105,11 +112,11 @@ async function updateTheme(theme) {
105
112
  if (colorA !== colorB) updateVariables[`@${key.slice(2)}`] = colorA
106
113
  }
107
114
 
115
+ less.modifyVars({...themeVariables, ...updateVariables}).then(console.log("换肤成功"))
116
+
108
117
  if (!Object.keys(updateVariables).length) return
109
118
 
110
119
  const updateCSSViables = parseExtendVariables(cssVariables, updateVariables).export
111
-
112
- less.modifyVars(updateVariables).then(console.log("换肤成功"))
113
120
 
114
121
  for (const [key, color] of Object.entries(updateCSSViables)) {
115
122
  root.style.setProperty(key, color)
@@ -22,7 +22,7 @@ import PageLoading from "./PageLoading"
22
22
  * 全局切换主题
23
23
  * @theme 主题key 使用方法: this.$setTheme(@theme)
24
24
  * */
25
- // import setTheme from './Theme'
25
+ import { updateTheme } from './UpdateTheme'
26
26
 
27
27
  /**
28
28
  * 全局挂载组件自定义指令
@@ -57,7 +57,7 @@ export default {
57
57
  /** 加载便捷弹窗等 */
58
58
  Vue.use(Dialog)
59
59
  Vue.use(PageLoading)
60
- // Vue.use(setTheme)
60
+ Vue.prototype.$setTheme = updateTheme
61
61
 
62
62
  // 指令注册
63
63
  Object.keys(Directives).forEach(key => {
@@ -3,7 +3,7 @@ import { Modal } from 'ant-design-vue/es'
3
3
  import IconFont from '../icon-font'
4
4
 
5
5
  const ModalProps = {
6
- mode: PropTypes.string.def('small'),
6
+ size: PropTypes.string.def('small'),
7
7
  title: PropTypes.string.def('标题'),
8
8
  visible: PropTypes.bool.def(false),
9
9
  cancelHandle: PropTypes.func
@@ -15,11 +15,11 @@ const ModalComp = {
15
15
  data() {
16
16
  return {
17
17
  modalModes: ['small', 'middle', 'large', 'max'],
18
- internalMode: this.mode
18
+ internalMode: this.size
19
19
  }
20
20
  },
21
21
  watch: {
22
- mode(newVal) {
22
+ size(newVal) {
23
23
  this.internalMode = newVal
24
24
  }
25
25
  },
@@ -30,7 +30,7 @@ const ModalComp = {
30
30
  handleConvert() {
31
31
  const currentIndex = this.modalModes.indexOf(this.internalMode)
32
32
  const isLastMode = currentIndex === this.modalModes.length - 1
33
- this.internalMode = isLastMode ? this.mode : this.modalModes[currentIndex + 1]
33
+ this.internalMode = isLastMode ? this.size : this.modalModes[currentIndex + 1]
34
34
  }
35
35
  },
36
36
  render() {
@@ -53,7 +53,7 @@ const ModalComp = {
53
53
  ...this.$listeners
54
54
  }}
55
55
  >
56
- {this.mode !== 'screen' && (
56
+ {this.size !== 'screen' && (
57
57
  <div slot="title" class="AntModal__Title">
58
58
  <span>{this.title}</span>
59
59
  <div class="AntModal__Title__Blank">
@@ -1,6 +1,3 @@
1
- @import '~ant-design-vue/lib/style/themes/default.less';
2
- @import './mixins.less';
3
-
4
1
  html,
5
2
  body,
6
3
  #app, #root {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "web-component-gallery",
3
- "version": "2.1.21",
3
+ "version": "2.2.0",
4
4
  "description": "基础vue、antdvue、less实现的私有组件库",
5
5
  "main": "dist/index.umd.js",
6
6
  "files": [