mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-07-03 14:09:06 +02:00
🎨 Supports drag-and-drop on tablet https://github.com/siyuan-note/siyuan/issues/17612
Signed-off-by: Daniel <845765@qq.com>
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
|
||||
& > .resize__move {
|
||||
height: 42px;
|
||||
touch-action: none;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,6 +90,7 @@
|
||||
height: 6px;
|
||||
width: 100%;
|
||||
position: absolute;
|
||||
touch-action: none;
|
||||
|
||||
&--lr {
|
||||
cursor: col-resize;
|
||||
@@ -116,6 +117,7 @@
|
||||
position: relative;
|
||||
z-index: 4; // 需大于 #sidebarContainer https://github.com/siyuan-note/siyuan/issues/11759
|
||||
flex-shrink: 0;
|
||||
touch-action: none;
|
||||
|
||||
&:hover::after {
|
||||
background-color: var(--b3-scroll-color);
|
||||
@@ -217,6 +219,7 @@
|
||||
line-height: 22px;
|
||||
cursor: pointer;
|
||||
user-select: none;
|
||||
touch-action: none;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
transition: var(--b3-transition);
|
||||
@@ -386,6 +389,7 @@
|
||||
border-radius: var(--b3-border-radius);
|
||||
margin: 7px;
|
||||
cursor: pointer;
|
||||
touch-action: none;
|
||||
|
||||
&:hover:not(.dock__item--activefocus),
|
||||
&--active {
|
||||
@@ -398,3 +402,10 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Touch drag support for dialog and editor resize handles */
|
||||
[class*="resize__"],
|
||||
.av__widthdrag,
|
||||
.av__drag-fill {
|
||||
touch-action: none;
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
width: 6px;
|
||||
margin: 0 -6px 0 0;
|
||||
height: auto;
|
||||
touch-action: none;
|
||||
|
||||
&::after {
|
||||
width: .5px;
|
||||
|
||||
@@ -101,6 +101,7 @@
|
||||
left: 0;
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
touch-action: none;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -456,6 +457,7 @@ table + .protyle-action__table {
|
||||
width: 6px;
|
||||
cursor: col-resize;
|
||||
z-index: 2;
|
||||
touch-action: none;
|
||||
|
||||
&:hover::after {
|
||||
background-color: var(--b3-theme-primary-light);
|
||||
|
||||
@@ -182,6 +182,7 @@
|
||||
word-break: keep-all;
|
||||
height: calc(1.625em + 8px);
|
||||
line-height: calc(1.625em + 8px);
|
||||
touch-action: none;
|
||||
|
||||
& ~ [data-type="NodeHeading"] {
|
||||
&.h1 {
|
||||
@@ -469,6 +470,7 @@
|
||||
border-radius: var(--b3-border-radius);
|
||||
cursor: col-resize;
|
||||
transition: var(--b3-transition);
|
||||
touch-action: none;
|
||||
position: absolute;
|
||||
top: 35%;
|
||||
margin-left: -8px;
|
||||
|
||||
@@ -65,7 +65,7 @@ export const globalClick = (event: MouseEvent & { target: HTMLElement }) => {
|
||||
// Dock item click
|
||||
const dockItemElement = hasClosestByClassName(event.target as HTMLElement, "dock__item");
|
||||
if (dockItemElement) {
|
||||
const type = dockItemElement.getAttribute("data-type") as TDock
|
||||
const type = dockItemElement.getAttribute("data-type") as TDock;
|
||||
if (type) {
|
||||
getDockByType(type).toggleModel(type, false, true);
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ const scrollAnimation = (timestamp: number) => {
|
||||
dragoverScroll.lastTime = timestamp;
|
||||
};
|
||||
|
||||
export const dragOverScroll = (moveEvent: MouseEvent, contentRect: DOMRect, element: Element) => {
|
||||
export const dragOverScroll = (moveEvent: MouseEvent | { clientY: number }, contentRect: DOMRect, element: Element) => {
|
||||
const dragToUp = moveEvent.clientY < contentRect.top + Constants.SIZE_SCROLL_TB;
|
||||
if (dragToUp ||
|
||||
moveEvent.clientY > contentRect.bottom - Constants.SIZE_SCROLL_TB) {
|
||||
|
||||
@@ -8,11 +8,7 @@ import {Constants} from "../../constants";
|
||||
import {isIPad} from "../../protyle/util/compatibility";
|
||||
import {globalTouchEnd, globalTouchStart} from "./touch";
|
||||
import {initDockMenu} from "../../menus/dock";
|
||||
import {
|
||||
hasClosestByAttribute,
|
||||
hasClosestByClassName,
|
||||
isInEmbedBlock
|
||||
} from "../../protyle/util/hasClosest";
|
||||
import {hasClosestByAttribute, hasClosestByClassName, isInEmbedBlock} from "../../protyle/util/hasClosest";
|
||||
import {initTabMenu} from "../../menus/tab";
|
||||
import {getInstanceById} from "../../layout/util";
|
||||
import {Tab} from "../../layout/Tab";
|
||||
@@ -22,6 +18,7 @@ import {checkFold} from "../../util/noRelyPCFunction";
|
||||
import {hideAllElements} from "../../protyle/ui/hideElements";
|
||||
import {dragOverScroll, stopScrollAnimation} from "./dragover";
|
||||
import {setWebViewFocusable} from "../../mobile/util/mobileAppUtil";
|
||||
import {initTouchDragBridge} from "../../protyle/util/touchDrag";
|
||||
|
||||
export const initWindowEvent = (app: App) => {
|
||||
document.body.addEventListener("mouseleave", () => {
|
||||
@@ -161,6 +158,9 @@ export const initWindowEvent = (app: App) => {
|
||||
event.preventDefault();
|
||||
return;
|
||||
}
|
||||
if (window.siyuan.touchDragActive) {
|
||||
return;
|
||||
}
|
||||
if (new Date().getTime() - time <= 900) {
|
||||
return;
|
||||
}
|
||||
@@ -209,4 +209,5 @@ export const initWindowEvent = (app: App) => {
|
||||
}
|
||||
}
|
||||
}, false);
|
||||
initTouchDragBridge();
|
||||
};
|
||||
|
||||
@@ -4,11 +4,10 @@ import {Model} from "./Model";
|
||||
import {Editor} from "../editor";
|
||||
import {hasClosestByTag} from "../protyle/util/hasClosest";
|
||||
import {Constants} from "../constants";
|
||||
import {escapeLessThans, escapeHtml} from "../util/escape";
|
||||
import {escapeHtml, escapeLessThans} from "../util/escape";
|
||||
import {unicode2Emoji} from "../emoji";
|
||||
import {fetchPost} from "../util/fetch";
|
||||
import {hideTooltip, showTooltip} from "../dialog/tooltip";
|
||||
import {isTouchDevice} from "../util/functions";
|
||||
/// #if !BROWSER
|
||||
import {openNewWindow} from "../window/openNewWindow";
|
||||
import {ipcRenderer} from "electron";
|
||||
@@ -82,11 +81,6 @@ export class Tab {
|
||||
}
|
||||
});
|
||||
this.headElement.addEventListener("dragstart", (event: DragEvent & { target: HTMLElement }) => {
|
||||
if (isTouchDevice()) {
|
||||
event.stopPropagation();
|
||||
event.preventDefault();
|
||||
return;
|
||||
}
|
||||
window.getSelection().removeAllRanges();
|
||||
hideTooltip();
|
||||
const tabElement = hasClosestByTag(event.target, "LI");
|
||||
@@ -139,7 +133,9 @@ export class Tab {
|
||||
}
|
||||
});
|
||||
}
|
||||
/// #if !BROWSER
|
||||
ipcRenderer.send(Constants.SIYUAN_SEND_WINDOWS, {cmd: "resetTabsStyle", data: "addRegionStyle"});
|
||||
/// #endif
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import {escapeAriaLabel, escapeLessThans, escapeHtml} from "../../util/escape";
|
||||
import {escapeAriaLabel, escapeHtml, escapeLessThans} from "../../util/escape";
|
||||
import {Tab} from "../Tab";
|
||||
import {Model} from "../Model";
|
||||
import {setPanelFocus} from "../util";
|
||||
@@ -25,7 +25,6 @@ import {
|
||||
hasClosestByTag,
|
||||
hasTopClosestByTag
|
||||
} from "../../protyle/util/hasClosest";
|
||||
import {isTouchDevice} from "../../util/functions";
|
||||
import {App} from "../../index";
|
||||
import {refreshFileTree} from "../../dialog/processSystem";
|
||||
/// #if !BROWSER
|
||||
@@ -446,11 +445,6 @@ export class Files extends Model {
|
||||
}
|
||||
});
|
||||
this.element.addEventListener("dragstart", (event: DragEvent & { target: HTMLElement }) => {
|
||||
if (isTouchDevice()) {
|
||||
event.stopPropagation();
|
||||
event.preventDefault();
|
||||
return;
|
||||
}
|
||||
window.getSelection().removeAllRanges();
|
||||
hideTooltip();
|
||||
const liElement = hasClosestByTag(event.target, "LI");
|
||||
@@ -486,9 +480,13 @@ export class Files extends Model {
|
||||
event.dataTransfer.dropEffect = "move";
|
||||
window.siyuan.dragElement = document.createElement("div");
|
||||
window.siyuan.dragElement.innerText = ids;
|
||||
setTimeout(() => {
|
||||
ghostElement.remove();
|
||||
});
|
||||
if (window.siyuan.touchDragActive) {
|
||||
window.siyuan.touchDragGhost = ghostElement;
|
||||
} else {
|
||||
setTimeout(() => {
|
||||
ghostElement.remove();
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
this.element.addEventListener("dragend", (event) => {
|
||||
|
||||
@@ -174,9 +174,13 @@ export class Gutter {
|
||||
ghostElement.setAttribute("style", `position:fixed;opacity:.1;width:${selectElements[0].clientWidth}px;padding:0;`);
|
||||
document.body.append(ghostElement);
|
||||
event.dataTransfer.setDragImage(ghostElement, 0, 0);
|
||||
setTimeout(() => {
|
||||
ghostElement.remove();
|
||||
});
|
||||
if (window.siyuan.touchDragActive) {
|
||||
window.siyuan.touchDragGhost = ghostElement;
|
||||
} else {
|
||||
setTimeout(() => {
|
||||
ghostElement.remove();
|
||||
});
|
||||
}
|
||||
buttonElement.style.opacity = "0.38";
|
||||
window.siyuan.dragElement = avElement as HTMLElement || protyle.wysiwyg.element;
|
||||
event.dataTransfer.setData(`${Constants.SIYUAN_DROP_GUTTER}${buttonElement.getAttribute("data-type")}${Constants.ZWSP}${buttonElement.getAttribute("data-subtype")}${Constants.ZWSP}${selectIds}${Constants.ZWSP}${window.siyuan.config.system.workspaceDir}`,
|
||||
|
||||
@@ -20,7 +20,8 @@ import {getAllEditor} from "../../layout/getAll";
|
||||
import {updatePanelByEditor} from "../../editor/util";
|
||||
/// #endif
|
||||
import {blockRender} from "../render/blockRender";
|
||||
import {uploadLocalFiles} from "../upload";
|
||||
/// #else
|
||||
import {uploadFiles, uploadLocalFiles} from "../upload";
|
||||
import {insertHTML} from "./insertHTML";
|
||||
import {isBrowser} from "../../util/functions";
|
||||
import {hideElements} from "../ui/hideElements";
|
||||
@@ -30,8 +31,6 @@ import {setFold, zoomOut} from "../../menus/protyle";
|
||||
/// #if !BROWSER
|
||||
import {webUtils} from "electron";
|
||||
import {dragUpload} from "../render/av/asset";
|
||||
/// #else
|
||||
import {uploadFiles} from "../upload";
|
||||
/// #endif
|
||||
import {addDragFill, getTypeByCellElement} from "../render/av/cell";
|
||||
import {processClonePHElement} from "../render/util";
|
||||
@@ -581,9 +580,13 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
|
||||
ghostElement.setAttribute("style", `position:fixed;opacity:.1;width:${target.parentElement.clientWidth}px;padding:0;`);
|
||||
document.body.append(ghostElement);
|
||||
event.dataTransfer.setDragImage(ghostElement, 0, 0);
|
||||
setTimeout(() => {
|
||||
ghostElement.remove();
|
||||
});
|
||||
if (window.siyuan.touchDragActive) {
|
||||
window.siyuan.touchDragGhost = ghostElement;
|
||||
} else {
|
||||
setTimeout(() => {
|
||||
ghostElement.remove();
|
||||
});
|
||||
}
|
||||
|
||||
window.siyuan.dragElement = protyle.wysiwyg.element;
|
||||
event.dataTransfer.setData(`${Constants.SIYUAN_DROP_GUTTER}NodeListItem${Constants.ZWSP}${target.parentElement.getAttribute("data-subtype")}${Constants.ZWSP}${[target.parentElement.getAttribute("data-node-id")]}`,
|
||||
@@ -651,9 +654,13 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
|
||||
ghostElement.setAttribute("style", "left: 1px;top:100vh;position:fixed;opacity:.1;padding:0;z-index: 8");
|
||||
document.body.append(ghostElement);
|
||||
event.dataTransfer.setDragImage(ghostElement, -10, -10);
|
||||
setTimeout(() => {
|
||||
ghostElement.remove();
|
||||
});
|
||||
if (window.siyuan.touchDragActive) {
|
||||
window.siyuan.touchDragGhost = ghostElement;
|
||||
} else {
|
||||
setTimeout(() => {
|
||||
ghostElement.remove();
|
||||
});
|
||||
}
|
||||
window.siyuan.dragElement = target;
|
||||
const selectIds: string[] = [];
|
||||
blockElement.querySelectorAll(".av__gallery-item--select").forEach(item => {
|
||||
|
||||
@@ -0,0 +1,372 @@
|
||||
import {stopScrollAnimation} from "../../boot/globalEvent/dragover";
|
||||
|
||||
const DRAG_THRESHOLD = 5;
|
||||
|
||||
interface TouchDragState {
|
||||
dataTransfer: DataTransfer | null;
|
||||
startX: number;
|
||||
startY: number;
|
||||
ghostElement: HTMLElement | null;
|
||||
isDragging: boolean;
|
||||
draggableElement: HTMLElement;
|
||||
editorElement: HTMLElement | null;
|
||||
requireLongPress: boolean;
|
||||
touchStartTime: number;
|
||||
longPressCancelled: boolean;
|
||||
}
|
||||
|
||||
let dragState: TouchDragState | null = null;
|
||||
let lastDragOverElement: Element | null = null;
|
||||
|
||||
// ── Manual mousedown bridge ──────────────────────────────────────────
|
||||
|
||||
let manualTouchActive = false;
|
||||
|
||||
const dispatchSyntheticMouse = (type: string, clientX: number, clientY: number, target: Element) => {
|
||||
const mouseEvent = new MouseEvent(type, {
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
clientX,
|
||||
clientY,
|
||||
button: 0,
|
||||
view: window,
|
||||
});
|
||||
target.dispatchEvent(mouseEvent);
|
||||
};
|
||||
|
||||
const callDocumentMouseMove = (clientX: number, clientY: number) => {
|
||||
if (typeof document.onmousemove !== "function") return;
|
||||
const target = document.elementFromPoint(clientX, clientY);
|
||||
if (!target) return;
|
||||
const fakeEvent = new MouseEvent("mousemove", {
|
||||
clientX,
|
||||
clientY,
|
||||
cancelable: true,
|
||||
bubbles: true,
|
||||
});
|
||||
target.dispatchEvent(fakeEvent);
|
||||
};
|
||||
|
||||
const callDocumentMouseUp = (clientX: number, clientY: number) => {
|
||||
if (typeof document.onmouseup !== "function") return;
|
||||
const target = document.elementFromPoint(clientX, clientY) || document.body;
|
||||
const fakeEvent = new MouseEvent("mouseup", {
|
||||
clientX,
|
||||
clientY,
|
||||
bubbles: true,
|
||||
});
|
||||
target.dispatchEvent(fakeEvent);
|
||||
};
|
||||
|
||||
const handleManualTouchStart = (e: TouchEvent) => {
|
||||
if (dragState) return;
|
||||
if (e.touches.length !== 1) return;
|
||||
|
||||
const target = e.target as HTMLElement;
|
||||
// All areas with manual mousedown/mousemove/mouseup drag/resize operations
|
||||
if (!target.closest(".dock") &&
|
||||
!target.closest(".b3-dialog") &&
|
||||
!target.closest(".sy__outline") &&
|
||||
!target.closest(".layout__resize") &&
|
||||
!target.closest(".layout__resize--lr") &&
|
||||
!target.closest(".layout__dockresize") &&
|
||||
!target.closest(".layout__dockresize--lr") &&
|
||||
!target.closest(".search__drag") &&
|
||||
// Editor-internal resize handles (not native Drag API)
|
||||
!target.closest(".av__widthdrag") &&
|
||||
!target.closest(".av__drag-fill") &&
|
||||
!target.closest(".protyle-action__drag") &&
|
||||
!target.closest(".table__resize")) return;
|
||||
|
||||
const touch = e.touches[0];
|
||||
dispatchSyntheticMouse("mousedown", touch.clientX, touch.clientY, target);
|
||||
manualTouchActive = true;
|
||||
};
|
||||
|
||||
const handleManualTouchMove = (e: TouchEvent) => {
|
||||
if (dragState?.isDragging) return;
|
||||
if (!manualTouchActive) return;
|
||||
|
||||
const touch = e.touches[0];
|
||||
if (document.onmousemove) {
|
||||
e.preventDefault();
|
||||
callDocumentMouseMove(touch.clientX, touch.clientY);
|
||||
}
|
||||
};
|
||||
|
||||
const handleManualTouchEnd = (e: TouchEvent) => {
|
||||
if (dragState?.isDragging) return;
|
||||
if (!manualTouchActive) return;
|
||||
|
||||
if (document.onmouseup) {
|
||||
callDocumentMouseUp(e.changedTouches[0].clientX, e.changedTouches[0].clientY);
|
||||
}
|
||||
manualTouchActive = false;
|
||||
};
|
||||
|
||||
// ── Native Drag API bridge ───────────────────────────────────────────
|
||||
|
||||
const getDraggableAncestor = (el: HTMLElement): HTMLElement | null => {
|
||||
let current: HTMLElement | null = el;
|
||||
while (current) {
|
||||
if (current.getAttribute?.("draggable") === "true") {
|
||||
return current;
|
||||
}
|
||||
if (current === document.body) break;
|
||||
current = current.parentElement;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const getElementUnderTouch = (clientX: number, clientY: number): Element | null => {
|
||||
if (dragState?.ghostElement) {
|
||||
dragState.ghostElement.style.display = "none";
|
||||
}
|
||||
const el = document.elementFromPoint(clientX, clientY);
|
||||
if (dragState?.ghostElement) {
|
||||
dragState.ghostElement.style.display = "";
|
||||
}
|
||||
return el;
|
||||
};
|
||||
|
||||
const positionGhost = (clientX: number, clientY: number) => {
|
||||
if (dragState?.ghostElement) {
|
||||
// Offset ghost so it's visible beside the finger, not hidden under it
|
||||
dragState.ghostElement.style.left = `${clientX + 12}px`;
|
||||
dragState.ghostElement.style.top = `${clientY + 12}px`;
|
||||
}
|
||||
};
|
||||
|
||||
const clearDragoverClasses = () => {
|
||||
document.querySelectorAll(".dragover__top, .dragover__bottom, .dragover__left, .dragover__right, .dragover").forEach((item) => {
|
||||
item.classList.remove("dragover__top", "dragover__bottom", "dragover__left", "dragover__right", "dragover");
|
||||
});
|
||||
};
|
||||
|
||||
const startTouchDrag = (touch: Touch) => {
|
||||
const dt = new DataTransfer();
|
||||
dragState.dataTransfer = dt;
|
||||
dragState.isDragging = true;
|
||||
|
||||
dragState.editorElement = dragState.draggableElement.closest(".protyle-wysiwyg") as HTMLElement;
|
||||
|
||||
window.siyuan.touchDragActive = true;
|
||||
window.siyuan.touchDragGhost = null;
|
||||
|
||||
const dragStartEvent = new DragEvent("dragstart", {
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
clientX: touch.clientX,
|
||||
clientY: touch.clientY,
|
||||
dataTransfer: dt,
|
||||
view: window,
|
||||
});
|
||||
dragState.draggableElement.dispatchEvent(dragStartEvent);
|
||||
|
||||
dragState.ghostElement = window.siyuan.touchDragGhost || null;
|
||||
if (dragState.ghostElement) {
|
||||
dragState.ghostElement.style.pointerEvents = "none";
|
||||
dragState.ghostElement.style.zIndex = (++window.siyuan.zIndex).toString();
|
||||
// Position first, then show — avoids flash at wrong position
|
||||
positionGhost(touch.clientX, touch.clientY);
|
||||
dragState.ghostElement.style.opacity = "0.6";
|
||||
}
|
||||
|
||||
if (dragState.editorElement) {
|
||||
const dragEnterEvent = new DragEvent("dragenter", {
|
||||
bubbles: false,
|
||||
cancelable: true,
|
||||
clientX: touch.clientX,
|
||||
clientY: touch.clientY,
|
||||
dataTransfer: dt,
|
||||
view: window,
|
||||
});
|
||||
dragState.editorElement.dispatchEvent(dragEnterEvent);
|
||||
}
|
||||
};
|
||||
|
||||
const continueTouchDrag = (touch: Touch) => {
|
||||
if (!dragState.isDragging) return;
|
||||
|
||||
const elementUnderTouch = getElementUnderTouch(touch.clientX, touch.clientY);
|
||||
|
||||
// Track dragenter / dragleave across container-level elements.
|
||||
// Only dispatch when element's parent changes, to avoid flickering
|
||||
// when moving between siblings of the same parent.
|
||||
if (elementUnderTouch !== lastDragOverElement) {
|
||||
const prevContainer = lastDragOverElement?.parentElement;
|
||||
const currContainer = elementUnderTouch?.parentElement;
|
||||
if (prevContainer !== currContainer || (!prevContainer && currContainer) || (prevContainer && !currContainer)) {
|
||||
if (lastDragOverElement) {
|
||||
const dragLeaveEvent = new DragEvent("dragleave", {
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
clientX: touch.clientX,
|
||||
clientY: touch.clientY,
|
||||
dataTransfer: dragState.dataTransfer,
|
||||
view: window,
|
||||
});
|
||||
lastDragOverElement.dispatchEvent(dragLeaveEvent);
|
||||
}
|
||||
if (elementUnderTouch) {
|
||||
const dragEnterEvent = new DragEvent("dragenter", {
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
clientX: touch.clientX,
|
||||
clientY: touch.clientY,
|
||||
dataTransfer: dragState.dataTransfer,
|
||||
view: window,
|
||||
});
|
||||
elementUnderTouch.dispatchEvent(dragEnterEvent);
|
||||
}
|
||||
}
|
||||
lastDragOverElement = elementUnderTouch;
|
||||
}
|
||||
|
||||
if (elementUnderTouch) {
|
||||
const dragOverEvent = new DragEvent("dragover", {
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
clientX: touch.clientX,
|
||||
clientY: touch.clientY,
|
||||
dataTransfer: dragState.dataTransfer,
|
||||
view: window,
|
||||
});
|
||||
elementUnderTouch.dispatchEvent(dragOverEvent);
|
||||
}
|
||||
|
||||
positionGhost(touch.clientX, touch.clientY);
|
||||
};
|
||||
|
||||
const endTouchDrag = (touch: Touch) => {
|
||||
if (!dragState.isDragging) return;
|
||||
|
||||
const elementUnderTouch = getElementUnderTouch(touch.clientX, touch.clientY);
|
||||
if (elementUnderTouch) {
|
||||
const dropEvent = new DragEvent("drop", {
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
clientX: touch.clientX,
|
||||
clientY: touch.clientY,
|
||||
dataTransfer: dragState.dataTransfer,
|
||||
view: window,
|
||||
});
|
||||
elementUnderTouch.dispatchEvent(dropEvent);
|
||||
}
|
||||
|
||||
const dragEndEvent = new DragEvent("dragend", {
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
clientX: touch.clientX,
|
||||
clientY: touch.clientY,
|
||||
dataTransfer: dragState.dataTransfer,
|
||||
view: window,
|
||||
});
|
||||
dragState.draggableElement.dispatchEvent(dragEndEvent);
|
||||
|
||||
clearDragoverClasses();
|
||||
};
|
||||
|
||||
const cleanupDrag = () => {
|
||||
stopScrollAnimation();
|
||||
clearDragoverClasses();
|
||||
|
||||
if (dragState?.ghostElement) {
|
||||
dragState.ghostElement.remove();
|
||||
}
|
||||
|
||||
window.siyuan.touchDragActive = false;
|
||||
window.siyuan.touchDragGhost = null;
|
||||
dragState = null;
|
||||
lastDragOverElement = null;
|
||||
};
|
||||
|
||||
const handleTouchStart = (e: TouchEvent) => {
|
||||
if (e.touches.length !== 1) return;
|
||||
|
||||
const target = e.target as HTMLElement;
|
||||
const draggable = getDraggableAncestor(target);
|
||||
if (!draggable) return;
|
||||
|
||||
const touch = e.touches[0];
|
||||
dragState = {
|
||||
dataTransfer: null,
|
||||
startX: touch.clientX,
|
||||
startY: touch.clientY,
|
||||
ghostElement: null,
|
||||
isDragging: false,
|
||||
draggableElement: draggable,
|
||||
editorElement: null,
|
||||
// File tree needs long-press to avoid conflict with scroll
|
||||
requireLongPress: draggable.closest(".sy__file") !== null,
|
||||
touchStartTime: Date.now(),
|
||||
longPressCancelled: false,
|
||||
};
|
||||
};
|
||||
|
||||
const handleTouchMove = (e: TouchEvent) => {
|
||||
if (!dragState) return;
|
||||
|
||||
const touch = e.touches[0];
|
||||
|
||||
if (!dragState.isDragging) {
|
||||
const dx = touch.clientX - dragState.startX;
|
||||
const dy = touch.clientY - dragState.startY;
|
||||
|
||||
if (Math.abs(dx) < DRAG_THRESHOLD && Math.abs(dy) < DRAG_THRESHOLD) {
|
||||
return;
|
||||
}
|
||||
|
||||
// File tree items: must hold still for 300ms, then drag.
|
||||
// Moving before 300ms is a scroll — cancel drag entirely.
|
||||
if (dragState.requireLongPress) {
|
||||
if (dragState.longPressCancelled) {
|
||||
return;
|
||||
}
|
||||
if (Date.now() - dragState.touchStartTime < 300) {
|
||||
dragState.longPressCancelled = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
e.preventDefault();
|
||||
startTouchDrag(touch);
|
||||
return;
|
||||
}
|
||||
|
||||
e.preventDefault();
|
||||
continueTouchDrag(touch);
|
||||
};
|
||||
|
||||
const handleTouchEnd = (e: TouchEvent) => {
|
||||
if (!dragState) return;
|
||||
|
||||
if (dragState.isDragging) {
|
||||
e.preventDefault();
|
||||
endTouchDrag(e.changedTouches[0]);
|
||||
}
|
||||
|
||||
cleanupDrag();
|
||||
};
|
||||
|
||||
const handleTouchCancel = () => {
|
||||
if (dragState?.isDragging) {
|
||||
cleanupDrag();
|
||||
}
|
||||
dragState = null;
|
||||
lastDragOverElement = null;
|
||||
manualTouchActive = false;
|
||||
};
|
||||
|
||||
export const initTouchDragBridge = () => {
|
||||
// Native Drag API bridge (for [draggable="true"] elements)
|
||||
document.addEventListener("touchstart", handleTouchStart, {passive: false});
|
||||
document.addEventListener("touchmove", handleTouchMove, {passive: false});
|
||||
document.addEventListener("touchend", handleTouchEnd);
|
||||
document.addEventListener("touchcancel", handleTouchCancel);
|
||||
|
||||
// Manual mousedown bridge (for dock / dialog / outline)
|
||||
document.addEventListener("touchstart", handleManualTouchStart, {passive: false});
|
||||
document.addEventListener("touchmove", handleManualTouchMove, {passive: false});
|
||||
document.addEventListener("touchend", handleManualTouchEnd);
|
||||
};
|
||||
Vendored
+2
@@ -556,6 +556,8 @@ interface ISiyuan {
|
||||
},
|
||||
dragElement?: HTMLElement,
|
||||
currentDragOverTabHeadersElement?: HTMLElement
|
||||
touchDragActive?: boolean,
|
||||
touchDragGhost?: HTMLElement | null,
|
||||
layout?: {
|
||||
layout?: import("../layout").Layout,
|
||||
centerLayout?: import("../layout").Layout,
|
||||
|
||||
Reference in New Issue
Block a user