Files
AFFiNE-Mirror/tests/blocksuite/e2e/edgeless/brush.spec.ts
fundon 2c4278058b feat(editor): add highlighter (#10573)
Closes: [BS-2909](https://linear.app/affine-design/issue/BS-2909/新增highlighter)

### What's Changed!

Currently the highlighter tool is very similar to brush, but for the future, it's a standalone module.

* Added `Highlighter` element model
* Added `Highlighter` tool
* Added `Highlighter` entry to the global toolbar
2025-03-27 08:53:26 +00:00

198 lines
6.1 KiB
TypeScript

import { expect } from '@playwright/test';
import { lightThemeV2 } from '@toeverything/theme/v2';
import {
assertEdgelessTool,
deleteAll,
pickColorAtPoints,
selectBrushColor,
selectBrushSize,
setEdgelessTool,
switchEditorMode,
updateExistedBrushElementSize,
zoomResetByKeyboard,
} from '../utils/actions/edgeless.js';
import {
addBasicBrushElement,
click,
dragBetweenCoords,
enterPlaygroundRoom,
initEmptyEdgelessState,
resizeElementByHandle,
} from '../utils/actions/index.js';
import {
assertEdgelessColorSameWithHexColor,
assertEdgelessSelectedRect,
assertSameColor,
} from '../utils/asserts.js';
import { test } from '../utils/playwright.js';
test('change editor mode when brush color palette opening', async ({
page,
}) => {
await enterPlaygroundRoom(page);
await initEmptyEdgelessState(page);
await switchEditorMode(page);
await setEdgelessTool(page, 'brush');
const penMenu = page.locator('edgeless-pen-menu');
const colorPalettes = penMenu.locator('edgeless-color-panel');
await expect(colorPalettes).toBeVisible();
await switchEditorMode(page);
await expect(colorPalettes).toBeHidden();
});
test('add brush element', async ({ page }) => {
await enterPlaygroundRoom(page);
await initEmptyEdgelessState(page);
await switchEditorMode(page);
const start = { x: 100, y: 100 };
const end = { x: 200, y: 200 };
await addBasicBrushElement(page, start, end, false);
await assertEdgelessTool(page, 'brush');
});
test('resize brush element', async ({ page }) => {
await enterPlaygroundRoom(page);
await initEmptyEdgelessState(page);
await switchEditorMode(page);
const start = { x: 100, y: 100 };
const end = { x: 200, y: 200 };
await addBasicBrushElement(page, start, end);
await page.mouse.click(start.x + 5, start.y + 5);
await assertEdgelessSelectedRect(page, [98, 98, 104, 104]);
await page.mouse.click(start.x + 5, start.y + 5);
const delta = { x: 20, y: 40 };
await resizeElementByHandle(page, delta, 'top-left', 10);
await page.mouse.click(start.x + 25, start.y + 45);
await assertEdgelessSelectedRect(page, [118, 138, 84, 64]);
});
test('add brush element with color', async ({ page }) => {
await enterPlaygroundRoom(page);
await initEmptyEdgelessState(page);
await switchEditorMode(page);
await setEdgelessTool(page, 'brush');
await selectBrushColor(page, 'Blue');
const start = { x: 100, y: 100 };
const end = { x: 200, y: 200 };
await dragBetweenCoords(page, start, end, { steps: 100 });
const [pickedColor] = await pickColorAtPoints(page, [[110, 110]]);
const color = lightThemeV2['edgeless/palette/medium/blueMedium'];
await assertEdgelessColorSameWithHexColor(page, color, pickedColor);
});
test('keep same color when mouse mode switched back to brush', async ({
page,
}) => {
await enterPlaygroundRoom(page);
await initEmptyEdgelessState(page);
await switchEditorMode(page);
await deleteAll(page);
await setEdgelessTool(page, 'brush');
await selectBrushColor(page, 'Blue');
const start = { x: 200, y: 200 };
const end = { x: 300, y: 300 };
await dragBetweenCoords(page, start, end, { steps: 100 });
await setEdgelessTool(page, 'default');
await click(page, { x: 50, y: 50 });
await setEdgelessTool(page, 'brush');
const origin = { x: 100, y: 100 };
await dragBetweenCoords(page, origin, start, { steps: 100 });
const [pickedColor] = await pickColorAtPoints(page, [[110, 110]]);
const color = lightThemeV2['edgeless/palette/medium/blueMedium'];
await assertEdgelessColorSameWithHexColor(page, color, pickedColor);
});
// TODO(@fundon): should add it back?
test.skip('add brush element with different size', async ({ page }) => {
await enterPlaygroundRoom(page);
await initEmptyEdgelessState(page);
await switchEditorMode(page);
await setEdgelessTool(page, 'brush');
await selectBrushSize(page, 'ten');
await selectBrushColor(page, 'Blue');
const start = { x: 100, y: 100 };
const end = { x: 200, y: 100 };
await dragBetweenCoords(page, start, end, { steps: 100 });
const [topEdge, bottomEdge, nearTopEdge, nearBottomEdge] =
await pickColorAtPoints(page, [
// Select two points on the top and bottom border of the line,
// their color should be the same as the specified color
[110, 95],
[110, 104],
// Select two points close to the upper and lower boundaries of the line,
// their color should be different from the specified color
[110, 94],
[110, 105],
]);
const color = lightThemeV2['edgeless/palette/medium/blueMedium'];
await assertEdgelessColorSameWithHexColor(page, color, topEdge);
await assertEdgelessColorSameWithHexColor(page, color, bottomEdge);
assertSameColor(nearTopEdge, '#84cfff');
assertSameColor(nearBottomEdge, '#84cfff');
});
test('change brush element size by component-toolbar', async ({ page }) => {
await enterPlaygroundRoom(page);
await initEmptyEdgelessState(page);
await switchEditorMode(page);
await zoomResetByKeyboard(page);
const start = { x: 100, y: 100 };
const end = { x: 200, y: 200 };
await addBasicBrushElement(page, start, end);
// wait for menu hide animation
await page.waitForTimeout(500);
// change to line width 12
await page.mouse.click(110, 110);
await updateExistedBrushElementSize(page, 6);
await assertEdgelessSelectedRect(page, [94, 94, 112, 112]);
// change to line width 10
await page.mouse.click(110, 110);
await updateExistedBrushElementSize(page, 5);
await assertEdgelessSelectedRect(page, [95, 95, 110, 110]);
// change to line width 8
await page.mouse.click(110, 110);
await updateExistedBrushElementSize(page, 4);
await assertEdgelessSelectedRect(page, [96, 96, 108, 108]);
// change to line width 6
await page.mouse.click(110, 110);
await updateExistedBrushElementSize(page, 3);
await assertEdgelessSelectedRect(page, [97, 97, 106, 106]);
// change to line width 4
await page.mouse.click(110, 110);
await updateExistedBrushElementSize(page, 2);
await assertEdgelessSelectedRect(page, [98, 98, 104, 104]);
// change to line width 2
await page.mouse.click(110, 110);
await updateExistedBrushElementSize(page, 1);
await assertEdgelessSelectedRect(page, [99, 99, 102, 102]);
});