feat(editor): improve latex editing support (#14924)

## Summary
- support converting selected text into inline LaTeX equations
- support turning text blocks into LaTeX equation blocks
- add equation entries to editor toolbars while keeping inline equation
with text formatting actions

## Tests
- yarn tsc -b blocksuite/affine/inlines/latex/tsconfig.json
blocksuite/affine/blocks/note/tsconfig.json
blocksuite/affine/blocks/root/tsconfig.json
blocksuite/affine/rich-text/tsconfig.json
blocksuite/affine/widgets/keyboard-toolbar/tsconfig.json --pretty false
- git diff --check origin/canary...HEAD

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
  * Equation block support with conversion from existing blocks.
  * Inline LaTeX insertion added to the inline formatting toolbar.
* Equation action added to the keyboard toolbar; Equation blocks
searchable via math/equation/latex aliases.

* **Improvements**
* Inline LaTeX editor opens and syncs more reliably; selection/convert
flow preserves distinct LaTeX values when converting in reverse order.

* **Tests**
  * New e2e tests for inline LaTeX conversions and value preservation.

<!-- review_stack_entry_start -->

[![Review Change
Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/toeverything/AFFiNE/pull/14924)

<!-- review_stack_entry_end -->
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Jachin
2026-05-14 11:56:54 +08:00
committed by GitHub
parent 7280fe33bc
commit 542da0b347
11 changed files with 249 additions and 39 deletions
+65 -5
View File
@@ -1,5 +1,12 @@
import { expect } from '@playwright/test';
import {
dragBetweenIndices,
enterPlaygroundRoom,
focusRichText,
initEmptyParagraphState,
waitNextFrame,
} from '../utils/actions/index.js';
import {
cutByKeyboard,
pasteByKeyboard,
@@ -15,17 +22,13 @@ import {
type,
undoByKeyboard,
} from '../utils/actions/keyboard.js';
import {
enterPlaygroundRoom,
focusRichText,
initEmptyParagraphState,
} from '../utils/actions/misc.js';
import {
assertRichTextInlineDeltas,
assertRichTextInlineRange,
} from '../utils/asserts.js';
import { ZERO_WIDTH_FOR_EMPTY_LINE } from '../utils/inline-editor.js';
import { test } from '../utils/playwright.js';
import { getFormatBar } from '../utils/query.js';
test('add inline latex at the start of line', async ({ page }, testInfo) => {
await enterPlaygroundRoom(page);
@@ -240,6 +243,63 @@ test('add inline latex using slash menu', async ({ page }, testInfo) => {
expect(await latexElement.locator('.katex').innerHTML()).toBe(innerHTML);
});
test('should preserve distinct latex values when converting selections in reverse order', async ({
page,
}) => {
await enterPlaygroundRoom(page);
await initEmptyParagraphState(page);
await focusRichText(page);
await type(page, 'a+b test a^2 test');
await dragBetweenIndices(page, [0, 9], [0, 12]);
const { formatBar } = getFormatBar(page);
await expect(formatBar).toBeVisible();
await formatBar.getByRole('button', { name: 'Inline equation' }).click();
await waitNextFrame(page);
await assertRichTextInlineDeltas(page, [
{
insert: 'a+b test ',
},
{
insert: ' ',
attributes: {
latex: 'a^2',
},
},
{
insert: ' test',
},
]);
await dragBetweenIndices(page, [0, 0], [0, 3]);
await expect(formatBar).toBeVisible();
await formatBar.getByRole('button', { name: 'Inline equation' }).click();
await waitNextFrame(page);
await assertRichTextInlineDeltas(page, [
{
insert: ' ',
attributes: {
latex: 'a+b',
},
},
{
insert: ' test ',
},
{
insert: ' ',
attributes: {
latex: 'a^2',
},
},
{
insert: ' test',
},
]);
});
test('add inline latex using markdown shortcut', async ({ page }) => {
await enterPlaygroundRoom(page);
await initEmptyParagraphState(page);