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 <> -
this.setState({ moveSource: !this.state.moveSource })} > - -
-
this.setState({ moveBrew: !this.state.moveBrew })} > - -
-
- -
- ; - } - }, - - renderDivider : function(){ - return <> - {this.props.showDividerButtons && this.renderMoveArrows()} -
-
- - - -
+ const renderMoveArrows = (showMoveArrows && + <> +
setMoveSource(!moveSource)} > +
- ; - }, +
setMoveBrew(!moveBrew)} > + +
+
+ +
+ + ); - render : function(){ - return
- - {React.cloneElement(this.props.children[0], { - ...(this.props.showDividerButtons && { - moveBrew : this.state.moveBrew, - moveSource : this.state.moveSource, - liveScroll : this.state.liveScroll, - setMoveArrows : this.setMoveArrows, - }), - })} + const renderDivider = ( +
+ {showDividerButtons && renderMoveArrows} +
+ + + +
+
+ ); + + return ( +
+ + {props.children[0]} - {this.renderDivider()} - {this.props.children[1]} -
; - } -}); + {renderDivider} + {props.children[1]} +
+ ); +}; -const Pane = createClass({ - displayName : 'Pane', - getDefaultProps : function() { - return { - width : null - }; - }, - render : function(){ - let styles = {}; - if(this.props.width){ - styles = { - flex : 'none', - width : `${this.props.width}px` - }; - } else { - styles = { - pointerEvents : this.props.isDragging ? 'none' : 'auto' //Disable mouse capture in the rightmost pane; dragging into the iframe drops the divider otherwise - }; - } +const Pane = ({ width, children, isDragging, moveBrew, moveSource, liveScroll, setMoveArrows })=>{ + const styles = width + ? { flex: 'none', width: `${width}px` } + : { pointerEvents: isDragging ? 'none' : 'auto' }; //Disable mouse capture in the right pane; else dragging into the iframe drops the divider - return
- {this.props.children} -
; - } -}); + return ( +
+ {React.cloneElement(children, { moveBrew, moveSource, liveScroll, setMoveArrows })} +
+ ); +}; module.exports = SplitPane; diff --git a/shared/naturalcrit/splitPane/splitPane.less b/shared/naturalcrit/splitPane/splitPane.less index e5b3dd7f8..8b61097be 100644 --- a/shared/naturalcrit/splitPane/splitPane.less +++ b/shared/naturalcrit/splitPane/splitPane.less @@ -1,69 +1,68 @@ -.splitPane{ +.splitPane { position : relative; display : flex; + flex-direction : row; height : 100%; outline : none; - flex-direction : row; - .pane{ + .pane { + flex : 1; overflow-x : hidden; overflow-y : hidden; - flex : 1; } - .divider{ - touch-action : none; + .divider { + position : relative; display : table; - height : 100%; width : 15px; - cursor : ew-resize; - background-color : #bbb; + height : 100%; text-align : center; - .dots{ + touch-action : none; + cursor : ew-resize; + background-color : #BBBBBB; + .dots { display : table-cell; - vertical-align : middle; text-align : center; - i{ + vertical-align : middle; + i { display : block !important; margin : 10px 0px; font-size : 6px; - color : #666; + color : #666666; } } - &:hover{ - background-color: #999; - } + &:hover,&.dragging { background-color : #999999; } } - .arrow{ + .arrow { position : absolute; + left : 50%; + z-index : 999; width : 25px; height : 25px; - border : 2px solid #bbb; - border-radius : 15px; - text-align : center; font-size : 1.2em; + text-align : center; cursor : pointer; - background-color : #ddd; - z-index : 999; - box-shadow : 0 4px 5px #0000007f; - &.left{ + background-color : #DDDDDD; + border : 2px solid #BBBBBB; + border-radius : 15px; + box-shadow : 0 4px 5px #0000007F; + translate : -50%; + &.left { .tooltipLeft('Jump to location in Editor'); top : 30px; } - &.right{ + &.right { .tooltipRight('Jump to location in Preview'); top : 60px; } - &.lock{ + &.lock { .tooltipRight('De-sync Editor and Preview locations.'); - top : 90px; - background: #666; + top : 90px; + background : #666666; } - &.unlock{ + &.unlock { .tooltipRight('Sync Editor and Preview locations'); top : 90px; } - &:hover{ - background-color: #666; - } + &:hover { background-color : #666666; } } }