Skip to content

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

:art: Align rich table cell previews with editing styles https://github.com/siyuan-note/siyuan/issues/19188

Daniel20d72bddd38c
diff --git a/app/src/assets/scss/component/_typography.scss b/app/src/assets/scss/component/_typography.scss
index 33e32eb38d..f43518e47a 100644
--- a/app/src/assets/scss/component/_typography.scss
+++ b/app/src/assets/scss/component/_typography.scss
@@ -11,7 +11,8 @@
}
.b3-typography,
-.protyle-wysiwyg {
+.protyle-wysiwyg,
+.table__cell-rich {
font-variant-ligatures: no-common-ligatures;
display: flex;
flex-direction: column;
@@ -27,7 +28,8 @@
li > p,
& > p,
blockquote > p,
- [data-node-id] {
+ [data-node-id],
+ [data-table-cell-node] {
line-height: 1.625;
padding: 4px;
margin: 2px 0;
diff --git a/app/src/assets/scss/protyle/_protyle.scss b/app/src/assets/scss/protyle/_protyle.scss
index 52e397a655..55862f861e 100644
--- a/app/src/assets/scss/protyle/_protyle.scss
+++ b/app/src/assets/scss/protyle/_protyle.scss
@@ -838,29 +838,20 @@
}
.table__cell-rich {
+ min-width: 180px;
min-height: 1.6em;
white-space: normal;
cursor: text;
+ padding: 4px !important;
- &.b3-typography {
- font-size: inherit;
- line-height: inherit;
- }
-
- > :first-child {
- margin-top: 0;
- }
-
- > :last-child {
- margin-bottom: 0;
- }
-
- p {
- margin: 0.25em 0;
+ .protyle-action {
+ pointer-events: none;
}
- pre {
- white-space: pre-wrap;
+ .protyle-icons,
+ .protyle-icon,
+ .protyle-action__language {
+ display: none !important;
}
}
@@ -875,8 +866,6 @@
.protyle-wysiwyg {
padding: 4px !important;
min-height: 1.6em;
- box-shadow: inset 0 0 0 1px var(--b3-theme-primary);
- border-radius: var(--b3-border-radius);
}
}
diff --git a/app/src/assets/scss/protyle/_wysiwyg.scss b/app/src/assets/scss/protyle/_wysiwyg.scss
index 44fd2cadee..e2abd13b90 100644
--- a/app/src/assets/scss/protyle/_wysiwyg.scss
+++ b/app/src/assets/scss/protyle/_wysiwyg.scss
@@ -15,7 +15,8 @@
pointer-events: none;
}
-.protyle-wysiwyg {
+.protyle-wysiwyg,
+.table__cell-rich {
padding: 16px 16px 16px 24px;
box-sizing: border-box;
// 移除三击无法选中整个 https://github.com/siyuan-note/siyuan/issues/8511
@@ -44,7 +45,8 @@
cursor: text;
}
- [data-node-id] {
+ [data-node-id],
+ [data-table-cell-node] {
position: relative;
&.protyle-wysiwyg--navigation {
@@ -181,11 +183,11 @@
border-left-color: var(--b3-scroll-color);
}
- & > [data-node-id] {
+ & > :is([data-node-id], [data-table-cell-node]) {
margin-left: 34px;
}
- & > [data-node-id]:not(.p):not(.code-block) {
+ & > :is([data-node-id], [data-table-cell-node]):not(.p):not(.code-block) {
padding-right: 0;
}
@@ -303,8 +305,8 @@
direction: rtl;
}
- .li > [data-node-id],
- & > [data-node-id] {
+ .li > :is([data-node-id], [data-table-cell-node]),
+ & > :is([data-node-id], [data-table-cell-node]) {
margin-right: 34px;
margin-left: 0;
}
diff --git a/app/src/protyle/render/tableCellRich.ts b/app/src/protyle/render/tableCellRich.ts
index 5f162309af..4187f11ad8 100644
--- a/app/src/protyle/render/tableCellRich.ts
+++ b/app/src/protyle/render/tableCellRich.ts
@@ -1,5 +1,7 @@
import {highlightRender} from "./highlightRender";
import {mathRender} from "./mathRender";
+import {getTableCellRichBlockDOM} from "../util/tableCellRich";
+import {TABLE_CELL_RICH_ATTRIBUTE} from "../util/tableCellRichValue";
export const renderTableCellRichElements = (root: Element) => {
const elements = [...root.querySelectorAll<HTMLElement>(".table__cell-rich")];
@@ -7,20 +9,26 @@ export const renderTableCellRichElements = (root: Element) => {
elements.unshift(root as HTMLElement);
}
elements.forEach(element => {
- element.classList.add("b3-typography");
- element.dataset.protyleLiteRender = "safe";
- element.querySelectorAll<HTMLElement>("div.language-math").forEach(math => {
- math.dataset.content = math.textContent || "";
- math.className = "render-node";
- math.dataset.subtype = "math";
- math.textContent = "";
- });
- element.querySelectorAll<HTMLElement>("pre > code").forEach(code => {
- code.parentElement.classList.add("code-block");
- code.parentElement.dataset.language = Array.from(code.classList)
- .find(name => name.startsWith("language-"))?.slice(9) || "plaintext";
+ const cell = element.closest(`th[${TABLE_CELL_RICH_ATTRIBUTE}], td[${TABLE_CELL_RICH_ATTRIBUTE}]`);
+ if (!cell) {
+ return;
+ }
+ // 预览复用编辑结构,但不携带文档块身份或编辑能力。
+ const template = document.createElement("template");
+ template.innerHTML = getTableCellRichBlockDOM(cell);
+ template.content.querySelectorAll<HTMLElement>("*").forEach(node => {
+ if (node.hasAttribute("data-node-id")) {
+ node.setAttribute("data-table-cell-node", "");
+ }
+ ["id", "data-node-id", "data-node-index", "updated", "spellcheck", "draggable"].forEach(attribute =>
+ node.removeAttribute(attribute));
+ if (node.hasAttribute("contenteditable")) {
+ node.contentEditable = "false";
+ }
});
- element.querySelectorAll<HTMLInputElement>('input[type="checkbox"]').forEach(input => input.disabled = true);
+ element.replaceChildren(template.content);
+ element.classList.remove("b3-typography");
+ element.dataset.protyleLiteRender = "safe";
mathRender(element);
highlightRender(element);
});
diff --git a/app/src/protyle/render/tableCellRichEditor.ts b/app/src/protyle/render/tableCellRichEditor.ts
index ee398a6b58..0f2709b6a9 100644
--- a/app/src/protyle/render/tableCellRichEditor.ts
+++ b/app/src/protyle/render/tableCellRichEditor.ts
@@ -3,6 +3,7 @@ import {showMessage} from "../../dialog/message";
import {hintRef, hintSlash} from "../hint/extend";
import {mountProtyleLiteFragment} from "../lite/fragmentEditor";
import {getDefaultToolbar} from "../toolbar/defaults";
+import {hideElements} from "../ui/hideElements";
import {updateTransaction} from "../wysiwyg/transaction";
import {configureAVRichTextLute, getAVRichTextLute, sanitizeAVRichTextBlockDOM} from "./av/richText";
import {highlightRender} from "./highlightRender";
@@ -89,6 +90,7 @@ export const openTableCellRichEditor = (owner: IProtyle, cell: HTMLTableCellElem
}
owner.wysiwyg.tableControl?.clear();
owner.wysiwyg.tableControl?.setHidden(true);
+ hideElements(["gutter"], owner);
table.setAttribute(TABLE_RICH_ATTRIBUTE, "1");
const initialHTML = table.outerHTML;
if (!cell.hasAttribute(TABLE_CELL_RICH_ATTRIBUTE)) {
@@ -106,6 +108,10 @@ export const openTableCellRichEditor = (owner: IProtyle, cell: HTMLTableCellElem
"copy", "cut", "paste", "pointerdown", "pointerup", "pointermove", "mousedown", "mouseup", "mousemove",
"click", "dblclick", "contextmenu", "dragstart", "dragover", "drop", "focusin", "focusout"];
events.forEach(type => host.addEventListener(type, event => event.stopPropagation()));
+ ["mouseover", "pointerover"].forEach(type => host.addEventListener(type, event => {
+ hideElements(["gutter"], owner);
+ event.stopPropagation();
+ }));
const toolbar = getDefaultToolbar(isMobile()).filter(item => typeof item === "string" ? item !== "ai" : item.name !== "ai");
const safeSlash = (key: string, protyle: IProtyle, hintSource: THintSource) =>
hintSlash(key, protyle, hintSource).filter(item => SAFE_SLASH_IDS.has(item.id));