2021-06-23 05:37:08 -04:00
|
|
|
import { createContext, useContext, useCallback, useMemo } from 'react'
|
2021-02-23 05:17:41 -05:00
|
|
|
import PropTypes from 'prop-types'
|
2021-10-08 06:09:41 -04:00
|
|
|
import useScopeValue from '../hooks/use-scope-value'
|
2021-06-16 05:32:38 -04:00
|
|
|
import { useIdeContext } from './ide-context'
|
2021-10-15 05:39:56 -04:00
|
|
|
import localStorage from '../../infrastructure/local-storage'
|
2021-02-23 05:17:41 -05:00
|
|
|
|
|
|
|
export const LayoutContext = createContext()
|
|
|
|
|
|
|
|
LayoutContext.Provider.propTypes = {
|
|
|
|
value: PropTypes.shape({
|
|
|
|
view: PropTypes.string,
|
|
|
|
setView: PropTypes.func.isRequired,
|
|
|
|
chatIsOpen: PropTypes.bool,
|
|
|
|
setChatIsOpen: PropTypes.func.isRequired,
|
|
|
|
reviewPanelOpen: PropTypes.bool,
|
|
|
|
setReviewPanelOpen: PropTypes.func.isRequired,
|
|
|
|
leftMenuShown: PropTypes.bool,
|
2021-03-10 07:19:54 -05:00
|
|
|
setLeftMenuShown: PropTypes.func.isRequired,
|
2021-10-15 05:39:56 -04:00
|
|
|
pdfLayout: PropTypes.oneOf(['sideBySide', 'flat']).isRequired,
|
2021-04-27 03:52:58 -04:00
|
|
|
}).isRequired,
|
2021-02-23 05:17:41 -05:00
|
|
|
}
|
|
|
|
|
2021-06-16 05:32:38 -04:00
|
|
|
export function LayoutProvider({ children }) {
|
|
|
|
const { $scope } = useIdeContext()
|
|
|
|
|
2021-10-08 05:51:04 -04:00
|
|
|
// what to show in the "flat" view (editor or pdf)
|
2021-06-16 05:32:38 -04:00
|
|
|
const [view, _setView] = useScopeValue('ui.view')
|
|
|
|
|
2021-05-31 04:25:32 -04:00
|
|
|
const setView = useCallback(
|
|
|
|
value => {
|
2021-06-18 05:29:06 -04:00
|
|
|
_setView(oldValue => {
|
|
|
|
// ensure that the "history:toggle" event is broadcast when switching in or out of history view
|
|
|
|
if (value === 'history' || oldValue === 'history') {
|
|
|
|
$scope.toggleHistory()
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
})
|
2021-05-31 04:25:32 -04:00
|
|
|
},
|
|
|
|
[$scope, _setView]
|
|
|
|
)
|
|
|
|
|
2021-10-08 05:51:04 -04:00
|
|
|
// whether the chat pane is open
|
2021-06-16 05:32:38 -04:00
|
|
|
const [chatIsOpen, setChatIsOpen] = useScopeValue('ui.chatOpen')
|
2021-10-08 05:51:04 -04:00
|
|
|
|
|
|
|
// whether the review pane is open
|
2021-02-23 05:17:41 -05:00
|
|
|
const [reviewPanelOpen, setReviewPanelOpen] = useScopeValue(
|
2021-06-16 05:32:38 -04:00
|
|
|
'ui.reviewPanelOpen'
|
2021-02-23 05:17:41 -05:00
|
|
|
)
|
2021-10-08 05:51:04 -04:00
|
|
|
|
|
|
|
// whether the menu pane is open
|
2021-06-16 05:32:38 -04:00
|
|
|
const [leftMenuShown, setLeftMenuShown] = useScopeValue('ui.leftMenuShown')
|
2021-10-08 05:51:04 -04:00
|
|
|
|
|
|
|
// whether to display the editor and preview side-by-side or full-width ("flat")
|
|
|
|
const [pdfLayout, setPdfLayout] = useScopeValue('ui.pdfLayout')
|
|
|
|
|
2021-10-15 05:39:56 -04:00
|
|
|
// switch to either side-by-side or flat (full-width) layout
|
|
|
|
const switchLayout = useCallback(() => {
|
|
|
|
setPdfLayout(layout => {
|
|
|
|
const newLayout = layout === 'sideBySide' ? 'flat' : 'sideBySide'
|
|
|
|
setView(newLayout === 'sideBySide' ? 'editor' : 'pdf')
|
|
|
|
setPdfLayout(newLayout)
|
|
|
|
localStorage.setItem('pdf.layout', newLayout)
|
|
|
|
})
|
|
|
|
}, [setPdfLayout, setView])
|
2021-02-23 05:17:41 -05:00
|
|
|
|
2021-06-21 06:02:38 -04:00
|
|
|
const value = useMemo(
|
|
|
|
() => ({
|
|
|
|
chatIsOpen,
|
|
|
|
leftMenuShown,
|
|
|
|
pdfLayout,
|
2021-10-08 05:51:04 -04:00
|
|
|
reviewPanelOpen,
|
|
|
|
setChatIsOpen,
|
|
|
|
setLeftMenuShown,
|
|
|
|
setPdfLayout,
|
|
|
|
setReviewPanelOpen,
|
|
|
|
setView,
|
2021-10-15 05:39:56 -04:00
|
|
|
switchLayout,
|
2021-10-08 05:51:04 -04:00
|
|
|
view,
|
2021-06-21 06:02:38 -04:00
|
|
|
}),
|
|
|
|
[
|
|
|
|
chatIsOpen,
|
|
|
|
leftMenuShown,
|
|
|
|
pdfLayout,
|
|
|
|
reviewPanelOpen,
|
|
|
|
setChatIsOpen,
|
|
|
|
setLeftMenuShown,
|
2021-10-08 05:51:04 -04:00
|
|
|
setPdfLayout,
|
2021-06-21 06:02:38 -04:00
|
|
|
setReviewPanelOpen,
|
|
|
|
setView,
|
2021-10-15 05:39:56 -04:00
|
|
|
switchLayout,
|
2021-06-21 06:02:38 -04:00
|
|
|
view,
|
|
|
|
]
|
|
|
|
)
|
2021-02-23 05:17:41 -05:00
|
|
|
|
|
|
|
return (
|
2021-06-16 05:32:38 -04:00
|
|
|
<LayoutContext.Provider value={value}>{children}</LayoutContext.Provider>
|
2021-02-23 05:17:41 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
LayoutProvider.propTypes = {
|
|
|
|
children: PropTypes.any,
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useLayoutContext(propTypes) {
|
|
|
|
const data = useContext(LayoutContext)
|
|
|
|
PropTypes.checkPropTypes(propTypes, data, 'data', 'LayoutContext.Provider')
|
|
|
|
return data
|
|
|
|
}
|