layout-context.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.oneOf(['editor', 'file', 'pdf', 'history']),
  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. pdfPreviewOpen: PropTypes.bool,
  33. }).isRequired,
  34. }
  35. function setLayoutInLocalStorage(pdfLayout) {
  36. localStorage.setItem(
  37. 'pdf.layout',
  38. pdfLayout === 'sideBySide' ? 'split' : 'flat'
  39. )
  40. }
  41. export function LayoutProvider({ children }) {
  42. const { $scope } = useIdeContext()
  43. // what to show in the "flat" view (editor or pdf)
  44. const [view, _setView] = useScopeValue('ui.view')
  45. const setView = useCallback(
  46. value => {
  47. _setView(oldValue => {
  48. // ensure that the "history:toggle" event is broadcast when switching in or out of history view
  49. if (value === 'history' || oldValue === 'history') {
  50. $scope.toggleHistory()
  51. }
  52. if (value === 'editor' && $scope.openFile) {
  53. // if a file is currently opened, ensure the view is 'file' instead of
  54. // 'editor' when the 'editor' view is requested. This is to ensure
  55. // that the entity selected in the file tree is the one visible and
  56. // that docs don't take precendence over files.
  57. return 'file'
  58. }
  59. return value
  60. })
  61. },
  62. [$scope, _setView]
  63. )
  64. // whether the chat pane is open
  65. const [chatIsOpen, setChatIsOpen] = useScopeValue('ui.chatOpen')
  66. // whether the review pane is open
  67. const [reviewPanelOpen, setReviewPanelOpen] =
  68. useScopeValue('ui.reviewPanelOpen')
  69. // whether the menu pane is open
  70. const [leftMenuShown, setLeftMenuShown] = useScopeValue('ui.leftMenuShown')
  71. // whether to display the editor and preview side-by-side or full-width ("flat")
  72. const [pdfLayout, setPdfLayout] = useScopeValue('ui.pdfLayout')
  73. // switch to either side-by-side or flat (full-width) layout
  74. const switchLayout = useCallback(() => {
  75. setPdfLayout(layout => {
  76. const newLayout = layout === 'sideBySide' ? 'flat' : 'sideBySide'
  77. setView(newLayout === 'sideBySide' ? 'editor' : 'pdf')
  78. setPdfLayout(newLayout)
  79. setLayoutInLocalStorage(newLayout)
  80. })
  81. }, [setPdfLayout, setView])
  82. const changeLayout = useCallback(
  83. (newLayout, newView) => {
  84. setPdfLayout(newLayout)
  85. setView(newLayout === 'sideBySide' ? 'editor' : newView)
  86. setLayoutInLocalStorage(newLayout)
  87. },
  88. [setPdfLayout, setView]
  89. )
  90. const {
  91. reattach,
  92. detach,
  93. isLinking: detachIsLinking,
  94. isLinked: detachIsLinked,
  95. role: detachRole,
  96. isRedundant: detachIsRedundant,
  97. } = useDetachLayout()
  98. const pdfPreviewOpen =
  99. pdfLayout === 'sideBySide' || view === 'pdf' || detachRole === 'detacher'
  100. useEffect(() => {
  101. if (debugPdfDetach) {
  102. console.log('Layout Effect', {
  103. detachIsRedundant,
  104. detachRole,
  105. detachIsLinking,
  106. detachIsLinked,
  107. })
  108. }
  109. if (detachRole !== 'detacher') return // not in a PDF detacher layout
  110. if (detachIsRedundant) {
  111. changeLayout('sideBySide')
  112. return
  113. }
  114. if (detachIsLinking || detachIsLinked) {
  115. // the tab is linked to a detached tab (or about to be linked); show
  116. // editor only
  117. changeLayout('flat', 'editor')
  118. }
  119. }, [
  120. detachIsRedundant,
  121. detachRole,
  122. detachIsLinking,
  123. detachIsLinked,
  124. changeLayout,
  125. ])
  126. const value = useMemo(
  127. () => ({
  128. reattach,
  129. detach,
  130. detachIsLinked,
  131. detachRole,
  132. changeLayout,
  133. chatIsOpen,
  134. leftMenuShown,
  135. pdfLayout,
  136. pdfPreviewOpen,
  137. reviewPanelOpen,
  138. setChatIsOpen,
  139. setLeftMenuShown,
  140. setPdfLayout,
  141. setReviewPanelOpen,
  142. setView,
  143. switchLayout,
  144. view,
  145. }),
  146. [
  147. reattach,
  148. detach,
  149. detachIsLinked,
  150. detachRole,
  151. changeLayout,
  152. chatIsOpen,
  153. leftMenuShown,
  154. pdfLayout,
  155. pdfPreviewOpen,
  156. reviewPanelOpen,
  157. setChatIsOpen,
  158. setLeftMenuShown,
  159. setPdfLayout,
  160. setReviewPanelOpen,
  161. setView,
  162. switchLayout,
  163. view,
  164. ]
  165. )
  166. return (
  167. <LayoutContext.Provider value={value}>{children}</LayoutContext.Provider>
  168. )
  169. }
  170. LayoutProvider.propTypes = {
  171. children: PropTypes.any,
  172. }
  173. export function useLayoutContext(propTypes) {
  174. const data = useContext(LayoutContext)
  175. PropTypes.checkPropTypes(propTypes, data, 'data', 'LayoutContext.Provider')
  176. return data
  177. }