fix(editor): keep select all in the equation editor on Firefox (#15858)

Fixes #15643

## Problem

In Firefox, pressing Ctrl+A in the LaTeX editor popup (inline equation
or equation block) selects the formula for a moment, then the selection
disappears. After that, typing has no effect. Chromium works.

Two things go wrong on Firefox:

1. **The select-all is dropped.** Firefox places the native select-all
range on the inline root rather than on text nodes. The editor's range
sync (`RangeBinding._onNativeSelectionChanged`) removes ranges whose
endpoints aren't inside a `v-text`. Table and database cells already
handle `Mod+A` themselves with `inlineEditor.selectAll()`, but the popup
didn't.
2. **Input over a full selection goes to the end.** Even with a proper
selection, Firefox's `beforeinput` `getTargetRanges()` starts at the
inline root (for example `DIV[data-v-root]@2`, just before the
`v-line`). That point resolves to the end of the text.
`EventService._onBeforeInput` then lets the target range replace the
native one, so `a+b=c` + `x` became `a+b=cx` instead of `x`, and
Backspace did nothing.

## Fix

- `LatexEditorMenu` handles `Mod+A` with `inlineEditor.selectAll()` and
stops the event, the same way `table-cell.ts` and the database cell
renderers do.
- `EventService._onBeforeInput` uses the target range only when the
native selection is collapsed. A non-collapsed selection is exactly what
the input replaces. Target ranges are still used for collapsed
selections, e.g. word deletion and the embed/empty-line case above it.

## Testing

- New unit test in `event-beforeinput.unit.spec.ts`. It selects the
whole text and feeds a Firefox-shaped target range that starts at the
inline root.
- Without the `event.ts` change it fails with `expected 'a+b=cx' to be
'x'`, the same result as in Firefox.
  - With the change it passes.
- `yarn vitest run` in `blocksuite/framework/std`: 12 files, 102 tests
pass.
- `oxlint --deny-warnings` and `oxfmt --check` pass on the changed
files, and `tsc -b blocksuite/affine/inlines/latex/tsconfig.json`
passes.
- I couldn't run a patched build in Firefox. Instead I checked both
changes in Firefox and Chromium (Playwright, headless) on
app.affine.pro, using a guest doc and injecting equivalent handlers into
the page. With both changes, Ctrl+A in the inline equation editor keeps
`a+b=c` selected, typing `x` gives `x`, and Backspace clears it. Without
them, Firefox shows the bug described in the issue.


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

<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Bug Fixes**
* Select-all now works in the LaTeX editor with the platform’s standard
keyboard shortcut.
* Fixed text replacement after selecting all, including cases where the
browser reports the selection from the start of the editor.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Mohamed Hussein authored and GitHub committed 2026-11-20 18:23:36 +08:00
1 parent 6ce486b2b8
commit a2dbbec72b
3 files changed
+43 -1

No files matched your search

@@ -3,6 +3,7 @@ import type { RichText } from '@blocksuite/affine-rich-text';
import { ThemeProvider } from '@blocksuite/affine-shared/services';
import { unsafeCSSVar } from '@blocksuite/affine-shared/theme';
import type { AffineTextAttributes } from '@blocksuite/affine-shared/types';
import { IS_MAC } from '@blocksuite/global/env';
import { SignalWatcher, WithDisposable } from '@blocksuite/global/lit';
import { noop } from '@blocksuite/global/utils';
import { DoneIcon } from '@blocksuite/icons/lit';
@@ -155,6 +156,12 @@ export class LatexEditorMenu extends SignalWatcher(
e.stopPropagation();
this.abortController.abort();
}
// Firefox's native select all is dropped by the editor's range sync.
if (e.key === 'a' && (IS_MAC ? e.metaKey : e.ctrlKey)) {
e.preventDefault();
e.stopPropagation();
this.richText?.inlineEditor?.selectAll();
}
});
this.disposables.addFromEvent(this, 'pointerdown', e => {
@@ -142,3 +142,37 @@ test('beforeinput ignores un-resolvable target range and still applies input', a
await teardownInlineEditor(ctx);
}
});
test('beforeinput replaces a full selection when the target range starts at the inline root', async () => {
const ctx = await setupInlineEditor('a+b=c');
try {
const range = ctx.editor.toDomRange({ index: 0, length: 5 });
expect(range).not.toBeNull();
setNativeSelection(range!);
// Firefox reports the target range of a select-all from before the first line.
const line = ctx.root.querySelector('v-line')!;
const lineOffset = Array.from(ctx.root.childNodes).indexOf(line);
const event = {
inputType: 'insertText',
data: 'x',
dataTransfer: null,
preventDefault: vi.fn(),
stopPropagation: vi.fn(),
getTargetRanges: () => [
{
startContainer: ctx.root,
startOffset: lineOffset,
endContainer: range!.endContainer,
endOffset: range!.endOffset,
},
],
} as unknown as InputEvent;
await (ctx.editor.eventService as any)._onBeforeInput(event);
expect(ctx.editor.yTextString).toBe('x');
} finally {
await teardownInlineEditor(ctx);
}
});
@@ -180,7 +180,8 @@ export class EventService<TextAttributes extends BaseTextAttributes> {
if (!inlineRange) return;
}
let ifHandleTargetRange = true;
// Keep a non-collapsed selection: Firefox may start the target range at the inline root.
let ifHandleTargetRange = inlineRange.length === 0;
if (
event.inputType.startsWith('delete') &&