Skip to content

Commit 3eda7a8

Browse files
committed
(kb) cleaner way to toggle panel clipboard group/ignore classes
we now check if the passed doc observer is actually ready before doing anything
1 parent 3e61c2b commit 3eda7a8

File tree

1 file changed

+31
-33
lines changed

1 file changed

+31
-33
lines changed

app/client/components/RegionFocusSwitcher.ts

+31-33
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const log = (...args: any[]) => {
5555
export class RegionFocusSwitcher extends Disposable {
5656
// Currently focused region
5757
public readonly current: Observable<Region | undefined>;
58+
private readonly _gristDocObs?: Observable<GristDoc | null>;
5859
private _currentUpdateInitiator: 'keyboard' | MouseEvent = 'keyboard';
5960
// Previously focused elements for each panel (not used for view section ids)
6061
private _prevFocusedElements: Record<Panel, Element | null> = {
@@ -64,12 +65,25 @@ export class RegionFocusSwitcher extends Disposable {
6465
main: null,
6566
};
6667

67-
constructor(private readonly _gristDocObs?: Observable<GristDoc | null>) {
68+
constructor(gristDocObs?: Observable<GristDoc | null>) {
6869
super();
6970
this.current = Observable.create(this, undefined);
71+
if (gristDocObs) {
72+
this._gristDocObs = gristDocObs;
73+
}
7074
}
7175

7276
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+
7387
this.autoDispose(commands.createGroup({
7488
nextRegion: () => this._cycle('next'),
7589
prevRegion: () => this._cycle('prev'),
@@ -83,7 +97,6 @@ export class RegionFocusSwitcher extends Disposable {
8397
const onClick = this._onClick.bind(this);
8498
document.addEventListener('mouseup', onClick);
8599
this.onDispose(() => document.removeEventListener('mouseup', onClick));
86-
this._dirtyClassesFix();
87100
}
88101
}
89102

@@ -108,9 +121,6 @@ export class RegionFocusSwitcher extends Disposable {
108121
public reset() {
109122
log('reset');
110123
this.focusRegion(undefined);
111-
if (this._gristDocObs) {
112-
this._dirtyClassesFix();
113-
}
114124
}
115125

116126
public panelAttrs(id: Panel, ariaLabel: string) {
@@ -120,6 +130,20 @@ export class RegionFocusSwitcher extends Disposable {
120130
dom.attr('role', 'region'),
121131
dom.attr('aria-label', ariaLabel),
122132
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+
}),
123147
dom.cls(`${cssFocusedPanel.className}-focused`, use => {
124148
const current = use(this.current);
125149
return this._currentUpdateInitiator === 'keyboard' && current?.type === 'panel' && current.id === id;
@@ -141,32 +165,6 @@ export class RegionFocusSwitcher extends Disposable {
141165
}
142166
}
143167

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-
170168
/**
171169
* When clicking on a grist doc page:
172170
* - 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) => {
519517
* This can be false if we are on a grist-doc special page, or if the grist doc is not yet ready.
520518
*/
521519
const hasViewLayout = (doc: GristDoc | null) => {
522-
return doc
520+
return !!(doc
523521
&& !doc.viewModel.isDisposed()
524522
&& doc.viewLayout
525523
&& !doc.viewLayout.isDisposed()
526-
&& doc.viewLayout.layout;
524+
&& doc.viewLayout.layout);
527525
};
528526

529527

0 commit comments

Comments
 (0)