@@ -36,41 +36,60 @@ function setupPageInjector(app: Application) {
36
36
function toolbar ( toolbar : ( props : PageEvent < Reflection > ) => JSX . Element ) {
37
37
return function ( props : PageEvent < Reflection > ) {
38
38
const origin = toolbar ( props ) ;
39
- const found = findByProp ( origin , 'id' , 'tsd-search' ) ;
40
- found . children . push ( < select id = 'version-select' class = 'title loading' /> ) ;
39
+ // find the <a> element with class 'title' and inject a <select> element after it
40
+ findByProp ( origin , 'class' , 'title' , 'a' , ( elem , indices ) => {
41
+ const index = indices . pop ( ) ;
42
+ const children = indices . reduce ( ( arr , idx ) => arr [ idx ] , elem . children ) as JSX . Children [ ] ;
43
+ children . splice (
44
+ index + 1 ,
45
+ 0 ,
46
+ < select id = 'version-select' class = 'title loading' /> ,
47
+ ) ;
48
+ return true ; // no need to continue searching
49
+ } ) ;
41
50
return origin ;
42
51
}
43
52
}
44
53
45
- function findByProp ( element : JSX . Element , name : string , value : string ) : JSX . Element | undefined {
46
- function findInElement ( element : JSX . Element ) {
47
- if ( element . props ?. [ name ] === value ) {
48
- return element ;
54
+ function findByProp (
55
+ root : JSX . Element ,
56
+ name : string ,
57
+ value : string ,
58
+ tagName ?: string ,
59
+ cb ?: ( elem : JSX . Element , indices : number [ ] ) => void ) {
60
+ function findInElement ( parent : JSX . Element , element : JSX . Element , indices : number [ ] ) {
61
+ if ( ( tagName == null || element . tag === tagName ) && element . props ?. [ name ] === value ) {
62
+ if ( cb ) {
63
+ return cb ( parent , indices . slice ( ) ) ;
64
+ }
49
65
}
50
- return findInChildren ( element . children ) ;
66
+ return findInChildren ( element , [ ] , element . children ) ;
51
67
}
52
- function findInChildren ( children : JSX . Children [ ] ) {
53
- for ( const child of children ) {
68
+ function findInChildren ( parent : JSX . Element , indices : number [ ] , children : JSX . Children [ ] ) {
69
+ const newIndices = indices . slice ( ) ;
70
+ newIndices . push ( 0 ) ;
71
+ for ( let i = 0 ; i < children . length ; i ++ ) {
72
+ const child = children [ i ] ;
73
+ newIndices [ newIndices . length - 1 ] = i ;
54
74
if ( child == null ) {
55
75
continue ;
56
76
}
57
77
if ( Array . isArray ( child ) ) { // child is JSX.Children[]
58
- const found = findInChildren ( child ) ;
59
- if ( found != null ) {
60
- return found ;
61
- }
78
+ if ( findInChildren ( parent , newIndices , child ) ) {
79
+ return true ;
80
+ } ;
62
81
continue ; // didn't find in the child(JSX.Children[]), try next child
63
82
}
64
83
if ( typeof child === 'object' ) { // child is JSX.Element
65
- const found = findInElement ( child ) ;
66
- if ( found != null ) {
67
- return found ;
68
- }
84
+ if ( findInElement ( parent , child , newIndices ) ) {
85
+ return true ;
86
+ } ;
69
87
}
70
88
}
71
- return undefined ; // not found
89
+ return false ; // not found
72
90
}
73
- return findInElement ( element ) ;
91
+
92
+ findInChildren ( root , [ ] , root . children ) ;
74
93
}
75
94
76
95
function setupAssets ( app : Application ) {
0 commit comments