From c0ff567a2aca6a1af01e4a8a4ec3e1a3d72c1b13 Mon Sep 17 00:00:00 2001 From: fundon Date: Fri, 18 Apr 2025 10:59:31 +0000 Subject: [PATCH] fix(editor): get block props (#11807) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes: [BS-3184](https://linear.app/affine-design/issue/BS-3184/duplicate-图片,一直在loading) --- .../blocks/attachment/src/configs/toolbar.ts | 4 +-- .../affine/blocks/attachment/src/utils.ts | 13 -------- .../shared/src/utils/model/block-props.ts | 6 ++-- .../e2e/blocksuite/toolbar.spec.ts | 32 +++++++++++++++++++ 4 files changed, 36 insertions(+), 19 deletions(-) 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(); 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 { - 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); + }); +});