This commit is contained in:
Vanessa 2022-08-08 23:25:07 +08:00
parent b1ee790ee2
commit a8072882fc
5 changed files with 24 additions and 16 deletions

View file

@ -23,8 +23,6 @@ import {showMessage} from "../dialog/message";
import {openFileById, updatePanelByEditor} from "../editor/util";
import {scrollCenter} from "../util/highlightById";
import {getAllModels} from "./getAll";
import {fetchPost} from "../util/fetch";
import {onGet} from "../protyle/util/onGet";
import {countBlockWord} from "./status";
import {saveScroll} from "../protyle/scroll/saveScroll";
@ -312,7 +310,7 @@ export class Wnd {
}
});
const initData = currentTab.headElement.getAttribute("data-initdata")
const initData = currentTab.headElement.getAttribute("data-initdata");
if (initData) {
const json = JSON.parse(initData);
currentTab.addModel(new Editor({

View file

@ -22,7 +22,6 @@ import {hideElements} from "../protyle/ui/hideElements";
import {fetchPost} from "../util/fetch";
import {hasClosestBlock} from "../protyle/util/hasClosest";
import {getContenteditableElement} from "../protyle/wysiwyg/getBlock";
import {updatePanelByEditor} from "../editor/util";
import {Constants} from "../constants";
import {openSearch} from "../search/spread";
import {saveScroll} from "../protyle/scroll/saveScroll";

View file

@ -3,7 +3,6 @@ import {fetchPost} from "../../util/fetch";
import {openMobileFileById} from "../editor";
import {Constants} from "../../constants";
import {getEventName} from "../../protyle/util/compatibility";
import {focusBlock} from "../../protyle/util/selection";
export class MobileOutline {
private tree: Tree;

View file

@ -26,7 +26,6 @@ import {getNoContainerElement} from "../wysiwyg/getBlock";
import {commonHotkey} from "../wysiwyg/commonHotkey";
import {code160to32} from "../util/code160to32";
import {deleteFile} from "../../editor/deleteFile";
import {restoreScroll} from "../scroll/saveScroll";
export class Title {
public element: HTMLElement;

View file

@ -4,32 +4,36 @@ 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 {disabledProtyle, enableProtyle} from "../util/onGet";
export const saveScroll = (protyle: IProtyle, getObject = false) => {
const attr: IScrollAttr = {
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,
}
let range: Range
};
let range: Range;
if (getSelection().rangeCount > 0) {
range = getSelection().getRangeAt(0)
range = getSelection().getRangeAt(0);
}
if (!range || !protyle.wysiwyg.element.contains(range.startContainer)) {
range = protyle.toolbar.range
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");
attr.focusStart = position.start
attr.focusEnd = position.end
attr.focusStart = position.start;
attr.focusEnd = position.end;
}
}
if (protyle.block.showAll) {
attr.zoomInId = protyle.block.id
attr.zoomInId = protyle.block.id;
}
if (getObject) {
return attr;
@ -38,7 +42,7 @@ export const saveScroll = (protyle: IProtyle, getObject = false) => {
fetchPost("/api/attr/setBlockAttrs", {id: protyle.block.rootID, attrs: {scroll: jsonAttr}}, () => {
protyle.wysiwyg.element.setAttribute("scroll", jsonAttr);
});
}
};
export const restoreScroll = (protyle: IProtyle, scrollAttr: IScrollAttr) => {
preventScroll(protyle);
@ -67,7 +71,16 @@ export const restoreScroll = (protyle: IProtyle, scrollAttr: IScrollAttr) => {
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 (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);
@ -75,6 +88,6 @@ export const restoreScroll = (protyle: IProtyle, scrollAttr: IScrollAttr) => {
pushBack(protyle, range || undefined);
/// #endif
}
})
});
}
}
};