Skip to content

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

:bug: Split table cell paragraphs after consecutive soft breaks https://github.com/siyuan-note/siyuan/issues/20253

Daniel60446c516472
diff --git a/app/src/protyle/util/tableCellRichLute.test.ts b/app/src/protyle/util/tableCellRichLute.test.ts
index f6b44dbfc0..2a3a052484 100644
--- a/app/src/protyle/util/tableCellRichLute.test.ts
+++ b/app/src/protyle/util/tableCellRichLute.test.ts
@@ -169,12 +169,57 @@ const browserCases = async (source: string, enterSource: string, hintSource: str
check.equal(prefixed.firstElementChild.querySelector('[contenteditable="true"]').textContent, "first\n\nsecond");
check.ok(prefixed.querySelector('[data-type="strong"]'));
const plain = parse(paragraph("first\n\nsecond<wbr>"));
- check.equal(plain.childElementCount, 1);
- check.equal(plain.querySelector('[contenteditable="true"]').textContent, "first\n\nsecond");
+ check.equal(plain.childElementCount, 2);
+ check.deepEqual(Array.from(plain.children).map(block =>
+ block.querySelector('[contenteditable="true"]').textContent), ["first", "second"]);
+ for (const separator of ["\n\n", "\n\n\n", "\n \t\n"]) {
+ const divided = parse(paragraph("first" + separator + "second<wbr>"));
+ check.equal(divided.childElementCount, 2, separator);
+ check.deepEqual(Array.from(divided.children).map(block =>
+ block.querySelector('[contenteditable="true"]').textContent), ["first", "second"]);
+ check.ok(divided.lastElementChild.querySelector("wbr"), "the caret follows the paragraph split");
+ const stored = document.createElement("td");
+ api.updateTableCellEditingValue(stored, api.serializeTableCellRich(divided.innerHTML));
+ const reopened = document.createElement("div");
+ reopened.innerHTML = api.getTableCellRichBlockDOM(stored);
+ check.equal(reopened.childElementCount, 2, "the paragraph boundary survives save and reopen");
+ }
+ const singleBreak = parse(paragraph("first\nsecond<wbr>"));
+ check.equal(singleBreak.childElementCount, 1);
+ check.equal(singleBreak.querySelector('[contenteditable="true"]').textContent, "first\nsecond");
+ const storedSoftBreaks = document.createElement("td");
+ const storedSource = api.encodeTableCellRich("first<br /><br />second");
+ storedSoftBreaks.setAttribute("data-sy-table-cell-rich", storedSource);
+ const restoredSoftBreaks = parse(api.getTableCellRichBlockDOM(storedSoftBreaks));
+ check.equal(restoredSoftBreaks.childElementCount, 1, "reading existing soft breaks does not split paragraphs");
+ check.equal(restoredSoftBreaks.querySelector('[contenteditable="true"]').textContent, "first\n\nsecond");
+ api.updateTableCellEditingValue(storedSoftBreaks, api.serializeTableCellRich(restoredSoftBreaks.innerHTML));
+ check.equal(storedSoftBreaks.getAttribute("data-sy-table-cell-rich"), storedSource);
+ for (const source of ["```go\nfirst\n\nsecond\n```", "$$\nfirst\n\nsecond\n$$"]) {
+ const holder = document.createElement("div");
+ holder.innerHTML = base.Md2BlockDOM(source);
+ const editable = holder.querySelector('[contenteditable="true"]');
+ const mathSource = holder.firstElementChild.getAttribute("data-content");
+ (editable || holder.firstElementChild).insertAdjacentHTML("beforeend", "<wbr>");
+ const preserved = parse(holder.innerHTML);
+ check.equal(preserved.childElementCount, 1, "code and math retain their internal blank lines");
+ if (editable) {
+ check.match(preserved.querySelector('[contenteditable="true"]').textContent, /first\n\nsecond/);
+ } else {
+ check.equal(preserved.firstElementChild.getAttribute("data-content"), mathSource);
+ }
+ }
+ const formatted = parse(paragraph('<span data-type="strong">first</span>\n\n<span data-type="em">second<wbr></span>'));
+ check.equal(formatted.childElementCount, 2);
+ check.equal(formatted.firstElementChild.querySelector('[data-type="strong"]').textContent, "first");
+ check.equal(formatted.lastElementChild.querySelector('[data-type="em"]').textContent, "second");
+ const listParagraphs = parse(base.Md2BlockDOM("- first").replace("first", "first\n\nsecond<wbr>"));
+ check.equal(listParagraphs.querySelectorAll('[data-type="NodeListItem"]').length, 1);
+ check.equal(listParagraphs.querySelectorAll('[data-type="NodeParagraph"]').length, 2);
for (const [line, type] of [["# Heading", "NodeHeading"], ["- item", "NodeList"], ["[] task", "NodeList"]]) {
const converted = parse(paragraph("first\n\n" + line + "<wbr>"));
check.ok(converted.querySelector(`[data-type="${type}"] wbr`), line);
- check.equal(converted.firstElementChild.querySelector('[contenteditable="true"]').textContent, "first\n");
+ check.equal(converted.firstElementChild.querySelector('[contenteditable="true"]').textContent, "first");
}
const literal = parse(paragraph('<span data-type="text">```js\n```</span><wbr>'));
check.equal(literal.querySelector('[data-type="NodeCodeBlock"]'), null);
diff --git a/app/src/protyle/util/tableCellRichLute.ts b/app/src/protyle/util/tableCellRichLute.ts
index ca31d9daac..1ef59fb464 100644
--- a/app/src/protyle/util/tableCellRichLute.ts
+++ b/app/src/protyle/util/tableCellRichLute.ts
@@ -92,14 +92,17 @@ export const getTableCellEditorLute = (lute: Lute, enableFullWidthTaskList = fal
}
const protectedHTML = template.innerHTML;
if (boundary) {
- // 只尝试解析光标前最后一处换行,其他空行和软换行仍由占位符保护。
+ // 当前行前的连续换行一起参与解析,空行可分段,其他位置的软换行仍由占位符保护。
const protectedText = boundary.textContent;
const offset = protectedText.lastIndexOf(token);
- boundary.textContent = protectedText.substring(0, offset) + "\n" +
+ const precedingBreaks = protectedText.substring(0, offset).match(new RegExp(`(?:${token}[ \\t]*)+$`))?.[0] || "";
+ const separatorStart = offset - precedingBreaks.length;
+ const separator = (precedingBreaks + token).split(token).join("\n");
+ boundary.textContent = protectedText.substring(0, separatorStart) + separator +
protectedText.substring(offset + token.length);
// 将当前行的快捷待办标记转为解析器语法,保留前文和标记后的行内内容。
const lineRange = document.createRange();
- lineRange.setStart(boundary, offset + 1);
+ lineRange.setStart(boundary, separatorStart + separator.length);
lineRange.setEnd(editable, editable.childNodes.length);
const line = document.createElement("div");
line.appendChild(lineRange.cloneContents());
@@ -116,7 +119,11 @@ export const getTableCellEditorLute = (lute: Lute, enableFullWidthTaskList = fal
const parsed = document.createElement("template");
parsed.innerHTML = candidate;
const parsedMarker = parsed.content.querySelector("wbr");
- if (parsedMarker && getBlockPath(parsedMarker) !== blockPath) {
+ const splitParagraph = /\n[ \t]*\n/.test(separator) &&
+ editable.parentElement.getAttribute("data-type") === "NodeParagraph" &&
+ parsed.content.querySelectorAll('[data-type="NodeParagraph"]').length >
+ template.content.querySelectorAll('[data-type="NodeParagraph"]').length;
+ if (parsedMarker && (splitParagraph || getBlockPath(parsedMarker) !== blockPath)) {
return candidate.split(token).join("&#10;");
}
}