Commit 2624d531cb

2624d531cb166e704dfa459a7a063d199ba77493

parent: 40efa57360

Unsigned

Duck <57977673+woducku@users.noreply.github.com> · 2026-06-20 04:23 UTC

Copy button

Layout: unified · split

docs/.vitepress/theme/Layout.vue +70 −1
@@ -503,6 +503,7 @@ watch(() => route.data, () => {
503503 void tryOpenAnchoredContent()
504504 rewriteAnchorLinksDeferred()
505505 nextTick(setupOutlineFollow)
506 nextTick(setupManualCopyButtons)
506507}, { flush: 'post' })
507508watch(() => route.query, () => {
508509 void tryOpenAnchoredContent()
@@ -655,9 +656,75 @@ function initTitleOnly() {
655656 })
656657}
657658
659function initCopyButtonsInRoot(root: ParentNode) {
660 root.querySelectorAll<HTMLTableElement>('table').forEach(table => {
661 const headers = table.querySelectorAll<HTMLTableCellElement>('thead th')
662 let manualColIndex = -1
663 headers.forEach((th, i) => {
664 if (th.textContent?.trim() === 'Manual') manualColIndex = i
665 })
666 if (manualColIndex === -1) return
667
668 table.querySelectorAll<HTMLTableRowElement>('tbody tr').forEach(row => {
669 const cell = row.querySelectorAll('td')[manualColIndex]
670 if (!cell || cell.querySelector('.manual-copy-btn')) return
671
672 const link = cell.querySelector<HTMLAnchorElement>('a[href]')
673 if (!link) return
674 const url = link.getAttribute('href')!
675
676 const btn = document.createElement('button')
677 btn.className = 'manual-copy-btn'
678 btn.title = 'Copy URL'
679 const icon = document.createElement('span')
680 icon.className = 'i-lucide:copy'
681 btn.appendChild(icon)
682
683 btn.addEventListener('click', async (e) => {
684 e.preventDefault()
685 try {
686 await navigator.clipboard.writeText(url)
687 icon.className = 'i-lucide:check'
688 btn.classList.add('copied')
689 setTimeout(() => {
690 icon.className = 'i-lucide:copy'
691 btn.classList.remove('copied')
692 }, 2000)
693 } catch {}
694 })
695
696 cell.classList.add('has-manual-copy')
697 cell.appendChild(btn)
698 })
699 })
700}
701
702let copyButtonObserver: MutationObserver | null = null
703
704function setupManualCopyButtons() {
705 initCopyButtonsInRoot(document)
706
707 copyButtonObserver?.disconnect()
708 copyButtonObserver = new MutationObserver((mutations) => {
709 for (const mutation of mutations) {
710 for (const node of mutation.addedNodes) {
711 if (!(node instanceof HTMLElement)) continue
712 if (node.classList.contains('plugin-tabs--content')) {
713 initCopyButtonsInRoot(node)
714 } else {
715 node.querySelectorAll<HTMLElement>('.plugin-tabs--content').forEach(
716 panel => initCopyButtonsInRoot(panel)
717 )
718 }
719 }
720 }
721 })
722 copyButtonObserver.observe(document.body, { childList: true, subtree: true })
723}
724
658725watch(
659726 () => route.path,
660 () => nextTick(() => initTitleOnly()),
727 () => nextTick(() => { initTitleOnly(); setupManualCopyButtons() }),
661728 { flush: 'post' }
662729)
663730
@@ -665,6 +732,7 @@ onMounted(() => {
665732 if (import.meta.env.SSR) return
666733
667734 initTitleOnly()
735 nextTick(setupManualCopyButtons)
668736
669737 // Icon tooltip: fixed-position popup appended to <body> to escape table overflow clipping
670738 const tooltipEl = document.createElement('div')
@@ -867,6 +935,7 @@ onMounted(() => {
867935
868936onUnmounted(() => {
869937 outlineFollowObserver?.disconnect()
938 copyButtonObserver?.disconnect()
870939 cancelAnimationFrame(outlineFollowRaf)
871940 takodachiDisable.value && takodachiDisable.value()
872941})
docs/.vitepress/theme/styles/style.scss +35
@@ -1580,6 +1580,41 @@ html.reduce-blur-effects {
15801580 }
15811581}
15821582
1583/* Manual column copy button */
1584.vp-doc table td.has-manual-copy {
1585 display: flex;
1586 align-items: center;
1587 gap: 6px;
1588}
1589
1590.manual-copy-btn {
1591 display: inline-flex;
1592 align-items: center;
1593 justify-content: center;
1594 padding: 2px;
1595 aspect-ratio: 1;
1596 border: 1px solid var(--vp-c-divider);
1597 border-radius: 4px;
1598 background: var(--vp-c-bg-elv);
1599 color: var(--vp-c-text-2);
1600 cursor: pointer;
1601 font-size: 13px;
1602 line-height: 1;
1603 transition: color 0.15s, border-color 0.15s, background 0.15s;
1604 flex-shrink: 0;
1605
1606 &:hover {
1607 background: var(--vp-c-bg-alt);
1608 color: var(--vp-c-text-1);
1609 border-color: var(--vp-c-brand-1);
1610 }
1611
1612 &.copied {
1613 color: var(--vp-c-green-1);
1614 border-color: var(--vp-c-green-1);
1615 }
1616}
1617
15831618/* ScrapeTable */
15841619:root {
15851620 --scrape-cell-bg: #ebedf0;
unocss.config.ts +3 −1
@@ -37,7 +37,9 @@ const safelist = [
3737 'i-lucide:circle-x',
3838 'i-lucide:lightbulb',
3939 'i-lucide:heart',
40 'i-lucide:message-circle'
40 'i-lucide:message-circle',
41 'i-lucide:copy',
42 'i-lucide:check'
4143]
4244
4345export default defineConfig({