-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
41 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use client' | ||
|
||
import { useEffect } from "react" | ||
|
||
export default function BlogLayout({ children }) { | ||
useEffect(()=>{ | ||
// 注册复制代码的按钮功能 | ||
const btns = document.querySelectorAll('.copy-code-button') | ||
const handleClick = (event) => { | ||
const button = event.target; | ||
const codeElement = button.closest('pre').querySelector('code'); | ||
if (codeElement) { | ||
const codeContent = codeElement.textContent; | ||
// 复制到剪贴板 | ||
navigator.clipboard.writeText(codeContent).then(() => { | ||
button.innerText = 'Copied!'; | ||
setTimeout(() => { button.innerText = 'Copy'; }, 2000); | ||
}).catch(err => { | ||
console.error('Failed to copy code:', err); | ||
}); | ||
} | ||
}; | ||
|
||
btns.forEach(button => { | ||
button.addEventListener('click', handleClick); | ||
}); | ||
|
||
// 清理函数,组件卸载时移除事件监听器 | ||
return () => { | ||
btns.forEach(button => { | ||
button.removeEventListener('click', handleClick); | ||
}); | ||
}; | ||
}, []) | ||
return <> | ||
{children} | ||
</> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters