Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: switch tab to sync multiple group status #4574

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/client/app/composables/codeGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,38 @@ export function useCodeGroups() {
}

if (inBrowser) {
function syncMultipleCodeGroups(group: HTMLElement) {
const selector = group.className
.split(' ')
.filter(Boolean)
.map((c) => `.${c}`)
.join('')
let checkTabText =
group.querySelector('input:checked')?.nextElementSibling?.textContent ||
''
document.querySelectorAll(selector).forEach((groupEl) => {
if (group === groupEl) {
return
}
const labels = groupEl.querySelectorAll('label')
if (!labels.length) return
const targetIndex = Array.from(labels).findIndex(
(label) => label.textContent === checkTabText
)
if (targetIndex < 0) return
const input = labels[targetIndex]
.previousElementSibling as HTMLInputElement
input.checked = true
const blocks = groupEl.querySelector('.blocks')
if (!blocks) return
const current = blocks.children[targetIndex]
if (!current) return
Array.from(blocks.children).forEach((child) => {
child.classList.remove('active')
})
blocks.children[targetIndex].classList.add('active')
})
}
window.addEventListener('click', (e) => {
const el = e.target as HTMLInputElement

Expand All @@ -38,6 +70,8 @@ export function useCodeGroups() {
current.classList.remove('active')
next.classList.add('active')

syncMultipleCodeGroups(group)

const label = group?.querySelector(`label[for="${el.id}"]`)
label?.scrollIntoView({ block: 'nearest' })
}
Expand Down