From 2198970fc99c7df70fd00a45000c3970c7d6707a Mon Sep 17 00:00:00 2001 From: CommaBot Date: Mon, 23 Nov 2026 22:57:38 +0800 Subject: [PATCH] fix(editor): make default highlight color work in mobile keyboard toolbar (#15911) In the mobile keyboard toolbar's highlight panel, the "Default Color" items under Color and Background had no action, so tapping them did nothing and a highlight could not be removed on mobile. They now clear `color` / `background` (set it to `null`), the same as the desktop highlight menu. The panel's formatting chain is shared by all items instead of being repeated per group. Adds an integration test that applies a red color/background through the toolbar and checks that "Default Color" removes it. fix #13745 Co-authored-by: DarkSky --- .../widgets/keyboard-toolbar/src/config.ts | 100 +++++++----------- .../main/keyboard-toolbar-highlight.spec.ts | 77 ++++++++++++++ 2 files changed, 115 insertions(+), 62 deletions(-) create mode 100644 blocksuite/integration-test/src/__tests__/main/keyboard-toolbar-highlight.spec.ts diff --git a/blocksuite/affine/widgets/keyboard-toolbar/src/config.ts b/blocksuite/affine/widgets/keyboard-toolbar/src/config.ts index cdce1dcb0e..aba2663854 100644 --- a/blocksuite/affine/widgets/keyboard-toolbar/src/config.ts +++ b/blocksuite/affine/widgets/keyboard-toolbar/src/config.ts @@ -950,6 +950,32 @@ const textStyleToolItems: KeyboardToolbarItem[] = [ }, ]; +const highlightColors = [ + 'red', + 'orange', + 'yellow', + 'green', + 'teal', + 'blue', + 'purple', + 'grey', +] as const; + +const formatHighlight = ( + std: BlockStdScope, + styles: AffineTextStyleAttributes +) => { + const payload = { styles }; + std.command + .chain() + .try(chain => [ + chain.pipe(getTextSelectionCommand).pipe(formatTextCommand, payload), + chain.pipe(getBlockSelectionsCommand).pipe(formatBlockCommand, payload), + chain.pipe(formatNativeCommand, payload), + ]) + .run(); +}; + const highlightToolPanel: KeyboardToolPanelConfig = { icon: ({ std }) => { const [_, { textAttributes }] = std.command.exec(getTextAttributes); @@ -966,40 +992,15 @@ const highlightToolPanel: KeyboardToolPanelConfig = { { name: 'Default Color', icon: TextColorIcon(cssVarV2('text/highlight/fg/orange')), + action: ({ std }) => formatHighlight(std, { color: null }), }, - ...( - [ - 'red', - 'orange', - 'yellow', - 'green', - 'teal', - 'blue', - 'purple', - 'grey', - ] as const - ).map(color => ({ + ...highlightColors.map(color => ({ name: color.charAt(0).toUpperCase() + color.slice(1), icon: TextColorIcon(cssVarV2(`text/highlight/fg/${color}`)), - action: ({ std }) => { - const payload = { - styles: { - color: cssVarV2(`text/highlight/fg/${color}`), - } satisfies AffineTextStyleAttributes, - }; - std.command - .chain() - .try(chain => [ - chain - .pipe(getTextSelectionCommand) - .pipe(formatTextCommand, payload), - chain - .pipe(getBlockSelectionsCommand) - .pipe(formatBlockCommand, payload), - chain.pipe(formatNativeCommand, payload), - ]) - .run(); - }, + action: ({ std }) => + formatHighlight(std, { + color: cssVarV2(`text/highlight/fg/${color}`), + }), })), ], }, @@ -1009,42 +1010,17 @@ const highlightToolPanel: KeyboardToolPanelConfig = { { name: 'Default Color', icon: TextBackgroundDuotoneIcon(cssVarV2('text/highlight/bg/orange')), + action: ({ std }) => formatHighlight(std, { background: null }), }, - ...( - [ - 'red', - 'orange', - 'yellow', - 'green', - 'teal', - 'blue', - 'purple', - 'grey', - ] as const - ).map(color => ({ + ...highlightColors.map(color => ({ name: color.charAt(0).toUpperCase() + color.slice(1), icon: TextBackgroundDuotoneIcon( cssVarV2(`text/highlight/bg/${color}`) ), - action: ({ std }) => { - const payload = { - styles: { - background: cssVarV2(`text/highlight/bg/${color}`), - } satisfies AffineTextStyleAttributes, - }; - std.command - .chain() - .try(chain => [ - chain - .pipe(getTextSelectionCommand) - .pipe(formatTextCommand, payload), - chain - .pipe(getBlockSelectionsCommand) - .pipe(formatBlockCommand, payload), - chain.pipe(formatNativeCommand, payload), - ]) - .run(); - }, + action: ({ std }) => + formatHighlight(std, { + background: cssVarV2(`text/highlight/bg/${color}`), + }), })), ], }, diff --git a/blocksuite/integration-test/src/__tests__/main/keyboard-toolbar-highlight.spec.ts b/blocksuite/integration-test/src/__tests__/main/keyboard-toolbar-highlight.spec.ts new file mode 100644 index 0000000000..839a88d3a0 --- /dev/null +++ b/blocksuite/integration-test/src/__tests__/main/keyboard-toolbar-highlight.spec.ts @@ -0,0 +1,77 @@ +import type { ParagraphBlockModel } from '@blocksuite/affine-model'; +import { type BlockComponent, BlockSelection } from '@blocksuite/std'; +import { Text } from '@blocksuite/store'; +import { beforeEach, expect, test } from 'vitest'; + +import { + defaultKeyboardToolbarConfig, + type KeyboardToolbarActionItem, + type KeyboardToolPanelGroup, +} from '../../../../affine/widgets/keyboard-toolbar/src/config.js'; +import { wait } from '../utils/common.js'; +import { addNote } from '../utils/edgeless.js'; +import { setupEditor } from '../utils/setup.js'; + +beforeEach(async () => { + const cleanup = await setupEditor('page'); + return cleanup; +}); + +function findHighlightItem( + groupName: string, + name: string +): KeyboardToolbarActionItem { + const groups = defaultKeyboardToolbarConfig.items + .flatMap(item => ('items' in item ? item.items : [item])) + .flatMap(item => ('groups' in item ? item.groups : [])) + .filter( + (group): group is KeyboardToolPanelGroup => typeof group !== 'function' + ); + const highlightGroups = groups.slice( + groups.findIndex(group => group.name === 'Color') + ); + const item = highlightGroups + .find(group => group.name === groupName) + ?.items.find(entry => entry.name === name); + if (!item) { + throw new Error(`Cannot find highlight item: ${groupName}/${name}`); + } + return item; +} + +test.each([ + { group: 'Color', attribute: 'color' }, + { group: 'Background', attribute: 'background' }, +] as const)( + 'keyboard toolbar default $group item removes the $attribute highlight', + async ({ group, attribute }) => { + const noteId = addNote(doc); + const paragraph = doc.getBlock(noteId)!.model + .children[0] as ParagraphBlockModel; + doc.updateBlock(paragraph, { text: new Text('hello') }); + await wait(); + editor.host!.selection.setGroup('note', [ + editor.host!.selection.create(BlockSelection, { + blockId: paragraph.id, + }), + ]); + + const ctx = { + std: editor.std, + rootComponent: editor.std.view.getBlock(doc.root!.id) as BlockComponent, + closeToolPanel: () => {}, + }; + const highlight = () => + paragraph.props.text + .toDelta() + .map(delta => delta.attributes?.[attribute]); + + await findHighlightItem(group, 'Red').action?.(ctx); + await wait(); + expect(highlight()).toEqual([expect.stringContaining('red')]); + + await findHighlightItem(group, 'Default Color').action?.(ctx); + await wait(); + expect(highlight()).toEqual([undefined]); + } +);