From 1f918e07733ffa6896956593beddba2908f1aa9f Mon Sep 17 00:00:00 2001 From: Vanessa Date: Wed, 12 Jun 2024 10:55:26 +0800 Subject: [PATCH] :art: fix https://github.com/siyuan-note/siyuan/issues/11694 --- app/src/protyle/util/selection.ts | 5 +++-- app/src/protyle/util/table.ts | 5 +++-- app/src/protyle/wysiwyg/keydown.ts | 5 +++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/src/protyle/util/selection.ts b/app/src/protyle/util/selection.ts index 85b6db3a4..925dab49b 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 c19409e99..d14696573 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 68e26cced..845356214 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);