diff --git a/app/src/protyle/wysiwyg/remove.ts b/app/src/protyle/wysiwyg/remove.ts index 28900f91b..22c4d7266 100644 --- a/app/src/protyle/wysiwyg/remove.ts +++ b/app/src/protyle/wysiwyg/remove.ts @@ -19,7 +19,7 @@ import {Constants} from "../../constants"; import {scrollCenter} from "../../util/highlightById"; import {isMobile} from "../../util/functions"; import {mathRender} from "../render/mathRender"; -import {hasClosestByClassName} from "../util/hasClosest"; +import {hasClosestBlock, hasClosestByClassName} from "../util/hasClosest"; import {getInstanceById} from "../../layout/util"; import {Tab} from "../../layout/Tab"; import {Backlink} from "../../layout/dock/Backlink"; @@ -131,7 +131,7 @@ export const removeBlock = async (protyle: IProtyle, blockElement: Element, rang data, id, previousID, - parentID: topElement.parentElement.getAttribute("data-node-id") || protyle.block.parentID + parentID: (hasClosestBlock(topElement.parentElement) as HTMLElement)?.getAttribute("data-node-id") || protyle.block.parentID }); if (topElement.getAttribute("data-subtype") === "o" && topElement.classList.contains("li")) { listElement = topElement.parentElement; @@ -240,7 +240,8 @@ export const removeBlock = async (protyle: IProtyle, blockElement: Element, rang } const blockParentElement = isCallout ? blockElement.parentElement.parentElement : blockElement.parentElement; blockParentElement.insertAdjacentElement("beforebegin", blockElement); - if (isCallout ? blockParentElement.querySelector(".callout-content").childElementCount === 0:blockParentElement.childElementCount === 1 ) { + if (isCallout ? blockParentElement.querySelector(".callout-content").childElementCount === 0 : + blockParentElement.childElementCount === 1) { transaction(protyle, [{ action: "move", id: blockElement.getAttribute("data-node-id"), @@ -352,7 +353,7 @@ export const removeBlock = async (protyle: IProtyle, blockElement: Element, rang return; } - const parentElement = blockElement.parentElement; + const parentElement = hasClosestBlock(blockElement.parentElement); const editableElement = getContenteditableElement(blockElement); const previousLastElement = getLastBlock(previousElement) as HTMLElement; if (range.toString() === "" && isMobile() && previousLastElement && previousLastElement.classList.contains("hr") && getSelectionOffset(editableElement).start === 0) { @@ -394,7 +395,7 @@ export const removeBlock = async (protyle: IProtyle, blockElement: Element, rang }]; blockElement.remove(); // 取消超级块 - if (parentElement.getAttribute("data-type") === "NodeSuperBlock" && parentElement.childElementCount === 2) { + if (parentElement && parentElement.getAttribute("data-type") === "NodeSuperBlock" && parentElement.childElementCount === 2) { const sbData = await cancelSB(protyle, parentElement); transaction(protyle, doOperations.concat(sbData.doOperations), sbData.undoOperations.concat(undoOperations)); } else { @@ -427,7 +428,7 @@ export const removeBlock = async (protyle: IProtyle, blockElement: Element, rang id: removeId, // 不能使用 previousLastElement,否则在超级块下的元素前删除撤销错误 previousID: blockElement.previousElementSibling?.getAttribute("data-node-id"), - parentID: parentElement.getAttribute("data-node-id") + parentID: parentElement ? parentElement.getAttribute("data-node-id") : protyle.block.parentID }]; const doOperations: IOperation[] = [{ action: "delete", @@ -502,7 +503,7 @@ export const removeBlock = async (protyle: IProtyle, blockElement: Element, rang id: previousId, }); } - if (parentElement.getAttribute("data-type") === "NodeSuperBlock" && parentElement.childElementCount === 2) { + if (parentElement && parentElement.getAttribute("data-type") === "NodeSuperBlock" && parentElement.childElementCount === 2) { const sbData = await cancelSB(protyle, parentElement); transaction(protyle, doOperations.concat(sbData.doOperations), sbData.undoOperations.concat(undoOperations)); } else {