feat(editor): add widget in viewUpdated slot (#10452)

This commit is contained in:
Saul-Mirone
2025-02-26 13:24:32 +00:00
parent 589622043c
commit 1c5e360d7e
4 changed files with 33 additions and 9 deletions

View File

@@ -423,8 +423,9 @@ export class SurfaceRefBlockComponent extends BlockComponent<SurfaceRefBlockMode
override mounted() { override mounted() {
const disposable = this.std.view.viewUpdated.on(payload => { const disposable = this.std.view.viewUpdated.on(payload => {
if (payload.type !== 'block') return;
if ( if (
payload.type === 'add' && payload.method === 'add' &&
matchModels(payload.view.model, [RootBlockModel]) matchModels(payload.view.model, [RootBlockModel])
) { ) {
disposable.dispose(); disposable.dispose();

View File

@@ -145,6 +145,8 @@ export class PreviewHelper {
} }
this.std.view.viewUpdated.on(payload => { this.std.view.viewUpdated.on(payload => {
if (payload.type !== 'block') return;
if (payload.view.model.flavour === 'affine:page') { if (payload.view.model.flavour === 'affine:page') {
const gfx = this.std.get(GfxControllerIdentifier); const gfx = this.std.get(GfxControllerIdentifier);

View File

@@ -1625,10 +1625,13 @@ export class DragEventWatcher {
disposables.add( disposables.add(
std.view.viewUpdated.on(payload => { std.view.viewUpdated.on(payload => {
if (payload.type === 'add') { if (payload.type !== 'block') {
return;
}
if (payload.method === 'add') {
this._makeDropTarget(payload.view); this._makeDropTarget(payload.view);
} else if ( } else if (
payload.type === 'delete' && payload.method === 'delete' &&
this.dropTargetCleanUps.has(payload.id) this.dropTargetCleanUps.has(payload.id)
) { ) {
this.dropTargetCleanUps.get(payload.id)!.forEach(clean => clean()); this.dropTargetCleanUps.get(payload.id)!.forEach(clean => clean());

View File

@@ -3,16 +3,20 @@ import { Slot } from '@blocksuite/global/utils';
import { LifeCycleWatcher } from '../extension/index.js'; import { LifeCycleWatcher } from '../extension/index.js';
import type { BlockComponent, WidgetComponent } from './element/index.js'; import type { BlockComponent, WidgetComponent } from './element/index.js';
type ViewUpdatePayload = type ViewUpdateMethod = 'delete' | 'add';
export type ViewUpdatePayload =
| { | {
id: string; id: string;
type: 'delete'; method: ViewUpdateMethod;
type: 'block';
view: BlockComponent; view: BlockComponent;
} }
| { | {
id: string; id: string;
type: 'add'; method: ViewUpdateMethod;
view: BlockComponent; type: 'widget';
view: WidgetComponent;
}; };
export class ViewStore extends LifeCycleWatcher { export class ViewStore extends LifeCycleWatcher {
@@ -42,7 +46,8 @@ export class ViewStore extends LifeCycleWatcher {
this._blockMap.delete(node.model.id); this._blockMap.delete(node.model.id);
this.viewUpdated.emit({ this.viewUpdated.emit({
id: node.model.id, id: node.model.id,
type: 'delete', method: 'delete',
type: 'block',
view: node, view: node,
}); });
}; };
@@ -51,6 +56,12 @@ export class ViewStore extends LifeCycleWatcher {
const id = node.dataset.widgetId as string; const id = node.dataset.widgetId as string;
const widgetIndex = `${node.model.id}|${id}`; const widgetIndex = `${node.model.id}|${id}`;
this._widgetMap.delete(widgetIndex); this._widgetMap.delete(widgetIndex);
this.viewUpdated.emit({
id: node.model.id,
method: 'delete',
type: 'widget',
view: node,
});
}; };
getBlock = (id: string): BlockComponent | null => { getBlock = (id: string): BlockComponent | null => {
@@ -72,7 +83,8 @@ export class ViewStore extends LifeCycleWatcher {
this._blockMap.set(node.model.id, node); this._blockMap.set(node.model.id, node);
this.viewUpdated.emit({ this.viewUpdated.emit({
id: node.model.id, id: node.model.id,
type: 'add', method: 'add',
type: 'block',
view: node, view: node,
}); });
}; };
@@ -81,6 +93,12 @@ export class ViewStore extends LifeCycleWatcher {
const id = node.dataset.widgetId as string; const id = node.dataset.widgetId as string;
const widgetIndex = `${node.model.id}|${id}`; const widgetIndex = `${node.model.id}|${id}`;
this._widgetMap.set(widgetIndex, node); this._widgetMap.set(widgetIndex, node);
this.viewUpdated.emit({
id: node.model.id,
method: 'add',
type: 'widget',
view: node,
});
}; };
walkThrough = ( walkThrough = (