Skip to content

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

Improve toolbar position calculation (#15983)

Jeffrey Chen4aede9687ef0
diff --git a/app/src/assets/scss/protyle/_toolbar.scss b/app/src/assets/scss/protyle/_toolbar.scss
index 68efa97ca5..97229f8353 100644
--- a/app/src/assets/scss/protyle/_toolbar.scss
+++ b/app/src/assets/scss/protyle/_toolbar.scss
@@ -6,7 +6,10 @@
box-shadow: var(--b3-point-shadow);
border-radius: var(--b3-border-radius);
display: flex;
- transition: top .15s cubic-bezier(0, 0, .2, 1) 0ms;
+
+ &__transition {
+ transition: top .15s cubic-bezier(0, 0, .2, 1) 0ms;
+ }
&__item {
color: var(--b3-theme-on-surface);
diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.ts
index 441e8fad71..db499d8ed9 100644
--- a/app/src/protyle/toolbar/index.ts
+++ b/app/src/protyle/toolbar/index.ts
@@ -164,12 +164,17 @@ export class Toolbar {
this.element.classList.add("fn__none");
return;
}
- const rangePosition = getSelectionPosition(nodeElement, range);
- this.element.classList.remove("fn__none");
+ this.element.classList.remove("fn__none", "protyle-toolbar__transition");
+
this.toolbarHeight = this.element.clientHeight;
- const y = rangePosition.top - this.toolbarHeight - 4;
+ const rangePosition = getSelectionPosition(nodeElement, range, this.element.clientWidth);
+ const y = rangePosition.isToolbarAtBottom ?
+ Math.min(rangePosition.top + 4, protyle.element.getBoundingClientRect().bottom - this.toolbarHeight) :
+ Math.max(rangePosition.top - this.toolbarHeight - 4, protyle.element.getBoundingClientRect().top + 30);
this.element.setAttribute("data-inity", y + Constants.ZWSP + protyle.contentElement.scrollTop.toString());
- setPosition(this.element, rangePosition.left - 52, Math.max(y, protyle.element.getBoundingClientRect().top + 30));
+ setPosition(this.element, rangePosition.left, y);
+
+ this.element.classList.add("protyle-toolbar__transition");
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 e839b34c33..79d6a8e0f7 100644
--- a/app/src/protyle/util/selection.ts
+++ b/app/src/protyle/util/selection.ts
@@ -194,7 +194,7 @@ export const getEditorRange = (element: Element): Range => {
return range;
};
-export const getSelectionPosition = (nodeElement: Element, range?: Range) => {
+export const getSelectionPosition = (nodeElement: Element, range?: Range, toolbarWidth?: number) => {
if (!range) {
range = getEditorRange(nodeElement);
}
@@ -261,9 +261,24 @@ export const getSelectionPosition = (nodeElement: Element, range?: Range) => {
} else {
const rects = range.getClientRects(); // 由于长度过长折行,光标在行首时有多个 rects https://github.com/siyuan-note/siyuan/issues/6156
if (range.toString()) {
- return { // 选中多行不应遮挡第一行 https://github.com/siyuan-note/siyuan/issues/7541
- left: rects[rects.length - 1].left,
- top: rects[0].top
+ const selection = window.getSelection();
+ // 判断选择方向
+ const isBackward = selection && "direction" in selection ?
+ (selection as { direction: "forward" | "backward" | "none" }).direction === "backward"
+ : range.startContainer === selection?.focusNode && range.startOffset === selection?.focusOffset;
+ let isToolbarAtBottom = false;
+ if (!isBackward) {
+ // 检查是否有多个垂直位置不同的矩形
+ isToolbarAtBottom = rects.length > 1 && Array.from(rects).some((rect: DOMRect) => rect.top !== rects[0].top);
+ }
+
+ return {
+ // 向左选择:使用第一个矩形的左边界;向右选择:使用最后一个矩形的右边界
+ // 减去工具栏宽度的1/4:将工具栏中不太常用的按钮往右偏一点
+ left: (isBackward ? rects[0].left : rects[rects.length - 1].right) - (toolbarWidth || 0) / 4,
+ // 如果向右选择时有多个垂直位置不同的矩形:使用最后一个矩形的下边界;否则使用第一个矩形的上边界
+ top: isToolbarAtBottom ? rects[rects.length - 1].bottom : rects[0].top,
+ isToolbarAtBottom: isToolbarAtBottom,
};
} else {
return { // 代码块首 https://github.com/siyuan-note/siyuan/issues/13113