Skip to content

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

:bug: Keep empty table cell editing consistent and compact https://github.com/siyuan-note/siyuan/issues/19188

Daniel09e2de04af36
diff --git a/app/src/assets/scss/protyle/_protyle.scss b/app/src/assets/scss/protyle/_protyle.scss
index 55862f861e..af15ba66dd 100644
--- a/app/src/assets/scss/protyle/_protyle.scss
+++ b/app/src/assets/scss/protyle/_protyle.scss
@@ -857,6 +857,7 @@
.table__cell-editor {
min-width: 180px;
+ min-height: 0;
white-space: normal;
.protyle-content {
@@ -869,6 +870,31 @@
}
}
+.table__cell-rich.table__cell--inline,
+.table__cell-editor.table__cell--inline {
+ min-width: 0;
+ min-height: 0;
+ padding: 0 !important;
+
+ &,
+ .protyle-wysiwyg {
+ padding: 0 !important;
+ min-height: 0;
+
+ > .p {
+ margin: 0;
+ padding: 0;
+ line-height: inherit;
+ }
+ }
+}
+
+.table__cell-rich.table__cell--empty > .p,
+.table__cell-editor.table__cell--empty .protyle-wysiwyg > .p {
+ // 空单元格的光标占位与表格 td:empty 的 16px 占位高度一致。
+ line-height: 16px;
+}
+
table[contenteditable="false"] + .protyle-action__table > .table__resize {
display: none !important;
}
diff --git a/app/src/protyle/render/tableCellRich.ts b/app/src/protyle/render/tableCellRich.ts
index 4187f11ad8..ce9e631248 100644
--- a/app/src/protyle/render/tableCellRich.ts
+++ b/app/src/protyle/render/tableCellRich.ts
@@ -1,6 +1,6 @@
import {highlightRender} from "./highlightRender";
import {mathRender} from "./mathRender";
-import {getTableCellRichBlockDOM} from "../util/tableCellRich";
+import {getTableCellRichBlockDOM, updateTableCellContentLayout} from "../util/tableCellRich";
import {TABLE_CELL_RICH_ATTRIBUTE} from "../util/tableCellRichValue";
export const renderTableCellRichElements = (root: Element) => {
@@ -16,6 +16,7 @@ export const renderTableCellRichElements = (root: Element) => {
// 预览复用编辑结构,但不携带文档块身份或编辑能力。
const template = document.createElement("template");
template.innerHTML = getTableCellRichBlockDOM(cell);
+ updateTableCellContentLayout(element, template.innerHTML);
template.content.querySelectorAll<HTMLElement>("*").forEach(node => {
if (node.hasAttribute("data-node-id")) {
node.setAttribute("data-table-cell-node", "");
diff --git a/app/src/protyle/render/tableCellRichEditor.ts b/app/src/protyle/render/tableCellRichEditor.ts
index 4bccf5ee53..ea3cf8c206 100644
--- a/app/src/protyle/render/tableCellRichEditor.ts
+++ b/app/src/protyle/render/tableCellRichEditor.ts
@@ -13,6 +13,7 @@ import {cleanTableCellRichHTML, getTableCellInlineHTML, getTableCellRichBlockDOM
import {TABLE_CELL_RICH_ATTRIBUTE} from "../util/tableCellRichValue";
import {focusByOffset, getSelectionOffset} from "../util/selection";
import {fixTable} from "../util/table";
+import {updateTableCellContentLayout} from "../util/tableCellRich";
const SAFE_SLASH_IDS = new Set([
"ref", "heading1", "heading2", "heading3", "heading4", "heading5", "heading6", "list", "orderedList", "check",
@@ -130,7 +131,6 @@ export const openTableCellRichEditor = (owner: IProtyle, cell: HTMLTableCellElem
const fragment = mountProtyleLiteFragment(host, {
app: owner.app,
initialBlockHTML,
- placeholder: window.siyuan.languages.empty,
protyleOptions: {notebookId: owner.notebookId, toolbar, hint},
runtimeCapabilities: {
upload: false,
@@ -143,11 +143,13 @@ export const openTableCellRichEditor = (owner: IProtyle, cell: HTMLTableCellElem
restoreLuteMarkdownSyntax: configureAVRichTextLute,
},
afterSetContent: (protyle, element) => {
+ updateTableCellContentLayout(host, element.innerHTML);
highlightRender(element);
mathRender(element);
protyle.undo.clear();
},
onChange: () => {
+ updateTableCellContentLayout(host, fragment.getBlockHTML());
window.clearTimeout(timer);
if (!finished && !composing) {
timer = window.setTimeout(commit, 200);
diff --git a/app/src/protyle/util/tableCellRich.ts b/app/src/protyle/util/tableCellRich.ts
index cf997f38c8..358974897c 100644
--- a/app/src/protyle/util/tableCellRich.ts
+++ b/app/src/protyle/util/tableCellRich.ts
@@ -3,6 +3,16 @@ import {decodeTableCellRich, encodeTableCellRich, TABLE_CELL_RICH_ATTRIBUTE, TAB
export const TABLE_CELL_INLINE_ATTRIBUTE = "data-sy-table-cell-inline";
+export const updateTableCellContentLayout = (element: HTMLElement, blockDOM: string) => {
+ const inline = getTableCellInlineHTML(blockDOM);
+ element.classList.toggle("table__cell--inline", inline !== null);
+ const template = document.createElement("template");
+ template.innerHTML = inline || "";
+ const empty = inline !== null && (template.content.textContent || "").replace(/\u200b/g, "") === "" &&
+ !template.content.querySelector("br, img, [data-type~='inline-math']");
+ element.classList.toggle("table__cell--empty", empty);
+};
+
export const getTableCellInlineHTML = (blockDOM: string): string | null => {
const template = document.createElement("template");
template.innerHTML = blockDOM;
@@ -42,6 +52,10 @@ export const getTableCellRichBlockDOM = (cell: Element) => {
`${(cell.getAttribute(TABLE_CELL_INLINE_ATTRIBUTE) ?? cell.innerHTML) || "\u200b"}</div></div>`;
const template = document.createElement("template");
template.innerHTML = blockDOM;
+ if (!template.content.childNodes.length) {
+ template.innerHTML = `<div class="p" data-type="NodeParagraph" data-node-id="${Lute.NewNodeID()}">` +
+ '<div contenteditable="true">\u200b</div></div>';
+ }
const walker = document.createTreeWalker(template.content, NodeFilter.SHOW_TEXT);
const texts: Text[] = [];
while (walker.nextNode()) {
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index ede055ff47..58c6a2c220 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -3876,6 +3876,20 @@ export class WYSIWYG {
}
return keyState;
};
+ this.element.addEventListener("keyup", (event: KeyboardEvent) => {
+ if (event.isComposing || protyle.disabled || event.ctrlKey || event.metaKey || event.altKey ||
+ !["Tab", "Enter", "ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"].includes(event.key) ||
+ (event.shiftKey && event.key !== "Tab")) {
+ return;
+ }
+ const selection = getSelection();
+ const target = selection?.focusNode;
+ const element = target instanceof Element ? target : target?.parentElement;
+ const cell = element?.closest<HTMLTableCellElement>("th, td");
+ if (cell && cell.closest(".protyle-wysiwyg") === this.element && cell.contains(selection.anchorNode)) {
+ void import("../render/tableCellRichEditor").then(module => module.openTableCellRichEditor(protyle, cell));
+ }
+ });
this.element.addEventListener("keydown", (event: KeyboardEvent) => {
if (event.key === "F2" && !event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey &&
!event.isComposing && !protyle.disabled) {