editor-manager-context.tsx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. import {
  2. createContext,
  3. FC,
  4. useCallback,
  5. useContext,
  6. useEffect,
  7. useMemo,
  8. useRef,
  9. useState,
  10. } from 'react'
  11. import { sendMB } from '@/infrastructure/event-tracking'
  12. import useScopeValue from '@/shared/hooks/use-scope-value'
  13. import { useIdeContext } from '@/shared/context/ide-context'
  14. import { OpenDocuments } from '@/features/ide-react/editor/open-documents'
  15. import EditorWatchdogManager from '@/features/ide-react/connection/editor-watchdog-manager'
  16. import { useIdeReactContext } from '@/features/ide-react/context/ide-react-context'
  17. import { useConnectionContext } from '@/features/ide-react/context/connection-context'
  18. import { debugConsole } from '@/utils/debugging'
  19. import { DocumentContainer } from '@/features/ide-react/editor/document-container'
  20. import { useLayoutContext } from '@/shared/context/layout-context'
  21. import { GotoLineOptions } from '@/features/ide-react/types/goto-line-options'
  22. import { Doc } from '../../../../../types/doc'
  23. import { useFileTreeData } from '@/shared/context/file-tree-data-context'
  24. import { findDocEntityById } from '@/features/ide-react/util/find-doc-entity-by-id'
  25. import useScopeEventEmitter from '@/shared/hooks/use-scope-event-emitter'
  26. import { useModalsContext } from '@/features/ide-react/context/modals-context'
  27. import { useTranslation } from 'react-i18next'
  28. import customLocalStorage from '@/infrastructure/local-storage'
  29. import useEventListener from '@/shared/hooks/use-event-listener'
  30. import { EditorType } from '@/features/ide-react/editor/types/editor-type'
  31. import { DocId } from '../../../../../types/project-settings'
  32. import { Update } from '@/features/history/services/types/update'
  33. import { useDebugDiffTracker } from '../hooks/use-debug-diff-tracker'
  34. interface GotoOffsetOptions {
  35. gotoOffset: number
  36. }
  37. interface OpenDocOptions
  38. extends Partial<GotoLineOptions>,
  39. Partial<GotoOffsetOptions> {
  40. gotoOffset?: number
  41. forceReopen?: boolean
  42. }
  43. export type EditorManager = {
  44. getEditorType: () => EditorType | null
  45. showSymbolPalette: boolean
  46. currentDocument: DocumentContainer
  47. currentDocumentId: DocId | null
  48. getCurrentDocValue: () => string | null
  49. getCurrentDocId: () => DocId | null
  50. startIgnoringExternalUpdates: () => void
  51. stopIgnoringExternalUpdates: () => void
  52. openDocId: (docId: string, options?: OpenDocOptions) => void
  53. openDoc: (document: Doc, options?: OpenDocOptions) => void
  54. openDocs: OpenDocuments
  55. openInitialDoc: (docId: string) => void
  56. jumpToLine: (options: GotoLineOptions) => void
  57. wantTrackChanges: boolean
  58. setWantTrackChanges: React.Dispatch<
  59. React.SetStateAction<EditorManager['wantTrackChanges']>
  60. >
  61. debugTimers: React.MutableRefObject<Record<string, number>>
  62. }
  63. function hasGotoLine(options: OpenDocOptions): options is GotoLineOptions {
  64. return typeof options.gotoLine === 'number'
  65. }
  66. function hasGotoOffset(options: OpenDocOptions): options is GotoOffsetOptions {
  67. return typeof options.gotoOffset === 'number'
  68. }
  69. export type EditorScopeValue = {
  70. showSymbolPalette: false
  71. toggleSymbolPalette: () => void
  72. sharejs_doc: DocumentContainer | null
  73. open_doc_id: string | null
  74. open_doc_name: string | null
  75. opening: boolean
  76. trackChanges: boolean
  77. wantTrackChanges: boolean
  78. showVisual: boolean
  79. newSourceEditor: boolean
  80. error_state: boolean
  81. }
  82. const EditorManagerContext = createContext<EditorManager | undefined>(undefined)
  83. export const EditorManagerProvider: FC = ({ children }) => {
  84. const { t } = useTranslation()
  85. const ide = useIdeContext()
  86. const { projectId } = useIdeReactContext()
  87. const { reportError, eventEmitter, eventLog } = useIdeReactContext()
  88. const { socket, disconnect, connectionState } = useConnectionContext()
  89. const { view, setView } = useLayoutContext()
  90. const { showGenericMessageModal, genericModalVisible, showOutOfSyncModal } =
  91. useModalsContext()
  92. const [showSymbolPalette, setShowSymbolPalette] = useScopeValue<boolean>(
  93. 'editor.showSymbolPalette'
  94. )
  95. const [showVisual] = useScopeValue<boolean>('editor.showVisual')
  96. const [currentDocument, setCurrentDocument] =
  97. useScopeValue<DocumentContainer>('editor.sharejs_doc')
  98. const [openDocId, setOpenDocId] = useScopeValue<DocId | null>(
  99. 'editor.open_doc_id'
  100. )
  101. const [, setOpenDocName] = useScopeValue<string | null>(
  102. 'editor.open_doc_name'
  103. )
  104. const [, setOpening] = useScopeValue<boolean>('editor.opening')
  105. const [, setIsInErrorState] = useScopeValue<boolean>('editor.error_state')
  106. const [, setTrackChanges] = useScopeValue<boolean>('editor.trackChanges')
  107. const [wantTrackChanges, setWantTrackChanges] = useScopeValue<boolean>(
  108. 'editor.wantTrackChanges'
  109. )
  110. const wantTrackChangesRef = useRef(wantTrackChanges)
  111. useEffect(() => {
  112. wantTrackChangesRef.current = wantTrackChanges
  113. }, [wantTrackChanges])
  114. const goToLineEmitter = useScopeEventEmitter('editor:gotoLine')
  115. const { fileTreeData } = useFileTreeData()
  116. const [ignoringExternalUpdates, setIgnoringExternalUpdates] = useState(false)
  117. const { createDebugDiff, debugTimers } = useDebugDiffTracker(
  118. projectId,
  119. currentDocument
  120. )
  121. const [globalEditorWatchdogManager] = useState(
  122. () =>
  123. new EditorWatchdogManager({
  124. onTimeoutHandler: (meta: Record<string, any>) => {
  125. let diffSize: number | null = null
  126. createDebugDiff()
  127. .then(calculatedDiffSize => {
  128. diffSize = calculatedDiffSize
  129. })
  130. .finally(() => {
  131. sendMB('losing-edits', {
  132. ...meta,
  133. diffSize,
  134. timers: debugTimers.current,
  135. })
  136. reportError('losing-edits', {
  137. ...meta,
  138. diffSize,
  139. timers: debugTimers.current,
  140. })
  141. })
  142. },
  143. })
  144. )
  145. // Store the most recent document error and consume it in an effect, which
  146. // prevents circular dependencies in useCallbacks
  147. const [docError, setDocError] = useState<{
  148. doc: Doc
  149. document: DocumentContainer
  150. error: Error | string
  151. meta?: Record<string, any>
  152. editorContent?: string
  153. } | null>(null)
  154. const [docTooLongErrorShown, setDocTooLongErrorShown] = useState(false)
  155. useEffect(() => {
  156. if (!genericModalVisible) {
  157. setDocTooLongErrorShown(false)
  158. }
  159. }, [genericModalVisible])
  160. const [openDocs] = useState(
  161. () =>
  162. new OpenDocuments(
  163. socket,
  164. globalEditorWatchdogManager,
  165. eventEmitter,
  166. eventLog
  167. )
  168. )
  169. const openDocIdStorageKey = `doc.open_id.${projectId}`
  170. // Persist the open document ID to local storage
  171. useEffect(() => {
  172. if (openDocId) {
  173. customLocalStorage.setItem(openDocIdStorageKey, openDocId)
  174. }
  175. }, [openDocId, openDocIdStorageKey])
  176. const editorOpenDocEpochRef = useRef(0)
  177. // TODO: This looks dodgy because it wraps a state setter and is itself
  178. // stored in React state in the scope store. The problem is that it needs to
  179. // be exposed via the scope store because some components access it that way;
  180. // it would be better to simply access it from a context, but the current
  181. // implementation in EditorManager interacts with Angular scope to update
  182. // the layout. Once Angular is gone, this can become a context method.
  183. useEffect(() => {
  184. ide.scopeStore.set('editor.toggleSymbolPalette', () => {
  185. setShowSymbolPalette(show => {
  186. const newValue = !show
  187. sendMB(newValue ? 'symbol-palette-show' : 'symbol-palette-hide')
  188. return newValue
  189. })
  190. })
  191. }, [ide.scopeStore, setShowSymbolPalette])
  192. const getEditorType = useCallback((): EditorType | null => {
  193. if (!currentDocument) {
  194. return null
  195. }
  196. return showVisual ? 'cm6-rich-text' : 'cm6'
  197. }, [currentDocument, showVisual])
  198. const getCurrentDocValue = useCallback(() => {
  199. return currentDocument?.getSnapshot() ?? null
  200. }, [currentDocument])
  201. const getCurrentDocId = useCallback(() => openDocId, [openDocId])
  202. const startIgnoringExternalUpdates = useCallback(
  203. () => setIgnoringExternalUpdates(true),
  204. []
  205. )
  206. const stopIgnoringExternalUpdates = useCallback(
  207. () => setIgnoringExternalUpdates(false),
  208. []
  209. )
  210. const jumpToLine = useCallback(
  211. (options: GotoLineOptions) => {
  212. goToLineEmitter(
  213. options.gotoLine,
  214. options.gotoColumn ?? 0,
  215. options.syncToPdf ?? false
  216. )
  217. },
  218. [goToLineEmitter]
  219. )
  220. const unbindFromDocumentEvents = (document: DocumentContainer) => {
  221. document.off()
  222. }
  223. const attachErrorHandlerToDocument = useCallback(
  224. (doc: Doc, document: DocumentContainer) => {
  225. document.on(
  226. 'error',
  227. (
  228. error: Error | string,
  229. meta?: Record<string, any>,
  230. editorContent?: string
  231. ) => {
  232. setDocError({ doc, document, error, meta, editorContent })
  233. }
  234. )
  235. },
  236. []
  237. )
  238. const bindToDocumentEvents = useCallback(
  239. (doc: Doc, document: DocumentContainer) => {
  240. attachErrorHandlerToDocument(doc, document)
  241. document.on('externalUpdate', (update: Update) => {
  242. if (ignoringExternalUpdates) {
  243. return
  244. }
  245. if (
  246. update.meta.type === 'external' &&
  247. update.meta.source === 'git-bridge'
  248. ) {
  249. return
  250. }
  251. showGenericMessageModal(
  252. t('document_updated_externally'),
  253. t('document_updated_externally_detail')
  254. )
  255. })
  256. },
  257. [
  258. attachErrorHandlerToDocument,
  259. ignoringExternalUpdates,
  260. showGenericMessageModal,
  261. t,
  262. ]
  263. )
  264. const syncTimeoutRef = useRef<number | null>(null)
  265. const syncTrackChangesState = useCallback(
  266. (doc: DocumentContainer) => {
  267. if (!doc) {
  268. return
  269. }
  270. if (syncTimeoutRef.current) {
  271. window.clearTimeout(syncTimeoutRef.current)
  272. syncTimeoutRef.current = null
  273. }
  274. const want = wantTrackChangesRef.current
  275. const have = doc.getTrackingChanges()
  276. if (want === have) {
  277. setTrackChanges(want)
  278. return
  279. }
  280. const tryToggle = () => {
  281. const saved = doc.getInflightOp() == null && doc.getPendingOp() == null
  282. if (saved) {
  283. doc.setTrackingChanges(want)
  284. setTrackChanges(want)
  285. } else {
  286. syncTimeoutRef.current = window.setTimeout(tryToggle, 100)
  287. }
  288. }
  289. tryToggle()
  290. },
  291. [setTrackChanges]
  292. )
  293. const doOpenNewDocument = useCallback(
  294. (doc: Doc) =>
  295. new Promise<DocumentContainer>((resolve, reject) => {
  296. debugConsole.log('[doOpenNewDocument] Opening...')
  297. const newDocument = openDocs.getDocument(doc._id)
  298. if (!newDocument) {
  299. debugConsole.error(`No open document with ID '${doc._id}' found`)
  300. reject(new Error('no open document found'))
  301. return
  302. }
  303. const preJoinEpoch = ++editorOpenDocEpochRef.current
  304. newDocument.join(error => {
  305. if (error) {
  306. debugConsole.log(
  307. `[doOpenNewDocument] error joining doc ${doc._id}`,
  308. error
  309. )
  310. reject(error)
  311. return
  312. }
  313. if (editorOpenDocEpochRef.current !== preJoinEpoch) {
  314. debugConsole.log(
  315. `[doOpenNewDocument] editorOpenDocEpoch mismatch ${editorOpenDocEpochRef.current} vs ${preJoinEpoch}`
  316. )
  317. newDocument.leaveAndCleanUp()
  318. reject(new Error('another document was loaded'))
  319. }
  320. bindToDocumentEvents(doc, newDocument)
  321. resolve(newDocument)
  322. })
  323. }),
  324. [bindToDocumentEvents, openDocs]
  325. )
  326. const openNewDocument = useCallback(
  327. async (doc: Doc): Promise<DocumentContainer> => {
  328. // Leave the current document
  329. // - when we are opening a different new one, to avoid race conditions
  330. // between leaving and joining the same document
  331. // - when the current one has pending ops that need flushing, to avoid
  332. // race conditions from cleanup
  333. const currentDocId = currentDocument?.doc_id
  334. const hasBufferedOps = currentDocument?.hasBufferedOps()
  335. const changingDoc = currentDocument && currentDocId !== doc._id
  336. if (changingDoc || hasBufferedOps) {
  337. debugConsole.log('[openNewDocument] Leaving existing open doc...')
  338. // Do not trigger any UI changes from remote operations
  339. unbindFromDocumentEvents(currentDocument)
  340. // Keep listening for out-of-sync and similar errors.
  341. attachErrorHandlerToDocument(doc, currentDocument)
  342. // Teardown the Document -> ShareJsDoc -> sharejs doc
  343. // By the time this completes, the Document instance is no longer
  344. // registered in OpenDocuments and doOpenNewDocument can start
  345. // from scratch -- read: no corrupted internal state.
  346. const preLeaveEpoch = ++editorOpenDocEpochRef.current
  347. try {
  348. await currentDocument.leaveAndCleanUpPromise()
  349. } catch (error) {
  350. debugConsole.log(
  351. `[openNewDocument] error leaving doc ${currentDocId}`,
  352. error
  353. )
  354. throw error
  355. }
  356. if (editorOpenDocEpochRef.current !== preLeaveEpoch) {
  357. debugConsole.log(
  358. `[openNewDocument] editorOpenDocEpoch mismatch ${editorOpenDocEpochRef.current} vs ${preLeaveEpoch}`
  359. )
  360. throw new Error('another document was loaded')
  361. }
  362. }
  363. return doOpenNewDocument(doc)
  364. },
  365. [attachErrorHandlerToDocument, doOpenNewDocument, currentDocument]
  366. )
  367. const openDocIdRef = useRef(openDocId)
  368. useEffect(() => {
  369. openDocIdRef.current = openDocId
  370. }, [openDocId])
  371. const openDoc = useCallback(
  372. async (doc: Doc, options: OpenDocOptions = {}) => {
  373. debugConsole.log(`[openDoc] Opening ${doc._id}`)
  374. if (view === 'editor') {
  375. // store position of previous doc before switching docs
  376. eventEmitter.emit('store-doc-position')
  377. }
  378. setView('editor')
  379. const done = (isNewDoc: boolean) => {
  380. window.dispatchEvent(
  381. new CustomEvent('doc:after-opened', { detail: isNewDoc })
  382. )
  383. if (hasGotoLine(options)) {
  384. window.setTimeout(() => jumpToLine(options))
  385. // Jump to the line again after a stored scroll position has been restored
  386. if (isNewDoc) {
  387. window.addEventListener(
  388. 'editor:scroll-position-restored',
  389. () => jumpToLine(options),
  390. { once: true }
  391. )
  392. }
  393. } else if (hasGotoOffset(options)) {
  394. window.setTimeout(() => {
  395. eventEmitter.emit('editor:gotoOffset', options.gotoOffset)
  396. })
  397. }
  398. }
  399. // If we already have the document open, or are opening the document, we can return at this point.
  400. // Note: only use forceReopen:true to override this when the document is
  401. // out of sync and needs to be reloaded from the server.
  402. if (doc._id === openDocIdRef.current && !options.forceReopen) {
  403. done(false)
  404. return
  405. }
  406. // We're now either opening a new document or reloading a broken one.
  407. openDocIdRef.current = doc._id as DocId
  408. setOpenDocId(doc._id as DocId)
  409. setOpenDocName(doc.name)
  410. setOpening(true)
  411. try {
  412. const document = await openNewDocument(doc)
  413. syncTrackChangesState(document)
  414. eventEmitter.emit('doc:opened')
  415. setOpening(false)
  416. setCurrentDocument(document)
  417. done(true)
  418. } catch (error: any) {
  419. if (error?.message === 'another document was loaded') {
  420. debugConsole.log(
  421. `[openDoc] another document was loaded while ${doc._id} was loading`
  422. )
  423. return
  424. }
  425. debugConsole.error('Error opening document', error)
  426. showGenericMessageModal(
  427. t('error_opening_document'),
  428. t('error_opening_document_detail')
  429. )
  430. }
  431. },
  432. [
  433. eventEmitter,
  434. jumpToLine,
  435. openNewDocument,
  436. setCurrentDocument,
  437. setOpenDocId,
  438. setOpenDocName,
  439. setOpening,
  440. setView,
  441. showGenericMessageModal,
  442. syncTrackChangesState,
  443. t,
  444. view,
  445. ]
  446. )
  447. const openDocWithId = useCallback(
  448. (docId: string, options: OpenDocOptions = {}) => {
  449. const doc = findDocEntityById(fileTreeData, docId)
  450. if (!doc) {
  451. return
  452. }
  453. openDoc(doc, options)
  454. },
  455. [fileTreeData, openDoc]
  456. )
  457. const openInitialDoc = useCallback(
  458. (fallbackDocId: string) => {
  459. const docId =
  460. customLocalStorage.getItem(openDocIdStorageKey) || fallbackDocId
  461. if (docId) {
  462. openDocWithId(docId)
  463. }
  464. },
  465. [openDocIdStorageKey, openDocWithId]
  466. )
  467. useEffect(() => {
  468. if (docError) {
  469. const { doc, document, error, meta } = docError
  470. let { editorContent } = docError
  471. const message = typeof error === 'string' ? error : error?.message ?? ''
  472. // Clear document error so that it's only handled once
  473. setDocError(null)
  474. if (message.includes('maxDocLength')) {
  475. openDoc(doc, { forceReopen: true })
  476. showGenericMessageModal(
  477. t('document_too_long'),
  478. t('document_too_long_detail')
  479. )
  480. setDocTooLongErrorShown(true)
  481. } else if (/too many comments or tracked changes/.test(message)) {
  482. showGenericMessageModal(
  483. t('too_many_comments_or_tracked_changes'),
  484. t('too_many_comments_or_tracked_changes_detail')
  485. )
  486. } else if (!docTooLongErrorShown) {
  487. // Do not allow this doc to open another error modal.
  488. document.off('error')
  489. // Preserve the sharejs contents before the teardown.
  490. // eslint-disable-next-line no-unused-vars
  491. editorContent =
  492. typeof editorContent === 'string'
  493. ? editorContent
  494. : document.doc?._doc.snapshot
  495. // Tear down the ShareJsDoc.
  496. if (document.doc) document.doc.clearInflightAndPendingOps()
  497. // Do not re-join after re-connecting.
  498. document.leaveAndCleanUp()
  499. disconnect()
  500. reportError(error, meta)
  501. // Tell the user about the error state.
  502. setIsInErrorState(true)
  503. showOutOfSyncModal(editorContent || '')
  504. // Do not forceReopen the document.
  505. return
  506. }
  507. const handleProjectJoined = () => {
  508. openDoc(doc, { forceReopen: true })
  509. }
  510. eventEmitter.once('project:joined', handleProjectJoined)
  511. return () => {
  512. eventEmitter.off('project:joined', handleProjectJoined)
  513. }
  514. }
  515. }, [
  516. disconnect,
  517. docError,
  518. docTooLongErrorShown,
  519. eventEmitter,
  520. openDoc,
  521. reportError,
  522. setIsInErrorState,
  523. showGenericMessageModal,
  524. showOutOfSyncModal,
  525. t,
  526. ])
  527. useEventListener('editor:insert-symbol', () => {
  528. sendMB('symbol-palette-insert')
  529. })
  530. useEventListener('flush-changes', () => {
  531. openDocs.flushAll()
  532. })
  533. useEventListener('blur', () => {
  534. openDocs.flushAll()
  535. })
  536. // Flush changes before disconnecting
  537. useEffect(() => {
  538. if (connectionState.forceDisconnected) {
  539. openDocs.flushAll()
  540. }
  541. }, [connectionState.forceDisconnected, openDocs])
  542. // Watch for changes in wantTrackChanges
  543. const previousWantTrackChangesRef = useRef(wantTrackChanges)
  544. useEffect(() => {
  545. if (wantTrackChanges !== previousWantTrackChangesRef.current) {
  546. previousWantTrackChangesRef.current = wantTrackChanges
  547. syncTrackChangesState(currentDocument)
  548. }
  549. }, [currentDocument, syncTrackChangesState, wantTrackChanges])
  550. const editorManager = useMemo(
  551. () => ({
  552. getEditorType,
  553. showSymbolPalette,
  554. currentDocument,
  555. currentDocumentId: openDocId,
  556. getCurrentDocValue,
  557. getCurrentDocId,
  558. startIgnoringExternalUpdates,
  559. stopIgnoringExternalUpdates,
  560. openDocId: openDocWithId,
  561. openDoc,
  562. openDocs,
  563. openInitialDoc,
  564. jumpToLine,
  565. wantTrackChanges,
  566. setWantTrackChanges,
  567. debugTimers,
  568. }),
  569. [
  570. getEditorType,
  571. showSymbolPalette,
  572. currentDocument,
  573. openDocId,
  574. getCurrentDocValue,
  575. getCurrentDocId,
  576. startIgnoringExternalUpdates,
  577. stopIgnoringExternalUpdates,
  578. openDocWithId,
  579. openDoc,
  580. openDocs,
  581. openInitialDoc,
  582. jumpToLine,
  583. wantTrackChanges,
  584. setWantTrackChanges,
  585. debugTimers,
  586. ]
  587. )
  588. // Expose editorManager via ide object because some React code relies on it,
  589. // for now
  590. ide.editorManager = editorManager
  591. return (
  592. <EditorManagerContext.Provider value={editorManager}>
  593. {children}
  594. </EditorManagerContext.Provider>
  595. )
  596. }
  597. export function useEditorManagerContext(): EditorManager {
  598. const context = useContext(EditorManagerContext)
  599. if (!context) {
  600. throw new Error(
  601. 'useEditorManagerContext is only available inside EditorManagerProvider'
  602. )
  603. }
  604. return context
  605. }