diff --git a/shared/naturalcrit/splitPane/splitPane.jsx b/shared/naturalcrit/splitPane/splitPane.jsx index 23ae5d321..60fc5c997 100644 --- a/shared/naturalcrit/splitPane/splitPane.jsx +++ b/shared/naturalcrit/splitPane/splitPane.jsx @@ -1,200 +1,146 @@ require('./splitPane.less'); const React = require('react'); -const createClass = require('create-react-class'); +const { useState, useEffect, useRef } = React; const cx = require('classnames'); -const SplitPane = createClass({ - displayName : 'SplitPane', - getDefaultProps : function() { - return { - storageKey : 'naturalcrit-pane-split', - onDragFinish : function(){}, //fires when dragging - showDividerButtons : true - }; - }, +const SplitPane = (props) => { + props = { + storageKey : 'naturalcrit-pane-split', + onDragFinish : function(){}, //fires when dragging + showDividerButtons : true, + ...props + }; + const pane1 = useRef(null); + const pane2 = useRef(null); + const [currentDividerPos, setCurrentDividerPos] = useState(null); + const [userSetDividerPos, setUserSetDividerPos] = useState(null); + const [windowWidth, setWindowWidth] = useState(null); + const [isDragging, setIsDragging] = useState(false); + const [moveSource, setMoveSource] = useState(false); + const [moveBrew, setMoveBrew] = useState(false); + const [showMoveArrows, setShowMoveArrows] = useState(true); + const [liveScroll, setLiveScroll] = useState(false); - getInitialState : function() { - return { - currentDividerPos : null, - windowWidth : 0, - isDragging : false, - moveSource : false, - moveBrew : false, - showMoveArrows : true - }; - }, + const storageKey = props.storageKey || 'naturalcrit-pane-split'; + const onDragFinish = props.onDragFinish || (() => {}); - pane1 : React.createRef(null), - pane2 : React.createRef(null), + // Fetch saved divider position and scroll state on mount + useEffect(() => { + setWindowWidth(window.innerWidth); + const dividerPos = window.localStorage.getItem(storageKey); + const liveScrollSetting = window.localStorage.getItem('liveScroll') === 'true'; + setLiveScroll(liveScrollSetting); - 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 - }); + if (dividerPos) { + const limitedPos = limitPosition(dividerPos, 0.1 * (window.innerWidth - 13), 0.9 * (window.innerWidth - 13)); + setCurrentDividerPos(limitedPos); + setUserSetDividerPos(dividerPos); } else { - this.setState({ - currentDividerPos : window.innerWidth / 2, - userSetDividerPos : window.innerWidth / 2 - }); + setCurrentDividerPos(window.innerWidth / 2); + setUserSetDividerPos(window.innerWidth / 2); } - window.addEventListener('resize', this.handleWindowResize); - // 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 }); - }, + const handleResize = () => { + const newPos = limitPosition(userSetDividerPos, 0.1 * (window.innerWidth - 13), 0.9 * (window.innerWidth - 13)); + setCurrentDividerPos(newPos); + setWindowWidth(window.innerWidth); + }; + window.addEventListener('resize', handleResize); - componentWillUnmount : function() { - window.removeEventListener('resize', this.handleWindowResize); - }, + return () => { + window.removeEventListener('resize', handleResize); + }; + }, []); - 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)); + const limitPosition = (x, min = 1, max = window.innerWidth - 13) => { + return Math.round(Math.min(max, Math.max(min, x))); + }; - 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){ + 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(currentDividerPos); + window.localStorage.setItem(storageKey, currentDividerPos); } - 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 - }); - }, + const newSize = limitPosition(e.pageX); + setCurrentDividerPos(newSize); + setUserSetDividerPos(newSize); + }; - 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 = () => { + const newScrollState = !liveScroll; + window.localStorage.setItem('liveScroll', String(newScrollState)); + setLiveScroll(newScrollState); + }; + + const renderMoveArrows = () => { + console.log('showMoveArrows: ', showMoveArrows); + if (showMoveArrows) { + return ( + <> +