Skip to content

Commit

Permalink
refactor(blocks): simplify frame manager implementation (#8507)
Browse files Browse the repository at this point in the history
This PR removes redundant code related to the container-child element relationship, which is now maintained by the new `TreeManager` after #8239 .
  • Loading branch information
L-Sun committed Oct 14, 2024
1 parent 1e7573e commit c427926
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 54 deletions.
2 changes: 1 addition & 1 deletion packages/affine/model/src/blocks/frame/frame-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class FrameBlockModel
});
}

addChildren(elements: (BlockSuite.EdgelessModel | string)[]): void {
addChildren(elements: (GfxModel | string)[]): void {
elements = [...new Set(elements)];

const newChildren: Record<string, boolean> = {};
Expand Down
59 changes: 7 additions & 52 deletions packages/blocks/src/root-block/edgeless/frame-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import type { SurfaceBlockModel } from '@blocksuite/affine-block-surface';
import type { Doc } from '@blocksuite/store';

import { Overlay } from '@blocksuite/affine-block-surface';
import {
GroupElementModel,
MindmapElementModel,
} from '@blocksuite/affine-model';
import { MindmapElementModel } from '@blocksuite/affine-model';
import {
getTopElements,
type GfxModel,
Expand Down Expand Up @@ -188,36 +185,19 @@ export class EdgelessFrameManager {
const frame = this.getFrameFromPoint(element.elementBound.center);

// if the container created with a frame, skip it.
// |<-- Container |< -- frame -->| Container -->|
if (isGfxContainerElm(element) && frame && element.hasChild(frame)) {
return;
}

// TODO(@L-Sun): refactor this in a tree manager
if (element.group instanceof MindmapElementModel) {
element = element.group;
}

// TODO(@L-Sun): refactor this in a tree manager
if (element instanceof GroupElementModel) {
if (frame && element.hasChild(frame)) return;
element.childElements.forEach(child => {
// The children of new group may already have a parent frame
this.removeParentFrame(child);
});
}

frame && this.addElementsToFrame(frame, [element]);
}
})
);

this._disposable.add(
this._rootService.surface.elementRemoved.on(({ model, local }) => {
local && this.removeParentFrame(model);
})
);

this._disposable.add(
this._rootService.doc.slots.blockUpdated.on(payload => {
if (
Expand All @@ -243,10 +223,6 @@ export class EdgelessFrameManager {
}
this.addElementsToFrame(frame, [payload.model]);
}
if (payload.type === 'delete') {
const element = this._rootService.getElementById(payload.model.id);
if (element) this.removeParentFrame(element);
}
})
);
}
Expand All @@ -260,15 +236,11 @@ export class EdgelessFrameManager {
}

elements = elements.filter(
({ id }) => id !== frame.id && !frame.childIds.includes(id)
el => el !== frame && !frame.childElements.includes(el)
);

if (elements.length === 0) return;

elements.forEach(element => {
frame.addChild(element);
});

frame.addChildren(elements);
}

Expand Down Expand Up @@ -396,26 +368,10 @@ export class EdgelessFrameManager {
});
}

removeParentFrame(element: GfxModel) {
// TODO(@L-Sun): refactor this with tree manager
// since current implementation may cause one element has multiple parent containers
// this is a workaround to avoid this
if (element.group instanceof MindmapElementModel) element = element.group;
if (element instanceof MindmapElementModel) {
[element, ...element.descendantElements].forEach(child => {
const parentFrame = this.getParentFrame(child);
if (!parentFrame) return;
// eslint-disable-next-line unicorn/prefer-dom-node-remove
parentFrame.removeChild(child);
});
return;
}

removeFromParentFrame(element: GfxModel) {
const parentFrame = this.getParentFrame(element);
if (!parentFrame) return;

// eslint-disable-next-line unicorn/prefer-dom-node-remove
parentFrame.removeChild(element);
parentFrame?.removeChild(element);
}
}

Expand Down Expand Up @@ -443,9 +399,8 @@ export function getBlocksInFrameBound(
fullyContained: boolean = true
) {
const bound = Bound.deserialize(model.xywh);
const surfaceModel = doc.getBlockByFlavour([
'affine:surface',
]) as SurfaceBlockModel[];
const surface = model.surface;
if (!surface) return [];

return (
getNotesInFrameBound(
Expand All @@ -454,7 +409,7 @@ export function getBlocksInFrameBound(
fullyContained
) as BlockSuite.EdgelessBlockModelType[]
).concat(
surfaceModel[0].children.filter(ele => {
surface.children.filter(ele => {
if (ele.id === model.id) return;
if (ele instanceof GfxBlockModel) {
const blockBound = Bound.deserialize(ele.xywh);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ export class DefaultToolController extends EdgelessToolController<DefaultTool> {
} else {
// only apply to root nodes of trees
toBeMovedTopElements.map(element =>
frameManager.removeParentFrame(element)
frameManager.removeFromParentFrame(element)
);
}
}
Expand Down

0 comments on commit c427926

Please sign in to comment.