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/18424

Vanessab47eb75e9b48
diff --git a/app/src/boot/globalEvent/event.ts b/app/src/boot/globalEvent/event.ts
index 84c4a8b7b5..507c9d65b8 100644
--- a/app/src/boot/globalEvent/event.ts
+++ b/app/src/boot/globalEvent/event.ts
@@ -15,6 +15,7 @@ import {isWindow} from "../../util/functions";
import {getDockByType} from "../../layout/tabUtil";
import {fetchPost} from "../../util/fetch";
import {initHarmonyTextSelectionMenu} from "../../util/harmonyTextSelectionMenu";
+import {clearDragTipGhost, hideDragTip} from "../../protyle/util/dragTip";
const KANBAN_GROUP_DRAG_TYPE = `${Constants.SIYUAN_DROP_GUTTER}NodeAttributeView${Constants.ZWSP}Group${Constants.ZWSP}`.toLowerCase();
@@ -81,6 +82,7 @@ export const initWindowEvent = (app: App) => {
}, true);
let scrollTarget: HTMLElement | false;
+ window.addEventListener("dragstart", clearDragTipGhost, true);
window.addEventListener("dragover", (event: DragEvent & { target: HTMLElement }) => {
if (event.dataTransfer.types.includes(Constants.SIYUAN_DROP_TAB)) {
if (!hasClosestByClassName(event.target, "layout-tab-bar")) {
@@ -183,7 +185,8 @@ export const initWindowEvent = (app: App) => {
});
window.addEventListener("dragend", () => {
stopScrollAnimation();
- document.querySelector(".drag-tip")?.remove();
+ hideDragTip();
+ clearDragTipGhost();
window.siyuan.dragTitle = "";
});
window.addEventListener("dragleave", () => {
diff --git a/app/src/layout/dock/Bookmark.ts b/app/src/layout/dock/Bookmark.ts
index 8673c84b3c..8ef13dcc6a 100644
--- a/app/src/layout/dock/Bookmark.ts
+++ b/app/src/layout/dock/Bookmark.ts
@@ -11,6 +11,7 @@ import {openBookmarkMenu} from "../../menus/bookmark";
import type {App} from "../../index";
import {Constants} from "../../constants";
import {checkFold} from "../../util/noRelyPCFunction";
+import {setDragTipGhost} from "../../protyle/util/dragTip";
export class Bookmark extends Model {
private openNodes: string[];
@@ -145,6 +146,8 @@ export class Bookmark extends Model {
element.style.opacity = "0.38";
window.siyuan.dragElement = undefined;
window.siyuan.dragTitle = element.querySelector(".b3-list-item__text")?.textContent?.trim() || "";
+ const rect = element.getBoundingClientRect();
+ setDragTipGhost(element, event.clientX - rect.left, event.clientY - rect.top);
return true;
},
dragEnd: (element) => {
diff --git a/app/src/layout/dock/Files.ts b/app/src/layout/dock/Files.ts
index 1251acb757..bc6dfe8691 100644
--- a/app/src/layout/dock/Files.ts
+++ b/app/src/layout/dock/Files.ts
@@ -32,7 +32,7 @@ import {ipcRenderer} from "electron";
/// #endif
import {hideTooltip, showTooltip} from "../../dialog/tooltip";
import {selectOpenTab} from "./util";
-import {hideDragTip, showDragTip, transparentImgSrc} from "../../protyle/util/dragTip";
+import {hideDragTip, setDragTipGhost, showDragTip} from "../../protyle/util/dragTip";
import {
cancelFileTreeCollapse,
collapseFileTree,
@@ -463,15 +463,12 @@ export class Files extends Model {
ghostElement.setAttribute("style", `width: 219px;position: fixed;top:-${selectElements.length * 30}px`);
ghostElement.setAttribute("class", "b3-list b3-list--background");
document.body.append(ghostElement);
+ setDragTipGhost(ghostElement, 16, 16);
+ event.dataTransfer.setDragImage(ghostElement, 16, 16);
if (window.siyuan.touchDragActive) {
// 触屏保留 DOM ghost 供 touchDragBridge 跟随手指
- event.dataTransfer.setDragImage(ghostElement, 16, 16);
window.siyuan.touchDragGhost = ghostElement;
} else {
- // 桌面端隐藏原生 ghost,改用自定义双区跟随框
- const transparentImg = new Image();
- transparentImg.src = transparentImgSrc;
- event.dataTransfer.setDragImage(transparentImg, 0, 0);
setTimeout(() => {
ghostElement.remove();
});
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts
index 3c410fe87b..c68bdec5b8 100644
--- a/app/src/protyle/gutter/index.ts
+++ b/app/src/protyle/gutter/index.ts
@@ -49,7 +49,7 @@ import {
import * as dayjs from "dayjs";
import {fetchPost, fetchSyncPost} from "../../util/fetch";
import {cancelSB, genEmptyElement, getLangByType, insertEmptyBlock, jumpToParent,} from "../../block/util";
-import {transparentImgSrc} from "../util/dragTip";
+import {setDragTipGhost} from "../util/dragTip";
import {countBlockWord} from "../../layout/status";
import {Constants} from "../../constants";
import {mathRender} from "../render/mathRender";
@@ -217,24 +217,15 @@ export class Gutter {
});
ghostElement.setAttribute("style", `position:fixed;opacity:.1;width:${selectElements[0].clientWidth}px;padding:0;`);
document.body.append(ghostElement);
- // 普通块(段落/标题/列表块/引用块等)拖拽时隐藏原生 ghost 并改用自定义双区跟随框;AV 行保留原生 ghost
const isBlockDrag = !buttonElement.dataset.rowId;
- if (isBlockDrag && !window.siyuan.touchDragActive) {
- const transparentImg = new Image();
- transparentImg.src = transparentImgSrc;
- event.dataTransfer.setDragImage(transparentImg, 0, 0);
+ setDragTipGhost(ghostElement, 0, 0);
+ event.dataTransfer.setDragImage(ghostElement, 0, 0);
+ if (window.siyuan.touchDragActive) {
+ window.siyuan.touchDragGhost = ghostElement;
+ } else {
setTimeout(() => {
ghostElement.remove();
});
- } else {
- event.dataTransfer.setDragImage(ghostElement, 0, 0);
- if (window.siyuan.touchDragActive) {
- window.siyuan.touchDragGhost = ghostElement;
- } else {
- setTimeout(() => {
- ghostElement.remove();
- });
- }
}
if (isBlockDrag) {
const text = getContenteditableElement(selectElements[0] as HTMLElement)?.textContent?.trim() || "";
diff --git a/app/src/protyle/util/dragTip.ts b/app/src/protyle/util/dragTip.ts
index dca8c66196..9a5dc384b3 100644
--- a/app/src/protyle/util/dragTip.ts
+++ b/app/src/protyle/util/dragTip.ts
@@ -1,16 +1,34 @@
// 拖拽时跟随鼠标的自定义双区提示框:上半=操作对象名称,下半=操作文案
// 通过 .drag-tip 类做全局单例,在编辑器和文档树两处 dragover 共用
-export const transparentImgSrc = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=";
-
const dragTipState = {
rafId: 0, title: "", action: "", x: 0, y: 0,
element: null as HTMLElement, titleElement: null as HTMLElement, actionElement: null as HTMLElement,
- lastTitle: "", lastAction: ""
+ lastTitle: "", lastAction: "", width: 0, height: 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 ghostLeft = dragTipState.x - dragTipState.ghost.offsetX;
+ const ghostTop = dragTipState.y - dragTipState.ghost.offsetY;
+ return {
+ left: ghostLeft,
+ top: ghostTop - dragTipState.height - gap
+ };
};
const renderDragTip = () => {
dragTipState.rafId = 0;
+ let updateSize = false;
if (!dragTipState.element || !dragTipState.element.isConnected) {
// 优先复用已有的 .drag-tip(跨编辑器/文档树区域时避免重复创建)
dragTipState.element = (document.querySelector(".drag-tip") as HTMLElement) || null;
@@ -40,6 +58,7 @@ const renderDragTip = () => {
}
dragTipState.lastTitle = "";
dragTipState.lastAction = "";
+ updateSize = true;
}
// 名称/文案变化才写 textContent,减少 DOM 写入
if (dragTipState.lastTitle !== dragTipState.title) {
@@ -47,13 +66,34 @@ const renderDragTip = () => {
dragTipState.lastTitle = dragTipState.title;
// 名称为空时隐藏上半行
dragTipState.titleElement.style.display = dragTipState.title ? "" : "none";
+ updateSize = true;
}
if (dragTipState.lastAction !== dragTipState.action) {
dragTipState.actionElement.textContent = dragTipState.action;
dragTipState.lastAction = dragTipState.action;
+ updateSize = true;
}
- // 固定偏移到光标右下方,不读取 offsetHeight 以免触发同步布局造成卡顿
- dragTipState.element.style.transform = `translate(${dragTipState.x + 16}px, ${dragTipState.y + 16}px)`;
+ if (updateSize) {
+ const rect = dragTipState.element.getBoundingClientRect();
+ dragTipState.width = rect.width;
+ dragTipState.height = rect.height;
+ }
+ const position = getDragTipPosition();
+ dragTipState.element.style.transform = `translate(${position.left}px, ${position.top}px)`;
+};
+
+export const setDragTipGhost = (element: HTMLElement, offsetX: number, offsetY: number) => {
+ const rect = element.getBoundingClientRect();
+ dragTipState.ghost = {
+ width: rect.width,
+ height: rect.height,
+ offsetX,
+ offsetY
+ };
+};
+
+export const clearDragTipGhost = () => {
+ dragTipState.ghost = null;
};
export const showDragTip = (title: string, action: string, x: number, y: number) => {
@@ -102,5 +142,7 @@ export const hideDragTip = () => {
dragTipState.actionElement = null;
dragTipState.lastTitle = "";
dragTipState.lastAction = "";
+ dragTipState.width = 0;
+ dragTipState.height = 0;
hideCaretLine();
};
diff --git a/app/src/protyle/util/editorCommonEvent.ts b/app/src/protyle/util/editorCommonEvent.ts
index e577b2dcbc..67b3eaba68 100644
--- a/app/src/protyle/util/editorCommonEvent.ts
+++ b/app/src/protyle/util/editorCommonEvent.ts
@@ -7,7 +7,7 @@ import {
getSbChildBlockCount,
getTopAloneElement
} from "../wysiwyg/getBlock";
-import {hideCaretLine, hideDragTip, showCaretLine, showDragTip, transparentImgSrc} from "./dragTip";
+import {hideCaretLine, hideDragTip, setDragTipGhost, showCaretLine, showDragTip} from "./dragTip";
import {
hasClosestBlock,
hasClosestByAttribute,
@@ -891,15 +891,12 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
ghostElement.append(cloneElement);
ghostElement.setAttribute("style", `position:fixed;opacity:.1;width:${target.parentElement.clientWidth}px;padding:0;`);
document.body.append(ghostElement);
+ setDragTipGhost(ghostElement, 0, 0);
+ event.dataTransfer.setDragImage(ghostElement, 0, 0);
if (window.siyuan.touchDragActive) {
// 触屏保留 DOM ghost 供 touchDragBridge 跟随手指
- event.dataTransfer.setDragImage(ghostElement, 0, 0);
window.siyuan.touchDragGhost = ghostElement;
} else {
- // 桌面端隐藏原生 ghost,改用自定义双区跟随框
- const transparentImg = new Image();
- transparentImg.src = transparentImgSrc;
- event.dataTransfer.setDragImage(transparentImg, 0, 0);
setTimeout(() => {
ghostElement.remove();
});