fix(editor): keep sorted table row in place when typing starts editing (#15915)

Row order is locked while a table cell is being edited, but typing into
a selected cell wrote the first character before that lock applied. In a
sorted view the row moved immediately, while the selection kept pointing
at its old index, so typing was interrupted.

Typing now enters edit mode and locks the rows before writing the
character. The row re-sorts when editing ends, the same as when editing
starts with a double click.

fix #14043

Co-authored-by: DarkSky <darksky2048@gmail.com>
This commit is contained in:
CommaBotandDarkSky authored and GitHub committed 2026-11-23 22:43:02 +08:00
1 parent f0e5009a9f
commit 10ea096689
5 files changed
+79 -9

No files matched your search

@@ -17,6 +17,7 @@ const TAG_COLUMN_TYPES = [
function createLogic() {
const view = {
rowsDelete: vi.fn(),
lockRows: vi.fn(),
rows$: { value: [] },
groupTrait: { groupsDataList$: { value: [] } },
};
@@ -73,6 +74,7 @@ describe('TableHotkeysController', () => {
const cell = {
rowId: 'r1',
dataset: { rowId: 'r1', columnId: 'c1' },
isEditing$: { value: true },
column: {
valueSetFromString: vi.fn(),
type$: { value: 'text' },
@@ -140,6 +142,7 @@ describe('Virtual TableHotkeysController', () => {
const cell = {
rowId: 'r1',
dataset: { rowId: 'r1', columnId: 'c1' },
isEditing$: { value: true },
column$: {
value: {
valueSetFromString: vi.fn(),
@@ -412,6 +412,7 @@ export class TableHotkeysController implements ReactiveController {
this.selectionController
),
updateSelection: sel => (this.selectionController.selection = sel),
lockRows: lock => this.logic.view.lockRows(lock),
getColumn: cell => cell.column$.value,
});
})
@@ -410,6 +410,7 @@ export class TableHotkeysController implements ReactiveController {
this.selectionController
),
updateSelection: sel => (this.selectionController.selection = sel),
lockRows: lock => this.logic.view.lockRows(lock),
getColumn: cell => cell.column,
});
})
@@ -7,6 +7,7 @@ import { TableViewRowSelection } from './selection';
export interface TableCell {
rowId: string;
isEditing$: ReadonlySignal<boolean>;
setTagDraft?(value: string): void;
}
@@ -31,14 +32,21 @@ export interface StartEditOptions<T extends TableCell> {
columnIndex: number
) => T | undefined;
updateSelection: (sel: TableViewSelectionWithType) => void;
lockRows: (lock: boolean) => void;
getColumn: ColumnAccessor<T>;
}
export function handleCharStartEdit<T extends TableCell>(
options: StartEditOptions<T>
): boolean {
const { event, selection, getCellContainer, updateSelection, getColumn } =
options;
const {
event,
selection,
getCellContainer,
updateSelection,
lockRows,
getColumn,
} = options;
const target = event.target as HTMLElement | null;
if (target && (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA')) {
@@ -61,14 +69,20 @@ export function handleCharStartEdit<T extends TableCell>(
);
if (cell) {
const column = getColumn(cell);
if (column) {
if (TAG_COLUMN_TYPES.has(column.type$.value) && cell.setTagDraft) {
cell.setTagDraft(event.key);
} else {
column.valueSetFromString(cell.rowId, event.key);
}
if (
column &&
TAG_COLUMN_TYPES.has(column.type$.value) &&
cell.setTagDraft
) {
cell.setTagDraft(event.key);
updateSelection({ ...selection, isEditing: true });
} else {
updateSelection({ ...selection, isEditing: true });
// Row order must be frozen before the first write, or a sorted view
// moves the row out from under the editor.
lockRows(cell.isEditing$.value);
column?.valueSetFromString(cell.rowId, event.key);
}
updateSelection({ ...selection, isEditing: true });
event.preventDefault();
return true;
}
@@ -192,6 +192,57 @@ test.each([false, true])(
}
);
test('typing into a sorted database title keeps editing the same row', async () => {
const noteId = addNote(doc);
const id = doc.addBlock('affine:database', {}, noteId);
const model = doc.getModelById(id) as DatabaseBlockModel;
const source = new DatabaseBlockDataSource(model, source => {
source.serviceSet(EditorHostKey, editor.host!);
});
const viewId = source.viewManager.viewAdd('table');
const titleId = source.properties$.value.find(
propertyId => source.propertyTypeGet(propertyId) === 'title'
)!;
const editedRow = source.rowAdd('end');
const otherRow = source.rowAdd('end');
doc.updateBlock(doc.getModelById(editedRow)!, { text: new Text('b') });
doc.updateBlock(doc.getModelById(otherRow)!, { text: new Text('c') });
source.viewDataUpdate(viewId, data => ({
...data,
sort: {
manuallySort: [],
sortBy: [{ ref: { type: 'ref', name: titleId }, desc: false }],
},
}));
await wait();
const database = editor.host!.view.getBlock(id)!;
await database.updateComplete;
await wait();
const container = database.querySelector<
HTMLElement & { selectCurrentCell: (editing: boolean) => void }
>(
`dv-table-view-cell-container[data-row-id="${editedRow}"][data-column-id="${titleId}"]`
)!;
container.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true }));
container.selectCurrentCell(false);
await wait();
container.dispatchEvent(
new KeyboardEvent('keydown', { key: 'z', bubbles: true, cancelable: true })
);
await wait();
await wait();
const titleCells = database.querySelectorAll<HTMLElement>(
`dv-table-view-cell-container[data-column-id="${titleId}"]`
);
expect(Array.from(titleCells, cell => cell.dataset.rowId)).toEqual([
editedRow,
otherRow,
]);
expect(titleCells[0].dataset.editing).toBe('true');
});
test.each(['page', 'edgeless'] as const)(
'table selection preserves its input owner in %s mode',
async mode => {