Skip to content

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

:art: Improve mobile block hint fallback positioning https://github.com/siyuan-note/siyuan/issues/19037

Daniel2bfb5c688b04
diff --git a/app/src/protyle/hint/index.ts b/app/src/protyle/hint/index.ts
index e351bebfee..afdcd63090 100644
--- a/app/src/protyle/hint/index.ts
+++ b/app/src/protyle/hint/index.ts
@@ -352,7 +352,7 @@ export class Hint {
Constants.BLOCK_HINT_CLOSE_KEYS[this.splitChar], Constants.SIZE_TITLE);
}
- private getMobileSelectionTop(protyle: IProtyle) {
+ private getMobileSelectionBounds(protyle: IProtyle) {
const range = getEditorRange(protyle.wysiwyg.element);
const position = getSelectionPosition(protyle.wysiwyg.element, range);
if (range.startContainer.nodeType === 3 && range.startContainer.textContent.length > 0) {
@@ -369,13 +369,19 @@ export class Hint {
const rects = textRange.getClientRects();
const rect = rects[rects.length - 1];
if (rect?.height > 0) {
- return rect.top;
+ return {
+ top: rect.top,
+ bottom: rect.bottom,
+ };
}
}
- return position.top;
+ return {
+ top: position.top,
+ bottom: position.top + 26,
+ };
}
- private setMobilePosition(anchorTop: number) {
+ private setMobilePosition(anchorTop: number, anchorBottom: number) {
const viewportBounds = getVisibleViewportBounds();
const viewportTop = Math.max(viewportBounds.top, getTopBarHeight());
let viewportBottom = viewportBounds.bottom;
@@ -386,14 +392,14 @@ export class Hint {
viewportBottom = Math.max(viewportTop, viewportBottom);
const heightLimit = (viewportBottom - viewportTop) / 3;
const gap = 8;
- let position = getMobileHintPosition(anchorTop, this.element.scrollHeight, viewportTop, viewportBottom,
- heightLimit, gap);
+ let position = getMobileHintPosition(anchorTop, anchorBottom, this.element.scrollHeight, viewportTop,
+ viewportBottom, heightLimit, gap);
const hintStyle = getComputedStyle(this.element);
const verticalInset = parseFloat(hintStyle.paddingTop) + parseFloat(hintStyle.paddingBottom) +
parseFloat(hintStyle.borderTopWidth) + parseFloat(hintStyle.borderBottomWidth);
this.element.style.maxHeight = `${Math.max(0, position.maxHeight - verticalInset)}px`;
- position = getMobileHintPosition(anchorTop, this.element.getBoundingClientRect().height, viewportTop,
- viewportBottom, heightLimit, gap);
+ position = getMobileHintPosition(anchorTop, anchorBottom, this.element.getBoundingClientRect().height,
+ viewportTop, viewportBottom, heightLimit, gap);
this.element.style.left = "0";
this.element.style.top = `${position.top}px`;
}
@@ -410,7 +416,7 @@ export class Hint {
/// #if !MOBILE
setPosition(this.element, cellRect.left, cellRect.bottom, cellRect.height);
/// #else
- this.setMobilePosition(cellRect.top);
+ this.setMobilePosition(cellRect.top, cellRect.bottom);
/// #endif
}
} else {
@@ -418,7 +424,8 @@ export class Hint {
/// #if !MOBILE
setPosition(this.element, textareaPosition.left, textareaPosition.top + 26, 30);
/// #else
- this.setMobilePosition(this.getMobileSelectionTop(protyle));
+ const selectionBounds = this.getMobileSelectionBounds(protyle);
+ this.setMobilePosition(selectionBounds.top, selectionBounds.bottom);
/// #endif
}
} else if (!this.element.querySelector(".fn__loading")) {
@@ -507,7 +514,7 @@ export class Hint {
/// #if !MOBILE
setPosition(this.element, cellRect.left, cellRect.bottom, cellRect.height);
/// #else
- this.setMobilePosition(cellRect.top);
+ this.setMobilePosition(cellRect.top, cellRect.bottom);
/// #endif
}
} else {
@@ -515,7 +522,8 @@ export class Hint {
/// #if !MOBILE
setPosition(this.element, textareaPosition.left, textareaPosition.top + 26, 30);
/// #else
- this.setMobilePosition(this.getMobileSelectionTop(protyle));
+ const selectionBounds = this.getMobileSelectionBounds(protyle);
+ this.setMobilePosition(selectionBounds.top, selectionBounds.bottom);
/// #endif
}
this.element.scrollTop = 0;
diff --git a/app/src/protyle/hint/mobileHintPosition.test.ts b/app/src/protyle/hint/mobileHintPosition.test.ts
index ff51482123..51fe9ecb54 100644
--- a/app/src/protyle/hint/mobileHintPosition.test.ts
+++ b/app/src/protyle/hint/mobileHintPosition.test.ts
@@ -4,23 +4,41 @@ import {getMobileHintPosition} from "./mobileHintPosition";
describe("getMobileHintPosition", () => {
it("keeps the hint directly above the caret", () => {
- assert.deepEqual(getMobileHintPosition(420, 200, 48, 500, Infinity, 8), {
+ assert.deepEqual(getMobileHintPosition(420, 446, 200, 48, 500, Infinity, 8), {
maxHeight: 364,
top: 212,
});
});
it("limits the hint to part of the visible viewport", () => {
- assert.deepEqual(getMobileHintPosition(420, 400, 48, 500, 226, 8), {
+ assert.deepEqual(getMobileHintPosition(420, 446, 400, 48, 500, 226, 8), {
maxHeight: 226,
top: 186,
});
});
- it("keeps the hint inside the viewport when little space is available", () => {
- assert.deepEqual(getMobileHintPosition(60, 100, 48, 500, Infinity, 8), {
- maxHeight: 4,
+ it("places the hint below the caret when the space above is too narrow", () => {
+ assert.deepEqual(getMobileHintPosition(100, 126, 200, 48, 500, 150, 8), {
+ maxHeight: 150,
+ top: 134,
+ });
+ assert.deepEqual(getMobileHintPosition(100, 126, 40, 48, 500, 150, 8), {
+ maxHeight: 150,
+ top: 134,
+ });
+ });
+
+ it("keeps the hint above when the space below is even narrower", () => {
+ assert.deepEqual(getMobileHintPosition(100, 126, 200, 48, 160, 150, 8), {
+ maxHeight: 44,
top: 48,
});
});
+
+ it("uses the configured height limit as the usable-height threshold", () => {
+ assert.deepEqual(getMobileHintPosition(100, 126, 200, 48, 500, 40, 8), {
+ maxHeight: 40,
+ top: 52,
+ });
+ });
});
diff --git a/app/src/protyle/hint/mobileHintPosition.ts b/app/src/protyle/hint/mobileHintPosition.ts
index 1330073433..2437ec1658 100644
--- a/app/src/protyle/hint/mobileHintPosition.ts
+++ b/app/src/protyle/hint/mobileHintPosition.ts
@@ -1,10 +1,18 @@
-export const getMobileHintPosition = (anchorTop: number, hintHeight: number, viewportTop: number,
- viewportBottom: number, heightLimit = Infinity, gap = 0) => {
+const MOBILE_HINT_USABLE_HEIGHT = 128;
+
+export const getMobileHintPosition = (anchorTop: number, anchorBottom: number, hintHeight: number,
+ viewportTop: number, viewportBottom: number, heightLimit = Infinity,
+ gap = 0) => {
const visibleAnchorTop = Math.max(viewportTop, Math.min(anchorTop, viewportBottom));
- const maxHeight = Math.min(Math.max(0, visibleAnchorTop - viewportTop - gap), Math.max(0, heightLimit));
+ const visibleAnchorBottom = Math.max(visibleAnchorTop, Math.min(anchorBottom, viewportBottom));
+ const spaceAbove = Math.max(0, visibleAnchorTop - viewportTop - gap);
+ const spaceBelow = Math.max(0, viewportBottom - visibleAnchorBottom - gap);
+ const usableHeight = Math.min(MOBILE_HINT_USABLE_HEIGHT, Math.max(0, heightLimit));
+ const below = spaceAbove < usableHeight && spaceBelow > spaceAbove;
+ const maxHeight = Math.min(below ? spaceBelow : spaceAbove, Math.max(0, heightLimit));
const height = Math.min(hintHeight, maxHeight);
return {
maxHeight,
- top: visibleAnchorTop - gap - height,
+ top: below ? visibleAnchorBottom + gap : visibleAnchorTop - gap - height,
};
};