Skip to content

Commit fb869f4

Browse files
committed
feat(theme): auto compute color variants
1 parent 6f9eedb commit fb869f4

File tree

5 files changed

+63
-11
lines changed

5 files changed

+63
-11
lines changed

eslint.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,10 @@ export default defineConfigWithVueTs(
2424
...pluginVitest.configs.recommended,
2525
files: ['src/**/__tests__/*'],
2626
},
27+
{
28+
rules: {
29+
'vue/multi-word-component-names': 'off',
30+
},
31+
},
2732
skipFormatting,
2833
)

src/views/ThemeEditor.vue

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
:inactive-icon="Sunny"
1717
style="margin-right: 10px"
1818
/>
19+
<el-switch
20+
v-model="autoColorCalc"
21+
active-text="色值自动计算"
22+
inactive-text="手动"
23+
style="margin-right: 10px"
24+
/>
1925
<el-button @click="saveObjectAsCss('cssVar.css')">导出</el-button>
2026
<!-- <el-button>编辑</el-button> -->
2127
<el-button type="primary">保存</el-button>
@@ -348,6 +354,7 @@ import Github from '@/components/icons/Github.vue'
348354
const activeName = ref('first')
349355
const activeNames = ref(['1'])
350356
const modifyCssVar: Ref<Record<string, string>> = ref({})
357+
const autoColorCalc = ref(true)
351358
352359
const isDark = useDark({
353360
selector: 'html',
@@ -358,10 +365,52 @@ const isDark = useDark({
358365
storage: localStorage,
359366
})
360367
368+
const mixColor = (color1: string, color2: string, weight: number) => {
369+
const w = Math.max(Math.min(weight, 1), 0)
370+
const toHex = (color: string) => color.replace('#', '')
371+
const c1 = toHex(color1)
372+
const c2 = toHex(color2)
373+
const r = Math.round(parseInt(c1.slice(0, 2), 16) * w + parseInt(c2.slice(0, 2), 16) * (1 - w))
374+
const g = Math.round(parseInt(c1.slice(2, 4), 16) * w + parseInt(c2.slice(2, 4), 16) * (1 - w))
375+
const b = Math.round(parseInt(c1.slice(4, 6), 16) * w + parseInt(c2.slice(4, 6), 16) * (1 - w))
376+
return `#${r.toString(16).padStart(2, '0')}${g
377+
.toString(16)
378+
.padStart(2, '0')}${b.toString(16).padStart(2, '0')}`
379+
}
380+
381+
const recomputeColorVars = (cssVar: string, base: string) => {
382+
const match = cssVar.match(/^--el-color-(\w+)$/)
383+
if (!match) return
384+
const type = match[1]
385+
const targetType = type === 'warning' ? 'warnning' : type
386+
const group = color.find((c) => c.type === targetType)
387+
if (!group) return
388+
group.data.forEach((item) => {
389+
if (item.cssVar === cssVar) return
390+
const light = item.cssVar.match(/light-(\d+)$/)
391+
const dark = item.cssVar.match(/dark-(\d+)$/)
392+
let value: string | undefined
393+
if (light) {
394+
value = mixColor('#ffffff', base, Number(light[1]) / 10)
395+
} else if (dark) {
396+
value = mixColor('#000000', base, Number(dark[1]) / 10)
397+
}
398+
if (value) {
399+
item.value = value
400+
setCssVarValue(item.cssVar, value)
401+
modifyCssVar.value[item.cssVar] = value
402+
}
403+
})
404+
}
405+
361406
const updateCssVar = (props: CssVarInfo) => {
362407
const value = props.value + props.unit
363408
setCssVarValue(props.cssVar, value)
364409
modifyCssVar.value[props.cssVar] = value
410+
411+
if (autoColorCalc.value) {
412+
recomputeColorVars(props.cssVar, props.value)
413+
}
365414
}
366415
367416
const setCssVarValue = (name: string, value: string) => {

src/views/components/data/Tree.vue

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,28 @@
2424
</template>
2525
<script lang="ts" setup>
2626
import type Node from 'element-plus/es/components/tree/src/model/node'
27-
import type { DragEvents } from 'element-plus/es/components/tree/src/model/useDragNode'
2827
import type { AllowDropType, NodeDropType } from 'element-plus/es/components/tree/src/tree.type'
2928
30-
const handleDragStart = (node: Node, ev: DragEvents) => {
29+
const handleDragStart = (node: Node) => {
3130
console.log('drag start', node)
3231
}
33-
const handleDragEnter = (draggingNode: Node, dropNode: Node, ev: DragEvents) => {
32+
const handleDragEnter = (draggingNode: Node, dropNode: Node) => {
3433
console.log('tree drag enter:', dropNode.label)
3534
}
36-
const handleDragLeave = (draggingNode: Node, dropNode: Node, ev: DragEvents) => {
35+
const handleDragLeave = (draggingNode: Node, dropNode: Node) => {
3736
console.log('tree drag leave:', dropNode.label)
3837
}
39-
const handleDragOver = (draggingNode: Node, dropNode: Node, ev: DragEvents) => {
38+
const handleDragOver = (draggingNode: Node, dropNode: Node) => {
4039
console.log('tree drag over:', dropNode.label)
4140
}
4241
const handleDragEnd = (
4342
draggingNode: Node,
4443
dropNode: Node,
45-
dropType: NodeDropType,
46-
ev: DragEvents
44+
dropType: NodeDropType
4745
) => {
4846
console.log('tree drag end:', dropNode && dropNode.label, dropType)
4947
}
50-
const handleDrop = (draggingNode: Node, dropNode: Node, dropType: NodeDropType, ev: DragEvents) => {
48+
const handleDrop = (draggingNode: Node, dropNode: Node, dropType: NodeDropType) => {
5149
console.log('tree drop:', dropNode.label, dropType)
5250
}
5351
const allowDrop = (draggingNode: Node, dropNode: Node, type: AllowDropType) => {

src/views/components/form/Upload.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const handleExceed: UploadProps['onExceed'] = (files, uploadFiles) => {
9090
)
9191
}
9292
93-
const beforeRemove: UploadProps['beforeRemove'] = (uploadFile, uploadFiles) => {
93+
const beforeRemove: UploadProps['beforeRemove'] = (uploadFile) => {
9494
return ElMessageBox.confirm(`Cancel the transfer of ${uploadFile.name} ?`).then(
9595
() => true,
9696
() => false

tailwind.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ export default {
290290
'justify-content': 'space-between',
291291
'align-items': 'center',
292292
},
293-
}),
294-
addVariant('black', '.dark &')
293+
})
294+
addVariant('black', '.dark &')
295295
}),
296296
],
297297
}

0 commit comments

Comments
 (0)