Skip to content

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

:bug: Keep document drop options visible with a single tab https://github.com/siyuan-note/siyuan/issues/19114

Daniel1de30b4f4abd
diff --git a/app/src/assets/scss/business/_layout.scss b/app/src/assets/scss/business/_layout.scss
index e1a70d4053..1750d7c8c4 100644
--- a/app/src/assets/scss/business/_layout.scss
+++ b/app/src/assets/scss/business/_layout.scss
@@ -203,6 +203,44 @@
background-color: var(--b3-theme-primary-lightest);
}
+ &-bars--document-drop {
+ position: relative;
+
+ > .item__document-drop {
+ align-items: stretch;
+ background-color: var(--b3-theme-background);
+ border-radius: var(--b3-border-radius);
+ box-shadow: 0 0 0 1px var(--b3-theme-primary-lighter) inset, var(--b3-point-shadow);
+ direction: ltr;
+ display: flex;
+ max-width: calc(100% - 8px);
+ overflow: hidden;
+ position: absolute;
+ transform: translateX(-50%);
+ width: 200px;
+ z-index: 4;
+
+ .item__document-drop-option {
+ align-items: center;
+ direction: initial;
+ display: flex;
+ flex: 1;
+ font-size: 12px;
+ justify-content: center;
+ min-width: 0;
+ overflow: hidden;
+ padding: 0 8px;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+
+ &--active {
+ background-color: var(--b3-theme-primary-lightest);
+ color: var(--b3-theme-primary);
+ }
+ }
+ }
+ }
+
&-bar {
list-style: none;
align-items: center;
@@ -245,42 +283,9 @@
&--document-drop {
cursor: grabbing;
- overflow: visible;
z-index: 3;
}
- &__document-drop {
- align-items: stretch;
- background-color: var(--b3-theme-background);
- border-radius: var(--b3-border-radius);
- box-shadow: 0 0 0 1px var(--b3-theme-primary-lighter) inset, var(--b3-point-shadow);
- direction: ltr;
- display: flex;
- height: 100%;
- left: 50%;
- min-width: 200px;
- overflow: hidden;
- position: absolute;
- top: 0;
- transform: translateX(-50%);
-
- &-option {
- align-items: center;
- direction: initial;
- display: flex;
- flex: 1;
- font-size: 12px;
- justify-content: center;
- padding: 0 8px;
- white-space: nowrap;
-
- &--active {
- background-color: var(--b3-theme-primary-lightest);
- color: var(--b3-theme-primary);
- }
- }
- }
-
&--full {
max-width: none;
flex: 1;
diff --git a/app/src/layout/Wnd.ts b/app/src/layout/Wnd.ts
index b4be4e9194..5b726c4685 100644
--- a/app/src/layout/Wnd.ts
+++ b/app/src/layout/Wnd.ts
@@ -57,6 +57,7 @@ import {
clearTabHoverSwitch,
findDefaultTabNextId,
findNextTabId,
+ getDocumentTabMovePreviewLeft,
getDocumentTabMovePosition,
reorderTabItems,
scheduleTabHoverSwitch
@@ -70,7 +71,10 @@ interface IDocumentTabMoveTarget {
}
const getDocumentTabMoveTarget = (target: HTMLElement, headersElement: HTMLElement) => {
- const tabHeaderElement = hasClosestByAttribute(target, "data-type", "tab-header");
+ const dropElement = hasClosestByClassName(target, "item__document-drop");
+ const tabHeaderElement = dropElement ? (Array.from(headersElement.children) as HTMLElement[]).find((item) =>
+ item.dataset.id === dropElement.dataset.targetTabId) :
+ hasClosestByAttribute(target, "data-type", "tab-header");
if (!tabHeaderElement || !headersElement.contains(tabHeaderElement)) {
return;
}
@@ -88,18 +92,27 @@ const getDocumentTabMoveTarget = (target: HTMLElement, headersElement: HTMLEleme
};
};
-const updateDocumentTabMovePreview = (target: IDocumentTabMoveTarget, clientX: number) => {
- if (!target.element.classList.contains("item--document-drop")) {
+const updateDocumentTabMovePreview = (target: IDocumentTabMoveTarget, tabHeadersElement: HTMLElement, clientX: number) => {
+ let dropElement = tabHeadersElement.querySelector<HTMLElement>(":scope > .item__document-drop");
+ if (!target.element.classList.contains("item--document-drop") || !dropElement) {
clearDocumentTabMovePreview();
target.element.classList.add("item--document-drop");
- target.element.insertAdjacentHTML("beforeend", `<span class="item__document-drop">
+ tabHeadersElement.classList.add("layout-tab-bars--document-drop");
+ tabHeadersElement.insertAdjacentHTML("beforeend", `<span class="item__document-drop" data-target-tab-id="${escapeHtml(target.element.dataset.id)}">
<span class="item__document-drop-option" data-position="sibling">${escapeHtml(window.siyuan.languages.moveDocToSameLevel)}</span>
<span class="item__document-drop-option" data-position="child">${escapeHtml(window.siyuan.languages.moveDocAsChild)}</span>
</span>`);
+ dropElement = tabHeadersElement.querySelector<HTMLElement>(":scope > .item__document-drop");
}
- const dropElement = target.element.querySelector<HTMLElement>(":scope > .item__document-drop");
+ const headersRect = tabHeadersElement.getBoundingClientRect();
+ const targetRect = target.element.getBoundingClientRect();
const rect = dropElement.getBoundingClientRect();
- const position = getDocumentTabMovePosition(clientX, rect.left, rect.width);
+ dropElement.style.left = `${getDocumentTabMovePreviewLeft(targetRect.left, targetRect.width, headersRect.left,
+ headersRect.width, rect.width)}px`;
+ dropElement.style.top = `${targetRect.top - headersRect.top}px`;
+ dropElement.style.height = `${targetRect.height}px`;
+ const positionedRect = dropElement.getBoundingClientRect();
+ const position = getDocumentTabMovePosition(clientX, positionedRect.left, positionedRect.width);
target.element.dataset.documentDropPosition = position;
dropElement.querySelectorAll<HTMLElement>(".item__document-drop-option").forEach((item) => {
item.classList.toggle("item__document-drop-option--active", item.dataset.position === position);
@@ -334,7 +347,7 @@ export class Wnd {
const documentTabTarget = getDocumentTabMoveTarget(event.target, this.headersElement);
it.classList.remove("layout-tab-bars--drag");
if (documentTabTarget) {
- updateDocumentTabMovePreview(documentTabTarget, event.clientX);
+ updateDocumentTabMovePreview(documentTabTarget, it, event.clientX);
event.dataTransfer.dropEffect = "move";
} else {
clearDocumentTabMovePreview(tabHeadersElement);
diff --git a/app/src/layout/tabDrag.test.ts b/app/src/layout/tabDrag.test.ts
index 604b93e9cc..5910867e83 100644
--- a/app/src/layout/tabDrag.test.ts
+++ b/app/src/layout/tabDrag.test.ts
@@ -1,6 +1,7 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
import {
+ getDocumentTabMovePreviewLeft,
getDocumentTabMovePosition,
clearTabHoverSwitch,
findDefaultTabNextId,
@@ -15,6 +16,11 @@ describe("document tab move target", () => {
assert.equal(getDocumentTabMovePosition(120, 100, 40), "child");
assert.equal(getDocumentTabMovePosition(139, 100, 40), "child");
});
+
+ it("keeps the preview inside the tab bar for a single narrow tab", () => {
+ assert.equal(getDocumentTabMovePreviewLeft(435, 68, 422, 538, 200), 104);
+ assert.equal(getDocumentTabMovePreviewLeft(700, 68, 422, 538, 200), 312);
+ });
});
const createItems = () => [
diff --git a/app/src/layout/tabDrag.ts b/app/src/layout/tabDrag.ts
index a1fad020f3..855efa0bee 100644
--- a/app/src/layout/tabDrag.ts
+++ b/app/src/layout/tabDrag.ts
@@ -3,12 +3,26 @@ export type TDocumentTabMovePosition = "sibling" | "child";
export const getDocumentTabMovePosition = (clientX: number, left: number, width: number): TDocumentTabMovePosition =>
clientX < left + width / 2 ? "sibling" : "child";
+export const getDocumentTabMovePreviewLeft = (targetLeft: number, targetWidth: number, containerLeft: number,
+ containerWidth: number, previewWidth: number, padding = 4) => {
+ const minLeft = previewWidth / 2 + padding;
+ const maxLeft = containerWidth - previewWidth / 2 - padding;
+ const preferredLeft = targetLeft - containerLeft + targetWidth / 2;
+ return Math.min(Math.max(preferredLeft, minLeft), Math.max(minLeft, maxLeft));
+};
+
export const clearDocumentTabMovePreview = (tabHeadersElement?: ParentNode) => {
const root = tabHeadersElement || document;
+ root.querySelectorAll(".item__document-drop").forEach((item) => item.remove());
+ if (root instanceof HTMLElement) {
+ root.classList.remove("layout-tab-bars--document-drop");
+ }
+ root.querySelectorAll<HTMLElement>(".layout-tab-bars--document-drop").forEach((item) => {
+ item.classList.remove("layout-tab-bars--document-drop");
+ });
root.querySelectorAll<HTMLElement>(".item--document-drop").forEach((item) => {
item.classList.remove("item--document-drop");
delete item.dataset.documentDropPosition;
- item.querySelector(":scope > .item__document-drop")?.remove();
});
};