147 lines
8.7 KiB
JavaScript
147 lines
8.7 KiB
JavaScript
// Device-only layout preferences. Never contains or changes a story selection/session/native binding.
|
|
(() => {
|
|
const root = document.querySelector('.writer3-destination');
|
|
const destination = root?.dataset.writerDestination;
|
|
if (!['Write', 'Explore', 'Plan', 'Review', 'Reflect'].includes(destination)) return;
|
|
const workspace = root.querySelector('.writer3-workspace');
|
|
const main = workspace?.querySelector('.writer3-main');
|
|
const companion = workspace?.querySelector('.writer3-reference');
|
|
if (!workspace || !main || !companion) return; // Empty-book page remains accessible without a split.
|
|
const key = `plotdirector.writerLayout.v1.${root.dataset.layoutOwner || 'device'}.${destination}`;
|
|
let preference = { version: 1, width: .33, collapsed: false };
|
|
try {
|
|
const saved = JSON.parse(localStorage.getItem(key));
|
|
if (saved?.version === 1) {
|
|
if (typeof saved.width === 'number' && Number.isFinite(saved.width)) preference.width = Math.max(.15, Math.min(.6, saved.width));
|
|
if (typeof saved.collapsed === 'boolean') preference.collapsed = saved.collapsed;
|
|
}
|
|
} catch { /* Blocked storage and malformed/old preferences use safe defaults. */ }
|
|
function save() { try { localStorage.setItem(key, JSON.stringify(preference)); } catch { } }
|
|
const toggle = document.querySelector('[data-writer-companion-toggle]');
|
|
if(!toggle)return;
|
|
toggle.hidden=false;
|
|
const toolbar=toggle.closest('.pd-ribbon-group');
|
|
companion.id = 'writer3-companion';
|
|
const divider = document.createElement('div');
|
|
divider.className = 'writer3-divider'; divider.tabIndex = 0;
|
|
divider.setAttribute('role', 'separator'); divider.setAttribute('aria-orientation', 'vertical');
|
|
divider.setAttribute('aria-label', 'Resize contextual companion');
|
|
divider.setAttribute('aria-controls', 'writer3-companion');
|
|
divider.setAttribute('aria-describedby', 'writer3-resize-help');
|
|
const help = document.createElement('span'); help.id = 'writer3-resize-help'; help.className = 'visually-hidden';
|
|
help.textContent = 'Left arrow widens the companion; Right arrow narrows it. Shift changes in larger steps. Home narrows and End widens to the limit.';
|
|
toolbar.append(help); main.after(divider);
|
|
root.classList.add('writer3-viewport'); workspace.classList.add('writer3-split');
|
|
let narrow = false, overlay = false, frame = 0, drag = null, opener = toggle;
|
|
function bounds() {
|
|
const width = workspace.clientWidth - parseFloat(getComputedStyle(workspace).getPropertyValue('--writer-divider-size'));
|
|
return { width, min: Math.max(280, width * .22), max: Math.min(width * .55, width - 440) };
|
|
}
|
|
function render() {
|
|
const b = bounds();
|
|
const size = Math.max(b.min, Math.min(b.max, b.width * preference.width));
|
|
workspace.style.setProperty('--writer-companion-width', `${Math.round(size)}px`);
|
|
workspace.dataset.collapsed = String(preference.collapsed);
|
|
workspace.dataset.narrow = String(narrow);
|
|
workspace.dataset.overlay = String(overlay);
|
|
const reading=root.querySelector('.manuscript-reader')?.dataset.reading==='true';
|
|
const visible = !reading && (narrow ? overlay : !preference.collapsed);
|
|
toggle.disabled=reading;
|
|
companion.hidden = !visible;
|
|
divider.hidden = narrow || preference.collapsed;
|
|
const top=parseFloat(getComputedStyle(root).getPropertyValue('--writer-sticky-top'))||8;
|
|
const panelHeight=Math.max(main.offsetHeight,companion.offsetHeight);
|
|
workspace.style.setProperty('--writer-divider-height',`${Math.max(44,Math.min(panelHeight,(window.visualViewport?.height||innerHeight)-top-8))}px`);
|
|
toggle.querySelector('span').textContent = reading ? 'Companion (Review mode)' : visible ? 'Hide companion' : 'Show companion';
|
|
toggle.setAttribute('aria-pressed',String(visible));
|
|
toggle.setAttribute('aria-expanded', String(visible));
|
|
divider.setAttribute('aria-valuemin', String(Math.round(b.min)));
|
|
divider.setAttribute('aria-valuemax', String(Math.max(Math.round(b.min), Math.round(b.max))));
|
|
divider.setAttribute('aria-valuenow', String(Math.round(size)));
|
|
divider.setAttribute('aria-valuetext', `${Math.round(size)} pixels, ${Math.round(size / b.width * 100)} percent companion width`);
|
|
}
|
|
let pointerFrame = 0, pointerX = null;
|
|
function applyPointer() {
|
|
pointerFrame = 0;
|
|
if (!drag || pointerX === null) return;
|
|
const b = bounds(), right = workspace.getBoundingClientRect().right;
|
|
preference.width = Math.max(b.min, Math.min(b.max, right - pointerX)) / b.width;
|
|
pointerX = null; render();
|
|
}
|
|
function endDrag(event) {
|
|
if (!drag || (event?.pointerId != null && drag.id !== event.pointerId)) return;
|
|
if (pointerFrame) cancelAnimationFrame(pointerFrame);
|
|
applyPointer();
|
|
const id = drag.id; drag = null; root.classList.remove('writer3-dragging');
|
|
if (divider.hasPointerCapture(id)) divider.releasePointerCapture(id);
|
|
save();
|
|
}
|
|
function measure() {
|
|
frame = 0;
|
|
const shell=document.querySelector('.pd-application-header');
|
|
const sticky=shell&&getComputedStyle(shell).position==='sticky'?shell.getBoundingClientRect().height:0;
|
|
root.style.setProperty('--writer-sticky-top',`${Math.ceil(sticky)+8}px`);
|
|
root.classList.add('writer3-flow');
|
|
const next = workspace.clientWidth < 900;
|
|
if (next !== narrow) {
|
|
if (companion.contains(document.activeElement) && (next || preference.collapsed)) toggle.focus();
|
|
overlay = false; narrow = next; endDrag();
|
|
}
|
|
render();
|
|
}
|
|
function schedule() { if (!frame) frame = requestAnimationFrame(measure); }
|
|
function hide() {
|
|
if (companion.contains(document.activeElement)) toggle.focus();
|
|
if (narrow) overlay = false; else { preference.collapsed = true; save(); }
|
|
render();
|
|
}
|
|
toggle.addEventListener('click', () => {
|
|
if (narrow) {
|
|
overlay = !overlay; opener = toggle; render();
|
|
if (overlay) companion.focus();
|
|
} else { preference.collapsed = !preference.collapsed; save(); render(); }
|
|
});
|
|
companion.addEventListener('keydown', event => {
|
|
if (event.key === 'Escape' && narrow && overlay) { event.preventDefault(); hide(); opener.focus(); }
|
|
});
|
|
// Non-modal contextual overlay: the application ribbon and Help remain usable; no focus trap.
|
|
root.addEventListener('focusout', () => {
|
|
if (narrow && overlay) queueMicrotask(() => {
|
|
if (!companion.contains(document.activeElement) && document.activeElement !== toggle) { overlay = false; render(); }
|
|
});
|
|
});
|
|
divider.addEventListener('keydown', event => {
|
|
if (event.altKey || event.ctrlKey || event.metaKey) return; // Preserve browser/OS navigation shortcuts.
|
|
const b = bounds(); let size = Math.max(b.min, Math.min(b.max, b.width * preference.width));
|
|
const step = event.shiftKey ? 50 : 10;
|
|
if (event.key === 'ArrowLeft') size += step;
|
|
else if (event.key === 'ArrowRight') size -= step;
|
|
else if (event.key === 'Home') size = b.min;
|
|
else if (event.key === 'End') size = b.max;
|
|
else return;
|
|
event.preventDefault(); preference.width = Math.max(b.min, Math.min(b.max, size)) / b.width; render(); save();
|
|
});
|
|
divider.addEventListener('pointerdown', event => {
|
|
if (!event.isPrimary || event.button !== 0) return;
|
|
event.preventDefault(); divider.focus();
|
|
drag = { id: event.pointerId }; divider.setPointerCapture(event.pointerId); root.classList.add('writer3-dragging');
|
|
});
|
|
divider.addEventListener('pointermove', event => {
|
|
if (drag?.id !== event.pointerId) return;
|
|
pointerX = event.clientX;
|
|
if (!pointerFrame) pointerFrame = requestAnimationFrame(applyPointer);
|
|
});
|
|
for (const name of ['pointerup', 'pointercancel', 'lostpointercapture']) divider.addEventListener(name, endDrag);
|
|
window.addEventListener('blur', () => endDrag());
|
|
window.addEventListener('pagehide', () => endDrag());
|
|
window.addEventListener('resize', () => { endDrag(); schedule(); });
|
|
window.addEventListener('plotdirector:ribbon-layout-changed', schedule);
|
|
document.addEventListener('fullscreenchange', schedule);
|
|
window.visualViewport?.addEventListener('resize', schedule);
|
|
window.visualViewport?.addEventListener('scroll', schedule);
|
|
const observer = new ResizeObserver(schedule);
|
|
for (const node of [main,companion,root.querySelector('.writer3-header'), document.querySelector('[data-ribbon-shell]'), root.parentElement, document.querySelector('[data-help-layout]')]) if (node) observer.observe(node);
|
|
document.fonts?.ready.then(schedule);
|
|
measure();
|
|
})();
|