fix(editor): cut and paste surface-ref to same doc should remain surface-ref (#11639)

Close [BS-3107](https://linear.app/affine-design/issue/BS-3107/剪切-surface-ref-block-会变成指向当前文档的link-card)
This commit is contained in:
donteatfriedrice
2025-04-12 01:56:24 +00:00
parent afdc40b510
commit 4da00eba0d
2 changed files with 79 additions and 46 deletions
@@ -11,12 +11,16 @@ export const surfaceRefToEmbed =
} }
}); });
slots.beforeImport.subscribe(payload => { slots.beforeImport.subscribe(payload => {
// only handle surface-ref block snapshot
if ( if (
pageId && payload.type !== 'block' ||
payload.type === 'block' && payload.snapshot.flavour !== 'affine:surface-ref'
payload.snapshot.flavour === 'affine:surface-ref' && )
!std.store.hasBlock(payload.snapshot.id) return;
) {
// turn into embed-linked-doc if the current doc is different from the pageId of the surface-ref block
const isNotSameDoc = pageId !== std.store.doc.id;
if (pageId && isNotSameDoc) {
// The blockId of the original surface-ref block // The blockId of the original surface-ref block
const blockId = payload.snapshot.id; const blockId = payload.snapshot.id;
payload.snapshot.id = std.workspace.idGenerator(); payload.snapshot.id = std.workspace.idGenerator();
@@ -9,6 +9,7 @@ import {
} from '@affine-test/kit/utils/editor'; } from '@affine-test/kit/utils/editor';
import { import {
copyByKeyboard, copyByKeyboard,
cutByKeyboard,
pasteByKeyboard, pasteByKeyboard,
pressEnter, pressEnter,
} from '@affine-test/kit/utils/keyboard'; } from '@affine-test/kit/utils/keyboard';
@@ -174,18 +175,17 @@ test.describe('paste in multiple blocks text selection', () => {
}); });
}); });
test('paste surface-ref block to another doc as embed-linked-doc block', async ({ test.describe('surface-ref block', () => {
page, async function setupSurfaceRefBlock(page: Page) {
}) => {
await clickEdgelessModeButton(page); await clickEdgelessModeButton(page);
const container = locateEditorContainer(page); const container = locateEditorContainer(page);
await container.click(); await container.click();
// add a shape // add a shape
await page.keyboard.press('s'); await page.keyboard.press('s');
// click to add a shape
await container.click({ position: { x: 100, y: 300 } }); await container.click({ position: { x: 100, y: 300 } });
await page.waitForTimeout(50); await page.waitForTimeout(50);
// add a frame // add a frame
await page.keyboard.press('f'); await page.keyboard.press('f');
await page.waitForTimeout(50); await page.waitForTimeout(50);
@@ -196,7 +196,6 @@ test('paste surface-ref block to another doc as embed-linked-doc block', async (
await page.waitForTimeout(50); await page.waitForTimeout(50);
const toolbar = page.locator('affine-toolbar-widget editor-toolbar'); const toolbar = page.locator('affine-toolbar-widget editor-toolbar');
const insertIntoPageButton = toolbar.getByLabel('Insert into Page'); const insertIntoPageButton = toolbar.getByLabel('Insert into Page');
await insertIntoPageButton.click(); await insertIntoPageButton.click();
@@ -204,6 +203,14 @@ test('paste surface-ref block to another doc as embed-linked-doc block', async (
await waitForEditorLoad(page); await waitForEditorLoad(page);
await container.click(); await container.click();
return { container };
}
test('paste surface-ref block to another doc as embed-linked-doc block', async ({
page,
}) => {
await setupSurfaceRefBlock(page);
// copy surface-ref block // copy surface-ref block
const surfaceRefBlock = page.locator('affine-surface-ref'); const surfaceRefBlock = page.locator('affine-surface-ref');
await surfaceRefBlock.click(); await surfaceRefBlock.click();
@@ -226,6 +233,28 @@ test('paste surface-ref block to another doc as embed-linked-doc block', async (
await expect(embedLinkedDocBlockTitle).toHaveText('Clipboard Test'); await expect(embedLinkedDocBlockTitle).toHaveText('Clipboard Test');
}); });
test('cut and paste surface-ref block to same doc should remain surface-ref block', async ({
page,
}) => {
const { container } = await setupSurfaceRefBlock(page);
// cut surface-ref block
const surfaceRefBlock = page.locator('affine-surface-ref');
await surfaceRefBlock.click();
await page.waitForSelector('affine-surface-ref .focused');
await cutByKeyboard(page);
// focus on the editor
await container.click();
// paste the surface-ref block
await pasteByKeyboard(page);
await page.waitForTimeout(50);
await expect(surfaceRefBlock).toHaveCount(1);
await expect(surfaceRefBlock).toBeVisible();
});
});
test.describe('paste to code block', () => { test.describe('paste to code block', () => {
test('should replace the selected text when pasting plain text', async ({ test('should replace the selected text when pasting plain text', async ({
page, page,