Vanessa 2024-06-12 10:55:26 +08:00
parent 11df239bd2
commit 1f918e0773
3 changed files with 9 additions and 6 deletions

View file

@ -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;
};

View file

@ -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;

View file

@ -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);