Skip to content

Commit

Permalink
fix(edgeless): mindmap layout for existing model (#8426)
Browse files Browse the repository at this point in the history
  • Loading branch information
doodlewind authored Sep 21, 2024
1 parent ef57070 commit a15c539
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 35 deletions.
8 changes: 4 additions & 4 deletions packages/affine/block-surface/src/element-model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import {
BrushElementModel,
ConnectorElementModel,
GroupElementModel,
MindmapElementModel,
ShapeElementModel,
TextElementModel,
} from '@blocksuite/affine-model';

import { LayoutableMindmapElementModel } from '../utils/mindmap/utils.js';
import { SurfaceElementModel } from './base.js';

export const elementsCtorMap = {
Expand All @@ -15,14 +15,14 @@ export const elementsCtorMap = {
shape: ShapeElementModel,
brush: BrushElementModel,
text: TextElementModel,
mindmap: MindmapElementModel,
mindmap: LayoutableMindmapElementModel,
};

export {
BrushElementModel,
ConnectorElementModel,
GroupElementModel,
MindmapElementModel,
LayoutableMindmapElementModel,
ShapeElementModel,
SurfaceElementModel,
TextElementModel,
Expand All @@ -43,7 +43,7 @@ export type ElementModelMap = {
['connector']: ConnectorElementModel;
['text']: TextElementModel;
['group']: GroupElementModel;
['mindmap']: MindmapElementModel;
['mindmap']: LayoutableMindmapElementModel;
};

export function isCanvasElementType(type: string): type is CanvasElementType {
Expand Down
2 changes: 0 additions & 2 deletions packages/affine/block-surface/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ import {
addTree,
createFromTree,
detachMindmap,
handleLayout,
hideTargetConnector,
moveMindMapSubtree,
showMergeIndicator,
Expand Down Expand Up @@ -144,7 +143,6 @@ export const MindmapUtils = {
addTree,
createFromTree,
detachMindmap,
handleLayout,
hideTargetConnector,
moveMindMapSubtree,
showMergeIndicator,
Expand Down
12 changes: 6 additions & 6 deletions packages/affine/block-surface/src/utils/mindmap/layout.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type {
MindmapElementModel,
MindmapNode,
MindmapRoot,
} from '@blocksuite/affine-model';
import type { SerializedXYWH } from '@blocksuite/global/utils';

import { LayoutType } from '@blocksuite/affine-model';
import {
LayoutType,
type MindmapElementModel,
type MindmapNode,
type MindmapRoot,
} from '@blocksuite/affine-model';
import { Bound } from '@blocksuite/global/utils';

export const NODE_VERTICAL_SPACING = 45;
Expand Down
11 changes: 8 additions & 3 deletions packages/affine/block-surface/src/utils/mindmap/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
applyNodeStyle,
LayoutType,
type MindmapElementModel,
MindmapElementModel,
type MindmapNode,
type MindmapRoot,
type MindmapStyle,
Expand All @@ -21,6 +21,12 @@ import { ConnectorPathGenerator } from '../../managers/connector-manager.js';
import { fitContent } from '../../renderer/elements/shape/utils.js';
import { layout } from './layout.js';

export class LayoutableMindmapElementModel extends MindmapElementModel {
override layout() {
handleLayout(this, this.tree, true, this.layoutType);
}
}

export function getHoveredArea(
target: ShapeElementModel,
position: [number, number],
Expand Down Expand Up @@ -410,8 +416,7 @@ export function createFromTree(
style,
});
const mindmap = surface.getElementById(mindmapId) as MindmapElementModel;
mindmap.setLayoutHandler(handleLayout);
mindmap.layout();
handleLayout(mindmap, mindmap.tree, true, mindmap.layoutType);

return mindmap;
}
Expand Down
4 changes: 0 additions & 4 deletions packages/affine/model/src/elements/mindmap/mindmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -693,10 +693,6 @@ export class MindmapElementModel extends GfxGroupLikeElementModel<MindmapElement
return result as SerializedMindmapElement;
}

setLayoutHandler(handler: typeof this._layoutHandler) {
this._layoutHandler = handler;
}

stashTree(node: MindmapNode | string) {
const mindNode = typeof node === 'string' ? this.getNode(node) : node;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type { IBound } from '@blocksuite/global/utils';
import {
type ElementRenderer,
elementRenderers,
MindmapUtils,
type Overlay,
type SurfaceBlockModel,
type SurfaceContext,
Expand Down Expand Up @@ -267,12 +266,6 @@ export class EdgelessRootService extends RootService implements SurfaceContext {
index: props.index ?? this.generateIndex(type),
};
const id = this._surface.addElement(nProps);

if (type === 'mindmap') {
const model = this._surface.getElementById(id) as MindmapElementModel;
model.setLayoutHandler(MindmapUtils.handleLayout);
model.layout();
}
return id;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { SurfaceBlockModel } from '@blocksuite/affine-block-surface';

import { MindmapUtils } from '@blocksuite/affine-block-surface';
import {
MindmapStyleFour,
MindmapStyleOne,
Expand Down Expand Up @@ -164,11 +163,7 @@ export class MiniMindmapPreview extends WithDisposable(LitElement) {
children: mindmapNode,
style: this.mindmapStyle ?? MindmapStyle.FOUR,
});
const mindmap = this.surface.getElementById(
this.mindmapId
) as MindmapElementModel;
mindmap.setLayoutHandler(MindmapUtils.handleLayout);
mindmap.layout();
this.surface.getElementById(this.mindmapId) as MindmapElementModel;

const centerPosition = this._mindmap?.tree.element.xywh;

Expand Down
3 changes: 0 additions & 3 deletions packages/presets/src/__tests__/edgeless/group.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
type EdgelessRootBlockComponent,
type GroupElementModel,
LayoutType,
MindmapUtils,
NoteDisplayMode,
} from '@blocksuite/blocks';
import { DocCollection } from '@blocksuite/store';
Expand Down Expand Up @@ -277,8 +276,6 @@ describe('mindmap', () => {
});
const mindmap = () =>
service.getElementById(mindmapId) as MindmapElementModel;
mindmap().setLayoutHandler(MindmapUtils.handleLayout);
mindmap().layout();

doc.captureSync();
await wait();
Expand Down

0 comments on commit a15c539

Please sign in to comment.