Skip to content

Commit

Permalink
fix(edgeless): multi parent container of node of mindmap (#8317)
Browse files Browse the repository at this point in the history
Closes: [BS-1398](https://linear.app/affine-design/issue/BS-1398/[bug]-拖出-frame-后-mindmap-依然跟随-frame),  [BS-1400](https://linear.app/affine-design/issue/BS-1400/mindmap无法移出frame)

This PR fix mindmap is moved with frame moving when the mindmap has been drag out of frame.

### What changes
- add workround to avoid multiple container in `FrameManager` for nodes of mindmap.
  - The fix can be refactored after #8239
- add frame and mindmap e2e tests
  • Loading branch information
L-Sun committed Sep 12, 2024
1 parent bb18e8b commit 51b9405
Show file tree
Hide file tree
Showing 5 changed files with 379 additions and 45 deletions.
32 changes: 26 additions & 6 deletions packages/blocks/src/root-block/edgeless/frame-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { FrameBlockModel, NoteBlockModel } from '@blocksuite/affine-model';
import type { Doc } from '@blocksuite/store';

import {
MindmapElementModel,
Overlay,
renderableInEdgeless,
} from '@blocksuite/affine-block-surface';
Expand All @@ -22,7 +23,7 @@ import type { EdgelessRootService } from '../../index.js';
import { GfxBlockModel } from './block-model.js';
import { edgelessElementsBound } from './utils/bound-utils.js';
import { isFrameBlock } from './utils/query.js';
import { getTopElements } from './utils/tree.js';
import { getAllDescendantElements, getTopElements } from './utils/tree.js';

const MIN_FRAME_WIDTH = 800;
const MIN_FRAME_HEIGHT = 640;
Expand Down Expand Up @@ -148,10 +149,15 @@ export class EdgelessFrameManager {
private _watchElementAddedOrDeleted() {
this._disposable.add(
this._rootService.surface.elementAdded.on(({ id, local }) => {
const element = this._rootService.surface.getElementById(id);
let element = this._rootService.surface.getElementById(id);
if (element && local) {
const frame = this.getFrameFromPoint(element.elementBound.center);

// 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;
Expand Down Expand Up @@ -371,11 +377,25 @@ export class EdgelessFrameManager {
}

removeParentFrame(element: BlockSuite.EdgelessModel) {
const parentFrame = this.getParentFrame(element);
if (parentFrame) {
// eslint-disable-next-line unicorn/prefer-dom-node-remove
parentFrame.removeChild(element);
// 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, ...getAllDescendantElements(element)].forEach(child => {
const parentFrame = this.getParentFrame(child);
if (!parentFrame) return;
// eslint-disable-next-line unicorn/prefer-dom-node-remove
parentFrame.removeChild(child);
});
return;
}

const parentFrame = this.getParentFrame(element);
if (!parentFrame) return;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,11 @@ export class DefaultToolController extends EdgelessToolController<DefaultTool> {

{
const frameManager = this._service.frame;
const toBeMovedTopElements = getTopElements(this._toBeMoved);
const toBeMovedTopElements = getTopElements(
this._toBeMoved.map(el =>
el.group instanceof MindmapElementModel ? el.group : el
)
);
if (this._hoveredFrame) {
frameManager.addElementsToFrame(
this._hoveredFrame,
Expand Down
Loading

0 comments on commit 51b9405

Please sign in to comment.