diff --git a/app/src/protyle/util/onGet.ts b/app/src/protyle/util/onGet.ts index 82cfab087..683617de9 100644 --- a/app/src/protyle/util/onGet.ts +++ b/app/src/protyle/util/onGet.ts @@ -17,6 +17,7 @@ import {preventScroll} from "../scroll/preventScroll"; import {restoreScroll} from "../scroll/saveScroll"; import {removeLoading} from "../ui/initUI"; import {isMobile} from "../../util/functions"; +import {foldPassiveType} from "../wysiwyg/renderBacklink"; export const onGet = (data: IWebSocketData, protyle: IProtyle, action: string[] = [], scrollAttr?: IScrollAttr, renderTitle = false) => { protyle.wysiwyg.element.removeAttribute("data-top"); @@ -182,25 +183,7 @@ const setHTML = (options: { protyle.wysiwyg.element.innerHTML = options.content; } if (options.action.includes(Constants.CB_GET_BACKLINK)) { - if (protyle.wysiwyg.element.firstElementChild.classList.contains("li")) { - if (options.expand) { - const thirdLiElement = protyle.wysiwyg.element.querySelector(".li .li .li"); - if (thirdLiElement) { - thirdLiElement.setAttribute("fold", "1"); - } - } else { - protyle.wysiwyg.element.firstElementChild.setAttribute("fold", "1"); - } - } else if (protyle.wysiwyg.element.firstElementChild.getAttribute("data-type") === "NodeHeading") { - Array.from(protyle.wysiwyg.element.children).forEach((item, index) => { - if ((options.expand && index > 2) || (!options.expand && index > 1)) { - if ((options.expand && index === 3) || (!options.expand && index === 2)) { - item.insertAdjacentHTML("beforebegin", '
'); - } - item.classList.add("fn__none"); - } - }); - } + foldPassiveType(options.expand, protyle.wysiwyg.element); } processRender(protyle.wysiwyg.element); highlightRender(protyle.wysiwyg.element); diff --git a/app/src/protyle/wysiwyg/renderBacklink.ts b/app/src/protyle/wysiwyg/renderBacklink.ts index ae90943ac..b70f73c2a 100644 --- a/app/src/protyle/wysiwyg/renderBacklink.ts +++ b/app/src/protyle/wysiwyg/renderBacklink.ts @@ -27,21 +27,20 @@ export const renderBacklink = (protyle: IProtyle, backlinkData: { } }; -const setBacklinkFold = (html: string, expand: boolean) => { - const tempDom = document.createElement("template"); - tempDom.innerHTML = html; - if (tempDom.content.firstElementChild.classList.contains("li")) { +// 传递型折叠处理 +export const foldPassiveType = (expand: boolean, element: HTMLElement | DocumentFragment) => { + if (element.firstElementChild.classList.contains("li")) { if (expand) { - tempDom.content.querySelectorAll(".li .li .li").forEach(item => { + element.querySelectorAll(".li .li .li").forEach(item => { if (item.childElementCount > 3) { item.setAttribute("fold", "1"); } }); } else { - tempDom.content.firstElementChild.setAttribute("fold", "1"); + element.firstElementChild.setAttribute("fold", "1"); } - } else if (tempDom.content.firstElementChild.getAttribute("data-type") === "NodeHeading") { - Array.from(tempDom.content.children).forEach((item, index) => { + } else if (element.firstElementChild.getAttribute("data-type") === "NodeHeading") { + Array.from(element.children).forEach((item, index) => { if ((expand && index > 2) || (!expand && index > 1)) { if ((expand && index === 3) || (!expand && index === 2)) { item.insertAdjacentHTML("beforebegin", ''); @@ -50,6 +49,12 @@ const setBacklinkFold = (html: string, expand: boolean) => { } }); } +} + +const setBacklinkFold = (html: string, expand: boolean) => { + const tempDom = document.createElement("template"); + tempDom.innerHTML = html; + foldPassiveType(expand, tempDom.content); return tempDom.innerHTML; };