siyuan/app/src/protyle/scroll/saveScroll.ts

88 lines
3.1 KiB
TypeScript
Raw Normal View History

import {hasClosestBlock} from "../util/hasClosest";
2023-05-16 00:05:18 +08:00
import {getSelectionOffset} from "../util/selection";
import {fetchPost} from "../../util/fetch";
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) {
2022-08-10 10:06:07 +08:00
// 报错或者空白页面
return undefined;
}
const attr: IScrollAttr = {
rootId: protyle.block.rootID,
startId: protyle.wysiwyg.element.firstElementChild.getAttribute("data-node-id"),
endId: protyle.wysiwyg.element.lastElementChild.getAttribute("data-node-id"),
scrollTop: protyle.contentElement.scrollTop || parseInt(protyle.contentElement.getAttribute("data-scrolltop")) || 0,
2022-08-08 23:25:07 +08:00
};
let range: Range;
if (getSelection().rangeCount > 0) {
2022-08-08 23:25:07 +08:00
range = getSelection().getRangeAt(0);
}
if (!range || !protyle.wysiwyg.element.contains(range.startContainer)) {
2022-08-08 23:25:07 +08:00
range = protyle.toolbar.range;
}
if (range && protyle.wysiwyg.element.contains(range.startContainer)) {
const blockElement = hasClosestBlock(range.startContainer);
if (blockElement) {
const position = getSelectionOffset(blockElement, undefined, range);
attr.focusId = blockElement.getAttribute("data-node-id");
2022-08-08 23:25:07 +08:00
attr.focusStart = position.start;
attr.focusEnd = position.end;
}
}
if (protyle.block.showAll) {
2022-08-08 23:25:07 +08:00
attr.zoomInId = protyle.block.id;
}
if (getObject) {
return attr;
}
const jsonAttr = JSON.stringify(attr);
fetchPost("/api/attr/setBlockAttrs", {id: protyle.block.rootID, attrs: {scroll: jsonAttr}}, () => {
protyle.wysiwyg.element.setAttribute("scroll", jsonAttr);
});
2022-08-08 23:25:07 +08:00
};
export const getDocByScroll = (options: {
protyle: IProtyle,
scrollAttr: IScrollAttr,
mergedOptions?: IOptions,
cb?: () => void
focus?: boolean
}) => {
2023-05-16 00:05:18 +08:00
let actions: string[] = [];
if (options.mergedOptions) {
2023-05-16 00:05:18 +08:00
actions = options.mergedOptions.action;
} else {
if (options.focus) {
2023-05-16 00:05:18 +08:00
actions = [Constants.CB_GET_UNUNDO, Constants.CB_GET_FOCUS];
} else {
actions = [Constants.CB_GET_UNUNDO];
}
}
if (options.scrollAttr.zoomInId) {
fetchPost("/api/filetree/getDoc", {
id: options.scrollAttr.zoomInId,
size: Constants.SIZE_GET_MAX,
}, response => {
actions.push(Constants.CB_GET_ALL);
onGet(response, options.protyle, actions, options.scrollAttr);
if (options.cb) {
2023-05-16 00:05:18 +08:00
options.cb();
}
2022-08-08 23:25:07 +08:00
});
return;
}
fetchPost("/api/filetree/getDoc", {
id: options.scrollAttr.rootId || options.mergedOptions?.blockId || options.protyle.block?.rootID || options.scrollAttr.startId,
startID: options.scrollAttr.startId,
endID: options.scrollAttr.endId,
}, response => {
onGet(response, options.protyle, actions, options.scrollAttr);
if (options.cb) {
2023-05-16 00:05:18 +08:00
options.cb();
}
});
2022-08-08 23:25:07 +08:00
};