2022-08-07 00:14:50 +08:00
|
|
|
import {hasClosestBlock} from "../util/hasClosest";
|
2023-05-16 00:05:18 +08:00
|
|
|
import {getSelectionOffset} from "../util/selection";
|
2022-08-07 00:14:50 +08:00
|
|
|
import {fetchPost} from "../../util/fetch";
|
2023-05-13 23:04:00 +08:00
|
|
|
import {onGet} from "../util/onGet";
|
|
|
|
|
import {Constants} from "../../constants";
|
2022-08-07 00:14:50 +08:00
|
|
|
|
2022-08-07 21:58:41 +08:00
|
|
|
export const saveScroll = (protyle: IProtyle, getObject = false) => {
|
2023-03-10 10:00:03 +08:00
|
|
|
if (!protyle.wysiwyg.element.firstElementChild || window.siyuan.config.readonly) {
|
2022-08-10 10:06:07 +08:00
|
|
|
// 报错或者空白页面
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
2022-08-07 21:58:41 +08:00
|
|
|
const attr: IScrollAttr = {
|
2023-05-17 12:37:56 +08:00
|
|
|
rootId: protyle.block.rootID,
|
2022-08-07 21:58:41 +08:00
|
|
|
startId: protyle.wysiwyg.element.firstElementChild.getAttribute("data-node-id"),
|
|
|
|
|
endId: protyle.wysiwyg.element.lastElementChild.getAttribute("data-node-id"),
|
2022-08-08 10:42:07 +08:00
|
|
|
scrollTop: protyle.contentElement.scrollTop || parseInt(protyle.contentElement.getAttribute("data-scrolltop")) || 0,
|
2022-08-08 23:25:07 +08:00
|
|
|
};
|
|
|
|
|
let range: Range;
|
2022-08-07 00:14:50 +08:00
|
|
|
if (getSelection().rangeCount > 0) {
|
2022-08-08 23:25:07 +08:00
|
|
|
range = getSelection().getRangeAt(0);
|
2022-08-07 00:14:50 +08:00
|
|
|
}
|
2022-08-08 10:42:07 +08:00
|
|
|
if (!range || !protyle.wysiwyg.element.contains(range.startContainer)) {
|
2022-08-08 23:25:07 +08:00
|
|
|
range = protyle.toolbar.range;
|
2022-08-08 10:42:07 +08:00
|
|
|
}
|
2022-08-07 00:14:50 +08:00
|
|
|
if (range && protyle.wysiwyg.element.contains(range.startContainer)) {
|
|
|
|
|
const blockElement = hasClosestBlock(range.startContainer);
|
|
|
|
|
if (blockElement) {
|
|
|
|
|
const position = getSelectionOffset(blockElement, undefined, range);
|
2022-08-07 21:58:41 +08:00
|
|
|
attr.focusId = blockElement.getAttribute("data-node-id");
|
2022-08-08 23:25:07 +08:00
|
|
|
attr.focusStart = position.start;
|
|
|
|
|
attr.focusEnd = position.end;
|
2022-08-07 00:14:50 +08:00
|
|
|
}
|
|
|
|
|
}
|
2022-08-08 10:42:07 +08:00
|
|
|
|
2022-08-07 00:14:50 +08:00
|
|
|
if (protyle.block.showAll) {
|
2022-08-08 23:25:07 +08:00
|
|
|
attr.zoomInId = protyle.block.id;
|
2022-08-07 00:14:50 +08:00
|
|
|
}
|
2022-08-07 21:58:41 +08:00
|
|
|
if (getObject) {
|
2022-08-07 17:56:07 +08:00
|
|
|
return attr;
|
|
|
|
|
}
|
2022-08-08 10:48:49 +08:00
|
|
|
const jsonAttr = JSON.stringify(attr);
|
|
|
|
|
fetchPost("/api/attr/setBlockAttrs", {id: protyle.block.rootID, attrs: {scroll: jsonAttr}}, () => {
|
|
|
|
|
protyle.wysiwyg.element.setAttribute("scroll", jsonAttr);
|
2022-08-07 00:14:50 +08:00
|
|
|
});
|
2022-08-08 23:25:07 +08:00
|
|
|
};
|
2022-08-07 00:14:50 +08:00
|
|
|
|
2023-05-13 23:04:00 +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[] = [];
|
2023-05-13 23:04:00 +08:00
|
|
|
if (options.mergedOptions) {
|
2023-05-16 00:05:18 +08:00
|
|
|
actions = options.mergedOptions.action;
|
2023-05-13 23:04:00 +08:00
|
|
|
} else {
|
|
|
|
|
if (options.focus) {
|
2023-05-16 00:05:18 +08:00
|
|
|
actions = [Constants.CB_GET_UNUNDO, Constants.CB_GET_FOCUS];
|
2023-05-13 23:04:00 +08:00
|
|
|
} else {
|
|
|
|
|
actions = [Constants.CB_GET_UNUNDO];
|
2022-08-07 21:58:41 +08:00
|
|
|
}
|
2023-05-13 23:04:00 +08:00
|
|
|
}
|
|
|
|
|
if (options.scrollAttr.zoomInId) {
|
2022-08-07 00:14:50 +08:00
|
|
|
fetchPost("/api/filetree/getDoc", {
|
2023-05-13 23:04:00 +08:00
|
|
|
id: options.scrollAttr.zoomInId,
|
|
|
|
|
size: Constants.SIZE_GET_MAX,
|
|
|
|
|
}, response => {
|
2023-05-17 12:37:56 +08:00
|
|
|
actions.push(Constants.CB_GET_ALL);
|
2023-05-17 19:14:21 +08:00
|
|
|
options.protyle.breadcrumb?.toggleExit(false);
|
2023-05-13 23:04:00 +08:00
|
|
|
onGet(response, options.protyle, actions, options.scrollAttr);
|
|
|
|
|
if (options.cb) {
|
2023-05-16 00:05:18 +08:00
|
|
|
options.cb();
|
2023-05-10 22:58:07 +08:00
|
|
|
}
|
2022-08-08 23:25:07 +08:00
|
|
|
});
|
2023-05-17 12:37:56 +08:00
|
|
|
return;
|
2022-08-07 00:14:50 +08:00
|
|
|
}
|
2023-05-13 23:04:00 +08:00
|
|
|
fetchPost("/api/filetree/getDoc", {
|
2023-05-17 12:38:31 +08:00
|
|
|
id: options.scrollAttr.rootId || options.mergedOptions?.blockId || options.protyle.block?.rootID || options.scrollAttr.startId,
|
2023-05-13 23:04:00 +08:00
|
|
|
startID: options.scrollAttr.startId,
|
|
|
|
|
endID: options.scrollAttr.endId,
|
|
|
|
|
}, response => {
|
2023-05-17 19:14:21 +08:00
|
|
|
options.protyle.breadcrumb?.toggleExit(true);
|
2023-05-13 23:04:00 +08:00
|
|
|
onGet(response, options.protyle, actions, options.scrollAttr);
|
|
|
|
|
if (options.cb) {
|
2023-05-16 00:05:18 +08:00
|
|
|
options.cb();
|
2023-05-13 23:04:00 +08:00
|
|
|
}
|
|
|
|
|
});
|
2022-08-08 23:25:07 +08:00
|
|
|
};
|