miao-plugin/apps/help/HelpTheme.js

69 lines
2.7 KiB
JavaScript
Raw Normal View History

import lodash from 'lodash'
2023-10-24 19:34:36 +00:00
import fs from 'node:fs'
2023-03-07 17:52:11 +00:00
import { Data } from '#miao'
2022-10-06 22:20:46 +00:00
let HelpTheme = {
async getThemeCfg (theme, exclude) {
let dirPath = './plugins/miao-plugin/resources/help/theme/'
let ret = []
let names = []
let dirs = fs.readdirSync(dirPath)
lodash.forEach(dirs, (dir) => {
if (fs.existsSync(`${dirPath}${dir}/main.png`)) {
names.push(dir)
}
})
if (lodash.isArray(theme)) {
ret = lodash.intersection(theme, names)
} else if (theme === 'all') {
ret = names
}
if (exclude && lodash.isArray(exclude)) {
ret = lodash.difference(ret, exclude)
}
if (ret.length === 0) {
ret = ['default']
}
let name = lodash.sample(ret)
let resPath = '{{_res_path}}/help/theme/'
return {
main: `${resPath}${name}/main.png`,
bg: fs.existsSync(`${dirPath}${name}/bg.jpg`) ? `${resPath}${name}/bg.jpg` : `${resPath}default/bg.jpg`,
2023-03-07 17:52:11 +00:00
style: (await Data.importModule(`resources/help/theme/${name}/config.js`, 'miao')).style || {}
}
},
async getThemeData (diyStyle, sysStyle) {
let helpConfig = lodash.extend({}, sysStyle, diyStyle)
let colCount = Math.min(5, Math.max(parseInt(helpConfig?.colCount) || 3, 2))
let colWidth = Math.min(500, Math.max(100, parseInt(helpConfig?.colWidth) || 265))
let width = Math.min(2500, Math.max(800, colCount * colWidth + 30))
2022-10-06 22:20:46 +00:00
let theme = await HelpTheme.getThemeCfg(diyStyle.theme || sysStyle.theme, diyStyle.themeExclude || sysStyle.themeExclude)
let themeStyle = theme.style || {}
let ret = [`
body{background-image:url(${theme.bg});width:${width}px;}
.container{background-image:url(${theme.main});width:${width}px;}
.help-table .td,.help-table .th{width:${100 / colCount}%}
`]
let css = function (sel, css, key, def, fn) {
let val = Data.def(themeStyle[key], diyStyle[key], sysStyle[key], def)
if (fn) {
val = fn(val)
}
ret.push(`${sel}{${css}:${val}}`)
}
css('.help-title,.help-group', 'color', 'fontColor', '#ceb78b')
css('.help-title,.help-group', 'text-shadow', 'fontShadow', 'none')
css('.help-desc', 'color', 'descColor', '#eee')
css('.cont-box', 'background', 'contBgColor', 'rgba(43, 52, 61, 0.8)')
2022-09-27 02:25:31 +00:00
css('.cont-box', 'backdrop-filter', 'contBgBlur', 3, (n) => diyStyle.bgBlur === false ? 'none' : `blur(${n}px)`)
css('.help-group', 'background', 'headerBgColor', 'rgba(34, 41, 51, .4)')
css('.help-table .tr:nth-child(odd)', 'background', 'rowBgColor1', 'rgba(34, 41, 51, .2)')
css('.help-table .tr:nth-child(even)', 'background', 'rowBgColor2', 'rgba(34, 41, 51, .4)')
return {
style: `<style>${ret.join('\n')}</style>`,
colCount
}
}
}
2022-10-06 22:20:46 +00:00
export default HelpTheme