Skip to content

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

:art: fix https://github.com/siyuan-note/siyuan/issues/11694

Vanessa1f918e07733f
diff --git a/app/src/protyle/util/selection.ts b/app/src/protyle/util/selection.ts
index 85b6db3a47..925dab49b8 100644
--- a/app/src/protyle/util/selection.ts
+++ b/app/src/protyle/util/selection.ts
@@ -261,8 +261,9 @@ export const getSelectionOffset = (selectElement: Node, editorElement?: Element,
preSelectionRange.selectNodeContents(selectElement);
}
preSelectionRange.setEnd(range.startContainer, range.startOffset);
- position.start = preSelectionRange.toString().length;
- position.end = position.start + range.toString().length;
+ // 需加上表格内软换行 br 的长度
+ position.start = preSelectionRange.toString().length + preSelectionRange.cloneContents().querySelectorAll("br").length;
+ position.end = position.start + range.toString().length + range.cloneContents().querySelectorAll("br").length;
return position;
};
diff --git a/app/src/protyle/util/table.ts b/app/src/protyle/util/table.ts
index c19409e999..d146965731 100644
--- a/app/src/protyle/util/table.ts
+++ b/app/src/protyle/util/table.ts
@@ -6,6 +6,7 @@ import {isNotCtrl} from "./compatibility";
import {scrollCenter} from "../../util/highlightById";
import {insertEmptyBlock} from "../../block/util";
import {removeBlock} from "../wysiwyg/remove";
+import {hasPreviousSibling} from "../wysiwyg/getBlock";
const scrollToView = (nodeElement: Element, rowElement: HTMLElement, protyle: IProtyle) => {
if (nodeElement.getAttribute("custom-pinthead") === "true") {
@@ -463,14 +464,14 @@ export const fixTable = (protyle: IProtyle, event: KeyboardEvent, range: Range)
const startContainer = range.startContainer as HTMLElement;
let previousBrElement;
if (startContainer.nodeType !== 3 && (startContainer.tagName === "TH" || startContainer.tagName === "TD")) {
- previousBrElement = (startContainer.childNodes[Math.max(0, range.startOffset - 1)] as HTMLElement)?.previousElementSibling;
+ previousBrElement = (startContainer.childNodes[Math.min(range.startOffset, startContainer.childNodes.length - 1)] as HTMLElement);
} else if (startContainer.parentElement.tagName === "SPAN") {
previousBrElement = startContainer.parentElement.previousElementSibling;
} else {
previousBrElement = startContainer.previousElementSibling;
}
while (previousBrElement) {
- if (previousBrElement.tagName === "BR") {
+ if (previousBrElement.tagName === "BR" && hasPreviousSibling(previousBrElement)) {
return false;
}
previousBrElement = previousBrElement.previousElementSibling;
diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts
index 68e26cced9..8453562144 100644
--- a/app/src/protyle/wysiwyg/keydown.ts
+++ b/app/src/protyle/wysiwyg/keydown.ts
@@ -622,7 +622,8 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
const tdElement = hasClosestByMatchTag(range.startContainer, "TD") || hasClosestByMatchTag(range.startContainer, "TH");
const nodeEditableElement = (tdElement || getContenteditableElement(nodeElement) || nodeElement) as HTMLElement;
const position = getSelectionOffset(nodeEditableElement, protyle.wysiwyg.element, range);
- if (event.key === "ArrowDown" && nodeEditableElement?.textContent.trimRight().substr(position.start).indexOf("\n") === -1 && (
+ // 需使用 innerText 否则表格内 br 无法传唤为 /n
+ if (event.key === "ArrowDown" && nodeEditableElement?.innerText.trimRight().substr(position.start).indexOf("\n") === -1 && (
(tdElement && !tdElement.parentElement.nextElementSibling && nodeElement.getAttribute("data-type") === "NodeTable" && !getNextBlock(nodeElement)) ||
(nodeElement.getAttribute("data-type") === "NodeCodeBlock" && !getNextBlock(nodeElement)) ||
(nodeElement.parentElement.getAttribute("data-type") === "NodeBlockquote" && nodeElement.nextElementSibling.classList.contains("protyle-attr") && !getNextBlock(nodeElement.parentElement))
@@ -697,7 +698,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
}
}
if (event.key === "ArrowDown") {
- if (nodeEditableElement?.textContent.trimRight().substr(position.start).indexOf("\n") === -1 &&
+ if (nodeEditableElement?.innerText.trimRight().substr(position.start).indexOf("\n") === -1 &&
nodeElement.isSameNode(protyle.wysiwyg.element.lastElementChild)) {
setLastNodeRange(getContenteditableElement(nodeEditableElement), range, false);
range.collapse(false);