mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-11-30 01:00:27 +03:00
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 <darksky2048@gmail.com>
This commit is contained in:
1 parent
4c3593d926
commit
2198970fc9
2 files changed
+115
-62
No files matched your search
@@ -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<KeyboardToolbarActionItem>(color => ({
|
||||
...highlightColors.map<KeyboardToolbarActionItem>(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<KeyboardToolbarActionItem>(color => ({
|
||||
...highlightColors.map<KeyboardToolbarActionItem>(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}`),
|
||||
}),
|
||||
})),
|
||||
],
|
||||
},
|
||||
|
||||
@@ -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]);
|
||||
}
|
||||
);
|
||||
Reference in new issue
Block a user