Skip to content

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

:sparkles: Support deleting selected table rows and columns https://github.com/siyuan-note/siyuan/issues/4785

Daniel0806d52fbe7d
diff --git a/app/src/protyle/util/table.ts b/app/src/protyle/util/table.ts
index d2d4b2853b..c799ca59e0 100644
--- a/app/src/protyle/util/table.ts
+++ b/app/src/protyle/util/table.ts
@@ -1,5 +1,12 @@
import {updateTransaction} from "../wysiwyg/transaction";
-import {focusBlock, focusByRange, focusByWbr, getSelectionOffset, getSelectionPosition,} from "./selection";
+import {
+ focusBlock,
+ focusByRange,
+ focusByWbr,
+ getEditorRange,
+ getSelectionOffset,
+ getSelectionPosition,
+} from "./selection";
import {hasClosestBlock, hasClosestByClassName, hasClosestByTag} from "./hasClosest";
import {matchHotKey} from "./hotKey";
import {isNotCtrl} from "./compatibility";
@@ -965,6 +972,162 @@ export const buildTableGrid = (tableElement: HTMLElement): ITableGrid => {
};
};
+export const getTableFullRowSelection = (
+ tableElement: HTMLTableElement,
+ cellElements: HTMLTableCellElement[],
+) => {
+ const grid = buildTableGrid(tableElement);
+ const selectedCells = new Set(cellElements);
+ const indexes: number[] = [];
+ let hasPartialRow = false;
+ for (let row = 0; row < grid.rowCount; row++) {
+ const cells = grid.grid[row] || [];
+ const selectedSlots = cells.filter(cell => cell && selectedCells.has(cell)).length;
+ if (selectedSlots === 0) {
+ continue;
+ }
+ if (cells.length !== grid.columnCount || selectedSlots !== grid.columnCount) {
+ hasPartialRow = true;
+ break;
+ }
+ indexes.push(row);
+ }
+ return {
+ indexes: hasPartialRow ? [] : indexes,
+ merged: grid.cellInfos.some(info => info.rowspan > 1 || info.colspan > 1),
+ };
+};
+
+export const getTableFullColumnSelection = (
+ tableElement: HTMLTableElement,
+ cellElements: HTMLTableCellElement[],
+) => {
+ const grid = buildTableGrid(tableElement);
+ const selectedCells = new Set(cellElements);
+ const indexes: number[] = [];
+ let hasPartialColumn = false;
+ for (let column = 0; column < grid.columnCount; column++) {
+ let slotCount = 0;
+ let selectedSlotCount = 0;
+ for (let row = 0; row < grid.rowCount; row++) {
+ const cell = grid.grid[row]?.[column];
+ if (cell) {
+ slotCount++;
+ if (selectedCells.has(cell)) {
+ selectedSlotCount++;
+ }
+ }
+ }
+ if (selectedSlotCount === 0) {
+ continue;
+ }
+ if (slotCount !== grid.rowCount || selectedSlotCount !== grid.rowCount) {
+ hasPartialColumn = true;
+ break;
+ }
+ indexes.push(column);
+ }
+ return {
+ indexes: hasPartialColumn ? [] : indexes,
+ merged: grid.cellInfos.some(info => info.rowspan > 1 || info.colspan > 1),
+ };
+};
+
+const replaceTableCellTag = (cell: HTMLTableCellElement, tag: "th" | "td") => {
+ if (cell.tagName.toLowerCase() === tag) {
+ return;
+ }
+ const newCell = document.createElement(tag);
+ Array.from(cell.attributes).forEach(attribute => {
+ newCell.setAttribute(attribute.name, attribute.value);
+ });
+ while (cell.firstChild) {
+ newCell.append(cell.firstChild);
+ }
+ cell.replaceWith(newCell);
+};
+
+const normalizeTableSections = (tableElement: HTMLTableElement) => {
+ const rows = Array.from(tableElement.rows);
+ const head = tableElement.tHead || tableElement.createTHead();
+ const body = tableElement.tBodies[0] || tableElement.createTBody();
+ head.innerHTML = "";
+ body.innerHTML = "";
+ rows.forEach((row, index) => {
+ Array.from(row.cells).forEach(cell => replaceTableCellTag(cell, index === 0 ? "th" : "td"));
+ (index === 0 ? head : body).append(row);
+ });
+ Array.from(tableElement.tBodies).slice(1).forEach(item => item.remove());
+};
+
+export const deleteTableRows = (
+ protyle: IProtyle,
+ nodeElement: HTMLElement,
+ rowIndexes: number[],
+) => {
+ const tableElement = nodeElement.querySelector("table");
+ if (!tableElement) {
+ return false;
+ }
+ const grid = buildTableGrid(tableElement);
+ if (grid.cellInfos.some(info => info.rowspan > 1 || info.colspan > 1)) {
+ return false;
+ }
+ const indexes = Array.from(new Set(rowIndexes))
+ .filter(index => index >= 0 && index < grid.rowCount)
+ .sort((a, b) => b - a);
+ if (indexes.length === 0) {
+ return false;
+ }
+ if (indexes.length >= grid.rowCount) {
+ const range = getEditorRange(nodeElement);
+ nodeElement.classList.add("protyle-wysiwyg--select");
+ removeBlock(protyle, nodeElement, range, "remove");
+ return true;
+ }
+ const oldHTML = nodeElement.outerHTML;
+ const rows = Array.from(tableElement.rows);
+ indexes.forEach(index => rows[index]?.remove());
+ normalizeTableSections(tableElement);
+ updateTransaction(protyle, nodeElement, oldHTML);
+ return true;
+};
+
+export const deleteTableColumns = (
+ protyle: IProtyle,
+ nodeElement: HTMLElement,
+ columnIndexes: number[],
+) => {
+ const tableElement = nodeElement.querySelector("table");
+ if (!tableElement) {
+ return false;
+ }
+ const grid = buildTableGrid(tableElement);
+ if (grid.cellInfos.some(info => info.rowspan > 1 || info.colspan > 1)) {
+ return false;
+ }
+ const indexes = Array.from(new Set(columnIndexes))
+ .filter(index => index >= 0 && index < grid.columnCount)
+ .sort((a, b) => b - a);
+ if (indexes.length === 0) {
+ return false;
+ }
+ if (indexes.length >= grid.columnCount) {
+ const range = getEditorRange(nodeElement);
+ nodeElement.classList.add("protyle-wysiwyg--select");
+ removeBlock(protyle, nodeElement, range, "remove");
+ return true;
+ }
+ const oldHTML = nodeElement.outerHTML;
+ Array.from(tableElement.rows).forEach(row => {
+ indexes.forEach(index => row.cells[index]?.remove());
+ });
+ const columns = Array.from(tableElement.querySelectorAll("col"));
+ indexes.forEach(index => columns[index]?.remove());
+ updateTransaction(protyle, nodeElement, oldHTML);
+ return true;
+};
+
const getTableRangeBounds = (cellInfos: ITableCellInfo[], rowCount: number, startCell: HTMLElement, endCell: HTMLElement) => {
const startInfo = cellInfos.find(info => info.cell === startCell);
const endInfo = cellInfos.find(info => info.cell === endCell);
diff --git a/app/src/protyle/util/tableControl.ts b/app/src/protyle/util/tableControl.ts
index e699cc0c3b..cfc084c841 100644
--- a/app/src/protyle/util/tableControl.ts
+++ b/app/src/protyle/util/tableControl.ts
@@ -1,11 +1,14 @@
import {MenuItem} from "../../menus/Menu";
-import {removeBlock} from "../wysiwyg/remove";
import {updateTransaction} from "../wysiwyg/transaction";
import {encodeBase64, isMac} from "./compatibility";
import {removeZWJ} from "./normalizeText";
import {focusByRange, getEditorRange} from "./selection";
import {
buildTableGrid,
+ deleteTableColumns,
+ deleteTableRows,
+ getTableFullColumnSelection,
+ getTableFullRowSelection,
getTableRangeHTML,
ITableCellInfo,
ITableGrid,
@@ -971,6 +974,37 @@ export class TableControl {
accelerator: !mergedCell && !rectangle ? window.siyuan.languages.tableRectangleSelectionRequired : undefined,
click: () => mergedCell ? this.splitCell(cells[0]) : this.mergeCells(),
}).element);
+ const rowSelection = getTableFullRowSelection(this.selection.table, cells);
+ const columnSelection = getTableFullColumnSelection(this.selection.table, cells);
+ if (rowSelection.indexes.length > 0 || columnSelection.indexes.length > 0) {
+ window.siyuan.menus.menu.append(new MenuItem({type: "separator"}).element);
+ }
+ if (rowSelection.indexes.length > 0) {
+ window.siyuan.menus.menu.append(new MenuItem({
+ icon: "iconTrashcan",
+ label: window.siyuan.languages["delete-row"],
+ disabled: rowSelection.merged,
+ accelerator: rowSelection.merged ? window.siyuan.languages.cancelMerged : undefined,
+ click: () => {
+ if (deleteTableRows(this.protyle, this.selection.node, rowSelection.indexes)) {
+ this.clear();
+ }
+ },
+ }).element);
+ }
+ if (columnSelection.indexes.length > 0) {
+ window.siyuan.menus.menu.append(new MenuItem({
+ icon: "iconTrashcan",
+ label: window.siyuan.languages["delete-column"],
+ disabled: columnSelection.merged,
+ accelerator: columnSelection.merged ? window.siyuan.languages.cancelMerged : undefined,
+ click: () => {
+ if (deleteTableColumns(this.protyle, this.selection.node, columnSelection.indexes)) {
+ this.clear();
+ }
+ },
+ }).element);
+ }
}
private appendAlignmentMenus() {
@@ -1086,29 +1120,11 @@ export class TableControl {
private deleteRowsOrColumns() {
const selection = this.selection;
- const grid = buildTableGrid(selection.table);
- const total = selection.mode === "row" ? grid.rowCount : grid.columnCount;
- if (selection.indexes.size >= total) {
- const range = getEditorRange(selection.node);
- selection.node.classList.add("protyle-wysiwyg--select");
- removeBlock(this.protyle, selection.node, range, "remove");
- this.clear();
- return;
- }
- const oldHTML = selection.node.outerHTML;
- const indexes = Array.from(selection.indexes).sort((a, b) => b - a);
if (selection.mode === "row") {
- const rows = Array.from(selection.table.rows);
- indexes.forEach(index => rows[index]?.remove());
- this.normalizeTableSections(selection.table);
+ deleteTableRows(this.protyle, selection.node, Array.from(selection.indexes));
} else {
- Array.from(selection.table.rows).forEach(row => {
- indexes.forEach(index => row.cells[index]?.remove());
- });
- const columns = Array.from(selection.table.querySelectorAll("col"));
- indexes.forEach(index => columns[index]?.remove());
+ deleteTableColumns(this.protyle, selection.node, Array.from(selection.indexes));
}
- updateTransaction(this.protyle, selection.node, oldHTML);
this.clear();
}
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index 74b2e6f09d..b570dd143f 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -80,7 +80,17 @@ import {copyPlainText, encodeBase64, isInIOS, isMac, isOnlyMeta, readClipboard}
import {MenuItem} from "../../menus/Menu";
import {fetchPost, fetchSyncPost} from "../../util/fetch";
import {onGet} from "../util/onGet";
-import {clearTableCell, getTableRangeHTML, isIncludeCell, setTableAlign, updateTableTitle} from "../util/table";
+import {
+ clearTableCell,
+ deleteTableColumns,
+ deleteTableRows,
+ getTableFullColumnSelection,
+ getTableFullRowSelection,
+ getTableRangeHTML,
+ isIncludeCell,
+ setTableAlign,
+ updateTableTitle,
+} from "../util/table";
import {TableControl} from "../util/tableControl";
import {countBlockWord, countSelectWord} from "../../layout/status";
import {showMessage} from "../../dialog/message";
@@ -2358,15 +2368,6 @@ export class WYSIWYG {
}
}
}).element);
- window.siyuan.menus.menu.append(new MenuItem({
- id: "clear",
- label: window.siyuan.languages.clear,
- icon: "iconTrashcan",
- accelerator: "⌦",
- click() {
- clearTableCell(protyle, tableBlockElement as HTMLElement);
- }
- }).element);
window.siyuan.menus.menu.append(new MenuItem({
id: "paste",
label: window.siyuan.languages.paste,
@@ -2385,6 +2386,66 @@ export class WYSIWYG {
}
}
}).element);
+ window.siyuan.menus.menu.append(new MenuItem({
+ id: "clear",
+ label: window.siyuan.languages.clear,
+ icon: "iconTrashcan",
+ accelerator: "⌦",
+ click() {
+ clearTableCell(protyle, tableBlockElement as HTMLElement);
+ }
+ }).element);
+ const tableElement = tableBlockElement.querySelector("table");
+ const selectedCellElements: HTMLTableCellElement[] = [];
+ const scrollLeft = tableBlockElement.firstElementChild.scrollLeft;
+ const scrollTop = tableElement.scrollTop;
+ tableBlockElement.querySelectorAll("th, td").forEach((item: HTMLTableCellElement) => {
+ if (!item.classList.contains("fn__none") && isIncludeCell({
+ tableSelectElement,
+ scrollLeft,
+ scrollTop,
+ item,
+ })) {
+ selectedCellElements.push(item);
+ }
+ });
+ const rowSelection = getTableFullRowSelection(tableElement, selectedCellElements);
+ const columnSelection = getTableFullColumnSelection(tableElement, selectedCellElements);
+ if (rowSelection.indexes.length > 0 || columnSelection.indexes.length > 0) {
+ window.siyuan.menus.menu.append(new MenuItem({
+ id: "deleteRowsSeparator",
+ type: "separator",
+ }).element);
+ }
+ if (rowSelection.indexes.length > 0) {
+ window.siyuan.menus.menu.append(new MenuItem({
+ id: "deleteRows",
+ icon: "iconTrashcan",
+ label: window.siyuan.languages["delete-row"],
+ disabled: rowSelection.merged,
+ accelerator: rowSelection.merged ? window.siyuan.languages.cancelMerged : undefined,
+ click() {
+ tableSelectElement.removeAttribute("style");
+ deleteTableRows(protyle, tableBlockElement as HTMLElement,
+ rowSelection.indexes);
+ },
+ }).element);
+ }
+ if (columnSelection.indexes.length > 0) {
+ window.siyuan.menus.menu.append(new MenuItem({
+ id: "deleteColumns",
+ icon: "iconTrashcan",
+ label: window.siyuan.languages["delete-column"],
+ disabled: columnSelection.merged,
+ accelerator: columnSelection.merged ?
+ window.siyuan.languages.cancelMerged : undefined,
+ click() {
+ tableSelectElement.removeAttribute("style");
+ deleteTableColumns(protyle, tableBlockElement as HTMLElement,
+ columnSelection.indexes);
+ },
+ }).element);
+ }
}
window.siyuan.menus.menu.popup({x: mouseUpEvent.clientX - 8, y: mouseUpEvent.clientY - 16});
}