125 lines
7.1 KiB
JavaScript
125 lines
7.1 KiB
JavaScript
// Explore-only device presentation preference; never stores investigation content or working focus.
|
|
(() => {
|
|
const workspace = document.querySelector('[data-explore-layout]');
|
|
const divider = workspace?.querySelector('[data-explore-divider]');
|
|
if (!divider) return;
|
|
const root = workspace.closest('[data-layout-owner]');
|
|
const key = `plotdirector.writerExploreLayout.v1.${root?.dataset.layoutOwner || 'device'}`;
|
|
const motion = matchMedia('(prefers-reduced-motion: reduce)');
|
|
let investigationPreference = null, ratio = .5, active = !!new URLSearchParams(location.search).get('investigation') && location.hash !== '#new-investigation', animation = 0, target = .5, drag = null, pointerFrame = 0, pointerX = null;
|
|
try {
|
|
const saved = JSON.parse(localStorage.getItem(key));
|
|
if (saved?.version === 1 && Number.isFinite(saved.leftRatio) && saved.leftRatio >= .1 && saved.leftRatio <= .9) investigationPreference = saved.leftRatio;
|
|
} catch { /* Storage may be blocked or contain an old preference. */ }
|
|
function bounds() {
|
|
const size = parseFloat(getComputedStyle(workspace).getPropertyValue('--explore-divider-size'));
|
|
const width = Math.max(1, workspace.clientWidth - size);
|
|
return { width, min: Math.max(.15, Math.min(.5, 260 / width)), max: Math.min(.8, Math.max(.5, 1 - 360 / width)) };
|
|
}
|
|
function paint(value) {
|
|
const b = bounds();
|
|
ratio = Math.max(b.min, Math.min(b.max, value));
|
|
workspace.style.setProperty('--explore-ratio', ratio);
|
|
divider.setAttribute('aria-valuemin', String(Math.round(b.min * 100)));
|
|
divider.setAttribute('aria-valuemax', String(Math.round(b.max * 100)));
|
|
divider.setAttribute('aria-valuenow', String(Math.round(ratio * 100)));
|
|
divider.setAttribute('aria-valuetext', `${Math.round(ratio * 100)} percent discovery, ${Math.round((1 - ratio) * 100)} percent investigation`);
|
|
}
|
|
function stop() {
|
|
cancelAnimationFrame(animation); animation = 0;
|
|
workspace.removeAttribute('data-resize-animating');
|
|
}
|
|
function move(value, animate = false) {
|
|
stop(); target = value;
|
|
if (!animate || motion.matches || workspace.dataset.narrow === 'true' || Math.abs(value - ratio) < .0001) { paint(value); return; }
|
|
const from = ratio, started = performance.now();
|
|
workspace.dataset.resizeAnimating = 'true';
|
|
function tick(now) {
|
|
const elapsed = Math.min(1, (now - started) / 450);
|
|
paint(from + (value - from) * (1 - Math.pow(1 - elapsed, 3)));
|
|
if (elapsed < 1) animation = requestAnimationFrame(tick);
|
|
else { animation = 0; workspace.removeAttribute('data-resize-animating'); }
|
|
}
|
|
animation = requestAnimationFrame(tick);
|
|
}
|
|
function save() {
|
|
target = ratio;
|
|
if (!active) return; // Landing adjustments are temporary, never an investigation preference.
|
|
investigationPreference = ratio;
|
|
try { localStorage.setItem(key, JSON.stringify({ version: 1, leftRatio: investigationPreference })); } catch { }
|
|
}
|
|
function applyPointer() {
|
|
pointerFrame = 0;
|
|
if (!drag || pointerX === null) return;
|
|
const b = bounds();
|
|
// Center the handle on the pointer, so picking it up never jumps a column.
|
|
paint((pointerX - workspace.getBoundingClientRect().left - drag.offset) / b.width);
|
|
pointerX = null; drag.moved = true;
|
|
}
|
|
function endDrag(event) {
|
|
if (!drag || (event?.pointerId != null && event.pointerId !== drag.id)) return;
|
|
cancelAnimationFrame(pointerFrame); applyPointer();
|
|
const finished = drag; drag = null; pointerX = null;
|
|
workspace.classList.remove('writer3-dragging');
|
|
if (divider.hasPointerCapture(finished.id)) divider.releasePointerCapture(finished.id);
|
|
if (finished.moved) save();
|
|
}
|
|
divider.addEventListener('pointerdown', event => {
|
|
if (!event.isPrimary || event.button !== 0 || workspace.dataset.narrow === 'true') return;
|
|
event.preventDefault(); stop(); divider.focus();
|
|
drag = { id: event.pointerId, offset: event.clientX - workspace.getBoundingClientRect().left - ratio * bounds().width, moved: false };
|
|
divider.setPointerCapture(event.pointerId); workspace.classList.add('writer3-dragging');
|
|
});
|
|
divider.addEventListener('pointermove', event => {
|
|
if (event.pointerId !== drag?.id) return;
|
|
pointerX = event.clientX;
|
|
if (!pointerFrame) pointerFrame = requestAnimationFrame(applyPointer);
|
|
});
|
|
for (const type of ['pointerup', 'pointercancel', 'lostpointercapture']) divider.addEventListener(type, endDrag);
|
|
window.addEventListener('blur', () => endDrag());
|
|
window.addEventListener('pagehide', () => { endDrag(); stop(); });
|
|
divider.addEventListener('keydown', event => {
|
|
if (event.altKey || event.ctrlKey || event.metaKey) return;
|
|
const b = bounds(), step = (event.shiftKey ? 50 : 10) / b.width;
|
|
let value;
|
|
if (event.key === 'ArrowLeft') value = ratio - step;
|
|
else if (event.key === 'ArrowRight') value = ratio + step;
|
|
else if (event.key === 'Home') value = b.min;
|
|
else if (event.key === 'End') value = b.max;
|
|
else return;
|
|
event.preventDefault(); stop(); paint(value); save();
|
|
});
|
|
workspace.addEventListener('writer-explore:state', event => {
|
|
const next = event.detail.active;
|
|
if (next === active && !event.detail.initial && (active || target === .5 && !drag)) return; // Questions and an already-balanced landing retain their layout.
|
|
endDrag(); // Finish in the previous state before choosing the new state's target.
|
|
active = next;
|
|
workspace.dataset.exploreState = active ? 'investigation' : 'landing';
|
|
move(active ? investigationPreference ?? .25 : .5, !event.detail.initial);
|
|
});
|
|
function measure() {
|
|
const narrow = workspace.clientWidth <= 800 || innerWidth <= 800;
|
|
if (workspace.dataset.narrow !== String(narrow)) {
|
|
endDrag();
|
|
if (narrow && document.activeElement === divider) workspace.querySelector('#explore-concern')?.focus({preventScroll:true});
|
|
workspace.dataset.narrow = String(narrow); divider.tabIndex = narrow ? -1 : 0;
|
|
}
|
|
// Height changes from questions/reflow must not interrupt or restart an animation.
|
|
if (!animation) paint(target);
|
|
}
|
|
let measuredWidth = 0;
|
|
new ResizeObserver(() => {
|
|
if (workspace.clientWidth === measuredWidth) return;
|
|
measuredWidth = workspace.clientWidth;
|
|
endDrag(); measure(); // Keep an in-flight glide when a deleted guidance panel changes available width.
|
|
}).observe(workspace);
|
|
window.addEventListener('resize', () => { endDrag(); stop(); measure(); });
|
|
window.addEventListener('pageshow', event => {
|
|
if (event.persisted && !active) { endDrag(); move(.5); }
|
|
else if (!animation) measure();
|
|
});
|
|
motion.addEventListener('change', () => { if (motion.matches && animation) { stop(); paint(target); } });
|
|
workspace.dataset.exploreState = active ? 'investigation' : 'landing';
|
|
target = active ? investigationPreference ?? .25 : .5; measure();
|
|
})();
|