@@ -55,6 +55,7 @@ const log = (...args: any[]) => {
55
55
export class RegionFocusSwitcher extends Disposable {
56
56
// Currently focused region
57
57
public readonly current : Observable < Region | undefined > ;
58
+ private readonly _gristDocObs ?: Observable < GristDoc | null > ;
58
59
private _currentUpdateInitiator : 'keyboard' | MouseEvent = 'keyboard' ;
59
60
// Previously focused elements for each panel (not used for view section ids)
60
61
private _prevFocusedElements : Record < Panel , Element | null > = {
@@ -64,12 +65,25 @@ export class RegionFocusSwitcher extends Disposable {
64
65
main : null ,
65
66
} ;
66
67
67
- constructor ( private readonly _gristDocObs ?: Observable < GristDoc | null > ) {
68
+ constructor ( gristDocObs ?: Observable < GristDoc | null > ) {
68
69
super ( ) ;
69
70
this . current = Observable . create ( this , undefined ) ;
71
+ if ( gristDocObs ) {
72
+ this . _gristDocObs = gristDocObs ;
73
+ }
70
74
}
71
75
72
76
public init ( ) {
77
+ // if we have a grist doc, wait for the current grist doc to be ready before doing anything
78
+ if ( this . _gristDocObs && this . _gristDocObs . get ( ) === null ) {
79
+ this . _gristDocObs . addListener ( ( doc , prevDoc ) => {
80
+ if ( doc && prevDoc === null ) {
81
+ this . init ( ) ;
82
+ }
83
+ } ) ;
84
+ return ;
85
+ }
86
+
73
87
this . autoDispose ( commands . createGroup ( {
74
88
nextRegion : ( ) => this . _cycle ( 'next' ) ,
75
89
prevRegion : ( ) => this . _cycle ( 'prev' ) ,
@@ -83,7 +97,6 @@ export class RegionFocusSwitcher extends Disposable {
83
97
const onClick = this . _onClick . bind ( this ) ;
84
98
document . addEventListener ( 'mouseup' , onClick ) ;
85
99
this . onDispose ( ( ) => document . removeEventListener ( 'mouseup' , onClick ) ) ;
86
- this . _dirtyClassesFix ( ) ;
87
100
}
88
101
}
89
102
@@ -108,9 +121,6 @@ export class RegionFocusSwitcher extends Disposable {
108
121
public reset ( ) {
109
122
log ( 'reset' ) ;
110
123
this . focusRegion ( undefined ) ;
111
- if ( this . _gristDocObs ) {
112
- this . _dirtyClassesFix ( ) ;
113
- }
114
124
}
115
125
116
126
public panelAttrs ( id : Panel , ariaLabel : string ) {
@@ -120,6 +130,20 @@ export class RegionFocusSwitcher extends Disposable {
120
130
dom . attr ( 'role' , 'region' ) ,
121
131
dom . attr ( 'aria-label' , ariaLabel ) ,
122
132
dom . attr ( ATTRS . regionId , id ) ,
133
+ dom . cls ( 'clipboard_group_focus' , use => {
134
+ const gristDoc = this . _gristDocObs ? use ( this . _gristDocObs ) : null ;
135
+ if ( ! gristDoc ) {
136
+ return true ;
137
+ }
138
+ return ! hasViewLayout ( gristDoc ) ;
139
+ } ) ,
140
+ dom . cls ( 'clipboard_forbid_focus' , use => {
141
+ const gristDoc = this . _gristDocObs ? use ( this . _gristDocObs ) : null ;
142
+ if ( ! gristDoc ) {
143
+ return false ;
144
+ }
145
+ return hasViewLayout ( gristDoc ) ;
146
+ } ) ,
123
147
dom . cls ( `${ cssFocusedPanel . className } -focused` , use => {
124
148
const current = use ( this . current ) ;
125
149
return this . _currentUpdateInitiator === 'keyboard' && current ?. type === 'panel' && current . id === id ;
@@ -141,32 +165,6 @@ export class RegionFocusSwitcher extends Disposable {
141
165
}
142
166
}
143
167
144
- // @TODO : fix this. This is dirty code to see if what I want to do works.
145
- // My issue is when starting the regionFocusSwitcher, the gristDoc is not yet ready.
146
- // I need to see how to correctly wait for this and be sure there are view layout sections or not.
147
- private _dirtyClassesFix ( tries = 0 ) : any {
148
- if ( tries > 20 ) {
149
- return ;
150
- }
151
- const main = document . querySelector ( `[${ ATTRS . regionId } ="main"]` ) ;
152
- if ( ! main ) {
153
- return setTimeout ( ( ) => this . _dirtyClassesFix ( tries + 1 ) , 100 ) ;
154
- }
155
- const hasGristDoc = ! ! this . _gristDocObs ;
156
- const gristDoc = this . _getGristDoc ( ) ;
157
- if ( hasGristDoc && ! gristDoc ) {
158
- return setTimeout ( ( ) => this . _dirtyClassesFix ( tries + 1 ) , 100 ) ;
159
- }
160
- if ( hasGristDoc ) {
161
- main ?. classList . remove ( 'clipboard_group_focus' ) ;
162
- main ?. classList . add ( 'clipboard_forbid_focus' ) ;
163
- } else {
164
- main ?. classList . remove ( 'clipboard_forbid_focus' ) ;
165
- main ?. classList . add ( 'clipboard_group_focus' ) ;
166
- }
167
- log ( 'dirtyClassesFix, main classes:' , main ?. className ) ;
168
- }
169
-
170
168
/**
171
169
* When clicking on a grist doc page:
172
170
* - if necessary, make it easier to tab through things inside panels by "unfocusing" the view section,
@@ -519,11 +517,11 @@ const findRegionIndex = (regions: Region[], region: Region | undefined) => {
519
517
* This can be false if we are on a grist-doc special page, or if the grist doc is not yet ready.
520
518
*/
521
519
const hasViewLayout = ( doc : GristDoc | null ) => {
522
- return doc
520
+ return ! ! ( doc
523
521
&& ! doc . viewModel . isDisposed ( )
524
522
&& doc . viewLayout
525
523
&& ! doc . viewLayout . isDisposed ( )
526
- && doc . viewLayout . layout ;
524
+ && doc . viewLayout . layout ) ;
527
525
} ;
528
526
529
527
0 commit comments