Vanessa 2023-05-13 23:04:00 +08:00
parent 65dc60a9fe
commit 0daed2d12a
12 changed files with 81 additions and 116 deletions

View file

@ -1044,7 +1044,7 @@ const editKeydown = (event: KeyboardEvent) => {
return false; return false;
} }
if (matchHotKey(window.siyuan.config.keymap.editor.general.refresh.custom, event)) { if (matchHotKey(window.siyuan.config.keymap.editor.general.refresh.custom, event)) {
reloadProtyle(protyle); reloadProtyle(protyle, true);
event.preventDefault(); event.preventDefault();
return true; return true;
} }

View file

@ -334,7 +334,7 @@ export const editor = {
} }
window.siyuan.config.editor = editorData; window.siyuan.config.editor = editorData;
getAllModels().editor.forEach((item) => { getAllModels().editor.forEach((item) => {
reloadProtyle(item.editor.protyle); reloadProtyle(item.editor.protyle, false);
setPadding(item.editor.protyle); setPadding(item.editor.protyle);
if (window.siyuan.config.editor.fullWidth) { if (window.siyuan.config.editor.fullWidth) {
item.editor.protyle.contentElement.setAttribute("data-fullwidth", "true"); item.editor.protyle.contentElement.setAttribute("data-fullwidth", "true");

View file

@ -36,7 +36,7 @@ export const reloadSync = (data: { upsertRootIDs: string[], removeRootIDs: strin
if (data.removeRootIDs.includes(window.siyuan.mobile.popEditor.protyle.block.rootID)) { if (data.removeRootIDs.includes(window.siyuan.mobile.popEditor.protyle.block.rootID)) {
hideElements(["dialog"]); hideElements(["dialog"]);
} else { } else {
reloadProtyle(window.siyuan.mobile.popEditor.protyle); reloadProtyle(window.siyuan.mobile.popEditor.protyle, false);
window.siyuan.mobile.popEditor.protyle.breadcrumb.render(window.siyuan.mobile.popEditor.protyle, true); window.siyuan.mobile.popEditor.protyle.breadcrumb.render(window.siyuan.mobile.popEditor.protyle, true);
} }
} }
@ -44,7 +44,7 @@ export const reloadSync = (data: { upsertRootIDs: string[], removeRootIDs: strin
if (data.removeRootIDs.includes(window.siyuan.mobile.editor.protyle.block.rootID)) { if (data.removeRootIDs.includes(window.siyuan.mobile.editor.protyle.block.rootID)) {
setEmpty(); setEmpty();
} else { } else {
reloadProtyle(window.siyuan.mobile.editor.protyle); reloadProtyle(window.siyuan.mobile.editor.protyle, false);
fetchPost("/api/block/getDocInfo", { fetchPost("/api/block/getDocInfo", {
id: window.siyuan.mobile.editor.protyle.block.rootID id: window.siyuan.mobile.editor.protyle.block.rootID
}, (response) => { }, (response) => {
@ -59,7 +59,7 @@ export const reloadSync = (data: { upsertRootIDs: string[], removeRootIDs: strin
const allModels = getAllModels(); const allModels = getAllModels();
allModels.editor.forEach(item => { allModels.editor.forEach(item => {
if (data.upsertRootIDs.includes(item.editor.protyle.block.rootID)) { if (data.upsertRootIDs.includes(item.editor.protyle.block.rootID)) {
reloadProtyle(item.editor.protyle); reloadProtyle(item.editor.protyle, false);
updateTitle(item.editor.protyle.block.rootID, item.parent); updateTitle(item.editor.protyle.block.rootID, item.parent);
} else if (data.removeRootIDs.includes(item.editor.protyle.block.rootID)) { } else if (data.removeRootIDs.includes(item.editor.protyle.block.rootID)) {
item.parent.parent.removeTab(item.parent.id, false, false, false); item.parent.parent.removeTab(item.parent.id, false, false, false);

View file

@ -59,7 +59,7 @@ const replace = (element: Element, config: ISearchOption, isAll: boolean) => {
if (ids.length > 1) { if (ids.length > 1) {
return; return;
} }
reloadProtyle(window.siyuan.mobile.editor.protyle); reloadProtyle(window.siyuan.mobile.editor.protyle, false);
if (currentLiElement.nextElementSibling) { if (currentLiElement.nextElementSibling) {
currentLiElement.nextElementSibling.classList.add("b3-list-item--focus"); currentLiElement.nextElementSibling.classList.add("b3-list-item--focus");

View file

@ -40,7 +40,7 @@ const setEditor = (modelMainElement: Element) => {
window.siyuan.config.editor.historyRetentionDays = parseInt((modelMainElement.querySelector("#historyRetentionDays") as HTMLInputElement).value); window.siyuan.config.editor.historyRetentionDays = parseInt((modelMainElement.querySelector("#historyRetentionDays") as HTMLInputElement).value);
fetchPost("/api/setting/setEditor", window.siyuan.config.editor, response => { fetchPost("/api/setting/setEditor", window.siyuan.config.editor, response => {
window.siyuan.config.editor = response.data; window.siyuan.config.editor = response.data;
reloadProtyle(window.siyuan.mobile.editor.protyle); reloadProtyle(window.siyuan.mobile.editor.protyle, false);
setInlineStyle(); setInlineStyle();
}); });
}; };

View file

@ -18,11 +18,11 @@ export const netImg2LocalAssets = (protyle: IProtyle) => {
id: protyle.block.rootID id: protyle.block.rootID
}, () => { }, () => {
/// #if MOBILE /// #if MOBILE
reloadProtyle(protyle); reloadProtyle(protyle, false);
/// #else /// #else
getAllModels().editor.forEach(item => { getAllModels().editor.forEach(item => {
if (item.editor.protyle.block.rootID === protyle.block.rootID) { if (item.editor.protyle.block.rootID === protyle.block.rootID) {
reloadProtyle(item.editor.protyle) reloadProtyle(item.editor.protyle, item.editor.protyle.element.isSameNode(protyle.element));
} }
}); });
/// #endif /// #endif

View file

@ -245,11 +245,11 @@ export class Breadcrumb {
id: protyle.block.rootID id: protyle.block.rootID
}, () => { }, () => {
/// #if MOBILE /// #if MOBILE
reloadProtyle(protyle); reloadProtyle(protyle, false);
/// #else /// #else
getAllModels().editor.forEach(item => { getAllModels().editor.forEach(item => {
if (item.editor.protyle.block.rootID === protyle.block.rootID) { if (item.editor.protyle.block.rootID === protyle.block.rootID) {
reloadProtyle(item.editor.protyle); reloadProtyle(item.editor.protyle, item.editor.protyle.element.isSameNode(protyle.element));
} }
}); });
/// #endif /// #endif
@ -304,7 +304,7 @@ export class Breadcrumb {
accelerator: window.siyuan.config.keymap.editor.general.refresh.custom, accelerator: window.siyuan.config.keymap.editor.general.refresh.custom,
label: window.siyuan.languages.refresh, label: window.siyuan.languages.refresh,
click: () => { click: () => {
reloadProtyle(protyle); reloadProtyle(protyle, !isMobile());
} }
}).element); }).element);
window.siyuan.menus.menu.append(new MenuItem({ window.siyuan.menus.menu.append(new MenuItem({

View file

@ -27,6 +27,7 @@ import {reloadProtyle} from "./util/reload";
import {renderBacklink} from "./wysiwyg/renderBacklink"; import {renderBacklink} from "./wysiwyg/renderBacklink";
import {setEmpty} from "../mobile/util/setEmpty"; import {setEmpty} from "../mobile/util/setEmpty";
import {resize} from "./util/resize"; import {resize} from "./util/resize";
import {getDocByScroll} from "./scroll/saveScroll";
export class Protyle { export class Protyle {
@ -111,7 +112,7 @@ export class Protyle {
onGet(getResponse, this.protyle); onGet(getResponse, this.protyle);
}); });
} else { } else {
reloadProtyle(this.protyle); reloadProtyle(this.protyle, false);
} }
/// #if !MOBILE /// #if !MOBILE
if (data.cmd === "heading2doc") { if (data.cmd === "heading2doc") {
@ -206,14 +207,28 @@ export class Protyle {
scrollObj = undefined; scrollObj = undefined;
} }
if (scrollObj) { if (scrollObj) {
this.getDocByScrollData(scrollObj, mergedOptions); getDocByScroll({
protyle: this.protyle,
scrollAttr: scrollObj,
mergedOptions,
cb: () => {
this.afterOnGet(mergedOptions);
}
});
} else { } else {
this.getDoc(mergedOptions); this.getDoc(mergedOptions);
} }
} }
}); });
} else { } else {
this.getDocByScrollData(options.scrollAttr, mergedOptions); getDocByScroll({
protyle: this.protyle,
scrollAttr: options.scrollAttr,
mergedOptions,
cb: () => {
this.afterOnGet(mergedOptions);
}
});
} }
} else { } else {
this.getDoc(mergedOptions); this.getDoc(mergedOptions);
@ -236,27 +251,6 @@ export class Protyle {
}); });
} }
private getDocByScrollData(scrollAttr: IScrollAttr, mergedOptions: IOptions) {
if (scrollAttr.zoomInId) {
fetchPost("/api/filetree/getDoc", {
id: scrollAttr.zoomInId,
size: Constants.SIZE_GET_MAX,
}, response => {
onGet(response, this.protyle, mergedOptions.action, scrollAttr);
this.afterOnGet(mergedOptions);
})
return;
}
fetchPost("/api/filetree/getDoc", {
id: scrollAttr.startId,
startID: scrollAttr.startId,
endID: scrollAttr.endId,
}, response => {
onGet(response, this.protyle, mergedOptions.action, scrollAttr);
this.afterOnGet(mergedOptions);
});
}
private afterOnGet(mergedOptions: IOptions) { private afterOnGet(mergedOptions: IOptions) {
if (this.protyle.model) { if (this.protyle.model) {
/// #if !MOBILE /// #if !MOBILE

View file

@ -1,14 +1,8 @@
import {hasClosestBlock} from "../util/hasClosest"; import {hasClosestBlock} from "../util/hasClosest";
import {focusByOffset, getSelectionOffset} from "../util/selection"; import {focusByOffset, getSelectionOffset} from "../util/selection";
import {fetchPost} from "../../util/fetch"; import {fetchPost} from "../../util/fetch";
import {zoomOut} from "../../menus/protyle"; import {onGet} from "../util/onGet";
import {preventScroll} from "./preventScroll"; import {Constants} from "../../constants";
import {pushBack} from "../../util/backForward";
import {processRender} from "../util/processCode";
import {highlightRender} from "../markdown/highlightRender";
import {blockRender} from "../markdown/blockRender";
import {disabledForeverProtyle, disabledProtyle, enableProtyle} from "../util/onGet";
import {showMessage} from "../../dialog/message";
export const saveScroll = (protyle: IProtyle, getObject = false) => { export const saveScroll = (protyle: IProtyle, getObject = false) => {
if (!protyle.wysiwyg.element.firstElementChild || window.siyuan.config.readonly) { if (!protyle.wysiwyg.element.firstElementChild || window.siyuan.config.readonly) {
@ -49,66 +43,45 @@ export const saveScroll = (protyle: IProtyle, getObject = false) => {
}); });
}; };
export const restoreScroll = (protyle: IProtyle, scrollAttr: IScrollAttr) => { export const getDocByScroll = (options: {
preventScroll(protyle); protyle: IProtyle,
if (protyle.wysiwyg.element.firstElementChild.getAttribute("data-node-id") === scrollAttr.startId && scrollAttr: IScrollAttr,
protyle.wysiwyg.element.lastElementChild.getAttribute("data-node-id") === scrollAttr.endId) { mergedOptions?: IOptions,
// 需等动画效果完毕,才能获得最大高度。否则尾部定位无法滚动到底部 cb?: () => void
setTimeout(() => { focus?: boolean
protyle.contentElement.scrollTop = scrollAttr.scrollTop; }) => {
}, 256); let actions: string[] = []
if (scrollAttr.focusId) { if (options.mergedOptions) {
const range = focusByOffset(protyle.wysiwyg.element.querySelector(`[data-node-id="${scrollAttr.focusId}"]`), scrollAttr.focusStart, scrollAttr.focusEnd); actions = options.mergedOptions.action
/// #if !MOBILE } else {
pushBack(protyle, range || undefined); if (options.focus) {
/// #endif actions = [Constants.CB_GET_UNUNDO, Constants.CB_GET_FOCUS]
} else {
actions = [Constants.CB_GET_UNUNDO];
} }
} else if (scrollAttr.zoomInId && protyle.block.id !== scrollAttr.zoomInId) { if (options.scrollAttr.zoomInId) {
fetchPost("/api/block/checkBlockExist", {id: scrollAttr.zoomInId}, existResponse => { actions.push(Constants.CB_GET_ALL);
if (existResponse.data) {
zoomOut(protyle, scrollAttr.zoomInId, undefined, true, () => {
protyle.contentElement.scrollTop = scrollAttr.scrollTop;
if (scrollAttr.focusId) {
focusByOffset(protyle.wysiwyg.element.querySelector(`[data-node-id="${scrollAttr.focusId}"]`), scrollAttr.focusStart, scrollAttr.focusEnd);
} }
});
} }
}); if (options.scrollAttr.zoomInId) {
} else if (!protyle.scroll.element.classList.contains("fn__none")) {
fetchPost("/api/filetree/getDoc", { fetchPost("/api/filetree/getDoc", {
id: protyle.block.id, id: options.scrollAttr.zoomInId,
startID: scrollAttr.startId, size: Constants.SIZE_GET_MAX,
endID: scrollAttr.endId, }, response => {
}, getResponse => { onGet(response, options.protyle, actions, options.scrollAttr);
protyle.block.showAll = false; if (options.cb) {
protyle.wysiwyg.element.innerHTML = getResponse.data.content; options.cb()
processRender(protyle.wysiwyg.element);
highlightRender(protyle.wysiwyg.element);
blockRender(protyle, protyle.wysiwyg.element);
if (getResponse.data.isSyncing) {
disabledForeverProtyle(protyle);
} else {
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);
/// #if !MOBILE
pushBack(protyle, range || undefined);
/// #endif
}
// 使用动态滚动条定位到最后一个块,重启后无法触发滚动事件,需要再次更新 index
protyle.scroll.updateIndex(protyle, scrollAttr.startId);
// https://github.com/siyuan-note/siyuan/issues/8224
if (protyle.wysiwyg.element.clientHeight - parseInt(protyle.wysiwyg.element.style.paddingBottom) < protyle.contentElement.clientHeight) {
showMessage(window.siyuan.languages.scrollGetMore);
} }
}); });
} else if (scrollAttr.scrollTop) {
protyle.contentElement.scrollTop = scrollAttr.scrollTop;
} }
fetchPost("/api/filetree/getDoc", {
id: options.scrollAttr.startId,
startID: options.scrollAttr.startId,
endID: options.scrollAttr.endId,
}, response => {
onGet(response, options.protyle, actions, options.scrollAttr);
if (options.cb) {
options.cb()
}
});
}; };

View file

@ -1,12 +1,11 @@
import {addLoading} from "../ui/initUI"; import {addLoading} from "../ui/initUI";
import {fetchPost} from "../../util/fetch"; import {fetchPost} from "../../util/fetch";
import {Constants} from "../../constants"; import {getDocByScroll, saveScroll} from "../scroll/saveScroll";
import {onGet} from "./onGet";
import {saveScroll} from "../scroll/saveScroll";
import {renderBacklink} from "../wysiwyg/renderBacklink"; import {renderBacklink} from "../wysiwyg/renderBacklink";
import {hasClosestByClassName} from "./hasClosest"; import {hasClosestByClassName} from "./hasClosest";
import {preventScroll} from "../scroll/preventScroll";
export const reloadProtyle = (protyle: IProtyle) => { export const reloadProtyle = (protyle: IProtyle, focus: boolean) => {
if (window.siyuan.config.editor.displayBookmarkIcon) { if (window.siyuan.config.editor.displayBookmarkIcon) {
protyle.wysiwyg.element.classList.add("protyle-wysiwyg--attr"); protyle.wysiwyg.element.classList.add("protyle-wysiwyg--attr");
} else { } else {
@ -38,12 +37,11 @@ export const reloadProtyle = (protyle: IProtyle) => {
}); });
} }
} else { } else {
fetchPost("/api/filetree/getDoc", { preventScroll(protyle);
id: protyle.block.showAll ? protyle.block.id : protyle.block.rootID, getDocByScroll({
mode: 0, protyle,
size: protyle.block.showAll ? Constants.SIZE_GET_MAX : window.siyuan.config.editor.dynamicLoadBlocks, focus,
}, getResponse => { scrollAttr: saveScroll(protyle, true)
onGet(getResponse, protyle, protyle.block.showAll ? [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS] : [Constants.CB_GET_FOCUS], saveScroll(protyle, true), true);
}); });
} }
}; };

View file

@ -647,7 +647,7 @@ export const onTransaction = (protyle: IProtyle, operation: IOperation, focus: b
return; return;
} }
if (operation.action === "append") { if (operation.action === "append") {
reloadProtyle(protyle); reloadProtyle(protyle, false);
} }
}; };

View file

@ -961,7 +961,7 @@ const replace = (element: Element, config: ISearchOption, edit: Protyle, isAll:
} }
getAllModels().editor.forEach(item => { getAllModels().editor.forEach(item => {
if (rootIds[0] === item.editor.protyle.block.rootID) { if (rootIds[0] === item.editor.protyle.block.rootID) {
reloadProtyle(item.editor.protyle); reloadProtyle(item.editor.protyle, false);
} }
}); });
if (currentList.nextElementSibling) { if (currentList.nextElementSibling) {