Skip to content

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

:sparkles: Support table row and whole-table alignment https://github.com/siyuan-note/siyuan/issues/18489

Danielef2b0ad0bb39
diff --git a/app/src/config/entryVisibility/catalog.test.ts b/app/src/config/entryVisibility/catalog.test.ts
index d99e3d3c17..e09adfb8ed 100644
--- a/app/src/config/entryVisibility/catalog.test.ts
+++ b/app/src/config/entryVisibility/catalog.test.ts
@@ -29,6 +29,10 @@ test("simple profile follows the reviewed defaults", () => {
"gutter.single.turnInto.code",
"gutter.single.table.tableHeaderRow",
"gutter.single.table.tableHeaderColumn",
+ "gutter.single.table.alignTop",
+ "gutter.single.table.alignMiddle",
+ "gutter.single.table.alignBottom",
+ "gutter.single.table.useDefaultVerticalAlign",
"inline.text.more.tableHeaderRow",
"inline.text.more.tableHeaderColumn",
"document.more.keepLazyLoad",
diff --git a/app/src/config/entryVisibility/catalog.ts b/app/src/config/entryVisibility/catalog.ts
index c20d28d02d..aaf0d9c0ab 100644
--- a/app/src/config/entryVisibility/catalog.ts
+++ b/app/src/config/entryVisibility/catalog.ts
@@ -208,6 +208,10 @@ const gutterCommon = () => [
node("alignCenter", lang("alignCenter")),
node("alignRight", lang("alignRight")),
node("useDefaultAlign", lang("useDefaultAlign")),
+ node("alignTop", lang("alignTop")),
+ node("alignMiddle", lang("alignMiddle")),
+ node("alignBottom", lang("alignBottom")),
+ node("useDefaultVerticalAlign", lang("useDefaultVerticalAlign")),
node("insertRowAbove", lang("insertRowAbove")),
node("insertRowBelow", lang("insertRowBelow")),
node("insertColumnLeft", lang("insertColumnLeft")),
diff --git a/app/src/menus/protyle.ts b/app/src/menus/protyle.ts
index c84d99bb54..5e8d7afec8 100644
--- a/app/src/menus/protyle.ts
+++ b/app/src/menus/protyle.ts
@@ -7,6 +7,7 @@ import {
isInEmbedBlock
} from "../protyle/util/hasClosest";
import {MenuItem} from "./Menu";
+import {getTableCellVerticalAlignmentMenus, setTableCellStyle} from "../protyle/util/tableControl";
import {focusBlock, focusByRange, focusByWbr, getEditorRange, selectAll,} from "../protyle/util/selection";
import {
deleteColumn,
@@ -2181,9 +2182,10 @@ export const videoMenu = (protyle: IProtyle, nodeElement: Element, type: string)
};
export const tableMenu = (protyle: IProtyle, nodeElement: Element, cellElement: HTMLTableCellElement, range: Range,
- hideTitle = false) => {
+ hideTitle = false, alignWholeTable = false) => {
const otherMenus: IMenu[] = [];
const colIndex = getColIndex(cellElement);
+ const tableElement = nodeElement.querySelector("table");
if (cellElement.rowSpan > 1 || cellElement.colSpan > 1) {
otherMenus.push({
id: "cancelMerged",
@@ -2294,13 +2296,14 @@ export const tableMenu = (protyle: IProtyle, nodeElement: Element, cellElement:
}
});
otherMenus.push({id: "separator_1", type: "separator"});
+ const horizontalCells = alignWholeTable ? Array.from(tableElement.rows[0].cells) : [cellElement];
otherMenus.push({
id: "alignLeft",
icon: "iconAlignLeft",
accelerator: window.siyuan.config.keymap.editor.general.alignLeft.custom,
label: window.siyuan.languages.alignLeft,
click: () => {
- setTableAlign(protyle, [cellElement], nodeElement, "left", range);
+ setTableAlign(protyle, horizontalCells, nodeElement, "left", range, alignWholeTable);
}
});
otherMenus.push({
@@ -2309,7 +2312,7 @@ export const tableMenu = (protyle: IProtyle, nodeElement: Element, cellElement:
label: window.siyuan.languages.alignCenter,
accelerator: window.siyuan.config.keymap.editor.general.alignCenter.custom,
click: () => {
- setTableAlign(protyle, [cellElement], nodeElement, "center", range);
+ setTableAlign(protyle, horizontalCells, nodeElement, "center", range, alignWholeTable);
}
});
otherMenus.push({
@@ -2318,7 +2321,7 @@ export const tableMenu = (protyle: IProtyle, nodeElement: Element, cellElement:
label: window.siyuan.languages.alignRight,
accelerator: window.siyuan.config.keymap.editor.general.alignRight.custom,
click: () => {
- setTableAlign(protyle, [cellElement], nodeElement, "right", range);
+ setTableAlign(protyle, horizontalCells, nodeElement, "right", range, alignWholeTable);
}
});
otherMenus.push({
@@ -2326,15 +2329,21 @@ export const tableMenu = (protyle: IProtyle, nodeElement: Element, cellElement:
icon: "",
label: window.siyuan.languages.useDefaultAlign,
click: () => {
- setTableAlign(protyle, [cellElement], nodeElement, "", range);
+ setTableAlign(protyle, horizontalCells, nodeElement, "", range, alignWholeTable);
}
});
+ if (alignWholeTable) {
+ otherMenus.push({id: "separator_verticalAlign", type: "separator"});
+ const cells = Array.from(tableElement.querySelectorAll<HTMLTableCellElement>("th, td"));
+ otherMenus.push(...getTableCellVerticalAlignmentMenus(cells, (property, value) => {
+ setTableCellStyle(protyle, nodeElement as HTMLElement, cells, property, value);
+ }));
+ }
const menus: IMenu[] = [];
menus.push(...otherMenus);
menus.push({
type: "separator"
});
- const tableElement = nodeElement.querySelector("table");
const hasNone = cellElement.parentElement.querySelector(".fn__none");
let hasColSpan = false;
let hasRowSpan = false;
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts
index f8b85ad69d..8813a85253 100644
--- a/app/src/protyle/gutter/index.ts
+++ b/app/src/protyle/gutter/index.ts
@@ -2052,7 +2052,7 @@ export class Gutter {
icon: "iconTable",
label: window.siyuan.languages.table,
submenu: tableMenu(protyle, nodeElement, cellElement as HTMLTableCellElement, range,
- !isEntryVisible("gutter.single.table.title")).menus as IMenu[]
+ !isEntryVisible("gutter.single.table.title"), true).menus as IMenu[]
}).element);
}
} else if (type === "NodeAttributeView") {
diff --git a/app/src/protyle/util/table.ts b/app/src/protyle/util/table.ts
index cd71dff5f3..6731cc207b 100644
--- a/app/src/protyle/util/table.ts
+++ b/app/src/protyle/util/table.ts
@@ -87,11 +87,20 @@ const goPreviousCell = (cellElement: HTMLElement, range: Range, isSelected = tru
return previousElement;
};
-export const setTableAlign = (protyle: IProtyle, cellElements: HTMLElement[], nodeElement: Element, type: string, range: Range) => {
+export const setTableAlign = (protyle: IProtyle, cellElements: HTMLElement[], nodeElement: Element, type: string,
+ range: Range, clearCellStyle = false) => {
range.insertNode(document.createElement("wbr"));
const html = nodeElement.outerHTML;
const tableElement = nodeElement.querySelector("table");
+ if (clearCellStyle) {
+ tableElement.querySelectorAll<HTMLElement>("th, td").forEach(cell => {
+ cell.style.removeProperty("text-align");
+ if (!cell.getAttribute("style")) {
+ cell.removeAttribute("style");
+ }
+ });
+ }
const columnCnt = tableElement.rows[0].cells.length;
const rowCnt = tableElement.rows.length;
const currentColumns: number[] = [];
diff --git a/app/src/protyle/util/tableControl.ts b/app/src/protyle/util/tableControl.ts
index 8ca668474d..42e3a54170 100644
--- a/app/src/protyle/util/tableControl.ts
+++ b/app/src/protyle/util/tableControl.ts
@@ -209,7 +209,6 @@ export const getTableCellAlignmentMenus = (
onChange: (property: string, value: string) => void,
): IMenu[] => {
const textAlign = getCommonTableCellStyle(cells, "text-align");
- const verticalAlign = getCommonTableCellStyle(cells, "vertical-align");
return [{
id: "alignLeft",
icon: "iconAlignLeft",
@@ -238,19 +237,31 @@ export const getTableCellAlignmentMenus = (
click: () => onChange("text-align", ""),
}, {
type: "separator",
- }, {
+ }, ...getTableCellVerticalAlignmentMenus(cells, onChange)];
+};
+
+export const getTableCellVerticalAlignmentMenus = (
+ cells: HTMLTableCellElement[],
+ onChange: (property: string, value: string) => void,
+): IMenu[] => {
+ const verticalAlign = getCommonTableCellStyle(cells, "vertical-align");
+ return [{
+ id: "alignTop",
label: window.siyuan.languages.alignTop,
checked: verticalAlign === "top",
click: () => onChange("vertical-align", "top"),
}, {
+ id: "alignMiddle",
label: window.siyuan.languages.alignMiddle,
checked: verticalAlign === "middle",
click: () => onChange("vertical-align", "middle"),
}, {
+ id: "alignBottom",
label: window.siyuan.languages.alignBottom,
checked: verticalAlign === "bottom",
click: () => onChange("vertical-align", "bottom"),
}, {
+ id: "useDefaultVerticalAlign",
label: window.siyuan.languages.useDefaultVerticalAlign,
checked: verticalAlign === "",
click: () => onChange("vertical-align", ""),
@@ -1280,6 +1291,8 @@ export class TableControl {
} else {
if (this.selection.mode === "column") {
this.appendAlignmentMenus();
+ } else {
+ this.appendVerticalAlignmentMenus();
}
menu.append(new MenuItem({type: "separator"}).element);
menu.append(new MenuItem({
@@ -1579,6 +1592,14 @@ export class TableControl {
}).element);
}
+ private appendVerticalAlignmentMenus() {
+ window.siyuan.menus.menu.append(new MenuItem({type: "separator"}).element);
+ getTableCellVerticalAlignmentMenus(this.getSelectedCells(),
+ (property, value) => this.setCellStyle(property, value)).forEach(item => {
+ window.siyuan.menus.menu.append(new MenuItem(item).element);
+ });
+ }
+
private getCommonCellStyle(property: string) {
return getCommonTableCellStyle(this.getSelectedCells(), property);
}