Vanessa 2023-05-13 23:04:00 +08:00
parent 65dc60a9fe
commit 0daed2d12a
12 changed files with 81 additions and 116 deletions

View file

@ -1,14 +1,8 @@
import {hasClosestBlock} from "../util/hasClosest";
import {focusByOffset, getSelectionOffset} from "../util/selection";
import {fetchPost} from "../../util/fetch";
import {zoomOut} from "../../menus/protyle";
import {preventScroll} from "./preventScroll";
import {pushBack} from "../../util/backForward";
import {processRender} from "../util/processCode";
import {highlightRender} from "../markdown/highlightRender";
import {blockRender} from "../markdown/blockRender";
import {disabledForeverProtyle, disabledProtyle, enableProtyle} from "../util/onGet";
import {showMessage} from "../../dialog/message";
import {onGet} from "../util/onGet";
import {Constants} from "../../constants";
export const saveScroll = (protyle: IProtyle, getObject = false) => {
if (!protyle.wysiwyg.element.firstElementChild || window.siyuan.config.readonly) {
@ -49,66 +43,45 @@ export const saveScroll = (protyle: IProtyle, getObject = false) => {
});
};
export const restoreScroll = (protyle: IProtyle, scrollAttr: IScrollAttr) => {
preventScroll(protyle);
if (protyle.wysiwyg.element.firstElementChild.getAttribute("data-node-id") === scrollAttr.startId &&
protyle.wysiwyg.element.lastElementChild.getAttribute("data-node-id") === scrollAttr.endId) {
// 需等动画效果完毕,才能获得最大高度。否则尾部定位无法滚动到底部
setTimeout(() => {
protyle.contentElement.scrollTop = scrollAttr.scrollTop;
}, 256);
if (scrollAttr.focusId) {
const range = focusByOffset(protyle.wysiwyg.element.querySelector(`[data-node-id="${scrollAttr.focusId}"]`), scrollAttr.focusStart, scrollAttr.focusEnd);
/// #if !MOBILE
pushBack(protyle, range || undefined);
/// #endif
export const getDocByScroll = (options: {
protyle: IProtyle,
scrollAttr: IScrollAttr,
mergedOptions?: IOptions,
cb?: () => void
focus?: boolean
}) => {
let actions: string[] = []
if (options.mergedOptions) {
actions = options.mergedOptions.action
} else {
if (options.focus) {
actions = [Constants.CB_GET_UNUNDO, Constants.CB_GET_FOCUS]
} else {
actions = [Constants.CB_GET_UNUNDO];
}
if (options.scrollAttr.zoomInId) {
actions.push(Constants.CB_GET_ALL);
}
} else if (scrollAttr.zoomInId && protyle.block.id !== scrollAttr.zoomInId) {
fetchPost("/api/block/checkBlockExist", {id: scrollAttr.zoomInId}, existResponse => {
if (existResponse.data) {
zoomOut(protyle, scrollAttr.zoomInId, undefined, true, () => {
protyle.contentElement.scrollTop = scrollAttr.scrollTop;
if (scrollAttr.focusId) {
focusByOffset(protyle.wysiwyg.element.querySelector(`[data-node-id="${scrollAttr.focusId}"]`), scrollAttr.focusStart, scrollAttr.focusEnd);
}
});
}
});
} else if (!protyle.scroll.element.classList.contains("fn__none")) {
fetchPost("/api/filetree/getDoc", {
id: protyle.block.id,
startID: scrollAttr.startId,
endID: scrollAttr.endId,
}, getResponse => {
protyle.block.showAll = false;
protyle.wysiwyg.element.innerHTML = getResponse.data.content;
processRender(protyle.wysiwyg.element);
highlightRender(protyle.wysiwyg.element);
blockRender(protyle, protyle.wysiwyg.element);
if (getResponse.data.isSyncing) {
disabledForeverProtyle(protyle);
} else {
if (protyle.disabled) {
disabledProtyle(protyle);
} else {
enableProtyle(protyle);
}
}
protyle.contentElement.scrollTop = scrollAttr.scrollTop;
if (scrollAttr.focusId) {
const range = focusByOffset(protyle.wysiwyg.element.querySelector(`[data-node-id="${scrollAttr.focusId}"]`), scrollAttr.focusStart, scrollAttr.focusEnd);
/// #if !MOBILE
pushBack(protyle, range || undefined);
/// #endif
}
// 使用动态滚动条定位到最后一个块,重启后无法触发滚动事件,需要再次更新 index
protyle.scroll.updateIndex(protyle, scrollAttr.startId);
// https://github.com/siyuan-note/siyuan/issues/8224
if (protyle.wysiwyg.element.clientHeight - parseInt(protyle.wysiwyg.element.style.paddingBottom) < protyle.contentElement.clientHeight) {
showMessage(window.siyuan.languages.scrollGetMore);
}
});
} else if (scrollAttr.scrollTop) {
protyle.contentElement.scrollTop = scrollAttr.scrollTop;
}
if (options.scrollAttr.zoomInId) {
fetchPost("/api/filetree/getDoc", {
id: options.scrollAttr.zoomInId,
size: Constants.SIZE_GET_MAX,
}, response => {
onGet(response, options.protyle, actions, options.scrollAttr);
if (options.cb) {
options.cb()
}
});
}
fetchPost("/api/filetree/getDoc", {
id: options.scrollAttr.startId,
startID: options.scrollAttr.startId,
endID: options.scrollAttr.endId,
}, response => {
onGet(response, options.protyle, actions, options.scrollAttr);
if (options.cb) {
options.cb()
}
});
};