Skip to content

Commit

Permalink
fix: issue
Browse files Browse the repository at this point in the history
  • Loading branch information
doouding committed Dec 9, 2024
1 parent d9e506d commit e57e4df
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
26 changes: 26 additions & 0 deletions packages/affine/block-surface/src/renderer/canvas-renderer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
GridManager,
LayerManager,
SurfaceBlockModel,
Viewport,
} from '@blocksuite/block-std/gfx';
import type { IBound } from '@blocksuite/global/utils';
Expand Down Expand Up @@ -37,6 +38,7 @@ type RendererOptions = {
onStackingCanvasCreated?: (canvas: HTMLCanvasElement) => void;
elementRenderers: Record<string, ElementRenderer>;
gridManager: GridManager;
surfaceModel: SurfaceBlockModel;
};

export class CanvasRenderer {
Expand Down Expand Up @@ -90,6 +92,8 @@ export class CanvasRenderer {
if (options.enableStackingCanvas) {
this._initStackingCanvas(options.onStackingCanvasCreated);
}

this._watchSurface(options.surfaceModel);
}

/**
Expand Down Expand Up @@ -321,6 +325,28 @@ export class CanvasRenderer {
this.refresh();
}

private _watchSurface(surfaceModel: SurfaceBlockModel) {
const slots = [
'elementAdded',
'elementRemoved',
'localElementAdded',
'localElementDeleted',
'localElementUpdated',
] as const;

slots.forEach(slotName => {
this._disposables.add(surfaceModel[slotName].on(() => this.refresh()));
});

this._disposables.add(
surfaceModel.elementUpdated.on(payload => {
// ignore externalXYWH update cause it's updated by the renderer
if (payload.props['externalXYWH']) return;
this.refresh();
})
);
}

addOverlay(overlay: Overlay) {
overlay.setRenderer(this);
this._overlays.add(overlay);
Expand Down
18 changes: 1 addition & 17 deletions packages/affine/block-surface/src/surface-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,25 +187,9 @@ export class SurfaceBlockComponent extends BlockComponent<
canvas.className = 'indexable-canvas';
},
elementRenderers: this._edgelessService.elementRenderers,
surfaceModel: this.model,
});

this._disposables.add(
this.model.elementUpdated.on(payload => {
// ignore externalXYWH update cause it's updated by the renderer
if (payload.props['externalXYWH']) return;
this._renderer.refresh();
})
);
this._disposables.add(
this.model.elementAdded.on(() => {
this._renderer.refresh();
})
);
this._disposables.add(
this.model.elementRemoved.on(() => {
this._renderer.refresh();
})
);
this._disposables.add(() => {
this._renderer.dispose();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ export class MindmapSurfaceBlock extends BlockComponent<SurfaceBlockModel> {
private _setupRenderer() {
this._disposables.add(
this.model.elementUpdated.on(() => {
this.renderer?.refresh();
this.mindmapService.center();
})
);
Expand Down Expand Up @@ -122,7 +121,9 @@ export class MindmapSurfaceBlock extends BlockComponent<SurfaceBlockModel> {
),
},
elementRenderers,
surfaceModel: this.model,
});
this._disposables.add(this.renderer);
}

override firstUpdated(_changedProperties: Map<PropertyKey, unknown>): void {
Expand Down

0 comments on commit e57e4df

Please sign in to comment.