A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. (PERSONAL FORK)
1import {Constants} from "../../constants";
2
3export const cancelDrag = () => {
4 const ghostElement = document.getElementById("dragGhost");
5 if (ghostElement) {
6 if (ghostElement.dataset.ghostType === "dock") {
7 ghostElement.parentElement.querySelectorAll(".dock__item").forEach((item: HTMLElement) => {
8 item.style.opacity = "";
9 });
10 document.querySelector("#dockMoveItem")?.remove();
11 } else {
12 const startElement = ghostElement.parentElement.querySelector(`[data-node-id="${ghostElement.getAttribute("data-node-id")}"]`) as HTMLElement;
13 if (startElement) {
14 startElement.style.opacity = "";
15 }
16 ghostElement.parentElement.querySelectorAll(".dragover__top, .dragover__bottom, .dragover, .dragover__current").forEach((item: HTMLElement) => {
17 item.classList.remove("dragover__top", "dragover__bottom", "dragover", "dragover__current");
18 item.style.opacity = "";
19 });
20 }
21 ghostElement.remove();
22 document.onmousemove = null;
23 stopScrollAnimation();
24 }
25};
26
27const dragoverScroll: {
28 animationId?: number,
29 element?: Element,
30 space?: number // -1 向上;1 向下
31 lastTime?: number
32} = {};
33export const stopScrollAnimation = () => {
34 if (dragoverScroll.animationId) {
35 cancelAnimationFrame(dragoverScroll.animationId);
36 dragoverScroll.animationId = null;
37 dragoverScroll.element = null;
38 dragoverScroll.space = null;
39 dragoverScroll.lastTime = null;
40 }
41};
42const scrollAnimation = (timestamp: number) => {
43 if (!dragoverScroll.lastTime) {
44 dragoverScroll.lastTime = timestamp - 8;
45 }
46 dragoverScroll.element.scroll({
47 top: dragoverScroll.element.scrollTop + (timestamp - dragoverScroll.lastTime) * dragoverScroll.space / 64
48 });
49 // 使用 requestAnimationFrame 继续动画
50 dragoverScroll.animationId = requestAnimationFrame(scrollAnimation);
51 dragoverScroll.lastTime = timestamp;
52};
53
54export const dragOverScroll = (moveEvent: MouseEvent, contentRect: DOMRect, element: Element) => {
55 const dragToUp = moveEvent.clientY < contentRect.top + Constants.SIZE_SCROLL_TB;
56 if (dragToUp ||
57 moveEvent.clientY > contentRect.bottom - Constants.SIZE_SCROLL_TB) {
58 dragoverScroll.space = dragToUp ? moveEvent.clientY - contentRect.top - Constants.SIZE_SCROLL_TB :
59 moveEvent.clientY - contentRect.bottom + Constants.SIZE_SCROLL_TB;
60 if (!dragoverScroll.animationId) {
61 dragoverScroll.element = element;
62 dragoverScroll.animationId = requestAnimationFrame(scrollAnimation);
63 }
64 } else {
65 // 离开滚动区域时停止滚动
66 stopScrollAnimation();
67 }
68};