/* Native article enhancements. No network, analytics or dependencies. */ (() => { 'use strict'; const article = document.querySelector('[data-reading-post]'); if (!article) return; const prose = article.querySelector('.prose'); const tools = article.querySelector('.reading-tools'); if (!prose || !tools) return; const opt = tools.querySelector('[data-resume-enabled]'); const prompt = tools.querySelector('.resume-prompt'); const status = tools.querySelector('[data-resume-status]'); const bar = tools.querySelector('progress'); const percent = tools.querySelector('[data-reading-percent]'); const key = `baa-reading-v1:${article.dataset.readingPost}:${article.dataset.readingVersion}`; const clamp = value => Math.max(0, Math.min(1, value)); let pending = null, returnRatio = null, timer = null, frame = null, interacted = false; let available = true; function storageFailure() { available = false; opt.checked = false; opt.disabled = true; prompt.hidden = true; status.textContent = 'Bu tarayıcıda okuma yeri saklanamıyor. Yazıyı okumaya devam edebilirsin.'; } function geometry() { const rect = prose.getBoundingClientRect(); const range = rect.height - window.innerHeight; return { top: rect.top + window.scrollY, range, offset: -rect.top, ratio: range > 0 ? clamp(-rect.top / range) : (rect.bottom <= window.innerHeight ? 1 : 0) }; } function meaningful(ratio, offset, range) { return Number.isFinite(ratio) && ratio >= 0.05 && ratio <= 0.95 && offset >= 200 && range >= 400; } function forget() { const restoreFocus = document.activeElement === tools.querySelector('[data-resume-forget]'); clearTimeout(timer); timer = null; pending = null; returnRatio = null; opt.checked = false; prompt.hidden = true; if (restoreFocus) opt.focus({preventScroll: true}); try { localStorage.removeItem(key); status.textContent = 'Bu yazının okuma yeri unutuldu.'; } catch { storageFailure(); } } try { const raw = localStorage.getItem(key); if (raw) { let record; try { record = JSON.parse(raw); } catch { localStorage.removeItem(key); } if (record && record.enabled === true && typeof record.ratio === 'number' && typeof record.offset === 'number' && typeof record.range === 'number' && meaningful(record.ratio, record.offset, record.range)) { pending = record; opt.checked = true; prompt.hidden = false; } else if (record) localStorage.removeItem(key); } } catch { storageFailure(); } tools.hidden = false; // Never write on initial layout, and never overwrite a pending return prompt. function save() { timer = null; if (!available || !opt.checked || pending || !interacted) return; const point = geometry(); try { if (meaningful(point.ratio, point.offset, point.range)) { localStorage.setItem(key, JSON.stringify({enabled: true, ratio: point.ratio, offset: point.offset, range: point.range})); } else if (point.ratio > 0.95) localStorage.removeItem(key); } catch { storageFailure(); } } function update() { frame = null; const value = Math.round(geometry().ratio * 100); bar.value = value; bar.textContent = `${value}%`; percent.textContent = `${value}%`; } function onScroll() { interacted = true; if (frame === null) frame = requestAnimationFrame(update); if (timer === null && available && opt.checked && !pending) timer = setTimeout(save, 900); } window.addEventListener('scroll', onScroll, {passive: true}); window.addEventListener('resize', update, {passive: true}); if ('ResizeObserver' in window) new ResizeObserver(update).observe(prose); window.addEventListener('pagehide', save); opt.addEventListener('change', () => { if (!opt.checked) forget(); else { interacted = true; status.textContent = 'Yalnızca bu yazının okuma yeri, bu tarayıcıda saklanır.'; save(); } }); tools.querySelector('[data-resume-forget]').addEventListener('click', forget); tools.querySelector('[data-resume-return]').addEventListener('click', () => { if (!pending && returnRatio === null) return; const ratio = pending ? pending.ratio : returnRatio; returnRatio = ratio; pending = null; const point = geometry(); if (point.range > 400) window.scrollTo({top: point.top + ratio * point.range, behavior: 'instant'}); status.textContent = 'Kaydedilen okuma yerine dönüldü.'; // Focus stays on the initiating control; no automatic scroll or prose focus. }); update(); if (typeof HTMLDialogElement === 'undefined') return; function imageURL(value) { try { const url = new URL(value, document.baseURI); return /^https?:$/.test(url.protocol) ? url.href : null; } catch { return null; } } const images = []; article.querySelectorAll('.prose figure img, .reading-cover img, .prose img[data-baa-lightbox]').forEach(img => { if (!img.closest('.article-screen, .wp-block-image, .reading-cover') && !img.hasAttribute('data-baa-lightbox')) return; if (img.closest('form, button, [data-no-lightbox]')) return; const link = img.closest('a'); if (link && (link.hasAttribute('download') || !/\.(?:png|jpe?g|webp|gif|avif)(?:[?#]|$)/i.test(link.href))) return; const full = imageURL(link ? link.href : img.dataset.fullUrl || img.dataset.origFile || img.currentSrc || img.src); if (!full) return; let trigger = link; if (!trigger) { trigger = document.createElement('button'); trigger.type = 'button'; trigger.className = 'image-enlarge'; trigger.setAttribute('aria-label', (img.alt || 'Yazı görseli') + ' — tam boyutta aç'); img.before(trigger); trigger.append(img); } trigger.setAttribute('aria-haspopup', 'dialog'); images.push({img, trigger, full}); const index = images.length - 1; trigger.addEventListener('click', event => { if (event.defaultPrevented || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return; event.preventDefault(); open(index); }); }); if (!images.length) return; const dialog = document.createElement('dialog'); dialog.className = 'reading-lightbox'; dialog.setAttribute('aria-label', 'Yazı görseli, tam boyut'); dialog.innerHTML = '