An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: Improve arrow key navigation for database blocks https://github.com/siyuan-note/siyuan/issues/12533
Daniel2f4e1083ee70
diff --git a/app/src/protyle/header/Title.ts b/app/src/protyle/header/Title.tsindex 5d89a37e40..aeda6fb9a2 100644--- a/app/src/protyle/header/Title.ts+++ b/app/src/protyle/header/Title.ts@@ -29,6 +29,7 @@ import {openTitleMenu} from "./openTitleMenu";import {electronUndo} from "../undo";import {enableLuteMarkdownSyntax, restoreLuteMarkdownSyntax} from "../util/paste";import {addSpellcheckMenuItems, requestSpellcheckContext} from "../../menus/spellcheck";+import {focusAVByArrow} from "../render/av/focus";export class Title {public element: HTMLElement;@@ -147,7 +148,10 @@ export class Title {const noContainerElement = getNoContainerElement(protyle.wysiwyg.element.firstElementChild);// https://github.com/siyuan-note/siyuan/issues/4923if (noContainerElement) {- focusBlock(noContainerElement, protyle.wysiwyg.element);+ if (!noContainerElement.classList.contains("av") ||+ !focusAVByArrow(protyle, noContainerElement as HTMLElement, event.key)) {+ focusBlock(noContainerElement, protyle.wysiwyg.element);+ }}event.preventDefault();event.stopPropagation();diff --git a/app/src/protyle/render/av/focus.ts b/app/src/protyle/render/av/focus.tsnew file mode 100644index 0000000000..e0d0b9e5da--- /dev/null+++ b/app/src/protyle/render/av/focus.ts@@ -0,0 +1,59 @@+import {addDragFill, cellScrollIntoView} from "./cell";+import {clearSelect} from "../../util/clear";+import {focusBlock, focusByRange} from "../../util/selection";+import {getFirstBlock, getLastBlock, getNextBlock, getPreviousBlock} from "../../wysiwyg/getBlock";+import {scrollCenter} from "../../../util/highlightById";++const isForwardArrow = (key: string) => key === "ArrowDown" || key === "ArrowRight";++export const focusAVByArrow = (protyle: IProtyle, blockElement: HTMLElement, key: string, skipTitle = false) => {+ if (!["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(key) ||+ !blockElement.querySelector(".av__cursor")) {+ return false;+ }++ const toStart = isForwardArrow(key);+ const titleElement = !skipTitle &&+ blockElement.querySelector(".av__title:not(.fn__none)") as HTMLElement;+ if (titleElement && titleElement.getClientRects().length > 0) {+ clearSelect(["av"], blockElement);+ const range = document.createRange();+ range.selectNodeContents(titleElement);+ range.collapse(toStart);+ focusByRange(range);+ return true;+ }++ if (blockElement.dataset.avType !== "table") {+ return false;+ }++ const cellElement = Array.from(blockElement.querySelectorAll<HTMLElement>(".av__row[data-id] .av__cell")).find(+ item => item.getClientRects().length > 0+ );+ if (cellElement && (!skipTitle || toStart)) {+ if (!focusBlock(blockElement)) {+ return false;+ }+ clearSelect(["av"], blockElement);+ cellElement.classList.add("av__cell--select");+ addDragFill(cellElement);+ cellScrollIntoView(blockElement, cellElement);+ return true;+ }++ clearSelect(["av"], blockElement);+ let adjacentElement = toStart ? getNextBlock(blockElement) : getPreviousBlock(blockElement);+ if (!adjacentElement) {+ return true;+ }+ adjacentElement = toStart ? getFirstBlock(adjacentElement) : getLastBlock(adjacentElement);+ if (adjacentElement.classList.contains("av") &&+ focusAVByArrow(protyle, adjacentElement as HTMLElement, key)) {+ scrollCenter(protyle, adjacentElement);+ return true;+ }+ focusBlock(adjacentElement, undefined, toStart);+ scrollCenter(protyle, adjacentElement);+ return true;+};diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.tsindex 81824506d6..821b1bd0ce 100644--- a/app/src/protyle/wysiwyg/index.ts+++ b/app/src/protyle/wysiwyg/index.ts@@ -115,6 +115,7 @@ import {processSiYuanUri} from "../../util/uri";import {enhanceRichClipboard} from "../util/richClipboard";import {addSpellcheckMenuItems, requestSpellcheckContext} from "../../menus/spellcheck";import {getAVTemplateInteractiveElement, isAVTemplateLink} from "../render/av/attributeValue";+import {focusAVByArrow} from "../render/av/focus";export class WYSIWYG {public lastHTMLs: { [key: string]: string } = {};@@ -2910,6 +2911,14 @@ export class WYSIWYG {// 输入法测试点 https://github.com/siyuan-note/siyuan/issues/3027let isComposition = false; // for iPhone+ // 仅矫正从数据库外进入的占位光标,避免重置数据库内部的方向键导航。+ let arrowStartElement: false | HTMLElement | undefined;+ this.element.addEventListener("keydown", (event: KeyboardEvent) => {+ if (!event.repeat && !event.altKey && !event.shiftKey && !event.ctrlKey && !event.metaKey &&+ !event.isComposing && event.key.startsWith("Arrow")) {+ arrowStartElement = hasClosestBlock(getEditorRange(this.element).startContainer);+ }+ });// 记录组合开始时的光标位置,用于取消组合后恢复光标(输入法删空候选词触发 compositionend 时浏览器会把光标移出可编辑单元格)let compositionRange: { cell: HTMLElement; offset: number };this.element.addEventListener("compositionstart", (event) => {@@ -3046,6 +3055,20 @@ export class WYSIWYG {}const range = getEditorRange(this.element).cloneRange();const nodeElement = hasClosestBlock(range.startContainer);+ const isArrowFromOutsideAV = arrowStartElement && !arrowStartElement.classList.contains("av");+ if (event.key.startsWith("Arrow")) {+ arrowStartElement = undefined;+ }++ if (!event.altKey && !event.shiftKey && !event.ctrlKey && !event.metaKey && !event.isComposing &&+ event.key.startsWith("Arrow") && isArrowFromOutsideAV &&+ nodeElement && nodeElement.classList.contains("av") &&+ !nodeElement.classList.contains("protyle-wysiwyg--select") &&+ hasClosestByClassName(range.startContainer, "av__cursor") &&+ focusAVByArrow(protyle, nodeElement, event.key)) {+ event.stopPropagation();+ return;+ }if (event.key !== "PageUp" && event.key !== "PageDown" && event.key !== "Home" && event.key !== "End" &&event.key.indexOf("Arrow") === -1 && event.key !== "Escape" && event.key !== "Shift" &&diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.tsindex 74da12366d..4a59841f0e 100644--- a/app/src/protyle/wysiwyg/keydown.ts+++ b/app/src/protyle/wysiwyg/keydown.ts@@ -79,6 +79,7 @@ import {updateCalloutType} from "./callout";import {tabCodeBlock} from "./codeBlock";import {getTopBarHeight} from "../../layout/getTopBarHeight";import {getAVTemplateInteractiveElement} from "../render/av/attributeValue";+import {focusAVByArrow} from "../render/av/focus";export const getContentByInlineHTML = (range: Range, cb: (content: string) => void) => {let html = "";@@ -641,6 +642,39 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {position.end -= 1;}+ const selectionPosition = getSelectionPosition(nodeEditableElement, range);+ const isFirstLine = (nodeEditableElement.innerText.substr(0, position.end).indexOf("\n") === -1 ||+ position.start === 0) &&+ selectionPosition.top - nodeEditableElement.getBoundingClientRect().top < 20;+ const isLastLine = (nodeEditableElement.innerText.substr(position.end).indexOf("\n") === -1 ||+ position.end >= nodeEditableElement.innerText.trimEnd().length) &&+ nodeEditableElement.getBoundingClientRect().bottom - selectionPosition.top < 40;+ const isStart = position.start === 0;+ const isEnd = position.end >= nodeEditableElement.textContent.replace(/\n$/, "").length;+ const toPrevious = (event.key === "ArrowUp" && isFirstLine) ||+ (event.key === "ArrowLeft" && isStart);+ const toNext = (event.key === "ArrowDown" && isLastLine) ||+ (event.key === "ArrowRight" && isEnd);+ if (selectText === "" && range.collapsed && nodeElement.classList.contains("av") &&+ hasClosestByClassName(range.startContainer, "av__title") && (toPrevious || toNext) &&+ focusAVByArrow(protyle, nodeElement, event.key, true)) {+ event.stopPropagation();+ event.preventDefault();+ return;+ }+ if (selectText === "" && range.collapsed && !nodeElement.classList.contains("av")) {+ let adjacentElement = toPrevious ? getPreviousBlock(nodeElement) :+ (toNext ? getNextBlock(nodeElement) : undefined);+ if (adjacentElement) {+ adjacentElement = toPrevious ? getLastBlock(adjacentElement) : getFirstBlock(adjacentElement);+ if (adjacentElement.classList.contains("av") &&+ focusAVByArrow(protyle, adjacentElement as HTMLElement, event.key)) {+ event.stopPropagation();+ event.preventDefault();+ return;+ }+ }+ }if (event.key === "ArrowUp") {const firstEditElement = getContenteditableElement(protyle.wysiwyg.element.firstElementChild);if ((