Browse Source

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

Vanessa 1 year ago
parent
commit
38673b2b7d
2 changed files with 34 additions and 19 deletions
  1. 6 2
      app/src/protyle/util/selection.ts
  2. 28 17
      app/src/protyle/wysiwyg/commonHotkey.ts

+ 6 - 2
app/src/protyle/util/selection.ts

@@ -308,10 +308,14 @@ export const setLastNodeRange = (editElement: Element, range: Range, setStart =
     if (!editElement) {
         return range;
     }
-    let lastNode = editElement.lastChild;
+    let lastNode = editElement.lastChild as Element;
     while (lastNode && lastNode.nodeType !== 3) {
+        if (lastNode.nodeType !== 3 && lastNode.tagName === "BR") {
+            // 防止单元格中 ⇧↓ 全部选中
+            return range;
+        }
         // 最后一个为多种行内元素嵌套
-        lastNode = lastNode.lastChild;
+        lastNode = lastNode.lastChild as Element;
     }
     if (!lastNode) {
         range.selectNodeContents(editElement);

+ 28 - 17
app/src/protyle/wysiwyg/commonHotkey.ts

@@ -4,7 +4,7 @@ import {isMac, writeText} from "../util/compatibility";
 import {
     focusBlock,
     getSelectionOffset,
-    setFirstNodeRange,
+    setFirstNodeRange, setLastNodeRange,
 } from "../util/selection";
 import {getContenteditableElement, hasNextSibling, hasPreviousSibling} from "./getBlock";
 import {hasClosestByMatchTag} from "../util/hasClosest";
@@ -95,21 +95,19 @@ export const upSelect = (options: {
         options.event.stopPropagation();
         options.event.preventDefault();
     } else {
-        if (isMac()  // Windows 中 ⌥⇧↓ 默认无选中功能会导致 https://ld246.com/article/1716635371149
-            && getSelectionOffset(options.nodeElement, options.editorElement, options.range).start !== 0) {
-            const editElement = getContenteditableElement(options.nodeElement);
-            if (editElement.tagName === "TABLE") {
-                const cellElement = hasClosestByMatchTag(options.range.startContainer, "TH") || hasClosestByMatchTag(options.range.startContainer, "TD") || editElement.querySelector("th, td");
-                if (getSelectionOffset(cellElement, cellElement, options.range).start !== 0) {
-                    setFirstNodeRange(cellElement, options.range);
-                    options.event.stopPropagation();
-                    options.event.preventDefault();
-                    return;
-                }
-            } else {
-                // 选中上一个节点的处理在 toolbar/index.ts 中 `shift+方向键或三击选中`
-                return;
+        const tdElement = hasClosestByMatchTag(options.range.startContainer, "TD") || hasClosestByMatchTag(options.range.startContainer, "TH");
+        const nodeEditableElement = (tdElement || getContenteditableElement(options.nodeElement) || options.nodeElement) as HTMLElement;
+        const startIndex = getSelectionOffset(nodeEditableElement, options.editorElement, options.range).start
+        const innerText = nodeEditableElement.innerText;
+        const isExpandUp = matchHotKey(window.siyuan.config.keymap.editor.general.expandUp.custom, options.event)
+        if (!isMac() && isExpandUp) {
+            // Windows 中 ⌥⇧↑ 默认无选中功能会导致 https://ld246.com/article/1716635371149
+        } else if (startIndex > 0) {
+            // 选中上一个节点的处理在 toolbar/index.ts 中 `shift+方向键或三击选中`
+            if (innerText.substr(0, startIndex).indexOf("\n") === -1) {
+                setFirstNodeRange(nodeEditableElement, options.range);
             }
+            return;
         }
     }
     options.range.collapse(true);
@@ -141,9 +139,22 @@ export const downSelect = (options: {
         options.event.stopPropagation();
         options.event.preventDefault();
     } else {
-        if (isMac() // Windows 中 ⌥⇧↑ 默认无选中功能会导致 https://ld246.com/article/1716635371149
-            && getSelectionOffset(options.nodeElement, options.editorElement, options.range).end < getContenteditableElement(options.nodeElement).textContent.length) {
+        const tdElement = hasClosestByMatchTag(options.range.startContainer, "TD") || hasClosestByMatchTag(options.range.startContainer, "TH");
+        const nodeEditableElement = (tdElement || getContenteditableElement(options.nodeElement) || options.nodeElement) as HTMLElement;
+        const endIndex = getSelectionOffset(nodeEditableElement, options.editorElement, options.range).end
+        const innerText = nodeEditableElement.innerText;
+        const isExpandDown = matchHotKey(window.siyuan.config.keymap.editor.general.expandDown.custom, options.event)
+        if (!isMac() && isExpandDown) {
+            // Windows 中 ⌥⇧↓ 默认无选中功能会导致 https://ld246.com/article/1716635371149
+        } else if (endIndex < innerText.length) {
             // 选中下一个节点的处理在 toolbar/index.ts 中 `shift+方向键或三击选中`
+            if (!options.nodeElement.nextElementSibling && innerText.trimRight().substr(endIndex).indexOf("\n") === -1) {
+                // 当为最后一个块时应选中末尾
+                setLastNodeRange(nodeEditableElement, options.range, false);
+                if (options.nodeElement.classList.contains("code-block") && isExpandDown) {
+                    options.event.preventDefault();
+                }
+            }
             return;
         }
     }