mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-21 11:54:05 -06:00
improve tag filtering
This commit is contained in:
parent
bcfa99fe22
commit
e8d4376728
@ -56,22 +56,30 @@ export default ((opts?: Partial<FolderContentOptions>) => {
|
|||||||
|
|
||||||
// Add client-side filtering script
|
// Add client-side filtering script
|
||||||
const filterScript = `
|
const filterScript = `
|
||||||
|
let isInitialized = false;
|
||||||
|
|
||||||
function initializeTagFiltering() {
|
function initializeTagFiltering() {
|
||||||
|
// Prevent double initialization
|
||||||
|
if (isInitialized) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const selectedTags = new Set()
|
const selectedTags = new Set()
|
||||||
const cards = document.querySelectorAll('.section-li')
|
const cards = document.querySelectorAll('.section-li')
|
||||||
const countEl = document.querySelector('.folder-count')
|
const countEl = document.querySelector('.folder-count')
|
||||||
const tagButtons = document.querySelectorAll('.tag-filter-btn')
|
const tagButtons = document.querySelectorAll('.tag-filter-btn')
|
||||||
|
|
||||||
// Debug logging
|
if (!cards.length || !tagButtons.length) {
|
||||||
console.log('Found ' + cards.length + ' cards')
|
return; // Don't initialize if elements aren't present
|
||||||
console.log('Found ' + tagButtons.length + ' tag buttons')
|
}
|
||||||
|
|
||||||
|
isInitialized = true;
|
||||||
|
|
||||||
function updateVisibility() {
|
function updateVisibility() {
|
||||||
let visibleCount = 0
|
let visibleCount = 0
|
||||||
cards.forEach(card => {
|
cards.forEach(card => {
|
||||||
try {
|
try {
|
||||||
const tagsAttr = card.getAttribute('data-tags')
|
const tagsAttr = card.getAttribute('data-tags')
|
||||||
console.log('Card tags:', tagsAttr)
|
|
||||||
const tags = JSON.parse(tagsAttr || '[]')
|
const tags = JSON.parse(tagsAttr || '[]')
|
||||||
const shouldShow = selectedTags.size === 0 ||
|
const shouldShow = selectedTags.size === 0 ||
|
||||||
tags.some(tag => selectedTags.has(tag))
|
tags.some(tag => selectedTags.has(tag))
|
||||||
@ -86,38 +94,41 @@ export default ((opts?: Partial<FolderContentOptions>) => {
|
|||||||
const countText = ${JSON.stringify(i18n(cfg.locale).pages.folderContent.itemsUnderFolder({count: 0}))}
|
const countText = ${JSON.stringify(i18n(cfg.locale).pages.folderContent.itemsUnderFolder({count: 0}))}
|
||||||
countEl.textContent = countText.replace('0', visibleCount.toString())
|
countEl.textContent = countText.replace('0', visibleCount.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debug logging
|
|
||||||
console.log('Selected tags:', Array.from(selectedTags))
|
|
||||||
console.log('Visible count:', visibleCount)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tagButtons.forEach(btn => {
|
tagButtons.forEach(btn => {
|
||||||
btn.addEventListener('click', () => {
|
// Remove any existing listeners first
|
||||||
const tag = btn.getAttribute('data-tag')
|
const newBtn = btn.cloneNode(true)
|
||||||
console.log('Clicked tag:', tag)
|
btn.parentNode.replaceChild(newBtn, btn)
|
||||||
|
|
||||||
|
newBtn.addEventListener('click', () => {
|
||||||
|
const tag = newBtn.getAttribute('data-tag')
|
||||||
if (selectedTags.has(tag)) {
|
if (selectedTags.has(tag)) {
|
||||||
selectedTags.delete(tag)
|
selectedTags.delete(tag)
|
||||||
btn.classList.remove('selected')
|
newBtn.classList.remove('selected')
|
||||||
} else {
|
} else {
|
||||||
selectedTags.add(tag)
|
selectedTags.add(tag)
|
||||||
btn.classList.add('selected')
|
newBtn.classList.add('selected')
|
||||||
}
|
}
|
||||||
updateVisibility()
|
updateVisibility()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to initialize immediately
|
// Initialize on first load
|
||||||
initializeTagFiltering()
|
initializeTagFiltering()
|
||||||
|
|
||||||
// Also try after a short delay to ensure DOM is ready
|
// Handle client-side navigation
|
||||||
setTimeout(initializeTagFiltering, 100)
|
document.addEventListener('nav', () => {
|
||||||
|
isInitialized = false;
|
||||||
// Also initialize on DOMContentLoaded if it hasn't fired yet
|
setTimeout(initializeTagFiltering, 0)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Backup initialization for edge cases
|
||||||
if (document.readyState === 'loading') {
|
if (document.readyState === 'loading') {
|
||||||
document.addEventListener('DOMContentLoaded', initializeTagFiltering)
|
document.addEventListener('DOMContentLoaded', initializeTagFiltering)
|
||||||
|
} else {
|
||||||
|
setTimeout(initializeTagFiltering, 0)
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user