From 69c2f09eba9dff48aa749b59eb75ff235ea819bb Mon Sep 17 00:00:00 2001 From: Jessy Latmi <69401292+JyVers@users.noreply.github.com> Date: Tue, 2 Jun 2026 04:52:05 +0200 Subject: [PATCH] fix(editor): keyboard shortcuts in table cells (#15067) ## Description Fixes keyboard shortcuts for text formatting (Ctrl+B, Ctrl+I, Ctrl+U, etc.) not working inside table cells. ## Changes - **Modified `table-cell.ts`**: Updated the `_handleKeyDown` method to only prevent default behavior for Tab key and allow other keyboard events to propagate, enabling text formatting shortcuts to work properly - **Created `table-keymap.ts`**: New module that registers the `textKeymap` for table blocks, ensuring text formatting shortcuts are available in table cells - **Updated `view.ts`**: Registered the `TableKeymapExtension` in the table view extension setup - **Cleaned up `format.ts`**: Removed unnecessary `TextSelection` check that was preventing shortcuts from working in table contexts ## Closes Closes #13916 #12127 ## Summary by CodeRabbit * **Bug Fixes** * Improved Tab key handling within table cells for more consistent keyboard navigation. * Simplified read-only detection for keyboard shortcuts to avoid unexpected behavior. * **Refactor** * Reworked table keyboard mapping and registration to streamline shortcut handling and event flow. --- blocksuite/affine/blocks/table/src/table-cell.ts | 9 +++------ blocksuite/affine/blocks/table/src/table-keymap.ts | 7 +++++++ blocksuite/affine/blocks/table/src/view.ts | 2 ++ blocksuite/affine/inlines/preset/src/keymap/format.ts | 11 ++--------- 4 files changed, 14 insertions(+), 15 deletions(-) create mode 100644 blocksuite/affine/blocks/table/src/table-keymap.ts diff --git a/blocksuite/affine/blocks/table/src/table-cell.ts b/blocksuite/affine/blocks/table/src/table-cell.ts index d57d610713..8cd31c223a 100644 --- a/blocksuite/affine/blocks/table/src/table-cell.ts +++ b/blocksuite/affine/blocks/table/src/table-cell.ts @@ -649,12 +649,9 @@ export class TableCell extends SignalWatcher( } private readonly _handleKeyDown = (e: KeyboardEvent) => { - if (e.key !== 'Escape') { - if (e.key === 'Tab') { - e.preventDefault(); - return; - } - e.stopPropagation(); + if (e.key !== 'Escape' && e.key === 'Tab') { + e.preventDefault(); + return; } }; diff --git a/blocksuite/affine/blocks/table/src/table-keymap.ts b/blocksuite/affine/blocks/table/src/table-keymap.ts new file mode 100644 index 0000000000..e83b78a160 --- /dev/null +++ b/blocksuite/affine/blocks/table/src/table-keymap.ts @@ -0,0 +1,7 @@ +import { textKeymap } from '@blocksuite/affine-inline-preset'; +import { TableBlockSchema } from '@blocksuite/affine-model'; +import { KeymapExtension } from '@blocksuite/std'; + +export const TableKeymapExtension = KeymapExtension(textKeymap, { + flavour: TableBlockSchema.model.flavour, +}); diff --git a/blocksuite/affine/blocks/table/src/view.ts b/blocksuite/affine/blocks/table/src/view.ts index 833f20165c..4e967cbcc0 100644 --- a/blocksuite/affine/blocks/table/src/view.ts +++ b/blocksuite/affine/blocks/table/src/view.ts @@ -9,6 +9,7 @@ import { literal } from 'lit/static-html.js'; import { tableSlashMenuConfig } from './configs/slash-menu'; import { effects } from './effects'; +import { TableKeymapExtension } from './table-keymap.js'; export class TableViewExtension extends ViewExtensionProvider { override name = 'affine-table-block'; @@ -22,6 +23,7 @@ export class TableViewExtension extends ViewExtensionProvider { super.setup(context); context.register([ FlavourExtension(TableModelFlavour), + TableKeymapExtension, BlockViewExtension(TableModelFlavour, literal`affine-table`), SlashMenuConfigExtension(TableModelFlavour, tableSlashMenuConfig), ]); diff --git a/blocksuite/affine/inlines/preset/src/keymap/format.ts b/blocksuite/affine/inlines/preset/src/keymap/format.ts index 3d620536ad..b0ad136d96 100644 --- a/blocksuite/affine/inlines/preset/src/keymap/format.ts +++ b/blocksuite/affine/inlines/preset/src/keymap/format.ts @@ -1,8 +1,4 @@ -import { - type BlockStdScope, - TextSelection, - type UIEventHandler, -} from '@blocksuite/std'; +import { type BlockStdScope, type UIEventHandler } from '@blocksuite/std'; import { textFormatConfigs } from '../command/index.js'; @@ -14,12 +10,9 @@ export const textFormatKeymap = (std: BlockStdScope) => return { ...acc, [config.hotkey as string]: ctx => { - const { store: doc, selection } = std; + const { store: doc } = std; if (doc.readonly) return; - const textSelection = selection.find(TextSelection); - if (!textSelection) return; - const allowed = config.textChecker?.(std.host) ?? true; if (!allowed) return;