An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Improve mobile block reference hints https://github.com/siyuan-note/siyuan/issues/19037
Daniel1460a46df291
diff --git a/app/src/mobile/util/keyboardToolbar.ts b/app/src/mobile/util/keyboardToolbar.tsindex fe33f926b0..8241550391 100644--- a/app/src/mobile/util/keyboardToolbar.ts+++ b/app/src/mobile/util/keyboardToolbar.ts@@ -55,6 +55,7 @@ import {shouldPreserveTableCellSelectAll,type TSelectionEndpoint,} from "./touchSelection";+import {getVisibleViewportBounds} from "./visibleViewport";type TAndroidBoundedSelection = {container: HTMLElement,@@ -329,19 +330,6 @@ const preventKeyboardToolbarRender = () => {}, 1000);};-const getVisibleViewportBounds = () => {- if (!isInMobileApp() && window.visualViewport) {- return {- top: window.visualViewport.offsetTop,- bottom: window.visualViewport.offsetTop + window.visualViewport.height,- };- }- return {- top: 0,- bottom: window.innerHeight,- };-};-const updateKeyboardToolbarPosition = () => {if (isInMobileApp() || !window.visualViewport) {return;diff --git a/app/src/mobile/util/visibleViewport.ts b/app/src/mobile/util/visibleViewport.tsnew file mode 100644index 0000000000..b72caa68a3--- /dev/null+++ b/app/src/mobile/util/visibleViewport.ts@@ -0,0 +1,14 @@+import {isInMobileApp} from "../../protyle/util/compatibility";++export const getVisibleViewportBounds = () => {+ if (!isInMobileApp() && window.visualViewport) {+ return {+ top: window.visualViewport.offsetTop,+ bottom: window.visualViewport.offsetTop + window.visualViewport.height,+ };+ }+ return {+ top: 0,+ bottom: window.innerHeight,+ };+};diff --git a/app/src/protyle/hint/blockHintRange.test.ts b/app/src/protyle/hint/blockHintRange.test.tsindex 3d3901a4b4..1645f38a14 100644--- a/app/src/protyle/hint/blockHintRange.test.ts+++ b/app/src/protyle/hint/blockHintRange.test.ts@@ -4,6 +4,7 @@ import {endsWithMultiCharHintPrefix,getBlockHintTriggerOffset,getBlockRefStaticText,+ isBlockHintQueryAtCaret,shouldCaptureHintUndoFocus,shouldIgnoreHintTrigger,} from "./blockHintRange";@@ -58,6 +59,23 @@ describe("getBlockHintTriggerOffset", () => {});});+describe("isBlockHintQueryAtCaret", () => {+ it("recognizes an unfinished query around the caret", () => {+ assert.equal(isBlockHintQueryAtCaret("prefix [[que", "ry", "[[", "]]", 512), true);+ assert.equal(isBlockHintQueryAtCaret("prefix ((query", "", "((", "))", 512), true);+ });++ it("keeps a query editable before its closing marker", () => {+ assert.equal(isBlockHintQueryAtCaret("[[que", "ry]]", "[[", "]]", 512), true);+ });++ it("rejects text outside a valid query", () => {+ assert.equal(isBlockHintQueryAtCaret("plain text", "", "[[", "]]", 512), false);+ assert.equal(isBlockHintQueryAtCaret("[[ query", "", "[[", "]]", 512), false);+ assert.equal(isBlockHintQueryAtCaret("[[12345", "", "[[", "]]", 5), false);+ });+});+describe("getBlockRefStaticText", () => {it("preserves the complete toolbar selection", () => {assert.equal(getBlockRefStaticText("旧的开始", "((", false), "旧的开始");diff --git a/app/src/protyle/hint/blockHintRange.ts b/app/src/protyle/hint/blockHintRange.tsindex 85dbbe4522..f041c6f062 100644--- a/app/src/protyle/hint/blockHintRange.ts+++ b/app/src/protyle/hint/blockHintRange.ts@@ -8,6 +8,16 @@ export const getBlockHintTriggerOffset = (textBeforeCaret: string, textAfterCarereturn tripleOffset > -1 ? Math.min(latestOffset, tripleOffset) : latestOffset;};+export const isBlockHintQueryAtCaret = (textBeforeCaret: string, textAfterCaret: string,+ splitChar: string, endSplit: string, maxLength: number) => {+ const triggerOffset = getBlockHintTriggerOffset(textBeforeCaret, textAfterCaret, splitChar, endSplit);+ if (triggerOffset < 0) {+ return false;+ }+ const query = textBeforeCaret.substring(triggerOffset + splitChar.length);+ return query.trimStart() === query && query.length < maxLength;+};+export const getBlockRefStaticText = (selectedText: string, splitChar: string, includesTrigger: boolean) => {return includesTrigger ? selectedText.substring(splitChar.length) : selectedText;};diff --git a/app/src/protyle/hint/index.ts b/app/src/protyle/hint/index.tsindex cbe9f28485..e300192893 100644--- a/app/src/protyle/hint/index.ts+++ b/app/src/protyle/hint/index.ts@@ -64,9 +64,13 @@ import {endsWithMultiCharHintPrefix,getBlockHintTriggerOffset,getBlockRefStaticText,+ isBlockHintQueryAtCaret,shouldCaptureHintUndoFocus,shouldIgnoreHintTrigger,} from "./blockHintRange";+import {getMobileHintPosition} from "./mobileHintPosition";+import {getVisibleViewportBounds} from "../../mobile/util/visibleViewport";+import {getTopBarHeight} from "../../layout/getTopBarHeight";const genEmojiInsertHTML = (value: string) => {const kind = getIconValueKind(value);@@ -327,6 +331,44 @@ export class Hint {});}+ public canResumeBlockHint(protyle: IProtyle, range: Range) {+ if (!range.collapsed || !protyle.wysiwyg.element.contains(range.startContainer) ||+ !Constants.BLOCK_HINT_KEYS.includes(this.splitChar) ||+ hasClosestByAttribute(range.startContainer, "data-type", "code") ||+ hasClosestByAttribute(range.startContainer, "data-type", "NodeCodeBlock")) {+ return false;+ }+ const start = getSelectionOffset(range.startContainer, protyle.wysiwyg.element, range).start;+ const textBeforeCaret = range.startContainer.textContent.substring(0, start) || "";+ let textAfterCaret = "";+ if (range.startContainer.nodeType === 3) {+ const textNode = range.startContainer as Text;+ const caretOffset = getWholeTextOffset(textNode, range.startOffset);+ textAfterCaret = textNode.wholeText.substring(caretOffset);+ }+ return isBlockHintQueryAtCaret(textBeforeCaret, textAfterCaret, this.splitChar,+ Constants.BLOCK_HINT_CLOSE_KEYS[this.splitChar], Constants.SIZE_TITLE);+ }++ private setMobilePosition(anchorTop: number, anchorBottom: number) {+ const viewportBounds = getVisibleViewportBounds();+ const viewportTop = Math.max(viewportBounds.top, getTopBarHeight());+ let viewportBottom = viewportBounds.bottom;+ const keyboardToolbarElement = document.getElementById("keyboardToolbar");+ if (keyboardToolbarElement && !keyboardToolbarElement.classList.contains("fn__none")) {+ viewportBottom = Math.min(viewportBottom, keyboardToolbarElement.getBoundingClientRect().top);+ }+ viewportBottom = Math.max(viewportTop, viewportBottom);+ const heightLimit = (viewportBottom - viewportTop) / 2;+ let position = getMobileHintPosition(anchorTop, anchorBottom, this.element.scrollHeight,+ viewportTop, viewportBottom, heightLimit);+ this.element.style.maxHeight = `${position.maxHeight}px`;+ position = getMobileHintPosition(anchorTop, anchorBottom, this.element.getBoundingClientRect().height,+ viewportTop, viewportBottom, heightLimit);+ this.element.style.left = "0";+ this.element.style.top = `${position.top}px`;+ }+public genLoading(protyle: IProtyle) {this.destroyEmojiPanel();if (this.element.classList.contains("fn__none")) {@@ -335,19 +377,19 @@ export class Hint {if (this.source === "av") {const cellElement = hasClosestByClassName(protyle.toolbar.range.startContainer, "av__cell");if (cellElement) {- /// #if !MOBILEconst cellRect = cellElement.getBoundingClientRect();+ /// #if !MOBILEsetPosition(this.element, cellRect.left, cellRect.bottom, cellRect.height);/// #else- setPosition(this.element, 0, 0);+ this.setMobilePosition(cellRect.top, cellRect.bottom);/// #endif}} else {- /// #if !MOBILEconst textareaPosition = getSelectionPosition(protyle.wysiwyg.element);+ /// #if !MOBILEsetPosition(this.element, textareaPosition.left, textareaPosition.top + 26, 30);/// #else- setPosition(this.element, 0, 0);+ this.setMobilePosition(textareaPosition.top, textareaPosition.top + 26);/// #endif}} else if (!this.element.querySelector(".fn__loading")) {@@ -436,7 +478,7 @@ export class Hint {/// #if !MOBILEsetPosition(this.element, cellRect.left, cellRect.bottom, cellRect.height);/// #else- setPosition(this.element, 0, 0);+ this.setMobilePosition(cellRect.top, cellRect.bottom);/// #endif}} else {@@ -444,7 +486,7 @@ export class Hint {/// #if !MOBILEsetPosition(this.element, textareaPosition.left, textareaPosition.top + 26, 30);/// #else- setPosition(this.element, 0, 0);+ this.setMobilePosition(textareaPosition.top, textareaPosition.top + 26);/// #endif}this.element.scrollTop = 0;diff --git a/app/src/protyle/hint/mobileHintPosition.test.ts b/app/src/protyle/hint/mobileHintPosition.test.tsnew file mode 100644index 0000000000..2a33eecd9f--- /dev/null+++ b/app/src/protyle/hint/mobileHintPosition.test.ts@@ -0,0 +1,44 @@+import {describe, it} from "node:test";+import * as assert from "node:assert/strict";+import {getMobileHintPosition} from "./mobileHintPosition";++describe("getMobileHintPosition", () => {+ it("places the hint below the caret when more space is available there", () => {+ assert.deepEqual(getMobileHintPosition(80, 106, 200, 48, 500), {+ maxHeight: 394,+ top: 106,+ });+ });++ it("places the hint above the caret without covering it", () => {+ assert.deepEqual(getMobileHintPosition(420, 446, 200, 48, 500), {+ maxHeight: 372,+ top: 220,+ });+ });++ it("limits the hint to the available space above the caret", () => {+ assert.deepEqual(getMobileHintPosition(180, 206, 300, 48, 250), {+ maxHeight: 132,+ top: 48,+ });+ });++ it("keeps the hint inside the viewport when the caret is obscured", () => {+ assert.deepEqual(getMobileHintPosition(540, 566, 200, 48, 500), {+ maxHeight: 452,+ top: 300,+ });+ assert.deepEqual(getMobileHintPosition(10, 36, 200, 48, 500), {+ maxHeight: 452,+ top: 48,+ });+ });++ it("limits the hint to part of the visible viewport", () => {+ assert.deepEqual(getMobileHintPosition(420, 446, 400, 48, 500, 226), {+ maxHeight: 226,+ top: 194,+ });+ });+});diff --git a/app/src/protyle/hint/mobileHintPosition.ts b/app/src/protyle/hint/mobileHintPosition.tsnew file mode 100644index 0000000000..9c7e54c28a--- /dev/null+++ b/app/src/protyle/hint/mobileHintPosition.ts@@ -0,0 +1,14 @@+export const getMobileHintPosition = (anchorTop: number, anchorBottom: number, hintHeight: number,+ viewportTop: number, viewportBottom: number, heightLimit = Infinity) => {+ const visibleAnchorTop = Math.max(viewportTop, Math.min(anchorTop, viewportBottom));+ const visibleAnchorBottom = Math.max(visibleAnchorTop, Math.min(anchorBottom, viewportBottom));+ const spaceAbove = visibleAnchorTop - viewportTop;+ const spaceBelow = viewportBottom - visibleAnchorBottom;+ const below = spaceBelow >= spaceAbove;+ const maxHeight = Math.min(below ? spaceBelow : spaceAbove, Math.max(0, heightLimit));+ const height = Math.min(hintHeight, maxHeight);+ return {+ maxHeight,+ top: below ? visibleAnchorBottom : visibleAnchorTop - height,+ };+};diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.tsindex 81ea7656b8..93cbafd273 100644--- a/app/src/protyle/wysiwyg/index.ts+++ b/app/src/protyle/wysiwyg/index.ts@@ -4399,6 +4399,8 @@ export class WYSIWYG {}}const range = getEditorRange(this.element);+ const resumeBlockHint = !protyle.hint.element.classList.contains("fn__none") &&+ Constants.BLOCK_HINT_KEYS.includes(protyle.hint.splitChar);// 面包屑定位,需至于前,否则 return 的元素就无法进行面包屑定位if (protyle.options.render.breadcrumb) {protyle.breadcrumb.render(protyle, false, hasClosestBlock(range.startContainer));@@ -5044,7 +5046,7 @@ export class WYSIWYG {mobileBlur = false;}, (isMobile() || isInIOS()) ? 520 : 0); // Android/iPad 双击慢了出不来- protyle.hint.enableExtend = false;+ protyle.hint.enableExtend = resumeBlockHint && protyle.hint.canResumeBlockHint(protyle, range);if (this.element.querySelector(".protyle-wysiwyg--select") && range.toString() !== "") {// 选中块后,文字不能被选中。需在 shift click 之后,防止shift点击单个块出现文字选中