2021-11-15 11:33:57 -05:00
|
|
|
import {
|
|
|
|
createContext,
|
|
|
|
useContext,
|
|
|
|
useCallback,
|
|
|
|
useMemo,
|
|
|
|
useEffect,
|
|
|
|
} 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-11-15 11:33:57 -05:00
|
|
|
import useDetachLayout from '../hooks/use-detach-layout'
|
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-11-30 09:54:14 -05:00
|
|
|
import getMeta from '../../utils/meta'
|
|
|
|
|
|
|
|
const debugPdfDetach = getMeta('ol-debugPdfDetach')
|
2021-02-23 05:17:41 -05:00
|
|
|
|
|
|
|
export const LayoutContext = createContext()
|
|
|
|
|
|
|
|
LayoutContext.Provider.propTypes = {
|
|
|
|
value: PropTypes.shape({
|
2021-11-30 09:54:14 -05:00
|
|
|
reattach: PropTypes.func.isRequired,
|
|
|
|
detach: PropTypes.func.isRequired,
|
|
|
|
detachIsLinked: PropTypes.bool,
|
|
|
|
detachRole: PropTypes.string,
|
|
|
|
changeLayout: PropTypes.func.isRequired,
|
2022-03-15 09:55:17 -04:00
|
|
|
view: PropTypes.oneOf(['editor', 'file', 'pdf', 'history']),
|
2021-02-23 05:17:41 -05:00
|
|
|
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-11-30 09:53:59 -05:00
|
|
|
function setLayoutInLocalStorage(pdfLayout) {
|
|
|
|
localStorage.setItem(
|
|
|
|
'pdf.layout',
|
|
|
|
pdfLayout === 'sideBySide' ? 'split' : 'flat'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2022-03-15 09:55:17 -04:00
|
|
|
if (value === 'editor' && $scope.openFile) {
|
|
|
|
// if a file is currently opened, ensure the view is 'file' instead of
|
|
|
|
// 'editor' when the 'editor' view is requested. This is to ensure
|
|
|
|
// that the entity selected in the file tree is the one visible and
|
|
|
|
// that docs don't take precendence over files.
|
|
|
|
return 'file'
|
|
|
|
}
|
|
|
|
|
2021-06-18 05:29:06 -04:00
|
|
|
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
|
2022-01-10 05:23:05 -05:00
|
|
|
const [reviewPanelOpen, setReviewPanelOpen] =
|
|
|
|
useScopeValue('ui.reviewPanelOpen')
|
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)
|
2021-11-30 09:53:59 -05:00
|
|
|
setLayoutInLocalStorage(newLayout)
|
2021-10-15 05:39:56 -04:00
|
|
|
})
|
|
|
|
}, [setPdfLayout, setView])
|
2021-02-23 05:17:41 -05:00
|
|
|
|
2021-11-03 09:21:14 -04:00
|
|
|
const changeLayout = useCallback(
|
|
|
|
(newLayout, newView) => {
|
|
|
|
setPdfLayout(newLayout)
|
|
|
|
setView(newLayout === 'sideBySide' ? 'editor' : newView)
|
2021-11-30 09:53:59 -05:00
|
|
|
setLayoutInLocalStorage(newLayout)
|
2021-11-03 09:21:14 -04:00
|
|
|
},
|
|
|
|
[setPdfLayout, setView]
|
|
|
|
)
|
|
|
|
|
2021-11-15 11:33:57 -05:00
|
|
|
const {
|
|
|
|
reattach,
|
|
|
|
detach,
|
2021-11-30 09:54:14 -05:00
|
|
|
isLinking: detachIsLinking,
|
|
|
|
isLinked: detachIsLinked,
|
2021-11-15 11:33:57 -05:00
|
|
|
role: detachRole,
|
|
|
|
} = useDetachLayout()
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-11-30 09:54:14 -05:00
|
|
|
if (debugPdfDetach) {
|
|
|
|
console.log('Layout Effect', {
|
|
|
|
detachRole,
|
|
|
|
detachIsLinking,
|
|
|
|
detachIsLinked,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (detachRole !== 'detacher') return // not in a PDF detacher layout
|
|
|
|
|
|
|
|
if (detachIsLinking || detachIsLinked) {
|
|
|
|
// the tab is linked to a detached tab (or about to be linked); show
|
|
|
|
// editor only
|
2021-12-02 10:18:38 -05:00
|
|
|
changeLayout('flat', 'editor')
|
2021-11-15 11:33:57 -05:00
|
|
|
}
|
2021-12-02 10:18:38 -05:00
|
|
|
}, [detachRole, detachIsLinking, detachIsLinked, changeLayout])
|
2021-11-15 11:33:57 -05:00
|
|
|
|
2021-06-21 06:02:38 -04:00
|
|
|
const value = useMemo(
|
|
|
|
() => ({
|
2021-11-15 11:33:57 -05:00
|
|
|
reattach,
|
|
|
|
detach,
|
2021-11-30 09:54:14 -05:00
|
|
|
detachIsLinked,
|
2021-11-15 11:33:57 -05:00
|
|
|
detachRole,
|
2021-11-03 09:21:14 -04:00
|
|
|
changeLayout,
|
2021-06-21 06:02:38 -04:00
|
|
|
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
|
|
|
}),
|
|
|
|
[
|
2021-11-15 11:33:57 -05:00
|
|
|
reattach,
|
|
|
|
detach,
|
2021-11-30 09:54:14 -05:00
|
|
|
detachIsLinked,
|
2021-11-15 11:33:57 -05:00
|
|
|
detachRole,
|
2021-11-03 09:21:14 -04:00
|
|
|
changeLayout,
|
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
|
|
|
|
}
|