Vanessa 2025-02-08 09:25:14 +08:00
parent 4155d9afdd
commit 0d5d62ff60
3 changed files with 41 additions and 7 deletions

View file

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

View file

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

View file

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