fix(editor): should clear selection when switching doc mode (#11545)

Closes: [BS-3050](https://linear.app/affine-design/issue/BS-3050/切换模式时,清除选区)
This commit is contained in:
fundon
2025-04-10 03:10:50 +00:00
parent e7d4684531
commit bb79781dd8
5 changed files with 36 additions and 2 deletions
@@ -23,6 +23,7 @@ import {
} from '@blocksuite/affine-gfx-text'; } from '@blocksuite/affine-gfx-text';
import { NoteBlockSchema } from '@blocksuite/affine-model'; import { NoteBlockSchema } from '@blocksuite/affine-model';
import { import {
AutoClearSelectionService,
DNDAPIExtension, DNDAPIExtension,
DocModeService, DocModeService,
EmbedOptionService, EmbedOptionService,
@@ -77,6 +78,7 @@ export const CommonSpecs: ExtensionType[] = [
DNDAPIExtension, DNDAPIExtension,
FileDropExtension, FileDropExtension,
ToolbarRegistryExtension, ToolbarRegistryExtension,
AutoClearSelectionService,
...RootBlockAdapterExtensions, ...RootBlockAdapterExtensions,
...clipboardConfigs, ...clipboardConfigs,
...EdgelessElementViews, ...EdgelessElementViews,
@@ -0,0 +1,12 @@
import { LifeCycleWatcher } from '@blocksuite/std';
// Auto Clear selection when switching doc mode.
export class AutoClearSelectionService extends LifeCycleWatcher {
static override readonly key = 'auto-clear-selection-service';
override unmounted() {
if (this.std.store.readonly) return;
this.std.selection.clear();
}
}
@@ -1,3 +1,4 @@
export * from './auto-clear-selection-service';
export * from './block-meta-service'; export * from './block-meta-service';
export * from './doc-display-meta-service'; export * from './doc-display-meta-service';
export * from './doc-mode-service'; export * from './doc-mode-service';
@@ -353,3 +353,20 @@ test('Dropdown menus should be closed automatically when toolbar is displayed',
await expect(toolbar).toBeVisible(); await expect(toolbar).toBeVisible();
await expect(moreMenu).toBeHidden(); await expect(moreMenu).toBeHidden();
}); });
test('should clear selection when switching doc mode', async ({ page }) => {
await page.keyboard.press('Enter');
await page.keyboard.type('hello world');
await page.keyboard.press('Shift+ArrowLeft');
await page.keyboard.press('Shift+ArrowLeft');
await page.keyboard.press('Shift+ArrowLeft');
const toolbar = locateToolbar(page);
await expect(toolbar).toBeVisible();
await clickEdgelessModeButton(page);
await expect(toolbar).toBeHidden();
});
+4 -2
View File
@@ -516,7 +516,7 @@ test('the viewport should be fit when the linked document is with edgeless mode'
// move viewport // move viewport
const { x, y } = noteBoundingBox; const { x, y } = noteBoundingBox;
await page.mouse.click(x, y); await page.mouse.click(x - 10, y - 10);
await page.keyboard.down('Space'); await page.keyboard.down('Space');
await page.waitForTimeout(50); await page.waitForTimeout(50);
await page.mouse.down(); await page.mouse.down();
@@ -524,10 +524,12 @@ test('the viewport should be fit when the linked document is with edgeless mode'
await page.mouse.up(); await page.mouse.up();
await page.keyboard.up('Space'); await page.keyboard.up('Space');
await expect(note).toBeHidden();
// create edgeless text // create edgeless text
await page.keyboard.press('t'); await page.keyboard.press('t');
await page.mouse.click(x, y); await page.mouse.click(x, y);
await page.locator('affine-edgeless-text').waitFor({ state: 'visible' }); await page.waitForSelector('affine-edgeless-text');
await page.keyboard.type('Edgeless Text'); await page.keyboard.type('Edgeless Text');
const url = new URL(page.url()); const url = new URL(page.url());