🐛 back to the zoom in block

This commit is contained in:
Vanessa 2023-05-11 20:00:21 +08:00
parent 9383c943b3
commit a974bbdece
5 changed files with 46 additions and 49 deletions

View file

@ -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);

View file

@ -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) {

View file

@ -46,7 +46,8 @@ const focusStack = (backStack: IBackStack) => {
});
}
if (backStack.isZoom) {
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, () => {
@ -54,8 +55,12 @@ const focusStack = (backStack: IBackStack) => {
});
}
});
} 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();

View file

@ -162,7 +162,7 @@ interface IBackStack {
callback?: string[],
position?: { start: number, end: number }
protyle?: IProtyle,
isZoom?: boolean
zoomId?: string
}
interface IEmoji {

View file

@ -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) {
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);
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;
}
});
return true;
}
focusByOffset(getContenteditableElement(blockElement), stack.position.start, stack.position.end);
setTimeout(() => {
// 图片、视频等加载完成后再定位
scrollCenter(stack.protyle, blockElement, true);
}, Constants.TIMEOUT_LOAD);
});
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();