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";
|
|
|
|
|
import {showMessage} from "../../dialog/message";
|
|
|
|
|
import {updateHotkeyTip} from "../util/compatibility";
|
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";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
2022-10-25 15:17:08 +08:00
|
|
|
let getIndexTimeout: number
|
2022-05-26 15:18:53 +08:00
|
|
|
export const scrollEvent = (protyle: IProtyle, element: HTMLElement) => {
|
2022-08-03 18:08:28 +08:00
|
|
|
let elementRect = element.getBoundingClientRect();
|
2022-10-25 15:17:08 +08:00
|
|
|
element.addEventListener("scroll", (event) => {
|
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);
|
2022-08-03 18:08:28 +08:00
|
|
|
if (elementRect.width === 0) {
|
|
|
|
|
elementRect = element.getBoundingClientRect();
|
|
|
|
|
}
|
|
|
|
|
const toolbarHeight = 29;
|
|
|
|
|
if (top < elementRect.top - toolbarHeight || top > elementRect.bottom - toolbarHeight) {
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
if (!protyle.selectElement.classList.contains("fn__none")) {
|
|
|
|
|
showMessage(window.siyuan.languages.crossPageUse.replace("${}", updateHotkeyTip("⇧Click")), 9000);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-31 01:14:45 +08:00
|
|
|
const panelContextElement = protyle.breadcrumb?.element.parentElement.querySelector('[data-type="context"]');
|
2022-05-26 15:18:53 +08:00
|
|
|
if (panelContextElement && !panelContextElement.classList.contains("ft__primary")) {
|
|
|
|
|
// 悬浮窗需展开上下文后才能进行滚动 https://github.com/siyuan-note/siyuan/issues/2311
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-10-25 15:20:32 +08:00
|
|
|
if (protyle.scroll && !protyle.scroll.element.classList.contains("fn__none")) {
|
|
|
|
|
clearTimeout(getIndexTimeout);
|
|
|
|
|
getIndexTimeout = window.setTimeout(() => {
|
|
|
|
|
elementRect = element.getBoundingClientRect();
|
|
|
|
|
const targetElement = document.elementFromPoint(elementRect.left + elementRect.width / 2, elementRect.top + 10)
|
|
|
|
|
const blockElement = hasClosestBlock(targetElement);
|
|
|
|
|
if (!blockElement) {
|
2022-10-25 16:00:38 +08:00
|
|
|
if (hasClosestByClassName(targetElement, "protyle-background") ||
|
|
|
|
|
hasClosestByClassName(targetElement, "protyle-title")) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
fetchPost("/api/block/getBlockIndex", {id: blockElement.getAttribute("data-node-id")}, (response) => {
|
|
|
|
|
if (!response.data) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const inputElement = protyle.scroll.element.querySelector(".b3-slider") as HTMLInputElement
|
|
|
|
|
inputElement.value = response.data;
|
|
|
|
|
protyle.scroll.element.setAttribute("aria-label", `Blocks ${response.data}/${protyle.block.blockCount}`);
|
|
|
|
|
});
|
|
|
|
|
}, Constants.TIMEOUT_BLOCKLOAD);
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
if (protyle.wysiwyg.element.getAttribute("data-top") || protyle.block.showAll || protyle.scroll.lastScrollTop === element.scrollTop || protyle.scroll.lastScrollTop === -1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (protyle.scroll.lastScrollTop - element.scrollTop > 0) {
|
|
|
|
|
// up
|
2022-08-14 10:50:59 +08:00
|
|
|
if (element.scrollTop < element.clientHeight &&
|
2022-05-26 15:18:53 +08:00
|
|
|
protyle.wysiwyg.element.firstElementChild.getAttribute("data-eof") !== "true") {
|
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,
|
|
|
|
|
k: protyle.options.key || "",
|
|
|
|
|
size: Constants.SIZE_GET,
|
|
|
|
|
}, 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 = "";
|
2022-05-26 15:18:53 +08:00
|
|
|
onGet(getResponse, protyle, [Constants.CB_GET_BEFORE, Constants.CB_GET_UNCHANGEID]);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else if ((element.scrollTop > element.scrollHeight - element.clientHeight * 1.8) &&
|
|
|
|
|
protyle.wysiwyg.element.lastElementChild &&
|
|
|
|
|
protyle.wysiwyg.element.lastElementChild.getAttribute("data-eof") !== "true") {
|
|
|
|
|
protyle.wysiwyg.element.setAttribute("data-top", element.scrollTop.toString());
|
|
|
|
|
fetchPost("/api/filetree/getDoc", {
|
|
|
|
|
id: protyle.wysiwyg.element.lastElementChild.getAttribute("data-node-id"),
|
|
|
|
|
mode: 2,
|
|
|
|
|
k: protyle.options.key || "",
|
|
|
|
|
size: Constants.SIZE_GET,
|
|
|
|
|
}, getResponse => {
|
|
|
|
|
onGet(getResponse, protyle, [Constants.CB_GET_APPEND, Constants.CB_GET_UNCHANGEID]);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
};
|