This commit is contained in:
Vanessa 2023-05-17 12:37:56 +08:00
parent 05248d66be
commit fb0c2e3d6a
7 changed files with 26 additions and 24 deletions

View file

@ -351,6 +351,10 @@ export const JSONToCenter = (app: App, json: ILayoutJSON, layout?: Layout | Wnd
if (window.siyuan.config.fileTree.openFilesUseCurrentTab) {
(layout as Tab).headElement.classList.add("item--unupdate");
}
if (json.scrollAttr) {
// 历史数据兼容
json.scrollAttr.rootId = json.rootId;
}
(layout as Tab).headElement.setAttribute("data-initdata", JSON.stringify(json));
} else if (json.instance === "Asset") {
(layout as Tab).addModel(new Asset({
@ -647,7 +651,7 @@ export const copyTab = (app: App, tab: Tab) => {
if (tab.model instanceof Editor) {
model = new Editor({
tab: newTab,
blockId: tab.model.editor.protyle.block.rootID,
blockId: tab.model.editor.protyle.block.id,
scrollAttr: saveScroll(tab.model.editor.protyle, true)
});
} else if (tab.model instanceof Asset) {
@ -708,9 +712,12 @@ export const copyTab = (app: App, tab: Tab) => {
}
} else if (!tab.model && tab.headElement) {
const initData = JSON.parse(tab.headElement.getAttribute("data-initdata") || "{}");
if (initData.scrollAttr) {
initData.scrollAttr.rootId = initData.rootId;
}
model = new Editor({
tab: newTab,
blockId: initData.rootId || initData.blockId,
blockId: initData.blockId,
mode: initData.mode,
action: typeof initData.action === "string" ? [initData.action] : initData.action,
scrollAttr: initData.scrollAttr,

View file

@ -218,6 +218,7 @@ export class Protyle {
}
}
if (scrollObj) {
scrollObj.rootId = response.data.rootID;
getDocByScroll({
protyle: this.protyle,
scrollAttr: scrollObj,

View file

@ -10,6 +10,7 @@ export const saveScroll = (protyle: IProtyle, getObject = false) => {
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,
@ -59,23 +60,22 @@ export const getDocByScroll = (options: {
} else {
actions = [Constants.CB_GET_UNUNDO];
}
if (options.scrollAttr.zoomInId) {
actions.push(Constants.CB_GET_ALL);
}
}
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) {
options.cb();
}
});
return;
}
fetchPost("/api/filetree/getDoc", {
id: options.mergedOptions.blockId,
id: options.mergedOptions?.blockId || options.protyle.block?.rootID || options.scrollAttr.startId,
startID: options.scrollAttr.startId,
endID: options.scrollAttr.endId,
}, response => {

View file

@ -55,7 +55,7 @@ export const onGet = (data: IWebSocketData, protyle: IProtyle, action: string[]
protyle.block.scroll = data.data.scroll;
protyle.block.action = action;
if (!action.includes(Constants.CB_GET_UNCHANGEID)) {
protyle.block.id = data.data.id;
protyle.block.id = data.data.id; // 非缩放情况时不一定是 rootID搜索打开页签缩放时必为缩放 id否则需查看代码
protyle.scroll.lastScrollTop = 0;
protyle.contentElement.scrollTop = 0;
protyle.wysiwyg.element.setAttribute("data-doc-type", data.data.type);
@ -173,7 +173,7 @@ const setHTML = (options: {
}
/// #endif
} else {
focusElement(protyle, options);
focusElementById(protyle, options.action);
}
}
if (!protyle.scroll.element.classList.contains("fn__none")) {
@ -193,7 +193,7 @@ const setHTML = (options: {
}
/// #endif
} else if (options.action.includes(Constants.CB_GET_FOCUS)) {
focusElement(protyle, options);
focusElementById(protyle, options.action);
} else if (options.action.includes(Constants.CB_GET_FOCUSFIRST)) {
// settimeout 时间需短一点,否则定位后快速滚动无效
const headerHeight = protyle.wysiwyg.element.offsetTop - 16;
@ -322,13 +322,7 @@ export const enableProtyle = (protyle: IProtyle) => {
};
const focusElement = (protyle: IProtyle, options: {
content: string,
action?: string[],
isSyncing: boolean,
expand: boolean,
scrollAttr?: IScrollAttr
}) => {
const focusElementById = (protyle: IProtyle, action: string[]) => {
let focusElement: Element;
Array.from(protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${protyle.block.id}"]`)).find((item: HTMLElement) => {
if (!hasClosestByAttribute(item, "data-type", "block-render", true)) {
@ -343,7 +337,7 @@ const focusElement = (protyle: IProtyle, options: {
if (focusElement && !protyle.wysiwyg.element.firstElementChild.isSameNode(focusElement)) {
focusBlock(focusElement);
/// #if !MOBILE
if (!options.action.includes(Constants.CB_GET_UNUNDO)) {
if (!action.includes(Constants.CB_GET_UNUNDO)) {
pushBack(protyle, undefined, focusElement);
}
/// #endif
@ -355,7 +349,7 @@ const focusElement = (protyle: IProtyle, options: {
} else {
focusBlock(protyle.wysiwyg.element.firstElementChild);
/// #if !MOBILE
if (!options.action.includes(Constants.CB_GET_UNUNDO)) {
if (!action.includes(Constants.CB_GET_UNUNDO)) {
pushBack(protyle, undefined, protyle.wysiwyg.element.firstElementChild);
}
/// #endif

View file

@ -242,6 +242,7 @@ interface ISiyuan {
}
interface IScrollAttr {
rootId: string,
startId: string,
endId: string
scrollTop: number,
@ -269,6 +270,7 @@ interface IObject {
}
declare interface ILayoutJSON extends ILayoutOptions {
scrollAttr?: IScrollAttr,
instance?: string,
width?: string,
height?: string,

View file

@ -47,6 +47,7 @@ const focusStack = async (stack: IBackStack) => {
docIcon: info.data.rootIcon,
callback(tab) {
const scrollAttr = saveScroll(stack.protyle, true);
scrollAttr.rootId = stack.protyle.block.rootID;
scrollAttr.focusId = stack.id;
scrollAttr.focusStart = stack.position.start;
scrollAttr.focusEnd = stack.position.end;

View file

@ -45,6 +45,8 @@ export const openNewWindowById = (id: string) => {
fetchPost("/api/attr/getBlockAttrs", {id}, (attrResponse) => {
if (attrResponse.data.scroll) {
json.children.scrollAttr = JSON.parse(attrResponse.data.scroll);
// 历史数据兼容
json.children.scrollAttr.rootId = response.data.rootID;
}
/// #if !BROWSER
ipcRenderer.send(Constants.SIYUAN_OPENWINDOW, {
@ -56,12 +58,7 @@ export const openNewWindowById = (id: string) => {
} else {
json.children.action = Constants.CB_GET_ALL;
json.children.scrollAttr = {
startId: id,
endId: id,
scrollTop: 0,
focusId: id,
focusStart: 0,
focusEnd: 0
zoomInId: id,
};
/// #if !BROWSER
ipcRenderer.send(Constants.SIYUAN_OPENWINDOW, {