Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Preserve database selection during arrow navigation https://github.com/siyuan-note/siyuan/issues/19131

Daniel1e15213a5f4b
diff --git a/app/src/protyle/render/av/verticalNavigation.test.ts b/app/src/protyle/render/av/verticalNavigation.test.ts
index 4739b31a8d..f27355b1bd 100644
--- a/app/src/protyle/render/av/verticalNavigation.test.ts
+++ b/app/src/protyle/render/av/verticalNavigation.test.ts
@@ -1,6 +1,10 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
-import {getAVVerticalNavigationAction} from "./verticalNavigation";
+import {
+ getAVVerticalNavigationAction,
+ shouldPreserveAVSelectionOnKeyup,
+ shouldRunAVKeyupFallback,
+} from "./verticalNavigation";
describe("database vertical navigation", () => {
it("moves inside the database when an adjacent item exists", () => {
@@ -10,4 +14,15 @@ describe("database vertical navigation", () => {
it("leaves the database at its logical item boundary", () => {
assert.equal(getAVVerticalNavigationAction(false), "leave");
});
+
+ it("preserves a database selection created by an arrow keydown", () => {
+ assert.equal(shouldPreserveAVSelectionOnKeyup("ArrowUp", true), true);
+ assert.equal(shouldPreserveAVSelectionOnKeyup("ArrowUp", false), false);
+ assert.equal(shouldPreserveAVSelectionOnKeyup("Enter", true), false);
+ });
+
+ it("does not run the legacy keyup fallback after keydown handled navigation", () => {
+ assert.equal(shouldRunAVKeyupFallback(true, true), false);
+ assert.equal(shouldRunAVKeyupFallback(false, true), true);
+ });
});
diff --git a/app/src/protyle/render/av/verticalNavigation.ts b/app/src/protyle/render/av/verticalNavigation.ts
index 73541de7d0..d9657c89b4 100644
--- a/app/src/protyle/render/av/verticalNavigation.ts
+++ b/app/src/protyle/render/av/verticalNavigation.ts
@@ -1,2 +1,8 @@
export const getAVVerticalNavigationAction = (hasAdjacentItem: boolean) =>
hasAdjacentItem ? "move" as const : "leave" as const;
+
+export const shouldPreserveAVSelectionOnKeyup = (key: string, isFocusedInAV: boolean) =>
+ key.startsWith("Arrow") && isFocusedInAV;
+
+export const shouldRunAVKeyupFallback = (preventKeyup: boolean, isArrowFromOutsideAV: boolean) =>
+ !preventKeyup && isArrowFromOutsideAV;
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index 253da868e3..04d8e5494b 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -184,6 +184,7 @@ import {
import {addSpellcheckMenuItems, requestSpellcheckContext} from "../../menus/spellcheck";
import {getAVTemplateInteractiveElement, isAVTemplateLink} from "../render/av/attributeValue";
import {focusAVByArrow} from "../render/av/focus";
+import {shouldRunAVKeyupFallback} from "../render/av/verticalNavigation";
import {applyAVDragSelection, clearAVDragSelection, isAVDragSelectSupported} from "../render/av/dragSelect";
import {
selectAVCellRange,
@@ -4234,7 +4235,8 @@ export class WYSIWYG {
}
if (!event.altKey && !event.shiftKey && !event.ctrlKey && !event.metaKey && !event.isComposing &&
- event.key.startsWith("Arrow") && isArrowFromOutsideAV &&
+ event.key.startsWith("Arrow") && shouldRunAVKeyupFallback(this.preventKeyup,
+ !!isArrowFromOutsideAV) &&
nodeElement && nodeElement.classList.contains("av") &&
!nodeElement.classList.contains("protyle-wysiwyg--select") &&
hasClosestByClassName(range.startContainer, "av__cursor") &&
diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts
index df6db777cd..a861c18d9a 100644
--- a/app/src/protyle/wysiwyg/keydown.ts
+++ b/app/src/protyle/wysiwyg/keydown.ts
@@ -119,6 +119,7 @@ import {preventScroll} from "../scroll/preventScroll";
import {newFileBySelectRange} from "../../util/newFile";
import {removeSearchMark} from "../toolbar/util";
import {avKeydown} from "../render/av/keydown";
+import {shouldPreserveAVSelectionOnKeyup} from "../render/av/verticalNavigation";
import {checkFold} from "../../util/noRelyPCFunction";
import {AIActions} from "../../ai/actions";
import {openLink} from "../../editor/openLink";
@@ -209,6 +210,14 @@ const showSelectAllTip = () => {
});
};
+const preserveAVSelectionOnKeyup = (protyle: IProtyle, event: KeyboardEvent) => {
+ const focusedElement = hasClosestBlock(getEditorRange(protyle.wysiwyg.element).startContainer);
+ if (shouldPreserveAVSelectionOnKeyup(event.key,
+ !!focusedElement && focusedElement.classList.contains("av"))) {
+ protyle.wysiwyg.preventKeyup = true;
+ }
+};
+
const showSelectAllIncompleteTip = () => {
if (window.siyuan.config.appearance.notifications?.selectAllIncompleteTip === false) {
return;
@@ -341,9 +350,11 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
if (document.querySelector(".av__panel")) {
return;
}
- if (avKeydown(event, nodeElement, protyle, direction => {
+ const avHandled = avKeydown(event, nodeElement, protyle, direction => {
leaveAVVerticalRegion(protyle, nodeElement, direction, verticalGoalX ?? 0);
- })) {
+ });
+ if (avHandled) {
+ preserveAVSelectionOnKeyup(protyle, event);
return;
}
@@ -1004,6 +1015,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
isAtomicVerticalNavigationTarget(nodeElement)) {
focusAdjacentVerticalRegion(protyle, nodeElement, verticalDirection, verticalGoalX ?? 0,
range.startContainer);
+ preserveAVSelectionOnKeyup(protyle, event);
event.stopPropagation();
event.preventDefault();
return;
@@ -1044,6 +1056,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
if (selectText === "" && range.collapsed && verticalDirection && (toPrevious || toNext)) {
focusAdjacentVerticalRegion(protyle, nodeElement, verticalDirection, verticalGoalX ?? 0,
range.startContainer);
+ preserveAVSelectionOnKeyup(protyle, event);
event.stopPropagation();
event.preventDefault();
return;
@@ -1051,6 +1064,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
if (selectText === "" && range.collapsed && !verticalDirection && nodeElement.classList.contains("av") &&
hasClosestByClassName(range.startContainer, "av__title") && (toPrevious || toNext) &&
focusAVByArrow(protyle, nodeElement, event.key, true)) {
+ preserveAVSelectionOnKeyup(protyle, event);
event.stopPropagation();
event.preventDefault();
return;
@@ -1062,6 +1076,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
adjacentElement = toPrevious ? getLastBlock(adjacentElement) : getFirstBlock(adjacentElement);
if (adjacentElement.classList.contains("av") &&
focusAVByArrow(protyle, adjacentElement as HTMLElement, event.key)) {
+ preserveAVSelectionOnKeyup(protyle, event);
event.stopPropagation();
event.preventDefault();
return;