mirror of
https://github.com/jerryc127/hexo-theme-butterfly.git
synced 2026-04-08 12:07:06 +08:00
Merge pull request #1778 from Void4m0n/feature/cloudtags-custom-colors
feat(cloudTags): add custom colors support for cloudTags on card_tags widget & tags page Pr_Satus(Finished)
This commit is contained in:
@@ -1,2 +1,2 @@
|
||||
.tag-cloud-list.text-center
|
||||
!=cloudTags({source: site.tags, orderby: page.orderby || 'random', order: page.order || 1, minfontsize: 1.2, maxfontsize: 1.5, limit: 0, unit: 'em'})
|
||||
!=cloudTags({source: site.tags, orderby: page.orderby || 'random', order: page.order || 1, minfontsize: 1.2, maxfontsize: 1.5, limit: 0, unit: 'em', custom_colors: page.custom_colors})
|
||||
|
||||
@@ -5,10 +5,10 @@ if theme.aside.card_tags.enable
|
||||
i.fas.fa-tags
|
||||
span= _p('aside.card_tags')
|
||||
|
||||
- let { limit, orderby, order } = theme.aside.card_tags
|
||||
- let { limit, orderby, order, custom_colors } = theme.aside.card_tags
|
||||
- limit = limit === 0 ? 0 : limit || 40
|
||||
|
||||
if theme.aside.card_tags.color
|
||||
.card-tag-cloud!= cloudTags({source: site.tags, orderby: orderby, order: order, minfontsize: 1.15, maxfontsize: 1.45, limit: limit, unit: 'em', page: 'index'})
|
||||
.card-tag-cloud!= cloudTags({source: site.tags, orderby: orderby, order: order, minfontsize: 1.15, maxfontsize: 1.45, limit: limit, unit: 'em', page: 'index', custom_colors: custom_colors})
|
||||
else
|
||||
.card-tag-cloud!= tagcloud({orderby: orderby, order: order, min_font: 1.1, max_font: 1.5, amount: limit , color: true, start_color: '#999', end_color: '#99a9bf', unit: 'em'})
|
||||
.card-tag-cloud!= tagcloud({orderby: orderby, order: order, min_font: 1.1, max_font: 1.5, amount: limit, color: true, start_color: '#999', end_color: '#99a9bf', unit: 'em'})
|
||||
|
||||
@@ -179,6 +179,7 @@ module.exports = {
|
||||
enable: true,
|
||||
limit: 40,
|
||||
color: false,
|
||||
custom_colors: null,
|
||||
orderby: 'random',
|
||||
order: 1,
|
||||
sort_order: null
|
||||
|
||||
@@ -19,7 +19,7 @@ hexo.extend.helper.register('postDesc', data => {
|
||||
|
||||
hexo.extend.helper.register('cloudTags', function (options = {}) {
|
||||
const env = this
|
||||
let { source, minfontsize, maxfontsize, limit, unit = 'px', orderby, order, page = 'tags' } = options
|
||||
let { source, minfontsize, maxfontsize, limit, unit = 'px', orderby, order, page = 'tags', custom_colors } = options
|
||||
|
||||
if (limit > 0) {
|
||||
source = source.limit(limit)
|
||||
@@ -36,15 +36,48 @@ hexo.extend.helper.register('cloudTags', function (options = {}) {
|
||||
return `rgb(${Math.max(r, 50)}, ${Math.max(g, 50)}, ${Math.max(b, 50)})`
|
||||
}
|
||||
|
||||
const generateStyle = (size, unit, page) => {
|
||||
const colorStyle = page === 'tags' ? `background-color: ${getRandomColor()};` : `color: ${getRandomColor()};`
|
||||
const normalizeColors = input => {
|
||||
if (!input) return null
|
||||
if (typeof input === 'string') {
|
||||
const color = input.trim()
|
||||
return color ? [color] : null
|
||||
}
|
||||
if (Array.isArray(input)) {
|
||||
const result = []
|
||||
for (let i = 0; i < input.length; i++) {
|
||||
const value = input[i]
|
||||
if (value === null || value === undefined) continue
|
||||
const color = String(value).trim()
|
||||
if (!color) continue
|
||||
result.push(color)
|
||||
}
|
||||
return result.length ? result : null
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
const userColors = normalizeColors(custom_colors)
|
||||
|
||||
const resolveColorClass = (idx) => `tag-color-${idx % userColors.length}`
|
||||
|
||||
const generateStyle = (size, unit, page, color) => {
|
||||
const colorStyle = page === 'tags' ? `background-color: ${color};` : `color: ${color};`
|
||||
return `font-size: ${parseFloat(size.toFixed(2))}${unit}; ${colorStyle}`
|
||||
}
|
||||
|
||||
return source.sort(orderby, order).map(tag => {
|
||||
return source.sort(orderby, order).map((tag, idx) => {
|
||||
const ratio = length ? sizeMap.get(tag.length) / length : 0
|
||||
const size = minfontsize + ((maxfontsize - minfontsize) * ratio)
|
||||
const style = generateStyle(size, unit, page)
|
||||
|
||||
if (userColors && userColors.length) {
|
||||
const colorClass = resolveColorClass(idx)
|
||||
const color = userColors[idx % userColors.length]
|
||||
const style = generateStyle(size, unit, page, color)
|
||||
return `<a href="${env.url_for(tag.path)}" class="tag-cloud-item ${colorClass}" style="${style}">${tag.name}</a>`
|
||||
}
|
||||
|
||||
const color = getRandomColor()
|
||||
const style = generateStyle(size, unit, page, color)
|
||||
return `<a href="${env.url_for(tag.path)}" style="${style}">${tag.name}</a>`
|
||||
}).join('')
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user