file-tree-selectable.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import React, { createContext, useContext, useReducer, useEffect } from 'react'
  2. import PropTypes from 'prop-types'
  3. import classNames from 'classnames'
  4. import { findInTree } from '../util/find-in-tree'
  5. import { useFileTreeMutable } from './file-tree-mutable'
  6. const FileTreeSelectableContext = createContext(new Set())
  7. const ACTION_TYPES = {
  8. SELECT: 'SELECT',
  9. MULTI_SELECT: 'MULTI_SELECT',
  10. UNSELECT: 'UNSELECT'
  11. }
  12. function fileTreeSelectableReadWriteReducer(selectedEntityIds, action) {
  13. switch (action.type) {
  14. case ACTION_TYPES.SELECT: {
  15. // reset selection
  16. return new Set([action.id])
  17. }
  18. case ACTION_TYPES.MULTI_SELECT: {
  19. const selectedEntityIdsCopy = new Set(selectedEntityIds)
  20. if (selectedEntityIdsCopy.has(action.id)) {
  21. // entity already selected
  22. if (selectedEntityIdsCopy.size > 1) {
  23. // entity already multi-selected; remove from set
  24. selectedEntityIdsCopy.delete(action.id)
  25. }
  26. } else {
  27. // entity not selected: add to set
  28. selectedEntityIdsCopy.add(action.id)
  29. }
  30. return selectedEntityIdsCopy
  31. }
  32. case ACTION_TYPES.UNSELECT: {
  33. const selectedEntityIdsCopy = new Set(selectedEntityIds)
  34. selectedEntityIdsCopy.delete(action.id)
  35. return selectedEntityIdsCopy
  36. }
  37. default:
  38. throw new Error(`Unknown selectable action type: ${action.type}`)
  39. }
  40. }
  41. function fileTreeSelectableReadOnlyReducer(selectedEntityIds, action) {
  42. switch (action.type) {
  43. case ACTION_TYPES.SELECT:
  44. return new Set([action.id])
  45. case ACTION_TYPES.MULTI_SELECT:
  46. case ACTION_TYPES.UNSELECT:
  47. return selectedEntityIds
  48. default:
  49. throw new Error(`Unknown selectable action type: ${action.type}`)
  50. }
  51. }
  52. export function FileTreeSelectableProvider({
  53. hasWritePermissions,
  54. initialSelectedEntityId = null,
  55. onSelect,
  56. children
  57. }) {
  58. const [selectedEntityIds, dispatch] = useReducer(
  59. hasWritePermissions
  60. ? fileTreeSelectableReadWriteReducer
  61. : fileTreeSelectableReadOnlyReducer,
  62. initialSelectedEntityId ? new Set([initialSelectedEntityId]) : new Set()
  63. )
  64. const { fileTreeData } = useFileTreeMutable()
  65. // calls `onSelect` on entities selection
  66. useEffect(
  67. () => {
  68. const selectedEntities = Array.from(selectedEntityIds).map(id =>
  69. findInTree(fileTreeData, id)
  70. )
  71. onSelect(selectedEntities)
  72. },
  73. [fileTreeData, selectedEntityIds]
  74. )
  75. useEffect(() => {
  76. // listen for `editor.openDoc` and selected that doc
  77. function handleOpenDoc(ev) {
  78. dispatch({ type: ACTION_TYPES.SELECT, id: ev.detail })
  79. }
  80. window.addEventListener('editor.openDoc', handleOpenDoc)
  81. return () => window.removeEventListener('editor.openDoc', handleOpenDoc)
  82. }, [])
  83. return (
  84. <FileTreeSelectableContext.Provider value={{ selectedEntityIds, dispatch }}>
  85. {children}
  86. </FileTreeSelectableContext.Provider>
  87. )
  88. }
  89. FileTreeSelectableProvider.propTypes = {
  90. hasWritePermissions: PropTypes.bool.isRequired,
  91. initialSelectedEntityId: PropTypes.string,
  92. onSelect: PropTypes.func.isRequired,
  93. children: PropTypes.oneOfType([
  94. PropTypes.arrayOf(PropTypes.node),
  95. PropTypes.node
  96. ]).isRequired
  97. }
  98. export function useSelectableEntity(id) {
  99. const { selectedEntityIds, dispatch } = useContext(FileTreeSelectableContext)
  100. const isSelected = selectedEntityIds.has(id)
  101. function selectOrMultiSelectEntity(ev) {
  102. const isMultiSelect = ev.ctrlKey || ev.metaKey
  103. const actionType = isMultiSelect
  104. ? ACTION_TYPES.MULTI_SELECT
  105. : ACTION_TYPES.SELECT
  106. dispatch({ type: actionType, id })
  107. }
  108. function handleClick(ev) {
  109. selectOrMultiSelectEntity(ev)
  110. }
  111. function handleKeyPress(ev) {
  112. if (ev.key === 'Enter' || ev.key === ' ') {
  113. selectOrMultiSelectEntity(ev)
  114. }
  115. }
  116. function handleContextMenu(ev) {
  117. // make sure the right-clicked entity gets selected
  118. if (!selectedEntityIds.has(id)) selectOrMultiSelectEntity(ev)
  119. }
  120. return {
  121. isSelected,
  122. props: {
  123. className: classNames({ selected: isSelected }),
  124. 'aria-selected': isSelected,
  125. onClick: handleClick,
  126. onContextMenu: handleContextMenu,
  127. onKeyPress: handleKeyPress
  128. }
  129. }
  130. }
  131. export function useFileTreeSelectable() {
  132. const { selectedEntityIds, dispatch } = useContext(FileTreeSelectableContext)
  133. function unselect(id) {
  134. dispatch({ type: ACTION_TYPES.UNSELECT, id })
  135. }
  136. return {
  137. selectedEntityIds,
  138. unselect
  139. }
  140. }