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'
|
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
|
|
|
|
return new Set([action.id])
|
|
|
|
}
|
|
|
|
|
|
|
|
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}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 10:46:46 -05: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
|
|
|
|
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
|
|
|
}
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
export function useSelectableEntity(id, isFile) {
|
|
|
|
const { view, setView } = useLayoutContext(layoutContextPropTypes)
|
2021-05-04 07:36:22 -04:00
|
|
|
const { selectedEntityIds, selectOrMultiSelectEntity } = useContext(
|
|
|
|
FileTreeSelectableContext
|
|
|
|
)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
const isSelected = selectedEntityIds.has(id)
|
|
|
|
|
2021-05-04 07:36:22 -04:00
|
|
|
const handleEvent = useCallback(
|
2021-03-10 04:11:43 -05:00
|
|
|
ev => {
|
2021-05-04 07:36:22 -04:00
|
|
|
selectOrMultiSelectEntity(id, ev.ctrlKey || ev.metaKey)
|
2022-03-01 04:14:41 -05:00
|
|
|
setView(isFile ? 'file' : 'editor')
|
2021-03-10 04:11:43 -05:00
|
|
|
},
|
2022-03-01 04:14:41 -05:00
|
|
|
[id, selectOrMultiSelectEntity, setView, isFile]
|
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)
|
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 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
|
|
|
)
|
|
|
|
|
2022-03-01 04:14:41 -05:00
|
|
|
const isVisuallySelected = 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
|
|
|
}
|
|
|
|
|
2022-03-01 04:14:41 -05:00
|
|
|
const layoutContextPropTypes = {
|
|
|
|
view: PropTypes.string,
|
|
|
|
setView: PropTypes.func.isRequired,
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|