From 194a18fc95197e9899abcd76f7d22688305ca673 Mon Sep 17 00:00:00 2001 From: Vanessa Date: Wed, 31 Aug 2022 12:57:38 +0800 Subject: [PATCH] :bug: fix https://github.com/siyuan-note/siyuan/issues/5767 --- app/src/config/editor.ts | 3 +- app/src/mobile/settings/appearance.ts | 3 +- app/src/protyle/index.ts | 54 +++++++++---------------- app/src/protyle/scroll/saveScroll.ts | 2 + app/src/protyle/util/reload.ts | 22 ++++++++++ app/src/protyle/wysiwyg/commonHotkey.ts | 15 ++----- app/src/search/spread.ts | 3 +- 7 files changed, 51 insertions(+), 51 deletions(-) create mode 100644 app/src/protyle/util/reload.ts diff --git a/app/src/config/editor.ts b/app/src/config/editor.ts index 736a7b22c..faa4b85e0 100644 --- a/app/src/config/editor.ts +++ b/app/src/config/editor.ts @@ -3,6 +3,7 @@ import {setInlineStyle} from "../util/assets"; import {fetchPost} from "../util/fetch"; import {confirmDialog} from "../dialog/confirmDialog"; import {setPadding} from "../protyle/ui/initUI"; +import {reloadProtyle} from "../protyle/util/reload"; export const editor = { element: undefined as Element, @@ -201,7 +202,7 @@ export const editor = { onSetEditor: (editor: IEditor) => { window.siyuan.config.editor = editor; getAllModels().editor.forEach((item) => { - item.editor.reload(); + reloadProtyle(item.editor.protyle) setPadding(item.editor.protyle); if (window.siyuan.config.editor.fullWidth) { item.editor.protyle.contentElement.setAttribute("data-fullwidth", "true"); diff --git a/app/src/mobile/settings/appearance.ts b/app/src/mobile/settings/appearance.ts index 540049b1c..05ff1e0e6 100644 --- a/app/src/mobile/settings/appearance.ts +++ b/app/src/mobile/settings/appearance.ts @@ -2,6 +2,7 @@ import {closePanel} from "../util/closePanel"; import {fetchPost} from "../../util/fetch"; import {setInlineStyle} from "../../util/assets"; import {genOptions} from "../../util/genOptions"; +import {reloadProtyle} from "../../protyle/util/reload"; export const initAppearance = (modelElement: HTMLElement, modelMainElement: HTMLElement) => { closePanel(); @@ -69,7 +70,7 @@ export const initAppearance = (modelElement: HTMLElement, modelMainElement: HTML emoji: window.siyuan.config.editor.emoji }, (response) => { window.siyuan.config.editor = response.data; - window.siyuan.mobileEditor.reload(); + reloadProtyle(window.siyuan.mobileEditor.protyle); setInlineStyle(); }); }); diff --git a/app/src/protyle/index.ts b/app/src/protyle/index.ts index 20d556065..6847ad442 100644 --- a/app/src/protyle/index.ts +++ b/app/src/protyle/index.ts @@ -2,7 +2,7 @@ import {Constants} from "../constants"; import {Hint} from "./hint"; import {setLute} from "./markdown/setLute"; import {Preview} from "./preview"; -import {addLoading, initUI, setPadding} from "./ui/initUI"; +import {initUI, setPadding} from "./ui/initUI"; import {Undo} from "./undo"; import {Upload} from "./upload"; import {Options} from "./util/Options"; @@ -24,6 +24,7 @@ import {setPanelFocus} from "../layout/util"; import {Background} from "./header/Background"; import {getDisplayName} from "../util/pathName"; import {onGet} from "./util/onGet"; +import {reloadProtyle} from "./util/reload"; class Protyle { @@ -93,24 +94,22 @@ class Protyle { case "heading2doc": case "li2doc": if (this.protyle.block.rootID === data.data.srcRootBlockID) { - const scrollTop = this.protyle.contentElement.scrollTop; - fetchPost("/api/filetree/getDoc", { - id: this.protyle.block.id, - size: Constants.SIZE_GET, - }, getResponse => { - onGet(getResponse, this.protyle); - /// #if !MOBILE - if (data.cmd === "heading2doc") { - // 文档标题互转后,需更新大纲 - updatePanelByEditor(this.protyle, false, false, true); - } - /// #endif - // 文档标题互转后,编辑区会跳转到开头 https://github.com/siyuan-note/siyuan/issues/2939 - setTimeout(() => { - this.protyle.contentElement.scrollTop = scrollTop; - this.protyle.scroll.lastScrollTop = scrollTop - 1; - }, Constants.TIMEOUT_BLOCKLOAD); - }); + if (this.protyle.block.showAll && data.cmd === "heading2doc") { + fetchPost("/api/filetree/getDoc", { + id: this.protyle.block.rootID, + size: Constants.SIZE_GET, + }, getResponse => { + onGet(getResponse, this.protyle); + }) + } else { + reloadProtyle(this.protyle); + } + /// #if !MOBILE + if (data.cmd === "heading2doc") { + // 文档标题互转后,需更新大纲 + updatePanelByEditor(this.protyle, false, false, true); + } + /// #endif } break; case "rename": @@ -206,23 +205,6 @@ class Protyle { } } - public reload() { - if (window.siyuan.config.editor.displayBookmarkIcon) { - this.protyle.wysiwyg.element.classList.add("protyle-wysiwyg--attr"); - } else { - this.protyle.wysiwyg.element.classList.remove("protyle-wysiwyg--attr"); - } - this.protyle.lute.SetProtyleMarkNetImg(window.siyuan.config.editor.displayNetImgMark); - addLoading(this.protyle); - fetchPost("/api/filetree/getDoc", { - id: this.protyle.block.id, - mode: 0, - size: Constants.SIZE_GET, - }, getResponse => { - onGet(getResponse, this.protyle); - }); - } - private init() { this.protyle.lute = setLute({ emojiSite: this.protyle.options.hint.emojiPath, diff --git a/app/src/protyle/scroll/saveScroll.ts b/app/src/protyle/scroll/saveScroll.ts index 42ff6cf5a..9a3511193 100644 --- a/app/src/protyle/scroll/saveScroll.ts +++ b/app/src/protyle/scroll/saveScroll.ts @@ -97,5 +97,7 @@ export const restoreScroll = (protyle: IProtyle, scrollAttr: IScrollAttr) => { /// #endif } }); + } else if (scrollAttr.scrollTop) { + protyle.contentElement.scrollTop = scrollAttr.scrollTop; } }; diff --git a/app/src/protyle/util/reload.ts b/app/src/protyle/util/reload.ts new file mode 100644 index 000000000..c7202a432 --- /dev/null +++ b/app/src/protyle/util/reload.ts @@ -0,0 +1,22 @@ +import {addLoading} from "../ui/initUI"; +import {fetchPost} from "../../util/fetch"; +import {Constants} from "../../constants"; +import {onGet} from "./onGet"; +import {saveScroll} from "../scroll/saveScroll"; + +export const reloadProtyle = (protyle:IProtyle) => { + if (window.siyuan.config.editor.displayBookmarkIcon) { + protyle.wysiwyg.element.classList.add("protyle-wysiwyg--attr"); + } else { + protyle.wysiwyg.element.classList.remove("protyle-wysiwyg--attr"); + } + protyle.lute.SetProtyleMarkNetImg(window.siyuan.config.editor.displayNetImgMark); + addLoading(protyle); + fetchPost("/api/filetree/getDoc", { + id: protyle.block.showAll ? protyle.block.id : protyle.block.rootID, + mode: 0, + size: protyle.block.showAll ? Constants.SIZE_GET_MAX : Constants.SIZE_GET, + }, getResponse => { + onGet(getResponse, protyle, protyle.block.showAll ? [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS] : [Constants.CB_GET_FOCUS], saveScroll(protyle, true), true); + }); +} diff --git a/app/src/protyle/wysiwyg/commonHotkey.ts b/app/src/protyle/wysiwyg/commonHotkey.ts index 39cd07b10..80293c256 100644 --- a/app/src/protyle/wysiwyg/commonHotkey.ts +++ b/app/src/protyle/wysiwyg/commonHotkey.ts @@ -3,11 +3,9 @@ import {fetchPost} from "../../util/fetch"; import {writeText} from "../util/compatibility"; import {focusByOffset, getSelectionOffset} from "../util/selection"; import {fullscreen, netImg2LocalAssets} from "../breadcrumb/action"; -import {addLoading, setPadding} from "../ui/initUI"; -import {Constants} from "../../constants"; -import {onGet} from "../util/onGet"; +import {setPadding} from "../ui/initUI"; import {openBacklink, openGraph, openOutline} from "../../layout/dock/util"; -import {saveScroll} from "../scroll/saveScroll"; +import {reloadProtyle} from "../util/reload"; export const commonHotkey = (protyle: IProtyle, event: KeyboardEvent) => { const target = event.target as HTMLElement; @@ -23,14 +21,7 @@ export const commonHotkey = (protyle: IProtyle, event: KeyboardEvent) => { } if (matchHotKey(window.siyuan.config.keymap.editor.general.refresh.custom, event)) { - addLoading(protyle); - fetchPost("/api/filetree/getDoc", { - id: protyle.block.showAll ? protyle.block.id : protyle.block.rootID, - mode: 0, - size: protyle.block.showAll ? Constants.SIZE_GET_MAX : Constants.SIZE_GET, - }, getResponse => { - onGet(getResponse, protyle, protyle.block.showAll ? [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS] : [Constants.CB_GET_FOCUS], saveScroll(protyle, true), true); - }); + reloadProtyle(protyle); event.preventDefault(); event.stopPropagation(); return true; diff --git a/app/src/search/spread.ts b/app/src/search/spread.ts index fb9eeb40d..826886492 100644 --- a/app/src/search/spread.ts +++ b/app/src/search/spread.ts @@ -11,6 +11,7 @@ import {addLoading} from "../protyle/ui/initUI"; import {getAllModels} from "../layout/getAll"; import {showMessage} from "../dialog/message"; import {focusByRange} from "../protyle/util/selection"; +import {reloadProtyle} from "../protyle/util/reload"; let protyle: Protyle; export const openSearch = async (hotkey: string, key?: string, notebookId?: string, searchPath?: string) => { @@ -536,7 +537,7 @@ export const openSearch = async (hotkey: string, key?: string, notebookId?: stri } getAllModels().editor.forEach(item => { if (rootIds[0] === item.editor.protyle.block.rootID) { - item.editor.reload(); + reloadProtyle(item.editor.protyle) } }); if (!currentList.nextElementSibling && searchPanelElement.children[0]) {