Files
AFFiNE-Mirror/blocksuite/affine/block-surface/src/watchers/group.ts
T
2024-12-20 15:38:06 +08:00

28 lines
748 B
TypeScript

import { SurfaceGroupLikeModel } from '../element-model/base.js';
import type { SurfaceBlockModel, SurfaceMiddleware } from '../surface-model.js';
export const groupRelationWatcher: SurfaceMiddleware = (
surface: SurfaceBlockModel
) => {
const disposables = [
surface.elementUpdated
.filter(payload => payload.local)
.on(({ id, props }) => {
const element = surface.getElementById(id)!;
// remove the group if it has no children
if (
element instanceof SurfaceGroupLikeModel &&
props['childIds'] &&
element.childIds.length === 0
) {
surface.deleteElement(id);
}
}),
];
return () => {
disposables.forEach(d => d.dispose());
};
};