diff --git a/app/src/editor/util.ts b/app/src/editor/util.ts index 2856ed79a..7756abbae 100644 --- a/app/src/editor/util.ts +++ b/app/src/editor/util.ts @@ -338,7 +338,11 @@ const switchEditor = (editor: Editor, options: IOpenFileOptions, allModels: IMod highlightById(editor.editor.protyle, options.id, true); } else if (options.action.includes(Constants.CB_GET_FOCUS)) { if (nodeElement) { - focusBlock(nodeElement); + const newRange = focusBlock(nodeElement); + if (newRange) { + // 需要更新 range,否则文档大纲点击导致切换页签时因为 resize 中 `保持光标位置不变` 会导致光标跳动 + editor.editor.protyle.toolbar.range = newRange + } scrollCenter(editor.editor.protyle, nodeElement, true); } else if (editor.editor.protyle.block.rootID === options.id) { // 由于 https://github.com/siyuan-note/siyuan/issues/5420,移除定位 @@ -455,7 +459,7 @@ export const updatePanelByEditor = (options: { if (options.resize) { resize(options.protyle); } - if (focus) { + if (options.focus) { if (options.protyle.toolbar.range) { focusByRange(options.protyle.toolbar.range); countSelectWord(options.protyle.toolbar.range, options.protyle.block.rootID); diff --git a/app/src/layout/Wnd.ts b/app/src/layout/Wnd.ts index 5b54237e5..19ea3e86a 100644 --- a/app/src/layout/Wnd.ts +++ b/app/src/layout/Wnd.ts @@ -409,6 +409,10 @@ export class Wnd { public switchTab(target: HTMLElement, pushBack = false, update = true) { setPanelFocus(this.headersElement.parentElement.parentElement); + // https://github.com/siyuan-note/siyuan/issues/8233 + if (target.classList.contains("item--focus")) { + return; + } let currentTab: Tab; this.children.forEach((item) => { if (target === item.headElement) { diff --git a/app/src/mobile/util/MobileBackFoward.ts b/app/src/mobile/util/MobileBackFoward.ts index 4c3e5835b..bc7a36172 100644 --- a/app/src/mobile/util/MobileBackFoward.ts +++ b/app/src/mobile/util/MobileBackFoward.ts @@ -46,16 +46,21 @@ const focusStack = (backStack: IBackStack) => { }); } - if (backStack.isZoom) { - fetchPost("/api/block/checkBlockExist", {id: backStack.id}, existResponse => { - if (existResponse.data) { - zoomOut(protyle, backStack.id, undefined, false, () => { - protyle.contentElement.scrollTop = backStack.scrollTop; - }); - } - }); + if (backStack.zoomId) { + if (backStack.zoomId !== protyle.block.id) { + fetchPost("/api/block/checkBlockExist", {id: backStack.id}, existResponse => { + if (existResponse.data) { + zoomOut(protyle, backStack.id, undefined, false, () => { + protyle.contentElement.scrollTop = backStack.scrollTop; + }); + } + }); + } else { + protyle.contentElement.scrollTop = backStack.scrollTop; + } return; } + fetchPost("/api/filetree/getDoc", { id: backStack.id, startID: startEndId[0], @@ -95,7 +100,7 @@ export const pushBack = () => { endId: protyle.wysiwyg.element.firstElementChild.getAttribute("data-node-id") + Constants.ZWSP + protyle.wysiwyg.element.lastElementChild.getAttribute("data-node-id"), scrollTop: protyle.contentElement.scrollTop, callback: protyle.block.action, - isZoom: protyle.block.showAll + zoomId: protyle.block.showAll ? protyle.block.id : undefined }); }; @@ -164,7 +169,7 @@ export const goBack = () => { endId: protyle.wysiwyg.element.firstElementChild.getAttribute("data-node-id") + Constants.ZWSP + protyle.wysiwyg.element.lastElementChild.getAttribute("data-node-id"), scrollTop: protyle.contentElement.scrollTop, callback: protyle.block.action, - isZoom: protyle.block.showAll + zoomId: protyle.block.showAll ? protyle.block.id : undefined }); } const item = window.siyuan.backStack.pop(); diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts index 64df8cfd2..b019dc669 100644 --- a/app/src/types/index.d.ts +++ b/app/src/types/index.d.ts @@ -162,7 +162,7 @@ interface IBackStack { callback?: string[], position?: { start: number, end: number } protyle?: IProtyle, - isZoom?: boolean + zoomId?: string } interface IEmoji { diff --git a/app/src/util/backForward.ts b/app/src/util/backForward.ts index 5975dceb0..802b0dd1b 100644 --- a/app/src/util/backForward.ts +++ b/app/src/util/backForward.ts @@ -53,8 +53,8 @@ const focusStack = async (stack: IBackStack) => { const editor = new Editor({ tab, scrollAttr, - blockId: stack.isZoom ? stack.id : stack.protyle.block.rootID, - action: stack.isZoom ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL] : [Constants.CB_GET_FOCUS] + blockId: stack.zoomId || stack.protyle.block.rootID, + action: stack.zoomId ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL] : [Constants.CB_GET_FOCUS] }); tab.addModel(editor); } @@ -110,6 +110,8 @@ const focusStack = async (stack: IBackStack) => { if (stack.protyle.title.editElement.getBoundingClientRect().height === 0) { // 切换 tab stack.protyle.model.parent.parent.switchTab(stack.protyle.model.parent.headElement); + // 需要更新 range,否则 resize 中 `保持光标位置不变` 会导致光标跳动 + stack.protyle.toolbar.range = undefined; } focusByOffset(stack.protyle.title.editElement, stack.position.start, stack.position.end); return true; @@ -125,34 +127,15 @@ const focusStack = async (stack: IBackStack) => { // 切换 tab stack.protyle.model.parent.parent.switchTab(stack.protyle.model.parent.headElement); } - if (stack.isZoom) { - zoomOut(stack.protyle, stack.id, undefined, false); - return true; - } - if (blockElement && !stack.protyle.block.showAll) { - focusByOffset(getContenteditableElement(blockElement), stack.position.start, stack.position.end); - scrollCenter(stack.protyle, blockElement, true); - return true; - } - // 缩放不一致 - fetchPost("/api/filetree/getDoc", { - id: stack.id, - mode: stack.isZoom ? 0 : 3, - size: stack.isZoom ? Constants.SIZE_GET_MAX : window.siyuan.config.editor.dynamicLoadBlocks, - }, getResponse => { - onGet(getResponse, stack.protyle, [Constants.CB_GET_HTML]); - Array.from(stack.protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${stack.id}"]`)).find(item => { - if (!hasClosestByAttribute(item, "data-type", "NodeBlockQueryEmbed")) { - blockElement = item; - return true; - } - }); - focusByOffset(getContenteditableElement(blockElement), stack.position.start, stack.position.end); - setTimeout(() => { - // 图片、视频等加载完成后再定位 + if (stack.zoomId && stack.zoomId !== stack.protyle.block.id) { + zoomOut(stack.protyle, stack.zoomId, undefined, false, () => { + focusByOffset(getContenteditableElement(blockElement), stack.position.start, stack.position.end); scrollCenter(stack.protyle, blockElement, true); - }, Constants.TIMEOUT_LOAD); - }); + }); + return true; + } + focusByOffset(getContenteditableElement(blockElement), stack.position.start, stack.position.end); + scrollCenter(stack.protyle, blockElement, true); return true; } if (stack.protyle.element.parentElement) { @@ -165,11 +148,11 @@ const focusStack = async (stack: IBackStack) => { return false; } fetchPost("/api/filetree/getDoc", { - id: stack.id, - mode: stack.isZoom ? 0 : 3, - size: stack.isZoom ? Constants.SIZE_GET_MAX : window.siyuan.config.editor.dynamicLoadBlocks, + id: stack.zoomId ? stack.zoomId : stack.id, + mode: stack.zoomId ? 0 : 3, + size: stack.zoomId ? Constants.SIZE_GET_MAX : window.siyuan.config.editor.dynamicLoadBlocks, }, getResponse => { - onGet(getResponse, stack.protyle, stack.isZoom ? [Constants.CB_GET_HTML, Constants.CB_GET_ALL] : [Constants.CB_GET_HTML]); + onGet(getResponse, stack.protyle, stack.zoomId ? [Constants.CB_GET_HTML, Constants.CB_GET_ALL] : [Constants.CB_GET_HTML]); Array.from(stack.protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${stack.id}"]`)).find(item => { if (!hasClosestByAttribute(item, "data-type", "NodeBlockQueryEmbed")) { blockElement = item; @@ -262,8 +245,9 @@ export const pushBack = (protyle: IProtyle, range?: Range, blockElement?: Elemen const position = getSelectionOffset(editElement, undefined, range); const id = blockElement.getAttribute("data-node-id") || protyle.block.rootID; const lastStack = window.siyuan.backStack[window.siyuan.backStack.length - 1]; - const isZoom = protyle.block.showAll; - if (lastStack && lastStack.id === id && lastStack.isZoom === isZoom) { + if (lastStack && lastStack.id === id && ( + (protyle.block.showAll && lastStack.zoomId === protyle.block.id) || (!lastStack.zoomId && !protyle.block.showAll) + )) { lastStack.position = position; } else { if (forwardStack.length > 0) { @@ -277,7 +261,7 @@ export const pushBack = (protyle: IProtyle, range?: Range, blockElement?: Elemen position, id, protyle, - isZoom + zoomId: protyle.block.showAll ? protyle.block.id : undefined, }); if (window.siyuan.backStack.length > Constants.SIZE_UNDO) { window.siyuan.backStack.shift();