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

165 lines
6.3 KiB
JavaScript
Raw Normal View History

2026-05-24 21:34:06 +08:00
// Scanner — page is blanked, beam paints the new theme back line by line
function themeSweep(newTheme, callback) {
var isH = Math.random() < 0.5;
var w = window.innerWidth, h = window.innerHeight;
// Blank overlay: covers entire page with new-theme's dominant color
var blank = newTheme === 'dark' ? '#111827' : '#fcfaf5';
var sheet = document.createElement('div');
sheet.style.cssText =
'position:fixed;inset:0;z-index:9999;' +
'background:' + blank + ';' +
(isH
? 'clip-path:inset(0 0 0 0);transition:clip-path 1.2s cubic-bezier(0.4,0,0.2,1);will-change:clip-path;'
: 'clip-path:inset(0 0 0 0);transition:clip-path 1.2s cubic-bezier(0.4,0,0.2,1);will-change:clip-path;');
document.body.appendChild(sheet);
// Thin scan beam at the leading edge
var beam = document.createElement('div');
beam.style.cssText =
'position:fixed;z-index:10000;pointer-events:none;' +
'background:rgba(255,255,255,0.7);' +
'box-shadow:0 0 4px 1px rgba(255,255,255,0.35),0 0 12px 2px rgba(255,255,255,0.12);' +
'transition:transform 1.2s cubic-bezier(0.4,0,0.2,1);will-change:transform;' +
(isH
? 'top:0;right:0;width:1px;height:100%;transform:translateX(0);'
: 'bottom:0;left:0;height:1px;width:100%;transform:translateY(0);');
document.body.appendChild(beam);
sheet.offsetWidth;
// Switch theme immediately — sheet covers it, beam reveals
callback();
if (isH) {
sheet.style.clipPath = 'inset(0 0 0 100%)';
beam.style.transform = 'translateX(' + (-w) + 'px)';
} else {
sheet.style.clipPath = 'inset(100% 0 0 0)';
beam.style.transform = 'translateY(' + (-h) + 'px)';
}
beam.addEventListener('transitionend', function(e) {
if (e.propertyName !== 'transform') return;
if (sheet.parentNode) sheet.parentNode.removeChild(sheet);
if (beam.parentNode) beam.parentNode.removeChild(beam);
});
}
2026-01-31 21:27:06 +08:00
// Toast Message Helper
2026-01-31 22:06:19 +08:00
window.showToast = function(text, type) {
if (window.Toast) {
var t = type || 'info';
if (typeof window.Toast[t] === 'function') {
window.Toast[t](text);
2026-01-31 22:06:19 +08:00
} else {
window.Toast.info(text);
2026-01-31 22:06:19 +08:00
}
2026-01-31 21:27:06 +08:00
}
};
// Theme Toggle Logic
window.initThemeToggle = function() {
2026-05-24 21:34:06 +08:00
var avatar = document.getElementById('theme-toggle-avatar');
2026-01-31 21:27:06 +08:00
if (!avatar) return;
2026-05-24 21:34:06 +08:00
var newAvatar = avatar.cloneNode(true);
2026-01-31 21:27:06 +08:00
avatar.parentNode.replaceChild(newAvatar, avatar);
2026-05-24 21:34:06 +08:00
2026-01-31 21:27:06 +08:00
newAvatar.addEventListener('click', function(e) {
e.preventDefault();
2026-02-05 21:53:08 +08:00
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'));
};
2026-05-24 21:34:06 +08:00
themeSweep(newTheme, apply);
2026-01-31 21:27:06 +08:00
});
};
// Code Folding Logic
window.initCodeFold = function() {
2026-05-24 21:34:06 +08:00
var codeBlocks = document.querySelectorAll('.highlight');
codeBlocks.forEach(function(block) {
2026-01-31 21:27:06 +08:00
if (block.offsetHeight > 360) {
if (block.parentNode.classList.contains('code-fold-container')) return;
2026-05-24 21:34:06 +08:00
var wrapper = document.createElement('div');
2026-01-31 21:27:06 +08:00
wrapper.className = 'code-fold-container collapsed';
block.parentNode.insertBefore(wrapper, block);
wrapper.appendChild(block);
2026-05-24 21:34:06 +08:00
var btn = document.createElement('button');
2026-01-31 21:27:06 +08:00
btn.className = 'code-expand-btn';
2026-02-01 12:45:32 +08:00
btn.innerHTML = '<span><i class="ri-arrow-down-s-line" style="margin-right:4px;"></i> 展开代码 </span>';
2026-01-31 21:27:06 +08:00
btn.onclick = function() {
wrapper.classList.remove('collapsed');
btn.style.display = 'none';
};
wrapper.appendChild(btn);
}
});
};
// Code Copy Logic
window.initCodeCopy = function() {
2026-05-24 21:34:06 +08:00
var codeBlocks = document.querySelectorAll('pre');
codeBlocks.forEach(function(pre) {
2026-01-31 21:27:06 +08:00
if (pre.parentNode.querySelector('.copy-code-btn')) return;
if (pre.querySelector('.copy-code-btn')) return;
2026-05-24 21:34:06 +08:00
var btn = document.createElement('button');
2026-01-31 21:27:06 +08:00
btn.className = 'copy-code-btn';
btn.innerHTML = '<i class="ri-file-copy-line"></i>';
2026-02-01 12:45:32 +08:00
btn.title = "复制代码";
2026-05-24 21:34:06 +08:00
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;';
2026-01-31 21:27:06 +08:00
2026-05-24 21:34:06 +08:00
pre.addEventListener('mouseenter', function() { btn.style.opacity = '1'; });
pre.addEventListener('mouseleave', function() { btn.style.opacity = '0'; });
if (window.isMobile()) { btn.style.opacity = '1'; }
btn.addEventListener('click', function(e) {
2026-01-31 21:27:06 +08:00
e.stopPropagation();
2026-05-24 21:34:06 +08:00
var code = pre.querySelector('code') ? pre.querySelector('code').innerText : pre.innerText;
2026-01-31 21:27:06 +08:00
if (navigator.clipboard) {
2026-05-24 21:34:06 +08:00
navigator.clipboard.writeText(code).then(function() {
2026-01-31 22:06:19 +08:00
window.showToast('代码已复制 / Copied!', 'success');
2026-01-31 21:27:06 +08:00
btn.innerHTML = '<i class="ri-check-line"></i>';
2026-05-24 21:34:06 +08:00
setTimeout(function() { btn.innerHTML = '<i class="ri-file-copy-line"></i>'; }, 2000);
}).catch(function() {
2026-01-31 22:06:19 +08:00
window.showToast('复制失败 / Failed', 'error');
2026-01-31 21:27:06 +08:00
});
} else {
2026-05-24 21:34:06 +08:00
var textarea = document.createElement('textarea');
2026-01-31 21:27:06 +08:00
textarea.value = code;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
2026-01-31 22:06:19 +08:00
window.showToast('代码已复制 / Copied!', 'success');
2026-01-31 21:27:06 +08:00
}
});
2026-05-24 21:34:06 +08:00
2026-01-31 21:27:06 +08:00
if (getComputedStyle(pre).position === 'static') {
pre.style.position = 'relative';
}
2026-05-24 21:34:06 +08:00
2026-01-31 21:27:06 +08:00
pre.appendChild(btn);
});
};