mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-31 21:21:03 -04:00
845b2fbc04
GitOrigin-RevId: b1a6a4e871af3b52fb3d100a83b479c834cb75ca
101 lines
2.6 KiB
JavaScript
101 lines
2.6 KiB
JavaScript
import { createContext, useContext, useCallback, useMemo } from 'react'
|
|
import PropTypes from 'prop-types'
|
|
import useScopeValue from '../hooks/use-scope-value'
|
|
import { useIdeContext } from './ide-context'
|
|
|
|
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,
|
|
setLeftMenuShown: PropTypes.func.isRequired,
|
|
pdfLayout: PropTypes.oneOf(['sideBySide', 'flat', 'split']).isRequired,
|
|
}).isRequired,
|
|
}
|
|
|
|
export function LayoutProvider({ children }) {
|
|
const { $scope } = useIdeContext()
|
|
|
|
// what to show in the "flat" view (editor or pdf)
|
|
const [view, _setView] = useScopeValue('ui.view')
|
|
|
|
const setView = useCallback(
|
|
value => {
|
|
_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
|
|
})
|
|
},
|
|
[$scope, _setView]
|
|
)
|
|
|
|
// whether the chat pane is open
|
|
const [chatIsOpen, setChatIsOpen] = useScopeValue('ui.chatOpen')
|
|
|
|
// whether the review pane is open
|
|
const [reviewPanelOpen, setReviewPanelOpen] = useScopeValue(
|
|
'ui.reviewPanelOpen'
|
|
)
|
|
|
|
// whether the menu pane is open
|
|
const [leftMenuShown, setLeftMenuShown] = useScopeValue('ui.leftMenuShown')
|
|
|
|
// whether to display the editor and preview side-by-side or full-width ("flat")
|
|
const [pdfLayout, setPdfLayout] = useScopeValue('ui.pdfLayout')
|
|
|
|
// whether the PDF preview pane is hidden
|
|
const [pdfHidden] = useScopeValue('ui.pdfHidden')
|
|
|
|
const value = useMemo(
|
|
() => ({
|
|
chatIsOpen,
|
|
leftMenuShown,
|
|
pdfHidden,
|
|
pdfLayout,
|
|
reviewPanelOpen,
|
|
setChatIsOpen,
|
|
setLeftMenuShown,
|
|
setPdfLayout,
|
|
setReviewPanelOpen,
|
|
setView,
|
|
view,
|
|
}),
|
|
[
|
|
chatIsOpen,
|
|
leftMenuShown,
|
|
pdfHidden,
|
|
pdfLayout,
|
|
reviewPanelOpen,
|
|
setChatIsOpen,
|
|
setLeftMenuShown,
|
|
setPdfLayout,
|
|
setReviewPanelOpen,
|
|
setView,
|
|
view,
|
|
]
|
|
)
|
|
|
|
return (
|
|
<LayoutContext.Provider value={value}>{children}</LayoutContext.Provider>
|
|
)
|
|
}
|
|
|
|
LayoutProvider.propTypes = {
|
|
children: PropTypes.any,
|
|
}
|
|
|
|
export function useLayoutContext(propTypes) {
|
|
const data = useContext(LayoutContext)
|
|
PropTypes.checkPropTypes(propTypes, data, 'data', 'LayoutContext.Provider')
|
|
return data
|
|
}
|