Files
blog/themes/Ying/assets/js/modules/utils.js
T

154 lines
5.5 KiB
JavaScript

function themeSweep(newTheme, callback) {
// View Transition API — best quality if available
if (document.startViewTransition) {
document.startViewTransition(function() { callback(); });
return;
}
// UIkit animate fallback
if (window.UIkit && UIkit.animate) {
var body = document.body;
UIkit.animate(body, 'uk-animation-fade uk-animation-fast');
setTimeout(function() {
callback();
UIkit.animate(body, 'uk-animation-fade uk-animation-fast');
}, 150);
return;
}
// Last resort: simple opacity
callback();
}
// Toast Message Helper
window.showToast = function(text, type) {
if (window.Toast) {
var t = type || 'info';
if (typeof window.Toast[t] === 'function') {
window.Toast[t](text);
} else {
window.Toast.info(text);
}
}
};
// Theme Toggle Logic
window.initThemeToggle = function() {
var avatar = document.getElementById('theme-toggle-avatar');
if (!avatar) return;
var newAvatar = avatar.cloneNode(true);
avatar.parentNode.replaceChild(newAvatar, avatar);
newAvatar.addEventListener('click', function(e) {
e.preventDefault();
var html = document.documentElement;
var currentTheme = html.getAttribute('data-theme');
var newTheme = currentTheme === 'dark' ? 'light' : 'dark';
var apply = function() {
html.setAttribute('data-theme', newTheme);
localStorage.setItem('theme', newTheme);
window.showToast('已切换至 ' + (newTheme === 'dark' ? '深色' : '浅色') + '模式', 'success');
if (window.artalk && typeof window.artalk.setDarkMode === 'function') {
window.artalk.setDarkMode(newTheme === 'dark');
}
window.dispatchEvent(new Event('resize'));
};
themeSweep(newTheme, apply);
});
};
// Code Folding Logic
window.initCodeFold = function() {
var codeBlocks = document.querySelectorAll('.highlight');
codeBlocks.forEach(function(block) {
if (block.parentNode && block.parentNode.classList.contains('code-fold-container')) return;
var pre = block.querySelector('pre');
if (!pre) return;
// Use pre's content height; 0 means layout not ready, skip
var contentHeight = pre.scrollHeight || block.scrollHeight;
if (!contentHeight || contentHeight <= 360) return;
var wrapper = document.createElement('div');
wrapper.className = 'code-fold-container collapsed';
block.parentNode.insertBefore(wrapper, block);
wrapper.appendChild(block);
var btn = document.createElement('button');
btn.type = 'button';
btn.className = 'code-expand-btn';
wrapper.appendChild(btn);
var syncButton = function() {
var collapsed = wrapper.classList.contains('collapsed');
btn.innerHTML = collapsed
? '<span><i class="ri-arrow-down-s-line" style="margin-right:4px;"></i> 展开代码 </span>'
: '<span><i class="ri-arrow-up-s-line" style="margin-right:4px;"></i> 收起代码 </span>';
};
btn.onclick = function() {
wrapper.classList.toggle('collapsed');
syncButton();
};
syncButton();
});
};
// Code Copy Logic
window.initCodeCopy = function() {
var codeBlocks = document.querySelectorAll('pre');
codeBlocks.forEach(function(pre) {
if (pre.parentNode.querySelector('.copy-code-btn')) return;
if (pre.querySelector('.copy-code-btn')) return;
var btn = document.createElement('button');
btn.className = 'copy-code-btn';
btn.innerHTML = '<i class="ri-file-copy-line"></i>';
btn.title = "复制代码";
btn.style.cssText =
'position:absolute;top:-6px;right:5px;background:transparent;border:none;' +
'color:#888;cursor:pointer;padding:5px;border-radius:4px;' +
'transition:all 0.2s;opacity:0;z-index:10;';
pre.addEventListener('mouseenter', function() { btn.style.opacity = '1'; });
pre.addEventListener('mouseleave', function() { btn.style.opacity = '0'; });
if (typeof window.isMobile === 'function' && window.isMobile()) { btn.style.opacity = '1'; }
btn.addEventListener('click', function(e) {
e.stopPropagation();
var code = pre.querySelector('code') ? pre.querySelector('code').innerText : pre.innerText;
if (navigator.clipboard) {
navigator.clipboard.writeText(code).then(function() {
window.showToast('代码已复制 / Copied!', 'success');
btn.innerHTML = '<i class="ri-check-line"></i>';
setTimeout(function() { btn.innerHTML = '<i class="ri-file-copy-line"></i>'; }, 2000);
}).catch(function() {
window.showToast('复制失败 / Failed', 'error');
});
} else {
var textarea = document.createElement('textarea');
textarea.value = code;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
window.showToast('代码已复制 / Copied!', 'success');
}
});
if (getComputedStyle(pre).position === 'static') {
pre.style.position = 'relative';
}
pre.appendChild(btn);
});
};