diff --git a/app/src/protyle/wysiwyg/getBlock.ts b/app/src/protyle/wysiwyg/getBlock.ts index 3c9dbabb2..d33ea8a9f 100644 --- a/app/src/protyle/wysiwyg/getBlock.ts +++ b/app/src/protyle/wysiwyg/getBlock.ts @@ -163,6 +163,26 @@ export const hasNextSibling = (element: Node) => { return false; }; +export const isEndOfBlock = (range: Range) => { + if (range.endContainer.textContent.length !== range.endOffset) { + return false; + } + + let nextSibling = range.endContainer + while (nextSibling) { + if (hasNextSibling(nextSibling)) { + return false; + } else { + if (nextSibling.parentElement.getAttribute("spellcheck")) { + return true; + } + nextSibling = nextSibling.parentElement + } + } + + return true; +} + export const hasPreviousSibling = (element: Node) => { let previousSibling = element.previousSibling; while (previousSibling) { diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts index a5f52816a..c19f810cb 100644 --- a/app/src/protyle/wysiwyg/index.ts +++ b/app/src/protyle/wysiwyg/index.ts @@ -41,7 +41,7 @@ import { getNextBlock, getTopAloneElement, hasNextSibling, - hasPreviousSibling, + hasPreviousSibling, isEndOfBlock, isNotEditBlock } from "./getBlock"; import {transaction, updateTransaction} from "./transaction"; @@ -418,8 +418,7 @@ export class WYSIWYG { // 不能使用 commonAncestorContainer https://ld246.com/article/1643282894693 textPlain = tempElement.textContent; if (hasClosestByAttribute(range.startContainer, "data-type", "NodeCodeBlock")) { - if (range.endContainer.textContent.length === range.endOffset && - (range.endContainer.parentElement.getAttribute("spellcheck") ? !range.endContainer.nextSibling : !range.endContainer.parentElement.nextSibling)) { + if (isEndOfBlock(range)) { textPlain = textPlain.replace(/\n$/, ""); } } else if (!hasClosestByMatchTag(range.startContainer, "CODE")) { @@ -2155,8 +2154,7 @@ export class WYSIWYG { } // 按下方向键后块高亮跟随光标移动 https://github.com/siyuan-note/siyuan/issues/8918 - if ((event.key === "ArrowLeft" || event.key === "ArrowRight" || - event.key === "Alt" || event.key === "Shift") && // 选中后 alt+shift+arrowRight 会导致光标和选中块不一致 + if ((event.key === "ArrowLeft" || event.key === "ArrowRight") && nodeElement && !nodeElement.classList.contains("protyle-wysiwyg--select")) { const selectElements = Array.from(protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select")); let containRange = false; diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts index c35c898d5..0ec87fe31 100644 --- a/app/src/protyle/wysiwyg/keydown.ts +++ b/app/src/protyle/wysiwyg/keydown.ts @@ -28,7 +28,7 @@ import { getPreviousBlock, getTopAloneElement, hasNextSibling, - hasPreviousSibling, + hasPreviousSibling, isEndOfBlock, isNotEditBlock, } from "./getBlock"; import {matchHotKey} from "../util/hotKey"; @@ -328,13 +328,29 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => { event.preventDefault(); return; } - if (matchHotKey("⇧←", event) || matchHotKey("⇧→", event)) { + + if (event.shiftKey && (event.key === "ArrowLeft" || event.key === "ArrowRight")) { const selectElements = protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select"); if (selectElements.length > 0) { event.stopPropagation(); event.preventDefault(); return; } + + if (!range.toString()) { + if (event.key === "ArrowRight" && isEndOfBlock(range)) { + event.preventDefault(); + event.stopPropagation(); + return; + } + const nodeEditableElement = getContenteditableElement(nodeElement) + const position = getSelectionOffset(nodeEditableElement, protyle.wysiwyg.element, range); + if (position.start === 0 && event.key === "ArrowLeft") { + event.preventDefault(); + event.stopPropagation(); + return; + } + } } if (matchHotKey(window.siyuan.config.keymap.editor.general.expandUp.custom, event)) {