Skip to content

Commit

Permalink
refactor(copy): 使用clipboard做复制,同时保留原有的复制方法
Browse files Browse the repository at this point in the history
  • Loading branch information
Lete114 committed Apr 19, 2022
1 parent 1849c5f commit f1d12cb
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions source/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,15 @@ function codeCopy() {
// 获取所有代码块
// firstChild: 获取代码块中的第一个子元素
// childNodes: 返回当前元素的所有子元素(包括:before和:after)
var clipboard = item.firstChild.childNodes[1]
clipboard.onclick = function () {
var copy = item.firstChild.childNodes[1]
copy.onclick = function () {
var selection = window.getSelection()
selection.selectAllChildren(item.querySelector('.code'))
document.execCommand('copy')
navigator.clipboard ? navigator.clipboard.writeText(selection.toString()) : document.execCommand('copy')
selection.removeAllRanges()
clipboard.innerHTML = '<i class="fa fa-check" style="color:green"></i>'
copy.innerHTML = '<i class="fa fa-check" style="color:green"></i>'
setTimeout(function () {
clipboard.innerHTML = '<i class="fa fa-clipboard"></i>'
copy.innerHTML = '<i class="fa fa-clipboard"></i>'
}, 2000)
}
})
Expand Down

0 comments on commit f1d12cb

Please sign in to comment.