1
- import { Box } from '@chakra-ui/react' ;
1
+ import { Box , Button , HStack , Spacer , VStack } from '@chakra-ui/react' ;
2
2
import React from 'react' ;
3
- import { useLocation } from 'react-router' ;
3
+ import { useLocation , useNavigate } from 'react-router' ;
4
4
import PythonClass from '../model/PythonClass' ;
5
5
import PythonFunction from '../model/PythonFunction' ;
6
6
import PythonModule from '../model/PythonModule' ;
@@ -10,34 +10,164 @@ import ClassView from './ClassView';
10
10
import FunctionView from './FunctionView' ;
11
11
import ModuleView from './ModuleView' ;
12
12
import ParameterView from './ParameterView' ;
13
+ import { expandParentsInTreeView } from '../packageDataSlice' ;
14
+ import { useAppDispatch , useAppSelector } from '../../../app/hooks' ;
15
+ import PythonDeclaration from '../model/PythonDeclaration' ;
16
+ import AbstractPythonFilter from '../model/filters/AbstractPythonFilter' ;
17
+ import { AnnotationsState , selectAnnotations } from '../../annotations/annotationSlice' ;
13
18
14
19
interface SelectionViewProps {
15
20
pythonPackage : PythonPackage ;
21
+ pythonFilter : AbstractPythonFilter ;
16
22
}
17
23
18
- const SelectionView : React . FC < SelectionViewProps > = function ( {
19
- pythonPackage,
20
- } ) {
21
- const declaration = pythonPackage . getByRelativePath (
22
- useLocation ( ) . pathname . split ( '/' ) . splice ( 2 ) ,
23
- ) ;
24
+ const SelectionView : React . FC < SelectionViewProps > = function ( { pythonPackage, pythonFilter } ) {
25
+ const dispatch = useAppDispatch ( ) ;
26
+ const navigate = useNavigate ( ) ;
27
+
28
+ const declaration = pythonPackage . getByRelativePath ( useLocation ( ) . pathname . split ( '/' ) . splice ( 2 ) ) ;
29
+ const annotations = useAppSelector ( selectAnnotations ) ;
30
+
31
+ if ( ! declaration ) {
32
+ return < > </ > ;
33
+ }
24
34
25
35
return (
26
- < Box padding = { 4 } >
27
- { declaration instanceof PythonFunction && (
28
- < FunctionView pythonFunction = { declaration } />
29
- ) }
30
- { declaration instanceof PythonClass && (
31
- < ClassView pythonClass = { declaration } />
32
- ) }
33
- { declaration instanceof PythonModule && (
34
- < ModuleView pythonModule = { declaration } />
35
- ) }
36
- { declaration instanceof PythonParameter && (
37
- < ParameterView pythonParameter = { declaration } />
38
- ) }
39
- </ Box >
36
+ < VStack h = "100%" >
37
+ < Box w = "100%" flexGrow = { 1 } overflowY = "scroll" >
38
+ < Box padding = { 4 } >
39
+ { declaration instanceof PythonFunction && < FunctionView pythonFunction = { declaration } /> }
40
+ { declaration instanceof PythonClass && < ClassView pythonClass = { declaration } /> }
41
+ { declaration instanceof PythonModule && < ModuleView pythonModule = { declaration } /> }
42
+ { declaration instanceof PythonParameter && < ParameterView pythonParameter = { declaration } /> }
43
+ </ Box >
44
+ </ Box >
45
+
46
+ < Spacer />
47
+
48
+ < HStack borderTop = { 1 } layerStyle = "subtleBorder" padding = "0.5em 1em" w = "100%" >
49
+ < Button
50
+ onClick = { ( ) => {
51
+ let navStr = getPreviousElementPath ( declaration , pythonFilter , annotations ) ;
52
+ if ( navStr != null ) {
53
+ //navigate to element
54
+ navigate ( `/${ navStr } ` ) ;
55
+
56
+ //update tree selection
57
+ const parents = getParents ( navStr , pythonPackage ) ;
58
+ dispatch ( expandParentsInTreeView ( parents ) ) ;
59
+ }
60
+ } }
61
+ >
62
+ Previous
63
+ </ Button >
64
+ < Button
65
+ onClick = { ( ) => {
66
+ let navStr = getNextElementPath ( declaration , pythonFilter , annotations ) ;
67
+ if ( navStr != null ) {
68
+ //navigate to element
69
+ navigate ( `/${ navStr } ` ) ;
70
+
71
+ //update tree selection
72
+ const parents = getParents ( navStr , pythonPackage ) ;
73
+ dispatch ( expandParentsInTreeView ( parents ) ) ;
74
+ }
75
+ } }
76
+ >
77
+ Next
78
+ </ Button >
79
+ </ HStack >
80
+ </ VStack >
40
81
) ;
41
82
} ;
42
83
84
+ const getNextElementPath = function (
85
+ current : PythonDeclaration ,
86
+ filter : AbstractPythonFilter ,
87
+ annotations : AnnotationsState ,
88
+ ) : string | null {
89
+ const nextElement = getNextElementInTree ( current ) ;
90
+ if ( nextElement != null ) {
91
+ if ( filter . shouldKeepDeclaration ( nextElement , annotations ) ) {
92
+ return nextElement . pathAsString ( ) ;
93
+ }
94
+ return getNextElementPath ( nextElement , filter , annotations ) ;
95
+ }
96
+ return null ;
97
+ } ;
98
+
99
+ const getNextElementInTree = function ( current : PythonDeclaration ) : PythonDeclaration | null {
100
+ if ( current . children ( ) . length > 0 ) {
101
+ return current . children ( ) [ 0 ] ;
102
+ } else if ( current . parent ( ) != null ) {
103
+ return getNextFromParentInTree ( current ) ;
104
+ }
105
+ return null ;
106
+ } ;
107
+
108
+ const getNextFromParentInTree = function ( current : PythonDeclaration ) : PythonDeclaration | null {
109
+ if ( current instanceof PythonPackage && current . children ( ) . length > 0 ) {
110
+ return current . children ( ) [ 0 ] ;
111
+ }
112
+ const parent = current . parent ( ) ;
113
+ if ( parent != null ) {
114
+ const index = parent . children ( ) . indexOf ( current ) ;
115
+ if ( parent . children ( ) . length > index + 1 ) {
116
+ return parent . children ( ) [ index + 1 ] ;
117
+ }
118
+ return getNextFromParentInTree ( parent ) ;
119
+ }
120
+ return null ;
121
+ } ;
122
+
123
+ const getPreviousElementPath = function (
124
+ current : PythonDeclaration ,
125
+ filter : AbstractPythonFilter ,
126
+ annotations : AnnotationsState ,
127
+ ) : string | null {
128
+ const previousElement = getPreviousElementInTree ( current ) ;
129
+ if ( previousElement != null ) {
130
+ if ( filter . shouldKeepDeclaration ( previousElement , annotations ) ) {
131
+ return previousElement . pathAsString ( ) ;
132
+ }
133
+ return getPreviousElementPath ( previousElement , filter , annotations ) ;
134
+ }
135
+ return null ;
136
+ } ;
137
+
138
+ const getPreviousElementInTree = function ( current : PythonDeclaration ) : PythonDeclaration | null {
139
+ const parent = current . parent ( ) ;
140
+ if ( parent != null ) {
141
+ const index = parent . children ( ) . indexOf ( current ) ;
142
+ if ( index > 0 ) {
143
+ return getLastElementInTree ( parent . children ( ) [ index - 1 ] ) ;
144
+ }
145
+ if ( parent instanceof PythonPackage ) {
146
+ return getLastElementInTree ( parent ) ;
147
+ }
148
+ return parent ;
149
+ }
150
+ return null ;
151
+ } ;
152
+
153
+ const getLastElementInTree = function ( current : PythonDeclaration ) : PythonDeclaration {
154
+ if ( current . children ( ) . length > 0 ) {
155
+ return getLastElementInTree ( current . children ( ) [ current . children ( ) . length - 1 ] ) ;
156
+ }
157
+ return current ;
158
+ } ;
159
+
160
+ const getParents = function ( navStr : string , filteredPythonPackage : PythonPackage ) : string [ ] {
161
+ const parents : string [ ] = [ ] ;
162
+ let currentElement = filteredPythonPackage . getByRelativePathAsString ( navStr ) ;
163
+ if ( currentElement != null ) {
164
+ currentElement = currentElement . parent ( ) ;
165
+ while ( currentElement != null ) {
166
+ parents . push ( currentElement . pathAsString ( ) ) ;
167
+ currentElement = currentElement . parent ( ) ;
168
+ }
169
+ }
170
+ return parents ;
171
+ } ;
172
+
43
173
export default SelectionView ;
0 commit comments