Просмотр исходного кода

Call selectEntity directly after a doc is opened (#18329)

GitOrigin-RevId: bf471812bfeb5e394252733a16691430d7d45840
Alf Eaton 2 лет назад
Родитель
Сommit
c8947c781e

+ 3 - 1
services/web/frontend/js/features/ide-react/context/editor-manager-context.tsx

@@ -428,7 +428,9 @@ export const EditorManagerProvider: FC = ({ children }) => {
 
       const done = (isNewDoc: boolean) => {
         window.dispatchEvent(
-          new CustomEvent('doc:after-opened', { detail: isNewDoc })
+          new CustomEvent('doc:after-opened', {
+            detail: { isNewDoc, docId: doc._id },
+          })
         )
         if (hasGotoLine(options)) {
           window.setTimeout(() => jumpToLine(options))

+ 10 - 11
services/web/frontend/js/features/ide-react/context/file-tree-open-context.tsx

@@ -23,6 +23,7 @@ import { debugConsole } from '@/utils/debugging'
 import { convertFileRefToBinaryFile } from '@/features/ide-react/util/file-view'
 import { sendMB } from '@/infrastructure/event-tracking'
 import { FileRef } from '../../../../../types/file-ref'
+import useEventListener from '@/shared/hooks/use-event-listener'
 
 const FileTreeOpenContext = createContext<
   | {
@@ -113,21 +114,19 @@ export const FileTreeOpenProvider: FC = ({ children }) => {
     [eventEmitter, openDocId, openDocWithId, rootDocId]
   )
 
-  const openDocIdRef = useRef<typeof openDocId | null>(null)
-
   // Synchronize the file tree when openDoc or openDocId is called on the editor
   // manager context from elsewhere. If the file tree does change, it will
   // trigger the onSelect handler in this component, which will update the local
   // state.
-  useEffect(() => {
-    if (openDocId !== openDocIdRef.current) {
-      debugConsole.log(`openDocId changed to ${openDocId}`)
-      openDocIdRef.current = openDocId
-      if (openDocId !== null) {
-        selectEntity(openDocId)
-      }
-    }
-  }, [openDocId, selectEntity])
+  useEventListener(
+    'doc:after-opened',
+    useCallback(
+      (event: CustomEvent<{ docId: string }>) => {
+        selectEntity(event.detail.docId)
+      },
+      [selectEntity]
+    )
+  )
 
   // Open a document once the file tree and project are ready
   const initialOpenDoneRef = useRef(false)

+ 1 - 1
services/web/frontend/js/features/ide-react/context/outline-context.tsx

@@ -91,7 +91,7 @@ export const OutlineProvider: FC = ({ children }) => {
   useEventListener(
     'doc:after-opened',
     useCallback(evt => {
-      if (evt.detail) {
+      if (evt.detail.isNewDoc) {
         setIgnoreNextCursorUpdate(true)
       }
       setBinaryFileOpened(false)