Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

fix(editor): get block props (#11807)

fundonc0ff567a2aca
diff --git a/blocksuite/affine/blocks/attachment/src/configs/toolbar.ts b/blocksuite/affine/blocks/attachment/src/configs/toolbar.ts
index 10482dafd9..1e414d57bb 100644
--- a/blocksuite/affine/blocks/attachment/src/configs/toolbar.ts
+++ b/blocksuite/affine/blocks/attachment/src/configs/toolbar.ts
@@ -15,6 +15,7 @@ import {
type ToolbarModuleConfig,
ToolbarModuleExtension,
} from '@blocksuite/affine-shared/services';
+import { getBlockProps } from '@blocksuite/affine-shared/utils';
import { Bound } from '@blocksuite/global/gfx';
import {
CaptionIcon,
@@ -35,7 +36,6 @@ import { keyed } from 'lit/directives/keyed.js';
import { AttachmentBlockComponent } from '../attachment-block';
import { RenameModal } from '../components/rename-model';
import { AttachmentEmbedProvider } from '../embed';
-import { cloneAttachmentProperties } from '../utils';
const trackBaseProps = {
category: 'attachment',
@@ -219,7 +219,7 @@ const builtinToolbarConfig = {
ctx.store.addSiblingBlocks(model, [
{
flavour: model.flavour,
- ...cloneAttachmentProperties(model),
+ ...getBlockProps(model),
},
]);
},
diff --git a/blocksuite/affine/blocks/attachment/src/utils.ts b/blocksuite/affine/blocks/attachment/src/utils.ts
index 266ae67a99..9152f3b16c 100644
--- a/blocksuite/affine/blocks/attachment/src/utils.ts
+++ b/blocksuite/affine/blocks/attachment/src/utils.ts
@@ -3,7 +3,6 @@ import type {
AttachmentBlockModel,
AttachmentBlockProps,
} from '@blocksuite/affine-model';
-import { defaultAttachmentProps } from '@blocksuite/affine-model';
import {
EMBED_CARD_HEIGHT,
EMBED_CARD_WIDTH,
@@ -20,18 +19,6 @@ import type { BlockModel } from '@blocksuite/store';
import type { AttachmentBlockComponent } from './attachment-block.js';
-export function cloneAttachmentProperties(model: AttachmentBlockModel) {
- const clonedProps = {} as AttachmentBlockProps;
- for (const cur in defaultAttachmentProps) {
- const key = cur as keyof AttachmentBlockProps;
- // @ts-expect-error it's safe because we just cloned the props simply
- clonedProps[key] = model[
- key
- ] as AttachmentBlockProps[keyof AttachmentBlockProps];
- }
- return clonedProps;
-}
-
const attachmentUploads = new Set<string>();
export function setAttachmentUploading(blockId: string) {
attachmentUploads.add(blockId);
diff --git a/blocksuite/affine/shared/src/utils/model/block-props.ts b/blocksuite/affine/shared/src/utils/model/block-props.ts
index 51b8d13a7d..55f68cfc6c 100644
--- a/blocksuite/affine/shared/src/utils/model/block-props.ts
+++ b/blocksuite/affine/shared/src/utils/model/block-props.ts
@@ -1,8 +1,6 @@
import type { BlockModel } from '@blocksuite/store';
export function getBlockProps(model: BlockModel): Record<string, unknown> {
- const keys = model.keys as (keyof typeof model)[];
- const values = keys.map(key => model[key]);
- const blockProps = Object.fromEntries(keys.map((key, i) => [key, values[i]]));
- return blockProps;
+ const keys = model.keys as (keyof typeof model.props)[];
+ return Object.fromEntries(keys.map(key => [key, model.props[key]]));
}
diff --git a/tests/affine-local/e2e/blocksuite/toolbar.spec.ts b/tests/affine-local/e2e/blocksuite/toolbar.spec.ts
index 6d447f1f85..8bc0f077a1 100644
--- a/tests/affine-local/e2e/blocksuite/toolbar.spec.ts
+++ b/tests/affine-local/e2e/blocksuite/toolbar.spec.ts
@@ -370,3 +370,35 @@ test('should clear selection when switching doc mode', async ({ page }) => {
await expect(toolbar).toBeHidden();
});
+
+test.describe('Toolbar More Actions', () => {
+ test('should duplicate block', async ({ page }) => {
+ await page.keyboard.press('Enter');
+
+ await importImage(page, 'large-image.png');
+ const images = page.locator('affine-page-image');
+
+ const firstImage = images.first();
+ const firstImageUrl = await firstImage.locator('img').getAttribute('src');
+
+ await firstImage.hover();
+
+ const toolbar = locateToolbar(page);
+ const moreMenu = toolbar.getByLabel('More menu');
+ await moreMenu.click();
+
+ const duplicateButton = toolbar.getByTestId('duplicate');
+ await duplicateButton.click();
+
+ await expect(images).toHaveCount(2);
+
+ const secondImage = images.nth(1);
+ const secondImageUrl = await secondImage.locator('img').getAttribute('src');
+
+ expect(firstImageUrl).not.toBeNull();
+ expect(firstImageUrl!.startsWith('blob:')).toBe(true);
+
+ expect(secondImageUrl).not.toBeNull();
+ expect(secondImageUrl!.startsWith('blob:')).toBe(true);
+ });
+});