2022-05-26 15:18:53 +08:00
|
|
|
import {Constants} from "../../constants";
|
|
|
|
|
import {hideElements} from "../ui/hideElements";
|
|
|
|
|
import {fetchPost} from "../../util/fetch";
|
|
|
|
|
import {onGet} from "../util/onGet";
|
2022-08-08 10:42:07 +08:00
|
|
|
import {isMobile} from "../../util/functions";
|
2022-10-25 16:00:38 +08:00
|
|
|
import {hasClosestBlock, hasClosestByClassName} from "../util/hasClosest";
|
2023-11-15 09:02:17 +08:00
|
|
|
import {avScroll} from "../render/av/scroll";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
2022-10-27 11:34:34 +08:00
|
|
|
let getIndexTimeout: number;
|
2023-06-01 20:50:49 +08:00
|
|
|
export const scrollEvent = (protyle: IProtyle, element: HTMLElement) => {
|
2022-10-27 11:34:34 +08:00
|
|
|
element.addEventListener("scroll", () => {
|
2023-11-11 00:38:32 +08:00
|
|
|
const elementRect = element.getBoundingClientRect();
|
2022-08-03 18:08:28 +08:00
|
|
|
if (!protyle.toolbar.element.classList.contains("fn__none")) {
|
|
|
|
|
const initY = protyle.toolbar.element.getAttribute("data-inity").split(Constants.ZWSP);
|
2022-08-06 18:01:20 +08:00
|
|
|
const top = parseInt(initY[0]) + (parseInt(initY[1]) - element.scrollTop);
|
2023-11-11 00:38:32 +08:00
|
|
|
if (top < elementRect.top - protyle.toolbar.toolbarHeight || top > elementRect.bottom - protyle.toolbar.toolbarHeight) {
|
2022-08-03 18:08:28 +08:00
|
|
|
protyle.toolbar.element.style.display = "none";
|
|
|
|
|
} else {
|
|
|
|
|
protyle.toolbar.element.style.top = top + "px";
|
|
|
|
|
protyle.toolbar.element.style.display = "";
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-08 10:42:07 +08:00
|
|
|
|
2023-11-09 17:00:55 +08:00
|
|
|
protyle.wysiwyg.element.querySelectorAll(".av").forEach((item: HTMLElement) => {
|
2023-11-15 09:02:17 +08:00
|
|
|
avScroll(element, item);
|
2023-11-09 17:00:55 +08:00
|
|
|
});
|
|
|
|
|
|
2022-08-08 10:42:07 +08:00
|
|
|
if (!protyle.element.classList.contains("block__edit") && !isMobile()) {
|
|
|
|
|
protyle.contentElement.setAttribute("data-scrolltop", element.scrollTop.toString());
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
if (!window.siyuan.dragElement) { // https://ld246.com/article/1649638389841
|
|
|
|
|
hideElements(["gutter"], protyle);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 15:20:32 +08:00
|
|
|
if (protyle.scroll && !protyle.scroll.element.classList.contains("fn__none")) {
|
|
|
|
|
clearTimeout(getIndexTimeout);
|
|
|
|
|
getIndexTimeout = window.setTimeout(() => {
|
2022-10-27 11:34:34 +08:00
|
|
|
const targetElement = document.elementFromPoint(elementRect.left + elementRect.width / 2, elementRect.top + 10);
|
2022-10-25 15:20:32 +08:00
|
|
|
const blockElement = hasClosestBlock(targetElement);
|
|
|
|
|
if (!blockElement) {
|
2023-09-18 10:44:12 +08:00
|
|
|
if ((protyle.wysiwyg.element.firstElementChild.getAttribute("data-eof") === "1" ||
|
2023-11-10 10:51:34 +08:00
|
|
|
// goHome 时 data-eof 不为 1
|
2023-09-18 10:44:12 +08:00
|
|
|
protyle.wysiwyg.element.firstElementChild.getAttribute("data-node-index") === "0") &&
|
2023-05-10 22:32:08 +08:00
|
|
|
(hasClosestByClassName(targetElement, "protyle-background") || hasClosestByClassName(targetElement, "protyle-title"))) {
|
2022-10-25 16:00:38 +08:00
|
|
|
const inputElement = protyle.scroll.element.querySelector(".b3-slider") as HTMLInputElement;
|
|
|
|
|
inputElement.value = "1";
|
|
|
|
|
protyle.scroll.element.setAttribute("aria-label", `Blocks 1/${protyle.block.blockCount}`);
|
|
|
|
|
}
|
2022-10-25 15:20:32 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2023-05-10 22:32:08 +08:00
|
|
|
protyle.scroll.updateIndex(protyle, blockElement.getAttribute("data-node-id"));
|
2023-05-10 20:18:40 +08:00
|
|
|
}, Constants.TIMEOUT_LOAD);
|
2022-10-25 15:20:32 +08:00
|
|
|
}
|
2023-01-14 11:22:04 +08:00
|
|
|
if (protyle.wysiwyg.element.getAttribute("data-top") || protyle.block.showAll ||
|
|
|
|
|
(protyle.scroll && protyle.scroll.element.classList.contains("fn__none")) || !protyle.scroll ||
|
|
|
|
|
protyle.scroll.lastScrollTop === element.scrollTop || protyle.scroll.lastScrollTop === -1) {
|
2022-05-26 15:18:53 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (protyle.scroll.lastScrollTop - element.scrollTop > 0) {
|
|
|
|
|
// up
|
2022-08-14 10:50:59 +08:00
|
|
|
if (element.scrollTop < element.clientHeight &&
|
2023-05-10 22:32:08 +08:00
|
|
|
protyle.wysiwyg.element.firstElementChild.getAttribute("data-eof") !== "1") {
|
2022-10-26 23:12:41 +08:00
|
|
|
// 禁用滚动时会产生抖动 https://ld246.com/article/1666717094418
|
|
|
|
|
protyle.contentElement.style.width = (protyle.contentElement.clientWidth) + "px";
|
2022-08-14 10:50:59 +08:00
|
|
|
protyle.contentElement.style.overflow = "hidden";
|
2022-05-26 15:18:53 +08:00
|
|
|
protyle.wysiwyg.element.setAttribute("data-top", element.scrollTop.toString());
|
|
|
|
|
fetchPost("/api/filetree/getDoc", {
|
|
|
|
|
id: protyle.wysiwyg.element.firstElementChild.getAttribute("data-node-id"),
|
|
|
|
|
mode: 1,
|
2022-10-30 23:13:41 +08:00
|
|
|
size: window.siyuan.config.editor.dynamicLoadBlocks,
|
2022-05-26 15:18:53 +08:00
|
|
|
}, getResponse => {
|
2022-08-14 10:50:59 +08:00
|
|
|
protyle.contentElement.style.overflow = "";
|
2022-10-26 23:12:41 +08:00
|
|
|
protyle.contentElement.style.width = "";
|
2023-06-01 14:56:21 +08:00
|
|
|
onGet({
|
|
|
|
|
data: getResponse,
|
|
|
|
|
protyle,
|
|
|
|
|
action: [Constants.CB_GET_BEFORE, Constants.CB_GET_UNCHANGEID],
|
|
|
|
|
});
|
2022-05-26 15:18:53 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else if ((element.scrollTop > element.scrollHeight - element.clientHeight * 1.8) &&
|
|
|
|
|
protyle.wysiwyg.element.lastElementChild &&
|
2023-05-10 22:32:08 +08:00
|
|
|
protyle.wysiwyg.element.lastElementChild.getAttribute("data-eof") !== "2") {
|
2022-05-26 15:18:53 +08:00
|
|
|
protyle.wysiwyg.element.setAttribute("data-top", element.scrollTop.toString());
|
|
|
|
|
fetchPost("/api/filetree/getDoc", {
|
|
|
|
|
id: protyle.wysiwyg.element.lastElementChild.getAttribute("data-node-id"),
|
|
|
|
|
mode: 2,
|
2022-10-30 23:13:41 +08:00
|
|
|
size: window.siyuan.config.editor.dynamicLoadBlocks,
|
2022-05-26 15:18:53 +08:00
|
|
|
}, getResponse => {
|
2023-06-01 14:56:21 +08:00
|
|
|
onGet({
|
|
|
|
|
data: getResponse,
|
|
|
|
|
protyle,
|
|
|
|
|
action: [Constants.CB_GET_APPEND, Constants.CB_GET_UNCHANGEID],
|
|
|
|
|
});
|
2022-05-26 15:18:53 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
protyle.scroll.lastScrollTop = Math.max(element.scrollTop, 0);
|
2022-08-03 18:08:28 +08:00
|
|
|
}, {
|
|
|
|
|
capture: false,
|
|
|
|
|
passive: true,
|
|
|
|
|
once: false
|
2022-05-26 15:18:53 +08:00
|
|
|
});
|
|
|
|
|
};
|