Skip to content

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

:art: https://github.com/siyuan-note/siyuan/issues/16115

Vanessa95552da8142f
diff --git a/app/src/protyle/toolbar/Font.ts b/app/src/protyle/toolbar/Font.ts
index 2f6a39d056..b4f7238294 100644
--- a/app/src/protyle/toolbar/Font.ts
+++ b/app/src/protyle/toolbar/Font.ts
@@ -1,7 +1,6 @@
import {setStorageVal, updateHotkeyTip} from "../util/compatibility";
import {ToolbarItem} from "./ToolbarItem";
-import {setPosition} from "../../util/setPosition";
-import {focusByRange, getSelectionPosition} from "../util/selection";
+import {focusByRange} from "../util/selection";
import {Constants} from "../../constants";
import {hasClosestBlock, hasClosestByAttribute} from "../util/hasClosest";
import {updateBatchTransaction} from "../wysiwyg/transaction";
@@ -23,8 +22,7 @@ export class Font extends ToolbarItem {
protyle.toolbar.subElementCloseCB = undefined;
focusByRange(protyle.toolbar.range);
/// #if !MOBILE
- const position = getSelectionPosition(protyle.wysiwyg.element, protyle.toolbar.range);
- setPosition(protyle.toolbar.subElement, position.left, position.top + 18, 26);
+ protyle.toolbar.setSelectionElementPosition(protyle, protyle.toolbar.subElement);
/// #endif
});
}
diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.ts
index 387cd0840c..8c08a8c7c7 100644
--- a/app/src/protyle/toolbar/index.ts
+++ b/app/src/protyle/toolbar/index.ts
@@ -72,13 +72,22 @@ const filterPluginToolbar = (toolbar: Array<string | IMenuItem>, lite: boolean)
});
};
+interface IToolbarRangePosition {
+ left: number;
+ top: number;
+ isBottom?: boolean;
+ rectIndex?: number;
+}
+
export class Toolbar {
public element: HTMLElement;
public subElement: HTMLElement;
public subElementCloseCB: () => void;
public subElementResizeCB: () => void;
public range: Range;
+ public rangePosition?: IToolbarRangePosition;
public toolbarHeight: number;
+ private isMultipleClick = false;
private readonly LINE_HEIGHT = 32;
constructor(protyle: IProtyle) {
@@ -183,8 +192,33 @@ export class Toolbar {
});
}
- public render(protyle: IProtyle, range: Range) {
+ public setSelectionElementPosition(protyle: IProtyle, element: HTMLElement) {
+ if (!this.rangePosition || !this.range) {
+ return;
+ }
+ const protyleRect = protyle.element.getBoundingClientRect();
+ const rangeRects = this.range.getClientRects();
+ const rangeRect = (typeof this.rangePosition.rectIndex === "number" ?
+ rangeRects[this.rangePosition.rectIndex] : undefined) ||
+ (this.rangePosition.isBottom ? rangeRects[rangeRects.length - 1] : rangeRects[0]) ||
+ this.range.getBoundingClientRect();
+ const gap = this.isMultipleClick ? 2 : 4;
+ const above = rangeRect.top - element.clientHeight - gap;
+ const below = rangeRect.bottom + gap;
+ const y = this.rangePosition.isBottom ?
+ (below + element.clientHeight <= protyleRect.bottom ?
+ below : Math.max(above, protyleRect.top + 30)) :
+ (above >= protyleRect.top + 30 ?
+ above : Math.min(below, protyleRect.bottom - element.clientHeight));
+ const horizontalDivisor = this.isMultipleClick ? 3 : 4;
+ setPosition(element, this.rangePosition.left - element.clientWidth / horizontalDivisor, y);
+ return y;
+ }
+
+ public render(protyle: IProtyle, range: Range, position?: IPosition & {detail?: number}) {
this.range = range;
+ this.rangePosition = undefined;
+ this.isMultipleClick = (position?.detail || 0) > 1;
const nodeElement = hasClosestBlock(range.startContainer);
const endElement = hasClosestBlock(range.endContainer);
const isCrossBlock = !!nodeElement && !!endElement && nodeElement !== endElement;
@@ -215,20 +249,15 @@ export class Toolbar {
this.element.classList.add("fn__none");
return;
}
- const rangePosition = getSelectionPosition(nodeElement, range, true);
+ this.rangePosition = getSelectionPosition(nodeElement, range, true, position);
this.element.classList.remove("fn__none");
this.toolbarHeight = this.element.clientHeight;
- const protyleRect = protyle.element.getBoundingClientRect();
- const rangeRects = range.getClientRects();
- const rangeRect = (rangePosition.isBottom ? rangeRects[rangeRects.length - 1] : rangeRects[0]) ||
- range.getBoundingClientRect();
- const above = rangeRect.top - this.toolbarHeight - 4;
- const below = rangeRect.bottom + 4;
- const y = rangePosition.isBottom ?
- (below + this.toolbarHeight <= protyleRect.bottom ? below : Math.max(above, protyleRect.top + 30)) :
- (above >= protyleRect.top + 30 ? above : Math.min(below, protyleRect.bottom - this.toolbarHeight));
+ const y = this.setSelectionElementPosition(protyle, this.element);
+ if (typeof y !== "number") {
+ this.element.classList.add("fn__none");
+ return;
+ }
this.element.setAttribute("data-inity", y + Constants.ZWSP + protyle.contentElement.scrollTop.toString());
- setPosition(this.element, rangePosition.left - this.element.clientWidth / 4, y);
this.element.querySelectorAll(".protyle-toolbar__item--current").forEach(item => {
item.classList.remove("protyle-toolbar__item--current");
diff --git a/app/src/protyle/util/selection.ts b/app/src/protyle/util/selection.ts
index 257345eed1..80c02b6421 100644
--- a/app/src/protyle/util/selection.ts
+++ b/app/src/protyle/util/selection.ts
@@ -228,7 +228,53 @@ export const getEditorRange = (element: Element): Range => {
return range;
};
-export const getSelectionPosition = (nodeElement: Element, range?: Range, useDirect = false) => {
+interface IClosestSelectionRect {
+ rect: DOMRect;
+ rectIndex: number;
+ left: number;
+ verticalDistance: number;
+ horizontalDistance: number;
+}
+
+const getDistanceToInterval = (value: number, start: number, end: number) => {
+ if (value < start) {
+ return start - value;
+ }
+ if (value > end) {
+ return value - end;
+ }
+ return 0;
+};
+
+const getClosestSelectionRect = (rects: DOMRectList, position: IPosition) => {
+ const rectArray = Array.from(rects);
+ const hasTextRect = rectArray.some(rect => rect.width > 0.5 && rect.height > 0.5);
+ let closest: IClosestSelectionRect | undefined;
+ rectArray.forEach((rect, rectIndex) => {
+ if (hasTextRect && (rect.width <= 0.5 || rect.height <= 0.5)) {
+ return;
+ }
+ const verticalDistance = getDistanceToInterval(position.y, rect.top, rect.bottom);
+ const horizontalDistance = getDistanceToInterval(position.x, rect.left, rect.right);
+ // 文本按行排列,优先比较垂直距离,避免较长的其他行因水平距离较近而被选中
+ if (!closest ||
+ verticalDistance < closest.verticalDistance - 0.5 ||
+ (Math.abs(verticalDistance - closest.verticalDistance) <= 0.5 &&
+ horizontalDistance < closest.horizontalDistance)) {
+ closest = {
+ rect,
+ rectIndex,
+ left: Math.max(rect.left, Math.min(position.x, rect.right)),
+ verticalDistance,
+ horizontalDistance,
+ };
+ }
+ });
+ return closest;
+};
+
+export const getSelectionPosition = (nodeElement: Element, range?: Range, useDirect = false,
+ position?: IPosition) => {
if (!range) {
range = getEditorRange(nodeElement);
}
@@ -324,6 +370,29 @@ export const getSelectionPosition = (nodeElement: Element, range?: Range, useDir
const rects = range.getClientRects(); // 由于长度过长折行,光标在行首时有多个 rects https://github.com/siyuan-note/siyuan/issues/6156
if (range.toString()) {
if (useDirect) {
+ if (position) {
+ const closest = getClosestSelectionRect(rects, position);
+ if (closest) {
+ const textRects = Array.from(rects).filter(rect => rect.width > 0.5 && rect.height > 0.5);
+ const compareRects = textRects.length > 0 ? textRects : Array.from(rects);
+ const maxTop = Math.max(...compareRects.map(rect => rect.top));
+ const minBottom = Math.min(...compareRects.map(rect => rect.bottom));
+ const isSingleLine = maxTop <= minBottom + 0.5;
+ let isBottom = false;
+ if (!isSingleLine) {
+ const centers = compareRects.map(rect => (rect.top + rect.bottom) / 2);
+ const closestCenter = (closest.rect.top + closest.rect.bottom) / 2;
+ isBottom = Math.abs(Math.max(...centers) - closestCenter) <
+ Math.abs(closestCenter - Math.min(...centers));
+ }
+ return {
+ left: closest.left,
+ top: isBottom ? closest.rect.bottom : closest.rect.top,
+ isBottom,
+ rectIndex: closest.rectIndex,
+ };
+ }
+ }
const selection = window.getSelection() as Selection & {
direction: "forward" | "backward" | "none"
};
@@ -337,7 +406,8 @@ export const getSelectionPosition = (nodeElement: Element, range?: Range, useDir
left: isBackward ? rects[0].left : rects[rects.length - 1].right,
// 如果向右选择时有多个垂直位置不同的矩形:使用最后一个矩形的下边界;否则使用第一个矩形的上边界
top: isBottom ? rects[rects.length - 1].bottom : rects[0].top,
- isBottom
+ isBottom,
+ rectIndex: isBottom ? rects.length - 1 : 0,
};
} else {
return { // 选中多行不应遮挡第一行 https://github.com/siyuan-note/siyuan/issues/7541
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index f10e2dfd85..ca434592b7 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -1723,7 +1723,11 @@ export class WYSIWYG {
if (getSelection().rangeCount > 0) {
const range = getSelection().getRangeAt(0);
if (range.toString().replace(Constants.ZWSP, "") !== "") {
- protyle.toolbar.render(protyle, range);
+ protyle.toolbar.render(protyle, range, {
+ x: mouseUpEvent.clientX,
+ y: mouseUpEvent.clientY,
+ detail: mouseUpEvent.detail,
+ });
countSelectWord(range, protyle.block.rootID);
}
}
@@ -4411,7 +4415,11 @@ export class WYSIWYG {
}
/// #if !MOBILE
if (newRange.toString().replace(Constants.ZWSP, "") !== "") {
- protyle.toolbar.render(protyle, newRange);
+ protyle.toolbar.render(protyle, newRange, event.detail > 0 ? {
+ x: event.clientX,
+ y: event.clientY,
+ detail: event.detail,
+ } : undefined);
} else {
// https://github.com/siyuan-note/siyuan/issues/9785
protyle.toolbar.range = newRange;