diff --git a/app/src/mobile/dock/MobileFiles.ts b/app/src/mobile/dock/MobileFiles.ts index 7edbbd5e8..f0167a9a5 100644 --- a/app/src/mobile/dock/MobileFiles.ts +++ b/app/src/mobile/dock/MobileFiles.ts @@ -19,11 +19,19 @@ import {MenuItem} from "../../menus/Menu"; import {App} from "../../index"; import {refreshFileTree} from "../../dialog/processSystem"; import {setStorageVal} from "../../protyle/util/compatibility"; +import {showMessage} from "../../dialog/message"; export class MobileFiles extends Model { public element: HTMLElement; private actionsElement: HTMLElement; private closeElement: HTMLElement; + private touchDragState: { + selectedElement: HTMLElement; + startX: number; + startY: number; + isDragging: boolean; + ghostElement: HTMLElement; + }; constructor(app: App) { super({ @@ -275,12 +283,258 @@ export class MobileFiles extends Model { target = target.parentElement; } }); + filesElement.addEventListener("touchstart", (event: TouchEvent) => { + if (window.siyuan.config.readonly) return; + if (event.touches.length !== 1) return; + + const touch = event.touches[0]; + const target = touch.target as HTMLElement; + const liElement = target.closest(".b3-list-item") as HTMLElement; + if (!liElement) return; + + const dataType = liElement.getAttribute("data-type"); + if (dataType !== "navigation-file" && dataType !== "navigation-root") return; + + if (dataType === "navigation-root") { + if (window.siyuan.config.fileTree.sort !== 6) return; + } else { + const ulElement = liElement.closest("ul[data-sortmode]") as HTMLElement; + const sortMode = ulElement?.getAttribute("data-sortmode"); + if (sortMode !== "6" && !(window.siyuan.config.fileTree.sort === 6 && sortMode === "15")) return; + } + this.touchDragState = { + isDragging: false, + selectedElement: liElement, + startX: touch.clientX, + startY: touch.clientY, + ghostElement: null, + }; + }, {passive: false}); + + filesElement.addEventListener("touchmove", (event: TouchEvent) => { + const state = this.touchDragState; + const touch = event.touches[0]; + + if (!state.isDragging) { + if (Math.abs(touch.clientX - state.startX) < 5 && Math.abs(touch.clientY - state.startY) < 5) return; + state.isDragging = true; + + const ghostElement = document.createElement("ul"); + ghostElement.append(state.selectedElement.cloneNode(true)); + ghostElement.setAttribute("style", `touch-action: none;margin-left: -50%;margin-top:20px;z-index:8;position: fixed;top:${touch.clientX}px;left:${touch.clientY}px`); + ghostElement.setAttribute("class", "b3-list b3-list--background"); + document.body.append(ghostElement); + + state.ghostElement = ghostElement; + state.selectedElement.style.opacity = "0.38"; + } + + if (state.isDragging) { + state.ghostElement.style.left = `${touch.clientX}px`; + state.ghostElement.style.top = `${touch.clientY}px`; + + const target = document.elementFromPoint(touch.clientX, touch.clientY); + const liElement = target?.closest(".b3-list-item") as HTMLElement; + if (!liElement) return; + + this.clearDragIndicators(); + const targetDataType = liElement.getAttribute("data-type"); + const selectedDataType = state.selectedElement.getAttribute("data-type"); + if (targetDataType === "navigation-file" && selectedDataType === "navigation-root") { + return; + } + if (selectedDataType === "navigation-file" && targetDataType === "navigation-root") { + liElement.classList.add("dragover"); + return; + } + const liRect = liElement.getBoundingClientRect(); + if (selectedDataType === "navigation-root" && targetDataType === "navigation-root") { + if (touch.clientY > liRect.top + liRect.height / 2) { + liElement.classList.add("dragover__bottom"); + } else { + liElement.classList.add("dragover__top"); + } + return; + } + const dragHeight = liRect.height * .2; + if (touch.clientY > liRect.bottom - dragHeight) { + liElement.classList.add("dragover__bottom"); + } else if (touch.clientY < liRect.top + dragHeight) { + liElement.classList.add("dragover__top"); + } + + if (!liElement.classList.contains("dragover__top") && !liElement.classList.contains("dragover__bottom") && + !(selectedDataType === "navigation-root" && targetDataType === "navigation-root")) { + liElement.classList.add("dragover"); + } + event.preventDefault(); + } + }, {passive: false}); + + filesElement.addEventListener("touchend", async (event: TouchEvent) => { + const state = this.touchDragState; + if (!state) return; + state.selectedElement.style.opacity = ""; + if (state.isDragging) { + event.preventDefault(); + if (state.ghostElement) { + state.ghostElement.remove(); + } + const newElement = this.element.querySelector(".dragover, .dragover__bottom, .dragover__top"); + if (!newElement) { + return; + } + const newUlElement = hasTopClosestByTag(newElement, "UL"); + if (!newUlElement) { + return; + } + const oldScrollTop = this.element.scrollTop; + const toURL = newUlElement.getAttribute("data-url"); + const toPath = newElement.getAttribute("data-path"); + + const selectRootElements: HTMLElement[] = []; + const selectFileElements: HTMLElement[] = []; + const fromPaths: string[] = []; + if (state.selectedElement.getAttribute("data-type") === "navigation-root") { + selectRootElements.push(state.selectedElement); + } else { + const dataPath = state.selectedElement.getAttribute("data-path"); + // 禁止父节点移动到子节点 https://github.com/siyuan-note/siyuan/issues/12539 + if (newElement.getAttribute("data-path").startsWith(dataPath.replace(".sy", ""))) { + this.clearDragIndicators(); + this.touchDragState = null; + return; + } + fromPaths.push(dataPath); + selectFileElements.push(state.selectedElement); + } + + if (newElement.classList.contains("dragover")) { + fetchPost("/api/filetree/moveDocs", { + toNotebook: toURL, + fromPaths, + toPath, + }); + this.clearDragIndicators(); + this.touchDragState = null; + return; + } + if (newElement.classList.contains("dragover__bottom") || newElement.classList.contains("dragover__top")) { + const ulSort = newUlElement.getAttribute("data-sortmode"); + if (window.siyuan.config.fileTree.sort === 6 && selectRootElements.length > 0 && + newElement.getAttribute("data-path") === "/") { + if (newElement.classList.contains("dragover__top")) { + selectRootElements.forEach(item => { + newElement.parentElement.before(item.parentElement); + }); + } else { + selectRootElements.reverse().forEach(item => { + newElement.parentElement.after(item.parentElement); + }); + } + const notebooks: string[] = []; + Array.from(this.element.children).forEach(item => { + notebooks.push(item.getAttribute("data-url")); + }); + fetchPost("/api/notebook/changeSortNotebook", { + notebooks, + }); + } else if ((ulSort === "6" || (window.siyuan.config.fileTree.sort === 6 && ulSort === "15")) && selectFileElements.length > 0) { + let hasMove = false; + const toDir = pathPosix().dirname(toPath); + if (fromPaths.length > 0) { + await fetchSyncPost("/api/filetree/moveDocs", { + toNotebook: toURL, + fromPaths, + toPath: toDir === "/" ? "/" : toDir + ".sy", + callback: Constants.CB_MOVE_NOLIST, + }); + selectFileElements.forEach(item => { + item.setAttribute("data-path", pathPosix().join(toDir, item.getAttribute("data-node-id") + ".sy")); + }); + hasMove = true; + } + if (newElement.classList.contains("dragover__top")) { + selectFileElements.forEach(item => { + let nextULElement; + if (item.nextElementSibling && item.nextElementSibling.tagName === "UL") { + nextULElement = item.nextElementSibling; + } + newElement.before(item); + if (nextULElement) { + item.after(nextULElement); + } + }); + } else if (newElement.classList.contains("dragover__bottom")) { + selectFileElements.reverse().forEach(item => { + let nextULElement; + if (item.nextElementSibling && item.nextElementSibling.tagName === "UL") { + nextULElement = item.nextElementSibling; + } + if (newElement.nextElementSibling && newElement.nextElementSibling.tagName === "UL") { + newElement.nextElementSibling.after(item); + } else { + newElement.after(item); + } + if (nextULElement) { + item.after(nextULElement); + } + }); + } + const paths: string[] = []; + Array.from(newElement.parentElement.children).forEach(item => { + if (item.tagName === "LI") { + paths.push(item.getAttribute("data-path")); + } + }); + fetchPost("/api/filetree/changeSort", { + paths, + notebook: toURL + }, () => { + if (hasMove) { + fetchPost("/api/filetree/listDocsByPath", { + notebook: toURL, + path: toDir === "/" ? "/" : toDir + ".sy", + app: Constants.SIYUAN_APPID, + }, response => { + if (response.data.path === "/" && response.data.files.length === 0) { + showMessage(window.siyuan.languages.emptyContent); + return; + } + this.onLsHTML(response.data, oldScrollTop); + }); + } + }); + } + } + } + this.clearDragIndicators(); + this.touchDragState = null; + }); + + filesElement.addEventListener("touchcancel", () => { + if (this.touchDragState?.ghostElement) { + this.touchDragState.ghostElement.remove(); + } + if (this.touchDragState?.selectedElement) { + this.touchDragState.selectedElement.style.opacity = ""; + } + this.clearDragIndicators(); + this.touchDragState = null; + }); this.init(); if (window.siyuan.config.openHelp) { mountHelp(); } } + private clearDragIndicators = () => { + if (!this.touchDragState) return; + this.element.querySelectorAll(".dragover__top, .dragover__bottom, .dragover").forEach(el => { + el.classList.remove("dragover__top", "dragover__bottom", "dragover"); + }); + }; + private genSort() { window.siyuan.menus.menu.remove(); const subMenu = sortMenu("notebooks", window.siyuan.config.fileTree.sort, (sort: number) => { @@ -374,6 +628,7 @@ export class MobileFiles extends Model { let html = ""; let closeHtml = ""; let closeCounter = 0; + const scrollTop = this.element.scrollTop; window.siyuan.notebooks.forEach((item) => { if (item.closed) { closeCounter++; @@ -391,10 +646,11 @@ export class MobileFiles extends Model { } else { this.closeElement.classList.add("fn__none"); } - window.siyuan.storage[Constants.LOCAL_FILESPATHS].forEach((item: IFilesPath) => { - item.openPaths.forEach((openPath) => { - this.selectItem(item.notebookId, openPath, undefined, false, false); - }); + window.siyuan.storage[Constants.LOCAL_FILESPATHS].forEach(async (item: IFilesPath) => { + for (const openPath of item.openPaths) { + await this.selectItem(item.notebookId, openPath, undefined, false, false); + } + this.element.scrollTop = scrollTop; }); this.refreshPublishAccessSwitch(); if (!init) { @@ -573,7 +829,7 @@ export class MobileFiles extends Model { } - private onLsHTML(data: { files: IFile[], box: string, path: string }) { + private onLsHTML(data: { files: IFile[], box: string, path: string }, scrollTop?: number) { if (data.files.length === 0) { return; } @@ -600,6 +856,9 @@ export class MobileFiles extends Model { } }); nextElement.innerHTML = tempElement.innerHTML; + if (typeof scrollTop === "number") { + this.element.scroll({top: scrollTop, behavior: "smooth"}); + } this.refreshPublishAccessSwitch(); return; } @@ -613,6 +872,9 @@ export class MobileFiles extends Model { item.classList.remove("file-tree__sliderDown"); item.removeAttribute("style"); }); + if (typeof scrollTop === "number") { + this.element.scroll({top: scrollTop, behavior: "smooth"}); + } }, 120); }, 2); this.refreshPublishAccessSwitch(); diff --git a/app/src/mobile/util/touch.ts b/app/src/mobile/util/touch.ts index 0874931ac..886be21df 100644 --- a/app/src/mobile/util/touch.ts +++ b/app/src/mobile/util/touch.ts @@ -7,7 +7,7 @@ import { import {closeModel, closePanel} from "./closePanel"; import {popMenu} from "../menu"; import {activeBlur} from "./keyboardToolbar"; -import {isIPhone} from "../../protyle/util/compatibility"; +import {isChromeBrowser, isIPhone} from "../../protyle/util/compatibility"; import {getRangeByPoint} from "../../protyle/util/selection"; import {getCurrentEditor} from "../editor"; @@ -33,9 +33,9 @@ const popSide = (render = true) => { export const handleTouchEnd = (event: TouchEvent) => { const target = event.target as HTMLElement; - - if (!window.siyuan.touchDragActive && isIPhone() && typeof yDiff === "undefined" && - new Date().getTime() - time > 900) { + const currentTime = Date.now(); + if (!window.siyuan.touchDragActive && isIPhone() && !isChromeBrowser() && typeof yDiff === "undefined" && + currentTime - time > 900 && currentTime - time < 2000) { target.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true, cancelable: true, @@ -87,7 +87,7 @@ export const handleTouchEnd = (event: TouchEvent) => { } let scrollEnable = false; - if (new Date().getTime() - time < 1000) { + if (Date.now() - time < 1000) { scrollEnable = true; } else if (Math.abs(xDiff) > window.innerWidth / 3) { scrollEnable = true; @@ -164,6 +164,7 @@ export const handleTouchEnd = (event: TouchEvent) => { }; export const handleTouchStart = (event: TouchEvent) => { + time = Date.now(); const target = event.touches[0].target as HTMLElement; if (0 < event.touches.length && (target.tagName === "VIDEO" || target.tagName === "AUDIO")) { // https://github.com/siyuan-note/siyuan/issues/14569 @@ -173,12 +174,11 @@ export const handleTouchStart = (event: TouchEvent) => { // 存在其他拖拽元素时 const otherTouchElement = hasClosestByClassName(target, "b3-chip"); if ((otherTouchElement && otherTouchElement.parentElement.classList.contains("b3-chips__doctag")) || - target.closest(".protyle-gutters")|| + target.closest(".protyle-gutters") || target.closest('[style^="--file-toggle-width"]') || (target.tagName === "IMG" && target.style.cursor === "move" && target.parentElement.classList.contains("protyle-background__img"))) { clientX = null; clientY = null; - time = 0; return; } if (getSelection().rangeCount > 0 && hasClosestBlock(event.target as Element)) { @@ -198,11 +198,9 @@ export const handleTouchStart = (event: TouchEvent) => { (event.touches[0].clientX > 8 && event.touches[0].clientX < window.innerWidth - 8)) { clientX = event.touches[0].clientX; clientY = event.touches[0].clientY; - time = new Date().getTime(); } else { clientX = null; clientY = null; - time = 0; event.stopImmediatePropagation(); } isFirstMove = true;