From 3e883204b101a5d520d5381e3fea16d43f9e0d76 Mon Sep 17 00:00:00 2001 From: Vanessa Date: Fri, 21 Mar 2025 14:09:08 +0800 Subject: [PATCH] :art: https://github.com/siyuan-note/siyuan/issues/14413 --- app/src/protyle/wysiwyg/enter.ts | 24 ++++++++---------------- app/src/protyle/wysiwyg/getBlock.ts | 2 +- app/src/protyle/wysiwyg/keydown.ts | 5 ++++- app/src/protyle/wysiwyg/remove.ts | 2 ++ 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/app/src/protyle/wysiwyg/enter.ts b/app/src/protyle/wysiwyg/enter.ts index f3a25bbd2..8d66a39da 100644 --- a/app/src/protyle/wysiwyg/enter.ts +++ b/app/src/protyle/wysiwyg/enter.ts @@ -489,20 +489,14 @@ const listEnter = (protyle: IProtyle, blockElement: HTMLElement, range: Range) = } range.setEndAfter(editableElement.lastChild); newElement = genListItemElement(listItemElement, 0, false); - const selectNode = range.extractContents(); - if (selectNode.firstChild.nodeType !== 3 && selectNode.firstChild.textContent === "") { - // 回车移除空元素 https://github.com/siyuan-note/insider/issues/480 - selectNode.firstChild.after(document.createElement("wbr")); - selectNode.firstChild.remove(); - } - if (selectNode.textContent === Constants.ZWSP) { - // https://github.com/siyuan-note/siyuan/issues/12273 - selectNode.childNodes.forEach(item => { - if (item.nodeType === 3 && item.textContent === Constants.ZWSP) { - item.remove(); - } - }); - } + const newEditableElement = getContenteditableElement(newElement); + newEditableElement.appendChild(range.extractContents()); + // 回车移除空元素 https://github.com/siyuan-note/insider/issues/480 + // https://github.com/siyuan-note/siyuan/issues/12273 + // 文字和图片中间回车后图片前需添加 zwsp + newEditableElement.parentElement.outerHTML = protyle.lute.SpinBlockDOM(newEditableElement.parentElement.outerHTML); + listItemElement.insertAdjacentElement("afterend", newElement); + mathRender(newElement); // https://github.com/siyuan-note/siyuan/issues/3850 // https://github.com/siyuan-note/siyuan/issues/6018 if ((editableElement?.lastElementChild?.getAttribute("data-type") || "").indexOf("inline-math") > -1 && @@ -513,8 +507,6 @@ const listEnter = (protyle: IProtyle, blockElement: HTMLElement, range: Range) = if (editableElement?.lastElementChild?.classList.contains("img") && !hasNextSibling(editableElement?.lastElementChild)) { editableElement.insertAdjacentText("beforeend", Constants.ZWSP); } - getContenteditableElement(newElement).appendChild(selectNode); - listItemElement.insertAdjacentElement("afterend", newElement); if (listItemElement.getAttribute("data-subtype") === "o") { updateListOrder(listItemElement.parentElement); } diff --git a/app/src/protyle/wysiwyg/getBlock.ts b/app/src/protyle/wysiwyg/getBlock.ts index 559792e46..df46bfd3d 100644 --- a/app/src/protyle/wysiwyg/getBlock.ts +++ b/app/src/protyle/wysiwyg/getBlock.ts @@ -172,7 +172,7 @@ export const isEndOfBlock = (range: Range) => { } let nextSibling = range.endContainer; - if (range.endContainer.nodeType !== 3 && range.endContainer.childNodes[range.endOffset]) { + if (range.endContainer.nodeType !== 3) { nextSibling = range.endContainer.childNodes[range.endOffset]; } diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts index 5677040aa..5ca4b1690 100644 --- a/app/src/protyle/wysiwyg/keydown.ts +++ b/app/src/protyle/wysiwyg/keydown.ts @@ -946,7 +946,10 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => { return; } // 图片前有一个字符,在字符后删除 - if (position.start === 1 && !rangePreviousElement && rangeNextElement && rangeNextElement.nodeType === 1 && rangeNextElement.classList.contains("img")) { + if (position.start === 1 && + range.startContainer.textContent !== Constants.ZWSP && // 如果为 zwsp 需前移光标 + !rangePreviousElement && + rangeNextElement && rangeNextElement.nodeType === 1 && rangeNextElement.classList.contains("img")) { const wbrElement = document.createElement("wbr"); range.insertNode(wbrElement); const oldHTML = nodeElement.outerHTML; diff --git a/app/src/protyle/wysiwyg/remove.ts b/app/src/protyle/wysiwyg/remove.ts index 9421891e1..332daacf9 100644 --- a/app/src/protyle/wysiwyg/remove.ts +++ b/app/src/protyle/wysiwyg/remove.ts @@ -407,6 +407,8 @@ export const removeBlock = (protyle: IProtyle, blockElement: Element, range: Ran range.selectNodeContents(previousLastEditElement); range.collapse(false); range.insertNode(leftNodes); + // 图片前删除到上一个文字块时,图片前有 zwsp + previousLastElement.outerHTML = protyle.lute.SpinBlockDOM(previousLastElement.outerHTML); removeElement.remove(); // extractContents 内容过多时需要进行滚动条重置,否则位置会错位 protyle.contentElement.scrollTop = scroll;