layout-context.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import {
  2. createContext,
  3. useContext,
  4. useCallback,
  5. useMemo,
  6. useEffect,
  7. } from 'react'
  8. import PropTypes from 'prop-types'
  9. import useScopeValue from '../hooks/use-scope-value'
  10. import useDetachLayout from '../hooks/use-detach-layout'
  11. import { useIdeContext } from './ide-context'
  12. import localStorage from '../../infrastructure/local-storage'
  13. import getMeta from '../../utils/meta'
  14. const debugPdfDetach = getMeta('ol-debugPdfDetach')
  15. export const LayoutContext = createContext()
  16. LayoutContext.Provider.propTypes = {
  17. value: PropTypes.shape({
  18. reattach: PropTypes.func.isRequired,
  19. detach: PropTypes.func.isRequired,
  20. detachIsLinked: PropTypes.bool,
  21. detachRole: PropTypes.string,
  22. changeLayout: PropTypes.func.isRequired,
  23. view: PropTypes.string,
  24. setView: PropTypes.func.isRequired,
  25. chatIsOpen: PropTypes.bool,
  26. setChatIsOpen: PropTypes.func.isRequired,
  27. reviewPanelOpen: PropTypes.bool,
  28. setReviewPanelOpen: PropTypes.func.isRequired,
  29. leftMenuShown: PropTypes.bool,
  30. setLeftMenuShown: PropTypes.func.isRequired,
  31. pdfLayout: PropTypes.oneOf(['sideBySide', 'flat']).isRequired,
  32. }).isRequired,
  33. }
  34. function setLayoutInLocalStorage(pdfLayout) {
  35. localStorage.setItem(
  36. 'pdf.layout',
  37. pdfLayout === 'sideBySide' ? 'split' : 'flat'
  38. )
  39. }
  40. export function LayoutProvider({ children }) {
  41. const { $scope } = useIdeContext()
  42. // what to show in the "flat" view (editor or pdf)
  43. const [view, _setView] = useScopeValue('ui.view')
  44. const setView = useCallback(
  45. value => {
  46. _setView(oldValue => {
  47. // ensure that the "history:toggle" event is broadcast when switching in or out of history view
  48. if (value === 'history' || oldValue === 'history') {
  49. $scope.toggleHistory()
  50. }
  51. return value
  52. })
  53. },
  54. [$scope, _setView]
  55. )
  56. // whether the chat pane is open
  57. const [chatIsOpen, setChatIsOpen] = useScopeValue('ui.chatOpen')
  58. // whether the review pane is open
  59. const [reviewPanelOpen, setReviewPanelOpen] =
  60. useScopeValue('ui.reviewPanelOpen')
  61. // whether the menu pane is open
  62. const [leftMenuShown, setLeftMenuShown] = useScopeValue('ui.leftMenuShown')
  63. // whether to display the editor and preview side-by-side or full-width ("flat")
  64. const [pdfLayout, setPdfLayout] = useScopeValue('ui.pdfLayout')
  65. // switch to either side-by-side or flat (full-width) layout
  66. const switchLayout = useCallback(() => {
  67. setPdfLayout(layout => {
  68. const newLayout = layout === 'sideBySide' ? 'flat' : 'sideBySide'
  69. setView(newLayout === 'sideBySide' ? 'editor' : 'pdf')
  70. setPdfLayout(newLayout)
  71. setLayoutInLocalStorage(newLayout)
  72. })
  73. }, [setPdfLayout, setView])
  74. const changeLayout = useCallback(
  75. (newLayout, newView) => {
  76. setPdfLayout(newLayout)
  77. setView(newLayout === 'sideBySide' ? 'editor' : newView)
  78. setLayoutInLocalStorage(newLayout)
  79. },
  80. [setPdfLayout, setView]
  81. )
  82. const {
  83. reattach,
  84. detach,
  85. isLinking: detachIsLinking,
  86. isLinked: detachIsLinked,
  87. role: detachRole,
  88. } = useDetachLayout()
  89. useEffect(() => {
  90. if (debugPdfDetach) {
  91. console.log('Layout Effect', {
  92. detachRole,
  93. detachIsLinking,
  94. detachIsLinked,
  95. })
  96. }
  97. if (detachRole !== 'detacher') return // not in a PDF detacher layout
  98. if (detachIsLinking || detachIsLinked) {
  99. // the tab is linked to a detached tab (or about to be linked); show
  100. // editor only
  101. changeLayout('flat', 'editor')
  102. }
  103. }, [detachRole, detachIsLinking, detachIsLinked, changeLayout])
  104. const value = useMemo(
  105. () => ({
  106. reattach,
  107. detach,
  108. detachIsLinked,
  109. detachRole,
  110. changeLayout,
  111. chatIsOpen,
  112. leftMenuShown,
  113. pdfLayout,
  114. reviewPanelOpen,
  115. setChatIsOpen,
  116. setLeftMenuShown,
  117. setPdfLayout,
  118. setReviewPanelOpen,
  119. setView,
  120. switchLayout,
  121. view,
  122. }),
  123. [
  124. reattach,
  125. detach,
  126. detachIsLinked,
  127. detachRole,
  128. changeLayout,
  129. chatIsOpen,
  130. leftMenuShown,
  131. pdfLayout,
  132. reviewPanelOpen,
  133. setChatIsOpen,
  134. setLeftMenuShown,
  135. setPdfLayout,
  136. setReviewPanelOpen,
  137. setView,
  138. switchLayout,
  139. view,
  140. ]
  141. )
  142. return (
  143. <LayoutContext.Provider value={value}>{children}</LayoutContext.Provider>
  144. )
  145. }
  146. LayoutProvider.propTypes = {
  147. children: PropTypes.any,
  148. }
  149. export function useLayoutContext(propTypes) {
  150. const data = useContext(LayoutContext)
  151. PropTypes.checkPropTypes(propTypes, data, 'data', 'LayoutContext.Provider')
  152. return data
  153. }