From a2dbbec72b579a325e985979580341c480162a2b Mon Sep 17 00:00:00 2001 From: Mohamed Hussein Date: Fri, 20 Nov 2026 10:23:36 +0000 Subject: [PATCH] fix(editor): keep select all in the equation editor on Firefox (#15858) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. ## 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. --- .../latex/src/latex-node/latex-editor-menu.ts | 7 ++++ .../inline/event-beforeinput.unit.spec.ts | 34 +++++++++++++++++++ .../std/src/inline/services/event.ts | 3 +- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/blocksuite/affine/inlines/latex/src/latex-node/latex-editor-menu.ts b/blocksuite/affine/inlines/latex/src/latex-node/latex-editor-menu.ts index 7e6ebf7b59..146ddc0ae0 100644 --- a/blocksuite/affine/inlines/latex/src/latex-node/latex-editor-menu.ts +++ b/blocksuite/affine/inlines/latex/src/latex-node/latex-editor-menu.ts @@ -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 => { diff --git a/blocksuite/framework/std/src/__tests__/inline/event-beforeinput.unit.spec.ts b/blocksuite/framework/std/src/__tests__/inline/event-beforeinput.unit.spec.ts index 8133a9df44..1fec1225ea 100644 --- a/blocksuite/framework/std/src/__tests__/inline/event-beforeinput.unit.spec.ts +++ b/blocksuite/framework/std/src/__tests__/inline/event-beforeinput.unit.spec.ts @@ -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); + } +}); diff --git a/blocksuite/framework/std/src/inline/services/event.ts b/blocksuite/framework/std/src/inline/services/event.ts index 4d8387ae0e..f1b254cb3d 100644 --- a/blocksuite/framework/std/src/inline/services/event.ts +++ b/blocksuite/framework/std/src/inline/services/event.ts @@ -180,7 +180,8 @@ export class EventService { 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') &&