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

View File

@@ -121,6 +121,38 @@ export const updateBlockType: Command<
} }
return next({ updatedBlocks: [newModel] }); return next({ updatedBlocks: [newModel] });
}; };
const transformToLatex: Command<{}, { updatedBlocks: BlockModel[] }> = (
_,
next
) => {
if (flavour !== 'affine:latex') return;
const newModels: BlockModel[] = [];
blockModels.forEach(model => {
if (
!matchModels(model, [
ParagraphBlockModel,
ListBlockModel,
CodeBlockModel,
])
) {
return;
}
const latex = model.text?.toString() ?? '';
const newId = transformModel(model, 'affine:latex', { latex });
if (!newId) {
return;
}
const newModel = doc.getModelById(newId);
if (newModel) {
newModels.push(newModel);
}
});
if (newModels.length === 0) return;
return next({ updatedBlocks: newModels });
};
const focusText: Command<{ updatedBlocks: BlockModel[] }> = (ctx, next) => { const focusText: Command<{ updatedBlocks: BlockModel[] }> = (ctx, next) => {
const { updatedBlocks } = ctx; const { updatedBlocks } = ctx;
@@ -185,6 +217,27 @@ export const updateBlockType: Command<
}); });
return next(); return next();
}; };
const selectBlocks: Command<{ updatedBlocks: BlockModel[] }> = (
ctx,
next
) => {
const { updatedBlocks } = ctx;
if (!updatedBlocks || updatedBlocks.length === 0) {
return false;
}
requestAnimationFrame(() => {
host.selection.setGroup(
'note',
updatedBlocks.map(model =>
host.selection.create(BlockSelection, {
blockId: model.id,
})
)
);
});
return next();
};
const [result, resultCtx] = std.command const [result, resultCtx] = std.command
.chain() .chain()
@@ -196,6 +249,7 @@ export const updateBlockType: Command<
.try<{ updatedBlocks: BlockModel[] }>(chain => [ .try<{ updatedBlocks: BlockModel[] }>(chain => [
chain.pipe(mergeToCode), chain.pipe(mergeToCode),
chain.pipe(appendDivider), chain.pipe(appendDivider),
chain.pipe(transformToLatex),
chain.pipe((_, next) => { chain.pipe((_, next) => {
const newModels: BlockModel[] = []; const newModels: BlockModel[] = [];
blockModels.forEach(model => { blockModels.forEach(model => {
@@ -227,6 +281,14 @@ export const updateBlockType: Command<
]) ])
// focus // focus
.try(chain => [ .try(chain => [
chain
.pipe((_, next) => {
if (flavour === 'affine:latex') {
return next();
}
return false;
})
.pipe(selectBlocks),
chain.pipe((_, next) => { chain.pipe((_, next) => {
if (['affine:code', 'affine:divider'].includes(flavour)) { if (['affine:code', 'affine:divider'].includes(flavour)) {
return next(); return next();

View File

@@ -30,6 +30,7 @@
"@blocksuite/affine-gfx-pointer": "workspace:*", "@blocksuite/affine-gfx-pointer": "workspace:*",
"@blocksuite/affine-gfx-shape": "workspace:*", "@blocksuite/affine-gfx-shape": "workspace:*",
"@blocksuite/affine-gfx-text": "workspace:*", "@blocksuite/affine-gfx-text": "workspace:*",
"@blocksuite/affine-inline-latex": "workspace:*",
"@blocksuite/affine-inline-preset": "workspace:*", "@blocksuite/affine-inline-preset": "workspace:*",
"@blocksuite/affine-model": "workspace:*", "@blocksuite/affine-model": "workspace:*",
"@blocksuite/affine-rich-text": "workspace:*", "@blocksuite/affine-rich-text": "workspace:*",

View File

@@ -15,6 +15,7 @@ import {
import type { HighlightType } from '@blocksuite/affine-components/highlight-dropdown-menu'; import type { HighlightType } from '@blocksuite/affine-components/highlight-dropdown-menu';
import { toast } from '@blocksuite/affine-components/toast'; import { toast } from '@blocksuite/affine-components/toast';
import { EditorChevronDown } from '@blocksuite/affine-components/toolbar'; import { EditorChevronDown } from '@blocksuite/affine-components/toolbar';
import { insertInlineLatex } from '@blocksuite/affine-inline-latex';
import { import {
deleteTextCommand, deleteTextCommand,
formatBlockCommand, formatBlockCommand,
@@ -61,6 +62,7 @@ import {
DeleteIcon, DeleteIcon,
DuplicateIcon, DuplicateIcon,
LinkedPageIcon, LinkedPageIcon,
TeXIcon,
} from '@blocksuite/icons/lit'; } from '@blocksuite/icons/lit';
import { import {
type BlockComponent, type BlockComponent,
@@ -199,9 +201,9 @@ const alignActionGroup = {
const inlineTextActionGroup = { const inlineTextActionGroup = {
id: 'b.inline-text', id: 'b.inline-text',
when: ({ chain }) => isFormatSupported(chain).run()[0], when: ({ chain }) => isFormatSupported(chain).run()[0],
actions: textFormatConfigs.map( actions: textFormatConfigs.flatMap(
({ id, name, action, activeWhen, icon }, score) => { ({ id, name, action, activeWhen, icon }, score) => {
return { const textAction: ToolbarAction = {
id, id,
icon, icon,
score, score,
@@ -209,6 +211,28 @@ const inlineTextActionGroup = {
run: ({ host }) => action(host), run: ({ host }) => action(host),
active: ({ host }) => activeWhen(host), active: ({ host }) => activeWhen(host),
}; };
if (id !== 'underline') {
return [textAction];
}
return [
textAction,
{
id: 'inline-latex',
icon: TeXIcon(),
score: score + 0.5,
tooltip: 'Inline Equation',
run: ({ host }) => {
host.std.command
.chain()
.pipe(getTextSelectionCommand)
.pipe(insertInlineLatex)
.run();
},
active: () => false,
},
];
} }
), ),
} as const satisfies ToolbarActionGroup; } as const satisfies ToolbarActionGroup;

View File

@@ -27,6 +27,7 @@
{ "path": "../../gfx/pointer" }, { "path": "../../gfx/pointer" },
{ "path": "../../gfx/shape" }, { "path": "../../gfx/shape" },
{ "path": "../../gfx/text" }, { "path": "../../gfx/text" },
{ "path": "../../inlines/latex" },
{ "path": "../../inlines/preset" }, { "path": "../../inlines/preset" },
{ "path": "../../model" }, { "path": "../../model" },
{ "path": "../../rich-text" }, { "path": "../../rich-text" },

View File

@@ -2,14 +2,48 @@ import {
DocModeProvider, DocModeProvider,
TelemetryProvider, TelemetryProvider,
} from '@blocksuite/affine-shared/services'; } from '@blocksuite/affine-shared/services';
import type { AffineInlineEditor } from '@blocksuite/affine-shared/types';
import type { Command, TextSelection } from '@blocksuite/std'; import type { Command, TextSelection } from '@blocksuite/std';
import type { InlineRange } from '@blocksuite/std/inline';
function openInlineLatexEditor(
inlineEditor: AffineInlineEditor,
index: number
) {
inlineEditor
.waitForUpdate()
.then(async () => {
await inlineEditor.waitForUpdate();
const textPoint = inlineEditor.getTextPoint(index);
if (!textPoint) return;
const [text] = textPoint;
const latexNode = text.parentElement?.closest('affine-latex-node');
if (!latexNode) return;
latexNode.toggleEditor();
})
.catch(console.error);
}
function getSingleBlockInlineRange(
textSelection: TextSelection
): InlineRange | null {
if (textSelection.to) {
return null;
}
return {
index: textSelection.from.index,
length: textSelection.from.length,
};
}
export const insertInlineLatex: Command<{ export const insertInlineLatex: Command<{
currentTextSelection?: TextSelection; currentTextSelection?: TextSelection;
textSelection?: TextSelection; textSelection?: TextSelection;
}> = (ctx, next) => { }> = (ctx, next) => {
const textSelection = ctx.textSelection ?? ctx.currentTextSelection; const textSelection = ctx.textSelection ?? ctx.currentTextSelection;
if (!textSelection || !textSelection.isCollapsed()) return; if (!textSelection) return;
const blockComponent = ctx.std.view.getBlock(textSelection.from.blockId); const blockComponent = ctx.std.view.getBlock(textSelection.from.blockId);
if (!blockComponent) return; if (!blockComponent) return;
@@ -20,24 +54,19 @@ export const insertInlineLatex: Command<{
const inlineEditor = richText.inlineEditor; const inlineEditor = richText.inlineEditor;
if (!inlineEditor) return; if (!inlineEditor) return;
inlineEditor.insertText( const inlineRange = getSingleBlockInlineRange(textSelection);
{ if (!inlineRange) return;
index: textSelection.from.index,
length: 0, const latex = textSelection.isCollapsed()
}, ? ''
' ' : inlineEditor.yTextString.slice(
); inlineRange.index,
inlineEditor.formatText( inlineRange.index + inlineRange.length
{ );
index: textSelection.from.index,
length: 1, inlineEditor.insertText(inlineRange, ' ', { latex });
},
{
latex: '',
}
);
inlineEditor.setInlineRange({ inlineEditor.setInlineRange({
index: textSelection.from.index, index: inlineRange.index,
length: 1, length: 1,
}); });
@@ -56,19 +85,9 @@ export const insertInlineLatex: Command<{
control: 'create inline equation', control: 'create inline equation',
}); });
inlineEditor if (textSelection.isCollapsed()) {
.waitForUpdate() openInlineLatexEditor(inlineEditor, inlineRange.index + 1);
.then(async () => { }
await inlineEditor.waitForUpdate();
const textPoint = inlineEditor.getTextPoint(textSelection.from.index + 1);
if (!textPoint) return;
const [text] = textPoint;
const latexNode = text.parentElement?.closest('affine-latex-node');
if (!latexNode) return;
latexNode.toggleEditor();
})
.catch(console.error);
next(); next();
}; };

View File

@@ -15,7 +15,7 @@ import {
import type { DeltaInsert } from '@blocksuite/store'; import type { DeltaInsert } from '@blocksuite/store';
import { signal } from '@preact/signals-core'; import { signal } from '@preact/signals-core';
import katex from 'katex'; import katex from 'katex';
import { css, html, render } from 'lit'; import { css, html, type PropertyValues, render } from 'lit';
import { property } from 'lit/decorators.js'; import { property } from 'lit/decorators.js';
export class AffineLatexNode extends SignalWatcher( export class AffineLatexNode extends SignalWatcher(
@@ -85,6 +85,8 @@ export class AffineLatexNode extends SignalWatcher(
private _editorAbortController: AbortController | null = null; private _editorAbortController: AbortController | null = null;
private _isEditorOpen = false;
readonly latex$ = signal(''); readonly latex$ = signal('');
readonly latexEditorSignal = signal(''); readonly latexEditorSignal = signal('');
@@ -174,6 +176,22 @@ export class AffineLatexNode extends SignalWatcher(
return result; return result;
} }
protected override updated(changedProperties: PropertyValues<this>) {
super.updated(changedProperties);
if (!changedProperties.has('delta') || this._isEditorOpen) {
return;
}
const latex = this.deltaLatex;
if (this.latex$.peek() !== latex) {
this.latex$.value = latex;
}
if (this.latexEditorSignal.peek() !== latex) {
this.latexEditorSignal.value = latex;
}
}
override render() { override render() {
return html`<span class="affine-latex" data-selected=${this.selected} return html`<span class="affine-latex" data-selected=${this.selected}
><div class="latex-container"></div> ><div class="latex-container"></div>
@@ -212,9 +230,11 @@ export class AffineLatexNode extends SignalWatcher(
}, },
}); });
this._isEditorOpen = true;
this._editorAbortController.signal.addEventListener( this._editorAbortController.signal.addEventListener(
'abort', 'abort',
() => { () => {
this._isEditorOpen = false;
portal.remove(); portal.remove();
const latex = this.latexEditorSignal.peek(); const latex = this.latexEditorSignal.peek();
this.latex$.value = latex; this.latex$.value = latex;

View File

@@ -13,6 +13,7 @@ import {
QuoteIcon, QuoteIcon,
TextIcon, TextIcon,
} from '@blocksuite/affine-components/icons'; } from '@blocksuite/affine-components/icons';
import { TeXIcon } from '@blocksuite/icons/lit';
import type { TemplateResult } from 'lit'; import type { TemplateResult } from 'lit';
/** /**
@@ -119,6 +120,15 @@ export const textConversionConfigs: TextConversionConfig[] = [
hotkey: [`Mod-Alt-c`], hotkey: [`Mod-Alt-c`],
icon: CodeBlockIcon, icon: CodeBlockIcon,
}, },
{
flavour: 'affine:latex',
type: undefined,
name: 'Equation',
description: 'Formula block with LaTeX rendering.',
hotkey: null,
icon: TeXIcon(),
searchAlias: ['mathBlock', 'equationBlock', 'latexBlock'],
},
{ {
flavour: 'affine:paragraph', flavour: 'affine:paragraph',
type: 'quote', type: 'quote',

View File

@@ -222,6 +222,17 @@ const textToolActionItems: KeyboardToolbarActionItem[] = [
}); });
}, },
}, },
{
name: 'Equation',
showWhen: ({ std }) =>
std.store.schema.flavourSchemaMap.has('affine:latex'),
icon: TeXIcon(),
action: ({ std }) => {
std.command.exec(updateBlockType, {
flavour: 'affine:latex',
});
},
},
{ {
name: 'Quote', name: 'Quote',
showWhen: ({ std }) => showWhen: ({ std }) =>

View File

@@ -1,5 +1,12 @@
import { expect } from '@playwright/test'; import { expect } from '@playwright/test';
import {
dragBetweenIndices,
enterPlaygroundRoom,
focusRichText,
initEmptyParagraphState,
waitNextFrame,
} from '../utils/actions/index.js';
import { import {
cutByKeyboard, cutByKeyboard,
pasteByKeyboard, pasteByKeyboard,
@@ -15,17 +22,13 @@ import {
type, type,
undoByKeyboard, undoByKeyboard,
} from '../utils/actions/keyboard.js'; } from '../utils/actions/keyboard.js';
import {
enterPlaygroundRoom,
focusRichText,
initEmptyParagraphState,
} from '../utils/actions/misc.js';
import { import {
assertRichTextInlineDeltas, assertRichTextInlineDeltas,
assertRichTextInlineRange, assertRichTextInlineRange,
} from '../utils/asserts.js'; } from '../utils/asserts.js';
import { ZERO_WIDTH_FOR_EMPTY_LINE } from '../utils/inline-editor.js'; import { ZERO_WIDTH_FOR_EMPTY_LINE } from '../utils/inline-editor.js';
import { test } from '../utils/playwright.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) => { test('add inline latex at the start of line', async ({ page }, testInfo) => {
await enterPlaygroundRoom(page); 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); 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 }) => { test('add inline latex using markdown shortcut', async ({ page }) => {
await enterPlaygroundRoom(page); await enterPlaygroundRoom(page);
await initEmptyParagraphState(page); await initEmptyParagraphState(page);

View File

@@ -365,6 +365,7 @@ export const PackageList = [
'blocksuite/affine/gfx/pointer', 'blocksuite/affine/gfx/pointer',
'blocksuite/affine/gfx/shape', 'blocksuite/affine/gfx/shape',
'blocksuite/affine/gfx/text', 'blocksuite/affine/gfx/text',
'blocksuite/affine/inlines/latex',
'blocksuite/affine/inlines/preset', 'blocksuite/affine/inlines/preset',
'blocksuite/affine/model', 'blocksuite/affine/model',
'blocksuite/affine/rich-text', 'blocksuite/affine/rich-text',

View File

@@ -2241,6 +2241,7 @@ __metadata:
"@blocksuite/affine-gfx-pointer": "workspace:*" "@blocksuite/affine-gfx-pointer": "workspace:*"
"@blocksuite/affine-gfx-shape": "workspace:*" "@blocksuite/affine-gfx-shape": "workspace:*"
"@blocksuite/affine-gfx-text": "workspace:*" "@blocksuite/affine-gfx-text": "workspace:*"
"@blocksuite/affine-inline-latex": "workspace:*"
"@blocksuite/affine-inline-preset": "workspace:*" "@blocksuite/affine-inline-preset": "workspace:*"
"@blocksuite/affine-model": "workspace:*" "@blocksuite/affine-model": "workspace:*"
"@blocksuite/affine-rich-text": "workspace:*" "@blocksuite/affine-rich-text": "workspace:*"