// 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); }); } // 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.offsetHeight > 360) { if (block.parentNode.classList.contains('code-fold-container')) 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.className = 'code-expand-btn'; btn.innerHTML = ' 展开代码 '; btn.onclick = function() { wrapper.classList.remove('collapsed'); btn.style.display = 'none'; }; wrapper.appendChild(btn); } }); }; // 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 = ''; 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 (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 = ''; setTimeout(function() { btn.innerHTML = ''; }, 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); }); };