added 代码折叠 修复bug
This commit is contained in:
1 parent
c115c7c0fb
commit
33a6ce2d85
4 files changed
+212
-35
No files matched your search
@@ -114,12 +114,19 @@ window.initArtalk = function() {
|
||||
|
||||
// Inject AI Comment
|
||||
window.artalk.on('list-loaded', function() {
|
||||
if (window.artalkConfig.aiComment && !document.querySelector('.atk-ai-comment')) {
|
||||
var listBody = document.querySelector('.atk-list-body') || document.querySelector('.atk-list');
|
||||
if (listBody) {
|
||||
var aiData = window.artalkConfig.aiComment;
|
||||
var aiDiv = document.createElement('div');
|
||||
aiDiv.className = 'atk-comment atk-ai-comment';
|
||||
if (window.artalkConfig.aiComment) {
|
||||
// Force hide "No comments" message via CSS class
|
||||
var list = document.querySelector('.atk-list');
|
||||
if (list) {
|
||||
list.classList.add('has-ai-comment');
|
||||
}
|
||||
|
||||
if (!document.querySelector('.atk-ai-comment')) {
|
||||
var listBody = document.querySelector('.atk-list-body') || document.querySelector('.atk-list');
|
||||
if (listBody) {
|
||||
var aiData = window.artalkConfig.aiComment;
|
||||
var aiDiv = document.createElement('div');
|
||||
aiDiv.className = 'atk-comment atk-ai-comment';
|
||||
// Initially empty content for typewriter effect
|
||||
aiDiv.innerHTML = `
|
||||
<div class="atk-avatar">
|
||||
@@ -158,7 +165,8 @@ window.initArtalk = function() {
|
||||
setTimeout(type, 500);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Artalk 初始化完成后,再加载弹幕 (If available)
|
||||
if (typeof isMobile === 'function' && !isMobile() && window.loadBarrager) {
|
||||
@@ -190,7 +198,7 @@ function initCodeCopy() {
|
||||
|
||||
const copyBtn = document.createElement('button');
|
||||
copyBtn.className = 'copy-code-btn';
|
||||
copyBtn.innerHTML = '<i class="fa fa-copy"></i>'; // 使用 FontAwesome 图标
|
||||
copyBtn.innerHTML = '<i class="ri-file-copy-2-line"></i>'; // 使用 RemixIcon
|
||||
copyBtn.title = '复制代码';
|
||||
|
||||
// 样式 (建议放到 CSS 文件中)
|
||||
@@ -218,15 +226,15 @@ function initCodeCopy() {
|
||||
copyBtn.addEventListener('click', () => {
|
||||
const code = codeBlock.innerText;
|
||||
navigator.clipboard.writeText(code).then(() => {
|
||||
copyBtn.innerHTML = '<i class="fa fa-check"></i>';
|
||||
copyBtn.innerHTML = '<i class="ri-check-line"></i>';
|
||||
setTimeout(() => {
|
||||
copyBtn.innerHTML = '<i class="fa fa-copy"></i>';
|
||||
copyBtn.innerHTML = '<i class="ri-file-copy-2-line"></i>';
|
||||
}, 2000);
|
||||
|
||||
Qmsg.success("代码复制成功");
|
||||
}).catch(err => {
|
||||
console.error('复制失败:', err);
|
||||
copyBtn.innerHTML = '<i class="fa fa-times"></i>';
|
||||
copyBtn.innerHTML = '<i class="ri-close-line"></i>';
|
||||
Qmsg.error("复制失败");
|
||||
});
|
||||
});
|
||||
@@ -235,11 +243,73 @@ function initCodeCopy() {
|
||||
});
|
||||
}
|
||||
|
||||
// 页面加载完成后初始化 (Merged into main DOMContentLoaded listener)
|
||||
// document.addEventListener('DOMContentLoaded', function() {
|
||||
// initCodeCopy();
|
||||
// initDescriptionHitokoto();
|
||||
// });
|
||||
// 代码折叠功能
|
||||
function initCodeFold() {
|
||||
const threshold = 360;
|
||||
|
||||
// Helper to wrap element
|
||||
const wrap = (el, wrapper) => {
|
||||
el.parentNode.insertBefore(wrapper, el);
|
||||
wrapper.appendChild(el);
|
||||
};
|
||||
|
||||
// 1. Process .highlight blocks (Hugo generated)
|
||||
document.querySelectorAll('.highlight').forEach(hl => {
|
||||
if (hl.closest('.code-fold-container')) return; // Avoid double processing
|
||||
|
||||
// Check height
|
||||
if (hl.scrollHeight > threshold) {
|
||||
const wrapper = document.createElement('div');
|
||||
wrapper.className = 'code-fold-container collapsed';
|
||||
|
||||
// Wrap the highlight block
|
||||
wrap(hl, wrapper);
|
||||
|
||||
const btn = document.createElement('button');
|
||||
btn.className = 'code-expand-btn';
|
||||
// Use FontAwesome for reliability
|
||||
btn.innerHTML = '<span><i class="fa fa-chevron-down" style="margin-right: 4px;"></i> 展开代码</span>';
|
||||
|
||||
btn.addEventListener('click', () => {
|
||||
wrapper.classList.remove('collapsed');
|
||||
btn.remove();
|
||||
});
|
||||
|
||||
wrapper.appendChild(btn);
|
||||
}
|
||||
});
|
||||
|
||||
// 2. Process bare pre blocks (Fallback)
|
||||
document.querySelectorAll('.post-content > pre').forEach(pre => {
|
||||
if (pre.parentNode.classList.contains('highlight')) return;
|
||||
if (pre.closest('.code-fold-container')) return;
|
||||
if (pre.closest('.code-wrapper')) return; // Old wrapper check
|
||||
|
||||
if (pre.scrollHeight > threshold) {
|
||||
const wrapper = document.createElement('div');
|
||||
wrapper.className = 'code-fold-container collapsed';
|
||||
|
||||
wrap(pre, wrapper);
|
||||
|
||||
const btn = document.createElement('button');
|
||||
btn.className = 'code-expand-btn';
|
||||
btn.innerHTML = '<span><i class="fa fa-chevron-down" style="margin-right: 4px;"></i> 展开代码</span>';
|
||||
|
||||
btn.addEventListener('click', () => {
|
||||
wrapper.classList.remove('collapsed');
|
||||
btn.remove();
|
||||
});
|
||||
wrapper.appendChild(btn);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 页面加载完成后初始化
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
initCodeCopy();
|
||||
initCodeFold();
|
||||
initDescriptionHitokoto();
|
||||
});
|
||||
|
||||
// 初始化个人描述一言切换
|
||||
function initDescriptionHitokoto() {
|
||||
|
||||
Reference in new issue
Block a user