Skip to content

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

:bug: Keep mobile block hints above the caret https://github.com/siyuan-note/siyuan/issues/19037

Daniel1ad08dcbe665
diff --git a/app/src/protyle/hint/index.ts b/app/src/protyle/hint/index.ts
index 8546b56d85..016d329ae4 100644
--- a/app/src/protyle/hint/index.ts
+++ b/app/src/protyle/hint/index.ts
@@ -351,7 +351,7 @@ export class Hint {
Constants.BLOCK_HINT_CLOSE_KEYS[this.splitChar], Constants.SIZE_TITLE);
}
- private getMobileSelectionBounds(protyle: IProtyle) {
+ private getMobileSelectionTop(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) {
@@ -368,23 +368,13 @@ export class Hint {
const rects = textRange.getClientRects();
const rect = rects[rects.length - 1];
if (rect?.height > 0) {
- return {
- top: rect.top,
- bottom: rect.bottom,
- };
+ return rect.top;
}
}
- const rangeElement = range.startContainer.nodeType === 3 ?
- range.startContainer.parentElement : range.startContainer as HTMLElement;
- const computedLineHeight = rangeElement ? parseFloat(getComputedStyle(rangeElement).lineHeight) : NaN;
- const lineHeight = Number.isNaN(computedLineHeight) ? 26 : computedLineHeight;
- return {
- top: position.top - lineHeight,
- bottom: position.top + lineHeight,
- };
+ return position.top;
}
- private setMobilePosition(anchorTop: number, anchorBottom: number) {
+ private setMobilePosition(anchorTop: number) {
const viewportBounds = getVisibleViewportBounds();
const viewportTop = Math.max(viewportBounds.top, getTopBarHeight());
let viewportBottom = viewportBounds.bottom;
@@ -394,10 +384,15 @@ export class Hint {
}
viewportBottom = Math.max(viewportTop, viewportBottom);
const heightLimit = (viewportBottom - viewportTop) / 3;
- const gap = 4;
- const position = getMobileHintPosition(anchorTop, anchorBottom, viewportTop, viewportBottom,
+ const gap = 8;
+ let position = getMobileHintPosition(anchorTop, this.element.scrollHeight, viewportTop, viewportBottom,
heightLimit, gap);
- this.element.style.maxHeight = `${position.maxHeight}px`;
+ 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);
this.element.style.left = "0";
this.element.style.top = `${position.top}px`;
}
@@ -414,7 +409,7 @@ export class Hint {
/// #if !MOBILE
setPosition(this.element, cellRect.left, cellRect.bottom, cellRect.height);
/// #else
- this.setMobilePosition(cellRect.top, cellRect.bottom);
+ this.setMobilePosition(cellRect.top);
/// #endif
}
} else {
@@ -422,8 +417,7 @@ export class Hint {
/// #if !MOBILE
setPosition(this.element, textareaPosition.left, textareaPosition.top + 26, 30);
/// #else
- const selectionBounds = this.getMobileSelectionBounds(protyle);
- this.setMobilePosition(selectionBounds.top, selectionBounds.bottom);
+ this.setMobilePosition(this.getMobileSelectionTop(protyle));
/// #endif
}
} else if (!this.element.querySelector(".fn__loading")) {
@@ -512,7 +506,7 @@ export class Hint {
/// #if !MOBILE
setPosition(this.element, cellRect.left, cellRect.bottom, cellRect.height);
/// #else
- this.setMobilePosition(cellRect.top, cellRect.bottom);
+ this.setMobilePosition(cellRect.top);
/// #endif
}
} else {
@@ -520,8 +514,7 @@ export class Hint {
/// #if !MOBILE
setPosition(this.element, textareaPosition.left, textareaPosition.top + 26, 30);
/// #else
- const selectionBounds = this.getMobileSelectionBounds(protyle);
- this.setMobilePosition(selectionBounds.top, selectionBounds.bottom);
+ this.setMobilePosition(this.getMobileSelectionTop(protyle));
/// #endif
}
this.element.scrollTop = 0;
diff --git a/app/src/protyle/hint/mobileHintPosition.test.ts b/app/src/protyle/hint/mobileHintPosition.test.ts
index 24b54a1ed2..ff51482123 100644
--- a/app/src/protyle/hint/mobileHintPosition.test.ts
+++ b/app/src/protyle/hint/mobileHintPosition.test.ts
@@ -3,53 +3,24 @@ 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, 48, 500), {
- maxHeight: 394,
- top: 106,
- });
- });
-
- it("places the hint above the caret without covering it", () => {
- assert.deepEqual(getMobileHintPosition(420, 446, 48, 500), {
- maxHeight: 372,
- top: 48,
- });
- });
-
- it("limits the hint to the available space above the caret", () => {
- assert.deepEqual(getMobileHintPosition(180, 206, 48, 250), {
- maxHeight: 132,
- top: 48,
- });
- });
-
- it("keeps the hint inside the viewport when the caret is obscured", () => {
- assert.deepEqual(getMobileHintPosition(540, 566, 48, 500), {
- maxHeight: 452,
- top: 48,
- });
- assert.deepEqual(getMobileHintPosition(10, 36, 48, 500), {
- maxHeight: 452,
- top: 48,
+ it("keeps the hint directly above the caret", () => {
+ assert.deepEqual(getMobileHintPosition(420, 200, 48, 500, Infinity, 8), {
+ maxHeight: 364,
+ top: 212,
});
});
it("limits the hint to part of the visible viewport", () => {
- assert.deepEqual(getMobileHintPosition(420, 446, 48, 500, 226), {
+ assert.deepEqual(getMobileHintPosition(420, 400, 48, 500, 226, 8), {
maxHeight: 226,
- top: 48,
+ top: 186,
});
});
- it("leaves a gap around the caret line", () => {
- assert.deepEqual(getMobileHintPosition(420, 446, 48, 500, Infinity, 4), {
- maxHeight: 368,
+ it("keeps the hint inside the viewport when little space is available", () => {
+ assert.deepEqual(getMobileHintPosition(60, 100, 48, 500, Infinity, 8), {
+ maxHeight: 4,
top: 48,
});
- assert.deepEqual(getMobileHintPosition(80, 106, 48, 500, Infinity, 4), {
- maxHeight: 390,
- top: 110,
- });
});
});
diff --git a/app/src/protyle/hint/mobileHintPosition.ts b/app/src/protyle/hint/mobileHintPosition.ts
index 94daf730ae..1330073433 100644
--- a/app/src/protyle/hint/mobileHintPosition.ts
+++ b/app/src/protyle/hint/mobileHintPosition.ts
@@ -1,14 +1,10 @@
-export const getMobileHintPosition = (anchorTop: number, anchorBottom: number, viewportTop: number,
- viewportBottom: number, heightLimit = Infinity,
- gap = 0) => {
+export const getMobileHintPosition = (anchorTop: number, hintHeight: number, viewportTop: number,
+ viewportBottom: number, heightLimit = Infinity, gap = 0) => {
const visibleAnchorTop = Math.max(viewportTop, Math.min(anchorTop, viewportBottom));
- 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 below = spaceBelow >= spaceAbove;
- const maxHeight = Math.min(below ? spaceBelow : spaceAbove, Math.max(0, heightLimit));
+ const maxHeight = Math.min(Math.max(0, visibleAnchorTop - viewportTop - gap), Math.max(0, heightLimit));
+ const height = Math.min(hintHeight, maxHeight);
return {
maxHeight,
- top: below ? visibleAnchorBottom + gap : viewportTop,
+ top: visibleAnchorTop - gap - height,
};
};