Skip to content

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

:bug: Preserve block drag directions and keep tooltips in view https://github.com/siyuan-note/siyuan/issues/20321

Danielb85cf85e276d
diff --git a/app/src/assets/scss/component/_tooltips.scss b/app/src/assets/scss/component/_tooltips.scss
index d47d71b551..768b5d4c50 100644
--- a/app/src/assets/scss/component/_tooltips.scss
+++ b/app/src/assets/scss/component/_tooltips.scss
@@ -38,6 +38,34 @@
}
}
+.drag-tip {
+ max-width: min(216px, calc(100vw - 16px));
+ max-height: calc(100vh - 16px);
+
+ &__title,
+ &__target {
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+ }
+
+ &__action {
+ color: var(--b3-tooltips-second-color);
+ font-size: 12px;
+
+ &--target {
+ display: grid;
+ grid-template-columns: minmax(0, max-content) minmax(0, 1fr) minmax(0, max-content);
+ }
+ }
+
+ &__label {
+ color: var(--b3-tooltips-color);
+ overflow-wrap: anywhere;
+ white-space: pre-wrap;
+ }
+}
+
.b3-tooltips {
position: relative;
cursor: pointer;
diff --git a/app/src/layout/dock/Files.ts b/app/src/layout/dock/Files.ts
index 7192641c48..0ce29f1417 100644
--- a/app/src/layout/dock/Files.ts
+++ b/app/src/layout/dock/Files.ts
@@ -738,11 +738,11 @@ export class Files extends Model {
const name = liElement.querySelector(".b3-list-item__text")?.textContent || "";
const title = window.siyuan.dragTitle || "";
if (liElement.classList.contains("dragover__top")) {
- showDragTip(title, window.siyuan.languages.dragTipMoveBefore.replace("${x}", name), event.clientX, event.clientY);
+ showDragTip(title, window.siyuan.languages.dragTipMoveBefore, event.clientX, event.clientY, name);
} else if (liElement.classList.contains("dragover__bottom")) {
- showDragTip(title, window.siyuan.languages.dragTipMoveAfter.replace("${x}", name), event.clientX, event.clientY);
+ showDragTip(title, window.siyuan.languages.dragTipMoveAfter, event.clientX, event.clientY, name);
} else if (liElement.classList.contains("dragover")) {
- showDragTip(title, window.siyuan.languages.dragTipMoveChild.replace("${x}", name), event.clientX, event.clientY);
+ showDragTip(title, window.siyuan.languages.dragTipMoveChild, event.clientX, event.clientY, name);
} else {
hideDragTip();
}
@@ -754,13 +754,13 @@ export class Files extends Model {
const title = window.siyuan.dragTitle || "";
let action: string;
if (liElement.classList.contains("dragover__top")) {
- action = window.siyuan.languages.dragTip2DocBefore.replace("${x}", name);
+ action = window.siyuan.languages.dragTip2DocBefore;
} else if (liElement.classList.contains("dragover__bottom")) {
- action = window.siyuan.languages.dragTip2DocAfter.replace("${x}", name);
+ action = window.siyuan.languages.dragTip2DocAfter;
} else {
- action = window.siyuan.languages.dragTip2DocChild.replace("${x}", name);
+ action = window.siyuan.languages.dragTip2DocChild;
}
- showDragTip(title, action, event.clientX, event.clientY);
+ showDragTip(title, action, event.clientX, event.clientY, name);
}
}
event.preventDefault();
diff --git a/app/src/layout/dock/PinnedDocs.test.ts b/app/src/layout/dock/PinnedDocs.test.ts
index 5498030486..d2c8605075 100644
--- a/app/src/layout/dock/PinnedDocs.test.ts
+++ b/app/src/layout/dock/PinnedDocs.test.ts
@@ -381,15 +381,15 @@ test("pin drop tips describe pinning and source moves, and self targets clear fe
panel.list = {getBoundingClientRect: () => ({})};
runtime.dragElement = {innerText: "source"};
panel.previewDrop(10, 1);
- assert.deepEqual(tips.at(-1), ["Document", "Pin", 10, 1]);
+ assert.deepEqual(tips.at(-1), ["Document", "Pin", 10, 1, "Target"]);
source = true;
row.dataset.pinRoot = "false";
panel.previewDrop(10, 15, true);
- assert.deepEqual(tips.at(-1), ["Document", "Into Target", 10, 15]);
+ assert.deepEqual(tips.at(-1), ["Document", "Into ${x}", 10, 15, "Target"]);
panel.previewDrop(10, 1, true);
- assert.deepEqual(tips.at(-1), ["Document", "Before Target", 10, 1]);
+ assert.deepEqual(tips.at(-1), ["Document", "Before ${x}", 10, 1, "Target"]);
panel.previewDrop(10, 29, true);
- assert.deepEqual(tips.at(-1), ["Document", "After Target", 10, 29]);
+ assert.deepEqual(tips.at(-1), ["Document", "After ${x}", 10, 29, "Target"]);
for (source of [true, false]) {
runtime.dragElement.innerText = "target";
const count = highlights.length;
diff --git a/app/src/layout/dock/PinnedDocs.ts b/app/src/layout/dock/PinnedDocs.ts
index f53b14180f..1f067c0567 100644
--- a/app/src/layout/dock/PinnedDocs.ts
+++ b/app/src/layout/dock/PinnedDocs.ts
@@ -572,9 +572,8 @@ export class PinnedDocs {
const position = this.dropTarget.position;
const action = position.startsWith("pin") ? window.siyuan.languages.pinDoc :
(position === "inside" ? window.siyuan.languages.dragTipMoveChild :
- position === "before" ? window.siyuan.languages.dragTipMoveBefore : window.siyuan.languages.dragTipMoveAfter)
- .replace("${x}", name);
- showDragTip(window.siyuan.dragTitle || "", action, x, y);
+ position === "before" ? window.siyuan.languages.dragTipMoveBefore : window.siyuan.languages.dragTipMoveAfter);
+ showDragTip(window.siyuan.dragTitle || "", action, x, y, name);
const scrollElement = source ? this.sourceTree : this.list;
dragOverScroll({clientY: y} as MouseEvent, scrollElement.getBoundingClientRect(), scrollElement);
return true;
diff --git a/app/src/protyle/util/dragTip.ts b/app/src/protyle/util/dragTip.ts
index e41d58bfc7..9362ef56c0 100644
--- a/app/src/protyle/util/dragTip.ts
+++ b/app/src/protyle/util/dragTip.ts
@@ -2,27 +2,27 @@
// 通过 .drag-tip 类做全局单例,在编辑器和文档树两处 dragover 共用
const dragTipState = {
- rafId: 0, title: "", action: "", x: 0, y: 0,
+ rafId: 0, title: "", action: "", target: null as string | null, x: 0, y: 0,
element: null as HTMLElement, titleElement: null as HTMLElement, actionElement: null as HTMLElement,
- lastTitle: "", lastAction: "", width: 0, height: 0,
+ lastTitle: "", lastAction: "", lastTarget: null as string | null, width: 0, height: 0,
+ viewportWidth: 0, viewportHeight: 0,
ghost: null as {width: number, height: number, offsetX: number, offsetY: number}
};
const getDragTipPosition = () => {
const gap = 8;
const pointerOffset = 16;
- if (!dragTipState.ghost) {
- return {
- left: dragTipState.x,
- top: dragTipState.y - dragTipState.height - pointerOffset
- };
+ const ghost = dragTipState.ghost;
+ const anchorLeft = dragTipState.x - (ghost?.offsetX || 0);
+ const anchorTop = dragTipState.y - (ghost?.offsetY || 0);
+ let top = anchorTop - dragTipState.height - (ghost ? gap : pointerOffset);
+ // 上方空间不足时放到拖拽预览下方,并将提示框限制在视口内。
+ if (top < gap) {
+ top = anchorTop + (ghost?.height || 0) + (ghost ? gap : pointerOffset);
}
-
- const ghostLeft = dragTipState.x - dragTipState.ghost.offsetX;
- const ghostTop = dragTipState.y - dragTipState.ghost.offsetY;
return {
- left: ghostLeft,
- top: ghostTop - dragTipState.height - gap
+ left: Math.max(gap, Math.min(anchorLeft, window.innerWidth - dragTipState.width - gap)),
+ top: Math.max(gap, Math.min(top, window.innerHeight - dragTipState.height - gap))
};
};
@@ -46,10 +46,8 @@ const renderDragTip = () => {
dragTipState.element.style.left = "0";
dragTipState.titleElement = document.createElement("div");
dragTipState.titleElement.className = "drag-tip__title";
- dragTipState.titleElement.style.cssText = "max-width:200px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;color:var(--b3-tooltips-color);";
dragTipState.actionElement = document.createElement("div");
dragTipState.actionElement.className = "drag-tip__action";
- dragTipState.actionElement.style.cssText = "max-width:200px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;color:var(--b3-tooltips-second-color);font-size:12px;";
dragTipState.element.append(dragTipState.titleElement, dragTipState.actionElement);
document.body.append(dragTipState.element);
} else {
@@ -58,6 +56,7 @@ const renderDragTip = () => {
}
dragTipState.lastTitle = "";
dragTipState.lastAction = "";
+ dragTipState.lastTarget = null;
updateSize = true;
}
// 名称/文案变化才写 textContent,减少 DOM 写入
@@ -68,9 +67,32 @@ const renderDragTip = () => {
dragTipState.titleElement.style.display = dragTipState.title ? "" : "none";
updateSize = true;
}
- if (dragTipState.lastAction !== dragTipState.action) {
- dragTipState.actionElement.textContent = dragTipState.action;
+ if (dragTipState.lastAction !== dragTipState.action || dragTipState.lastTarget !== dragTipState.target) {
+ const placeholder = dragTipState.action.indexOf("${x}");
+ const hasTarget = dragTipState.target !== null && placeholder !== -1;
+ dragTipState.actionElement.classList.toggle("drag-tip__action--target", hasTarget);
+ if (hasTarget) {
+ // 保留本地化模板的语序,仅缩略目标文字,完整显示操作和落点方向。
+ const prefix = document.createElement("span");
+ prefix.className = "drag-tip__label";
+ prefix.textContent = dragTipState.action.slice(0, placeholder);
+ const target = document.createElement("span");
+ target.className = "drag-tip__target";
+ target.textContent = dragTipState.target;
+ const suffix = document.createElement("span");
+ suffix.className = "drag-tip__label";
+ suffix.textContent = dragTipState.action.slice(placeholder + 4);
+ dragTipState.actionElement.replaceChildren(prefix, target, suffix);
+ } else {
+ dragTipState.actionElement.textContent = dragTipState.action;
+ }
dragTipState.lastAction = dragTipState.action;
+ dragTipState.lastTarget = dragTipState.target;
+ updateSize = true;
+ }
+ if (dragTipState.viewportWidth !== window.innerWidth || dragTipState.viewportHeight !== window.innerHeight) {
+ dragTipState.viewportWidth = window.innerWidth;
+ dragTipState.viewportHeight = window.innerHeight;
updateSize = true;
}
if (updateSize) {
@@ -96,13 +118,14 @@ export const clearDragTipGhost = () => {
dragTipState.ghost = null;
};
-export const showDragTip = (title: string, action: string, x: number, y: number) => {
+export const showDragTip = (title: string, action: string, x: number, y: number, target?: string) => {
/// #if MOBILE
// 移动端不显示拖拽提示
return;
/// #endif
dragTipState.title = title;
dragTipState.action = action;
+ dragTipState.target = target !== undefined && action.includes("${x}") ? target : null;
dragTipState.x = x;
dragTipState.y = y;
// 合并到下一帧渲染,避免高频 dragover 下逐次写 DOM 造成卡顿
@@ -142,7 +165,10 @@ export const hideDragTip = () => {
dragTipState.actionElement = null;
dragTipState.lastTitle = "";
dragTipState.lastAction = "";
+ dragTipState.lastTarget = null;
dragTipState.width = 0;
dragTipState.height = 0;
+ dragTipState.viewportWidth = 0;
+ dragTipState.viewportHeight = 0;
hideCaretLine();
};
diff --git a/app/src/protyle/util/editorCommonEvent.ts b/app/src/protyle/util/editorCommonEvent.ts
index d3c24c96a4..cf5f5114d4 100644
--- a/app/src/protyle/util/editorCommonEvent.ts
+++ b/app/src/protyle/util/editorCommonEvent.ts
@@ -2357,12 +2357,12 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
} else if (shouldCopyBlockDrag(event)) {
action = window.siyuan.languages.duplicateCopy;
} else if (isChild) {
- action = window.siyuan.languages.dragTipListItemChild.replace("${x}", targetText);
+ action = window.siyuan.languages.dragTipListItemChild;
} else {
const key = position === "bottom" ? "dragTipListItemAfter" : "dragTipListItemBefore";
- action = window.siyuan.languages[key].replace("${x}", targetText);
+ action = window.siyuan.languages[key];
}
- showDragTip(window.siyuan.dragTitle || "", action, event.clientX, event.clientY);
+ showDragTip(window.siyuan.dragTitle || "", action, event.clientX, event.clientY, targetText);
};
// 缓存当前目标的文本和列布局判断,避免优化路径每次 dragover 重复计算
let cachedTargetText = "";
@@ -2951,8 +2951,8 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
const key = (isHorizontal || cachedIsCol)
? (isFront ? window.siyuan.languages.dragTipMoveTargetFront : window.siyuan.languages.dragTipMoveTargetBack)
: (isFront ? window.siyuan.languages.dragTipMoveTargetAbove : window.siyuan.languages.dragTipMoveTargetBelow);
- showDragTip(window.siyuan.dragTitle || "", key.replace("${x}", displayText),
- event.clientX, event.clientY);
+ showDragTip(window.siyuan.dragTitle || "", key,
+ event.clientX, event.clientY, displayText);
}
}
}
@@ -3017,8 +3017,8 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
const key = isSbLeftEdge
? window.siyuan.languages.dragTipMoveTargetFront
: window.siyuan.languages.dragTipMoveTargetBack;
- showDragTip(window.siyuan.dragTitle || "", key.replace("${x}", sbText),
- event.clientX, event.clientY);
+ showDragTip(window.siyuan.dragTitle || "", key,
+ event.clientX, event.clientY, sbText);
}
return;
}
@@ -3034,8 +3034,8 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
// 默认移动时,更新下半为带目标名的位置文案(超级块本身跳过)
if (!event.altKey && !event.shiftKey && !event.ctrlKey && gutterType && !isAvSubType && !isAvTarget && !targetElement.classList.contains("sb") && cachedTargetText) {
showDragTip(window.siyuan.dragTitle || "",
- window.siyuan.languages.dragTipMoveTargetFront.replace("${x}", cachedTargetText),
- event.clientX, event.clientY);
+ window.siyuan.languages.dragTipMoveTargetFront,
+ event.clientX, event.clientY, cachedTargetText);
}
} else if (event.clientX > nodeRect.right - 32 && event.clientX < nodeRect.right &&
!targetElement.classList.contains("av__row")) {
@@ -3044,8 +3044,8 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
// 默认移动时,更新下半为带目标名的位置文案(超级块本身跳过)
if (!event.altKey && !event.shiftKey && !event.ctrlKey && gutterType && !isAvSubType && !isAvTarget && !targetElement.classList.contains("sb") && cachedTargetText) {
showDragTip(window.siyuan.dragTitle || "",
- window.siyuan.languages.dragTipMoveTargetBack.replace("${x}", cachedTargetText),
- event.clientX, event.clientY);
+ window.siyuan.languages.dragTipMoveTargetBack,
+ event.clientX, event.clientY, cachedTargetText);
}
} else if (targetElement.classList.contains("av__row--header")) {
targetElement.classList.add("dragover__bottom");
@@ -3056,8 +3056,8 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
// 默认移动时,更新下半为带目标名的位置文案(超级块本身跳过)
if (!event.altKey && !event.shiftKey && !event.ctrlKey && gutterType && !isAvSubType && !isAvTarget && !targetElement.classList.contains("sb") && cachedTargetText) {
showDragTip(window.siyuan.dragTitle || "",
- (cachedIsCol ? window.siyuan.languages.dragTipMoveTargetBack : window.siyuan.languages.dragTipMoveTargetBelow).replace("${x}", cachedTargetText),
- event.clientX, event.clientY);
+ cachedIsCol ? window.siyuan.languages.dragTipMoveTargetBack : window.siyuan.languages.dragTipMoveTargetBelow,
+ event.clientX, event.clientY, cachedTargetText);
}
} else if (disabledPosition !== "top") {
targetElement.classList.add("dragover__top");
@@ -3065,8 +3065,8 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
// 默认移动时,更新下半为带目标名的位置文案(超级块本身跳过)
if (!event.altKey && !event.shiftKey && !event.ctrlKey && gutterType && !isAvSubType && !isAvTarget && !targetElement.classList.contains("sb") && cachedTargetText) {
showDragTip(window.siyuan.dragTitle || "",
- (cachedIsCol ? window.siyuan.languages.dragTipMoveTargetFront : window.siyuan.languages.dragTipMoveTargetAbove).replace("${x}", cachedTargetText),
- event.clientX, event.clientY);
+ cachedIsCol ? window.siyuan.languages.dragTipMoveTargetFront : window.siyuan.languages.dragTipMoveTargetAbove,
+ event.clientX, event.clientY, cachedTargetText);
}
}
}
@@ -3167,8 +3167,8 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
const key = isCol
? (isFront ? window.siyuan.languages.dragTipMoveTargetFront : window.siyuan.languages.dragTipMoveTargetBack)
: (isFront ? window.siyuan.languages.dragTipMoveTargetAbove : window.siyuan.languages.dragTipMoveTargetBelow);
- showDragTip(window.siyuan.dragTitle || "", key.replace("${x}", targetText),
- event.clientX, event.clientY);
+ showDragTip(window.siyuan.dragTitle || "", key,
+ event.clientX, event.clientY, targetText);
}
}
});
diff --git a/app/src/protyle/util/tableCellRichDrag.ts b/app/src/protyle/util/tableCellRichDrag.ts
index 76aa1a209d..4ffb48e150 100644
--- a/app/src/protyle/util/tableCellRichDrag.ts
+++ b/app/src/protyle/util/tableCellRichDrag.ts
@@ -83,7 +83,7 @@ export const bindTableCellRichDrag = (owner: IProtyle, cell: HTMLTableCellElemen
const key = drop.listTarget.isChild ? "dragTipListItemChild" :
drop.before ? "dragTipListItemBefore" : "dragTipListItemAfter";
showDragTip(window.siyuan.dragTitle || "", event.ctrlKey ? window.siyuan.languages.duplicateCopy :
- window.siyuan.languages[key].replace("${x}", targetText), event.clientX, event.clientY);
+ window.siyuan.languages[key], event.clientX, event.clientY, targetText);
}
}
}, {capture: true, signal});