2024-04-03 16:04:13 +08:00
|
|
|
|
import {hasClosestByAttribute, hasClosestByClassName, hasTopClosestByClassName,} from "../../protyle/util/hasClosest";
|
2023-12-19 23:18:45 +08:00
|
|
|
|
import {closeModel, closePanel} from "./closePanel";
|
2023-03-30 18:12:09 +08:00
|
|
|
|
import {popMenu} from "../menu";
|
2023-03-31 16:41:09 +08:00
|
|
|
|
import {activeBlur, hideKeyboardToolbar} from "./keyboardToolbar";
|
2023-09-06 17:19:11 +08:00
|
|
|
|
import {isIPhone} from "../../protyle/util/compatibility";
|
|
|
|
|
|
import {App} from "../../index";
|
2023-11-17 22:35:16 +08:00
|
|
|
|
import {globalTouchEnd, globalTouchStart} from "../../boot/globalEvent/touch";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
|
|
|
let clientX: number;
|
|
|
|
|
|
let clientY: number;
|
|
|
|
|
|
let xDiff: number;
|
|
|
|
|
|
let yDiff: number;
|
2023-03-30 18:12:09 +08:00
|
|
|
|
let time: number;
|
2023-03-31 10:46:40 +08:00
|
|
|
|
let firstDirection: "toLeft" | "toRight";
|
2024-04-02 15:10:40 +08:00
|
|
|
|
let firstXY: "x" | "y";
|
2023-03-31 10:46:40 +08:00
|
|
|
|
let lastClientX: number; // 和起始方向不一致时,记录最后一次的 clientX
|
2024-02-04 12:00:41 +08:00
|
|
|
|
let scrollBlock: boolean;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
2023-03-31 16:41:09 +08:00
|
|
|
|
const popSide = (render = true) => {
|
|
|
|
|
|
if (render) {
|
|
|
|
|
|
document.getElementById("toolbarFile").dispatchEvent(new CustomEvent("click"));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
hideKeyboardToolbar();
|
|
|
|
|
|
activeBlur();
|
2023-04-07 09:06:30 +08:00
|
|
|
|
document.getElementById("sidebar").style.transform = "translateX(0px)";
|
2023-03-31 16:41:09 +08:00
|
|
|
|
}
|
2023-04-02 10:42:42 +08:00
|
|
|
|
};
|
2023-03-31 16:41:09 +08:00
|
|
|
|
|
2023-09-06 17:19:11 +08:00
|
|
|
|
export const handleTouchEnd = (event: TouchEvent, app: App) => {
|
2023-09-07 09:58:07 +08:00
|
|
|
|
if (isIPhone() && globalTouchEnd(event, yDiff, time, app)) {
|
|
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
return;
|
2023-04-21 17:07:00 +08:00
|
|
|
|
}
|
2023-09-07 09:58:07 +08:00
|
|
|
|
const target = event.target as HTMLElement;
|
2024-03-29 23:53:00 +08:00
|
|
|
|
if (!clientY || typeof yDiff === "undefined" ||
|
2023-04-06 16:02:06 +08:00
|
|
|
|
target.tagName === "AUDIO" ||
|
2023-04-12 10:56:46 +08:00
|
|
|
|
hasClosestByClassName(target, "b3-dialog", true) ||
|
2023-04-10 11:25:25 +08:00
|
|
|
|
(window.siyuan.mobile.editor && !window.siyuan.mobile.editor.protyle.toolbar.subElement.classList.contains("fn__none")) ||
|
2023-04-09 23:04:30 +08:00
|
|
|
|
hasClosestByClassName(target, "viewer-container") ||
|
2023-03-31 16:41:09 +08:00
|
|
|
|
hasClosestByClassName(target, "keyboard") ||
|
2023-12-19 23:18:45 +08:00
|
|
|
|
hasClosestByAttribute(target, "id", "commonMenu")
|
2023-03-31 10:46:40 +08:00
|
|
|
|
) {
|
2022-08-01 10:20:35 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2023-04-12 18:22:54 +08:00
|
|
|
|
if (window.siyuan.mobile.editor) {
|
|
|
|
|
|
window.siyuan.mobile.editor.protyle.contentElement.style.overflow = "";
|
|
|
|
|
|
}
|
2023-04-06 19:14:23 +08:00
|
|
|
|
|
2023-03-31 10:46:40 +08:00
|
|
|
|
// 有些事件不经过 touchstart 和 touchmove,因此需设置为 null 不再继续执行
|
|
|
|
|
|
clientX = null;
|
|
|
|
|
|
// 有些事件不经过 touchmove
|
|
|
|
|
|
|
2024-02-04 12:00:41 +08:00
|
|
|
|
if (scrollBlock) {
|
|
|
|
|
|
return;
|
2023-03-30 18:12:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-31 10:46:40 +08:00
|
|
|
|
let scrollEnable = false;
|
2023-03-30 18:12:09 +08:00
|
|
|
|
if (new Date().getTime() - time < 1000) {
|
2023-03-31 10:46:40 +08:00
|
|
|
|
scrollEnable = true;
|
2023-03-30 18:12:09 +08:00
|
|
|
|
} else if (Math.abs(xDiff) > window.innerWidth / 3) {
|
2023-03-31 10:46:40 +08:00
|
|
|
|
scrollEnable = true;
|
2023-03-30 18:12:09 +08:00
|
|
|
|
}
|
2023-03-31 10:46:40 +08:00
|
|
|
|
|
2023-03-31 10:48:16 +08:00
|
|
|
|
const isXScroll = Math.abs(xDiff) > Math.abs(yDiff);
|
2023-12-19 23:18:45 +08:00
|
|
|
|
const modelElement = hasClosestByAttribute(target, "id", "model");
|
|
|
|
|
|
if (modelElement) {
|
|
|
|
|
|
if (isXScroll && firstDirection === "toRight" && !lastClientX) {
|
|
|
|
|
|
closeModel();
|
|
|
|
|
|
}
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2023-03-30 18:15:20 +08:00
|
|
|
|
const menuElement = hasClosestByAttribute(target, "id", "menu");
|
2023-03-31 10:46:40 +08:00
|
|
|
|
if (menuElement) {
|
|
|
|
|
|
if (isXScroll) {
|
2023-03-31 16:41:09 +08:00
|
|
|
|
if (firstDirection === "toRight") {
|
|
|
|
|
|
if (lastClientX) {
|
|
|
|
|
|
popMenu();
|
|
|
|
|
|
} else {
|
2023-03-31 10:46:40 +08:00
|
|
|
|
closePanel();
|
|
|
|
|
|
}
|
2023-03-31 16:41:09 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
if (lastClientX) {
|
|
|
|
|
|
closePanel();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
popMenu();
|
|
|
|
|
|
}
|
2023-03-31 10:46:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2023-03-31 16:41:09 +08:00
|
|
|
|
popMenu();
|
2023-03-31 10:46:40 +08:00
|
|
|
|
}
|
2023-03-30 18:12:09 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2023-03-30 18:15:20 +08:00
|
|
|
|
const sideElement = hasClosestByAttribute(target, "id", "sidebar");
|
2023-03-31 10:46:40 +08:00
|
|
|
|
if (sideElement) {
|
|
|
|
|
|
if (isXScroll) {
|
2023-03-31 16:41:09 +08:00
|
|
|
|
if (firstDirection === "toLeft") {
|
|
|
|
|
|
if (lastClientX) {
|
|
|
|
|
|
popSide(false);
|
|
|
|
|
|
} else {
|
2023-03-31 10:46:40 +08:00
|
|
|
|
closePanel();
|
|
|
|
|
|
}
|
2023-03-31 16:41:09 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
if (lastClientX) {
|
|
|
|
|
|
closePanel();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
popSide(false);
|
|
|
|
|
|
}
|
2023-03-31 10:46:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2023-03-31 16:41:09 +08:00
|
|
|
|
popSide(false);
|
2023-03-31 10:46:40 +08:00
|
|
|
|
}
|
2023-03-30 18:12:09 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2023-03-31 10:46:40 +08:00
|
|
|
|
if (!scrollEnable || !isXScroll) {
|
2023-03-30 18:12:09 +08:00
|
|
|
|
closePanel();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2023-03-31 10:46:40 +08:00
|
|
|
|
|
|
|
|
|
|
if (xDiff > 0) {
|
|
|
|
|
|
if (lastClientX) {
|
|
|
|
|
|
closePanel();
|
|
|
|
|
|
} else {
|
2023-03-30 18:12:09 +08:00
|
|
|
|
popMenu();
|
2023-03-31 10:46:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (lastClientX) {
|
|
|
|
|
|
closePanel();
|
2022-05-26 15:18:53 +08:00
|
|
|
|
} else {
|
2023-03-31 16:41:09 +08:00
|
|
|
|
popSide();
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export const handleTouchStart = (event: TouchEvent) => {
|
2023-11-17 22:35:16 +08:00
|
|
|
|
if (globalTouchStart(event)) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2023-03-31 10:48:16 +08:00
|
|
|
|
firstDirection = null;
|
|
|
|
|
|
xDiff = undefined;
|
|
|
|
|
|
yDiff = undefined;
|
|
|
|
|
|
lastClientX = undefined;
|
2024-04-02 15:10:40 +08:00
|
|
|
|
firstXY = undefined;
|
2023-09-06 17:19:11 +08:00
|
|
|
|
if (isIPhone() ||
|
2023-03-30 18:12:09 +08:00
|
|
|
|
(event.touches[0].clientX > 8 && event.touches[0].clientX < window.innerWidth - 8)) {
|
|
|
|
|
|
clientX = event.touches[0].clientX;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
clientY = event.touches[0].clientY;
|
2023-03-30 18:12:09 +08:00
|
|
|
|
time = new Date().getTime();
|
2022-05-26 15:18:53 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
clientX = null;
|
|
|
|
|
|
clientY = null;
|
2023-03-30 18:12:09 +08:00
|
|
|
|
time = 0;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
|
|
}
|
2024-02-06 09:16:01 +08:00
|
|
|
|
scrollBlock = false;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2023-03-31 10:46:40 +08:00
|
|
|
|
let previousClientX: number;
|
2024-04-03 16:04:13 +08:00
|
|
|
|
const sideMaskElement = document.querySelector(".side-mask") as HTMLElement;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
export const handleTouchMove = (event: TouchEvent) => {
|
2023-03-31 10:46:40 +08:00
|
|
|
|
const target = event.target as HTMLElement;
|
|
|
|
|
|
if (!clientX || !clientY ||
|
2023-04-06 16:02:06 +08:00
|
|
|
|
target.tagName === "AUDIO" ||
|
2023-04-12 10:56:46 +08:00
|
|
|
|
hasClosestByClassName(target, "b3-dialog", true) ||
|
2023-04-10 11:25:25 +08:00
|
|
|
|
(window.siyuan.mobile.editor && !window.siyuan.mobile.editor.protyle.toolbar.subElement.classList.contains("fn__none")) ||
|
2023-03-31 16:41:09 +08:00
|
|
|
|
hasClosestByClassName(target, "keyboard") ||
|
2023-04-09 23:04:30 +08:00
|
|
|
|
hasClosestByClassName(target, "viewer-container") ||
|
2024-04-02 15:10:40 +08:00
|
|
|
|
hasClosestByAttribute(target, "id", "commonMenu") || firstXY === "y"
|
2023-12-19 23:18:45 +08:00
|
|
|
|
) {
|
2023-03-30 18:12:09 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2023-04-02 22:23:12 +08:00
|
|
|
|
if (getSelection().rangeCount > 0) {
|
|
|
|
|
|
// 选中后扩选的情况
|
|
|
|
|
|
const range = getSelection().getRangeAt(0);
|
|
|
|
|
|
if (range.toString() !== "" && window.siyuan.mobile.editor.protyle.wysiwyg.element.contains(range.startContainer)) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-03-31 10:46:40 +08:00
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
xDiff = Math.floor(clientX - event.touches[0].clientX);
|
|
|
|
|
|
yDiff = Math.floor(clientY - event.touches[0].clientY);
|
2023-03-31 10:46:40 +08:00
|
|
|
|
if (!firstDirection) {
|
|
|
|
|
|
firstDirection = xDiff > 0 ? "toLeft" : "toRight";
|
|
|
|
|
|
}
|
2024-04-02 15:10:40 +08:00
|
|
|
|
// 上下滚动防止左右滑动
|
|
|
|
|
|
if (!firstXY) {
|
|
|
|
|
|
if (Math.abs(xDiff) > Math.abs(yDiff)) {
|
2024-04-03 16:04:13 +08:00
|
|
|
|
firstXY = "x";
|
2024-04-02 15:10:40 +08:00
|
|
|
|
} else {
|
2024-04-03 16:04:13 +08:00
|
|
|
|
firstXY = "y";
|
2024-04-02 15:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (firstXY === "x") {
|
|
|
|
|
|
if ((hasClosestByAttribute(target, "id", "menu") && firstDirection === "toLeft") ||
|
|
|
|
|
|
(hasClosestByAttribute(target, "id", "sidebar") && firstDirection === "toRight")) {
|
2024-04-03 16:04:13 +08:00
|
|
|
|
firstXY = "y";
|
|
|
|
|
|
yDiff = undefined;
|
2024-04-02 15:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-03-31 10:46:40 +08:00
|
|
|
|
if (previousClientX) {
|
|
|
|
|
|
if (firstDirection === "toRight") {
|
|
|
|
|
|
if (previousClientX > event.touches[0].clientX) {
|
|
|
|
|
|
lastClientX = event.touches[0].clientX;
|
|
|
|
|
|
} else {
|
2023-03-31 10:48:16 +08:00
|
|
|
|
lastClientX = undefined;
|
2023-03-31 10:46:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else if (firstDirection === "toLeft") {
|
|
|
|
|
|
if (previousClientX < event.touches[0].clientX) {
|
|
|
|
|
|
lastClientX = event.touches[0].clientX;
|
|
|
|
|
|
} else {
|
2023-03-31 10:48:16 +08:00
|
|
|
|
lastClientX = undefined;
|
2023-03-31 10:46:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
previousClientX = event.touches[0].clientX;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (Math.abs(xDiff) > Math.abs(yDiff)) {
|
2023-12-19 23:18:45 +08:00
|
|
|
|
if (hasClosestByAttribute(target, "id", "model", true)) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2023-04-04 21:47:52 +08:00
|
|
|
|
let scrollElement = hasClosestByAttribute(target, "data-type", "NodeCodeBlock") ||
|
2023-07-30 14:42:28 +08:00
|
|
|
|
hasClosestByAttribute(target, "data-type", "NodeAttributeView") ||
|
2024-02-04 12:00:41 +08:00
|
|
|
|
hasClosestByAttribute(target, "data-type", "NodeMathBlock") ||
|
2023-05-30 23:56:46 +08:00
|
|
|
|
hasClosestByAttribute(target, "data-type", "NodeTable") ||
|
|
|
|
|
|
hasTopClosestByClassName(target, "list");
|
2023-03-30 18:12:09 +08:00
|
|
|
|
if (scrollElement) {
|
2023-04-04 21:47:52 +08:00
|
|
|
|
if (scrollElement.classList.contains("table")) {
|
2023-04-06 22:32:33 +08:00
|
|
|
|
scrollElement = scrollElement.firstElementChild as HTMLElement;
|
2023-04-04 21:47:52 +08:00
|
|
|
|
} else if (scrollElement.classList.contains("code-block")) {
|
2023-04-06 22:32:33 +08:00
|
|
|
|
scrollElement = scrollElement.firstElementChild.nextElementSibling as HTMLElement;
|
2023-07-30 14:42:28 +08:00
|
|
|
|
} else if (scrollElement.classList.contains("av")) {
|
2023-12-31 09:33:30 +08:00
|
|
|
|
scrollElement = hasClosestByClassName(target, "layout-tab-bar") || hasClosestByClassName(target, "av__scroll");
|
2024-02-04 12:00:41 +08:00
|
|
|
|
} else if (scrollElement.dataset.type === "NodeMathBlock") {
|
|
|
|
|
|
scrollElement = target;
|
|
|
|
|
|
while (scrollElement && scrollElement.dataset.type !== "NodeMathBlock") {
|
|
|
|
|
|
if (scrollElement.nodeType === 1 && scrollElement.scrollWidth > scrollElement.clientWidth) {
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
scrollElement = scrollElement.parentElement;
|
|
|
|
|
|
}
|
2023-04-04 21:47:52 +08:00
|
|
|
|
}
|
2024-02-04 12:00:41 +08:00
|
|
|
|
|
2023-12-31 09:33:30 +08:00
|
|
|
|
if (scrollElement && (
|
|
|
|
|
|
(xDiff < 0 && scrollElement.scrollLeft > 0) ||
|
|
|
|
|
|
(xDiff > 0 && scrollElement.clientWidth + scrollElement.scrollLeft < scrollElement.scrollWidth)
|
|
|
|
|
|
)) {
|
2024-02-06 09:16:01 +08:00
|
|
|
|
scrollBlock = true;
|
2024-02-04 12:00:41 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (scrollBlock) {
|
2023-03-30 18:12:09 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-03-31 10:46:40 +08:00
|
|
|
|
|
2023-03-31 16:41:09 +08:00
|
|
|
|
const windowWidth = window.innerWidth;
|
2023-03-30 18:15:20 +08:00
|
|
|
|
const menuElement = hasClosestByAttribute(target, "id", "menu");
|
2023-03-31 10:46:40 +08:00
|
|
|
|
if (menuElement) {
|
|
|
|
|
|
if (xDiff < 0) {
|
2023-04-06 19:14:23 +08:00
|
|
|
|
menuElement.style.transform = `translateX(${-xDiff}px)`;
|
2023-03-31 16:41:09 +08:00
|
|
|
|
transformMask(-xDiff / windowWidth);
|
|
|
|
|
|
} else {
|
2023-04-07 09:06:30 +08:00
|
|
|
|
menuElement.style.transform = "translateX(0px)";
|
2023-03-31 16:41:09 +08:00
|
|
|
|
transformMask(0);
|
2023-03-31 10:46:40 +08:00
|
|
|
|
}
|
2023-03-30 18:12:09 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2023-03-30 18:15:20 +08:00
|
|
|
|
const sideElement = hasClosestByAttribute(target, "id", "sidebar");
|
2023-03-31 10:46:40 +08:00
|
|
|
|
if (sideElement) {
|
|
|
|
|
|
if (xDiff > 0) {
|
2023-04-06 19:14:23 +08:00
|
|
|
|
sideElement.style.transform = `translateX(${-xDiff}px)`;
|
2023-03-31 16:41:09 +08:00
|
|
|
|
transformMask(xDiff / windowWidth);
|
|
|
|
|
|
} else {
|
2023-04-07 09:06:30 +08:00
|
|
|
|
sideElement.style.transform = "translateX(0px)";
|
2023-03-31 16:41:09 +08:00
|
|
|
|
transformMask(0);
|
2023-03-31 10:46:40 +08:00
|
|
|
|
}
|
2023-03-30 18:12:09 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-04-02 14:16:43 +08:00
|
|
|
|
|
2023-03-31 10:46:40 +08:00
|
|
|
|
if (firstDirection === "toRight") {
|
2024-04-02 14:16:43 +08:00
|
|
|
|
document.getElementById("sidebar").style.transform = `translateX(${Math.min(-xDiff - windowWidth, 0)}px)`;
|
2023-03-31 16:41:09 +08:00
|
|
|
|
transformMask((windowWidth + xDiff) / windowWidth);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
} else {
|
2024-04-02 14:16:43 +08:00
|
|
|
|
document.getElementById("menu").style.transform = `translateX(${Math.max(windowWidth - xDiff, 0)}px)`;
|
2023-03-31 16:41:09 +08:00
|
|
|
|
transformMask((windowWidth - xDiff) / windowWidth);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2023-04-06 11:02:20 +08:00
|
|
|
|
activeBlur();
|
|
|
|
|
|
hideKeyboardToolbar();
|
2023-04-12 18:22:54 +08:00
|
|
|
|
if (window.siyuan.mobile.editor) {
|
|
|
|
|
|
window.siyuan.mobile.editor.protyle.contentElement.style.overflow = "hidden";
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
2023-03-31 16:41:09 +08:00
|
|
|
|
|
|
|
|
|
|
const transformMask = (opacity: number) => {
|
2024-04-02 14:16:43 +08:00
|
|
|
|
sideMaskElement.classList.remove("fn__none");
|
|
|
|
|
|
sideMaskElement.style.opacity = Math.min((1 - opacity), 0.68).toString();
|
2023-04-02 10:42:42 +08:00
|
|
|
|
};
|