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') &&