diff --git a/app/src/protyle/util/selection.ts b/app/src/protyle/util/selection.ts index 155035ed9..39e08f6d6 100644 --- a/app/src/protyle/util/selection.ts +++ b/app/src/protyle/util/selection.ts @@ -560,8 +560,23 @@ export const focusBlock = (element: Element, parentElement?: HTMLElement, toStar range = setFirstNodeRange(cursorElement, getEditorRange(cursorElement)); range.collapse(true); } else { + let focusHljs = false // 定位到末尾 https://github.com/siyuan-note/siyuan/issues/5982 - range = setLastNodeRange(cursorElement, getEditorRange(cursorElement)); + if (cursorElement.classList.contains("hljs")) { + // 代码块末尾定位需在 /n 之前 https://github.com/siyuan-note/siyuan/issues/9141,https://github.com/siyuan-note/siyuan/issues/9189 + let lastNode = cursorElement.lastChild + if (lastNode.textContent === "" && lastNode.nodeType === 3) { + lastNode = hasPreviousSibling(cursorElement.lastChild) as HTMLElement + } + if (lastNode && lastNode.textContent.endsWith("\n")) { + range = getEditorRange(cursorElement); + range.setStart(lastNode, lastNode.textContent.length - 1) + focusHljs = true + } + } + if (!focusHljs) { + range = setLastNodeRange(cursorElement, getEditorRange(cursorElement)); + } range.collapse(false); } focusByRange(range); diff --git a/app/src/protyle/wysiwyg/remove.ts b/app/src/protyle/wysiwyg/remove.ts index 31778f30c..99d43b0f9 100644 --- a/app/src/protyle/wysiwyg/remove.ts +++ b/app/src/protyle/wysiwyg/remove.ts @@ -385,7 +385,7 @@ export const removeBlock = (protyle: IProtyle, blockElement: Element, range: Ran const parentElement = blockElement.parentElement; const editableElement = getContenteditableElement(blockElement); - let previousLastElement = getLastBlock(previousElement) as HTMLElement; + const previousLastElement = getLastBlock(previousElement) as HTMLElement; const isSelectNode = previousLastElement && (previousLastElement.classList.contains("table") || previousLastElement.classList.contains("render-node") || previousLastElement.classList.contains("iframe") || previousLastElement.classList.contains("hr") || previousLastElement.classList.contains("code-block")); const previousId = previousLastElement.getAttribute("data-node-id"); if (isSelectNode) { @@ -411,9 +411,7 @@ export const removeBlock = (protyle: IProtyle, blockElement: Element, range: Ran } else { transaction(protyle, doOperations, undoOperations); } - // toStart 参数不能为 false, 否则 https://github.com/siyuan-note/siyuan/issues/9141 - previousLastElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${previousId}"]`); - focusBlock(previousLastElement, undefined, getContenteditableElement(previousLastElement).textContent === "\n"); + focusBlock(protyle.wysiwyg.element.querySelector(`[data-node-id="${previousId}"]`), undefined, false); } else { focusBlock(previousLastElement, undefined, false); }