2021-06-23 05:37:08 -04:00
|
|
|
import {
|
2021-01-07 09:22:40 -05:00
|
|
|
createContext,
|
2021-03-10 04:11:43 -05:00
|
|
|
useCallback,
|
2021-01-07 09:22:40 -05:00
|
|
|
useContext,
|
|
|
|
useReducer,
|
|
|
|
useEffect,
|
2021-03-10 04:11:43 -05:00
|
|
|
useMemo,
|
2021-04-27 03:52:58 -04:00
|
|
|
useState,
|
2021-01-07 09:22:40 -05:00
|
|
|
} from 'react'
|
2020-11-26 09:22:30 -05:00
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import classNames from 'classnames'
|
2021-12-14 08:24:41 -05:00
|
|
|
import _ from 'lodash'
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
import { findInTree } from '../util/find-in-tree'
|
2022-01-10 10:47:01 -05:00
|
|
|
import { useFileTreeData } from '../../../shared/context/file-tree-data-context'
|
2022-01-10 10:46:46 -05:00
|
|
|
import { useProjectContext } from '../../../shared/context/project-context'
|
|
|
|
import { useEditorContext } from '../../../shared/context/editor-context'
|
2022-03-01 04:14:41 -05:00
|
|
|
import { useLayoutContext } from '../../../shared/context/layout-context'
|
2021-05-18 06:56:56 -04:00
|
|
|
import usePersistedState from '../../../shared/hooks/use-persisted-state'
|
2021-12-14 08:24:41 -05:00
|
|
|
import usePreviousValue from '../../../shared/hooks/use-previous-value'
|
2023-10-12 04:38:31 -04:00
|
|
|
import { useFileTreeMainContext } from '@/features/file-tree/contexts/file-tree-main'
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2021-01-07 09:22:40 -05:00
|
|
|
const FileTreeSelectableContext = createContext()
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
const ACTION_TYPES = {
|
|
|
|
SELECT: 'SELECT',
|
|
|
|
MULTI_SELECT: 'MULTI_SELECT',
|
2021-04-27 03:52:58 -04:00
|
|
|
UNSELECT: 'UNSELECT',
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function fileTreeSelectableReadWriteReducer(selectedEntityIds, action) {
|
|
|
|
switch (action.type) {
|
|
|
|
case ACTION_TYPES.SELECT: {
|
|
|
|
// reset selection
|
2023-10-12 04:38:31 -04:00
|
|
|
return new Set(Array.isArray(action.id) ? action.id : [action.id])
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
case ACTION_TYPES.MULTI_SELECT: {
|
|
|
|
const selectedEntityIdsCopy = new Set(selectedEntityIds)
|
|
|
|
if (selectedEntityIdsCopy.has(action.id)) {
|
|
|
|
// entity already selected
|
|
|
|
if (selectedEntityIdsCopy.size > 1) {
|
|
|
|
// entity already multi-selected; remove from set
|
|
|
|
selectedEntityIdsCopy.delete(action.id)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// entity not selected: add to set
|
|
|
|
selectedEntityIdsCopy.add(action.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return selectedEntityIdsCopy
|
|
|
|
}
|
|
|
|
|
|
|
|
case ACTION_TYPES.UNSELECT: {
|
|
|
|
const selectedEntityIdsCopy = new Set(selectedEntityIds)
|
|
|
|
selectedEntityIdsCopy.delete(action.id)
|
|
|
|
return selectedEntityIdsCopy
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Error(`Unknown selectable action type: ${action.type}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function fileTreeSelectableReadOnlyReducer(selectedEntityIds, action) {
|
|
|
|
switch (action.type) {
|
|
|
|
case ACTION_TYPES.SELECT:
|
|
|
|
return new Set([action.id])
|
|
|
|
|
|
|
|
case ACTION_TYPES.MULTI_SELECT:
|
|
|
|
case ACTION_TYPES.UNSELECT:
|
|
|
|
return selectedEntityIds
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Error(`Unknown selectable action type: ${action.type}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-18 05:26:22 -04:00
|
|
|
export function FileTreeSelectableProvider({ onSelect, children }) {
|
2022-01-10 10:47:10 -05:00
|
|
|
const { _id: projectId, rootDocId } = useProjectContext(
|
2022-01-10 10:46:46 -05:00
|
|
|
projectContextPropTypes
|
|
|
|
)
|
|
|
|
const { permissionsLevel } = useEditorContext(editorContextPropTypes)
|
2021-01-07 09:22:40 -05:00
|
|
|
|
|
|
|
const [initialSelectedEntityId] = usePersistedState(
|
|
|
|
`doc.open_id.${projectId}`,
|
|
|
|
rootDocId
|
|
|
|
)
|
|
|
|
|
2022-03-21 10:47:01 -04:00
|
|
|
const { fileTreeData, setSelectedEntities } = useFileTreeData()
|
2021-01-08 05:03:10 -05:00
|
|
|
|
2023-08-18 05:26:22 -04:00
|
|
|
const [isRootFolderSelected, setIsRootFolderSelected] = useState(false)
|
|
|
|
|
2020-11-26 09:22:30 -05:00
|
|
|
const [selectedEntityIds, dispatch] = useReducer(
|
2022-01-10 10:46:46 -05:00
|
|
|
permissionsLevel === 'readOnly'
|
|
|
|
? fileTreeSelectableReadOnlyReducer
|
|
|
|
: fileTreeSelectableReadWriteReducer,
|
2021-01-08 05:03:10 -05:00
|
|
|
null,
|
|
|
|
() => {
|
|
|
|
if (!initialSelectedEntityId) return new Set()
|
|
|
|
|
|
|
|
// the entity with id=initialSelectedEntityId might not exist in the tree
|
|
|
|
// anymore. This checks that it exists before initialising the reducer
|
|
|
|
// with the id.
|
|
|
|
if (findInTree(fileTreeData, initialSelectedEntityId))
|
|
|
|
return new Set([initialSelectedEntityId])
|
|
|
|
|
|
|
|
// the entity doesn't exist anymore; don't select any files
|
|
|
|
return new Set()
|
|
|
|
}
|
2020-11-26 09:22:30 -05:00
|
|
|
)
|
|
|
|
|
2021-01-07 09:22:40 -05:00
|
|
|
const [selectedEntityParentIds, setSelectedEntityParentIds] = useState(
|
|
|
|
new Set()
|
|
|
|
)
|
|
|
|
|
|
|
|
// fills `selectedEntityParentIds` set
|
|
|
|
useEffect(() => {
|
|
|
|
const ids = new Set()
|
|
|
|
selectedEntityIds.forEach(id => {
|
|
|
|
const found = findInTree(fileTreeData, id)
|
|
|
|
if (found) {
|
|
|
|
found.path.forEach(pathItem => ids.add(pathItem))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
setSelectedEntityParentIds(ids)
|
|
|
|
}, [fileTreeData, selectedEntityIds])
|
|
|
|
|
2020-11-26 09:22:30 -05:00
|
|
|
// calls `onSelect` on entities selection
|
2021-12-14 08:24:41 -05:00
|
|
|
const previousSelectedEntityIds = usePreviousValue(selectedEntityIds)
|
2020-12-15 05:23:54 -05:00
|
|
|
useEffect(() => {
|
2021-12-14 08:24:41 -05:00
|
|
|
if (_.isEqual(selectedEntityIds, previousSelectedEntityIds)) {
|
|
|
|
return
|
|
|
|
}
|
2022-03-21 10:47:01 -04:00
|
|
|
const _selectedEntities = Array.from(selectedEntityIds)
|
2021-05-18 06:58:53 -04:00
|
|
|
.map(id => findInTree(fileTreeData, id))
|
|
|
|
.filter(Boolean)
|
2022-03-21 10:47:01 -04:00
|
|
|
onSelect(_selectedEntities)
|
|
|
|
setSelectedEntities(_selectedEntities)
|
|
|
|
}, [
|
|
|
|
fileTreeData,
|
|
|
|
selectedEntityIds,
|
|
|
|
previousSelectedEntityIds,
|
|
|
|
onSelect,
|
|
|
|
setSelectedEntities,
|
|
|
|
])
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
// listen for `editor.openDoc` and selected that doc
|
|
|
|
function handleOpenDoc(ev) {
|
2021-01-27 04:54:29 -05:00
|
|
|
const found = findInTree(fileTreeData, ev.detail)
|
|
|
|
if (!found) return
|
|
|
|
|
|
|
|
dispatch({ type: ACTION_TYPES.SELECT, id: found.entity._id })
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
2023-08-14 07:38:09 -04:00
|
|
|
|
2020-11-26 09:22:30 -05:00
|
|
|
window.addEventListener('editor.openDoc', handleOpenDoc)
|
|
|
|
return () => window.removeEventListener('editor.openDoc', handleOpenDoc)
|
2021-01-27 04:54:29 -05:00
|
|
|
}, [fileTreeData])
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2021-05-04 07:36:22 -04:00
|
|
|
const select = useCallback(id => {
|
|
|
|
dispatch({ type: ACTION_TYPES.SELECT, id })
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const unselect = useCallback(id => {
|
|
|
|
dispatch({ type: ACTION_TYPES.UNSELECT, id })
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const selectOrMultiSelectEntity = useCallback((id, isMultiSelect) => {
|
|
|
|
const actionType = isMultiSelect
|
|
|
|
? ACTION_TYPES.MULTI_SELECT
|
|
|
|
: ACTION_TYPES.SELECT
|
|
|
|
|
|
|
|
dispatch({ type: actionType, id })
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const value = {
|
|
|
|
selectedEntityIds,
|
|
|
|
selectedEntityParentIds,
|
|
|
|
select,
|
|
|
|
unselect,
|
|
|
|
selectOrMultiSelectEntity,
|
2023-08-18 05:26:22 -04:00
|
|
|
isRootFolderSelected,
|
|
|
|
setIsRootFolderSelected,
|
2021-05-04 07:36:22 -04:00
|
|
|
}
|
|
|
|
|
2020-11-26 09:22:30 -05:00
|
|
|
return (
|
2021-05-04 07:36:22 -04:00
|
|
|
<FileTreeSelectableContext.Provider value={value}>
|
2020-11-26 09:22:30 -05:00
|
|
|
{children}
|
|
|
|
</FileTreeSelectableContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
FileTreeSelectableProvider.propTypes = {
|
|
|
|
onSelect: PropTypes.func.isRequired,
|
|
|
|
children: PropTypes.oneOfType([
|
|
|
|
PropTypes.arrayOf(PropTypes.node),
|
2021-04-27 03:52:58 -04:00
|
|
|
PropTypes.node,
|
|
|
|
]).isRequired,
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
|
2022-01-10 10:46:46 -05:00
|
|
|
const projectContextPropTypes = {
|
|
|
|
_id: PropTypes.string.isRequired,
|
2022-01-10 10:47:10 -05:00
|
|
|
rootDocId: PropTypes.string,
|
2022-01-10 10:46:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const editorContextPropTypes = {
|
|
|
|
permissionsLevel: PropTypes.oneOf(['readOnly', 'readAndWrite', 'owner']),
|
|
|
|
}
|
2022-03-01 04:14:41 -05:00
|
|
|
|
2023-10-12 04:38:31 -04:00
|
|
|
const isMac = /Mac/.test(window.navigator?.platform)
|
|
|
|
|
2023-10-20 06:17:23 -04:00
|
|
|
export function useSelectableEntity(id, type) {
|
2023-10-18 05:14:13 -04:00
|
|
|
const { view, setView } = useLayoutContext()
|
2023-10-12 04:38:31 -04:00
|
|
|
const { setContextMenuCoords } = useFileTreeMainContext()
|
2023-10-20 06:17:23 -04:00
|
|
|
const { fileTreeData } = useFileTreeData()
|
2023-08-14 07:38:09 -04:00
|
|
|
const {
|
|
|
|
selectedEntityIds,
|
|
|
|
selectOrMultiSelectEntity,
|
2023-08-18 05:26:22 -04:00
|
|
|
isRootFolderSelected,
|
|
|
|
setIsRootFolderSelected,
|
2023-08-14 07:38:09 -04:00
|
|
|
} = useContext(FileTreeSelectableContext)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
const isSelected = selectedEntityIds.has(id)
|
|
|
|
|
2023-10-20 06:17:23 -04:00
|
|
|
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])
|
|
|
|
|
2021-05-04 07:36:22 -04:00
|
|
|
const handleEvent = useCallback(
|
2021-03-10 04:11:43 -05:00
|
|
|
ev => {
|
2023-08-14 07:38:09 -04:00
|
|
|
ev.stopPropagation()
|
2023-10-12 04:38:31 -04:00
|
|
|
// use Command (macOS) or Ctrl (other OS) to select multiple items,
|
|
|
|
// as long as the root folder wasn't selected
|
|
|
|
const multiSelect =
|
|
|
|
!isRootFolderSelected && (isMac ? ev.metaKey : ev.ctrlKey)
|
2023-08-18 05:26:22 -04:00
|
|
|
setIsRootFolderSelected(false)
|
2023-10-12 04:38:31 -04:00
|
|
|
selectOrMultiSelectEntity(id, multiSelect)
|
2023-10-20 06:17:23 -04:00
|
|
|
|
|
|
|
if (type === 'file') {
|
|
|
|
setView('file')
|
|
|
|
} else if (type === 'doc') {
|
|
|
|
setView('editor')
|
|
|
|
} else if (type === 'folder') {
|
|
|
|
setView(chooseView())
|
|
|
|
}
|
2021-03-10 04:11:43 -05:00
|
|
|
},
|
2023-10-12 04:38:31 -04:00
|
|
|
[
|
|
|
|
id,
|
|
|
|
isRootFolderSelected,
|
|
|
|
setIsRootFolderSelected,
|
|
|
|
selectOrMultiSelectEntity,
|
|
|
|
setView,
|
2023-10-20 06:17:23 -04:00
|
|
|
type,
|
|
|
|
chooseView,
|
2023-10-12 04:38:31 -04:00
|
|
|
]
|
2021-03-10 04:11:43 -05:00
|
|
|
)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2021-03-10 04:11:43 -05:00
|
|
|
const handleClick = useCallback(
|
|
|
|
ev => {
|
2021-05-04 07:36:22 -04:00
|
|
|
handleEvent(ev)
|
2023-10-12 04:38:31 -04:00
|
|
|
if (!ev.ctrlKey && !ev.metaKey) {
|
|
|
|
setContextMenuCoords(null)
|
|
|
|
}
|
2021-03-10 04:11:43 -05:00
|
|
|
},
|
2023-10-12 04:38:31 -04:00
|
|
|
[handleEvent, setContextMenuCoords]
|
2021-03-10 04:11:43 -05:00
|
|
|
)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2021-03-10 04:11:43 -05:00
|
|
|
const handleKeyPress = useCallback(
|
|
|
|
ev => {
|
|
|
|
if (ev.key === 'Enter' || ev.key === ' ') {
|
2021-05-04 07:36:22 -04:00
|
|
|
handleEvent(ev)
|
2021-03-10 04:11:43 -05:00
|
|
|
}
|
|
|
|
},
|
2021-05-04 07:36:22 -04:00
|
|
|
[handleEvent]
|
2021-03-10 04:11:43 -05:00
|
|
|
)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2021-03-10 04:11:43 -05:00
|
|
|
const handleContextMenu = useCallback(
|
|
|
|
ev => {
|
|
|
|
// make sure the right-clicked entity gets selected
|
2021-05-04 07:36:22 -04:00
|
|
|
if (!selectedEntityIds.has(id)) {
|
|
|
|
handleEvent(ev)
|
|
|
|
}
|
2021-03-10 04:11:43 -05:00
|
|
|
},
|
2021-05-04 07:36:22 -04:00
|
|
|
[id, handleEvent, selectedEntityIds]
|
2021-03-10 04:11:43 -05:00
|
|
|
)
|
|
|
|
|
2023-08-14 07:38:09 -04:00
|
|
|
const isVisuallySelected =
|
2023-08-18 05:26:22 -04:00
|
|
|
!isRootFolderSelected && isSelected && view !== 'pdf'
|
2021-03-10 04:11:43 -05:00
|
|
|
const props = useMemo(
|
|
|
|
() => ({
|
2022-03-01 04:14:41 -05:00
|
|
|
className: classNames({ selected: isVisuallySelected }),
|
|
|
|
'aria-selected': isVisuallySelected,
|
2020-11-26 09:22:30 -05:00
|
|
|
onClick: handleClick,
|
|
|
|
onContextMenu: handleContextMenu,
|
2021-04-27 03:52:58 -04:00
|
|
|
onKeyPress: handleKeyPress,
|
2021-03-10 04:11:43 -05:00
|
|
|
}),
|
2022-03-01 04:14:41 -05:00
|
|
|
[handleClick, handleContextMenu, handleKeyPress, isVisuallySelected]
|
2021-03-10 04:11:43 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
return { isSelected, props }
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function useFileTreeSelectable() {
|
2021-05-04 07:36:22 -04:00
|
|
|
const context = useContext(FileTreeSelectableContext)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2021-05-04 07:36:22 -04:00
|
|
|
if (!context) {
|
|
|
|
throw new Error(
|
|
|
|
`useFileTreeSelectable is only available inside FileTreeSelectableProvider`
|
|
|
|
)
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
2021-05-04 07:36:22 -04:00
|
|
|
|
|
|
|
return context
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|