From 0b2d11e6b1c7414f9fe4dab2b772f3af3ad9438e Mon Sep 17 00:00:00 2001 From: fundon Date: Wed, 15 Jan 2025 10:57:27 +0000 Subject: [PATCH] fix(editor): text highlighting (#9708) --- .../components/highlight/highlight-button.ts | 14 +-- .../e2e/blocksuite/toolbar.spec.ts | 91 +++++++++++++++++++ 2 files changed, 96 insertions(+), 9 deletions(-) create mode 100644 tests/affine-local/e2e/blocksuite/toolbar.spec.ts diff --git a/blocksuite/blocks/src/root-block/widgets/format-bar/components/highlight/highlight-button.ts b/blocksuite/blocks/src/root-block/widgets/format-bar/components/highlight/highlight-button.ts index 8984c2ae51..7ff3aa40db 100644 --- a/blocksuite/blocks/src/root-block/widgets/format-bar/components/highlight/highlight-button.ts +++ b/blocksuite/blocks/src/root-block/widgets/format-bar/components/highlight/highlight-button.ts @@ -16,8 +16,8 @@ import type { AffineFormatBarWidget } from '../../format-bar.js'; import { backgroundConfig, foregroundConfig } from './consts.js'; enum HighlightType { - Foreground, - Background, + Color = 'color', + Background = 'background', } let lastUsedColor: string | null = null; @@ -35,8 +35,7 @@ const updateHighlight = ( styles: AffineTextAttributes; } = { styles: { - color: highlightType === HighlightType.Foreground ? color : null, - background: highlightType === HighlightType.Background ? color : null, + [`${highlightType}`]: color, }, }; host.std.command @@ -63,11 +62,7 @@ const HighlightPanel = ( @@ -84,6 +79,7 @@ const HighlightPanel = ( ${backgroundConfig.map( ({ name, color }) => html` window.getComputedStyle(e).getPropertyValue('color')); + + const paragraph = page.locator('affine-paragraph'); + const textSpan = paragraph.getByText('rld'); + await expect(textSpan).toBeVisible(); + const fgColor2 = await textSpan.evaluate(e => + window.getComputedStyle(e).getPropertyValue('color') + ); + + expect(fgColor1).toBe(fgColor2); + }); + + test('should change text background color', 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 formatBar = page.locator('.affine-format-bar-widget'); + await formatBar.locator('.highlight-icon').hover(); + + const fgGreenButton = formatBar.locator( + '[data-testid="var(--affine-text-highlight-foreground-green)"]' + ); + await fgGreenButton.click(); + const fgColor1 = await fgGreenButton + .locator('span') + .evaluate(e => window.getComputedStyle(e).getPropertyValue('color')); + + const paragraph = page.locator('affine-paragraph'); + const text1Span = paragraph.getByText('rld'); + const fgColor2 = await text1Span.evaluate(e => + window.getComputedStyle(e).getPropertyValue('color') + ); + + expect(fgColor1).toBe(fgColor2); + + await page.keyboard.press('Shift+ArrowLeft'); + await page.keyboard.press('Shift+ArrowLeft'); + + await formatBar.locator('.highlight-icon').hover(); + + const bgYellowButton = formatBar.locator( + '[data-testid="var(--affine-text-highlight-yellow)"]' + ); + await bgYellowButton.click(); + const bgColor1 = await bgYellowButton + .locator('span') + .evaluate(e => window.getComputedStyle(e).getPropertyValue('background')); + + const text2Span = paragraph.getByText('world'); + const bgColor2 = await text2Span.evaluate(e => + window.getComputedStyle(e).getPropertyValue('background') + ); + + expect(bgColor1).toBe(bgColor2); + }); +});