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

Avoid changing the view when selecting a folder (#15315)

GitOrigin-RevId: 272c6f87507c65581041150905406f5c38e490d4
Alf Eaton 3 лет назад
Родитель
Сommit
0572535a7e

+ 1 - 1
services/web/frontend/js/features/file-tree/components/file-tree-doc.jsx

@@ -11,7 +11,7 @@ import classnames from 'classnames'
 function FileTreeDoc({ name, id, isFile, isLinkedFile }) {
   const { isSelected, props: selectableEntityProps } = useSelectableEntity(
     id,
-    isFile
+    isFile ? 'file' : 'doc'
   )
 
   return (

+ 4 - 1
services/web/frontend/js/features/file-tree/components/file-tree-folder.jsx

@@ -17,7 +17,10 @@ import usePersistedState from '../../../shared/hooks/use-persisted-state'
 function FileTreeFolder({ name, id, folders, docs, files }) {
   const { t } = useTranslation()
 
-  const { isSelected, props: selectableEntityProps } = useSelectableEntity(id)
+  const { isSelected, props: selectableEntityProps } = useSelectableEntity(
+    id,
+    'folder'
+  )
 
   const { selectedEntityParentIds } = useFileTreeSelectable(id)
 

+ 32 - 3
services/web/frontend/js/features/file-tree/contexts/file-tree-selectable.jsx

@@ -210,9 +210,10 @@ const editorContextPropTypes = {
 
 const isMac = /Mac/.test(window.navigator?.platform)
 
-export function useSelectableEntity(id, isFile) {
+export function useSelectableEntity(id, type) {
   const { view, setView } = useLayoutContext()
   const { setContextMenuCoords } = useFileTreeMainContext()
+  const { fileTreeData } = useFileTreeData()
   const {
     selectedEntityIds,
     selectOrMultiSelectEntity,
@@ -222,6 +223,26 @@ export function useSelectableEntity(id, isFile) {
 
   const isSelected = selectedEntityIds.has(id)
 
+  const chooseView = useCallback(() => {
+    for (const id of selectedEntityIds) {
+      const selectedEntity = findInTree(fileTreeData, id)
+
+      if (selectedEntity.type === 'doc') {
+        return 'editor'
+      }
+
+      if (selectedEntity.type === 'fileRef') {
+        return 'file'
+      }
+
+      if (selectedEntity.type === 'folder') {
+        return view
+      }
+    }
+
+    return null
+  }, [fileTreeData, selectedEntityIds, view])
+
   const handleEvent = useCallback(
     ev => {
       ev.stopPropagation()
@@ -231,7 +252,14 @@ export function useSelectableEntity(id, isFile) {
         !isRootFolderSelected && (isMac ? ev.metaKey : ev.ctrlKey)
       setIsRootFolderSelected(false)
       selectOrMultiSelectEntity(id, multiSelect)
-      setView(isFile ? 'file' : 'editor')
+
+      if (type === 'file') {
+        setView('file')
+      } else if (type === 'doc') {
+        setView('editor')
+      } else if (type === 'folder') {
+        setView(chooseView())
+      }
     },
     [
       id,
@@ -239,7 +267,8 @@ export function useSelectableEntity(id, isFile) {
       setIsRootFolderSelected,
       selectOrMultiSelectEntity,
       setView,
-      isFile,
+      type,
+      chooseView,
     ]
   )