|
| 1 | +import { Button, HStack, Spacer } from '@chakra-ui/react'; |
| 2 | +import React from 'react'; |
| 3 | +import { UsernameInput } from './UsernameInput'; |
| 4 | +import { useAppDispatch, useAppSelector } from '../../app/hooks'; |
| 5 | +import { PythonPackage } from '../packageData/model/PythonPackage'; |
| 6 | +import { selectFilteredPythonPackage, selectFlatSortedDeclarationList } from '../packageData/apiSlice'; |
| 7 | +import { Optional } from '../../common/util/types'; |
| 8 | +import { AbstractPythonFilter } from '../packageData/model/filters/AbstractPythonFilter'; |
| 9 | +import { AnnotationStore, redo, selectAnnotationStore, undo } from '../annotations/annotationSlice'; |
| 10 | +import { selectFilter, setAllCollapsedInTreeView, setAllExpandedInTreeView } from '../ui/uiSlice'; |
| 11 | +import { PythonDeclaration } from '../packageData/model/PythonDeclaration'; |
| 12 | +import { selectUsages } from '../usages/usageSlice'; |
| 13 | +import { UsageCountStore } from '../usages/model/UsageCountStore'; |
| 14 | +import { useNavigate } from 'react-router'; |
| 15 | + |
| 16 | +interface ActionBarProps { |
| 17 | + declaration: Optional<PythonDeclaration>; |
| 18 | +} |
| 19 | + |
| 20 | +export const ActionBar: React.FC<ActionBarProps> = function ({ declaration }) { |
| 21 | + const dispatch = useAppDispatch(); |
| 22 | + const navigate = useNavigate(); |
| 23 | + |
| 24 | + const allDeclarations = useAppSelector(selectFlatSortedDeclarationList); |
| 25 | + const pythonPackage = useAppSelector(selectFilteredPythonPackage); |
| 26 | + const pythonFilter = useAppSelector(selectFilter); |
| 27 | + const annotations = useAppSelector(selectAnnotationStore); |
| 28 | + const usages = useAppSelector(selectUsages); |
| 29 | + |
| 30 | + return ( |
| 31 | + <HStack borderTop={1} layerStyle="subtleBorder" padding="0.5em 1em" marginTop={0} w="100%"> |
| 32 | + <HStack> |
| 33 | + <Button |
| 34 | + accessKey="p" |
| 35 | + disabled={!declaration} |
| 36 | + onClick={() => { |
| 37 | + let navStr = getPreviousElementPath( |
| 38 | + allDeclarations, |
| 39 | + declaration!, |
| 40 | + pythonFilter, |
| 41 | + annotations, |
| 42 | + usages, |
| 43 | + ); |
| 44 | + if (navStr !== null) { |
| 45 | + //navigate to element |
| 46 | + navigate(`/${navStr}`); |
| 47 | + |
| 48 | + //update tree selection |
| 49 | + const parents = getAncestors(navStr, pythonPackage); |
| 50 | + dispatch(setAllExpandedInTreeView(parents)); |
| 51 | + } |
| 52 | + }} |
| 53 | + > |
| 54 | + Previous |
| 55 | + </Button> |
| 56 | + <Button |
| 57 | + accessKey="n" |
| 58 | + disabled={!declaration} |
| 59 | + onClick={() => { |
| 60 | + let navStr = getNextElementPath( |
| 61 | + allDeclarations, |
| 62 | + declaration!, |
| 63 | + pythonFilter, |
| 64 | + annotations, |
| 65 | + usages, |
| 66 | + ); |
| 67 | + if (navStr !== null) { |
| 68 | + //navigate to element |
| 69 | + navigate(`/${navStr}`); |
| 70 | + |
| 71 | + //update tree selection |
| 72 | + const parents = getAncestors(navStr, pythonPackage); |
| 73 | + dispatch(setAllExpandedInTreeView(parents)); |
| 74 | + } |
| 75 | + }} |
| 76 | + > |
| 77 | + Next |
| 78 | + </Button> |
| 79 | + |
| 80 | + <Button |
| 81 | + accessKey="u" |
| 82 | + onClick={() => { |
| 83 | + const parent = declaration?.parent(); |
| 84 | + if (parent && !(parent instanceof PythonPackage)) { |
| 85 | + navigate(`/${parent.id}`); |
| 86 | + } |
| 87 | + }} |
| 88 | + > |
| 89 | + Go to Parent |
| 90 | + </Button> |
| 91 | + |
| 92 | + <Button |
| 93 | + onClick={() => { |
| 94 | + dispatch(setAllExpandedInTreeView(getDescendantsOrSelf(pythonPackage))); |
| 95 | + }} |
| 96 | + > |
| 97 | + Expand All |
| 98 | + </Button> |
| 99 | + <Button |
| 100 | + onClick={() => { |
| 101 | + dispatch(setAllCollapsedInTreeView(getDescendantsOrSelf(pythonPackage))); |
| 102 | + }} |
| 103 | + > |
| 104 | + Collapse All |
| 105 | + </Button> |
| 106 | + |
| 107 | + <Button |
| 108 | + disabled={!declaration} |
| 109 | + onClick={() => { |
| 110 | + dispatch(setAllExpandedInTreeView(getDescendantsOrSelf(declaration!))); |
| 111 | + }} |
| 112 | + > |
| 113 | + Expand Selected |
| 114 | + </Button> |
| 115 | + <Button |
| 116 | + disabled={!declaration} |
| 117 | + onClick={() => { |
| 118 | + dispatch(setAllCollapsedInTreeView(getDescendantsOrSelf(declaration!))); |
| 119 | + }} |
| 120 | + > |
| 121 | + Collapse Selected |
| 122 | + </Button> |
| 123 | + <Button |
| 124 | + accessKey="z" |
| 125 | + disabled={!declaration} |
| 126 | + onClick={() => { |
| 127 | + dispatch(undo()); |
| 128 | + }} |
| 129 | + > |
| 130 | + Undo |
| 131 | + </Button> |
| 132 | + <Button |
| 133 | + accessKey="y" |
| 134 | + disabled={!declaration} |
| 135 | + onClick={() => { |
| 136 | + dispatch(redo()); |
| 137 | + }} |
| 138 | + > |
| 139 | + Redo |
| 140 | + </Button> |
| 141 | + </HStack> |
| 142 | + |
| 143 | + <Spacer /> |
| 144 | + |
| 145 | + <HStack> |
| 146 | + <UsernameInput /> |
| 147 | + </HStack> |
| 148 | + </HStack> |
| 149 | + ); |
| 150 | +}; |
| 151 | + |
| 152 | +const getNextElementPath = function ( |
| 153 | + declarations: PythonDeclaration[], |
| 154 | + start: PythonDeclaration, |
| 155 | + filter: AbstractPythonFilter, |
| 156 | + annotations: AnnotationStore, |
| 157 | + usages: UsageCountStore, |
| 158 | +): string { |
| 159 | + let current = getNextElementInTree(declarations, start); |
| 160 | + while (current !== start) { |
| 161 | + if (filter.shouldKeepDeclaration(current, annotations, usages)) { |
| 162 | + return current.id; |
| 163 | + } |
| 164 | + current = getNextElementInTree(declarations, current); |
| 165 | + } |
| 166 | + return start.id; |
| 167 | +}; |
| 168 | + |
| 169 | +const getNextElementInTree = function ( |
| 170 | + declarations: PythonDeclaration[], |
| 171 | + current: PythonDeclaration, |
| 172 | +): PythonDeclaration { |
| 173 | + if (declarations.length === 0) { |
| 174 | + return current; |
| 175 | + } |
| 176 | + |
| 177 | + const index = declarations.findIndex((it) => it.id === current.id); |
| 178 | + const nextIndex = (index + 1) % declarations.length; |
| 179 | + return declarations[nextIndex]; |
| 180 | +}; |
| 181 | + |
| 182 | +const getPreviousElementPath = function ( |
| 183 | + declarations: PythonDeclaration[], |
| 184 | + start: PythonDeclaration, |
| 185 | + filter: AbstractPythonFilter, |
| 186 | + annotations: AnnotationStore, |
| 187 | + usages: UsageCountStore, |
| 188 | +): string | null { |
| 189 | + let current = getPreviousElementInTree(declarations, start); |
| 190 | + while (current !== start && current !== null) { |
| 191 | + if (filter.shouldKeepDeclaration(current, annotations, usages)) { |
| 192 | + return current.id; |
| 193 | + } |
| 194 | + current = getPreviousElementInTree(declarations, current); |
| 195 | + } |
| 196 | + return null; |
| 197 | +}; |
| 198 | + |
| 199 | +const getPreviousElementInTree = function ( |
| 200 | + declarations: PythonDeclaration[], |
| 201 | + current: PythonDeclaration, |
| 202 | +): PythonDeclaration { |
| 203 | + if (declarations.length === 0) { |
| 204 | + return current; |
| 205 | + } |
| 206 | + |
| 207 | + const index = declarations.findIndex((it) => it.id === current.id); |
| 208 | + const previousIndex = (index - 1 + declarations.length) % declarations.length; |
| 209 | + return declarations[previousIndex]; |
| 210 | +}; |
| 211 | + |
| 212 | +const getAncestors = function (navStr: string, filteredPythonPackage: PythonPackage): string[] { |
| 213 | + const ancestors: string[] = []; |
| 214 | + |
| 215 | + let currentElement = filteredPythonPackage.getDeclarationById(navStr); |
| 216 | + if (currentElement) { |
| 217 | + currentElement = currentElement.parent(); |
| 218 | + while (currentElement) { |
| 219 | + ancestors.push(currentElement.id); |
| 220 | + currentElement = currentElement.parent(); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + return ancestors; |
| 225 | +}; |
| 226 | + |
| 227 | +const getDescendantsOrSelf = function (current: PythonDeclaration): string[] { |
| 228 | + return [...current.descendantsOrSelf()].map((descendant) => descendant.id); |
| 229 | +}; |
0 commit comments