diff --git a/app/src/layout/util.ts b/app/src/layout/util.ts index ae652112e..6100c80c4 100644 --- a/app/src/layout/util.ts +++ b/app/src/layout/util.ts @@ -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, diff --git a/app/src/protyle/index.ts b/app/src/protyle/index.ts index 74f63193b..ac781cabd 100644 --- a/app/src/protyle/index.ts +++ b/app/src/protyle/index.ts @@ -218,6 +218,7 @@ export class Protyle { } } if (scrollObj) { + scrollObj.rootId = response.data.rootID; getDocByScroll({ protyle: this.protyle, scrollAttr: scrollObj, diff --git a/app/src/protyle/scroll/saveScroll.ts b/app/src/protyle/scroll/saveScroll.ts index 34a1c8c88..df1267990 100644 --- a/app/src/protyle/scroll/saveScroll.ts +++ b/app/src/protyle/scroll/saveScroll.ts @@ -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 => { diff --git a/app/src/protyle/util/onGet.ts b/app/src/protyle/util/onGet.ts index 98f412832..df9fba4e6 100644 --- a/app/src/protyle/util/onGet.ts +++ b/app/src/protyle/util/onGet.ts @@ -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 diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts index 799b735c8..e4c620b08 100644 --- a/app/src/types/index.d.ts +++ b/app/src/types/index.d.ts @@ -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, diff --git a/app/src/util/backForward.ts b/app/src/util/backForward.ts index 254ff54ca..282027ad5 100644 --- a/app/src/util/backForward.ts +++ b/app/src/util/backForward.ts @@ -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; diff --git a/app/src/window/openNewWindow.ts b/app/src/window/openNewWindow.ts index fe8071260..d00b9fc62 100644 --- a/app/src/window/openNewWindow.ts +++ b/app/src/window/openNewWindow.ts @@ -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, {