Skip to content

Commit

Permalink
refactor: update group test utils using container interface
Browse files Browse the repository at this point in the history
  • Loading branch information
L-Sun committed Oct 14, 2024
1 parent af66304 commit 07f62a5
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 120 deletions.
22 changes: 22 additions & 0 deletions packages/framework/block-std/src/gfx/surface/surface-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import type { Boxed, Y } from '@blocksuite/store';
import { type Constructor, Slot } from '@blocksuite/global/utils';
import { BlockModel, DocCollection, nanoid } from '@blocksuite/store';

import type { GfxModel } from '../gfx-block-model.js';

import { TreeManager } from '../tree.js';
import {
type GfxContainerElement,
isGfxContainerElm,
} from './container-element.js';
import { createDecoratorState } from './decorators/common.js';
import { initializeObservers, initializeWatchers } from './decorators/index.js';
import {
Expand Down Expand Up @@ -451,6 +457,22 @@ export class SurfaceBlockModel extends BlockModel<SurfaceBlockProps> {
return this._elementModels.has(id);
}

isContainer(element: GfxModel): element is GfxModel & GfxContainerElement;
isContainer(id: string): boolean;
isContainer(element: string | GfxModel): boolean {
if (typeof element === 'string') {
const el = this.getElementById(element);
if (el) return isGfxContainerElm(el);

const blockModel = this.doc.getBlock(element)?.model;
if (blockModel) return isGfxContainerElm(blockModel);

return false;
} else {
return isGfxContainerElm(element);
}
}

isInMindmap(id: string) {
const group = this.getGroup(id);

Expand Down
16 changes: 8 additions & 8 deletions packages/framework/block-std/src/gfx/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class TreeManager {

private _watched = false;

constructor(readonly surface: SurfaceBlockModel) {}
constructor(private readonly _surface: SurfaceBlockModel) {}

getContainer(elementId: string): (GfxModel & GfxContainerElement) | null {
const container = this._elementToContainer.get(elementId);
Expand Down Expand Up @@ -82,7 +82,7 @@ export class TreeManager {

// Graphic Block Elements

const { doc } = this.surface;
const { doc } = this._surface;
const elements = doc
.getBlocks()
.filter(model => model instanceof GfxBlockElementModel) as GfxModel[];
Expand Down Expand Up @@ -132,7 +132,7 @@ export class TreeManager {

// Canvas Elements

this.surface.elementModels.forEach(el => {
this._surface.elementModels.forEach(el => {
if (isGfxContainerElm(el)) {
// we use `childIds` here because some blocks in doc may not be ready
el.childIds.forEach(childId => {
Expand All @@ -142,21 +142,21 @@ export class TreeManager {
});

disposable.add(
this.surface.elementAdded.on(({ id }) => {
const element = this.surface.getElementById(id);
this._surface.elementAdded.on(({ id }) => {
const element = this._surface.getElementById(id);
element && onGfxModelAdded(element);
})
);

disposable.add(
this.surface.elementRemoved.on(({ model }) => {
this._surface.elementRemoved.on(({ model }) => {
onGfxModelDeleted(model);
})
);

disposable.add(
this.surface.elementUpdated.on(({ id, oldValues }) => {
const element = this.surface.getElementById(id);
this._surface.elementUpdated.on(({ id, oldValues }) => {
const element = this._surface.getElementById(id);
if (!isGfxContainerElm(element)) return;

// Since the implement of GfxContainer may be different,
Expand Down
27 changes: 15 additions & 12 deletions tests/edgeless/group/clipboard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ import {
decreaseZoomLevel,
edgelessCommonSetup,
getAllSortedIds,
getFirstGroupId,
getFirstContainerId,
pasteByKeyboard,
selectAllByKeyboard,
Shape,
toViewCoord,
triggerComponentToolbarAction,
waitNextFrame,
} from '../../utils/actions/index.js';
import { assertGroupChildren, assertGroupIds } from '../../utils/asserts.js';
import {
assertContainerChildren,
assertContainerIds,
} from '../../utils/asserts.js';
import { test } from '../../utils/playwright.js';

test.describe('clipboard', () => {
Expand All @@ -27,23 +30,23 @@ test.describe('clipboard', () => {
await createShapeElement(page, [100, 0], [200, 100], Shape.Square);
await selectAllByKeyboard(page);
await triggerComponentToolbarAction(page, 'addGroup');
const originGroupId = await getFirstGroupId(page);
const originGroupId = await getFirstContainerId(page);

await copyByKeyboard(page);
await waitNextFrame(page, 100);
const move = await toViewCoord(page, [100, -50]);
await page.mouse.click(move[0], move[1]);
await waitNextFrame(page, 1000);
await pasteByKeyboard(page, false);
const copyedGroupId = await getFirstGroupId(page, [originGroupId]);
const copyedGroupId = await getFirstContainerId(page, [originGroupId]);

await assertGroupIds(page, {
await assertContainerIds(page, {
[originGroupId]: 2,
[copyedGroupId]: 2,
null: 2,
});
await assertGroupChildren(page, originGroupId, 2);
await assertGroupChildren(page, copyedGroupId, 2);
await assertContainerChildren(page, originGroupId, 2);
await assertContainerChildren(page, copyedGroupId, 2);
});

test('copy and paste group with connector', async ({ page }) => {
Expand All @@ -53,23 +56,23 @@ test.describe('clipboard', () => {
await createConnectorElement(page, [100, 50], [200, 50]);
await selectAllByKeyboard(page);
await triggerComponentToolbarAction(page, 'addGroup');
const originGroupId = await getFirstGroupId(page);
const originGroupId = await getFirstContainerId(page);

await copyByKeyboard(page);
await waitNextFrame(page, 100);
const move = await toViewCoord(page, [100, -50]);
await page.mouse.click(move[0], move[1]);
await waitNextFrame(page, 1000);
await pasteByKeyboard(page, false);
const copyedGroupId = await getFirstGroupId(page, [originGroupId]);
const copyedGroupId = await getFirstContainerId(page, [originGroupId]);

await assertGroupIds(page, {
await assertContainerIds(page, {
[originGroupId]: 3,
[copyedGroupId]: 3,
null: 2,
});
await assertGroupChildren(page, originGroupId, 3);
await assertGroupChildren(page, copyedGroupId, 3);
await assertContainerChildren(page, originGroupId, 3);
await assertContainerChildren(page, copyedGroupId, 3);
});
});

Expand Down
44 changes: 22 additions & 22 deletions tests/edgeless/group/group-and-ungroup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
clickView,
createShapeElement,
edgelessCommonSetup,
getFirstGroupId,
getFirstContainerId,
getIds,
redoByKeyboard,
selectAllByKeyboard,
Expand All @@ -16,9 +16,9 @@ import {
undoByKeyboard,
} from '../../utils/actions/index.js';
import {
assertGroupChildren,
assertGroupChildrenIds,
assertGroupIds,
assertContainerChildIds,
assertContainerChildren,
assertContainerIds,
assertSelectedBound,
} from '../../utils/asserts.js';
import { test } from '../../utils/playwright.js';
Expand All @@ -43,66 +43,66 @@ test.describe('group and ungroup in group', () => {
)[0];
await selectAllByKeyboard(page);
await triggerComponentToolbarAction(page, 'addGroup');
outterGroupId = await getFirstGroupId(page);
outterGroupId = await getFirstContainerId(page);
});

test('group in group', async ({ page }) => {
await clickView(page, [50, 50]);
await shiftClickView(page, [150, 50]);
await captureHistory(page);
await triggerComponentToolbarAction(page, 'addGroup');
const groupId = await getFirstGroupId(page, [outterGroupId]);
const groupId = await getFirstContainerId(page, [outterGroupId]);
await assertSelectedBound(page, [0, 0, 200, 100]);
await assertGroupIds(page, {
await assertContainerIds(page, {
[groupId]: 2,
[outterGroupId]: 2,
null: 1,
});
await assertGroupChildren(page, groupId, 2);
await assertGroupChildren(page, outterGroupId, 2);
await assertContainerChildren(page, groupId, 2);
await assertContainerChildren(page, outterGroupId, 2);

// undo the creation
await undoByKeyboard(page);
await assertGroupIds(page, {
await assertContainerIds(page, {
[outterGroupId]: 3,
null: 1,
});
await assertGroupChildren(page, outterGroupId, 3);
await assertContainerChildren(page, outterGroupId, 3);

// redo the creation
await redoByKeyboard(page);
await assertGroupIds(page, {
await assertContainerIds(page, {
[groupId]: 2,
[outterGroupId]: 2,
null: 1,
});
await assertGroupChildren(page, groupId, 2);
await assertGroupChildren(page, outterGroupId, 2);
await assertContainerChildren(page, groupId, 2);
await assertContainerChildren(page, outterGroupId, 2);
});

test('ungroup in group', async ({ page }) => {
await clickView(page, [50, 50]);
await shiftClickView(page, [150, 50]);
await triggerComponentToolbarAction(page, 'addGroup');
await captureHistory(page);
const groupId = await getFirstGroupId(page, [outterGroupId]);
const groupId = await getFirstContainerId(page, [outterGroupId]);
await triggerComponentToolbarAction(page, 'ungroup');
await assertGroupIds(page, { [outterGroupId]: 3, null: 1 });
await assertGroupChildrenIds(
await assertContainerIds(page, { [outterGroupId]: 3, null: 1 });
await assertContainerChildIds(
page,
toIdCountMap(await getIds(page, true)),
outterGroupId
);

// undo, group should in group again
await undoByKeyboard(page);
await assertGroupIds(page, {
await assertContainerIds(page, {
[outterGroupId]: 2,
[groupId]: 2,
null: 1,
});
await assertGroupChildrenIds(page, toIdCountMap(initShapes), groupId);
await assertGroupChildrenIds(
await assertContainerChildIds(page, toIdCountMap(initShapes), groupId);
await assertContainerChildIds(
page,
{
[groupId]: 1,
Expand All @@ -113,8 +113,8 @@ test.describe('group and ungroup in group', () => {

// redo, group should be ungroup again
await redoByKeyboard(page);
await assertGroupIds(page, { [outterGroupId]: 3, null: 1 });
await assertGroupChildrenIds(
await assertContainerIds(page, { [outterGroupId]: 3, null: 1 });
await assertContainerChildIds(
page,
toIdCountMap(await getIds(page, true)),
outterGroupId
Expand Down
Loading

0 comments on commit 07f62a5

Please sign in to comment.