require('./splitPane.less'); const React = require('react'); const { useState, useEffect } = React; const storageKey = 'naturalcrit-pane-split'; const SplitPane = (props)=>{ const { onDragFinish = ()=>{}, showDividerButtons = true } = props; const [isDragging, setIsDragging] = useState(false); const [dividerPos, setDividerPos] = useState(null); const [moveSource, setMoveSource] = useState(false); const [moveBrew, setMoveBrew] = useState(false); const [showMoveArrows, setShowMoveArrows] = useState(true); const [liveScroll, setLiveScroll] = useState(false); useEffect(()=>{ const savedPos = window.localStorage.getItem(storageKey); setDividerPos(savedPos ? limitPosition(savedPos, 0.1 * (window.innerWidth - 13), 0.9 * (window.innerWidth - 13)) : window.innerWidth / 2); setLiveScroll(window.localStorage.getItem('liveScroll') === 'true'); window.addEventListener('resize', handleResize); return ()=>window.removeEventListener('resize', handleResize); }, []); const limitPosition = (x, min = 1, max = window.innerWidth - 13)=>Math.round(Math.min(max, Math.max(min, x))); //when resizing, the divider should grow smaller if less space is given, then grow back if the space is restored, to the original position const handleResize = ()=>setDividerPos(limitPosition(window.localStorage.getItem(storageKey), 0.1 * (window.innerWidth - 13), 0.9 * (window.innerWidth - 13))); const handleUp =(e)=>{ e.preventDefault(); if(isDragging) { onDragFinish(dividerPos); window.localStorage.setItem(storageKey, dividerPos); } setIsDragging(false); }; const handleDown = (e)=>{ e.preventDefault(); setIsDragging(true); }; const handleMove = (e)=>{ if(!isDragging) return; e.preventDefault(); setDividerPos(limitPosition(e.pageX)); }; const liveScrollToggle = ()=>{ window.localStorage.setItem('liveScroll', String(!liveScroll)); setLiveScroll(!liveScroll); }; const renderMoveArrows = (showMoveArrows && <>