fix(editor): suface component can be null (#12270)

Closes: BS-3149

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

## Summary by CodeRabbit

- **Refactor**
  - Improved internal handling of surface components across various tools, resulting in safer and more consistent access patterns.
  - Enhanced code maintainability and reliability without altering any visible features or user workflows.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Saul-Mirone
2025-05-14 08:37:10 +00:00
parent 7e722957a9
commit f2164e4d70
6 changed files with 39 additions and 34 deletions

View File

@@ -47,20 +47,22 @@ export class NoteTool extends BaseTool<NoteToolOption> {
private _noteOverlay: NoteOverlay | null = null;
private get _surfaceComponent() {
return this.gfx.surfaceComponent as SurfaceBlockComponent | null;
}
// Ensure clear overlay before adding a new note
private _clearOverlay() {
this._noteOverlay = this._disposeOverlay(this._noteOverlay);
this._draggingNoteOverlay = this._disposeOverlay(this._draggingNoteOverlay);
(this.gfx.surfaceComponent as SurfaceBlockComponent).refresh();
this._surfaceComponent?.refresh();
}
private _disposeOverlay(overlay: NoteOverlay | null) {
if (!overlay) return null;
overlay.dispose();
(
this.gfx.surfaceComponent as SurfaceBlockComponent
)?.renderer.removeOverlay(overlay);
this._surfaceComponent?.renderer.removeOverlay(overlay);
return null;
}
@@ -69,7 +71,7 @@ export class NoteTool extends BaseTool<NoteToolOption> {
if (!this._noteOverlay) return;
this._noteOverlay.globalAlpha = 0;
(this.gfx.surfaceComponent as SurfaceBlockComponent)?.refresh();
this._surfaceComponent?.refresh();
}
private _resize(shift = false) {
@@ -102,7 +104,7 @@ export class NoteTool extends BaseTool<NoteToolOption> {
if (!this._noteOverlay) return;
this._noteOverlay.x = x;
this._noteOverlay.y = y;
(this.gfx.surfaceComponent as SurfaceBlockComponent).refresh();
this._surfaceComponent?.refresh();
}
override activate() {
@@ -111,9 +113,7 @@ export class NoteTool extends BaseTool<NoteToolOption> {
const background = attributes.background;
this._noteOverlay = new NoteOverlay(this.gfx, background);
this._noteOverlay.text = this.activatedOption.tip;
(this.gfx.surfaceComponent as SurfaceBlockComponent).renderer.addOverlay(
this._noteOverlay
);
this._surfaceComponent?.renderer.addOverlay(this._noteOverlay);
}
override click(e: PointerEventState): void {
@@ -176,9 +176,7 @@ export class NoteTool extends BaseTool<NoteToolOption> {
this.std.get(EditPropsStore).lastProps$.value['affine:note'];
const background = attributes.background;
this._draggingNoteOverlay = new DraggingNoteOverlay(this.gfx, background);
(this.gfx.surfaceComponent as SurfaceBlockComponent).renderer.addOverlay(
this._draggingNoteOverlay
);
this._surfaceComponent?.renderer.addOverlay(this._draggingNoteOverlay);
}
override mounted() {

View File

@@ -1,5 +1,5 @@
import {
type SurfaceBlockComponent,
getSurfaceComponent,
ToolOverlay,
} from '@blocksuite/affine-block-surface';
import { type Color, DefaultTheme } from '@blocksuite/affine-model';
@@ -36,7 +36,8 @@ export class NoteOverlay extends ToolOverlay {
if (this.gfx.tool.currentToolName$.value !== 'affine:note') return;
const tool = this.gfx.tool.currentTool$.peek() as NoteTool;
this.text = this._getOverlayText(tool.activatedOption.tip);
(this.gfx.surfaceComponent as SurfaceBlockComponent).refresh();
const surface = getSurfaceComponent(this.gfx.std);
surface?.refresh();
})
);
}
@@ -139,7 +140,8 @@ export class DraggingNoteOverlay extends NoteOverlay {
this.disposables.add(
this.slots.draggingNoteUpdated.subscribe(({ xywh }) => {
[this.x, this.y, this.width, this.height] = xywh;
(this.gfx.surfaceComponent as SurfaceBlockComponent).refresh();
const surface = getSurfaceComponent(this.gfx.std);
surface?.refresh();
})
);
}