Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Restore table shortcuts for cell and row selections https://github.com/siyuan-note/siyuan/issues/19276

Danield8c9694f24f0
diff --git a/app/src/protyle/util/tableControl.ts b/app/src/protyle/util/tableControl.ts
index 1698bc4182..0ec04bb569 100644
--- a/app/src/protyle/util/tableControl.ts
+++ b/app/src/protyle/util/tableControl.ts
@@ -529,6 +529,9 @@ export class TableControl {
if (!this.selection || this.protyle.disabled) {
return;
}
+ if (this.handleTableHotkey(event)) {
+ return;
+ }
const keymap = window.siyuan.config.keymap.editor.general;
const undo = matchHotKey(keymap.undo, event);
const redo = matchHotKey(keymap.redo, event);
@@ -656,6 +659,70 @@ export class TableControl {
}, {signal});
}
+ private handleTableHotkey(event: KeyboardEvent) {
+ if (event.isComposing) {
+ return false;
+ }
+ const keymap = window.siyuan.config.keymap.editor.table;
+ const action = ["insertRowAbove", "insertRowBelow", "insertColumnLeft", "insertColumnRight",
+ "moveToUp", "moveToDown", "moveToLeft", "moveToRight", "delete-row", "delete-column"]
+ .find(key => matchHotKey(keymap[key], event));
+ if (!action) {
+ return false;
+ }
+ event.preventDefault();
+ event.stopImmediatePropagation();
+ const selection = this.selection;
+ const grid = buildTableGrid(selection.table);
+ const mode = ["insertRowAbove", "insertRowBelow", "moveToUp", "moveToDown", "delete-row"].includes(action) ?
+ "row" : "column";
+ // 将单元格选区投影到目标行列,沿用表格操作的合并单元格边界检查。
+ const cells = new Set(this.getSelectedCells());
+ const indexes = new Set<number>();
+ grid.cellInfos.filter(info => cells.has(info.cell)).forEach(info => {
+ const start = mode === "row" ? info.row : info.col;
+ const span = mode === "row" ? info.rowspan : info.colspan;
+ for (let index = start; index < start + span; index++) {
+ indexes.add(index);
+ }
+ });
+ const sorted = Array.from(indexes).sort((a, b) => a - b);
+ if (sorted.length === 0) {
+ return true;
+ }
+ const first = sorted[0];
+ const last = sorted[sorted.length - 1];
+ if (action.startsWith("insert")) {
+ const index = action === "insertRowAbove" || action === "insertColumnLeft" ? first : last + 1;
+ if (this.canInsertAtBoundary(grid, mode, index)) {
+ if (mode === "row") {
+ this.insertRowAt(selection.node, selection.table, index);
+ } else {
+ this.insertColumnAt(selection.node, selection.table, index);
+ }
+ }
+ } else if (action === "delete-row" || action === "delete-column") {
+ if (mode === "row") {
+ deleteTableRows(this.protyle, selection.node, sorted);
+ } else {
+ deleteTableColumns(this.protyle, selection.node, sorted);
+ }
+ this.clear();
+ } else {
+ const target = action === "moveToUp" || action === "moveToLeft" ? first - 1 : last + 2;
+ const count = mode === "row" ? grid.rowCount : grid.columnCount;
+ if (target >= 0 && target <= count && !grid.cellInfos.some(info => info.rowspan > 1 || info.colspan > 1)) {
+ selection.mode = mode;
+ selection.indexes = indexes;
+ selection.anchor = first;
+ this.moveSelection(target);
+ this.updateSelectedCells();
+ this.scheduleRender();
+ }
+ }
+ return true;
+ }
+
private handleTablePointerMove(event: PointerEvent, fromControl: boolean) {
if (event.buttons !== 0) {
return;