mirror of
https://ghfast.top/https://github.com/zsjy/halo-theme-dream2.0-plus.git
synced 2025-03-16 00:49:40 +08:00
perf(code): 代码块支持选中行数,修复无法折叠bug
This commit is contained in:
parent
33442b52bd
commit
e03cbe4e83
@ -3560,6 +3560,16 @@ button.swiper-pagination-bullet {
|
||||
&:before {
|
||||
content: none !important;
|
||||
}
|
||||
|
||||
&.code-select:after {
|
||||
content: '';
|
||||
width: 100%;
|
||||
height: 1.5em;
|
||||
left: 0;
|
||||
background: rgba(151, 151, 151, 0.08);
|
||||
position: absolute;
|
||||
pointer-events: none;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
134
src/js/post.js
134
src/js/post.js
@ -1,55 +1,5 @@
|
||||
let postContextInitial = false
|
||||
const postContext = {
|
||||
/* 初始化事件 */
|
||||
initEvent() {
|
||||
if (postContextInitial) return
|
||||
let $body = $('body')
|
||||
// 代码块展开和关闭点击事件
|
||||
$body.on('click', 'figure>figcaption .fa-angle-down', function () {
|
||||
let $this = $(this)
|
||||
if ($this.is('.close')) {
|
||||
$($this.attr('data-code')).parent().slideDown(200)
|
||||
$this.removeClass('close')
|
||||
} else {
|
||||
$($this.attr('data-code')).parent().slideUp(200)
|
||||
$this.addClass('close')
|
||||
}
|
||||
})
|
||||
// 代码内容块展开和折叠点击事件
|
||||
$body.on('click', 'figure > pre > .expand-done', function () {
|
||||
Utils.foldBlock($(this).parent().parent())
|
||||
})
|
||||
// 图片的展开和折叠事件
|
||||
$body.on('click', '.gallery-item .expand-done', function (e) {
|
||||
e.stopPropagation()
|
||||
Utils.foldBlock($(this).parent())
|
||||
})
|
||||
// 喜欢
|
||||
Utils.initLikeEvent('.admire .agree.like', 'posts', ($elem) => $elem.find('span').find('span'))
|
||||
// 隐藏内容
|
||||
window.onCommentSuccessEvent = (comment, target) => {
|
||||
let name = encrypt('mew-hide-' + target)
|
||||
let commentIds = localStorage.getItem(name)
|
||||
commentIds = commentIds ? JSON.parse(decrypt(commentIds)) : []
|
||||
let id = String(comment.postId)
|
||||
if (commentIds.includes(id)) {
|
||||
return
|
||||
}
|
||||
commentIds.push(id)
|
||||
$(`.main-content[data-target='${target}'][data-id='${id}'] mew-hide[hide]`)
|
||||
.each(function () {
|
||||
$(this).before(decrypt(this.getAttribute('hide')))
|
||||
$(this).remove()
|
||||
commonContext.initGallery()
|
||||
postContext.initCodeBlock()
|
||||
postContext.initLiterature()
|
||||
postContext.initHighlighting()
|
||||
if (this.getAttribute('toc') === 'true') commonContext.initTocAndNotice()
|
||||
})
|
||||
localStorage.setItem(name, encrypt(JSON.stringify(commentIds)))
|
||||
}
|
||||
postContextInitial = true
|
||||
},
|
||||
/* 初始化代码块 */
|
||||
initCodeBlock() {
|
||||
const $code = $('*:not(figure) > pre > code')
|
||||
@ -59,17 +9,33 @@ const postContext = {
|
||||
let clazz = $(this).attr('class')
|
||||
// 通过class初始化代码块标题和是否默认关闭
|
||||
let title = ''
|
||||
let lines = [0, 0]
|
||||
let isClose = false
|
||||
if (clazz != null) {
|
||||
let num = clazz.indexOf('|')
|
||||
let closeNum = clazz.indexOf('<')
|
||||
if (num !== -1 || closeNum !== -1) {
|
||||
if (num === -1 || (closeNum !== -1 && closeNum < num)) {
|
||||
let str1 = clazz.match(/[|<](.*)$/)
|
||||
let str2 = clazz.match(/:(\d+)-(\d+)/)
|
||||
if (str1 || str2) {
|
||||
let num = 0
|
||||
if (str2) {
|
||||
num = str2.index
|
||||
if (str1) {
|
||||
if (str1[1].endsWith(str2[0])) {
|
||||
str1[1] = str1[1].substring(0, str2[0].length)
|
||||
}
|
||||
} else {
|
||||
title = clazz.substring(9, str2.index)
|
||||
}
|
||||
lines[0] = Number(str2[1])
|
||||
lines[1] = Number(str2[2])
|
||||
}
|
||||
if (str1) {
|
||||
num = str1.index < num ? str1.index : num
|
||||
if (str1[0][0] === '<') {
|
||||
isClose = true
|
||||
num = closeNum
|
||||
}
|
||||
title = str1[1]
|
||||
}
|
||||
$(this).attr('class', clazz.substring(0, num))
|
||||
title = num === clazz.length - 1 ? clazz.substring(9, num) : clazz.substring(num + 1)
|
||||
} else {
|
||||
title = clazz.substring(9)
|
||||
}
|
||||
@ -79,15 +45,15 @@ const postContext = {
|
||||
let lineDigit = String(nums).length
|
||||
if (lineDigit === 1) lineDigit = 2
|
||||
let lis = ''
|
||||
for (var i = 0; i < nums; i++) {
|
||||
lis += `<li>${String(i + 1).padStart(lineDigit, 0)}</li>`
|
||||
for (var i = 1; i <= nums; i++) {
|
||||
lis += `<li ${i >= lines[0] && i <= lines[1]? 'class="code-select"' : ''}>${String(i).padStart(lineDigit, 0)}</li>`
|
||||
}
|
||||
// 代码块的id,用于代码块复制和折叠
|
||||
let id = `codeBlock${index}-${new Date().getTime()}`
|
||||
let close = ''
|
||||
if (isClose) {
|
||||
close = ' close'
|
||||
$(this).hide()
|
||||
$(this).parent().hide()
|
||||
}
|
||||
// 生成标题栏的按钮
|
||||
let titleButton = `<div><i class="fa fa-angle-down${close}" data-code='#${id}'></i><i class="fa fa-clone btn-clipboard" title="复制代码" data-clipboard-target='#${id}'></i></div>`
|
||||
@ -169,6 +135,56 @@ const postContext = {
|
||||
}
|
||||
})
|
||||
},
|
||||
/* 初始化事件 */
|
||||
initEvent() {
|
||||
if (postContextInitial) return
|
||||
let $body = $('body')
|
||||
// 代码块展开和关闭点击事件
|
||||
$body.on('click', 'figure>figcaption .fa-angle-down', function () {
|
||||
let $this = $(this)
|
||||
if ($this.is('.close')) {
|
||||
$($this.attr('data-code')).parent().slideDown(200)
|
||||
$this.removeClass('close')
|
||||
} else {
|
||||
$($this.attr('data-code')).parent().slideUp(200)
|
||||
$this.addClass('close')
|
||||
}
|
||||
})
|
||||
// 代码内容块展开和折叠点击事件
|
||||
$body.on('click', 'figure > pre > .expand-done', function () {
|
||||
Utils.foldBlock($(this).parent().parent())
|
||||
})
|
||||
// 图片的展开和折叠事件
|
||||
$body.on('click', '.gallery-item .expand-done', function (e) {
|
||||
e.stopPropagation()
|
||||
Utils.foldBlock($(this).parent())
|
||||
})
|
||||
// 喜欢
|
||||
Utils.initLikeEvent('.admire .agree.like', 'posts', ($elem) => $elem.find('span').find('span'))
|
||||
// 隐藏内容
|
||||
window.onCommentSuccessEvent = (comment, target) => {
|
||||
let name = encrypt('mew-hide-' + target)
|
||||
let commentIds = localStorage.getItem(name)
|
||||
commentIds = commentIds ? JSON.parse(decrypt(commentIds)) : []
|
||||
let id = String(comment.postId)
|
||||
if (commentIds.includes(id)) {
|
||||
return
|
||||
}
|
||||
commentIds.push(id)
|
||||
$(`.main-content[data-target='${target}'][data-id='${id}'] mew-hide[hide]`)
|
||||
.each(function () {
|
||||
$(this).before(decrypt(this.getAttribute('hide')))
|
||||
$(this).remove()
|
||||
commonContext.initGallery()
|
||||
postContext.initCodeBlock()
|
||||
postContext.initLiterature()
|
||||
postContext.initHighlighting()
|
||||
if (this.getAttribute('toc') === 'true') commonContext.initTocAndNotice()
|
||||
})
|
||||
localStorage.setItem(name, encrypt(JSON.stringify(commentIds)))
|
||||
}
|
||||
postContextInitial = true
|
||||
},
|
||||
/* 初始化评论区 */
|
||||
initComment() {
|
||||
const $mainContent = $('.main-content')
|
||||
|
2
templates/assets/js/dshare.min.js
vendored
2
templates/assets/js/dshare.min.js
vendored
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user