Skip to content

Commit 6209f70

Browse files
authored
refactor(superdoc): guard nullable toolbar accesses (SD-2867 phase B) (#3069)
* refactor(superdoc): guard nullable toolbar accesses (SD-2867 phase B) * fix(superdoc): clarify toolbar guard rationale comments (SD-2867 phase B)
1 parent 73afecf commit 6209f70

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

packages/superdoc/src/core/SuperDoc.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,14 @@ export class SuperDoc extends EventEmitter {
11891189
this.toolbar = new SuperToolbar(config);
11901190

11911191
this.toolbar.on('exception', this.config.onException);
1192-
this.once('editorCreate', () => this.toolbar.updateToolbarState());
1192+
// `this.toolbar` infers as `SuperToolbar | null` from the field's
1193+
// first assignment in `#addToolbar` (the `null` placeholder a few
1194+
// lines up). The closure registers after the SuperToolbar instance
1195+
// is in place and reads `this.toolbar` at emission time, so under
1196+
// normal flow it will see the live instance; the optional chain
1197+
// is here to satisfy TS's typedef and to no-op if a future
1198+
// `destroy()` ever clears the field.
1199+
this.once('editorCreate', () => this.toolbar?.updateToolbarState());
11931200
}
11941201

11951202
/**
@@ -1421,7 +1428,14 @@ export class SuperDoc extends EventEmitter {
14211428
}
14221429

14231430
#setModeViewing() {
1424-
this.toolbar.activeEditor = null;
1431+
// `this.toolbar` infers as `SuperToolbar | null` from the field's
1432+
// first assignment in `#addToolbar` (the `null` placeholder before
1433+
// the SuperToolbar is constructed). `#addToolbar` runs once during
1434+
// init and unconditionally installs the instance, so by the time
1435+
// mode changes are reachable the toolbar is non-null. The guard
1436+
// keeps TS satisfied and stays a no-op if a future destroy/teardown
1437+
// ever clears the field.
1438+
if (this.toolbar) this.toolbar.activeEditor = null;
14251439

14261440
const commentsVisible = this.config.comments?.visible === true;
14271441
const trackChangesVisible = this.config.trackChanges?.visible === true;

0 commit comments

Comments
 (0)