Skip to content

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

:bug: Correct caret placement at wrapped line edges https://github.com/siyuan-note/siyuan/issues/18789

Daniel8864a6f180ed
diff --git a/app/src/protyle/wysiwyg/blockEdgeCaret.test.ts b/app/src/protyle/wysiwyg/blockEdgeCaret.test.ts
index 6cd84219dd..39b9b6e232 100644
--- a/app/src/protyle/wysiwyg/blockEdgeCaret.test.ts
+++ b/app/src/protyle/wysiwyg/blockEdgeCaret.test.ts
@@ -1,6 +1,6 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
-import {getBlockEdgeCaretPoint, isCaretRangeInsideElement} from "./blockEdgeCaret";
+import {getBlockEdgeCaretPoint, getBlockEdgeCaretRange, isCaretRangeInsideElement} from "./blockEdgeCaret";
const editableRect = {
left: 40,
@@ -59,11 +59,13 @@ class TestElement {
}
}
-const createRange = (startContainer: TestElement, endContainer = startContainer, collapsed = true) => ({
+const createRange = (startContainer: TestElement, endContainer = startContainer, collapsed = true,
+ rect = {top: 100, bottom: 120}) => ({
collapsed,
startContainer: startContainer as unknown as Node,
endContainer: endContainer as unknown as Node,
-}) as Range;
+ getClientRects: () => [rect],
+}) as unknown as Range;
describe("isCaretRangeInsideElement", () => {
it("accepts a collapsed caret inside the target editable element", () => {
@@ -87,3 +89,52 @@ describe("isCaretRangeInsideElement", () => {
assert.equal(isCaretRangeInsideElement(createRange(text, text, false), editable as unknown as Element), false);
});
});
+
+describe("getBlockEdgeCaretRange", () => {
+ it("moves a direct range to the clicked edge of its visual line", () => {
+ const text = new TestElement();
+ const editable = new TestElement().append(text);
+ const range = createRange(text);
+
+ assert.deepEqual(getBlockEdgeCaretRange(270, 110, 30, 250, editableRect,
+ editable as unknown as Element, () => range), {
+ range,
+ lineBoundaryDirection: "right",
+ });
+ });
+
+ it("moves toward the right edge from the closest range on a softly wrapped line", () => {
+ const text = new TestElement();
+ const editable = new TestElement().append(text);
+ const targetLineRange = createRange(text);
+ const nextLineRange = createRange(text, text, true, {top: 130, bottom: 150});
+
+ const caret = getBlockEdgeCaretRange(270, 110, 30, 250, editableRect,
+ editable as unknown as Element, (x) => x > 200 ? nextLineRange : targetLineRange);
+
+ assert.equal(caret.range, targetLineRange);
+ assert.equal(caret.lineBoundaryDirection, "right");
+ });
+
+ it("moves toward the left edge when its direct range belongs to the previous visual line", () => {
+ const text = new TestElement();
+ const editable = new TestElement().append(text);
+ const targetLineRange = createRange(text, text, true, {top: 130, bottom: 150});
+ const previousLineRange = createRange(text);
+
+ const caret = getBlockEdgeCaretRange(20, 140, 30, 250, editableRect,
+ editable as unknown as Element, (x) => x < 80 ? previousLineRange : targetLineRange);
+
+ assert.equal(caret.range, targetLineRange);
+ assert.equal(caret.lineBoundaryDirection, "left");
+ });
+
+ it("falls back when no range on the clicked visual line can be found", () => {
+ const text = new TestElement();
+ const editable = new TestElement().append(text);
+ const nextLineRange = createRange(text, text, true, {top: 130, bottom: 150});
+
+ assert.equal(getBlockEdgeCaretRange(270, 110, 30, 250, editableRect,
+ editable as unknown as Element, () => nextLineRange), undefined);
+ });
+});
diff --git a/app/src/protyle/wysiwyg/blockEdgeCaret.ts b/app/src/protyle/wysiwyg/blockEdgeCaret.ts
index 81db76f9fd..3378d2dcb0 100644
--- a/app/src/protyle/wysiwyg/blockEdgeCaret.ts
+++ b/app/src/protyle/wysiwyg/blockEdgeCaret.ts
@@ -1,4 +1,10 @@
type TBlockEdgeCaretRect = Pick<DOMRect, "left" | "right" | "top" | "bottom">;
+type TBlockEdgeCaretDirection = "left" | "right";
+
+interface IBlockEdgeCaretRange {
+ range: Range;
+ lineBoundaryDirection: TBlockEdgeCaretDirection;
+}
export const getBlockEdgeCaretPoint = (x: number, y: number, contentLeft: number, contentRight: number,
editableRect: TBlockEdgeCaretRect) => {
@@ -24,3 +30,51 @@ export const isCaretRangeInsideElement = (range: Range | null | undefined, eleme
const contains = (node: Node) => node === element || element.contains(node);
return contains(range.startContainer) && contains(range.endContainer);
};
+
+const isCaretRangeOnVisualLine = (range: Range, y: number) => {
+ const rects = range.getClientRects();
+ if (rects.length === 0) {
+ return false;
+ }
+ const rect = rects[rects.length - 1];
+ return y >= rect.top - 4 && y <= rect.bottom + 4;
+};
+
+export const getBlockEdgeCaretRange = (x: number, y: number, contentLeft: number, contentRight: number,
+ editableRect: TBlockEdgeCaretRect, editableElement: Element,
+ rangeFromPoint: (x: number, y: number) => Range | null):
+ IBlockEdgeCaretRange | undefined => {
+ const point = getBlockEdgeCaretPoint(x, y, contentLeft, contentRight, editableRect);
+ if (!point) {
+ return;
+ }
+ const fromLeft = x < contentLeft;
+ const getValidRange = (probeX: number) => {
+ const range = rangeFromPoint(probeX, point.y);
+ return isCaretRangeInsideElement(range, editableElement) ? range : undefined;
+ };
+ const edgeRange = getValidRange(point.x);
+ if (!edgeRange) {
+ return;
+ }
+ if (isCaretRangeOnVisualLine(edgeRange, point.y)) {
+ return {
+ range: edgeRange,
+ lineBoundaryDirection: fromLeft ? "left" : "right",
+ };
+ }
+
+ // 软折行边界的 Range 可能显示在相邻行,先向内容内部寻找目标视觉行
+ const horizontalInset = Math.min(4, (editableRect.right - editableRect.left) / 2);
+ const oppositeX = fromLeft ? editableRect.right - horizontalInset : editableRect.left + horizontalInset;
+ for (let index = 1; index <= 16; index++) {
+ const probeX = point.x + (oppositeX - point.x) * index / 16;
+ const range = getValidRange(probeX);
+ if (range && isCaretRangeOnVisualLine(range, point.y)) {
+ return {
+ range,
+ lineBoundaryDirection: fromLeft ? "left" : "right",
+ };
+ }
+ }
+};
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index 4d0a09a81c..54e7a3a725 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -193,7 +193,7 @@ import {
import {isCrossBlockTextRange} from "../gutter/multiSelect";
import {formatPainter} from "../toolbar/FormatPainter";
import {shouldOpenListItemAttr} from "./listContext";
-import {getBlockEdgeCaretPoint, isCaretRangeInsideElement} from "./blockEdgeCaret";
+import {getBlockEdgeCaretRange, isCaretRangeInsideElement} from "./blockEdgeCaret";
interface IShiftClickBlockPoint {
blockElement: HTMLElement;
@@ -234,16 +234,20 @@ const focusTextBlockEdgeByPoint = (blockElement: HTMLElement, x: number, y: numb
!["NodeParagraph", "NodeHeading"].includes(editableBlockElement.getAttribute("data-type"))) {
return false;
}
- const point = getBlockEdgeCaretPoint(x, y, contentLeft, contentRight,
- editableElement.getBoundingClientRect());
- if (!point) {
+ const caret = getBlockEdgeCaretRange(x, y, contentLeft, contentRight,
+ editableElement.getBoundingClientRect(), editableElement,
+ (pointX, pointY) => document.caretRangeFromPoint(pointX, pointY));
+ if (!caret) {
return false;
}
- const range = document.caretRangeFromPoint(point.x, point.y);
- if (!isCaretRangeInsideElement(range, editableElement)) {
+ focusByRange(caret.range);
+ // Range 可能吸附到目标行的另一端,由浏览器按视觉行移动到点击侧的行边界
+ const selection = getSelection();
+ selection.modify("move", caret.lineBoundaryDirection, "lineboundary");
+ const movedRange = selection.rangeCount > 0 ? selection.getRangeAt(0) : undefined;
+ if (!isCaretRangeInsideElement(movedRange, editableElement)) {
return false;
}
- focusByRange(range);
return true;
};