diff --git a/shared/naturalcrit/splitPane/splitPane.jsx b/shared/naturalcrit/splitPane/splitPane.jsx index 23ae5d321..1500c759f 100644 --- a/shared/naturalcrit/splitPane/splitPane.jsx +++ b/shared/naturalcrit/splitPane/splitPane.jsx @@ -1,200 +1,110 @@ require('./splitPane.less'); const React = require('react'); -const createClass = require('create-react-class'); -const cx = require('classnames'); +const { useState, useEffect } = React; -const SplitPane = createClass({ - displayName : 'SplitPane', - getDefaultProps : function() { - return { - storageKey : 'naturalcrit-pane-split', - onDragFinish : function(){}, //fires when dragging - showDividerButtons : true - }; - }, +const storageKey = 'naturalcrit-pane-split'; - getInitialState : function() { - return { - currentDividerPos : null, - windowWidth : 0, - isDragging : false, - moveSource : false, - moveBrew : false, - showMoveArrows : true - }; - }, +const SplitPane = (props)=>{ + const { + onDragFinish = ()=>{}, + showDividerButtons = true + } = props; - pane1 : React.createRef(null), - pane2 : React.createRef(null), + 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); - componentDidMount : function() { - const dividerPos = window.localStorage.getItem(this.props.storageKey); - if(dividerPos){ - this.setState({ - currentDividerPos : this.limitPosition(dividerPos, 0.1*(window.innerWidth-13), 0.9*(window.innerWidth-13)), - userSetDividerPos : dividerPos, - windowWidth : window.innerWidth - }); - } else { - this.setState({ - currentDividerPos : window.innerWidth / 2, - userSetDividerPos : window.innerWidth / 2 - }); - } - window.addEventListener('resize', this.handleWindowResize); + 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'); - // This lives here instead of in the initial render because you cannot touch localStorage until the componant mounts. - const loadLiveScroll = window.localStorage.getItem('liveScroll') === 'true'; - this.setState({ liveScroll: loadLiveScroll }); - }, + window.addEventListener('resize', handleResize); + return ()=>window.removeEventListener('resize', handleResize); + }, []); - componentWillUnmount : function() { - window.removeEventListener('resize', this.handleWindowResize); - }, + const limitPosition = (x, min = 1, max = window.innerWidth - 13)=>Math.round(Math.min(max, Math.max(min, x))); - handleWindowResize : function() { - // Allow divider to increase in size to last user-set position - // Limit current position to between 10% and 90% of visible space - const newLoc = this.limitPosition(this.state.userSetDividerPos, 0.1*(window.innerWidth-13), 0.9*(window.innerWidth-13)); - - this.setState({ - currentDividerPos : newLoc, - windowWidth : window.innerWidth - }); - }, - - limitPosition : function(x, min = 1, max = window.innerWidth - 13) { - const result = Math.round(Math.min(max, Math.max(min, x))); - return result; - }, - - handleUp : function(e){ + //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(this.state.isDragging){ - this.props.onDragFinish(this.state.currentDividerPos); - window.localStorage.setItem(this.props.storageKey, this.state.currentDividerPos); + if(isDragging) { + onDragFinish(dividerPos); + window.localStorage.setItem(storageKey, dividerPos); } - this.setState({ isDragging: false }); - }, + setIsDragging(false); + }; - handleDown : function(e){ + const handleDown = (e)=>{ e.preventDefault(); - this.setState({ isDragging: true }); - //this.unFocus() - }, - - handleMove : function(e){ - if(!this.state.isDragging) return; + setIsDragging(true); + }; + const handleMove = (e)=>{ + if(!isDragging) return; e.preventDefault(); - const newSize = this.limitPosition(e.pageX); - this.setState({ - currentDividerPos : newSize, - userSetDividerPos : newSize - }); - }, + setDividerPos(limitPosition(e.pageX)); + }; - liveScrollToggle : function() { - window.localStorage.setItem('liveScroll', String(!this.state.liveScroll)); - this.setState({ liveScroll: !this.state.liveScroll }); - }, - /* - unFocus : function() { - if(document.selection){ - document.selection.empty(); - }else{ - window.getSelection().removeAllRanges(); - } - }, - */ + const liveScrollToggle = ()=>{ + window.localStorage.setItem('liveScroll', String(!liveScroll)); + setLiveScroll(!liveScroll); + }; - setMoveArrows : function(newState) { - if(this.state.showMoveArrows != newState){ - this.setState({ - showMoveArrows : newState - }); - } - }, - - renderMoveArrows : function(){ - if(this.state.showMoveArrows) { - return <> -