layout-context.js 5.1 KB

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